Skip to content

Commit ec78d3c

Browse files
committed
Mark pointers in cpu registers as in use.
This prevents bugs where gc_collect is called from C code that did a recent allocation.
1 parent 252aacd commit ec78d3c

5 files changed

Lines changed: 105 additions & 4 deletions

File tree

main.c

100644100755
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "lib/utils/pyexec.h"
4545

4646
#include "mpconfigboard.h"
47+
#include "supervisor/cpu.h"
4748
#include "supervisor/port.h"
4849
#include "supervisor/filesystem.h"
4950
// TODO(tannewt): Figure out how to choose language at compile time.
@@ -381,16 +382,17 @@ int __attribute__((used)) main(void) {
381382
}
382383

383384
void gc_collect(void) {
384-
// WARNING: This gc_collect implementation doesn't try to get root
385-
// pointers from CPU registers, and thus may function incorrectly.
386-
void *dummy;
387385
gc_collect_start();
386+
387+
mp_uint_t regs[10];
388+
mp_uint_t sp = cpu_get_regs_and_sp(regs);
389+
388390
// This collects root pointers from the VFS mount table. Some of them may
389391
// have lost their references in the VM even though they are mounted.
390392
gc_collect_root((void**)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
391393
// This naively collects all object references from an approximate stack
392394
// range.
393-
gc_collect_root(&dummy, ((mp_uint_t)&_estack - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
395+
gc_collect_root((void**)sp, ((uint32_t)&_estack - sp) / sizeof(uint32_t));
394396
gc_collect_end();
395397
}
396398

ports/atmel-samd/Makefile

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,16 @@ endif
412412
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
413413
$(addprefix shared-module/, $(SRC_SHARED_MODULE))
414414

415+
SRC_O = supervisor/$(CHIP_FAMILY)_cpu.o
416+
415417
OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
416418
OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o))
417419
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o))
418420
OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o))
419421
ifeq ($(INTERNAL_LIBM),1)
420422
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
421423
endif
424+
OBJ += $(addprefix $(BUILD)/, $(SRC_O))
422425

423426
SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) $(STM_SRC_C)
424427

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.syntax unified
2+
.cpu cortex-m0
3+
.thumb
4+
.text
5+
.align 2
6+
7+
@ uint cpu_get_regs_and_sp(r0=uint regs[10])
8+
.global cpu_get_regs_and_sp
9+
.thumb
10+
.thumb_func
11+
.type cpu_get_regs_and_sp, %function
12+
cpu_get_regs_and_sp:
13+
@ store registers into given array
14+
str r4, [r0, #0]
15+
str r5, [r0, #4]
16+
str r6, [r0, #8]
17+
str r7, [r0, #12]
18+
push {r1}
19+
mov r1, r8
20+
str r1, [r0, #16]
21+
mov r1, r9
22+
str r1, [r0, #20]
23+
mov r1, r10
24+
str r1, [r0, #24]
25+
mov r1, r11
26+
str r1, [r0, #28]
27+
mov r1, r12
28+
str r1, [r0, #32]
29+
mov r1, r13
30+
str r1, [r0, #36]
31+
pop {r1}
32+
33+
@ return the sp
34+
mov r0, sp
35+
bx lr
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.syntax unified
2+
.cpu cortex-m4
3+
.thumb
4+
.text
5+
.align 2
6+
7+
@ uint cpu_get_regs_and_sp(r0=uint regs[10])
8+
.global cpu_get_regs_and_sp
9+
.thumb
10+
.thumb_func
11+
.type cpu_get_regs_and_sp, %function
12+
cpu_get_regs_and_sp:
13+
@ store registers into given array
14+
str r4, [r0], #4
15+
str r5, [r0], #4
16+
str r6, [r0], #4
17+
str r7, [r0], #4
18+
str r8, [r0], #4
19+
str r9, [r0], #4
20+
str r10, [r0], #4
21+
str r11, [r0], #4
22+
str r12, [r0], #4
23+
str r13, [r0], #4
24+
25+
@ return the sp
26+
mov r0, sp
27+
bx lr

supervisor/cpu.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SUPERVISOR_CPU_H
28+
#define MICROPY_INCLUDED_SUPERVISOR_CPU_H
29+
30+
// Adds up to 10 pointers from the CPUs registers to regs. This is used to make sure no actively
31+
// used heap memory is freed. Its usually implemented in assembly.
32+
mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs);
33+
34+
#endif // MICROPY_INCLUDED_SUPERVISOR_CPU_H

0 commit comments

Comments
 (0)