summaryrefslogtreecommitdiff
path: root/rts/Schedule.c
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2022-04-04 21:43:27 -0400
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-04-29 13:06:20 -0400
commit5630dde68185f96da026a4e0c722fe6631633299 (patch)
tree973de628f322377ce523fcfb09a2ae25bd74f295 /rts/Schedule.c
parent724d0dc04645b21865bcd1877b7cc77e9a3fe9b2 (diff)
downloadhaskell-5630dde68185f96da026a4e0c722fe6631633299.tar.gz
rts: Refactor handling of dead threads' stacks
This fixes a bug that @JunmingZhao42 and I noticed while working on her MMTK port. Specifically, in stg_stop_thread we used stg_enter_info as a sentinel at the tail of a stack after a thread has completed. However, stg_enter_info expects to have a two-field payload, which we do not push. Consequently, if the GC ends up somehow the stack it will attempt to interpret data past the end of the stack as the frame's fields, resulting in unsound behavior. To fix this I eliminate this hacky use of `stg_stop_thread` and instead introduce a new stack frame type, `stg_dead_thread_info`. Not only does this eliminate the potential for the previously mentioned memory unsoundness but it also more clearly captures the intended structure of the dead threads' stacks.
Diffstat (limited to 'rts/Schedule.c')
-rw-r--r--rts/Schedule.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/rts/Schedule.c b/rts/Schedule.c
index fa48bef1a7..9b1e98ea17 100644
--- a/rts/Schedule.c
+++ b/rts/Schedule.c
@@ -1315,7 +1315,9 @@ scheduleHandleThreadFinished (Capability *cap, Task *task, StgTSO *t)
if (t->what_next == ThreadComplete) {
if (task->incall->ret) {
// NOTE: return val is stack->sp[1] (see StgStartup.cmm)
- *(task->incall->ret) = (StgClosure *)task->incall->tso->stackobj->sp[1];
+ StgDeadThreadFrame *dead = (StgDeadThreadFrame *) &task->incall->tso->stackobj->sp[0];
+ ASSERT(dead->header.info == &stg_dead_thread_info);
+ *(task->incall->ret) = (StgClosure *) dead->result;
}
task->incall->rstat = Success;
} else {