From 3a4fb62b1bff5b6c3dd7f4db0dd55b4bdd4029ec Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 09:52:17 +0100 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index c2adf1d..242bce8 100644 --- a/PLAN.md +++ b/PLAN.md @@ -141,7 +141,7 @@ deferred statements run, a `return`'s results kept in the frame meanwhile; `rax`, `rdx` and `rcx`, which division and shifts need, aren't in the stack. `make bench` still passes. -- [ ] 9a. Failures caught by the code where the machine can't: a +- [x] 9a. Failures caught by the code where the machine can't: a `nil` test before a dereference or a call through a procedure value, a stack limit at each procedure's start, a zero test before a division; chosen per target. A `nil` test is left out diff --git a/doc/compiler.md b/doc/compiler.md index 9f488dd..caa44dc 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -297,6 +297,18 @@ offending expression (`LineCol` in `scan.pas`), and the code after it the statement's again. +What the machine catches, the code doesn't test for (report section +10, appendix A): on x86-64 Linux a division by zero raises SIGFPE, a +stack overflow meets the guard page under the stack, a call to `nil` +jumps to an address never mapped, and so does a load or store within +`NilPage` bytes of address 0. `Rt`'s handlers say where and why. What +is left is a field of a `nil` pointer past that page: an item reached +through `^` is `deref` until `NilTest` (`item.pas`) has it used, and +gen's `CheckNil` tests the pointer only if the use reaches past +`NilPage`. A port whose machine catches less tests for more, in its +own `gen.pas`: `Rt.fail` stops for a nil pointer, a division by zero +and a stack overflow as the signals would. + Calls: the arguments are in frame slots by the time `Call` runs, one each, computed left to right; only then are they loaded into the registers System V wants, and the stack aligned for the call. A record diff --git a/lib/Rt.raz b/lib/Rt.raz index 00f525c..e5c23b1 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -150,10 +150,26 @@ status 2. what is 0 for an index a and 1 for a slice a..b, of something whose length is len; 2 for a, which is no position of the enumeration called name; 3 for a slice a..b of a pointer, which - runs backwards. *) + runs backwards. What a target's machine doesn't catch the code + tests for, and stops at with what 4, a nil pointer; 5, a division + by zero; 6, a stack overflow; these end the program by the signal + the machine would have given, as if it had. *) proc fail*(what, a, b, len: INT; at: UINT; name: ^U8); +var sig: INT; begin where(at); + if what >= 4 then + put("stopped by "); + sig := SigSegv; + if what = 4 then put("a nil pointer"); + elsif what = 5 then put("a division fault"); sig := SigFpe; + else put("a stack overflow"); + end; + say; + act := {}; (* the signal's default action *) + SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) + SYSCALL(62, SYSCALL(39), sig); (* kill(getpid(), sig) *) + end; if what = 0 then put("index "); putInt(a); diff --git a/src/gen.pas b/src/gen.pas index 442249e..dbf4adc 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -41,6 +41,12 @@ const NoLabel = -1; WordSize = 8; { the size of an address, and of INT } + { What the machine catches by itself, which the code needn't test for + (report section 10): on x86-64 Linux, a division by zero, a stack + overflow (at the guard page under the stack), a call to nil, and + a load or store within NilPage bytes of address 0, which is never + mapped. } + NilPage = 4096; type TKind = (kWord, kLong, kF32, kF64); @@ -117,6 +123,7 @@ procedure CheckIndex(n: int64; const p: TPos); procedure CheckEnum(n: int64; name: longint; const p: TPos); +procedure CheckNil(reach: int64; const p: TPos); procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); @@ -900,6 +907,18 @@ r := Top; CmpI(r, n); Check(ccB, 2, p, r, NoMem, n, name); +end; + +{ The address on top, from a pointer, which is to be used reach bytes + past: the program stops if it is nil, unless the machine would stop + it, as for a reach within NilPage. } +procedure CheckNil(reach: int64; const p: TPos); +var r: longint; +begin + if reach <= NilPage then exit; + r := Top; + TestRR(r, r); + Check(ccNE, 4, p, r, NoMem, 0, -1); end; { Pops an index, and the address of the slice at a if it is on the diff --git a/src/item.pas b/src/item.pas index 44d5156..5ef578b 100644 --- a/src/item.pas +++ b/src/item.pas @@ -34,6 +34,8 @@ odd: boolean; { mVar: in a packed record, so maybe misaligned } ro: boolean; { mVar: another module's exported variable, or part of one, which only that module changes } + deref: boolean; { mVar: at an address from a pointer, which may be + nil and isn't tested yet (NilTest) } obj: PObj; { mProc, mBuiltin, mCase; mVar: the variable named } pos: TPos; { where it is in the source, for messages } end; @@ -56,6 +58,7 @@ procedure Value(var x: TItem); procedure Address(var x: TItem); procedure Discard(const x: TItem); +procedure NilTest(var x: TItem); procedure Coerce(var x: TItem; t: PType); procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); procedure NotReadOnly(const x: TItem); @@ -297,11 +300,21 @@ end; end; +{ x, at an address from a pointer, is about to be used: gen tests the + address for nil where the machine wouldn't stop at it (report section + 10), from its offset to its end. } +procedure NilTest(var x: TItem); +begin + if (x.mode = mVar) and x.deref then CheckNil(x.off + x.typ^.size, x.pos); + x.deref := false; +end; + { x's value, on top of the stack } procedure Value(var x: TItem); var f: single; begin if x.typ^.form = fReal then UsesReals(x.pos); + NilTest(x); case x.mode of mConst: if (x.typ^.form = fError) and (x.val > 0) then PushAdr(GlobalAdr(x.val - 1)) @@ -327,6 +340,7 @@ { The address of the variable or string x, on top of the stack } procedure Address(var x: TItem); begin + NilTest(x); if x.mode = mStr then PushAdr(GlobalAdr(x.chunk)) else if x.mode = mVar then begin PushAdr(AdrOf(x)); @@ -486,7 +500,7 @@ if t^.form <> fPtr then MarkAt(x.pos, 'expected a pointer, found ' + TypeName(t)); if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared'); Value(x); - x.mode := mVar; x.base := bStack; x.off := 0; + x.mode := mVar; x.base := bStack; x.off := 0; x.deref := true; x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; end; @@ -506,6 +520,7 @@ procedure IndexPrep(var x: TItem); begin + NilTest(x); if x.mode = mStr then Coerce(x, tBytes); if (x.typ^.form = fPtr) and (x.typ^.base^.form = fArray) then MarkAt(x.pos, 'a pointer to an array is indexed as p^[i]'); diff --git a/src/parse.pas b/src/parse.pas index dfea876..c4e65f0 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -883,6 +883,7 @@ end; until false; x.pos := SpanFrom(start); + NilTest(x); end; procedure ArrayLiteral(var x: TItem; want: PType); diff --git a/test/run/NilBig.expect b/test/run/NilBig.expect new file mode 100644 index 0000000..90be1f3 --- /dev/null +++ b/test/run/NilBig.expect @@ -0,0 +1 @@ +before diff --git a/test/run/NilBig.raz b/test/run/NilBig.raz new file mode 100644 index 0000000..b60e2fc --- /dev/null +++ b/test/run/NilBig.raz @@ -0,0 +1,18 @@ +module NilBig; +(* A field of a nil pointer past the page at address 0 that is never + mapped, which the machine wouldn't catch: the code tests for nil *) +import Out; + +type Big = record pad: [100000]U8; last: I64; end; + +var p: ^Big; + +proc main*; +begin + Out.str("before"); Out.ln; + Out.flush; + p^.last := 1; + Out.str("after"); Out.ln; +end; + +end NilBig. diff --git a/test/run/NilBig.status b/test/run/NilBig.status new file mode 100644 index 0000000..897bdc8 --- /dev/null +++ b/test/run/NilBig.status @@ -0,0 +1 @@ +139 diff --git a/test/run/NilBig.stderr b/test/run/NilBig.stderr new file mode 100644 index 0000000..7231701 --- /dev/null +++ b/test/run/NilBig.stderr @@ -0,0 +1 @@ +test/run/NilBig.raz:14:5: stopped by a nil pointer