Skip to content

Commit

Permalink
Allow taking address of EM_JS functions with dynamic linking (#18391)
Browse files Browse the repository at this point in the history
Fixes: #18370
  • Loading branch information
sbc100 authored Dec 17, 2022
1 parent f694b50 commit a742129
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
14 changes: 9 additions & 5 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ def update_settings_glue(wasm_file, metadata):
get_weak_imports(wasm_file)

settings.WASM_EXPORTS = metadata.exports + list(metadata.namedGlobals.keys())
settings.WASM_EXPORTS += list(metadata.emJsFuncs.keys())

# Store function exports so that Closure and metadce can track these even in
# -sDECLARE_ASM_MODULE_EXPORTS=0 builds.
settings.WASM_FUNCTION_EXPORTS = metadata.exports
Expand Down Expand Up @@ -328,7 +330,8 @@ def emscript(in_wasm, out_wasm, outfile_js, memfile):
with webassembly.Module(in_wasm) as module:
types = module.get_types()
for imp in module.get_imports():
import_map[imp.field] = imp
if imp.module not in ('GOT.mem', 'GOT.func'):
import_map[imp.field] = imp

for em_js_func, raw in metadata.emJsFuncs.items():
c_sig = raw.split('<::>')[0].strip('()')
Expand Down Expand Up @@ -613,10 +616,11 @@ def create_em_js(metadata):
args = args.split(',')
arg_names = [arg.split()[-1].replace("*", "") for arg in args if arg]
args = ','.join(arg_names)
func = f'function {name}({args}) {body}'
if settings.ASYNCIFY == 2 and name in metadata.emJsFuncTypes:
mangled = asmjs_mangle(name)
func = f'function {mangled}({args}) {body}'
if (settings.MAIN_MODULE or settings.ASYNCIFY == 2) and name in metadata.emJsFuncTypes:
sig = func_type_to_sig(metadata.emJsFuncTypes[name])
func = func + f'\n{name}.sig = \'{sig}\';'
func = func + f'\n{mangled}.sig = \'{sig}\';'
em_js_funcs.append(func)

return em_js_funcs
Expand Down Expand Up @@ -676,7 +680,7 @@ def create_sending(invoke_funcs, metadata):
send_items_map = {}

for name in metadata.emJsFuncs:
send_items_map[name] = name
send_items_map[name] = asmjs_mangle(name)
for name in invoke_funcs:
send_items_map[name] = name
for name in metadata.imports:
Expand Down
11 changes: 11 additions & 0 deletions test/core/test_em_js_address_taken.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <emscripten.h>

EM_JS(void, foobar, (), {console.error("foobar called");});

void(*func_ptr)() = &foobar;

int main() {
foobar();
func_ptr();
return 0;
}
2 changes: 2 additions & 0 deletions test/core/test_em_js_address_taken.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foobar called
foobar called
6 changes: 6 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,12 @@ def test_em_js_i64(self):
self.node_args += shared.node_bigint_flags()
self.do_core_test('test_em_js_i64.c')

def test_em_js_address_taken(self):
self.do_core_test('test_em_js_address_taken.c')
if self.check_dylink():
self.set_setting('MAIN_MODULE', 2)
self.do_core_test('test_em_js_address_taken.c')

def test_runtime_stacksave(self):
self.do_runf(test_file('core/test_runtime_stacksave.c'), 'success')

Expand Down

0 comments on commit a742129

Please sign in to comment.