Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageAllocator: sometimes incorrect alignment when remapping to an alignment and size greater than page size #22975

Open
NicoElbers opened this issue Feb 22, 2025 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@NicoElbers
Copy link
Contributor

Zig Version

0.14.0-dev.3241+55c46870b

Steps to Reproduce and Observed Behavior

mwe.zig:

pub fn main() !void {
    const alloc: Allocator = .{
        .ptr = undefined,
        .vtable = &std.heap.PageAllocator.vtable,
    };

    const alignment: Alignment = comptime .fromByteUnits(std.heap.page_size_min * 2);

    for (0..20_000) |size| {
        std.log.debug("size: {d}", .{size});
        var slice = try alloc.alignedAlloc(u8, alignment.toByteUnits(), 1);
        slice = alloc.remap(slice, size) orelse slice;

        alloc.free(slice);
    }
}

const std = @import("std");

const Allocator = std.mem.Allocator;
const Alignment = std.mem.Alignment;

run zig run mwe.zig

Observe:

...
debug: size: 8190
debug: size: 8191
debug: size: 8192
debug: size: 8193
thread 6940 panic: incorrect alignment
/nix/store/4h7cpfzzsczh6g8ivdvaz5k9dygy5z6p-zig-0.14.0-dev.3241+55c46870b/lib/std/mem/Allocator.zig:334:47: 0x103abd2 in remap__anon_2165 (mwe)
    const new_memory: []align(alignment) u8 = @alignCast(new_ptr[0..new_len_bytes]);
                                              ^
/home/nico/personal-projects/zig/alloc-bench/mwe.zig:12:28: 0x103a594 in main (mwe)
        slice = alloc.remap(slice, size) orelse slice;
                           ^
/nix/store/4h7cpfzzsczh6g8ivdvaz5k9dygy5z6p-zig-0.14.0-dev.3241+55c46870b/lib/std/start.zig:656:37: 0x103a3fa in posixCallMainAndExit (mwe)
            const result = root.main() catch |err| {
                                    ^
/nix/store/4h7cpfzzsczh6g8ivdvaz5k9dygy5z6p-zig-0.14.0-dev.3241+55c46870b/lib/std/start.zig:271:5: 0x1039fad in _start (mwe)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
zsh: IOT instruction (core dumped)  zig run mwe.zig

Originally observed this with a size of 20_000

Not sure if it's relevant, but I'm on x86_64 linux

Expected Behavior

Allocator disallowing alignment greater than page_size_min as proposed in #7952 or correct alignment

@NicoElbers NicoElbers added the bug Observed behavior contradicts documented or intended behavior label Feb 22, 2025
NicoElbers added a commit to NicoElbers/zig-alloc-bench that referenced this issue Feb 22, 2025
Split out correctness tests;
Add binned allocations, a test that should fit well for binned
allocators;
Add a test that runs all std tests;
Add a pageAlign test to test page alignment;
We currently cannot test > page_size_min due to
ziglang/zig#22975
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

1 participant