Skip to content

Segmentation Fault on aarch64 when linking with musl #11244

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

Closed
xoich opened this issue Mar 20, 2022 · 3 comments
Closed

Segmentation Fault on aarch64 when linking with musl #11244

xoich opened this issue Mar 20, 2022 · 3 comments

Comments

@xoich
Copy link

xoich commented Mar 20, 2022

Zig Version

0.10.0-dev.1448+b6203b89d

Steps to Reproduce

I run the following code with zig build-exe -target aarch64-linux-musl -lc sftest.zig; ./sftest.

const std = @import("std");

const Card = struct {
    a: u32 = 0,
    b: []u8 = undefined,
    c: []u8 = undefined,
    d: i8 = 0,
    e: i64 = 0,
    f: i64 = 0,
    g: f32 = 0,
    h: u32 = 0,
    i: i64 = 0,
    j: i64 = 0,
};

const Scheduler = struct {
    random: std.rand.Random,
    fn init() Scheduler {
        var prng = std.rand.DefaultPrng.init(0);
        const random = prng.random();
        return Scheduler{ .random = random };
    }
};

fn maybeCard() !Card {
    if (std.time.timestamp() < 0)
        return error.Hello;
    return Card{};
}

const Controller = struct {
    scheduler: *Scheduler,
    const Self = @This();
    pub fn createNewCard(self: *const Self, maybe: ?[]u64) !void {
        if (maybe) |t| {
            _ = t;
            var topic = try maybeCard();
            _ = topic;
        }
        _ = self.scheduler.random.intRangeLessThanBiased(i64, 0, 10);
    }
};

pub fn main() !void {
    var scheduler = Scheduler.init();
    var controller = Controller{ .scheduler = &scheduler };
    try controller.createNewCard(null);
}

Expected Behavior

The program runs without errors.

Actual Behavior

$ zig build-exe -target aarch64-linux-musl -lc sftest.zig
$ ./sftest
Segmentation fault at address 0x540000000ffbf2
/home/dn/sftest.zig:54:21: 0x2141c4 in main (sftest)
pub fn main() !void {
                    ^
Aborted

Notes

It does not happen on x86_64.

I think it might be a stack buffer overrun.

@xoich xoich added the bug Observed behavior contradicts documented or intended behavior label Mar 20, 2022
@xoich xoich changed the title Segmentation Fault on aarch64 linked with musl Segmentation Fault on aarch64 when linking with musl Mar 20, 2022
@xoich
Copy link
Author

xoich commented Mar 21, 2022

Building the same code without the -lc option result in this:

$ zig build-exe -target aarch64-linux-musl sftest.zig; ./sftest
Bus error at address 0x4c970dcf13cd5433
???:?:?: 0x4c970dcf13cd5433 in ??? (???)
Aborted

gdb:

Program received signal SIGBUS, Bus error.
0x4c970dcf13cd5433 in ?? ()
(gdb) bt
#0  0x4c970dcf13cd5433 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

@Vexu Vexu added arch-aarch64 64-bit ARM miscompilation The compiler reports success but produces semantically incorrect code. labels Mar 27, 2022
@Vexu Vexu added this to the 0.11.0 milestone Mar 27, 2022
@LemonBoy
Copy link
Contributor

Not a bug (sadly), it's #9080 all over again.

@Vexu Vexu removed bug Observed behavior contradicts documented or intended behavior arch-aarch64 64-bit ARM miscompilation The compiler reports success but produces semantically incorrect code. labels Mar 28, 2022
@Vexu Vexu removed this from the 0.11.0 milestone Mar 28, 2022
@Vexu Vexu closed this as completed Mar 28, 2022
@ominitay
Copy link
Contributor

You can instead use an anytype field and hold the prng in there :)

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

4 participants