From 930079df796ec110be7073507286d6bd411265a6 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 11 Oct 2024 14:22:50 -0700 Subject: [PATCH] Use `toIndexType` in minimal runtime This fixes the MINIMAL_RUNTIME + MEMORY64 + IMPORTED_MEMORY. See #22497 --- src/library.js | 2 +- src/preamble_minimal.js | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/library.js b/src/library.js index 38487c9fc2af0..0e58ea40ef0a0 100644 --- a/src/library.js +++ b/src/library.js @@ -2006,7 +2006,7 @@ addToLibrary({ #endif // In -Os and -Oz builds, do not implement a JS side wasm table mirror for small // code size, but directly access wasmTable, which is a bit slower as uncached. - return wasmTable.get(funcPtr); + return wasmTable.get({{{ toIndexType('funcPtr') }}}); }, #endif // SHRINK_LEVEL == 0 diff --git a/src/preamble_minimal.js b/src/preamble_minimal.js index c266903e66cdf..b4233126e5299 100644 --- a/src/preamble_minimal.js +++ b/src/preamble_minimal.js @@ -61,6 +61,8 @@ var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64, #endif #if IMPORTED_MEMORY +// This code is largely a duplcate of src/runtime_init_memory.js +// TODO(sbc): merge these two. #if PTHREADS if (!ENVIRONMENT_IS_PTHREAD) { #endif @@ -69,10 +71,17 @@ if (!ENVIRONMENT_IS_PTHREAD) { Module['mem'] || #endif new WebAssembly.Memory({ - 'initial': {{{ INITIAL_MEMORY / WASM_PAGE_SIZE }}}, -#if SHARED_MEMORY || !ALLOW_MEMORY_GROWTH || MAXIMUM_MEMORY != FOUR_GB - 'maximum': {{{ (ALLOW_MEMORY_GROWTH && MAXIMUM_MEMORY != FOUR_GB ? MAXIMUM_MEMORY : INITIAL_MEMORY) / WASM_PAGE_SIZE }}}, -#endif + 'initial': {{{ toIndexType(`${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': {{{ toIndexType(MAXIMUM_MEMORY / WASM_PAGE_SIZE) }}}, +#else + 'maximum': {{{ toIndexType(`${INITIAL_MEMORY} / ${WASM_PAGE_SIZE}`) }}}, +#endif // ALLOW_MEMORY_GROWTH #if SHARED_MEMORY 'shared': true, #endif