Prevent malloc from overwriting the stack#681
Open
matthijskooijman wants to merge 3 commits intoarduino:masterfrom
Open
Prevent malloc from overwriting the stack#681matthijskooijman wants to merge 3 commits intoarduino:masterfrom
matthijskooijman wants to merge 3 commits intoarduino:masterfrom
Conversation
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.
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 customsbrk()function that does proper checking. The code is based on thesbrk()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_startand__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
-nostdlibcompilation option, which was in the wrong place and thus effectively unused (and also unneeded).