raz / commit ef0484e1

commit
ef0484e1445c2aa74bf2a045cf8cc22dd279a5ad
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
823ebfd6
Heap's debugging mode: RAZ_HEAP=debug or Heap.debug(true)

Blocks given in it carry 8 guard bytes, checked when freed; a freed
block is filled with 0DEH and held in a quarantine of the last 1,024
frees, checked as it leaves. A break stops the program with the
block's address and size and the calls under way, the heap's lock let
go first. Normal mode is untouched: debug swaps std.alloc, and free
looks for a debug block only past its own check. make check runs the
library's tests again under it. Breaks no program.

patch · log

12 files changed, +288 -8

doc/raz-lib.md (history) modified +19 -2

@@ -24,7 +24,7 @@
2424 | `Task` | a thread given a value of its own, `Task(T)`: `spawn` copies it, with no cast |
2525 | `Guarded` | a value with its lock, `Guarded(T)`: reached only through `with` |
2626 | `Mem` | `Allocator`, the interface of every allocator, and pages from the system |
27-| `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program |
27+| `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program; `RAZ_HEAP=debug` finds writes past a block's end and after free |
2828 | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator |
2929 | `Str` | operations on `[]U8` strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text |
3030 | `Bytes` | searches of bytes, many at a time: `index`, `last`, `count` and `find` in a `[]U8`; one file per target where the target has instructions for it |
@@ -85,6 +85,7 @@
8585 proc free*(a: ^Allocator; p: ^U8); (* a^.free(a, p) *)
8686
8787 module Heap;
88+proc debug*(on: BOOL); (* debug mode on or off, for the blocks given from now on *)
8889 var heap*: ^Mem.Allocator;
8990 var sure*: ^Mem.Allocator; (* heap, but running out of memory stops the program *)
9091
@@ -103,7 +104,23 @@
103104 `NEW(p, a)` calls `a^.alloc`. `Heap` is the general
104105 allocator; `Heap.heap` gives `nil` when memory runs out, and `Heap.sure`
105106 stops the program instead, as `Buf` does, so that `NEW(p, Heap.sure)`
106-needs no check. An arena hands out memory from
107+needs no check.
108+
109+`Heap` has a debug mode, a light AddressSanitizer, chosen by `RAZ_HEAP=debug`
110+in the environment or by `Heap.debug(true)`. Normal mode pays nothing for
111+it. In debug mode each block given has 8 guard bytes after its last, and
112+a freed block is filled with `0DEH` bytes and held in a quarantine, the
113+last 1,024 frees, before it may be given again. A broken guard, a write
114+past the block's end, is found when the block is freed; a block that
115+leaves the quarantine with a byte not `0DEH`, a write after free, is
116+found then. Either stops the program, saying what was found and the
117+block's address and size, with the calls under way, and status 2. A big
118+block, which has pages of its own, is not quarantined: its guard is
119+checked and its pages unmapped, so a later use of it faults. `debug`
120+changes only the blocks given after it, so call it before the first
121+allocation; a block is freed as it was given, in either mode. Only writes
122+are found, not reads, and not those past the guard or after the
123+quarantine. An arena hands out memory from
107124 one block: its `free` does nothing, and `reset` forgets everything.
108125
109126 ```

lib/Heap.raz (history) modified +206 -5

@@ -5,7 +5,26 @@
55 of its pages for a big one, which start less than a page before it;
66 then a marker, which says the block is out, so that freeing it
77 twice, or freeing what the heap never gave out, stops the program.
8- One lock keeps it whole across threads. *)
8+ One lock keeps it whole across threads.
9+
10+ Debug mode, a light AddressSanitizer: RAZ_HEAP=debug in the
11+ environment, or debug(true), makes the blocks given out from then on
12+ carry 8 guard bytes after their last, and a freed block is filled
13+ with 0DEH and held in a quarantine, the last 1,024 frees, before it
14+ may be given again. A write past a block's end breaks its guard,
15+ found when it is freed; a write to a freed block breaks its fill,
16+ found when it leaves the quarantine. Either stops the program, with
17+ the block's address and size and the calls under way. It costs
18+ normal mode nothing: debug swaps the allocator's alloc, and free
19+ looks at what kind of block it has only past its own check.
20+
21+ header the block's bytes guard (slack) size
22+ |16 bytes|<-- size -------->|8 bytes| |8 bytes|
23+ the block's last 8
24+
25+ It depends on Mem, Lock, Rt and Sys. It leaves out the checks of a
26+ read past the end or before a block, and of a write after free once
27+ the block has left the quarantine. *)
928 module Heap;
1029 import Mem, Lock, Rt, Sys;
1130
@@ -15,6 +34,12 @@
1534 Refill = 65536;
1635 Largest = 1 shl 46; (* bigger sizes are refused *)
1736 Out = 5A6564426C6F636BH; (* the marker, xor the header's address *)
37+ Dbg = 5A6564446267626BH; (* a debug block's marker, the same way *)
38+ Guard = 0A5H; (* the byte of the guard after a debug block *)
39+ GuardLen = 8;
40+ Dead = 0DEH; (* the byte a freed debug block is filled with *)
41+ Digits = "0123456789abcdef"; (* for the numbers a stop says *)
42+ Held = 1024; (* how many freed debug blocks are quarantined *)
1843
1944 type
2045 Block = record next: ^Block; end;
@@ -25,6 +50,9 @@
2550 cur: ^U8; (* where the next new small block comes from *)
2651 left: INT;
2752 lock: Lock.Lock;
53+ held: [Held]^Head; (* the quarantine: a ring of freed debug blocks' headers *)
54+ oldest: INT; (* where in held the next freed block goes, and the oldest is *)
55+ debugOn: BOOL;
2856 std, surer: Mem.Allocator;
2957 (* The heap, for NEW(p, Heap.heap) and Mem's procedures. *)
3058 heap*: ^Mem.Allocator;
@@ -87,8 +115,133 @@
87115 proc pageOf(a: INT): INT;
88116 begin
89117 return a - a & (Mem.Page - 1);
118+end;
119+
120+(* Where the block with header h ends: its class's size, or its pages'. *)
121+proc endOf(h: ^Head): INT;
122+begin
123+ if h^.size < 0 then return pageOf(INT(h)) - h^.size; end;
124+ return INT(h) + (16 shl h^.size);
125+end;
126+
127+(* The size a debug block was asked for, kept in its last 8 bytes. *)
128+proc askedSize(h: ^Head): INT;
129+var w: ^INT;
130+begin
131+ w := ^INT(endOf(h) - 8);
132+ return w^;
133+end;
134+
135+(* Stops the program, saying what was found of the debug block with
136+ header h, its address and its size, and the calls under way. *)
137+proc found(what: []U8; h: ^Head);
138+var d: [20]U8; i, n: INT;
139+begin
140+ _ := Sys.write(Sys.stderr, "Heap: ");
141+ _ := Sys.write(Sys.stderr, what);
142+ _ := Sys.write(Sys.stderr, ": block at 0x");
143+ n := INT(h) + Header;
144+ i := LEN(d);
145+ repeat
146+ DEC(i);
147+ d[i] := Digits[n & 15];
148+ n := n shr 4;
149+ until n = 0;
150+ _ := Sys.write(Sys.stderr, d[i..LEN(d)]);
151+ _ := Sys.write(Sys.stderr, ", ");
152+ n := askedSize(h);
153+ i := LEN(d);
154+ repeat
155+ DEC(i);
156+ d[i] := Digits[n mod 10];
157+ n := n div 10;
158+ until n = 0;
159+ _ := Sys.write(Sys.stderr, d[i..LEN(d)]);
160+ _ := Sys.write(Sys.stderr, " bytes\n");
161+ Rt.trace;
162+ Rt.exit(2);
163+end;
164+
165+(* found, with the heap's lock held, which is let go first, so that
166+ what runs as the program ends may use the heap *)
167+proc badBlock(what: []U8; h: ^Head);
168+begin
169+ Lock.give(lock);
170+ found(what, h);
171+end;
172+
173+(* Whether the guard after debug block h is whole. *)
174+proc guardWhole(h: ^Head): BOOL;
175+var p: ^U8; i, size: INT;
176+begin
177+ p := ^U8(h) + Header;
178+ size := askedSize(h);
179+ for i := 0 to GuardLen - 1 do
180+ if p[size + i] # Guard then return false; end;
181+ end;
182+ return true;
183+end;
184+
185+(* Whether the freed debug block h is still all 0DEH. *)
186+proc stillDead(h: ^Head): BOOL;
187+var p: ^U8; i: INT;
188+begin
189+ p := ^U8(h) + Header;
190+ for i := 0 to askedSize(h) - 1 do
191+ if p[i] # Dead then return false; end;
192+ end;
193+ return true;
90194 end;
91195
196+(* Gives size bytes, as alloc does, but in debug mode: the block has a
197+ guard after it, and its last 8 bytes hold size. *)
198+proc allocDebug(_a: ^Mem.Allocator; size, align: INT): ^U8;
199+var p: ^U8; h: ^Head; w: ^INT; i: INT;
200+begin
201+ if (size < 0) or (size > Largest) then return nil; end;
202+ p := alloc(_a, size + GuardLen + 8, align);
203+ if p = nil then return nil; end;
204+ h := ^Head(p - Header);
205+ h^.mark := Dbg xor INT(h);
206+ for i := 0 to GuardLen - 1 do p[size + i] := Guard; end;
207+ w := ^INT(endOf(h) - 8);
208+ w^ := size;
209+ return p;
210+end;
211+
212+(* Frees the debug block h, with the lock held, which it lets go. The
213+ guard is checked, and the block filled with 0DEH and quarantined;
214+ the block that leaves the quarantine is checked to be still all 0DEH
215+ and guarded, and goes to its free list. A big block is not held: its
216+ guard is checked and its pages unmapped, so any use of it faults. *)
217+proc freeDebug(h: ^Head);
218+var out: ^Head; p: ^U8; i, size: INT; b: ^Block;
219+begin
220+ if not guardWhole(h) then badBlock("a write past the block's end", h); end;
221+ h^.mark := 0;
222+ if h^.size < 0 then
223+ Lock.give(lock);
224+ Mem.unmap(^U8(pageOf(INT(h))), -h^.size);
225+ return;
226+ end;
227+ size := askedSize(h);
228+ p := ^U8(h) + Header;
229+ FILL(p[0..size], Dead);
230+ out := held[oldest];
231+ held[oldest] := h;
232+ oldest := (oldest + 1) mod Held;
233+ if out # nil then
234+ if not stillDead(out) then badBlock("a write after free", out); end;
235+ if not guardWhole(out) then badBlock("a write past the block's end", out); end;
236+ i := out^.size;
237+ b := ^Block(out);
238+ b^.next := lists[i];
239+ lists[i] := b;
240+ end;
241+ Lock.give(lock);
242+end;
243+
244+(* Gives back the block p, which alloc or allocDebug gave. *)
92245 proc free(_a: ^Mem.Allocator; p: ^U8);
93246 var h: ^Head; b: ^Block; c: INT;
94247 begin
@@ -96,6 +249,7 @@
96249 h := ^Head(p - Header);
97250 Lock.take(lock);
98251 if h^.mark # Out xor INT(h) then
252+ if h^.mark = Dbg xor INT(h) then freeDebug(h); return; end;
99253 Rt.stop("Heap: a block freed twice, or never given out");
100254 end;
101255 h^.mark := 0;
@@ -111,12 +265,21 @@
111265 end;
112266 end;
113267
268+(* Debug mode on or off, for the blocks given from now on; a block
269+ given before is freed as it was given, in either mode. Call it
270+ before the first allocation to cover them all. *)
271+proc debug*(on: BOOL);
272+begin
273+ debugOn := on;
274+ if on then std.alloc := allocDebug; else std.alloc := alloc; end;
275+end;
276+
114277 (* The heap's alloc, but with no room the program stops, with the
115278 calls under way. *)
116-proc allocSure(a: ^Mem.Allocator; size, align: INT): ^U8;
279+proc allocSure(_a: ^Mem.Allocator; size, align: INT): ^U8;
117280 var p: ^U8;
118281 begin
119- p := alloc(a, size, align);
282+ p := std.alloc(ADR(std), size, align);
120283 if p = nil then
121284 _ := Sys.write(Sys.stderr, "Heap: out of memory\n");
122285 Rt.trace;
@@ -125,6 +288,38 @@
125288 return p;
126289 end;
127290
291+(* The debug mode's test: a block written past its end, or after it is
292+ freed, is found; a block's bytes, in a small and in a big one, are
293+ its own. The stop itself is test/run/HeapDebug. Mode is left as found. *)
294+proc debugTest;
295+var was: BOOL; p: ^U8; h: ^Head; size, i: INT;
296+begin
297+ was := debugOn;
298+ debug(true);
299+ for i := 0 to 1 do
300+ size := 100 + i * 9900; (* a small block, then a big one *)
301+ p := Mem.alloc(heap, size, 8);
302+ h := ^Head(p - Header);
303+ if (p = nil) or (h^.mark # Dbg xor INT(h)) or (askedSize(h) # size) or not guardWhole(h) then
304+ Rt.stop("Heap: debug block");
305+ end;
306+ FILL(p[0..size], 1);
307+ p[size] := 0; (* one past the end *)
308+ if guardWhole(h) then Rt.stop("Heap: a write past the end isn't found"); end;
309+ p[size] := Guard;
310+ if size < 4096 then
311+ Mem.free(heap, p);
312+ if not stillDead(h) then Rt.stop("Heap: freed block not filled"); end;
313+ p[7] := 0; (* after free *)
314+ if stillDead(h) then Rt.stop("Heap: a write after free isn't found"); end;
315+ p[7] := Dead;
316+ else
317+ Mem.free(heap, p);
318+ end;
319+ end;
320+ debug(was);
321+end;
322+
128323 (* The module's test: blocks of every class and big ones, written,
129324 freed and taken again. *)
130325 proc main*;
@@ -145,14 +340,19 @@
145340 end;
146341 Mem.free(heap, p[5]);
147342 again := Mem.alloc(heap, (25 * 37) mod 9000 + 1, 8);
148- if again # p[5] then Rt.stop("Heap: a freed block isn't taken again"); end;
149- for i := 0 to 39 do Mem.free(heap, p[i]); end;
343+ if (again = p[5]) = debugOn then Rt.stop("Heap: a freed block's reuse"); end;
344+ (* in debug mode again is another block, and p[5] is freed already *)
345+ if debugOn then Mem.free(heap, again); end;
346+ for i := 0 to 39 do
347+ if not debugOn or (i # 5) then Mem.free(heap, p[i]); end;
348+ end;
150349 NEW(q, sure);
151350 if (q = nil) or (INT(q) mod 8 # 0) then Rt.stop("Heap: sure"); end;
152351 q^ := 7;
153352 Mem.free(sure, ^U8(q));
154353 if Mem.alloc(heap, -1, 8) # nil then Rt.stop("Heap: negative size"); end;
155354 if Mem.alloc(heap, 7FFFFFFFFFFFFFFFH - 4, 8) # nil then Rt.stop("Heap: huge size"); end;
355+ debugTest;
156356 end;
157357
158358 begin
@@ -160,4 +360,5 @@
160360 heap := ADR(std);
161361 surer := {alloc: allocSure, free: free};
162362 sure := ADR(surer);
363+ if Sys.env("RAZ_HEAP") = "debug" then debug(true); end;
163364 end Heap.

test/check.sh (history) modified +12 -0

@@ -290,6 +290,18 @@
290290 else
291291 echo "FAIL tools/Test.raz:"; cat $out/raztest.out; fail=$((fail + 1))
292292 fi
293+ # and again with the debug heap, RAZ_HEAP=debug: guards after blocks,
294+ # freed blocks filled and quarantined
295+ RAZ_HEAP=debug RAZ=$raz $out/raztest -j $(nproc 2>/dev/null || echo 1) -o $out/libdebug \
296+ $(for f in lib/*.raz; do [ $f != lib/Sys.raz ] && grep -q '^proc main\*' $f && echo $f; done) \
297+ > $out/raztest-debug.out 2>&1
298+ set -- $(tail -1 $out/raztest-debug.out)
299+ if [ "$2" = passed, ] && [ "$6" = timed ]; then
300+ pass=$((pass + $1)); fail=$((fail + $3 + $5))
301+ [ $(($3 + $5)) = 0 ] || { echo "RAZ_HEAP=debug:"; grep -v '^ok ' $out/raztest-debug.out; }
302+ else
303+ echo "FAIL tools/Test.raz, RAZ_HEAP=debug:"; cat $out/raztest-debug.out; fail=$((fail + 1))
304+ fi
293305 else
294306 echo "FAIL tools/Test.raz: $(cat $out/raztest.cerr)"; fail=$((fail + 1))
295307 fi

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

An empty file.

test/run/HeapDebugEnd.raz (history) added +15 -0

@@ -0,0 +1,15 @@
1+module HeapDebugEnd;
2+(* in debug mode, a write one byte past a block's end is found when the
3+ block is freed, and stops the program *)
4+import Heap, Mem;
5+
6+proc main*;
7+var p: ^U8;
8+begin
9+ Heap.debug(true);
10+ p := Mem.alloc(Heap.heap, 20, 8);
11+ p[20] := 1;
12+ Mem.free(Heap.heap, p);
13+end;
14+
15+end HeapDebugEnd.

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

@@ -0,0 +1 @@
1+2

test/run/HeapDebugEnd.stderr (history) added +7 -0

@@ -0,0 +1,7 @@
1+Heap: a write past the block's end: block at 0x?, 20 bytes
2+ in Heap.found at lib/Heap.raz:161:5: h = 0x?, i = 18, n = 0
3+ in Heap.badBlock at lib/Heap.raz:170:5: h = 0x?
4+ in Heap.freeDebug at lib/Heap.raz:220:31: h = 0x?, out = nil, p = nil, i = 0, size = 0, b = nil
5+ in Heap.free at lib/Heap.raz:252:42: _a = 0x?, p = 0x?, h = 0x?, b = nil, c = 0
6+ in Mem.free at lib/Mem.raz:39:5: a = 0x?, p = 0x?
7+ in HeapDebugEnd.main at test/run/HeapDebugEnd.raz:12:5: p = 0x?

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

An empty file.

test/run/HeapDebugFree.raz (history) added +19 -0

@@ -0,0 +1,19 @@
1+module HeapDebugFree;
2+(* in debug mode, a write to a freed block is found when the block
3+ leaves the quarantine, 1,024 frees later, and stops the program *)
4+import Heap, Mem;
5+
6+proc main*;
7+var p, q: ^U8; i: INT;
8+begin
9+ Heap.debug(true);
10+ p := Mem.alloc(Heap.heap, 20, 8);
11+ Mem.free(Heap.heap, p);
12+ p[3] := 1;
13+ for i := 0 to 1024 do
14+ q := Mem.alloc(Heap.heap, 20, 8);
15+ Mem.free(Heap.heap, q);
16+ end;
17+end;
18+
19+end HeapDebugFree.

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

@@ -0,0 +1 @@
1+2

test/run/HeapDebugFree.stderr (history) added +7 -0

@@ -0,0 +1,7 @@
1+Heap: a write after free: block at 0x?, 20 bytes
2+ in Heap.found at lib/Heap.raz:161:5: h = 0x?, i = 18, n = 0
3+ in Heap.badBlock at lib/Heap.raz:170:5: h = 0x?
4+ in Heap.freeDebug at lib/Heap.raz:234:36: h = 0x?, out = 0x?, p = 0x?, i = 0, size = 20, b = nil
5+ in Heap.free at lib/Heap.raz:252:42: _a = 0x?, p = 0x?, h = 0x?, b = nil, c = 0
6+ in Mem.free at lib/Mem.raz:39:5: a = 0x?, p = 0x?
7+ in HeapDebugFree.main at test/run/HeapDebugFree.raz:15:9: p = 0x?, q = 0x?, i = 1023

test/run/HeapSure.stderr (history) modified +1 -1

@@ -1,3 +1,3 @@
11 Heap: out of memory
2- in Heap.allocSure at lib/Heap.raz:122:9: a = 0x?, size = 9223372036854775803, align = 8, p = nil
2+ in Heap.allocSure at lib/Heap.raz:285:9: _a = 0x?, size = 9223372036854775803, align = 8, p = nil
33 in HeapSure.main at test/run/HeapSure.raz:8:5