Skip to content

Segfault in debug mode does not exit #25243

@lordwelch

Description

@lordwelch

Zig Version

0.16.0-dev.234+32a1aabff

Steps to Reproduce and Observed Behavior

zig run ./test.zig

test.zig:

const std = @import("std");

pub fn main() !u8 {
    var a = std.heap.GeneralPurposeAllocator(.{}).init;
    const a_variable = "something";
    a.allocator().free(a_variable);
    return 0;
}

program never exits outputting the last three lines forever:

$ zig-aarch64-linux-0.16.0-dev.234+32a1aabff/zig run ./test.zig
Segmentation fault at address 0x100fe1a
/zig-aarch64-linux-0.16.0-dev.234+32a1aabff/lib/compiler_rt/memset.zig:19:14: 0x10f5934 in memset (compiler_rt)
            d[0] = c;
             ^
/zig-aarch64-linux-0.16.0-dev.234+32a1aabff/lib/std/mem/Allocator.zig:443:26: 0x10bddef in free__anon_22099 (test)
    @memset(non_const_ptr[0..bytes_len], undefined);
                         ^
/zig-aarch64-linux-0.16.0-dev.234+32a1aabff/lib/std/mem/Allocator.zig:443:26: 0x10bddef in free__anon_22099 (test)
    @memset(non_const_ptr[0..bytes_len], undefined);
...

Expected Behavior

Segfault and exit similar to ReleaseSafe.

$ zig-aarch64-linux-0.16.0-dev.234+32a1aabff/zig run -O ReleaseSafe ./test.zig
Segmentation fault at address 0x1017020
/zig-aarch64-linux-0.16.0-dev.234+32a1aabff/lib/std/heap/debug_allocator.zig:875:23: 0x101aee8 in free (test)
            if (bucket.canary != config.canary) @panic("Invalid free");
                      ^
/zig-aarch64-linux-0.16.0-dev.234+32a1aabff/lib/std/mem/Allocator.zig:160:25: 0x101acc7 in main (test)
    return a.vtable.free(a.ptr, memory, alignment, ret_addr);
                        ^
/zig-aarch64-linux-0.16.0-dev.234+32a1aabff/lib/std/start.zig:627:37: 0x101abb7 in posixCallMainAndExit (test)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x0 in ??? (???)
Aborted (core dumped)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions