Skip to content
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

Prevent malloc from overwriting the stack #681

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cores/arduino/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ int main( void )

return 0;
}

// Ensure our version of sbrk is used by forcing a reference to it here
// (without this, an undefined reference only occurs later when linking
// newlib, and the dummy from libgloss/nosys will be used instead).
// This variable will be optimized away later.
extern "C" void *_sbrk(int);
void * force_reference_to_sbrk = (void*)&_sbrk;
23 changes: 23 additions & 0 deletions cores/arduino/sbrk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <samd.h>
#include <stddef.h>
#include <errno.h>

/* How much free space to keep between the stack and the heap by malloc.
* Can be changed by the application at any time. */
size_t __malloc_margin = 64;

void *_sbrk(int incr)
{
extern char end; /* End of global variables, defined by the linker */
static char *brkval = &end ;
char *prev_brkval = brkval;

if (brkval + incr + __malloc_margin > (char *)__get_MSP()) {
/* Heap and stack collision */
errno = ENOMEM;
return (void*) -1;
}

brkval += incr ;
return (void*) prev_brkval;
}
4 changes: 2 additions & 2 deletions platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ compiler.optimization_flags.debug=-Og -g3

compiler.path={runtime.tools.arm-none-eabi-gcc-7-2017q4.path}/bin/
compiler.c.cmd=arm-none-eabi-gcc
compiler.c.flags=-mcpu={build.mcu} -mthumb -c -g {compiler.optimization_flags} {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -MMD
compiler.c.flags=-mcpu={build.mcu} -mthumb -c -g {compiler.optimization_flags} {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections --param max-inline-insns-single=500 -MMD
compiler.c.elf.cmd=arm-none-eabi-g++
compiler.c.elf.flags={compiler.optimization_flags} -Wl,--gc-sections -save-temps
compiler.S.cmd=arm-none-eabi-gcc
compiler.S.flags=-mcpu={build.mcu} -mthumb -c -g -x assembler-with-cpp -MMD
compiler.cpp.cmd=arm-none-eabi-g++
compiler.cpp.flags=-mcpu={build.mcu} -mthumb -c -g {compiler.optimization_flags} {compiler.warning_flags} -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD
compiler.cpp.flags=-mcpu={build.mcu} -mthumb -c -g {compiler.optimization_flags} {compiler.warning_flags} -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD
compiler.ar.cmd=arm-none-eabi-ar
compiler.ar.flags=rcs
compiler.objcopy.cmd=arm-none-eabi-objcopy
Expand Down