Skip to content

Fix firmware crashes seen via the Python Editor - #144

Merged
microbit-matt-hillsdon merged 3 commits into
mainfrom
fix-firmware-crashes
Nov 11, 2026
Merged

microbit-matt-hillsdon merged 3 commits into
mainfrom
fix-firmware-crashes

Conversation

@microbit-matt-hillsdon

Copy link
Copy Markdown
Contributor

Fixes three simulator crashes reported to the Python Editor's Sentry project as "Simulator internal error". The first two appeared on the beta editor after the Emscripten 6.0.6 update; the third is long-standing in production.

for loops over iterators trap with "memory access out of bounds"

MP_BC_FOR_ITER in py/vm.c indexes the value stack with sp[-MP_OBJ_ITER_BUF_NSLOTS + 1]. The macro is a size_t, so the index is a large unsigned value that relies on the address wrapping. The clang in Emscripten 6.0.6 assumes it cannot wrap and folds it into the Wasm load offset (i32.load offset=4294967284), which doesn't wrap, so the load always traps. Any for loop over something other than range(), and any comprehension, failed. Loops over range() compile to a counted loop and never reach FOR_ITER, which is why existing tests passed.

Fixed by compiling with -fwrapv-pointer.

machine.time_pulse_us traps with "null function"

src/mphalport.h didn't define the pin HAL macros that the device port does, so py/mphal.h fell back to the virtual pin API, which calls through a pin protocol the micro:bit pin types don't have. The call now works but always times out, as microbit_hal_pin_read is a stub in the simulator.

ExitStatus: Program terminated with exit(1)

Printing an uncaught exception and building the message to scroll both ran with no handler in place. Either can raise, most commonly when the program has run out of heap, which reached nlr_jump_fail. Both now run under nlr_push. The existing heap exhaustion test was marked as an expected failure for this reason and is now a normal test.

Tests

New e2e/micropython.spec.ts covers each case. Every new test failed with the reported error before its fix. Full e2e suite passes locally in both projects.

Not addressed

  • The same unprotected exception printing exists in the REPL path (pyexec.c) and the device firmware's main.c.
  • Printing a self-referential list (l = []; l.append(l); print(l)) overflows the browser call stack with a RangeError; MicroPython's stack check doesn't catch it.

MicroPython's FOR_ITER handler indexes the value stack with
sp[-MP_OBJ_ITER_BUF_NSLOTS + 1]. The macro is a size_t so the index is a
large unsigned value that relies on the address wrapping. The clang in
Emscripten 6.0.6 assumes it cannot wrap and folds it into the Wasm load
offset, which doesn't, so the load is always out of bounds. Any for loop
over something other than range(), and any comprehension, failed with
"memory access out of bounds" in mp_execute_bytecode.

Compile with -fwrapv-pointer so pointer arithmetic wraps as the code
expects. Existing tests only looped over range(), which compiles to a
counted loop that never reaches FOR_ITER.
The simulator's mphalport.h didn't define the pin HAL macros that the
device port does, so py/mphal.h fell back to the virtual pin API. That
calls through the pin type's protocol, which the micro:bit pin types
don't have, so any call to time_pulse_us trapped with "null function".
Printing the exception and building the message to scroll both ran with
no handler in place. Either can raise, which reached nlr_jump_fail and
exit(1). The embedder saw that as an internal error with ExitStatus.

The common case is a program that runs out of heap: the message for the
display is allocated while the heap is still full. A user-defined
__str__ that raises does the same.

Run both under nlr_push so a failure there is dropped and the REPL
starts as usual. The heap exhaustion test was marked as an expected
failure for this reason and now passes.
@github-actions

Copy link
Copy Markdown

@microbit-matt-hillsdon

Copy link
Copy Markdown
Contributor Author

for loops over iterators trap with "memory access out of bounds"

This was new in 0.2 because of the Emscripten update; the others are pre-existing. Likely the time_pulse_us one is represented in production Sentry logs with a cryptic error that's now much clearer on beta because we keep function names from sim 0.2.0.

@microbit-matt-hillsdon
microbit-matt-hillsdon merged commit 8f1ff52 into main Nov 11, 2026
2 checks passed
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