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.
| @@ -121,9 +121,10 @@ | ||
| 121 | 121 | - A failed check says what failed and with what values, at the place |
| 122 | 122 | of the offending expression. A signal is told at the place of the |
| 123 | 123 | 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. | |
| 127 | 128 | - The smallest `I64` divided by -1 traps (SIGFPE), as 6.1 says. |
| 128 | 129 | - Warnings come only for a module with no errors. A procedure naming |
| 129 | 130 | itself doesn't use it; a type named by a pointer in its own |
| @@ -1,9 +1,10 @@ | ||
| 1 | 1 | (* Threads for programs without C, made with the clone system call. |
| 2 | 2 | Each runs on a stack of its own, mmaped, with a page below it that |
| 3 | 3 | 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, | |
| 7 | 8 | through pthread_create, and so can't use these. *) |
| 8 | 9 | module Thread; |
| 9 | 10 | import Sys, Rt; |
| @@ -12,6 +13,7 @@ | ||
| 12 | 13 | const |
| 13 | 14 | Failed*: ERROR = "can't start a thread"; |
| 14 | 15 | Stack = 1 shl 20; |
| 16 | + Alt = 16384; (* the signal handlers' stack *) | |
| 15 | 17 | Page = 4096; |
| 16 | 18 | (* clone: share memory, files, signal handlers, the thread group; |
| 17 | 19 | write the id to tid, and clear it at the end *) |
| @@ -25,7 +27,11 @@ | ||
| 25 | 27 | tid: I64; (* the kernel's id, until the thread ends *) |
| 26 | 28 | base: ^U8; (* its stack's mapping, the guard page first *) |
| 27 | 29 | size: I64; |
| 30 | + fn: proc(arg: ^U8); (* what it runs, and with what *) | |
| 31 | + arg: ^U8; | |
| 28 | 32 | end; |
| 33 | + (* What sigaltstack takes *) | |
| 34 | + AltStack = record sp: ^U8; flags, size: I64; end; | |
| 29 | 35 | (* 0 free, 1 taken, 2 taken with a thread waiting *) |
| 30 | 36 | Mutex* = record state: I64; end; |
| 31 | 37 | |
| @@ -57,11 +63,22 @@ | ||
| 57 | 63 | return SYSCALL(202, word, op, val, 0); |
| 58 | 64 | end; |
| 59 | 65 | |
| 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 | + | |
| 60 | 77 | (* Starts p(arg) in a new thread. *) |
| 61 | 78 | proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); |
| 62 | 79 | var base: ^U8; t: ^Thread; size: I64; |
| 63 | 80 | begin |
| 64 | - size := Stack + Page; | |
| 81 | + size := Page + Stack + Alt + Page; | |
| 65 | 82 | base := Sys.mmap(size); |
| 66 | 83 | if base = nil then return nil, Failed; end; |
| 67 | 84 | (* mprotect: the guard page, no access; it fails when the process |
| @@ -70,11 +87,14 @@ | ||
| 70 | 87 | Sys.munmap(base, size); |
| 71 | 88 | return nil, Failed; |
| 72 | 89 | 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); | |
| 75 | 93 | t^.base := base; |
| 76 | 94 | 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 | |
| 78 | 98 | Sys.munmap(base, size); |
| 79 | 99 | return nil, Failed; |
| 80 | 100 | end; |
| @@ -2,9 +2,10 @@ | ||
| 2 | 2 | Threads for programs without C, made with the clone system call. |
| 3 | 3 | Each runs on a stack of its own, mmaped, with a page below it that |
| 4 | 4 | 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, | |
| 8 | 9 | through pthread_create, and so can't use these. |
| 9 | 10 | |
| 10 | 11 | const Failed*: ERROR = "can't start a thread"; |
| @@ -13,6 +14,8 @@ | ||
| 13 | 14 | tid: I64; (* the kernel's id, until the thread ends *) |
| 14 | 15 | base: ^U8; (* its stack's mapping, the guard page first *) |
| 15 | 16 | size: I64; |
| 17 | + fn: proc(arg: ^U8); (* what it runs, and with what *) | |
| 18 | + arg: ^U8; | |
| 16 | 19 | end; |
| 17 | 20 | |
| 18 | 21 | type Mutex* = record state: I64; end; |
An empty file.
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +139 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/ThreadOverflow.raz:6:1: stopped by a stack overflow |