What could be causing a recursive panic during alloc()?

I’m running into this error while testing my code on a M1 Mac:

Bus error at address 0x102cfc000
aborting due to recursive panic

Here’s the test in question:

test "executable" {
    var gpa = executable();
    var allocator = gpa.allocator();
    const memory = try allocator.alloc(u8, 256);
    _ = memory;
    // allocator.free(memory);
    // try expect(gpa.detectLeaks() == false);
}

executable() looks like this:

pub fn executable() std.heap.GeneralPurposeAllocator(.{}) {
    return .{
        .backing_allocator = .{
            .ptr = undefined,
            .vtable = &ExecutablePageAllocator.vtable,
        },
    };
}

Where ExecutablePageAllocator is just a copy-and-pasted version of PageAllocator that sets the executable flag.

The code works fine on Linux and on an Intel Mac. Initially I was getting an Access Denied error due to the absence of the MAP_JIT flag. After adding that now I get this new strange error.

I wonder whether these links are related:

https://forums.developer.apple.com/forums/thread/672804

1 Like

Thanks for the pointer. That’s precisely the problem. The memory is still protected after the call to mmap(). Since Allocator currently does a @memset() on memory it has obtained (#4298), alloc() blows up. Hopefully I can work around this.

1 Like