summaryrefslogtreecommitdiff
path: root/supervisor/shared/stack.c
diff options
context:
space:
mode:
Diffstat (limited to 'supervisor/shared/stack.c')
-rwxr-xr-xsupervisor/shared/stack.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/supervisor/shared/stack.c b/supervisor/shared/stack.c
index 6b74742eb..8f4da4ce6 100755
--- a/supervisor/shared/stack.c
+++ b/supervisor/shared/stack.c
@@ -27,6 +27,7 @@
#include "stack.h"
#include "py/mpconfig.h"
+#include "py/runtime.h"
#include "supervisor/cpu.h"
extern uint32_t _estack;
@@ -37,6 +38,8 @@ supervisor_allocation* stack_alloc = NULL;
#define EXCEPTION_STACK_SIZE 1024
+#define STACK_CANARY_VALUE 0x017829ef
+
void allocate_stack(void) {
mp_uint_t regs[10];
mp_uint_t sp = cpu_get_regs_and_sp(regs);
@@ -50,6 +53,19 @@ void allocate_stack(void) {
} else {
current_stack_size = next_stack_size;
}
+ *stack_alloc->ptr = STACK_CANARY_VALUE;
+}
+
+inline bool stack_ok(void) {
+ return *stack_alloc->ptr == STACK_CANARY_VALUE;
+}
+
+inline void assert_heap_ok(void) {
+ if (!stack_ok()) {
+ asm("nop");
+ while(true) {}
+ mp_raise_RuntimeError(translate("Stack clobbered heap."));
+ }
}
void stack_init(void) {
@@ -58,6 +74,7 @@ void stack_init(void) {
void stack_resize(void) {
if (next_stack_size == current_stack_size) {
+ *stack_alloc->ptr = STACK_CANARY_VALUE;
return;
}
free_memory(stack_alloc);