Skip to content

Commit

Permalink
Fix test_dlfcn_self test under wasm and wasm backend (emscripten-core…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored and VirtualTim committed May 21, 2019
1 parent 0fd80a5 commit 1a85480
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
31 changes: 26 additions & 5 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -2152,16 +2152,37 @@ LibraryManager.library = {
}

var lib = LDSO.loadedLibs[handle];
#if !WASM_BACKEND
symbol = '_' + symbol;
var isMainModule = lib.module == Module;

var mangled = '_' + symbol;
var modSymbol = mangled;
#if WASM_BACKEND
if (!isMainModule) {
modSymbol = symbol;
}
#endif
if (!lib.module.hasOwnProperty(symbol)) {
DLFCN.errorMsg = ('Tried to lookup unknown symbol "' + symbol +

if (!lib.module.hasOwnProperty(modSymbol)) {
DLFCN.errorMsg = ('Tried to lookup unknown symbol "' + modSymbol +
'" in dynamic lib: ' + lib.name);
return 0;
}

var result = lib.module[symbol];
var result = lib.module[modSymbol];
#if WASM
// Attempt to get the real "unwrapped" symbol so we have more chance of
// getting wasm function which can be added to a table.
if (isMainModule) {
#if WASM_BACKEND
var asmSymbol = symbol;
#else
var asmSymbol = mangled;
#endif
if (lib.module["asm"][asmSymbol]) {
result = lib.module["asm"][asmSymbol];
}
}
#endif
if (typeof result !== 'function')
return result;

Expand Down
4 changes: 4 additions & 0 deletions tests/core/test_dlfcn_self.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ EMSCRIPTEN_KEEPALIVE void repeatable() {
void* self = dlopen(NULL, RTLD_LAZY);
int* global_ptr = (int*)dlsym(self, "global");
void (*foo_ptr)(int) = (void (*)(int))dlsym(self, "foo");
if (!foo_ptr) {
printf("dlsym failed: %s\n", dlerror());
return;
}
foo_ptr(*global_ptr);
dlclose(self);
}
Expand Down
24 changes: 14 additions & 10 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2765,23 +2765,27 @@ def test_dlfcn_alignment_and_zeroing(self):
'''
self.do_run(src, 'success.\n')

@no_wasm('needs to be able to add JS functions to the wasm table')
@needs_dlfcn
def test_dlfcn_self(self):
self.set_setting('MAIN_MODULE')
self.set_setting('EXPORT_ALL')

def post(filename):
with open(filename) as f:
for line in f:
if 'var NAMED_GLOBALS' in line:
table = line
break
else:
self.fail('Could not find symbol table!')
table = table[table.find('{'):table.find('}') + 1]
js = open(filename).read()
start = js.find('var NAMED_GLOBALS')
first = js.find('{', start)
last = js.find('}', start)
exports = js[first + 1:last]
exports = exports.split(',')
# ensure there aren't too many globals; we don't want unnamed_addr
assert table.count(',') <= 30, table.count(',')
exports = [e.split(':')[0].strip('"') for e in exports]
exports.sort()
self.assertGreater(len(exports), 20)
# wasm backend includes alias in NAMED_GLOBALS
if self.is_wasm_backend():
self.assertLess(len(exports), 43)
else:
self.assertLess(len(exports), 30)

self.do_run_in_out_file_test('tests', 'core', 'test_dlfcn_self', post_build=post)

Expand Down

0 comments on commit 1a85480

Please sign in to comment.