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

Segfault in Allocator depending on reference creation position #7478

Closed
AnneKitsune opened this issue Dec 17, 2020 · 2 comments
Closed

Segfault in Allocator depending on reference creation position #7478

AnneKitsune opened this issue Dec 17, 2020 · 2 comments
Milestone

Comments

@AnneKitsune
Copy link

AnneKitsune commented Dec 17, 2020

System

Linux, zig 0.7.1 release.

Reproduce

mkdir segfault && cd segfault
zig init-exe

paste the following in src/main.zig:

const std = @import("std");
const ArgIterator = std.process.ArgIterator;

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    var alloc = arena.allocator;
    //var alloc = &arena.allocator;

    var args = ArgIterator.init();
    defer args.deinit();
    while (args.next(&alloc)) |arg| {
    //while (args.next(alloc)) |arg| {
    }
}

zig build run

Result

umaru% zig build run
Segmentation fault at address 0x0
/usr/lib/zig/std/special/c.zig:188:25: 0x234ec0 in memset (c)
        dest.?[index] = c;
                        ^
/usr/lib/zig/std/mem/Allocator.zig:186:41: 0x21df0a in std.mem.Allocator.alloc (membug)
    return self.allocAdvancedWithRetAddr(T, null, n, .exact, @returnAddress());
                                        ^
/usr/lib/zig/std/mem/Allocator.zig:469:40: 0x2325ab in std.mem.Allocator.dupeZ (membug)
    const new_buf = try allocator.alloc(T, m.len + 1);
                                       ^
/usr/lib/zig/std/process.zig:444:35: 0x232292 in std.process.ArgIterator.next (membug)
            return allocator.dupeZ(u8, self.inner.next() orelse return null);
                                  ^
/shareddata/share/prog/zig/membug/src/main.zig:12:21: 0x22b167 in main (membug)
    while (args.next(&alloc)) |arg| {
                    ^
/usr/lib/zig/std/start.zig:334:37: 0x204d5d in std.start.posixCallMainAndExit (membug)
            const result = root.main() catch |err| {
                                    ^
/usr/lib/zig/std/start.zig:162:5: 0x204a92 in std.start._start (membug)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
The following command terminated unexpectedly:
cd /shareddata/share/prog/zig/membug && /shareddata/share/prog/zig/membug/zig-cache/bin/membug
error: the following build command failed with exit code 1:
/shareddata/share/prog/zig/membug/zig-cache/o/ef07146b27f2cb074c4695700607b8a9/build /usr/bin/zig /shareddata/share/prog/zig/membug /shareddata/share/prog/zig/membug/zig-cache /home/jojolepro/.cache/zig run

If using the commented lines instead of the uncommented ones, the bug is not present.

I'm very new to zig, so I might be doing something wrong? This seems like an error that the compiler should warn about? Thanks!

@Vexu
Copy link
Member

Vexu commented Dec 17, 2020

You're copying the allocator interface out of the arena allocator leading to bad pointer dereferences, see #591.

@Vexu Vexu closed this as completed Dec 17, 2020
@AnneKitsune
Copy link
Author

aaah that explains. thank you!

@andrewrk andrewrk added this to the 0.8.0 milestone Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants