Skip to content

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

@mikemar10

Description

@mikemar10

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions