Skip to content

Detect and report late assignment of Module API elements #23629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ function run() {
#endif
#if expectToReceiveOnModule('onRuntimeInitialized')
Module['onRuntimeInitialized']?.();
#if ASSERTIONS
consumedModuleProp('onRuntimeInitialized');
#endif
#endif

#if HAS_MAIN
Expand Down Expand Up @@ -297,6 +300,9 @@ if (Module['preInit']) {
Module['preInit'].pop()();
}
}
#if ASSERTIONS
consumedModuleProp('preInit');
#endif
#endif

run();
Expand Down
6 changes: 6 additions & 0 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ function preRun() {
addOnPreRun(Module['preRun'].shift());
}
}
#if ASSERTIONS
consumedModuleProp('preRun');
#endif
#endif
<<< ATPRERUNS >>>
}
Expand Down Expand Up @@ -276,6 +279,9 @@ function postRun() {
addOnPostRun(Module['postRun'].shift());
}
}
#if ASSERTIONS
consumedModuleProp('postRun');
#endif
#endif

<<< ATPOSTRUNS >>>
Expand Down
12 changes: 12 additions & 0 deletions src/runtime_debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ function legacyModuleProp(prop, newName, incoming=true) {
}
}

function consumedModuleProp(prop) {
if (!Object.getOwnPropertyDescriptor(Module, prop)) {
Object.defineProperty(Module, prop, {
configurable: true,
set() {
abort(`Attempt to set \`Module.${prop}\` after it has already been processed. This can happen, for example, when code is injected via '--post-js' rather than '--pre-js'`);

}
});
}
}

function ignoredModuleProp(prop) {
if (Object.getOwnPropertyDescriptor(Module, prop)) {
abort(`\`Module.${prop}\` was supplied but \`${prop}\` not included in INCOMING_MODULE_JS_API`);
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7872
7985
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20962
21341
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51969
52492
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50401
50780
9 changes: 9 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -15607,3 +15607,12 @@ def test_instantiate_wasm(self):
return {}; // Compiling asynchronously, no exports.
}''')
self.do_runf('test_manual_wasm_instantiate.c', emcc_args=['--pre-js=pre.js'])

def test_late_module_api_assignment(self):
# When sync instantiation is used (or when async/await is used in MODULARIZE mode) certain
# Module properties cannot be assigned in `--post-js` code because its too late by the time
# it runs.
for prop in ('onRuntimeInitialized', 'postRun', 'preRun', 'preInit'):
create_file('post.js', f'Module.{prop} = () => console.log("will never fire since assigned too late")')
expected = f"Aborted(Attempt to set `Module.{prop}` after it has already been processed. This can happen, for example, when code is injected via '--post-js' rather than '--pre-js')"
self.do_runf(test_file('hello_world.c'), expected, emcc_args=['--post-js=post.js', '-sWASM_ASYNC_COMPILATION=0'], assert_returncode=NON_ZERO)