Skip to content

kernel hits an illegal memory access (CUDA 700) when it uses Option<&T>::unwrap_or(&literal) #132

Closed

Description

@hbtweb

I ran into this using Option<&u32> inside a kernel. If I have both a Some and a None of Option<&u32> live in the same function and do *x.unwrap_or(&77), the kernel faults at launch:

DriverError(700, "an illegal memory access was encountered")

The annoying part is it compiles cleanly — no warning, no error — so I only noticed when the kernel crashed at runtime. (And depending on layout it could just as easily read garbage instead of faulting.)

Reproduce

The whole trigger is in the kernel; the host just launches one thread and reads out back:

#[kernel]
pub fn opt_ref_unwrap_or(out: &[u32]) {
    if thread::index_1d().get() != 0 { return; }
    let r: u32 = 5;
    let a: Option<&u32> = Some(&r);
    let b: Option<&u32> = None;        // keeping BOTH a Some and a None live is what
                                       // makes -O const-fold the None arm's unwrap_or
                                       // into the `&77` reference-to-scalar constant
    let v0: u32 = *a.unwrap_or(&77);   // 5
    let v1: u32 = *b.unwrap_or(&77);   // should be 77 — pre-fix this lowers to load i32, ptr null

    unsafe {                                  // out is &[u32] aliasing a writable buffer;
        let p = out.as_ptr() as *mut u32;     // the raw write is incidental to the bug
        *p.add(0) = v0;
        *p.add(1) = v1;
    }
}

Launch 1 thread, read 2 elements. Expected out == [5, 77]. Pre-fix, out[1] faults with DriverError(700, …) (or reads garbage).

What I found digging in

Root cause (mir-importer / rvalue.rs)

-O const-folds None.unwrap_or(&77) into a reference-to-scalar constant &77. That's a ConstantKind::Allocated whose 8 pointer bytes are a zero relocation placeholder, with the real 77 allocation recorded in alloc.provenance.ptrs.

In translate_operand, the pointer-constant arm followed that provenance only when the pointee was a struct (translate_struct_constant + mir.ref). For a scalar pointee it skipped the follow and fell through to the raw-pointer branch, which emitted inttoptr i64 0 — dropping the relocation. So later load i32, ptr null → CUDA 700.

The fix that worked for me: generalize the same provenance-follow the struct path already uses to scalar pointees (materialize the pointee, wrap it in a MirRefOp). Genuine raw pointers (core::ptr::null(), inline non-followable bytes) still fall through unchanged, and provenance pointing at a non-memory allocation hard-errors rather than silently producing a null — so it doesn't widen the silent-miscompile surface (in the spirit of #27).

Related

Suggested labels: miscompile, safety, codegen.

I've got a small fix + this repro on a fork (DCO-signed) and I'm happy to open a PR if it's useful.

Activity

  1. changed the title [-]`Option<&T>` reference-to-scalar constant dropped -> silent null deref (CUDA 700) at runtime[/-] [+]kernel hits an illegal memory access (CUDA 700) when it uses Option<&T>::unwrap_or(&literal)[/+] on Jun 7, 2026
  2. added
    bug
    miscompile
    codegen
    on Jun 8, 2026
  3. added
    safety
    on Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcodegenmiscompilesafety

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions