From 2d47020e15b82b7f021e0b52dabe75baf5cb4c4d Mon Sep 17 00:00:00 2001 From: YoungJoon Chun Date: Wed, 16 Feb 2022 18:47:05 +0900 Subject: [PATCH] rp2/mpthreadport: Fix memory corruption when thread is created in core1. The stack (and arg) of core1 is itself a root pointer, not just the entries in it. Without this fix the GC could reclaim the entire stack (and argument object). Fixes issues #7124 and #7981. --- ports/rp2/mpthreadport.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ports/rp2/mpthreadport.c b/ports/rp2/mpthreadport.c index 8a36cfca7..cdb5945d2 100644 --- a/ports/rp2/mpthreadport.c +++ b/ports/rp2/mpthreadport.c @@ -52,12 +52,12 @@ void mp_thread_deinit(void) { } void mp_thread_gc_others(void) { - if (get_core_num() == 0) { - // GC running on core0, trace core1's stack, if it's running. - if (core1_entry != NULL) { - gc_collect_root((void **)core1_stack, core1_stack_num_words); - } - } else { + if (core1_entry != NULL) { + // Collect core1's stack if it is active. + gc_collect_root((void **)&core1_stack, 1); + gc_collect_root((void **)&core1_arg, 1); + } + if (get_core_num() == 1) { // GC running on core1, trace core0's stack. gc_collect_root((void **)&__StackBottom, (&__StackTop - &__StackBottom) / sizeof(uintptr_t)); }