Step 9a: failures the code tests for where the machine can't gen.NilPage is what x86-64 Linux never maps at address 0 (4096 here, the page): a division by zero, a stack overflow, a call to nil and a load or store within it are the machine's to catch, as before. What's left: a field of a nil pointer past that page. Deref marks an item deref; NilTest, from Value, Address, IndexPrep and the end of a Designator, has gen.CheckNil test the address when the use reaches past NilPage (offset + size), and clears the mark. Rt.fail takes what 4, 5 and 6 (nil pointer, division fault, stack overflow) for a port that tests for them: it says so as the handler would, resets the signal and raises it, so the status is the signal's. test/run/NilBig: a field 100,000 bytes into a nil record.
| @@ -141,7 +141,7 @@ | ||
| 141 | 141 | deferred statements run, a `return`'s results kept in the frame |
| 142 | 142 | meanwhile; `rax`, `rdx` and `rcx`, which division and shifts |
| 143 | 143 | need, aren't in the stack. `make bench` still passes. |
| 144 | -- [ ] 9a. Failures caught by the code where the machine can't: a | |
| 144 | +- [x] 9a. Failures caught by the code where the machine can't: a | |
| 145 | 145 | `nil` test before a dereference or a call through a procedure |
| 146 | 146 | value, a stack limit at each procedure's start, a zero test |
| 147 | 147 | before a division; chosen per target. A `nil` test is left out |
| @@ -297,6 +297,18 @@ | ||
| 297 | 297 | offending expression (`LineCol` in `scan.pas`), and the code after it |
| 298 | 298 | the statement's again. |
| 299 | 299 | |
| 300 | +What the machine catches, the code doesn't test for (report section | |
| 301 | +10, appendix A): on x86-64 Linux a division by zero raises SIGFPE, a | |
| 302 | +stack overflow meets the guard page under the stack, a call to `nil` | |
| 303 | +jumps to an address never mapped, and so does a load or store within | |
| 304 | +`NilPage` bytes of address 0. `Rt`'s handlers say where and why. What | |
| 305 | +is left is a field of a `nil` pointer past that page: an item reached | |
| 306 | +through `^` is `deref` until `NilTest` (`item.pas`) has it used, and | |
| 307 | +gen's `CheckNil` tests the pointer only if the use reaches past | |
| 308 | +`NilPage`. A port whose machine catches less tests for more, in its | |
| 309 | +own `gen.pas`: `Rt.fail` stops for a nil pointer, a division by zero | |
| 310 | +and a stack overflow as the signals would. | |
| 311 | + | |
| 300 | 312 | Calls: the arguments are in frame slots by the time `Call` runs, one |
| 301 | 313 | each, computed left to right; only then are they loaded into the |
| 302 | 314 | registers System V wants, and the stack aligned for the call. A record |
| @@ -150,10 +150,26 @@ | ||
| 150 | 150 | status 2. what is 0 for an index a and 1 for a slice a..b, of |
| 151 | 151 | something whose length is len; 2 for a, which is no position of |
| 152 | 152 | the enumeration called name; 3 for a slice a..b of a pointer, which |
| 153 | - runs backwards. *) | |
| 153 | + runs backwards. What a target's machine doesn't catch the code | |
| 154 | + tests for, and stops at with what 4, a nil pointer; 5, a division | |
| 155 | + by zero; 6, a stack overflow; these end the program by the signal | |
| 156 | + the machine would have given, as if it had. *) | |
| 154 | 157 | proc fail*(what, a, b, len: INT; at: UINT; name: ^U8); |
| 158 | +var sig: INT; | |
| 155 | 159 | begin |
| 156 | 160 | where(at); |
| 161 | + if what >= 4 then | |
| 162 | + put("stopped by "); | |
| 163 | + sig := SigSegv; | |
| 164 | + if what = 4 then put("a nil pointer"); | |
| 165 | + elsif what = 5 then put("a division fault"); sig := SigFpe; | |
| 166 | + else put("a stack overflow"); | |
| 167 | + end; | |
| 168 | + say; | |
| 169 | + act := {}; (* the signal's default action *) | |
| 170 | + SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) | |
| 171 | + SYSCALL(62, SYSCALL(39), sig); (* kill(getpid(), sig) *) | |
| 172 | + end; | |
| 157 | 173 | if what = 0 then |
| 158 | 174 | put("index "); |
| 159 | 175 | putInt(a); |
| @@ -41,6 +41,12 @@ | ||
| 41 | 41 | const |
| 42 | 42 | NoLabel = -1; |
| 43 | 43 | WordSize = 8; { the size of an address, and of INT } |
| 44 | + { What the machine catches by itself, which the code needn't test for | |
| 45 | + (report section 10): on x86-64 Linux, a division by zero, a stack | |
| 46 | + overflow (at the guard page under the stack), a call to nil, and | |
| 47 | + a load or store within NilPage bytes of address 0, which is never | |
| 48 | + mapped. } | |
| 49 | + NilPage = 4096; | |
| 44 | 50 | |
| 45 | 51 | type |
| 46 | 52 | TKind = (kWord, kLong, kF32, kF64); |
| @@ -117,6 +123,7 @@ | ||
| 117 | 123 | |
| 118 | 124 | procedure CheckIndex(n: int64; const p: TPos); |
| 119 | 125 | procedure CheckEnum(n: int64; name: longint; const p: TPos); |
| 126 | +procedure CheckNil(reach: int64; const p: TPos); | |
| 120 | 127 | procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); |
| 121 | 128 | procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); |
| 122 | 129 | procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); |
| @@ -900,6 +907,18 @@ | ||
| 900 | 907 | r := Top; |
| 901 | 908 | CmpI(r, n); |
| 902 | 909 | Check(ccB, 2, p, r, NoMem, n, name); |
| 910 | +end; | |
| 911 | + | |
| 912 | +{ The address on top, from a pointer, which is to be used reach bytes | |
| 913 | + past: the program stops if it is nil, unless the machine would stop | |
| 914 | + it, as for a reach within NilPage. } | |
| 915 | +procedure CheckNil(reach: int64; const p: TPos); | |
| 916 | +var r: longint; | |
| 917 | +begin | |
| 918 | + if reach <= NilPage then exit; | |
| 919 | + r := Top; | |
| 920 | + TestRR(r, r); | |
| 921 | + Check(ccNE, 4, p, r, NoMem, 0, -1); | |
| 903 | 922 | end; |
| 904 | 923 | |
| 905 | 924 | { Pops an index, and the address of the slice at a if it is on the |
| @@ -34,6 +34,8 @@ | ||
| 34 | 34 | odd: boolean; { mVar: in a packed record, so maybe misaligned } |
| 35 | 35 | ro: boolean; { mVar: another module's exported variable, or part |
| 36 | 36 | of one, which only that module changes } |
| 37 | + deref: boolean; { mVar: at an address from a pointer, which may be | |
| 38 | + nil and isn't tested yet (NilTest) } | |
| 37 | 39 | obj: PObj; { mProc, mBuiltin, mCase; mVar: the variable named } |
| 38 | 40 | pos: TPos; { where it is in the source, for messages } |
| 39 | 41 | end; |
| @@ -56,6 +58,7 @@ | ||
| 56 | 58 | procedure Value(var x: TItem); |
| 57 | 59 | procedure Address(var x: TItem); |
| 58 | 60 | procedure Discard(const x: TItem); |
| 61 | +procedure NilTest(var x: TItem); | |
| 59 | 62 | procedure Coerce(var x: TItem; t: PType); |
| 60 | 63 | procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); |
| 61 | 64 | procedure NotReadOnly(const x: TItem); |
| @@ -297,11 +300,21 @@ | ||
| 297 | 300 | end; |
| 298 | 301 | end; |
| 299 | 302 | |
| 303 | +{ x, at an address from a pointer, is about to be used: gen tests the | |
| 304 | + address for nil where the machine wouldn't stop at it (report section | |
| 305 | + 10), from its offset to its end. } | |
| 306 | +procedure NilTest(var x: TItem); | |
| 307 | +begin | |
| 308 | + if (x.mode = mVar) and x.deref then CheckNil(x.off + x.typ^.size, x.pos); | |
| 309 | + x.deref := false; | |
| 310 | +end; | |
| 311 | + | |
| 300 | 312 | { x's value, on top of the stack } |
| 301 | 313 | procedure Value(var x: TItem); |
| 302 | 314 | var f: single; |
| 303 | 315 | begin |
| 304 | 316 | if x.typ^.form = fReal then UsesReals(x.pos); |
| 317 | + NilTest(x); | |
| 305 | 318 | case x.mode of |
| 306 | 319 | mConst: |
| 307 | 320 | if (x.typ^.form = fError) and (x.val > 0) then PushAdr(GlobalAdr(x.val - 1)) |
| @@ -327,6 +340,7 @@ | ||
| 327 | 340 | { The address of the variable or string x, on top of the stack } |
| 328 | 341 | procedure Address(var x: TItem); |
| 329 | 342 | begin |
| 343 | + NilTest(x); | |
| 330 | 344 | if x.mode = mStr then PushAdr(GlobalAdr(x.chunk)) |
| 331 | 345 | else if x.mode = mVar then begin |
| 332 | 346 | PushAdr(AdrOf(x)); |
| @@ -486,7 +500,7 @@ | ||
| 486 | 500 | if t^.form <> fPtr then MarkAt(x.pos, 'expected a pointer, found ' + TypeName(t)); |
| 487 | 501 | if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared'); |
| 488 | 502 | Value(x); |
| 489 | - x.mode := mVar; x.base := bStack; x.off := 0; | |
| 503 | + x.mode := mVar; x.base := bStack; x.off := 0; x.deref := true; | |
| 490 | 504 | x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; |
| 491 | 505 | end; |
| 492 | 506 | |
| @@ -506,6 +520,7 @@ | ||
| 506 | 520 | |
| 507 | 521 | procedure IndexPrep(var x: TItem); |
| 508 | 522 | begin |
| 523 | + NilTest(x); | |
| 509 | 524 | if x.mode = mStr then Coerce(x, tBytes); |
| 510 | 525 | if (x.typ^.form = fPtr) and (x.typ^.base^.form = fArray) then |
| 511 | 526 | MarkAt(x.pos, 'a pointer to an array is indexed as p^[i]'); |
| @@ -883,6 +883,7 @@ | ||
| 883 | 883 | end; |
| 884 | 884 | until false; |
| 885 | 885 | x.pos := SpanFrom(start); |
| 886 | + NilTest(x); | |
| 886 | 887 | end; |
| 887 | 888 | |
| 888 | 889 | procedure ArrayLiteral(var x: TItem; want: PType); |
| @@ -0,0 +1 @@ | ||
| 1 | +before |
| @@ -0,0 +1,18 @@ | ||
| 1 | +module NilBig; | |
| 2 | +(* A field of a nil pointer past the page at address 0 that is never | |
| 3 | + mapped, which the machine wouldn't catch: the code tests for nil *) | |
| 4 | +import Out; | |
| 5 | + | |
| 6 | +type Big = record pad: [100000]U8; last: I64; end; | |
| 7 | + | |
| 8 | +var p: ^Big; | |
| 9 | + | |
| 10 | +proc main*; | |
| 11 | +begin | |
| 12 | + Out.str("before"); Out.ln; | |
| 13 | + Out.flush; | |
| 14 | + p^.last := 1; | |
| 15 | + Out.str("after"); Out.ln; | |
| 16 | +end; | |
| 17 | + | |
| 18 | +end NilBig. |
| @@ -0,0 +1 @@ | ||
| 1 | +139 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/NilBig.raz:14:5: stopped by a nil pointer |