Skip to content
This repository was archived by the owner on Mar 11, 2019. It is now read-only.

Commit 42c1d82

Browse files
committed
Better stack overflow checking
1 parent abf8e33 commit 42c1d82

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

ports/unix/main.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ int main(int argc, char **argv) {
1010
return 0;
1111
}
1212

13+
static void tinygo_check_canary() {
14+
if (goroutine->canary != STACK_CANARY) {
15+
Eface err;
16+
runtime_newErrorCString("stack overflow", &err);
17+
runtime_Panic(err, true);
18+
}
19+
}
20+
1321
__attribute__((noreturn))
1422
void tinygo_goroutine_exit() {
1523
goroutine_work_counter++;
1624

1725
DEBUG_printf("-- exit goroutine %d\n", goroutine->num);
1826

27+
tinygo_check_canary();
28+
1929
// cleanup goroutine
2030
if (goroutine->next == goroutine) {
2131
// This is the last goroutine.
@@ -53,6 +63,10 @@ void tinygo_run_internal(func fn, void *arg) {
5363
void tinygo_go(void *fn, void *arg, void *created_by) {
5464
static size_t goroutine_counter = 0;
5565

66+
if (goroutine != NULL) {
67+
tinygo_check_canary();
68+
}
69+
5670
// Allocate and init goroutine
5771
goroutine_t *r = tinygo_alloc(sizeof(goroutine_t));
5872
r->num = ++goroutine_counter;
@@ -84,11 +98,7 @@ void tinygo_block() {
8498
if (goroutine == goroutine->next) {
8599
tinygo_deadlocked();
86100
}
87-
if (goroutine->canary != STACK_CANARY) {
88-
Eface err;
89-
runtime_newErrorCString("stack overflow", &err);
90-
runtime_Panic(err, true);
91-
}
101+
tinygo_check_canary();
92102
goroutine_t *old = goroutine;
93103
goroutine_t *new = goroutine->next;
94104
goroutine = new;

0 commit comments

Comments
 (0)