Skip to content

Commit 28076f3

Browse files
atxdpgeorge
authored andcommitted
esp8266: Fix garbage collector by hard-coding stack end address.
As user_init() is not a true main functions, the stack pointer captured within is not pointing at the base of the stack. This caused gc_collect being called with sp being higher than stack_end, causing integer overflow and crashing as gc tried to scan almost the entire address space.
1 parent 9a42eb5 commit 28076f3

3 files changed

Lines changed: 4 additions & 11 deletions

File tree

esp8266/gccollect.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,12 @@
2929
#include "py/gc.h"
3030
#include "gccollect.h"
3131

32-
STATIC uint32_t stack_end;
32+
// As we do not have control over the application entry point, there is no way
33+
// to figure out the real stack base on runtime, so it needs to be hardcoded
34+
#define STACK_END 0x40000000
3335

3436
mp_uint_t gc_helper_get_regs_and_sp(mp_uint_t *regs);
3537

36-
void gc_collect_init(void) {
37-
mp_uint_t regs[8];
38-
mp_uint_t sp = gc_helper_get_regs_and_sp(regs);
39-
stack_end = sp;
40-
//printf("stack=%p ram_end=%p %d\n", stack_end, &_ram_end);
41-
}
42-
4338
void gc_collect(void) {
4439
// start the GC
4540
gc_collect_start();
@@ -53,7 +48,7 @@ void gc_collect(void) {
5348
mp_uint_t sp = gc_helper_get_regs_and_sp(regs);
5449

5550
// trace the stack, including the registers (since they live on the stack in this function)
56-
gc_collect_root((void**)sp, (stack_end - sp) / sizeof(uint32_t));
51+
gc_collect_root((void**)sp, (STACK_END - sp) / sizeof(uint32_t));
5752

5853
// end the GC
5954
gc_collect_end();

esp8266/gccollect.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@ extern uint32_t _bss_end;
3737
extern uint32_t _heap_start;
3838
extern uint32_t _heap_end;
3939

40-
void gc_collect_init(void);
4140
void gc_collect(void);

esp8266/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ STATIC void mp_reset(void) {
4343
mp_stack_set_limit(10240);
4444
mp_hal_init();
4545
gc_init(heap, heap + sizeof(heap));
46-
gc_collect_init();
4746
mp_init();
4847
mp_obj_list_init(mp_sys_path, 0);
4948
mp_obj_list_init(mp_sys_argv, 0);

0 commit comments

Comments
 (0)