| 1 | (* A general allocator: Heap.heap. Small blocks come in nine sizes, |
| 2 | 16 to 4096 bytes with their header, each with a list of the free |
| 3 | ones; a block bigger than that gets pages of its own. Every block |
| 4 | has a 16-byte header before it: its size class, or minus the size |
| 5 | of its pages for a big one, which start less than a page before it; |
| 6 | then a marker, which says the block is out, so that freeing it |
| 7 | twice, or freeing what the heap never gave out, stops the program. |
| 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. *) |
| 28 | module Heap; |
| 29 | import Mem, Lock, Rt, Sys; |
| 30 | import SYSTEM; |
| 31 | |
| 32 | const |
| 33 | Classes = 9; |
| 34 | Header = 16; |
| 35 | Refill = 65536; |
| 36 | Largest = 1 shl 46; (* bigger sizes are refused *) |
| 37 | Out = 5A6564426C6F636BH; (* the marker, xor the header's address *) |
| 38 | Dbg = 5A6564446267626BH; (* a debug block's marker, the same way *) |
| 39 | Guard = 0A5H; (* the byte of the guard after a debug block *) |
| 40 | GuardLen = 8; |
| 41 | Dead = 0DEH; (* the byte a freed debug block is filled with *) |
| 42 | Digits = "0123456789abcdef"; (* for the numbers a stop says *) |
| 43 | Held = 1024; (* how many freed debug blocks are quarantined *) |
| 44 | |
| 45 | type |
| 46 | Block = record next: ^Block; end; |
| 47 | Head = record size, mark: INT; end; |
| 48 | |
| 49 | var |
| 50 | lists: [Classes]^Block; |
| 51 | cur: ^U8; (* where the next new small block comes from *) |
| 52 | left: INT; |
| 53 | lock: Lock.Lock; |
| 54 | held: [Held]^Head; (* the quarantine: a ring of freed debug blocks' headers *) |
| 55 | oldest: INT; (* where in held the next freed block goes, and the oldest is *) |
| 56 | debugOn: BOOL; |
| 57 | std, surer: Mem.Allocator; |
| 58 | (* The heap, for NEW(p, Heap.heap) and Mem's procedures. *) |
| 59 | heap*: ^Mem.Allocator; |
| 60 | (* The heap, but running out of memory stops the program, as |
| 61 | Buf.add does, instead of giving nil: for NEW where nil would be |
| 62 | a bug nobody checks for. *) |
| 63 | sure*: ^Mem.Allocator; |
| 64 | |
| 65 | (* The block p, with size for its header, given out. *) |
| 66 | proc give(p: ^U8; size: INT): ^U8; |
| 67 | var h: ^Head; |
| 68 | begin |
| 69 | h := ^Head(p); |
| 70 | h^.size := size; |
| 71 | h^.mark := Out xor INT(h); |
| 72 | return p + Header; |
| 73 | end; |
| 74 | |
| 75 | (* size bytes aligned to align, or nil: also for a size below zero or |
| 76 | too big, and an alignment that isn't a power of two up to a page. *) |
| 77 | proc alloc(_a: ^Mem.Allocator; size, align: INT): ^U8; |
| 78 | var c, n: INT; b: ^Block; p: ^U8; |
| 79 | begin |
| 80 | if (size < 0) or (size > Largest) or (align < 1) or (align > Mem.Page) |
| 81 | or (align & (align - 1) # 0) then |
| 82 | return nil; |
| 83 | end; |
| 84 | n := size + Header; |
| 85 | c := 0; |
| 86 | while (c < Classes) and (16 shl c < n) do INC(c); end; |
| 87 | if (c = Classes) or (align > Header) then |
| 88 | (* pages of its own, the block at align from their start *) |
| 89 | if align < Header then align := Header; end; |
| 90 | n := (size + align + Mem.Page - 1) div Mem.Page * Mem.Page; |
| 91 | p := Mem.pages(n); |
| 92 | if p = nil then return nil; end; |
| 93 | return give(p + align - Header, -n); |
| 94 | end; |
| 95 | Lock.take(lock); |
| 96 | defer Lock.give(lock); |
| 97 | b := lists[c]; |
| 98 | if b # nil then |
| 99 | lists[c] := b^.next; |
| 100 | p := ^U8(b); |
| 101 | else |
| 102 | if left < 16 shl c then |
| 103 | cur := Mem.pages(Refill); |
| 104 | if cur = nil then left := 0; return nil; end; |
| 105 | left := Refill; |
| 106 | end; |
| 107 | p := cur; |
| 108 | cur := cur + (16 shl c); |
| 109 | left := left - (16 shl c); |
| 110 | end; |
| 111 | return give(p, c); |
| 112 | end; |
| 113 | |
| 114 | (* The start of the page holding address a; a mask, so it holds for the |
| 115 | top half of the address space, where an INT is negative *) |
| 116 | proc pageOf(a: INT): INT; |
| 117 | begin |
| 118 | return a - a & (Mem.Page - 1); |
| 119 | end; |
| 120 | |
| 121 | (* Where the block with header h ends: its class's size, or its pages'. *) |
| 122 | proc endOf(h: ^Head): INT; |
| 123 | begin |
| 124 | if h^.size < 0 then return pageOf(INT(h)) - h^.size; end; |
| 125 | return INT(h) + (16 shl h^.size); |
| 126 | end; |
| 127 | |
| 128 | (* The size a debug block was asked for, kept in its last 8 bytes. *) |
| 129 | proc askedSize(h: ^Head): INT; |
| 130 | var w: ^INT; |
| 131 | begin |
| 132 | w := ^INT(endOf(h) - 8); |
| 133 | return w^; |
| 134 | end; |
| 135 | |
| 136 | (* Stops the program, saying what was found of the debug block with |
| 137 | header h, its address and its size, and the calls under way. *) |
| 138 | proc found(what: []const U8; h: ^Head); |
| 139 | var d: [20]U8; i, n: INT; |
| 140 | begin |
| 141 | _ := Sys.write(Sys.stderr, "Heap: "); |
| 142 | _ := Sys.write(Sys.stderr, what); |
| 143 | _ := Sys.write(Sys.stderr, ": block at 0x"); |
| 144 | n := INT(h) + Header; |
| 145 | i := LEN(d); |
| 146 | repeat |
| 147 | DEC(i); |
| 148 | d[i] := Digits[n & 15]; |
| 149 | n := n shr 4; |
| 150 | until n = 0; |
| 151 | _ := Sys.write(Sys.stderr, d[i..LEN(d)]); |
| 152 | _ := Sys.write(Sys.stderr, ", "); |
| 153 | n := askedSize(h); |
| 154 | i := LEN(d); |
| 155 | repeat |
| 156 | DEC(i); |
| 157 | d[i] := Digits[n mod 10]; |
| 158 | n := n div 10; |
| 159 | until n = 0; |
| 160 | _ := Sys.write(Sys.stderr, d[i..LEN(d)]); |
| 161 | _ := Sys.write(Sys.stderr, " bytes\n"); |
| 162 | Rt.trace; |
| 163 | Rt.exit(2); |
| 164 | end; |
| 165 | |
| 166 | (* found, with the heap's lock held, which is let go first, so that |
| 167 | what runs as the program ends may use the heap *) |
| 168 | proc badBlock(what: []const U8; h: ^Head); |
| 169 | begin |
| 170 | Lock.give(lock); |
| 171 | found(what, h); |
| 172 | end; |
| 173 | |
| 174 | (* Whether the guard after debug block h is whole. *) |
| 175 | proc guardWhole(h: ^Head): BOOL; |
| 176 | var p: ^U8; i, size: INT; |
| 177 | begin |
| 178 | p := ^U8(h) + Header; |
| 179 | size := askedSize(h); |
| 180 | for i := 0 to GuardLen - 1 do |
| 181 | if p[size + i] # Guard then return false; end; |
| 182 | end; |
| 183 | return true; |
| 184 | end; |
| 185 | |
| 186 | (* Whether the freed debug block h is still all 0DEH. *) |
| 187 | proc stillDead(h: ^Head): BOOL; |
| 188 | var p: ^U8; i: INT; |
| 189 | begin |
| 190 | p := ^U8(h) + Header; |
| 191 | for i := 0 to askedSize(h) - 1 do |
| 192 | if p[i] # Dead then return false; end; |
| 193 | end; |
| 194 | return true; |
| 195 | end; |
| 196 | |
| 197 | (* Gives size bytes, as alloc does, but in debug mode: the block has a |
| 198 | guard after it, and its last 8 bytes hold size. *) |
| 199 | proc allocDebug(_a: ^Mem.Allocator; size, align: INT): ^U8; |
| 200 | var p: ^U8; h: ^Head; w: ^INT; i: INT; |
| 201 | begin |
| 202 | if (size < 0) or (size > Largest) then return nil; end; |
| 203 | p := alloc(_a, size + GuardLen + 8, align); |
| 204 | if p = nil then return nil; end; |
| 205 | h := ^Head(p - Header); |
| 206 | h^.mark := Dbg xor INT(h); |
| 207 | for i := 0 to GuardLen - 1 do p[size + i] := Guard; end; |
| 208 | w := ^INT(endOf(h) - 8); |
| 209 | w^ := size; |
| 210 | return p; |
| 211 | end; |
| 212 | |
| 213 | (* Frees the debug block h, with the lock held, which it lets go. The |
| 214 | guard is checked, and the block filled with 0DEH and quarantined; |
| 215 | the block that leaves the quarantine is checked to be still all 0DEH |
| 216 | and guarded, and goes to its free list. A big block is not held: its |
| 217 | guard is checked and its pages unmapped, so any use of it faults. *) |
| 218 | proc freeDebug(h: ^Head); |
| 219 | var out: ^Head; p: ^U8; i, size: INT; b: ^Block; |
| 220 | begin |
| 221 | if not guardWhole(h) then badBlock("a write past the block's end", h); end; |
| 222 | h^.mark := 0; |
| 223 | if h^.size < 0 then |
| 224 | Lock.give(lock); |
| 225 | Mem.unmap(^U8(pageOf(INT(h))), -h^.size); |
| 226 | return; |
| 227 | end; |
| 228 | size := askedSize(h); |
| 229 | p := ^U8(h) + Header; |
| 230 | FILL(p[0..size], Dead); |
| 231 | out := held[oldest]; |
| 232 | held[oldest] := h; |
| 233 | oldest := (oldest + 1) mod Held; |
| 234 | if out # nil then |
| 235 | if not stillDead(out) then badBlock("a write after free", out); end; |
| 236 | if not guardWhole(out) then badBlock("a write past the block's end", out); end; |
| 237 | i := out^.size; |
| 238 | b := ^Block(out); |
| 239 | b^.next := lists[i]; |
| 240 | lists[i] := b; |
| 241 | end; |
| 242 | Lock.give(lock); |
| 243 | end; |
| 244 | |
| 245 | (* Gives back the block p, which alloc or allocDebug gave. *) |
| 246 | proc free(_a: ^Mem.Allocator; p: ^U8); |
| 247 | var h: ^Head; b: ^Block; c: INT; |
| 248 | begin |
| 249 | if p = nil then return; end; |
| 250 | h := ^Head(p - Header); |
| 251 | Lock.take(lock); |
| 252 | if h^.mark # Out xor INT(h) then |
| 253 | if h^.mark = Dbg xor INT(h) then freeDebug(h); return; end; |
| 254 | Rt.stop("Heap: a block freed twice, or never given out"); |
| 255 | end; |
| 256 | h^.mark := 0; |
| 257 | if h^.size < 0 then |
| 258 | Lock.give(lock); |
| 259 | Mem.unmap(^U8(pageOf(INT(h))), -h^.size); |
| 260 | else |
| 261 | c := h^.size; (* before next, which takes its place, is set *) |
| 262 | b := ^Block(h); |
| 263 | b^.next := lists[c]; |
| 264 | lists[c] := b; |
| 265 | Lock.give(lock); |
| 266 | end; |
| 267 | end; |
| 268 | |
| 269 | (* Debug mode on or off, for the blocks given from now on; a block |
| 270 | given before is freed as it was given, in either mode. Call it |
| 271 | before the first allocation to cover them all. *) |
| 272 | proc debug*(on: BOOL); |
| 273 | begin |
| 274 | debugOn := on; |
| 275 | if on then std.alloc := allocDebug; else std.alloc := alloc; end; |
| 276 | end; |
| 277 | |
| 278 | (* The heap's alloc, but with no room the program stops, with the |
| 279 | calls under way. *) |
| 280 | proc allocSure(_a: ^Mem.Allocator; size, align: INT): ^U8; |
| 281 | var p: ^U8; |
| 282 | begin |
| 283 | p := std.alloc(SYSTEM.ADR(std), size, align); |
| 284 | if p = nil then |
| 285 | _ := Sys.write(Sys.stderr, "Heap: out of memory\n"); |
| 286 | Rt.trace; |
| 287 | Rt.exit(2); |
| 288 | end; |
| 289 | return p; |
| 290 | end; |
| 291 | |
| 292 | (* The debug mode's test: a block written past its end, or after it is |
| 293 | freed, is found; a block's bytes, in a small and in a big one, are |
| 294 | its own. The stop itself is test/run/HeapDebug. Mode is left as found. *) |
| 295 | proc debugTest; |
| 296 | var was: BOOL; p: ^U8; h: ^Head; size, i: INT; |
| 297 | begin |
| 298 | was := debugOn; |
| 299 | debug(true); |
| 300 | for i := 0 to 1 do |
| 301 | size := 100 + i * 9900; (* a small block, then a big one *) |
| 302 | p := Mem.alloc(heap, size, 8); |
| 303 | h := ^Head(p - Header); |
| 304 | if (p = nil) or (h^.mark # Dbg xor INT(h)) or (askedSize(h) # size) or not guardWhole(h) then |
| 305 | Rt.stop("Heap: debug block"); |
| 306 | end; |
| 307 | FILL(p[0..size], 1); |
| 308 | p[size] := 0; (* one past the end *) |
| 309 | if guardWhole(h) then Rt.stop("Heap: a write past the end isn't found"); end; |
| 310 | p[size] := Guard; |
| 311 | if size < 4096 then |
| 312 | Mem.free(heap, p); |
| 313 | if not stillDead(h) then Rt.stop("Heap: freed block not filled"); end; |
| 314 | p[7] := 0; (* after free *) |
| 315 | if stillDead(h) then Rt.stop("Heap: a write after free isn't found"); end; |
| 316 | p[7] := Dead; |
| 317 | else |
| 318 | Mem.free(heap, p); |
| 319 | end; |
| 320 | end; |
| 321 | debug(was); |
| 322 | end; |
| 323 | |
| 324 | (* The module's test: blocks of every class and big ones, written, |
| 325 | freed and taken again. *) |
| 326 | proc main*; |
| 327 | var p: [40]^U8; i, j, n: INT; again: ^U8; q: ^INT; |
| 328 | begin |
| 329 | for i := 0 to 39 do |
| 330 | n := (i * i * 37) mod 9000 + 1; |
| 331 | p[i] := Mem.alloc(heap, n, 8); |
| 332 | if (p[i] = nil) or (INT(p[i]) mod 8 # 0) then Rt.stop("Heap: alloc"); end; |
| 333 | for j := 0 to n - 1 do p[i][j] := U8(i); end; |
| 334 | end; |
| 335 | if (pageOf(-4095) # -4096) or (pageOf(-4096) # -4096) or (pageOf(4097) # 4096) then |
| 336 | Rt.stop("Heap: pageOf"); |
| 337 | end; |
| 338 | for i := 0 to 39 do |
| 339 | n := (i * i * 37) mod 9000 + 1; |
| 340 | if (p[i][0] # U8(i)) or (p[i][n - 1] # U8(i)) then Rt.stop("Heap: overlap"); end; |
| 341 | end; |
| 342 | Mem.free(heap, p[5]); |
| 343 | again := Mem.alloc(heap, (25 * 37) mod 9000 + 1, 8); |
| 344 | if (again = p[5]) = debugOn then Rt.stop("Heap: a freed block's reuse"); end; |
| 345 | (* in debug mode again is another block, and p[5] is freed already *) |
| 346 | if debugOn then Mem.free(heap, again); end; |
| 347 | for i := 0 to 39 do |
| 348 | if not debugOn or (i # 5) then Mem.free(heap, p[i]); end; |
| 349 | end; |
| 350 | NEW(q, sure); |
| 351 | if (q = nil) or (INT(q) mod 8 # 0) then Rt.stop("Heap: sure"); end; |
| 352 | q^ := 7; |
| 353 | Mem.free(sure, ^U8(q)); |
| 354 | if Mem.alloc(heap, -1, 8) # nil then Rt.stop("Heap: negative size"); end; |
| 355 | if Mem.alloc(heap, 7FFFFFFFFFFFFFFFH - 4, 8) # nil then Rt.stop("Heap: huge size"); end; |
| 356 | debugTest; |
| 357 | end; |
| 358 | |
| 359 | begin |
| 360 | std := {alloc: alloc, free: free}; |
| 361 | heap := SYSTEM.ADR(std); |
| 362 | surer := {alloc: allocSure, free: free}; |
| 363 | sure := SYSTEM.ADR(surer); |
| 364 | if Sys.env("RAZ_HEAP") = "debug" then debug(true); end; |
| 365 | end Heap. |