raz / commit 3a4fb62b

commit
3a4fb62b1bff5b6c3dd7f4db0dd55b4bdd4029ec
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
36efcd0f
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.

patch · log

10 files changed, +87 -3

PLAN.md (history) modified +1 -1

@@ -141,7 +141,7 @@
141141 deferred statements run, a `return`'s results kept in the frame
142142 meanwhile; `rax`, `rdx` and `rcx`, which division and shifts
143143 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
145145 `nil` test before a dereference or a call through a procedure
146146 value, a stack limit at each procedure's start, a zero test
147147 before a division; chosen per target. A `nil` test is left out

doc/compiler.md (history) modified +12 -0

@@ -297,6 +297,18 @@
297297 offending expression (`LineCol` in `scan.pas`), and the code after it
298298 the statement's again.
299299
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+
300312 Calls: the arguments are in frame slots by the time `Call` runs, one
301313 each, computed left to right; only then are they loaded into the
302314 registers System V wants, and the stack aligned for the call. A record

lib/Rt.raz (history) modified +17 -1

@@ -150,10 +150,26 @@
150150 status 2. what is 0 for an index a and 1 for a slice a..b, of
151151 something whose length is len; 2 for a, which is no position of
152152 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. *)
154157 proc fail*(what, a, b, len: INT; at: UINT; name: ^U8);
158+var sig: INT;
155159 begin
156160 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;
157173 if what = 0 then
158174 put("index ");
159175 putInt(a);

src/gen.pas modified +19 -0

@@ -41,6 +41,12 @@
4141 const
4242 NoLabel = -1;
4343 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;
4450
4551 type
4652 TKind = (kWord, kLong, kF32, kF64);
@@ -117,6 +123,7 @@
117123
118124 procedure CheckIndex(n: int64; const p: TPos);
119125 procedure CheckEnum(n: int64; name: longint; const p: TPos);
126+procedure CheckNil(reach: int64; const p: TPos);
120127 procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos);
121128 procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos);
122129 procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos);
@@ -900,6 +907,18 @@
900907 r := Top;
901908 CmpI(r, n);
902909 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);
903922 end;
904923
905924 { Pops an index, and the address of the slice at a if it is on the

src/item.pas modified +16 -1

@@ -34,6 +34,8 @@
3434 odd: boolean; { mVar: in a packed record, so maybe misaligned }
3535 ro: boolean; { mVar: another module's exported variable, or part
3636 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) }
3739 obj: PObj; { mProc, mBuiltin, mCase; mVar: the variable named }
3840 pos: TPos; { where it is in the source, for messages }
3941 end;
@@ -56,6 +58,7 @@
5658 procedure Value(var x: TItem);
5759 procedure Address(var x: TItem);
5860 procedure Discard(const x: TItem);
61+procedure NilTest(var x: TItem);
5962 procedure Coerce(var x: TItem; t: PType);
6063 procedure CoerceExact(var x: TItem; t: PType; const help: shortstring);
6164 procedure NotReadOnly(const x: TItem);
@@ -297,11 +300,21 @@
297300 end;
298301 end;
299302
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+
300312 { x's value, on top of the stack }
301313 procedure Value(var x: TItem);
302314 var f: single;
303315 begin
304316 if x.typ^.form = fReal then UsesReals(x.pos);
317+ NilTest(x);
305318 case x.mode of
306319 mConst:
307320 if (x.typ^.form = fError) and (x.val > 0) then PushAdr(GlobalAdr(x.val - 1))
@@ -327,6 +340,7 @@
327340 { The address of the variable or string x, on top of the stack }
328341 procedure Address(var x: TItem);
329342 begin
343+ NilTest(x);
330344 if x.mode = mStr then PushAdr(GlobalAdr(x.chunk))
331345 else if x.mode = mVar then begin
332346 PushAdr(AdrOf(x));
@@ -486,7 +500,7 @@
486500 if t^.form <> fPtr then MarkAt(x.pos, 'expected a pointer, found ' + TypeName(t));
487501 if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared');
488502 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;
490504 x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false;
491505 end;
492506
@@ -506,6 +520,7 @@
506520
507521 procedure IndexPrep(var x: TItem);
508522 begin
523+ NilTest(x);
509524 if x.mode = mStr then Coerce(x, tBytes);
510525 if (x.typ^.form = fPtr) and (x.typ^.base^.form = fArray) then
511526 MarkAt(x.pos, 'a pointer to an array is indexed as p^[i]');

src/parse.pas modified +1 -0

@@ -883,6 +883,7 @@
883883 end;
884884 until false;
885885 x.pos := SpanFrom(start);
886+ NilTest(x);
886887 end;
887888
888889 procedure ArrayLiteral(var x: TItem; want: PType);

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

@@ -0,0 +1 @@
1+before

test/run/NilBig.raz (history) added +18 -0

@@ -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.

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

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

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

@@ -0,0 +1 @@
1+test/run/NilBig.raz:14:5: stopped by a nil pointer