Skip to content

Accessing large static global behaves incorrectly #121868

Closed
@zyedidia

Description

@zyedidia

I tried this code:

static VALS: [u8; 1024 * 1024 * 1024 * 4] = [0u8; 1024 * 1024 * 1024 * 4];

fn main() {
    for i in 0..2048 {
        println!("{}", VALS[i]);
        if VALS[i] != 0 {
            println!("NON-ZERO: {}", VALS[i]);
            return
        }
    }
}

I expected to see this happen: it should print 2048 zeroes, since the array is statically initialized to zero.

Instead, this happened: it prints random values of memory. In debug mode, it also exits at the first non-zero value and prints "NON-ZERO". In release mode, it never prints "NON-ZERO", but the values that it displays are in fact non-zero (I believe this is cause it is optimizing out the if statement).

It appears that the linker is not including the data for the VALS array in the binary, but a symbol is still included, which causes the loads to access other variables stored at the location of the symbol.

Meta

rustc --version --verbose:

rustc 1.78.0-nightly (878c8a2a6 2024-02-29)
binary: rustc
commit-hash: 878c8a2a62d49ca5c454547ad67290a1df746cb5
commit-date: 2024-02-29
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions