ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.82k stars 2.55k forks source link

missing stack trace for segmentation fault #21798

Open andrewrk opened 2 weeks ago

andrewrk commented 2 weeks ago

Zig Version

0.14.0-dev.2034+56996a280

Steps to Reproduce and Observed Behavior

const std = @import("std");

test "example" {
    const gpa = std.testing.allocator;
    var list: std.ArrayListUnmanaged(i32) = .empty;
    defer list.deinit(gpa);
    try list.append(gpa, 1);
    while (list.items.len < list.capacity) {
        list.appendAssumeCapacity(2);
    }
    const x = &list.items[0];
    try list.append(gpa, 3);
    std.debug.print("x={d}\n", .{x.*});
}
andy@Andrews-MBP build-release % stage3/bin/zig test test.zig
Segmentation fault at address 0x100c04000
aborting due to recursive panic
error: the following test command crashed:
/Users/andy/dev/zig/.zig-cache/o/6fb5d82151d4dce14c3da2d13e407fbb/test --seed=0xed0b0137

Expected Behavior

Expected to see a stack trace, like what happens if you run that example on Linux.

ssergiienko commented 1 week ago

I'm not sure this is the same bug, but may be related:

const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{
        .safety = true,
        .thread_safe = true,
        .never_unmap = true,
        .retain_metadata = true,
    }){};
    const allocator = gpa.allocator();
    const ptr = try allocator.alloc(i32, 10);

    // Free the memory once
    allocator.free(ptr);
    allocator.free(ptr);
}
$ zig-master run -O ReleaseSafe ./test_buf_overflow.zig
error(gpa): Double free detected. Allocation: 
 First free: 
 Second free: 

$

^ No error location information.

Zig 0.14.0-dev.2034+56996a280

samrock5000 commented 4 days ago

no stack trace here either: Zig Version 0.14.0-dev.2162+3054486d1

Steps to Reproduce and Observed Behavior

const Structure = enum(u8) {
    HasAmount = 0x10,
    HasData = 0x20,
    HasCommitmentLength = 0x40,
    Reserved = 0x80,
};
const Capability = enum(u8) {
    None = 0x0,
    Mutable = 0x01,
    Minting = 0x02,
};

fn hasData(bitfield: u8) bool {
    return bitfield & @intFromEnum(Structure.HasData) != 0;
}
fn hasAmount(bitfield: u8) bool {
    return bitfield & @intFromEnum(Structure.HasAmount) != 0;
}
fn hasCommitmentLen(bitfield: u8) bool {
    return bitfield & @intFromEnum(Structure.HasCommitmentLength) != 0;
}
fn isMutable(bitfield: u8) bool {
    return hasData(bitfield) and @intFromEnum(Capability.Mutable) != 0;
}
fn isMinting(bitfield: u8) bool {
    return isMinting(bitfield) and @intFromEnum(Capability.Minting) != 0;
}
fn getcapability(bitfield: u8) u8 {
    return bitfield & 0x0f;
}
test "test" {
    const bitfield: u8 = 113;
    const is_mut = isMutable(bitfield) and isMinting(bitfield);
    _ = is_mut;
}

error: the following test command crashed: /Users/johndoe/.cache/zig/o/1744a080f14dd669409f1bc036c4a248/test --seed=0x5e44a8a7