Skip to content

Commit 79bf3e0

Browse files
authored
[SPLIT_MODULE] Fix imports into deferred module when loading (#25621)
Fix an error when attempted to reserve a new memory space in the deferred module when building with MEMORY64 + SPLIT_MODULE. ``` TypeError: Cannot convert <func ptr> to a BigInt ``` In a mem64 build, we call applySignatureConversions on `malloc()` in the wasmExports. `applySignatureConversions()` wraps the exports and converts the return value to a javascript number. The wasmExports is passed into the deferred module during instantiation so when we call a malloc in the deferred module, we'd be calling the wrapped function, hitting into this error. Furthermore, we are making a direct call when calling a malloc, so strictly speaking, we should not need to go through the JS trampoline.
1 parent 47b155d commit 79bf3e0

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

src/lib/libasync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ addToLibrary({
523523
_load_secondary_module: async function() {
524524
// Mark the module as loading for the wasm module (so it doesn't try to load it again).
525525
wasmExports['load_secondary_module_status'].value = 1;
526-
var imports = {'primary': wasmExports};
526+
var imports = {'primary': wasmRawExports};
527527
// Replace '.wasm' suffix with '.deferred.wasm'.
528528
var deferred = wasmBinaryFile.slice(0, -5) + '.deferred.wasm';
529529
await instantiateAsync(null, deferred, imports);

src/postamble.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ function checkUnflushedContent() {
289289
#endif // ASSERTIONS
290290

291291
var wasmExports;
292+
#if SPLIT_MODULE
293+
var wasmRawExports;
294+
#endif
292295

293296
#if MODULARIZE == 'instance'
294297
// In MODULARIZE=instance mode we delay most of the initialization work until

src/preamble.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ var splitModuleProxyHandler = {
514514
#if RUNTIME_DEBUG
515515
dbg(`placeholder function called: ${base}`);
516516
#endif
517-
var imports = {'primary': wasmExports};
517+
var imports = {'primary': wasmRawExports};
518518
// Replace '.wasm' suffix with '.deferred.wasm'.
519519
loadSplitModule(secondaryFile, imports, base);
520520
#if RUNTIME_DEBUG
@@ -734,6 +734,9 @@ function getWasmImports() {
734734
reportUndefinedSymbols();
735735
#endif
736736

737+
#if SPLIT_MODULE
738+
wasmRawExports = wasmExports;
739+
#endif
737740
#if MEMORY64 || CAN_ADDRESS_2GB
738741
wasmExports = applySignatureConversions(wasmExports);
739742
#endif

test/other/test_split_module.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@ int foo() {
77

88
EMSCRIPTEN_KEEPALIVE void say_hello() {
99
printf("Hello! answer: %d\n", foo());
10+
11+
void* ptr = malloc(10);
12+
if (ptr == NULL) {
13+
return;
14+
}
15+
16+
free(ptr);
17+
ptr = NULL;
1018
}

0 commit comments

Comments
 (0)