Skip to content

Commit a8c0a50

Browse files
authored
Fix SafeHeap crash when start function calls an imported function (#8306)
`findCalledFunctions` in SafeHeap transitively walks all functions called from the start function to determine which functions should not be instrumented. When the start function calls an imported function, the import was added to the worklist and then `FindAll<Call>` was called on its null body, causing an assertion failure in the walker. Fix: Skip imported functions in the traversal since they have no body to walk.
1 parent fc44a75 commit a8c0a50

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/passes/SafeHeap.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ static std::set<Name> findCalledFunctions(Module* module, Name startFunc) {
139139
auto next = toVisit.back();
140140
toVisit.pop_back();
141141
auto* func = module->getFunction(next);
142+
if (func->imported()) {
143+
continue;
144+
}
142145
for (auto* call : FindAll<Call>(func->body).list) {
143146
addFunction(call->target);
144147
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
2+
;; RUN: wasm-opt %s --safe-heap -S -o - | filecheck %s
3+
4+
;; Test that safe-heap does not crash when the start function calls an imported
5+
;; function. The findCalledFunctions helper transitively walks all called
6+
;; functions from the start, and must skip imported functions which have no body.
7+
8+
(module
9+
;; CHECK: (import "env" "some_import" (func $import))
10+
(import "env" "some_import" (func $import))
11+
(memory 1 1)
12+
13+
;; CHECK: (start $start)
14+
(start $start)
15+
16+
;; CHECK: (func $start
17+
;; CHECK-NEXT: (call $import)
18+
;; CHECK-NEXT: )
19+
(func $start
20+
;; The start function calls an imported function. Previously this would
21+
;; crash because findCalledFunctions would try to walk the null body of
22+
;; the imported function.
23+
(call $import)
24+
)
25+
)

0 commit comments

Comments
 (0)