diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 15:19:25 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 17:02:40 -0300 |
| commit | ecce844d227dcb9136a91355ea9e6ec543165fde (patch) | |
| tree | 252f9af081c47503b95e1d3ad58fed2891f729b9 /src | |
| parent | 12d15ce9c00c96f5f7aa5bf30102d7ebebf0f9b7 (diff) | |
| download | pardes-ecce844d227dcb9136a91355ea9e6ec543165fde.tar.gz pardes-ecce844d227dcb9136a91355ea9e6ec543165fde.zip | |
c_heap: a heap's allocator read as its bytes are, through a volatile pointer: std's stateless allocators (c_, page_, smp_, wasm_allocator) leave `ptr` undefined, and one never written outside a test (replica.zig's zstd heap over c_allocator, web.zig's over wasm_allocator) is a constant to a release build, which folded `known.ptr == current.ptr` to false: every malloc named a new allocator and the 65th panicked (the box's session aborted at about its 16th far join; the core's table held 64 copies of {ptr 0, c_allocator's vtable}); its test runs built ReleaseSafe in unit-test too, where it panicked before
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/c_heap.zig | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/src/c_heap.zig b/src/c_heap.zig index 2a3e865f..efd2064d 100644 --- a/src/c_heap.zig +++ b/src/c_heap.zig @@ -39,21 +39,28 @@ pub fn Heap(comptime current: *const std.mem.Allocator) type { var last: std.atomic.Value(usize) = .init(0); pub fn malloc(size: usize) callconv(.c) ?*anyopaque { + // `current` as its bytes are: std's stateless allocators (c_, + // page_, smp_allocator) leave `ptr` undefined, and a heap's + // allocator never repointed is a constant to a release build, + // which then takes `ptr` for whatever it likes at each + // comparison below: never found, an entry a malloc, the 65th a + // panic (a box session's zstd after 15 far joins, 2026-10-30). + const now = @as(*const volatile std.mem.Allocator, current).*; const total = std.math.add(usize, header, size) catch return null; - const bytes = current.alignedAlloc(u8, .@"16", total) catch return null; + const bytes = now.alignedAlloc(u8, .@"16", total) catch return null; var index = last.load(.monotonic); if (index >= allocators_len.load(.acquire) or - allocators[index].ptr != current.ptr or allocators[index].vtable != current.vtable) + allocators[index].ptr != now.ptr or allocators[index].vtable != now.vtable) { while (!allocators_lock.tryLock()) std.atomic.spinLoopHint(); defer allocators_lock.unlock(); const len = allocators_len.load(.monotonic); index = for (allocators[0..len], 0..) |known, at| { - if (known.ptr == current.ptr and known.vtable == current.vtable) break at; + if (known.ptr == now.ptr and known.vtable == now.vtable) break at; } else len; if (index == len) { if (len == allocators.len) @panic("c_heap: more distinct allocators than it can name"); - allocators[len] = current.*; + allocators[len] = now; allocators_len.store(len + 1, .release); } last.store(index, .monotonic); @@ -133,3 +140,15 @@ test "a C heap block keeps its bytes, frees exactly, and goes back where it came live = null; heap.free(null); } + +/// Never written, as a release session's zstd heap's allocator was: a +/// constant to the compiler, `ptr` (undefined in std's stateless +/// allocators) whatever it likes at each comparison. +var stateless: std.mem.Allocator = std.heap.page_allocator; + +test "a heap over a stateless allocator that is never repointed names it once" { + const heap = Heap(&stateless); + const before = allocators_len.load(.monotonic); + for (0..2 * allocators.len) |_| heap.free(heap.malloc(8) orelse return error.OutOfMemory); + try std.testing.expect(allocators_len.load(.monotonic) <= before + 1); +} |
