Skip to content

Commit eda3338

Browse files
committed
atmel-samd: Correct the stack_top pointer used in garbage collection.
Without this fix the gc will consider a large, random section of memory and it may never finish.
1 parent ffdc3f8 commit eda3338

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

atmel-samd/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ int main(int argc, char **argv) {
190190
init_flash_fs();
191191

192192
int stack_dummy;
193+
// Store the location of stack_dummy as an approximation for the top of the
194+
// stack so the GC can account for objects that may be referenced by the
195+
// stack between here and where gc_collect is called.
196+
stack_top = (char*)&stack_dummy;
193197
reset_mp();
194198

195199
// Main script is finished, so now go into REPL mode.
@@ -203,7 +207,6 @@ int main(int argc, char **argv) {
203207
}
204208
if (exit_code == PYEXEC_FORCED_EXIT) {
205209
mp_hal_stdout_tx_str("soft reboot\r\n");
206-
stack_top = (char*)&stack_dummy;
207210
reset_mp();
208211
} else if (exit_code != 0) {
209212
break;
@@ -218,6 +221,8 @@ void gc_collect(void) {
218221
// pointers from CPU registers, and thus may function incorrectly.
219222
void *dummy;
220223
gc_collect_start();
224+
// This naively collects all object references from an approximate stack
225+
// range.
221226
gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
222227
gc_collect_end();
223228
gc_dump_info();

0 commit comments

Comments
 (0)