Skip to content

LLVM miscompiles large stack allocations #100914

@Cl00e9ment

Description

@Cl00e9ment

I tried this code:

use std::thread;

const KILO: usize = 1024;
const MEGA: usize = 1024 * KILO;
const GIGA: usize = 1024 * MEGA;

const BUFFER_SIZE: usize = 4 * GIGA;
const REQUIRED_STACK_SIZE: usize = 512 * MEGA + BUFFER_SIZE;

fn main() {
    thread::Builder::new()
        .stack_size(REQUIRED_STACK_SIZE)
        .spawn(perform_double_free).unwrap()
        .join().unwrap();
}

fn perform_double_free() {
    make_noise();
    let mut buffer = [0; BUFFER_SIZE];
    write_to_buffer(&mut buffer, 0);
}

fn make_noise() {
    vec![0].append(&mut vec![0]);
}

fn write_to_buffer(buffer: &mut [u8; BUFFER_SIZE], mut i: usize) {
    i += 4096;
    if i >= BUFFER_SIZE {
        return;
    }
    write_to_buffer(buffer, i);
    buffer[i] = 1;
}

I expected the program to exit normally.
Instead, this happened:

free(): double free detected in tcache 2
Aborted

Meta

Tested on Linux with the following Rust versions :

  • rustc 1.63.0 (4b91a6ea7 2022-08-08)
  • rustc 1.64.0-beta.3 (82bf34178 2022-08-18)
  • rustc 1.65.0-nightly (015a824f2 2022-08-22)

Only reproducible with --release.
Can be reproduced on Windows inside a Rust Docker container.

⚠️ WARNING ⚠️
The program may use about 5 GiB of memory and may hang the system. So be sure to have enough space and be ready to kill the process.

Backtrace
No backtrace is printed with RUST_BACKTRACE=1.

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-miscompileIssue: Correct Rust code lowers to incorrect machine codeI-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.WG-llvmWorking group: LLVM backend code generation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions