Skip to content

Cap the heap at the cart-region boundary#2

Open
neilrackett wants to merge 1 commit into
sidecartridge:mainfrom
neilrackett:fix/heap-cart-region-overrun
Open

Cap the heap at the cart-region boundary#2
neilrackett wants to merge 1 commit into
sidecartridge:mainfrom
neilrackett:fix/heap-cart-region-overrun

Conversation

@neilrackett

Copy link
Copy Markdown

Problem

In rp/src/memmap_rp.ld, the heap ceiling is:

__StackLimit = ORIGIN(RAM) + LENGTH(RAM) + LENGTH(ROM_IN_RAM);

__StackLimit is what pico-sdk's _sbrk uses to bound the heap. Including LENGTH(ROM_IN_RAM) lets the heap grow straight through the ROM_IN_RAM mirror — the region the m68k executes from over the cartridge bus.

Boot-time allocations (the settings library alone mallocs ~13 KB) and runtime FatFs LFN buffers then silently overwrite the served cartridge code, crashing the ST with bombs / a black screen. As with the array-alignment issue, the exact trigger shifts with static layout, so it is intermittent and hard to trace.

Fix

Drop + LENGTH(ROM_IN_RAM) so the heap is capped at the RAM/cart boundary. A failed allocation then panics cleanly (pico-sdk panics on OOM) instead of corrupting the m68k image:

__StackLimit = ORIGIN(RAM) + LENGTH(RAM);

Verified in a downstream app whose settings-library boot mallocs were overrunning the cart region: with the heap capped, the region stays intact and any genuine OOM surfaces as a clean panic. Apps that need more heap should reclaim RAM rather than borrow the cartridge mirror.

__StackLimit is pico-sdk's _sbrk heap ceiling. Including
LENGTH(ROM_IN_RAM) let the heap grow through the entire ROM_IN_RAM
mirror -- the region the m68k executes over the cartridge bus.
Boot-time allocations (the settings library alone mallocs ~13 KB) and
runtime FatFs LFN buffers then silently overwrite the served cartridge
code, crashing the ST with bombs / black screens that vary per build
with static layout.

Drop the + LENGTH(ROM_IN_RAM) so the heap is capped at the RAM/cart
boundary and malloc fails cleanly (pico-sdk panics on failed malloc)
instead of corrupting the m68k image.
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.

1 participant