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

Segmentation fault in release mode with std.rand.Csprng.int(u8) #6384

Closed
pmwhite opened this issue Sep 19, 2020 · 4 comments
Closed

Segmentation fault in release mode with std.rand.Csprng.int(u8) #6384

pmwhite opened this issue Sep 19, 2020 · 4 comments

Comments

@pmwhite
Copy link

pmwhite commented Sep 19, 2020

Here is a repro I've just tested with the most recent master from ziglang.org:

const std = @import("std");

pub fn main() anyerror!void {
    var seed = @intCast(u64, std.time.timestamp());
    var csprng = std.rand.DefaultCsprng.init(seed);
    var rand = csprng.random;
    _ = rand.int(u8);
}

In normal build mode, this runs fine. However, in release-safe build mode, I get the following error when I run the executable.

Segmentation fault at address 0x0
/.../zig/lib/zig/std/rand.zig:939:26: 0x214673 in std.rand.Isaac64.fill (repro)
                buf[i] = @truncate(u8, n);
                         ^
The process was killed by SIGABRT: Aborted
@LemonBoy
Copy link
Contributor

var rand = csprng.random;

This creates a fresh copy of the random interface that's detached from csprng. When you call int you're basically mangling some random stack region, hence the segfault.

Use this instead:

var rand = &csprng.random;

@Tetralux
Copy link
Contributor

This is the second time I've come across someone having this problem in a week.
Perhaps this interface could be improved.

My first thought: maybe random should be a member function, like writer() is on io.Writer?

@Rocknest
Copy link
Contributor

Yeah looks like we need #3803 or some other protection

@jayschwa
Copy link
Sponsor Contributor

It looks like this is covered by #591 and related proposals.

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

5 participants