Skip to content

Skip loadExtRam when the buffer is empty#74

Merged
binji merged 1 commit into
binji:mainfrom
kwigbo-org:pr/load-extram-guard
May 12, 2026
Merged

Skip loadExtRam when the buffer is empty#74
binji merged 1 commit into
binji:mainfrom
kwigbo-org:pr/load-extram-guard

Conversation

@kwigbo
Copy link
Copy Markdown
Contributor

@kwigbo kwigbo commented May 11, 2026

An empty Uint8Array is still truthy, so the constructor's if (extRamBuffer) guard called loadExtRam even when there was no save data to restore. For carts that don't advertise external RAM (MBC5 with EXT_RAM_SIZE_NONE, ROM-only, etc.),
_ext_ram_file_data_new returns 0 and the wasm side then reads from a null file-data ptr inside _emulator_read_ext_ram. Sometimes that lands on valid wasm linear memory, sometimes it's out of bounds depending on heap state from prior alloc/free cycles — surfacing as an intermittent
"RuntimeError: memory access out of bounds" in long-running embed scenarios that recreate the Emulator multiple times.

Tighten the guard to also require extRamBuffer.byteLength > 0.

An empty Uint8Array is still truthy, so the constructor's
`if (extRamBuffer)` guard called loadExtRam even when there was no
save data to restore. For carts that don't advertise external RAM
(MBC5 with EXT_RAM_SIZE_NONE, ROM-only, etc.),
`_ext_ram_file_data_new` returns 0 and the wasm side then reads
from a null file-data ptr inside `_emulator_read_ext_ram`.
Sometimes that lands on valid wasm linear memory, sometimes it's
out of bounds depending on heap state from prior alloc/free
cycles — surfacing as an intermittent
"RuntimeError: memory access out of bounds" in long-running embed
scenarios that recreate the Emulator multiple times.

Tighten the guard to also require `extRamBuffer.byteLength > 0`.
Copilot AI review requested due to automatic review settings May 11, 2026 19:06
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an intermittent wasm “memory access out of bounds” when creating an Emulator with an empty external-RAM restore buffer in docs/simple.js. This aligns the JS-side constructor behavior with the expectation that “no save data” should not trigger an ext-RAM load path.

Changes:

  • Tighten the constructor guard to only call loadExtRam() when extRamBuffer.byteLength > 0.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@binji binji merged commit e3caa27 into binji:main May 12, 2026
9 of 10 checks passed
@kwigbo kwigbo deleted the pr/load-extram-guard branch May 14, 2026 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants