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

Large array sizes cause segfault or LLVM error #9906

Closed
mil opened this issue Oct 6, 2021 · 4 comments
Closed

Large array sizes cause segfault or LLVM error #9906

mil opened this issue Oct 6, 2021 · 4 comments
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@mil
Copy link
Contributor

mil commented Oct 6, 2021

Given example test file arraysize.zig like:

const std = @import("std");

pub fn main() void {
    var x : [2e7]u8 = undefined;
    x[1] = 2;
    std.debug.warn("Foo\n", .{});
}

Running produces segfault:

> zig run arraysize.zig
zsh: segmentation fault  zig run arraysize.zig

Changing size of array to even larger arbitrary value 2e10 produces LLVM error:

broken LLVM module found: invalid subrange count
!16474 = !DISubrange(count: -545128448, lowerBound: 0)

This is a bug in the Zig compiler.thread 9376 panic:
Unable to dump stack trace: debug info stripped
zsh: abort      zig run arraysize.zig

Maybe error message should be standardized? Or atleast compiler should try to prevent segfault case.

@mil
Copy link
Contributor Author

mil commented Oct 6, 2021

> zig version
0.9.0-dev.1324+598db831f

@ghost
Copy link

ghost commented Oct 8, 2021

The segfault comes from overflowing the stack with that 20MB array, so more or less normal behavior. Workaround: allocate large arrays on the heap 😄.

The second failure is a bug -- a more graceful error should be reported there.

@andrewrk andrewrk added backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Nov 20, 2021
@andrewrk andrewrk added this to the 0.10.0 milestone Nov 20, 2021
@Ultra-Code
Copy link

Also had a similar issue. I realized from try and error that when I have an array on the stack greater than ~15MiB (1024 * 1024 * 15) zig build run crashes. But when I set the exe.stack_size option to a value greater than 15MiB then I can have an array buffer greater than 15MiB but zig build run still crash but zig build and running exe manually works

Ultra-Code added a commit to Ultra-Code/recblock that referenced this issue Jul 24, 2022
Use 12MiB of stack memory instead of the full 15MiB
Because the 15MiB refers to the total stack and not the current fn's stack frame like I thaught

15MiB is the largest stack frame size zig stage1 allows without crashing zig build run.
when the exe.stack_size option is set to a high value this buffer can be larger but still crashes zig build run.
NOTE: running exe manually works
ISSUE: ziglang/zig#9906
@nektro
Copy link
Contributor

nektro commented Aug 12, 2023

fixed by #1424 in LLVM 17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

5 participants