Skip to content

Commit e63fe0b

Browse files
authored
Skip imports in table during RemoveImports (#2181)
This prevents RemoveImports from producing an invalid module that references functions that no longer exist.
1 parent cbc7e86 commit e63fe0b

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/passes/RemoveImports.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,19 @@ struct RemoveImports : public WalkerPass<PostWalker<RemoveImports>> {
4848
std::vector<Name> names;
4949
ModuleUtils::iterImportedFunctions(
5050
*curr, [&](Function* func) { names.push_back(func->name); });
51+
// Do not remove names referenced in a table
52+
std::set<Name> indirectNames;
53+
if (curr->table.exists) {
54+
for (auto& segment : curr->table.segments) {
55+
for (auto& name : segment.data) {
56+
indirectNames.insert(name);
57+
}
58+
}
59+
}
5160
for (auto& name : names) {
52-
curr->removeFunction(name);
61+
if (indirectNames.find(name) == indirectNames.end()) {
62+
curr->removeFunction(name);
63+
}
5364
}
5465
}
5566
};

test/passes/remove-imports.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
(type $FUNCSIG$v (func))
33
(type $FUNCSIG$i (func (result i32)))
44
(type $FUNCSIG$d (func (result f64)))
5+
(import "env" "table" (table $0 1 1 funcref))
6+
(elem (i32.const 0) $waka-sneaky)
57
(import "env" "memBase" (global $import$global0 i32))
8+
(import "somewhere" "waka-sneaky" (func $waka-sneaky))
69
(memory $0 1024 1024)
7-
(func $nada (; 0 ;) (type $FUNCSIG$v)
10+
(func $nada (; 1 ;) (type $FUNCSIG$v)
811
(nop)
912
(drop
1013
(i32.const 0)
1114
)
1215
(drop
1316
(f64.const 0)
1417
)
18+
(call_indirect (type $FUNCSIG$v)
19+
(i32.const 0)
20+
)
1521
)
1622
)

test/passes/remove-imports.wast

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
(import $waka "somewhere" "waka")
77
(import $waka-ret "somewhere" "waka-ret" (result i32))
88
(import $waka-ret-d "somewhere" "waka-ret-d" (result f64))
9+
(import $waka-sneaky "somewhere" "waka-sneaky")
910
(import "env" "memBase" (global i32))
11+
(import "env" "table" (table $table 1 1 funcref))
12+
(elem (i32.const 0) $waka-sneaky)
1013
(func $nada (type $FUNCSIG$v)
1114
(call $waka)
1215
(drop
@@ -15,5 +18,6 @@
1518
(drop
1619
(call $waka-ret-d)
1720
)
21+
(call_indirect (i32.const 0))
1822
)
1923
)

0 commit comments

Comments
 (0)