Skip to content

Commit

Permalink
Register side module dynCall helpers even when loaded with RTLD_LOCAL (
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Sep 25, 2024
1 parent 4f0ffdd commit 32801b3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
26 changes: 22 additions & 4 deletions src/library_dylink.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,22 @@ var LibraryDylink = {
return customSection;
},

#if DYNCALLS || !WASM_BIGINT
$registerDynCallSymbols: (exports) => {
for (var [sym, exp] of Object.entries(exports)) {
if (sym.startsWith('dynCall_') && !Module.hasOwnProperty(sym)) {
Module[sym] = exp;
}
}
},
#endif

// Module.symbols <- libModule.symbols (flags.global handler)
$mergeLibSymbols__deps: ['$isSymbolDefined'],
$mergeLibSymbols: (exports, libName) => {
#if DYNCALLS || !WASM_BIGINT
registerDynCallSymbols(exports);
#endif
// add symbols into global namespace TODO: weak linking etc.
for (var [sym, exp] of Object.entries(exports)) {
#if ASSERTIONS == 2
Expand Down Expand Up @@ -571,10 +584,6 @@ var LibraryDylink = {
setImport('main')
}
#endif

if (sym.startsWith('dynCall_') && !Module.hasOwnProperty(sym)) {
Module[sym] = exp;
}
}
},

Expand Down Expand Up @@ -938,6 +947,9 @@ var LibraryDylink = {
'$asyncLoad',
#if FILESYSTEM
'$preloadedWasm',
#endif
#if DYNCALLS || !WASM_BIGINT
'$registerDynCallSymbols',
#endif
],
$loadDynamicLibrary__docs: `
Expand All @@ -963,6 +975,9 @@ var LibraryDylink = {
if (localScope) {
Object.assign(localScope, dso.exports);
}
#if DYNCALLS || !WASM_BIGINT
registerDynCallSymbols(dso.exports);
#endif
} else if (!dso.global) {
// The library was previously loaded only locally but not
// we have a request with global=true.
Expand Down Expand Up @@ -1046,6 +1061,9 @@ var LibraryDylink = {
mergeLibSymbols(exports, libName);
} else if (localScope) {
Object.assign(localScope, exports);
#if DYNCALLS || !WASM_BIGINT
registerDynCallSymbols(exports);
#endif
}
dso.exports = exports;
}
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6285
6307
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13820
13896
22 changes: 21 additions & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4385,18 +4385,38 @@ def test_dylink_i64_c(self):
EMSCRIPTEN_KEEPALIVE int64_t function_ret_64(int32_t i, int32_t j, int32_t k);
''', force_c=True)

@parameterized({
'': (False,),
'rtld_local': (True,),
})
@needs_dylink
@also_with_wasm_bigint
def test_dylink_i64_invoke(self):
def test_dylink_i64_invoke(self, rtld_local):
if rtld_local:
self.set_setting('NO_AUTOLOAD_DYLIBS')
self.emcc_args.append('-DUSE_DLOPEN')
self.set_setting('DISABLE_EXCEPTION_CATCHING', 0)
self.dylink_test(r'''\
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#if USE_DLOPEN
#include <dlfcn.h>
typedef int64_t (*sidey_t)(int64_t arg);
#else
extern "C" int64_t sidey(int64_t arg);
#endif
int main(int argc, char *argv[]) {
int64_t temp = 42;
#if USE_DLOPEN
void* lib = dlopen("liblib.so", RTLD_LAZY);
assert(lib);
sidey_t sidey = (sidey_t)dlsym(lib, "sidey");
assert(sidey);
#endif
printf("got %lld\n", sidey(temp));
printf("got %lld\n", sidey(0));
return 0;
Expand Down

0 comments on commit 32801b3

Please sign in to comment.