浏览代码

Disallow malloc memory too close to stack pointer

Fixes #219
main
PaulStoffregen 7 年前
父节点
当前提交
7f1f852b10
共有 1 个文件被更改,包括 24 次插入2 次删除
  1. +24
    -2
      teensy3/mk20dx128.c

+ 24
- 2
teensy3/mk20dx128.c 查看文件

@@ -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;
}


正在加载...
取消
保存