Skip to content

Commit fefb75e

Browse files
authored
Properly handle fastcomp *wasm* safe heap (#2334)
Properly handle fastcomp wasm safe heap: emscripten_get_sbrk_ptr is an asm.js library function, which means it is inside the wasm after asm2wasm, and exported. Find it via the export.
1 parent 849ea21 commit fefb75e

3 files changed

Lines changed: 1921 additions & 1 deletion

File tree

src/passes/SafeHeap.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace wasm {
3434

3535
static const Name DYNAMICTOP_PTR_IMPORT("DYNAMICTOP_PTR");
3636
static const Name GET_SBRK_PTR_IMPORT("emscripten_get_sbrk_ptr");
37+
static const Name GET_SBRK_PTR_EXPORT("_emscripten_get_sbrk_ptr");
3738
static const Name SBRK("sbrk");
3839
static const Name SEGFAULT_IMPORT("segfault");
3940
static const Name ALIGNFAULT_IMPORT("alignfault");
@@ -119,12 +120,16 @@ struct SafeHeap : public Pass {
119120
ImportInfo info(*module);
120121
// Older emscripten imports env.DYNAMICTOP_PTR.
121122
// Newer emscripten imports emscripten_get_sbrk_ptr(), which is later
122-
// optimized to have the number in the binary.
123+
// optimized to have the number in the binary (or in the case of fastcomp,
124+
// emscripten_get_sbrk_ptr is an asm.js library function so it is inside
125+
// the wasm, and discoverable via an export).
123126
if (auto* existing = info.getImportedGlobal(ENV, DYNAMICTOP_PTR_IMPORT)) {
124127
dynamicTopPtr = existing->name;
125128
} else if (auto* existing =
126129
info.getImportedFunction(ENV, GET_SBRK_PTR_IMPORT)) {
127130
getSbrkPtr = existing->name;
131+
} else if (auto* existing = module->getExportOrNull(GET_SBRK_PTR_EXPORT)) {
132+
getSbrkPtr = existing->value;
128133
} else if (auto* existing = info.getImportedFunction(ENV, SBRK)) {
129134
sbrk = existing->name;
130135
} else {

0 commit comments

Comments
 (0)