Skip to content

Fix bad access to detached ArrayBuffer in $growMemory. #24524

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 13, 2025
Merged
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
13 changes: 5 additions & 8 deletions src/lib/libcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,24 @@ addToLibrary({
// Grows the wasm memory to the given byte size, and updates the JS views to
// it. Returns 1 on success, 0 on error.
$growMemory: (size) => {
var b = wasmMemory.buffer;
var pages = ((size - b.byteLength + {{{ WASM_PAGE_SIZE - 1 }}}) / {{{ WASM_PAGE_SIZE }}}) | 0;
var oldHeapSize = wasmMemory.buffer.byteLength;
var pages = ((size - oldHeapSize + {{{ WASM_PAGE_SIZE - 1 }}}) / {{{ WASM_PAGE_SIZE }}}) | 0;
#if RUNTIME_DEBUG
dbg(`growMemory: ${size} (+${size - b.byteLength} bytes / ${pages} pages)`);
#endif
#if MEMORYPROFILER
var oldHeapSize = b.byteLength;
dbg(`growMemory: ${size} (+${size - oldHeapSize} bytes / ${pages} pages)`);
#endif
try {
// round size grow request up to wasm page size (fixed 64KB per spec)
wasmMemory.grow({{{ toIndexType('pages') }}}); // .grow() takes a delta compared to the previous size
updateMemoryViews();
#if MEMORYPROFILER
if (typeof emscriptenMemoryProfiler != 'undefined') {
emscriptenMemoryProfiler.onMemoryResize(oldHeapSize, b.byteLength);
emscriptenMemoryProfiler.onMemoryResize(oldHeapSize, wasmMemory.buffer.byteLength);
}
#endif
return 1 /*success*/;
} catch(e) {
#if ASSERTIONS
err(`growMemory: Attempted to grow heap from ${b.byteLength} bytes to ${size} bytes, but got error: ${e}`);
err(`growMemory: Attempted to grow heap from ${oldHeapSize} bytes to ${size} bytes, but got error: ${e}`);
#endif
}
// implicit 0 return to save code size (caller will cast "undefined" into 0
Expand Down