Skip to content

Commit

Permalink
[wasm64] Fix for bad Memory initial size under firefox
Browse files Browse the repository at this point in the history
Recent versions of firefox started requiring bigint values for initial
and max memory.

See WebAssembly/memory64#68

Fixes: #22486
  • Loading branch information
sbc100 committed Sep 3, 2024
1 parent 4036606 commit 8b39774
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
10 changes: 9 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,15 @@ jobs:
# browser.test_webgl_offscreen_canvas_in_mainthread_after_pthread
# are crashing Firefox (bugzil.la/1281796). The former case is
# further blocked by issue #6897.
test_targets: "browser skip:browser.test_sdl2_mouse skip:browser.test_html5_webgl_create_context skip:browser.test_webgl_offscreen_canvas_in_pthread skip:browser.test_webgl_offscreen_canvas_in_mainthread_after_pthread skip:browser.test_glut_glutget"
test_targets: "
browser64.test_dylink_many
browser
skip:browser.test_sdl2_mouse
skip:browser.test_html5_webgl_create_context
skip:browser.test_webgl_offscreen_canvas_in_pthread
skip:browser.test_webgl_offscreen_canvas_in_mainthread_after_pthread
skip:browser.test_glut_glutget
"
# TODO(sbc): Re-enable once we figure out why the emrun tests are
# locking up.
#test-browser-chrome-emrun:
Expand Down
8 changes: 8 additions & 0 deletions src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,13 @@ function ENVIRONMENT_IS_MAIN_THREAD() {
return '(!(' + envs.join('||') + '))';
}

function memoryBounds(val) {
if (MEMORY64 == 1) {
return `toMemoryBounds(${val})`
}
return val;
}

addToCompileTimeContext({
ATEXITS,
ATINITS,
Expand Down Expand Up @@ -1141,6 +1148,7 @@ addToCompileTimeContext({
makeReturn64,
makeSetValue,
makeThrow,
memoryBounds,
modifyJSFunction,
receiveI64ParamAsI53,
receiveI64ParamAsI53Unchecked,
Expand Down
19 changes: 16 additions & 3 deletions src/runtime_init_memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,31 @@ if (!ENVIRONMENT_IS_PTHREAD) {

#if ASSERTIONS
assert(INITIAL_MEMORY >= {{{STACK_SIZE}}}, 'INITIAL_MEMORY should be larger than STACK_SIZE, was ' + INITIAL_MEMORY + '! (STACK_SIZE=' + {{{STACK_SIZE}}} + ')');
#endif
#if MEMORY64 == 1
// Probe for support of bigint bounds with memory64.
// TODO(sbc): Remove this once all browsers start requiring bigint here.
// See https://github.com/WebAssembly/memory64/issues/68
var bigintMemoryBounds = 1;
try {
new WebAssembly.Memory({'initial': 1n, 'index': 'i64'})
} catch (e) {
console.log(e);
bigintMemoryBounds = 0;
}
var toMemoryBounds = (i) => bigintMemoryBounds ? BigInt(i) : i;
#endif
wasmMemory = new WebAssembly.Memory({
'initial': INITIAL_MEMORY / {{{ WASM_PAGE_SIZE }}},
'initial': {{{ memoryBounds(`INITIAL_MEMORY / ${WASM_PAGE_SIZE}`) }}},
#if ALLOW_MEMORY_GROWTH
// In theory we should not need to emit the maximum if we want "unlimited"
// or 4GB of memory, but VMs error on that atm, see
// https://github.com/emscripten-core/emscripten/issues/14130
// And in the pthreads case we definitely need to emit a maximum. So
// always emit one.
'maximum': {{{ MAXIMUM_MEMORY }}} / {{{ WASM_PAGE_SIZE }}},
'maximum': {{{ memoryBounds(MAXIMUM_MEMORY / WASM_PAGE_SIZE }}},
#else
'maximum': INITIAL_MEMORY / {{{ WASM_PAGE_SIZE }}},
'maximum': {{{ memoryBounds(`INITIAL_MEMORY / ${WASM_PAGE_SIZE}`) }}},
#endif // ALLOW_MEMORY_GROWTH
#if SHARED_MEMORY
'shared': true,
Expand Down

0 comments on commit 8b39774

Please sign in to comment.