Skip to content

Commit 05f118b

Browse files
committed
Add assertion checking the number of args passed into exports.
Fixes #21348
1 parent d666035 commit 05f118b

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/preamble.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,15 @@ Module['FS_createPreloadedFile'] = FS.createPreloadedFile;
512512
#include "URIUtils.js"
513513

514514
#if ASSERTIONS
515-
function createExportWrapper(name) {
515+
function createExportWrapper(name, nargs) {
516516
return (...args) => {
517517
assert(runtimeInitialized, `native function \`${name}\` called before runtime initialization`);
518518
#if EXIT_RUNTIME
519519
assert(!runtimeExited, `native function \`${name}\` called after runtime exit (use NO_EXIT_RUNTIME to keep it alive after main() exits)`);
520520
#endif
521521
var f = wasmExports[name];
522522
assert(f, `exported native function \`${name}\` not found`);
523+
assert(args.length <= nargs, `native function \`${name}\` called with ${args.length} args but expects ${nargs}`);
523524
return f(...args);
524525
};
525526
}

test/test_other.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11985,6 +11985,20 @@ def test_native_call_after_exit(self):
1198511985
out = self.run_js('foo.js', assert_returncode=NON_ZERO)
1198611986
self.assertContained('native function `main` called after runtime exit', out)
1198711987

11988+
def test_native_call_nargs(self):
11989+
self.set_setting('ASSERTIONS')
11990+
self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_foo'])
11991+
create_file('foo.c', r'''
11992+
#include <emscripten.h>
11993+
void foo(int arg) {}
11994+
int main() {
11995+
EM_ASM(_foo(99, 100));
11996+
}
11997+
''')
11998+
self.build('foo.c')
11999+
out = self.run_js('foo.js', assert_returncode=NON_ZERO)
12000+
self.assertContained('native function `foo` called with 2 args but expects 1', out)
12001+
1198812002
def test_metadce_wasm2js_i64(self):
1198912003
# handling i64 unsigned remainder brings in some i64 support code. metadce
1199012004
# must not remove it.

tools/emscripten.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ def install_wrapper(sym):
851851
if settings.ASSERTIONS and install_wrapper(name):
852852
# With assertions enabled we create a wrapper that are calls get routed through, for
853853
# the lifetime of the program.
854-
wrapper += "createExportWrapper('%s');" % name
854+
wrapper += f"createExportWrapper('{name}', {nargs});"
855855
elif settings.WASM_ASYNC_COMPILATION:
856856
# With WASM_ASYNC_COMPILATION wrapper will replace the global var and Module var on
857857
# first use.

0 commit comments

Comments
 (0)