Skip to content

Prevent malloc from overwriting the stack#681

Open
matthijskooijman wants to merge 3 commits intoarduino:masterfrom
matthijskooijman:malloc-no-overwrite-stack
Open

Prevent malloc from overwriting the stack#681
matthijskooijman wants to merge 3 commits intoarduino:masterfrom
matthijskooijman:malloc-no-overwrite-stack

Conversation

@matthijskooijman
Copy link
Copy Markdown
Collaborator

The current implementation of malloc (or really, the underlying sbrk() implementation) happily allocates any amount of memory, even when that overwrites the stack or even beyond the end of RAM. This is fixed by implementing a custom sbrk() function that does proper checking. The code is based on the sbrk() from the STM32 Arduino core, but with an additional margin added (to make it fail when it comes close to the stack, instead of just when it would actually overwrite the stack).

This margin approach is copied from avr-libc's malloc implementation. I considered also copying more of avr-libc's configurability (e.g. __malloc_heap_start and __malloc_heap_end), but that ended up just adding complexity without a very clear usecase, so I left that out.

See the d232b59 commit message for much more detail on this problem and the solution.

This PR also has a somewhat unrelated commit removing the -nostdlib compilation option, which was in the wrong place and thus effectively unused (and also unneeded).

Loading
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