镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 6ed2ca8

Browse files
committed
fix(gc): harden maybe-managed barriers
1 parent 984ae51 commit 6ed2ca8

1 file changed

Lines changed: 38 additions & 9 deletions

File tree

‎src/heap.zig‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -559,22 +559,20 @@ pub fn Heap(comptime Binding: type) type {
559559
self.nursery_force_full.store(true, .release);
560560
return;
561561
};
562-
const h = Self.headerOf(p);
563-
if (h.magic != header_magic) {
562+
const h = self.headerForPayload(p) orelse {
564563
self.nursery_force_full.store(true, .release);
565564
return;
566-
}
565+
};
567566
if (!@atomicLoad(bool, &h.young, .acquire)) self.rememberOwner(h);
568567
}
569568

570569
fn rememberStrongStore(self: *Self, owner: ?*anyopaque, cell: ?*anyopaque) void {
571570
if (!self.nursery_enabled) return;
572571
const child_ptr = cell orelse return;
573-
const child = Self.headerOf(child_ptr);
574-
if (child.magic != header_magic or !@atomicLoad(bool, &child.young, .acquire)) return;
572+
const child = self.headerForPayload(child_ptr) orelse return;
573+
if (!@atomicLoad(bool, &child.young, .acquire)) return;
575574
if (owner) |owner_ptr| {
576-
const parent = Self.headerOf(owner_ptr);
577-
if (parent.magic == header_magic) {
575+
if (self.headerForPayload(owner_ptr)) |parent| {
578576
if (!@atomicLoad(bool, &parent.young, .acquire)) self.rememberOwner(parent);
579577
return;
580578
}
@@ -604,8 +602,7 @@ pub fn Heap(comptime Binding: type) type {
604602
fn incrementalBarrier(self: *Self, cell: ?*anyopaque) void {
605603
if (!self.marking.load(.acquire)) return;
606604
const p = cell orelse return;
607-
const h = Self.headerOf(p);
608-
if (h.magic != header_magic) return;
605+
const h = self.headerForPayload(p) orelse return;
609606
if (!self.claimMark(h)) return;
610607
if (self.concurrent.load(.acquire)) {
611608
// Hand the greyed cell to the marker thread (it owns mark_stack).
@@ -1562,6 +1559,38 @@ test "nursery child-only barrier is a conservative compatibility root" {
15621559
try std.testing.expectEqual(@as(usize, 0), heap.live_cells);
15631560
}
15641561

1562+
test "write barriers tolerate non-heap pointer values" {
1563+
const a = std.testing.allocator;
1564+
var rt = TestRT{};
1565+
defer rt.roots.deinit(a);
1566+
defer rt.finalized.deinit(a);
1567+
1568+
var heap = Heap(TestRT).init(a, &rt);
1569+
defer heap.deinit();
1570+
heap.setNurseryEnabled(true);
1571+
1572+
var stack_value: usize = 0;
1573+
const stack_ptr: *anyopaque = @ptrCast(&stack_value);
1574+
const wild: *anyopaque = @ptrFromInt(@as(usize, 0x1000_0000_0000));
1575+
1576+
// Nursery compatibility paths: maybe-managed child/owner pointers must be
1577+
// rejected by heap membership, not by peeking at their candidate headers.
1578+
heap.writeBarrier(stack_ptr);
1579+
heap.writeBarrier(wild);
1580+
heap.writeBarrierFrom(stack_ptr, wild);
1581+
heap.writeBarrierFrom(wild, stack_ptr);
1582+
heap.writeBarrierWeak(stack_ptr);
1583+
heap.writeBarrierWeak(wild);
1584+
1585+
// Incremental/concurrent barrier paths have the same public contract.
1586+
heap.marking.store(true, .release);
1587+
defer heap.marking.store(false, .release);
1588+
heap.writeBarrier(stack_ptr);
1589+
heap.writeBarrier(wild);
1590+
heap.writeBarrierFrom(stack_ptr, wild);
1591+
heap.writeBarrierFrom(wild, stack_ptr);
1592+
}
1593+
15651594
test "mark-sweep: optional conservative words root payload and interior pointers" {
15661595
const a = std.testing.allocator;
15671596
var rt = TestRT{};

0 commit comments

Comments
 (0)