raz / commit 566b557b

commit
566b557b66dddb0da2ca9541459dbdff680cb37e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
6e38bc77
Thread: a stack for the signal handlers in each thread's mapping

A stack overflow in a Thread thread now says so, as one in the main
thread does. The mapping gains 16 KB between the stack and the Thread
record; a new thread starts in run, which sets it with sigaltstack,
then calls its proc. test/run/ThreadOverflow; make stress passes.

patch · log

7 files changed, +64 -13

CLAUDE.md modified +4 -3

@@ -121,9 +121,10 @@
121121 - A failed check says what failed and with what values, at the place
122122 of the offending expression. A signal is told at the place of the
123123 statement it stopped in, or of the procedure for a stack overflow in
124- its prologue. The handlers run on a stack of `Rt`'s, which threads
125- made by `Thread` don't have: a stack overflow in one of them ends it
126- by the signal with no message.
124+ its prologue. The handlers run on a stack of their own: `Rt`'s for
125+ the main thread, and one in each `Thread` thread's mapping. C's
126+ threads have none, so a stack overflow in one ends the program by
127+ the signal with no message.
127128 - The smallest `I64` divided by -1 traps (SIGFPE), as 6.1 says.
128129 - Warnings come only for a module with no errors. A procedure naming
129130 itself doesn't use it; a type named by a pointer in its own

lib/Thread.linux-x86_64.raz (history) modified +27 -7

@@ -1,9 +1,10 @@
11 (* Threads for programs without C, made with the clone system call.
22 Each runs on a stack of its own, mmaped, with a page below it that
33 can't be touched, so that running off the stack stops the program
4- rather than spoiling what lies below. join waits on the thread's id,
5- which the kernel clears, and wakes it for, when the thread ends;
6- a Mutex sleeps on a futex. A program with C threads uses C's,
4+ rather than spoiling what lies below; above it, a little stack for
5+ Rt's signal handlers, so that they can say so. join waits on the
6+ thread's id, which the kernel clears, and wakes it for, when the
7+ thread ends; a Mutex sleeps on a futex. A program with C threads uses C's,
78 through pthread_create, and so can't use these. *)
89 module Thread;
910 import Sys, Rt;
@@ -12,6 +13,7 @@
1213 const
1314 Failed*: ERROR = "can't start a thread";
1415 Stack = 1 shl 20;
16+ Alt = 16384; (* the signal handlers' stack *)
1517 Page = 4096;
1618 (* clone: share memory, files, signal handlers, the thread group;
1719 write the id to tid, and clear it at the end *)
@@ -25,7 +27,11 @@
2527 tid: I64; (* the kernel's id, until the thread ends *)
2628 base: ^U8; (* its stack's mapping, the guard page first *)
2729 size: I64;
30+ fn: proc(arg: ^U8); (* what it runs, and with what *)
31+ arg: ^U8;
2832 end;
33+ (* What sigaltstack takes *)
34+ AltStack = record sp: ^U8; flags, size: I64; end;
2935 (* 0 free, 1 taken, 2 taken with a thread waiting *)
3036 Mutex* = record state: I64; end;
3137
@@ -57,11 +63,22 @@
5763 return SYSCALL(202, word, op, val, 0);
5864 end;
5965
66+(* Where a new thread starts: the signal handlers' stack is a thread's
67+ own, so it is set here, below t; then the thread runs its procedure. *)
68+proc run(arg: ^U8);
69+var t: ^Thread; alt: AltStack;
70+begin
71+ t := ^Thread(arg);
72+ alt := {sp: ^U8(t) - Alt, size: Alt};
73+ SYSCALL(131, ADR(alt), 0); (* sigaltstack *)
74+ t^.fn(t^.arg);
75+end;
76+
6077 (* Starts p(arg) in a new thread. *)
6178 proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR);
6279 var base: ^U8; t: ^Thread; size: I64;
6380 begin
64- size := Stack + Page;
81+ size := Page + Stack + Alt + Page;
6582 base := Sys.mmap(size);
6683 if base = nil then return nil, Failed; end;
6784 (* mprotect: the guard page, no access; it fails when the process
@@ -70,11 +87,14 @@
7087 Sys.munmap(base, size);
7188 return nil, Failed;
7289 end;
73- (* the Thread at the top, the stack below it, aligned to 16 *)
74- t := ^Thread(base + size - 32);
90+ (* from the top: the Thread in the last page, the handlers' stack,
91+ then the thread's, starting on a page *)
92+ t := ^Thread(base + size - Page);
7593 t^.base := base;
7694 t^.size := size;
77- if start(Flags, base + size - 32, ADR(t^.tid), ADR(t^.tid), p, arg) < 0 then
95+ t^.fn := p;
96+ t^.arg := arg;
97+ if start(Flags, base + Page + Stack, ADR(t^.tid), ADR(t^.tid), run, ^U8(t)) < 0 then
7898 Sys.munmap(base, size);
7999 return nil, Failed;
80100 end;

test/doc/Thread.expect (history) modified +6 -3

@@ -2,9 +2,10 @@
22 Threads for programs without C, made with the clone system call.
33 Each runs on a stack of its own, mmaped, with a page below it that
44 can't be touched, so that running off the stack stops the program
5- rather than spoiling what lies below. join waits on the thread's id,
6- which the kernel clears, and wakes it for, when the thread ends;
7- a Mutex sleeps on a futex. A program with C threads uses C's,
5+ rather than spoiling what lies below; above it, a little stack for
6+ Rt's signal handlers, so that they can say so. join waits on the
7+ thread's id, which the kernel clears, and wakes it for, when the
8+ thread ends; a Mutex sleeps on a futex. A program with C threads uses C's,
89 through pthread_create, and so can't use these.
910
1011 const Failed*: ERROR = "can't start a thread";
@@ -13,6 +14,8 @@
1314 tid: I64; (* the kernel's id, until the thread ends *)
1415 base: ^U8; (* its stack's mapping, the guard page first *)
1516 size: I64;
17+ fn: proc(arg: ^U8); (* what it runs, and with what *)
18+ arg: ^U8;
1619 end;
1720
1821 type Mutex* = record state: I64; end;

test/run/ThreadOverflow.expect (history) added +0 -0

An empty file.

test/run/ThreadOverflow.raz (history) added +25 -0

@@ -0,0 +1,25 @@
1+module ThreadOverflow;
2+(* A stack overflow in a thread is told as one in the main thread is:
3+ the thread's mapping holds a stack for the signal handlers *)
4+import Thread;
5+
6+proc down(n: I64): I64;
7+var pad: [64]I64;
8+begin
9+ pad[n mod 64] := n;
10+ return down(n + 1) + pad[0];
11+end;
12+
13+proc work(_arg: ^U8);
14+begin
15+ down(0);
16+end;
17+
18+proc main*;
19+var t: ^Thread.Thread; e: ERROR;
20+begin
21+ t, e := Thread.spawn(work, nil);
22+ if e = nil then Thread.join(t); end;
23+end;
24+
25+end ThreadOverflow.

test/run/ThreadOverflow.status (history) added +1 -0

@@ -0,0 +1 @@
1+139

test/run/ThreadOverflow.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+test/run/ThreadOverflow.raz:6:1: stopped by a stack overflow