Repository navigation
Description
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
- fix(codegen): compile step_by by translating Pat types and rebuilding niched Option aggregates #64 (merged) handled niche-optimized
Option<&T>aggregates/transmutes — this is the const-operand half of the sameOption<&T>story; they don't overlap in code. - a kernel which accepts a reference to an application struct seems to cause illegal memory access #29 has the same CUDA-700 symptom but looks like a different cause (kernel-argument marshalling, not const materialization) — flagging as same symptom, probably not the same bug.
- Same "no silent miscompiles" direction as fix(codegen): convert three silent miscompiles to hard build errors #27.
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
[-]`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)[/+]fix(mir-lower): harden enum tag paths against silent index/value conf…
fix(mir-importer): follow provenance for non-struct pointer constants
test: add option_ref_unwrap_or regression example for issue NVIDIA#132