summaryrefslogtreecommitdiff
path: root/src/tools
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/fuzzing.h25
-rw-r--r--src/tools/wasm-metadce.cpp34
2 files changed, 57 insertions, 2 deletions
diff --git a/src/tools/fuzzing.h b/src/tools/fuzzing.h
index f7dd20e76..0a7128e3e 100644
--- a/src/tools/fuzzing.h
+++ b/src/tools/fuzzing.h
@@ -197,6 +197,9 @@ public:
}
setupTable();
setupGlobals();
+ if (wasm.features.hasExceptionHandling()) {
+ setupEvents();
+ }
addImportLoggingSupport();
// keep adding functions until we run out of input
while (!finishedInput) {
@@ -397,6 +400,28 @@ private:
}
}
+ void setupEvents() {
+ Index num = upTo(3);
+ for (size_t i = 0; i < num; i++) {
+ // Events should have void return type and at least one param type
+ Type type = pick(i32, i64, f32, f64);
+ std::string sig = std::string("v") + getSig(type);
+ std::vector<Type> params;
+ params.push_back(type);
+ Index numValues = upToSquared(MAX_PARAMS - 1);
+ for (Index i = 0; i < numValues; i++) {
+ type = pick(i32, i64, f32, f64);
+ sig += getSig(type);
+ params.push_back(type);
+ }
+ auto* event = builder.makeEvent(std::string("event$") + std::to_string(i),
+ WASM_EVENT_ATTRIBUTE_EXCEPTION,
+ ensureFunctionType(sig, &wasm)->name,
+ std::move(params));
+ wasm.addEvent(event);
+ }
+ }
+
void finalizeTable() {
wasm.table.initial = wasm.table.segments[0].data.size();
wasm.table.max =
diff --git a/src/tools/wasm-metadce.cpp b/src/tools/wasm-metadce.cpp
index c04286f59..96b368bc4 100644
--- a/src/tools/wasm-metadce.cpp
+++ b/src/tools/wasm-metadce.cpp
@@ -55,10 +55,12 @@ struct MetaDCEGraph {
std::unordered_map<Name, Name> exportToDCENode;
std::unordered_map<Name, Name> functionToDCENode; // function name => DCE name
std::unordered_map<Name, Name> globalToDCENode; // global name => DCE name
+ std::unordered_map<Name, Name> eventToDCENode; // event name => DCE name
std::unordered_map<Name, Name> DCENodeToExport; // reverse maps
std::unordered_map<Name, Name> DCENodeToFunction;
std::unordered_map<Name, Name> DCENodeToGlobal;
+ std::unordered_map<Name, Name> DCENodeToEvent;
// imports are not mapped 1:1 to DCE nodes in the wasm, since env.X might
// be imported twice, for example. So we don't map a DCE node to an Import,
@@ -80,6 +82,11 @@ struct MetaDCEGraph {
return getImportId(imp->module, imp->base);
}
+ ImportId getEventImportId(Name name) {
+ auto* imp = wasm.getEvent(name);
+ return getImportId(imp->module, imp->base);
+ }
+
// import module.base => DCE name
std::unordered_map<Name, Name> importIdToDCENode;
@@ -106,8 +113,14 @@ struct MetaDCEGraph {
globalToDCENode[global->name] = dceName;
nodes[dceName] = DCENode(dceName);
});
- // only process function and global imports - the table and memory are
- // always there
+ ModuleUtils::iterDefinedEvents(wasm, [&](Event* event) {
+ auto dceName = getName("event", event->name.str);
+ DCENodeToEvent[dceName] = event->name;
+ eventToDCENode[event->name] = dceName;
+ nodes[dceName] = DCENode(dceName);
+ });
+ // only process function, global, and event imports - the table and memory
+ // are always there
ModuleUtils::iterImportedFunctions(wasm, [&](Function* import) {
auto id = getImportId(import->module, import->base);
if (importIdToDCENode.find(id) == importIdToDCENode.end()) {
@@ -122,6 +135,13 @@ struct MetaDCEGraph {
importIdToDCENode[id] = dceName;
}
});
+ ModuleUtils::iterImportedEvents(wasm, [&](Event* import) {
+ auto id = getImportId(import->module, import->base);
+ if (importIdToDCENode.find(id) == importIdToDCENode.end()) {
+ auto dceName = getName("importId", import->name.str);
+ importIdToDCENode[id] = dceName;
+ }
+ });
for (auto& exp : wasm.exports) {
if (exportToDCENode.find(exp->name) == exportToDCENode.end()) {
auto dceName = getName("export", exp->name.str);
@@ -145,6 +165,13 @@ struct MetaDCEGraph {
node.reaches.push_back(
importIdToDCENode[getGlobalImportId(exp->value)]);
}
+ } else if (exp->kind == ExternalKind::Event) {
+ if (!wasm.getEvent(exp->value)->imported()) {
+ node.reaches.push_back(eventToDCENode[exp->value]);
+ } else {
+ node.reaches.push_back(
+ importIdToDCENode[getEventImportId(exp->value)]);
+ }
}
}
// Add initializer dependencies
@@ -355,6 +382,9 @@ public:
if (DCENodeToGlobal.find(name) != DCENodeToGlobal.end()) {
std::cout << " is global " << DCENodeToGlobal[name] << '\n';
}
+ if (DCENodeToEvent.find(name) != DCENodeToEvent.end()) {
+ std::cout << " is event " << DCENodeToEvent[name] << '\n';
+ }
for (auto target : node.reaches) {
std::cout << " reaches: " << target.str << '\n';
}