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

Compiler allocates too much memory and crashes for large global undefined structures #10287

Open
mikemar10 opened this issue Dec 6, 2021 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@mikemar10
Copy link

Zig Version

0.9.0-dev.1903+2af94e76a

Steps to Reproduce

Attempting to compile this source code causes a panic at compile time:

mike@budgie:~/src/zigxperiments$ cat static-allocation.zig
var memory: [4294967296]u8 = undefined;

pub fn main() void {
  memory[0] = 1;
}

mike@budgie:~/src/zigxperiments$ zig build-exe static-allocation.zig
allocation failedthread 624995 panic:
Unable to dump stack trace: debug info stripped
Aborted (core dumped)

My (possibly incorrect) understanding was that since this is a global undefined var, it shouldn't be fully allocated at comptime but rather it should be specified in the .bss section of the object.

The zig cc compiler can build an equivalent C program:

mike@budgie:~/src/zigxperiments$ cat static-allocation.c
char memory[4294967296];
int main() {
	memory[0] = 1;
	return 0;
}
mike@budgie:~/src/zigxperiments$ zig cc static-allocation.c
mike@budgie:~/src/zigxperiments$

Smaller amounts of memory will compile successfully but since they're being allocated at compile time the additional memory usage can result in OOM conditions. These examples were all on Linux.

Expected Behavior

I expected the compiler to be able to build the exe with an entry in the .bss section. I don't think this memory needs to actually be allocated at comptime.

Actual Behavior

The compiler attempts to allocate the specified amount of memory at comptime and crashes if the amount of memory is too large.

@mikemar10 mikemar10 added the bug Observed behavior contradicts documented or intended behavior label Dec 6, 2021
@andrewrk andrewrk added the stage1 The process of building from source via WebAssembly and the C backend. label Dec 6, 2021
@andrewrk andrewrk added this to the 0.11.0 milestone Dec 6, 2021
@schmee
Copy link
Contributor

schmee commented Feb 12, 2023

This looks like a duplicate of #1424.

@Vexu
Copy link
Member

Vexu commented Feb 12, 2023

The original test case is now a duplicate of that but you can still cause the compiler to run out of memory with a slight modification:

pub fn main() void {
    comptime var memory: [4294967296]u8 = undefined;
    memory[0] = 1;
}

@Vexu Vexu removed the stage1 The process of building from source via WebAssembly and the C backend. label Feb 12, 2023
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
Projects
None yet
Development

No branches or pull requests

4 participants