Skip to content

Commit

Permalink
handle named globals in dlsym; enable test_dlfcn_data_and_fptr
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Apr 26, 2015
1 parent b757e8b commit 18aef0e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
12 changes: 8 additions & 4 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,6 @@ def save_settings():
requested != '_malloc': # special-case malloc, EXPORTED by default for internal use, but we bake in a trivial allocator and warn at runtime if used in ASSERTIONS
logging.warning('function requested to be exported, but not implemented: "%s"', requested)

# Add named globals
named_globals = '\n'.join(['var %s = %s;' % (k, v) for k, v in metadata['namedGlobals'].iteritems()])

asm_consts = [0]*len(metadata['asmConsts'])
for k, v in metadata['asmConsts'].iteritems():
const = v.encode('utf-8')
Expand All @@ -285,7 +282,7 @@ def save_settings():
return Runtime.asmConsts[code](%s) | 0;
}''' % (arity, ', '.join(all_args), ', '.join(args)))

pre = pre.replace('// === Body ===', '// === Body ===\n' + named_globals + '\nRuntime.asmConsts = [' + ', '.join(asm_consts) + '];\n' + '\n'.join(asm_const_funcs) + '\n')
pre = pre.replace('// === Body ===', '// === Body ===\n' + '\nRuntime.asmConsts = [' + ', '.join(asm_consts) + '];\n' + '\n'.join(asm_const_funcs) + '\n')

#if DEBUG: outfile.write('// pre\n')
outfile.write(pre)
Expand Down Expand Up @@ -677,6 +674,13 @@ def math_fix(g):
receiving += 'Module["' + name + '"] = ' + fullname + ';\n'
final_function_tables = '\n// EMSCRIPTEN_END_FUNCS\n'

if settings['RELOCATABLE']:
receiving += '''
var NAMED_GLOBALS = %s;
for (var named in NAMED_GLOBALS) NAMED_GLOBALS[named] += gb;
Module['NAMED_GLOBALS'] = NAMED_GLOBALS;
''' % json.dumps(metadata['namedGlobals'])

funcs_js = ['''
%s
Module%s = %s;
Expand Down
30 changes: 16 additions & 14 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -4138,30 +4138,32 @@ LibraryManager.library = {
dlsym: function(handle, symbol) {
// void *dlsym(void *restrict handle, const char *restrict name);
// http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html
symbol = '_' + Pointer_stringify(symbol);
symbol = Pointer_stringify(symbol);

if (!DLFCN.loadedLibs[handle]) {
DLFCN.errorMsg = 'Tried to dlsym() from an unopened handle: ' + handle;
return 0;
} else {
var lib = DLFCN.loadedLibs[handle];
// self-dlopen means that lib.module is not a superset of
// cached_functions, so check the latter first
if (lib.module.NAMED_GLOBALS.hasOwnProperty(symbol)) {
return lib.module.NAMED_GLOBALS[symbol];
}
// not a global var, must be a function
symbol = '_' + symbol;
if (lib.cached_functions.hasOwnProperty(symbol)) {
return lib.cached_functions[symbol];
}
if (!lib.module.hasOwnProperty(symbol)) {
DLFCN.errorMsg = ('Tried to lookup unknown symbol "' + symbol +
'" in dynamic lib: ' + lib.name);
return 0;
} else {
if (!lib.module.hasOwnProperty(symbol)) {
DLFCN.errorMsg = ('Tried to lookup unknown symbol "' + symbol +
'" in dynamic lib: ' + lib.name);
return 0;
} else {
var result = lib.module[symbol];
if (typeof result == 'function') {
result = Runtime.addFunction(result);
lib.cached_functions = result;
}
return result;
var result = lib.module[symbol];
if (typeof result == 'function') {
result = Runtime.addFunction(result);
lib.cached_functions = result;
}
return result;
}
}
},
Expand Down
2 changes: 0 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3130,8 +3130,6 @@ def test_dlfcn_qsort(self):
self.validate_asmjs(out)

def test_dlfcn_data_and_fptr(self):
return self.skip('todo')
if Settings.ASM_JS: return self.skip('this is not a valid case - libraries should not be able to access their parents globals willy nilly')
if not self.can_dlfcn(): return

if Building.LLVM_OPTS: return self.skip('LLVM opts will optimize out parent_func')
Expand Down

0 comments on commit 18aef0e

Please sign in to comment.