Repository navigation
Description
Summary
A #[kernel] that indexes a device slice with a data-dependent index using safe, checked Rust indexing (src[j]) no longer emits a bounds check. On an out-of-range index the kernel performs an out-of-bounds global read (CUDA 700) instead of terminating the thread.
I bisected this to the codegen backend (librustc_codegen_cuda):
- v0.2.0 (
faea395, 2026-06-05): the bounds check is present; the access is safe. a9748f4(2026-06-11): already gone.- current
main(29396b7, 2026-07-04): still gone.
So the change landed within about a week of the v0.2.0 release.
This looks distinct from #132 (Option::unwrap_or(&literal) miscompile) and #392 (a DisjointSlice::len() codegen crash): this is plain &[T] checked indexing losing its bounds check at runtime.
Suggested labels: safety, miscompile, codegen.
Kernel
#[kernel]
pub fn gather(src: &[f32], indices: &[u32], mut out: DisjointSlice<f32>) {
let idx = thread::index_1d();
let i = idx.get();
if let Some(out_elem) = out.get_mut(idx) {
let j = indices[i] as usize; // checked index load
*out_elem = src[j]; // checked index; j is runtime data, not statically provable
}
}j is loaded from device memory, so the compiler cannot prove j < src.len(). Under Rust semantics src[j] must bounds-check.
Reproduction
Set one index out of range and run the kernel:
let mut idx_host: Vec<u32> = (0..1024).map(|i| (1023 - i) as u32).collect();
idx_host[0] = 1_000_000; // src has 1024 elementscompute-sanitizer --tool memcheck cargo oxide run --arch sm_89
Observed behavior
Current main (29396b7, 2026-07-04):
========= Invalid __global__ read of size 4 bytes
========= at gather+0x110
========= by thread (0,0,0) in block (0,0,0)
========= Access to 0x... is out of bounds
========= and is 3987713 bytes after the nearest allocation at 0x... of size 4096 bytes
========= ERROR SUMMARY: 6 errors
(1024 f32 = 4096 bytes; index 1_000_000 x 4 = ~4 MB past the start — matching the reported offset.)
v0.2.0 (faea395, 2026-06-05), identical program:
Kernel finished without trapping.
out[0] (from OOB index 1_000_000) = 0
========= ERROR SUMMARY: 0 errors
The v0.2.0 PTX contains the guard: a setp.ge.u64 on indices[i] < src.len() that branches to a clean exit when out of range, so the out-of-bounds load never executes. The current-main PTX has no such guard and issues the ld.global unconditionally.
Expected behavior
Out-of-bounds checked indexing should not perform the read. Either the documented panic-to-trap behavior, or the v0.2.0 behavior (guard the access and skip it) — not a silent out-of-bounds global read.
PTX difference (gather kernel)
- v0.2.0: two bounds checks present (
setp.ge.u64→exitforindices[i] < indices.len()and forindices[i] < src.len()); generic loads. - current
main: only the thread guard remains; no bounds checks;ld.globalon the indexed access.
Environment
- Backend commits tested:
faea395(v0.2.0, safe),a9748f4(2026-06-11, unsafe),29396b7(2026-07-04main, unsafe). - Frontend: cargo-oxide v0.2.1. rustc nightly-2026-04-03. LLVM 21.1.8. CUDA 12.8. GPU: NVIDIA L4, driver 580.159.03,
sm_89.
I have the full repro (both kernels, before/after PTX, and both compute-sanitizer logs) and can share the repository link if useful.
Activity
SomeB1oody commentedon Jul 17, 2026
The problem
An out-of-bounds Rust index triggers a MIR Assert terminator.
mir-lowerincorrectly translated the assert's failure branch into a block containing only LLVMunreachable.In LLVM,
unreachablemeans "control flow can never reach this point" (reaching it is UB), not "stop when control flow reaches here". Based on that semantics,opt -O2(SimplifyCFG) folds the entire check intollvm.assume(j < len). The branch disappears and the optimizer treats the condition as always true. So an out-of-bounds index still executes, which is theInvalid __global__ readreported by compute-sanitizer.Why v0.2.0 didn't expose it
The v0.2.0 pipeline had no
opt -O2, so the branch was preserved as-is, and the NVPTX backend lowers unreachable to anexitinstruction. The IR was equally wrong back then. Safe by accident, not by design.I am working on fixing it.
fix(mir-lower): trap on assert failure so opt cannot delete bounds ch…
midiareshadi commentedon Jul 17, 2026
Thanks for the fast diagnosis and the fix — the
unreachable/ SimplifyCFG explanation makes complete sense of what I measured, and it corrects my framing: the IR was wrong all along, and v0.2.0 was safe by accident (noopt -O2), not by design.The full repro is public: https://github.com/midiareshadi/oxide-moving-target — three kernels, before/after PTX/SASS, and both compute-sanitizer logs (
faea395vs29396b7).I see #405 is verified on sm_120a. I have the original repro on an L4 (sm_89) — I'll test the fix there and report back.
fix(mir-lower): trap on assert failure so opt cannot delete bounds ch…