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

in debug builds, detect stack overflow at runtime and output a partial stack trace #1915

Closed
schmee opened this issue Feb 4, 2019 · 4 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@schmee
Copy link
Contributor

schmee commented Feb 4, 2019

The following snippet causes a segfaults on my machine:

pub fn main() void {
    const size = 8388608 - 1000;
    const bytes: [size]u8 = undefined;
}

Result:

➜  des git:(master) ✗ zig build-exe des.zig && ./des
[1]    13923 segmentation fault  ./des

I have little experience with low-level languages like this, but I guess this is some sort of stack overflow? If you subtract 10000 instead of 1000, the code runs successfully. I got the random-looking size from #1639 from the discussion of default stack sizes.

Is this intended behavior? Is it PEBKAC? 😄 I have been spoiled so far in Zig with very nice stack traces, would it be possible to have a stack trace for this situation also?

Versions:

➜  des git:(master) ✗ zig version
0.3.0+dfbc063f

macOS Mojave 10.14.3

@sjdh02
Copy link

sjdh02 commented Feb 4, 2019

Yep, looks like a stack overflow. Running your code under linux with valgrind and giving it a slightly larger stack works fine. Don't quote me on this, but to give a stack trace for a segfault would probably require a custom signal handler, which Zig should be able to do with cImport for the needed headers on each platform. I'm no Zig expert though, just giving my 2 cents :).

@emekoi
Copy link
Contributor

emekoi commented Feb 4, 2019

i did a little work on a crossplatform segfault handler for zig here. i only got it to work usefully on windows because ucontext_t is needed everywhere else.

@andrewrk andrewrk added this to the 0.5.0 milestone Feb 4, 2019
@andrewrk andrewrk added standard library This issue involves writing Zig code for the standard library. enhancement Solving this issue will likely involve adding new logic or components to the codebase. labels Feb 4, 2019
@andrewrk andrewrk changed the title Segfault with stack allocation in debug builds, detect stack overflow at runtime and output a partial stack trace Feb 4, 2019
@andrewrk
Copy link
Member

andrewrk commented Mar 1, 2019

Here's a small example of dumping a stack trace on posix when there is a segfault:

const std = @import("std");
const linux = std.os.linux;

pub fn main() void {
    _ = linux.sigaction(linux.SIGSEGV, &segv_handler_action, null);

    var x = @intToPtr(*i32, 0x1);
    x.* = 42;
}

const segv_handler_action = linux.Sigaction{
    .handler = segv_handler,
    .mask = linux.empty_sigset,
    .flags = linux.SA_RESETHAND,
};

extern fn segv_handler(sig: i32) void {
    std.debug.dumpCurrentStackTrace(null);
}

It doesn't find the restart address of the signal though, which is arguably the most important frame in the stack trace. So that's left to be figured out. I'm sure that's possible.

After that, and after #1047 is done, which will provide applications a way to opt out, I think we can turn on segfault handling by default.

@andrewrk
Copy link
Member

andrewrk commented Jul 2, 2019

Duplicate of #1616

@andrewrk andrewrk marked this as a duplicate of #1616 Jul 2, 2019
@andrewrk andrewrk closed this as completed Jul 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

4 participants