diff --git a/src/passes/SafeHeap.cpp b/src/passes/SafeHeap.cpp index 7ac077d7501..f81dc60bcbc 100644 --- a/src/passes/SafeHeap.cpp +++ b/src/passes/SafeHeap.cpp @@ -139,6 +139,9 @@ static std::set findCalledFunctions(Module* module, Name startFunc) { auto next = toVisit.back(); toVisit.pop_back(); auto* func = module->getFunction(next); + if (func->imported()) { + continue; + } for (auto* call : FindAll(func->body).list) { addFunction(call->target); } diff --git a/test/lit/passes/safe-heap-start-import.wast b/test/lit/passes/safe-heap-start-import.wast new file mode 100644 index 00000000000..1d32bf251bb --- /dev/null +++ b/test/lit/passes/safe-heap-start-import.wast @@ -0,0 +1,28 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. +;; RUN: wasm-opt %s --safe-heap --enable-threads --enable-simd -S -o - | filecheck %s + +;; Test that safe-heap does not crash when the start function calls an imported +;; function. The findCalledFunctions helper transitively walks all called +;; functions from the start, and must skip imported functions which have no body. + +(module + ;; CHECK: (import "env" "some_import" (func $import)) + (import "env" "some_import" (func $import)) + ;; CHECK: (import "env" "emscripten_get_sbrk_ptr" (func $emscripten_get_sbrk_ptr (result i32))) + (import "env" "emscripten_get_sbrk_ptr" (func $emscripten_get_sbrk_ptr (result i32))) + (memory 1 1 shared) + + ;; CHECK: (start $start) + + ;; CHECK: (func $start + ;; CHECK-NEXT: (call $import) + ;; CHECK-NEXT: ) + (func $start + ;; The start function calls an imported function. Previously this would + ;; crash because findCalledFunctions would try to walk the null body of + ;; the imported function. + (call $import) + ) + + (start $start) +)