Browse Source

Disallow malloc memory too close to stack pointer

Fixes #219
teensy4-core
PaulStoffregen 7 years ago
parent
commit
7f1f852b10
1 changed files with 24 additions and 2 deletions
  1. +24
    -2
      teensy3/mk20dx128.c

+ 24
- 2
teensy3/mk20dx128.c View File

@@ -31,6 +31,7 @@
#include "kinetis.h"
#include "core_pins.h" // testing only
#include "ser_print.h" // testing only
#include <errno.h>


// Flash Security Setting. On Teensy 3.2, you can lock the MK20 chip to prevent
@@ -1137,10 +1138,31 @@ void ResetHandler(void)

char *__brkval = (char *)&_ebss;

#ifndef STACK_MARGIN
#if defined(__MKL26Z64__)
#define STACK_MARGIN 512
#elif defined(__MK20DX128__)
#define STACK_MARGIN 1024
#elif defined(__MK20DX256__)
#define STACK_MARGIN 4096
#elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
#define STACK_MARGIN 8192
#endif
#endif

void * _sbrk(int incr)
{
char *prev = __brkval;
__brkval += incr;
char *prev, *stack;

prev = __brkval;
if (incr != 0) {
__asm__ volatile("mov %0, sp" : "=r" (stack) ::);
if (prev + incr >= stack - STACK_MARGIN) {
errno = ENOMEM;
return (void *)-1;
}
__brkval = prev + incr;
}
return prev;
}


Loading…
Cancel
Save