Skip to content

Add stack validity check and raise an error when it happens. #1294

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 9 commits into from
Dec 7, 2018

Conversation

tannewt
Copy link
Member

@tannewt tannewt commented Oct 23, 2018

The backtrace cannot be given because it relies on the validity
of the qstr data structures on the heap which may have been
corrupted.

In fact, it still can crash hard when the bytecode itself is
overwritten. To fix, we'd need a way to skip gathering the
backtrace completely.

This also increases the default stack size on M4s so it can
accomodate the stack needed by ASF4s nvm API.

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

Good idea!

I think we should still also sprinkle MP_STACK_CHECK calls around in a few places, like before or after imports. I think would catch the stack getting into the exception zone before the heap was smashed. See mp_stack_check() in py/stackctrl.c. That could be added as another fix for #1283.

Would this catch #1279? That would be something to test.


inline void assert_heap_ok(void) {
if (!stack_ok()) {
mp_raise_RuntimeError(translate("Stack clobbered heap."));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this is a very bad error, we should probably also say something like "Please restart" or "Please type ctrl-D". The heap may be damaged and may not be safe to do anything else

Copy link
Member Author

Choose a reason for hiding this comment

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

Think I should make it safe mode instead? It'd work more reliably because then it wouldn't try to use the heap at all.

@tannewt
Copy link
Member Author

tannewt commented Oct 23, 2018

I'll take a look at this again later this week. I'm hoping to have a MIDI demo tomorrow so I need to go heads down on it.

The backtrace cannot be given because it relies on the validity
of the qstr data structures on the heap which may have been
corrupted.

In fact, it still can crash hard when the bytecode itself is
overwritten. To fix, we'd need a way to skip gathering the
backtrace completely.

This also increases the default stack size on M4s so it can
accomodate the stack needed by ASF4s nvm API.
This creates a common safe mode mechanic that ports can share.
As a result, the nRF52 now has safe mode support as well.

The common safe mode adds a 700ms delay at startup where a reset
during that window will cause a reset into safe mode. This window
is designated by a yellow status pixel and flashing the single led
three times.

A couple NeoPixel fixes are included for the nRF52 as well.

Fixes micropython#1034. Fixes micropython#990. Fixes micropython#615.
@tannewt
Copy link
Member Author

tannewt commented Dec 7, 2018

Ok, Travis is finally happy. Please take another look @dhalbert

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

OK, looks good! Making the stack be 24kB on SAMD51 is very generous; we could trim it down later if necessary.

@dhalbert dhalbert merged commit 2fbaceb into adafruit:master Dec 7, 2018
@tannewt tannewt deleted the stack_check branch December 7, 2018 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants