Repository navigation
Description
Description
In a device function that takes a struct by value, calling core::ptr::read_unaligned(core::ptr::addr_of!(p)) and then reading a field of p directly (p.f1) fails device codegen with field_addr index 1 out of bounds for struct with 1 fields. The failure is independent of the struct's repr (Rust, C, align(8), align(16) all fail), depends on order (reading p.f1 before the read_unaligned compiles), depends on the field (p.f0 compiles, p.f1 fails), and also fires when the field is read from a copy made after the read_unaligned (let q = p; q.f1). The identical crate compiles and prints the right value with CUDA_OXIDE_DEBUG=full (--device-debug), which skips mem2reg and the preparation passes.
read_unaligned reads through #[repr(packed)] struct Unaligned<T>(T), so the importer casts the parameter's slot pointer to *const Unaligned<S> and loads an Unaligned<S>. With CUDA_OXIDE_DUMP_MIR=1 the dialect-mir after preparation is still well typed: the parameter slot is ptr<S> and the failing field_addr carries aggregate_ty: S, field_index: 1. Once mem2reg removes the intermediate pointer locals, the slot's uses are the cast chain to Unaligned<S> plus the later field_addr, and the lowering evidently types the slot's storage from the one-field Unaligned<S> load, so the field_addr is checked against a struct with one field: index 0 exists in both (compiles), index 1 does not (rejected). With the preparation passes off the cast target is only reachable through a pointer local, the slot keeps S, and everything lowers. Fail-closed today, but a mismatch that happened to pass the field-count check would address the wrong field silently. Reproduced on main at a412e09; mir-lower is unchanged at the current main (b9e9c24).
Error / actual behavior
$ cargo oxide run repro
error: [rustc_codegen_cuda] Device codegen failed: PTX generation failed: Lowering failed: Compilation error: invalid input program.
field_addr index 1 out of bounds for struct with 1 fields
body of fn body(p1: S) |
default build | CUDA_OXIDE_DEBUG=full |
|---|---|---|
read_unaligned(addr_of!(p1)) then p1.f1 (below) |
fails | ok, prints 3 |
same, S as #[repr(Rust)], #[repr(align(8))], #[repr(align(16))] |
fails | — |
read_unaligned(addr_of!(p1)) then p1.f0 |
ok | — |
p1.f1 first, then read_unaligned(addr_of!(p1)) |
ok | — |
read_unaligned(addr_of!(p1)) then let q = p1; q.f1 |
fails | — |
read(addr_of!(p1)) (aligned) then p1.f1 |
ok | — |
Expected behavior
The kernel compiles and prints 3 (t.f0 + p1.f1 = 1 + 2), as the host does and as the --device-debug build does.
Minimal reproduction example
use cuda_core::{CudaContext, DeviceBuffer, LaunchConfig};
use cuda_device::{DisjointSlice, cuda_module, device, kernel, thread};
#[derive(Clone, Copy)]
#[repr(C)]
pub struct S { pub f0: u32, pub f1: u32, pub f2: u32, pub f3: u32 }
#[device]
#[inline(never)]
pub fn body(p1: S) -> u64 {
let t: S = unsafe { core::ptr::read_unaligned(core::ptr::addr_of!(p1)) };
(t.f0 as u64) + (p1.f1 as u64) // fails; `p1.f0`, or reading `p1.f1` before the copy, compiles
}
#[cuda_module]
mod kernels {
use super::*;
#[kernel]
pub fn k(p1: S, mut out: DisjointSlice<u64>) {
let h = body(p1);
if let Some(s) = out.get_mut(thread::index_1d()) { *s = h; }
}
}
fn main() {
let ctx = CudaContext::new(0).unwrap();
let stream = ctx.default_stream();
let module = kernels::load(&ctx).unwrap();
let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1, 1, 1), shared_mem_bytes: 0 };
let mut out = DeviceBuffer::<u64>::zeroed(&stream, 1).unwrap();
unsafe { module.k(&stream, cfg, S { f0: 1, f1: 2, f2: 3, f3: 4 }, &mut out) }.unwrap();
println!("{}", out.to_host_vec(&stream).unwrap()[0]); // host: 3
}
// Standalone example crate; Cargo.toml as examples/vecadd. Run with `cargo oxide run <crate>`.Operating System
Ubuntu 24.04.3 LTS (Linux 6.14.0-29-generic)
GPU and GPU Architecture
NVIDIA GeForce RTX 4070 (sm_89). Not GPU-dependent: the failure is at build time, in the importer's verifier.
CUDA driver version
580.178.04
cargo oxide doctor
cargo-oxide environment check
==============================
Rust toolchain... ✓ rustc 1.99.1 (58cf8f9c3 2026-10-13)
rust-toolchain.toml... ✓ channel 1.99.1
Pinned toolchain active... ✓ 1.99.1-x86_64-unknown-linux-gnu
Required rustup components... ✓ rust-src, rustc-dev, rust-analyzer, clippy, rustfmt, llvm-tools
Codegen backend... ✓ <repo>/cuda-oxide/crates/rustc-codegen-cuda/target/x86_64-unknown-linux-gnu/debug/librustc_codegen_cuda.so
Project config (.cargo/cuda-oxide.toml)... - not present (using defaults)
CUDA headers (cuda.h)... ✓ /usr/local/cuda-13.3/include/cuda.h (discovered; neither CUDA_TOOLKIT_PATH nor CUDA_HOME is set)
CUDA toolkit (nvcc)... ✓ Cuda compilation tools, release 12.0, V12.0.140 (/bin/nvcc)
libNVVM (libnvvm.so)... ✓ libNVVM 2.0
nvJitLink (libnvJitLink.so)... ✓ nvJitLink 13.3
libdevice (libdevice.10.bc)... ✓ /usr/local/cuda/nvvm/libdevice/libdevice.10.bc
llc (LLVM)... ✓ LLVM version 23.1.1-rust-1.99.1-stable ($RUSTUP_HOME/toolchains/1.99.1-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/llc)
clang / libclang resource dir... ✓ /usr/lib/llvm-18/lib/clang/18 (via clang)
NVIDIA driver / GPU... ✓ NVIDIA GeForce RTX 4070 (compute capability 8.9, driver 580.178.04)
cuda-gdb (optional)... ✓ NVIDIA (R) CUDA Debugger (/bin/cuda-gdb)
compute-sanitizer (optional)... ✓ Version 2022.4.1 (/bin/compute-sanitizer)
wild linker (optional)... - not found (only needed for CUDA_OXIDE_LINKER=wild)
✅ Environment looks good!
rustc version
rustc 1.99.1 (58cf8f9c3 2026-10-13), host x86_64-unknown-linux-gnu, LLVM version 23.1.1
llc version
LLVM version 23.1.1-rust-1.99.1-stable (rustup llvm-tools component)
Additional context
No .ll or .ptx is produced. A second program reaches the same message through a different route: let v1: bool = false; let e = E::V1([129u8], if read_unaligned(addr_of!(v1)) { 1 } else { 2 }); fails with field_addr index 2 out of bounds for struct with 1 fields, i.e. the one-field Unaligned<bool> typing of the v1 slot leaks into the enum-payload construction that consumes the value. The common factor is a read_unaligned of a local or parameter followed by a projection involving it.
This issue was found with a fuzzer; the reproducer above was reduced from its test case. The report was written with LLM assistance.
Activity