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

Unreachable at zig-0.8.0/src/stage1/ir.cpp:22346 in buf_write_value_bytes #9401

Open
toffaletti opened this issue Jul 16, 2021 · 4 comments
Open
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@toffaletti
Copy link

Given this code in a file named ptr_cast_crasher.zig:

const std = @import("std");
const assert = std.debug.assert;

const Header = packed struct {
    a: u32,
    b: u32,
};

const data = @embedFile("./ptr_cast_crasher.zig");

pub fn main() void {
    const hdr = @ptrCast(*const Header, &data[0..8]);
    assert(hdr.a != 0x1020020);
}
% zig build-exe ./ptr_cast_crasher.zig
Unreachable at /tmp/zig-20210605-55791-tdqqwr/zig-0.8.0/src/stage1/ir.cpp:22346 in buf_write_value_bytes. This is a bug in the Zig compiler.thread 1156087 panic: 
Unable to dump stack trace: debug info stripped
zsh: abort      zig build-exe ./ptr_cast_crasher.zig

This code can be modified to crash in other ways as well, for example, if we change &data[0..8] to data[0..8]:

const std = @import("std");
const assert = std.debug.assert;

const Header = packed struct {
    a: u32,
    b: u32,
};

const data = @embedFile("./ptr_cast_crasher.zig");

pub fn main() void {
    const hdr = @ptrCast(*const Header, data[0..8]);
    assert(hdr.a != 0x1020020);
}
% zig build-exe ./ptr_cast_crasher.zig
zsh: segmentation fault  zig build-exe ./ptr_cast_crasher.zig
@toffaletti
Copy link
Author

I suspect this might be related to #4680

@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Aug 6, 2021
@Vexu Vexu added this to the 0.10.0 milestone Aug 6, 2021
@tau-dev
Copy link
Contributor

tau-dev commented Aug 28, 2021

I suspect I'm encountering the same or a closely related issue, without even using @embedFile (which might suggest it's not closely tied to #4680):

pub fn main() anyerror!void {
    _ = length_codes[0];
}

const length_codes = blk: {
    var codes: [2]SomeStruct = undefined;
    for (codes[0..1]) |_| {
    }
    break :blk codes;
};

const SomeStruct = struct {
    val: u64,
};
$ zig version
0.9.0-dev.1679+6cf8a49bb
$ zig build-exe src/main.zig
Unreachable at /home/tau/foreign/zig/src/stage1/ir.cpp:22940 in buf_write_value_bytes. This is a bug in the Zig compiler.

This occurs whether I set the array to undefined or initialize it explicitly, but only if its length is greater than 1. Does anyone have a workaround for the issue?

[EDIT 2021-11-19: still broken on current master.]

@toffaletti
Copy link
Author

toffaletti commented Sep 27, 2021

For what its worth, I no longer encounter this in 0.8.1 on an M1 Mac. I spoke too soon. I found a case where removing my work around triggers it still.

@RetroDev256
Copy link
Contributor

RetroDev256 commented Sep 9, 2024

This looks like the same bug as #10024

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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
4 participants