From 566b557b66dddb0da2ca9541459dbdff680cb37e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:08:13 +0100 Subject: [PATCH] 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index 9246ef2..d62241a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -121,9 +121,10 @@ - A failed check says what failed and with what values, at the place of the offending expression. A signal is told at the place of the statement it stopped in, or of the procedure for a stack overflow in - its prologue. The handlers run on a stack of `Rt`'s, which threads - made by `Thread` don't have: a stack overflow in one of them ends it - by the signal with no message. + its prologue. The handlers run on a stack of their own: `Rt`'s for + the main thread, and one in each `Thread` thread's mapping. C's + threads have none, so a stack overflow in one ends the program by + the signal with no message. - The smallest `I64` divided by -1 traps (SIGFPE), as 6.1 says. - Warnings come only for a module with no errors. A procedure naming itself doesn't use it; a type named by a pointer in its own diff --git a/lib/Thread.linux-x86_64.raz b/lib/Thread.linux-x86_64.raz index 49d5e0e..2b04f6c 100644 --- a/lib/Thread.linux-x86_64.raz +++ b/lib/Thread.linux-x86_64.raz @@ -1,9 +1,10 @@ (* Threads for programs without C, made with the clone system call. Each runs on a stack of its own, mmaped, with a page below it that can't be touched, so that running off the stack stops the program - rather than spoiling what lies below. join waits on the thread's id, - which the kernel clears, and wakes it for, when the thread ends; - a Mutex sleeps on a futex. A program with C threads uses C's, + rather than spoiling what lies below; above it, a little stack for + Rt's signal handlers, so that they can say so. join waits on the + thread's id, which the kernel clears, and wakes it for, when the + thread ends; a Mutex sleeps on a futex. A program with C threads uses C's, through pthread_create, and so can't use these. *) module Thread; import Sys, Rt; @@ -12,6 +13,7 @@ const Failed*: ERROR = "can't start a thread"; Stack = 1 shl 20; + Alt = 16384; (* the signal handlers' stack *) Page = 4096; (* clone: share memory, files, signal handlers, the thread group; write the id to tid, and clear it at the end *) @@ -25,7 +27,11 @@ tid: I64; (* the kernel's id, until the thread ends *) base: ^U8; (* its stack's mapping, the guard page first *) size: I64; + fn: proc(arg: ^U8); (* what it runs, and with what *) + arg: ^U8; end; + (* What sigaltstack takes *) + AltStack = record sp: ^U8; flags, size: I64; end; (* 0 free, 1 taken, 2 taken with a thread waiting *) Mutex* = record state: I64; end; @@ -57,11 +63,22 @@ return SYSCALL(202, word, op, val, 0); end; +(* Where a new thread starts: the signal handlers' stack is a thread's + own, so it is set here, below t; then the thread runs its procedure. *) +proc run(arg: ^U8); +var t: ^Thread; alt: AltStack; +begin + t := ^Thread(arg); + alt := {sp: ^U8(t) - Alt, size: Alt}; + SYSCALL(131, ADR(alt), 0); (* sigaltstack *) + t^.fn(t^.arg); +end; + (* Starts p(arg) in a new thread. *) proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); var base: ^U8; t: ^Thread; size: I64; begin - size := Stack + Page; + size := Page + Stack + Alt + Page; base := Sys.mmap(size); if base = nil then return nil, Failed; end; (* mprotect: the guard page, no access; it fails when the process @@ -70,11 +87,14 @@ Sys.munmap(base, size); return nil, Failed; end; - (* the Thread at the top, the stack below it, aligned to 16 *) - t := ^Thread(base + size - 32); + (* from the top: the Thread in the last page, the handlers' stack, + then the thread's, starting on a page *) + t := ^Thread(base + size - Page); t^.base := base; t^.size := size; - if start(Flags, base + size - 32, ADR(t^.tid), ADR(t^.tid), p, arg) < 0 then + t^.fn := p; + t^.arg := arg; + if start(Flags, base + Page + Stack, ADR(t^.tid), ADR(t^.tid), run, ^U8(t)) < 0 then Sys.munmap(base, size); return nil, Failed; end; diff --git a/test/doc/Thread.expect b/test/doc/Thread.expect index 7de500c..4fd5dc1 100644 --- a/test/doc/Thread.expect +++ b/test/doc/Thread.expect @@ -2,9 +2,10 @@ Threads for programs without C, made with the clone system call. Each runs on a stack of its own, mmaped, with a page below it that can't be touched, so that running off the stack stops the program - rather than spoiling what lies below. join waits on the thread's id, - which the kernel clears, and wakes it for, when the thread ends; - a Mutex sleeps on a futex. A program with C threads uses C's, + rather than spoiling what lies below; above it, a little stack for + Rt's signal handlers, so that they can say so. join waits on the + thread's id, which the kernel clears, and wakes it for, when the + thread ends; a Mutex sleeps on a futex. A program with C threads uses C's, through pthread_create, and so can't use these. const Failed*: ERROR = "can't start a thread"; @@ -13,6 +14,8 @@ tid: I64; (* the kernel's id, until the thread ends *) base: ^U8; (* its stack's mapping, the guard page first *) size: I64; + fn: proc(arg: ^U8); (* what it runs, and with what *) + arg: ^U8; end; type Mutex* = record state: I64; end; diff --git a/test/run/ThreadOverflow.expect b/test/run/ThreadOverflow.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/ThreadOverflow.raz b/test/run/ThreadOverflow.raz new file mode 100644 index 0000000..e2c345c --- /dev/null +++ b/test/run/ThreadOverflow.raz @@ -0,0 +1,25 @@ +module ThreadOverflow; +(* A stack overflow in a thread is told as one in the main thread is: + the thread's mapping holds a stack for the signal handlers *) +import Thread; + +proc down(n: I64): I64; +var pad: [64]I64; +begin + pad[n mod 64] := n; + return down(n + 1) + pad[0]; +end; + +proc work(_arg: ^U8); +begin + down(0); +end; + +proc main*; +var t: ^Thread.Thread; e: ERROR; +begin + t, e := Thread.spawn(work, nil); + if e = nil then Thread.join(t); end; +end; + +end ThreadOverflow. diff --git a/test/run/ThreadOverflow.status b/test/run/ThreadOverflow.status new file mode 100644 index 0000000..897bdc8 --- /dev/null +++ b/test/run/ThreadOverflow.status @@ -0,0 +1 @@ +139 diff --git a/test/run/ThreadOverflow.stderr b/test/run/ThreadOverflow.stderr new file mode 100644 index 0000000..b4f3903 --- /dev/null +++ b/test/run/ThreadOverflow.stderr @@ -0,0 +1 @@ +test/run/ThreadOverflow.raz:6:1: stopped by a stack overflow