Repository navigation
Description
Description
Aggregate constants are lowered by reading their bytes field by field with a cursor that advances by each field's size. That assumes the fields are packed in declaration order. A constant's bytes are the memory image of its type, so a field starts at the offset rustc's layout gives it, and rustc both pads between fields and reorders them. Every field after the first padded gap is therefore read from the wrong offset, and the device silently computes with the wrong values. No diagnostic, no error, wrong answers.
This reaches ordinary safe Rust: any const of a struct whose fields need padding, which is most of them.
Minimal reproducer
#[derive(Clone, Copy)]
pub struct Padded {
pub a: u8,
pub b: u64,
}
const PADDED: Padded = Padded { a: 200, b: 9017775720968160229 }; // 0x7d25933d707303e5
// The constant has to be materialised, so pass it by value across a call
// boundary. Reading `PADDED.b` directly is const-folded before it reaches the
// importer and comes out correct, which is part of why this went unnoticed.
#[inline(never)]
fn read_it(s: Padded) -> (u8, u64) {
(s.a, s.b)
}
#[kernel]
pub fn k(mut out: DisjointSlice<u64>) {
let (a, b) = read_it(PADDED);
// ... write a and b out
}Expected behavior
a == 200, b == 0x7d25933d707303e5, as on the host.
Actual behavior
On the device:
a == 229, which is0xe5, the low byte ofb.b == 0xc87d25933d707303, which isb's bytes shifted up by one, witha's0xc8dragged into the top.
The constants are already wrong in the emitted NVVM IR, before PTX:
; struct type is correctly reordered by layout, but the constants in it are not
%v0 = insertvalue { i64, i8, [7 x i8] } undef, i64 14446744493995619075, 0 ; want 9017775720968160229
%v1 = insertvalue { i64, i8, [7 x i8] } %v0, i8 229, 1 ; want 200Which shapes are affected
Any layout with padding. For tuple constants the rule is exact: a tuple miscompiles whenever its declared field order is not already sorted by descending alignment, which is precisely when rustc pads or reorders it.
| shape | result |
|---|---|
(u8, u64), (u16, u64), (u32, u64), (u8, u16), (char, u64), (char, u128), (u8, u128), (u64, u128) |
wrong |
(u64, u8), (u64, u32), (u64, char), (u32, char), (char, u32), (char, u8), (u16, u8), (u128, char) |
correct |
Reverse any failing pair and it passes: descending alignment needs no padding. Struct constants, nested structs, and struct constants with float fields are affected the same way. The float case is worse than a shift, since a misaligned read reinterprets the bytes: f: f32 = 1.25 came back as -3139584.
Root cause
crates/mir-importer/src/translator/rvalue.rs. Three walkers carve an aggregate constant's bytes with byte_offset += byte_size:
translate_tuple_constanttranslate_struct_constant, whose_rust_typarameter is unused and marked "Reserved for future layout computation", so the layout was never consultedbuild_const_from_bytes, for nested aggregates
constant_storage_size compounds it for the nested case: it reports a struct's size as the sum of its field sizes, which under-reports any padded struct and hands a short slice to the nested walker.
The enum constant path in the same file already reads its field offsets from the layout, so the correct pattern is in the tree; the struct and tuple paths just don't use it.
How it was found
The in-tree rustlantis differential fuzzer (crates/fuzzer), sweeping seeds 0..200: seed 71 produced different CPU and GPU trace hashes for the same program. Reducing it showed every individual value computed correctly and only aggregates diverging. The fuzzer is not run in CI, and this was the only mismatch in the sweep.
Environment
- GPU: RTX 5060 Laptop (sm_120a)
- CUDA driver version: 595.80
rustc --version: rustc 1.96.0-nightly (55e86c996 2026-04-02)llc --version: LLVM version 22.1.8
Fix
PR #394 takes each field's offset from the layout in all three walkers and reports a struct's rustc size in constant_storage_size. It adds a padded_constants example covering a padded struct, the same shape as a tuple, a nested padded struct, a padded struct with a float field, and a descending-alignment struct as a control. The example fails on the unfixed compiler and passes with it; the 0..200 fuzzer sweep is clean afterwards.
Activity
fix(fuzzer): parse a Rust type past the semicolon inside an array (#766)
feat(fuzzer): fold an aggregate into the trace by its leaves (#792)