Skip to content

Add addAtPreRun function for code injection at preRun. #23451

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions src/jsifier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import {
ATEXITS,
ATPRERUN,
ATINITS,
ATMAINS,
defineI64Param,
Expand Down Expand Up @@ -773,6 +774,7 @@ var proxiedFunctionTable = [
warnings: warningOccured(),
asyncFuncs,
libraryDefinitions: LibraryManager.libraryDefinitions,
ATPRERUN: ATPRERUN.join('\n'),
ATINITS: ATINITS.join('\n'),
ATMAINS: STRICT ? '' : ATMAINS.join('\n'),
ATEXITS: ATEXITS.join('\n'),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/libfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ addToLibrary({
],
$FS__postset: () => {
// TODO: do we need noFSInit?
addAtInit(`
addAtPreRun(`
if (!Module['noFSInit'] && !FS.initialized)
FS.init();
FS.ignorePermissions = false;
Expand Down
10 changes: 10 additions & 0 deletions src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,14 @@ function makeEval(code) {

export const ATMAINS = [];

export const ATPRERUN = [];

// Add code that runs after the Wasm module is loaded and the runtime ATINIT
// callbacks, but before static constructors and main are run.
function addAtPreRun(code) {
ATPRERUN.push(code);
}

export const ATINITS = [];

function addAtInit(code) {
Expand Down Expand Up @@ -1085,6 +1093,7 @@ function ENVIRONMENT_IS_WORKER_THREAD() {

addToCompileTimeContext({
ATEXITS,
ATPRERUN,
ATINITS,
FOUR_GB,
LONG_TYPE,
Expand All @@ -1104,6 +1113,7 @@ addToCompileTimeContext({
ENVIRONMENT_IS_WORKER_THREAD,
addAtExit,
addAtInit,
addAtPreRun,
asyncIf,
awaitIf,
buildStringArray,
Expand Down
2 changes: 1 addition & 1 deletion src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
#endif
updateMemoryViews();
#endif

<<< ATPRERUN >>>
initRuntime(wasmExports);
#if PTHREADS
// Export Wasm module for pthread creation to access.
Expand Down
10 changes: 6 additions & 4 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function preRun() {
}
#endif
callRuntimeCallbacks(__ATPRERUN__);
<<< ATPRERUN >>>
}

function initRuntime() {
Expand Down Expand Up @@ -217,6 +218,11 @@ function initRuntime() {
#if RELOCATABLE
callRuntimeCallbacks(__RELOC_FUNCS__);
#endif

#if hasExportedSymbol('__wasm_call_ctors')
wasmExports['__wasm_call_ctors']();
#endif

<<< ATINITS >>>
callRuntimeCallbacks(__ATINIT__);
}
Expand Down Expand Up @@ -954,10 +960,6 @@ function getWasmImports() {
#endif
#endif

#if hasExportedSymbol('__wasm_call_ctors')
addOnInit(wasmExports['__wasm_call_ctors']);
#endif

#if hasExportedSymbol('__wasm_apply_data_relocs')
__RELOC_FUNCS__.push(wasmExports['__wasm_apply_data_relocs']);
#endif
Expand Down
24 changes: 24 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -8431,6 +8431,30 @@ def test_binaryen_stack_ir(self, opts, disable_and_enable):
self.assertLess(err.count(DISABLE), 2)
self.assertLess(err.count(ENABLE), 2)

@also_with_minimal_runtime
def test_run_order(self):
create_file('lib.js', r'''
addToLibrary({
foo__postset: () => {
addAtPreRun("console.log(`addAtPreRun`);");
addAtInit("console.log(`addAtInit`);");
},
foo: () => {},
});
''')
create_file('src.c', r'''
#include <stdio.h>
void foo();
__attribute__((constructor)) void ctor() {
printf("ctor\n");
}
int main() {
printf("main\n");
foo();
}
''')
self.do_runf('src.c', 'addAtPreRun\nctor\naddAtInit\nmain\n', emcc_args=['--js-library', 'lib.js'])

def test_override_js_execution_environment(self):
create_file('main.c', r'''
#include <emscripten.h>
Expand Down
5 changes: 1 addition & 4 deletions tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,7 @@ def acorn_optimizer(filename, passes, extra_info=None, return_output=False, work
# evals ctors. if binaryen_bin is provided, it is the dir of the binaryen tool
# for this, and we are in wasm mode
def eval_ctors(js_file, wasm_file, debug_info):
if settings.MINIMAL_RUNTIME:
CTOR_ADD_PATTERN = f"wasmExports['{WASM_CALL_CTORS}']();" # TODO test
else:
CTOR_ADD_PATTERN = f"addOnInit(wasmExports['{WASM_CALL_CTORS}']);"
CTOR_ADD_PATTERN = f"wasmExports['{WASM_CALL_CTORS}']();"

js = utils.read_file(js_file)

Expand Down
1 change: 1 addition & 0 deletions tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def update_settings_glue(wasm_file, metadata, base_metadata):


def apply_static_code_hooks(forwarded_json, code):
code = shared.do_replace(code, '<<< ATPRERUN >>>', str(forwarded_json['ATPRERUN']))
code = shared.do_replace(code, '<<< ATINITS >>>', str(forwarded_json['ATINITS']))
if settings.HAS_MAIN:
code = shared.do_replace(code, '<<< ATMAINS >>>', str(forwarded_json['ATMAINS']))
Expand Down
Loading