Repository navigation
Fix firmware crashes seen via the Python Editor - #144
Merged
Merged
Conversation
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.
|
Preview build will be at |
Contributor
Author
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
forloops over iterators trap with "memory access out of bounds"MP_BC_FOR_ITERinpy/vm.cindexes the value stack withsp[-MP_OBJ_ITER_BUF_NSLOTS + 1]. The macro is asize_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. Anyforloop over something other thanrange(), and any comprehension, failed. Loops overrange()compile to a counted loop and never reachFOR_ITER, which is why existing tests passed.Fixed by compiling with
-fwrapv-pointer.machine.time_pulse_ustraps with "null function"src/mphalport.hdidn't define the pin HAL macros that the device port does, sopy/mphal.hfell 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, asmicrobit_hal_pin_readis 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 undernlr_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.tscovers each case. Every new test failed with the reported error before its fix. Full e2e suite passes locally in both projects.Not addressed
pyexec.c) and the device firmware'smain.c.l = []; l.append(l); print(l)) overflows the browser call stack with aRangeError; MicroPython's stack check doesn't catch it.