Skip to content

Automatically convert returned pointers to unsigned in CAN_ADDRESS_GB mode. #19755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,8 @@ def create_module(receiving, metadata, library_symbols):
module.append(create_invoke_wrappers(metadata))
else:
assert not metadata.invokeFuncs, "invoke_ functions exported but exceptions and longjmp are both disabled"
if settings.MEMORY64:
module.append(create_wasm64_wrappers(metadata))
if settings.MEMORY64 or settings.CAN_ADDRESS_2GB:
module.append(create_pointer_conversion_wrappers(metadata))
return module


Expand All @@ -852,7 +852,7 @@ def create_invoke_wrappers(metadata):
return invoke_wrappers


def create_wasm64_wrappers(metadata):
def create_pointer_conversion_wrappers(metadata):
# TODO(sbc): Move this into somewhere less static. Maybe it can become
# part of library.js file, even though this metadata relates specifically
# to native (non-JS) functions.
Expand Down Expand Up @@ -902,26 +902,34 @@ def create_wasm64_wrappers(metadata):
'emscripten_wasm_worker_initialize': '_p_',
}

wasm64_wrappers = '''
function instrumentWasmExportsForMemory64(exports) {
wrappers = '''
function applySignatureConversions(exports) {
// First, make a copy of the incoming exports object
exports = Object.assign({}, exports);'''
exports = Object.assign({}, exports);
'''

sigs_seen = set()
wrap_functions = []
for exp in metadata.exports:
sig = mapping.get(exp)
for symbol in metadata.exports:
sig = mapping.get(symbol)
if sig:
if sig not in sigs_seen:
sigs_seen.add(sig)
wasm64_wrappers += js_manipulation.make_wasm64_wrapper(sig)
wrap_functions.append(exp)
if settings.MEMORY64:
if sig not in sigs_seen:
wrappers += js_manipulation.make_wasm64_wrapper(sig)
sigs_seen.add(sig)
wrap_functions.append(symbol)
elif sig[0] == 'p':
if sig not in sigs_seen:
wrappers += js_manipulation.make_unsign_pointer_wrapper(sig)
sigs_seen.add(sig)
wrap_functions.append(symbol)

for f in wrap_functions:
sig = mapping[f]
wasm64_wrappers += f"\n exports['{f}'] = wasm64Wrapper_{sig}(exports['{f}']);"
wasm64_wrappers += '\n return exports\n}'
return wasm64_wrappers
wrappers += f"\n exports['{f}'] = makeWrapper_{sig}(exports['{f}']);"
wrappers += '\n return exports\n}'

return wrappers


def run(in_wasm, out_wasm, outfile_js, memfile, js_syms):
Expand Down
6 changes: 0 additions & 6 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,6 @@ var LibraryEmbind = {
// assumes 4-byte alignment
var base = _malloc({{{ POINTER_SIZE }}} + length + 1);
var ptr = base + {{{ POINTER_SIZE }}};
#if CAN_ADDRESS_2GB
ptr >>>= 0;
#endif
{{{ makeSetValue('base', '0', 'length', SIZE_TYPE) }}};
if (stdStringIsUTF8 && valueIsOfTypeString) {
stringToUTF8(value, ptr, length + 1);
Expand Down Expand Up @@ -810,9 +807,6 @@ var LibraryEmbind = {
// assumes 4-byte alignment
var length = lengthBytesUTF(value);
var ptr = _malloc(4 + length + charSize);
#if CAN_ADDRESS_2GB
ptr >>>= 0;
#endif
HEAPU32[ptr >> 2] = length >> shift;

encodeString(value, ptr + 4, length + charSize);
Expand Down
4 changes: 2 additions & 2 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ mergeInto(LibraryManager.library, {
emscripten_resize_heap: (requestedSize) => {
var oldSize = HEAPU8.length;
#if MEMORY64 != 1
requestedSize = requestedSize >>> 0;
requestedSize >>>= 0;
#endif
#if ALLOW_MEMORY_GROWTH == 0
#if ABORTING_MALLOC
Expand Down Expand Up @@ -3090,7 +3090,7 @@ mergeInto(LibraryManager.library, {
// Used by wasm-emscripten-finalize to implement STACK_OVERFLOW_CHECK
__handle_stack_overflow__deps: ['emscripten_stack_get_base', 'emscripten_stack_get_end', '$ptrToString'],
__handle_stack_overflow: (requested) => {
requested = requested >>> 0;
requested >>>= 0;
var base = _emscripten_stack_get_base();
var end = _emscripten_stack_get_end();
abort(`stack overflow (Attempt to set SP to ${ptrToString(requested)}` +
Expand Down
6 changes: 3 additions & 3 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,7 @@ var LibraryWebGPU = {

if (size === 0) warnOnce('getMappedRange size=0 no longer means WGPU_WHOLE_MAP_SIZE');

size = size >>> 0;
size >>>= 0;
if (size === {{{ gpu.WHOLE_MAP_SIZE }}}) size = undefined;

var mapped;
Expand Down Expand Up @@ -1875,7 +1875,7 @@ var LibraryWebGPU = {

if (size === 0) warnOnce('getMappedRange size=0 no longer means WGPU_WHOLE_MAP_SIZE');

size = size >>> 0;
size >>>= 0;
if (size === {{{ gpu.WHOLE_MAP_SIZE }}}) size = undefined;

if (bufferWrapper.mapMode !== {{{ gpu.MapMode.Write }}}) {
Expand Down Expand Up @@ -1916,7 +1916,7 @@ var LibraryWebGPU = {
bufferWrapper.onUnmap = [];
var buffer = bufferWrapper.object;

size = size >>> 0;
size >>>= 0;
if (size === {{{ gpu.WHOLE_MAP_SIZE }}}) size = undefined;

// `callback` takes (WGPUBufferMapAsyncStatus status, void * userdata)
Expand Down
4 changes: 2 additions & 2 deletions src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
asm = output.instance.exports;
#endif

#if MEMORY64
asm = instrumentWasmExportsForMemory64(asm);
#if MEMORY64 || CAN_ADDRESS_2GB
asm = applySignatureConversions(asm);
#endif

#if USE_OFFSET_CONVERTER
Expand Down
4 changes: 2 additions & 2 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,8 +991,8 @@ function createWasm() {
reportUndefinedSymbols();
#endif

#if MEMORY64
exports = instrumentWasmExportsForMemory64(exports);
#if MEMORY64 || CAN_ADDRESS_2GB
exports = applySignatureConversions(exports);
#endif

Module['asm'] = exports;
Expand Down
9 changes: 8 additions & 1 deletion tools/js_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,11 @@ def make_wasm64_wrapper(sig):
# are certain places we need to avoid strict mode still.
# e.g. emscripten_get_callstack (getCallstack) which uses the `arguments`
# global.
return f' var wasm64Wrapper_{sig} = (f) => function ({args_in}) {{ return {result} }};\n'
return f' var makeWrapper_{sig} = (f) => function ({args_in}) {{ return {result} }};\n'


def make_unsign_pointer_wrapper(sig):
assert sig[0] == 'p'
n_args = len(sig) - 1
args = ','.join('a%d' % i for i in range(n_args))
return f' var makeWrapper_{sig} = (f) => ({args}) => f({args}) >>> 0;\n'