From 45d0e77c9c0c0e7378fd8d40983883cdb2fa247a Mon Sep 17 00:00:00 2001 From: Ben Ling Date: Wed, 22 Oct 2025 22:27:06 +0800 Subject: [PATCH 1/5] Repro issue where we are passing in the wrong imports into deferred module upon loading --- test/other/test_split_module.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/other/test_split_module.c b/test/other/test_split_module.c index a23e41e128931..6b91bb49a88f4 100644 --- a/test/other/test_split_module.c +++ b/test/other/test_split_module.c @@ -1,5 +1,7 @@ #include #include +#include // For malloc and free +#include // For int64_t int foo() { return 42; @@ -7,4 +9,12 @@ int foo() { EMSCRIPTEN_KEEPALIVE void say_hello() { printf("Hello! answer: %d\n", foo()); + + int64_t* ptr = (int64_t*)malloc(sizeof(int64_t) * 10); + if (ptr == NULL) { + return; + } + + free(ptr); + ptr = NULL; } From f969384dda830030f4f7be7c86dccf09c8555375 Mon Sep 17 00:00:00 2001 From: Ben Ling Date: Thu, 23 Oct 2025 17:44:08 +0800 Subject: [PATCH 2/5] Attempt fix --- src/lib/libasync.js | 2 +- src/postamble.js | 1 + src/preamble.js | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/libasync.js b/src/lib/libasync.js index c79e4f6bfccbf..bb6f31f479886 100644 --- a/src/lib/libasync.js +++ b/src/lib/libasync.js @@ -517,7 +517,7 @@ addToLibrary({ _load_secondary_module: async function() { // Mark the module as loading for the wasm module (so it doesn't try to load it again). wasmExports['load_secondary_module_status'].value = 1; - var imports = {'primary': wasmExports}; + var imports = {'primary': wasmRawExports}; // Replace '.wasm' suffix with '.deferred.wasm'. var deferred = wasmBinaryFile.slice(0, -5) + '.deferred.wasm'; await instantiateAsync(null, deferred, imports); diff --git a/src/postamble.js b/src/postamble.js index 9879161660884..62dd359079f2d 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -289,6 +289,7 @@ function checkUnflushedContent() { #endif // ASSERTIONS var wasmExports; +var wasmRawExports; #if MODULARIZE == 'instance' // In MODULARIZE=instance mode we delay most of the initialization work until diff --git a/src/preamble.js b/src/preamble.js index c9002009f05d2..916328b68270d 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -514,7 +514,7 @@ var splitModuleProxyHandler = { #if RUNTIME_DEBUG dbg(`placeholder function called: ${base}`); #endif - var imports = {'primary': wasmExports}; + var imports = {'primary': wasmRawExports}; // Replace '.wasm' suffix with '.deferred.wasm'. loadSplitModule(secondaryFile, imports, base); #if RUNTIME_DEBUG @@ -734,6 +734,7 @@ function getWasmImports() { reportUndefinedSymbols(); #endif + wasmRawExports = wasmExports; #if MEMORY64 || CAN_ADDRESS_2GB wasmExports = applySignatureConversions(wasmExports); #endif From c54cf07bbea9a93d723df143c1d5d4d52c3bd987 Mon Sep 17 00:00:00 2001 From: Ben Ling Date: Fri, 24 Oct 2025 01:24:32 +0800 Subject: [PATCH 3/5] Hide changes behind `SPLIT_MODULE` macro --- src/postamble.js | 2 ++ src/preamble.js | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/postamble.js b/src/postamble.js index 62dd359079f2d..def31977cf86d 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -289,7 +289,9 @@ function checkUnflushedContent() { #endif // ASSERTIONS var wasmExports; +#if SPLIT_MODULE var wasmRawExports; +#endif #if MODULARIZE == 'instance' // In MODULARIZE=instance mode we delay most of the initialization work until diff --git a/src/preamble.js b/src/preamble.js index 916328b68270d..c86347746ee1a 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -734,7 +734,9 @@ function getWasmImports() { reportUndefinedSymbols(); #endif +#if SPLIT_MODULE wasmRawExports = wasmExports; +#endif #if MEMORY64 || CAN_ADDRESS_2GB wasmExports = applySignatureConversions(wasmExports); #endif From 568bd8a0a9d8ee5fd81410ca7a6427eee63a0779 Mon Sep 17 00:00:00 2001 From: Ben Ling Date: Fri, 24 Oct 2025 13:44:28 +0800 Subject: [PATCH 4/5] Simplify test case --- test/other/test_split_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/other/test_split_module.c b/test/other/test_split_module.c index 6b91bb49a88f4..11a8eab102220 100644 --- a/test/other/test_split_module.c +++ b/test/other/test_split_module.c @@ -10,7 +10,7 @@ int foo() { EMSCRIPTEN_KEEPALIVE void say_hello() { printf("Hello! answer: %d\n", foo()); - int64_t* ptr = (int64_t*)malloc(sizeof(int64_t) * 10); + void* ptr = malloc(10); if (ptr == NULL) { return; } From 98c625217c6c22b606cded094fbe95439894bf7b Mon Sep 17 00:00:00 2001 From: Ben Ling Date: Mon, 27 Oct 2025 13:12:11 +0800 Subject: [PATCH 5/5] Remove unnecessary imports --- test/other/test_split_module.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/other/test_split_module.c b/test/other/test_split_module.c index 11a8eab102220..4a46b391b559f 100644 --- a/test/other/test_split_module.c +++ b/test/other/test_split_module.c @@ -1,7 +1,5 @@ #include #include -#include // For malloc and free -#include // For int64_t int foo() { return 42;