| | @@ -5,7 +5,26 @@ |
| 5 | 5 | of its pages for a big one, which start less than a page before it; |
| 6 | 6 | then a marker, which says the block is out, so that freeing it |
| 7 | 7 | 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. *) |
| 9 | 28 | module Heap; |
| 10 | 29 | import Mem, Lock, Rt, Sys; |
| 11 | 30 | |
| | @@ -15,6 +34,12 @@ |
| 15 | 34 | Refill = 65536; |
| 16 | 35 | Largest = 1 shl 46; (* bigger sizes are refused *) |
| 17 | 36 | 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 *) |
| 18 | 43 | |
| 19 | 44 | type |
| 20 | 45 | Block = record next: ^Block; end; |
| | @@ -25,6 +50,9 @@ |
| 25 | 50 | cur: ^U8; (* where the next new small block comes from *) |
| 26 | 51 | left: INT; |
| 27 | 52 | 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; |
| 28 | 56 | std, surer: Mem.Allocator; |
| 29 | 57 | (* The heap, for NEW(p, Heap.heap) and Mem's procedures. *) |
| 30 | 58 | heap*: ^Mem.Allocator; |
| | @@ -87,8 +115,133 @@ |
| 87 | 115 | proc pageOf(a: INT): INT; |
| 88 | 116 | begin |
| 89 | 117 | 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; |
| 90 | 194 | end; |
| 91 | 195 | |
| 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. *) |
| 92 | 245 | proc free(_a: ^Mem.Allocator; p: ^U8); |
| 93 | 246 | var h: ^Head; b: ^Block; c: INT; |
| 94 | 247 | begin |
| | @@ -96,6 +249,7 @@ |
| 96 | 249 | h := ^Head(p - Header); |
| 97 | 250 | Lock.take(lock); |
| 98 | 251 | if h^.mark # Out xor INT(h) then |
| 252 | + if h^.mark = Dbg xor INT(h) then freeDebug(h); return; end; |
| 99 | 253 | Rt.stop("Heap: a block freed twice, or never given out"); |
| 100 | 254 | end; |
| 101 | 255 | h^.mark := 0; |
| | @@ -111,12 +265,21 @@ |
| 111 | 265 | end; |
| 112 | 266 | end; |
| 113 | 267 | |
| 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 | + |
| 114 | 277 | (* The heap's alloc, but with no room the program stops, with the |
| 115 | 278 | calls under way. *) |
| 116 | | -proc allocSure(a: ^Mem.Allocator; size, align: INT): ^U8; |
| 279 | +proc allocSure(_a: ^Mem.Allocator; size, align: INT): ^U8; |
| 117 | 280 | var p: ^U8; |
| 118 | 281 | begin |
| 119 | | - p := alloc(a, size, align); |
| 282 | + p := std.alloc(ADR(std), size, align); |
| 120 | 283 | if p = nil then |
| 121 | 284 | _ := Sys.write(Sys.stderr, "Heap: out of memory\n"); |
| 122 | 285 | Rt.trace; |
| | @@ -125,6 +288,38 @@ |
| 125 | 288 | return p; |
| 126 | 289 | end; |
| 127 | 290 | |
| 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 | + |
| 128 | 323 | (* The module's test: blocks of every class and big ones, written, |
| 129 | 324 | freed and taken again. *) |
| 130 | 325 | proc main*; |
| | @@ -145,14 +340,19 @@ |
| 145 | 340 | end; |
| 146 | 341 | Mem.free(heap, p[5]); |
| 147 | 342 | 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; |
| 150 | 349 | NEW(q, sure); |
| 151 | 350 | if (q = nil) or (INT(q) mod 8 # 0) then Rt.stop("Heap: sure"); end; |
| 152 | 351 | q^ := 7; |
| 153 | 352 | Mem.free(sure, ^U8(q)); |
| 154 | 353 | if Mem.alloc(heap, -1, 8) # nil then Rt.stop("Heap: negative size"); end; |
| 155 | 354 | if Mem.alloc(heap, 7FFFFFFFFFFFFFFFH - 4, 8) # nil then Rt.stop("Heap: huge size"); end; |
| 355 | + debugTest; |
| 156 | 356 | end; |
| 157 | 357 | |
| 158 | 358 | begin |
| | @@ -160,4 +360,5 @@ |
| 160 | 360 | heap := ADR(std); |
| 161 | 361 | surer := {alloc: allocSure, free: free}; |
| 162 | 362 | sure := ADR(surer); |
| 363 | + if Sys.env("RAZ_HEAP") = "debug" then debug(true); end; |
| 163 | 364 | end Heap. |