Skip to content

-C opt-level=1 miscompiles raw-pointer access through static mut into ud2 #164137

Closed

Description

@466774321-code

Code

static mut g1: *mut u8 = std::ptr::null_mut::<u8>();
static mut g4: *mut u8 = std::ptr::null_mut::<u8>();
static mut g21: *mut u8 = std::ptr::null_mut::<u8>();

unsafe fn fn_0() -> () {
    for i in 0u32..2u32 {
        if i == 0u32 {
            unsafe { std::ptr::write(&raw mut g21, unsafe { (&raw mut g21).cast::<u8>() }); }
            unsafe { std::ptr::write(&raw mut g1, unsafe { std::ptr::read((unsafe { std::ptr::read::<*mut u8>(&raw const g21) }).cast::<*mut u8>()) }); }
        } else {
            let g9: *mut u8 = unsafe { std::ptr::read((unsafe { std::ptr::read::<*mut u8>(&raw const g1) }).cast::<*mut u8>()) };
            unsafe { std::ptr::write(g9.cast::<*mut u8>(), std::ptr::null_mut::<u8>()); }
            unsafe { std::ptr::write(g9.cast::<*mut u8>(), unsafe { std::ptr::read::<*mut u8>(&raw const g21) }); }
            unsafe { std::ptr::write((unsafe { std::ptr::read::<*mut u8>(&raw const g1) }).cast::<*mut u8>(), unsafe { std::ptr::read::<*mut u8>(&raw const g1) }); }
            unsafe { std::ptr::write(&raw mut g4, unsafe { std::ptr::read((unsafe { std::ptr::read::<*mut u8>(&raw const g21) }).cast::<*mut u8>()) }); }
            return;
        }
    }
}

fn main() {
    unsafe { fn_0(); }
}

Reproduction

Save the code as case.rs, then run:

rustc +nightly --edition=2024 -C opt-level=0 case.rs -o case-o0
./case-o0

rustc +nightly --edition=2024 -C opt-level=1 case.rs -o case-o1
./case-o1

Expected Behavior

The program should run and exit normally at every optimization level.

g21 is used as a raw self-reference: it stores the address of its own static storage. On the second loop iteration, g1 holds &g21, and g9 is loaded from the storage of g21, so it also holds &g21.

The first store through g9 temporarily writes null into g21. That temporary null value is not dereferenced. A later store through g1 writes g1, namely &g21, back into g21. The final load through g21 therefore reads from the storage of g21, not from null.

Actual Behavior

At -C opt-level=0, the program exits normally.

At -C opt-level=1, the program traps with an illegal instruction:

Illegal instruction (core dumped)

Under gdb, the optimized binary stops at case::main, whose first instruction is ud2:

Program received signal SIGILL, Illegal instruction.
0x000055555556878e in case::main ()

=> 0x55555556878e <case::main+0000>: ud2

Emitting optimized LLVM IR also shows that case::main has been optimized to unreachable:

define hidden void @_RNv...4case4main() unnamed_addr #3 !guid !16 {
start:
  unreachable
}

Additional Results

Configuration Result
-C opt-level=0 exits 0
-C opt-level=1 SIGILL / ud2
-C opt-level=1 -C codegen-units=1 SIGILL / ud2

I also reproduced the -O1 trap independently with:

rustc 1.101.0-nightly (8d1a76430 2026-10-06)
binary: rustc
commit-hash: 8d1a76430406c877b35d0b627e7f796dcf0dfeca
commit-date: 2026-10-06
host: x86_64-unknown-linux-gnu
release: 1.101.0-nightly
LLVM version: 23.1.3

Miri

I also ran the program in an edition-2024 Cargo project under Miri:

MIRIFLAGS="-Zmiri-disable-isolation" cargo +nightly miri run
MIRIFLAGS="-Zmiri-disable-isolation -Zmiri-tree-borrows" cargo +nightly miri run

Both runs completed without reporting undefined behavior.

I understand that Miri not reporting UB is not a proof that the program is UB-free, but I do not see Rust-level UB here either:

  • The program is single-threaded, so there is no data race on the mutable statics.
  • The accesses use raw pointers created with &raw, not Rust references that would impose aliasing guarantees.
  • The pointer-sized reads and writes target the storage of g21, which is properly aligned for *mut u8.
  • The temporary null pointer value written into g21 is not dereferenced before g21 is restored to &g21.

Optimizer Bisection

Bisecting with:

rustc +nightly --edition=2024 -C opt-level=1 -C codegen-units=1 \
  -C llvm-args=-opt-bisect-limit=N case.rs

points at early-cse on fn_0, right after sroa:

BISECT: running pass (427) sroa on ...fn_0
BISECT: running pass (428) early-cse on ...fn_0

The before/after IR differs in a reload of @g21. The pass appears to forward an earlier pointer value across intervening stores that may alias @g21:

-  %_239 = load ptr, ptr @g21, align 8, !noundef
-  %_237 = load ptr, ptr %_239, align 8, !noundef
+  %_237 = load ptr, ptr %_230, align 8, !noundef

In this program, those intervening stores can write to the storage of @g21, so forwarding the earlier value appears to change the behavior and lets the optimizer conclude that the function is unreachable.

Environment

Ubuntu 22.04 under WSL
x86_64-unknown-linux-gnu

Original rustc +nightly --version --verbose:

rustc 1.101.0-nightly (32dba69d6 2026-10-09)
binary: rustc
commit-hash: 32dba69d69c5b10ea89a4042de8d0619f7756203
commit-date: 2026-10-09
host: x86_64-unknown-linux-gnu
release: 1.101.0-nightly
LLVM version: 23.1.3

Activity

  1. added
    C-bug
    on Oct 11, 2026
  2. added
    needs-triage
    on Oct 11, 2026
  3. nikic commented on Oct 11, 2026

    @nikic
    Contributor

    I haven't confirmed, but based on symptoms I suspect this is llvm/llvm-project#213232.

  4. self-assigned this
    on Oct 11, 2026
  5. added
    A-LLVM
    P-medium
    I-miscompile
    llvm-fixed-upstream
    and removed
    needs-triage
    on Oct 11, 2026
  6. nikic commented on Oct 12, 2026

    @nikic
    Contributor

    Confirmed that the miscompile is gone with that change.

  7. added 2 commits that reference this issue on Oct 14, 2026
  8. linked a pull request that will close this issue[beta] backports #164371on Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-LLVMC-bugI-miscompileP-mediumllvm-fixed-upstream

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions