Repository navigation
fix(mir-importer): read aggregate constant fields at their layout offsets - #394
Merged
nihalpasham merged 5 commits intoJul 23, 2026
Conversation
This was referenced Jul 13, 2026
Closed
alejandro-soto-franco
force-pushed
the
fix/aggregate-constant-field-offsets
branch
from
July 20, 2026 17:09
b787e13 to
f5fb360
Compare
…sets
An aggregate constant's bytes are the memory image of its type, so each field
starts at the offset rustc's layout gives it. The three walkers that carve those
bytes into fields instead advanced a cursor by each field's size, which assumes
the fields are packed in declaration order. That holds for neither a type rustc
pads nor one it reorders, and every field after the first padded gap was then
read from the wrong offset.
The values were wrong, silently. On the device
struct Padded { a: u8, b: u64 }
const PADDED: Padded = Padded { a: 200, b: 0x7d25933d707303e5 };
read back as `a = 229` (the low byte of `b`) and `b = 0xc87d25933d707303`
(`b` shifted by a byte), with no diagnostic. `(u8, u64)` and every other
ascending-alignment tuple went the same way; reversing the fields hid it,
since descending alignment needs no padding.
Take each field's offset from the layout instead:
- `translate_tuple_constant` and `translate_struct_constant` now read the
offsets from `rust_ty`'s layout. `translate_struct_constant` had them
available all along: its `_rust_ty` parameter was marked "Reserved for
future layout computation".
- `build_const_from_bytes`, which handles nested aggregates, takes them from
the struct type, which already carries rustc's layout.
- `constant_storage_size` reports a struct's rustc size rather than the sum of
its field sizes, which under-reported every padded struct and so handed a
short slice to the nested path.
That size is also the element stride in the array arm of
`build_const_from_bytes`, so `struct Batch { arr: [Padded; 2] }` strode by the
9-byte field sum where the elements sit 16 apart, and every element after the
first was read from the wrong offset. That second silent miscompile goes with
the first.
Whether a struct's layout is known is decided by its recorded offsets rather
than by its size. `translator/types.rs` stores empty offsets and a zero size
when `Ty::layout()` fails, so a zero size on its own would also reject a struct
that has fields and is genuinely zero bytes wide, such as a `PhantomData`
newtype. Those reach the struct arm because `is_zst_type` calls a struct
zero-sized only when it has no fields at all. `struct_storage_size` is a pure
predicate over that decision, table-tested across the padded, zero-sized,
field-less and layout-missing cases.
The enum constant path already did this, and is unchanged. An enum or an `f64`
nested inside a struct constant still reports an unsupported aggregate field
type; those error rather than miscompile, and are left as they were.
`aggregate_field_offsets` now sits in `translator/layout.rs` beside the enum
readers, whose module doc asks that every offset derivation live in one place
so the consumers cannot drift. The array arm gained the same bounds check the
struct arm carries, in place of an unchecked slice.
Found by the in-tree rustlantis differential fuzzer (`crates/fuzzer`), on seed
71 of a 0..200 sweep: CPU and GPU traces of the same program disagreed. That
sweep is now clean. The fuzzer is unchanged here, so its coverage of padded
aggregates stays incidental rather than guaranteed.
The `padded_constants` example covers a padded struct, the same shape as a
tuple, a nested padded struct, a padded struct with a float field, an array of
padded structs, a `#[repr(C)]` struct, and a descending-alignment struct as a
control. It fails on the unfixed compiler. CI compiles the example but does not
execute it, since `examples-compile.yml` runs `smoketest.sh --compile-only` on a
runner with no GPU, so the device-side assertions are a manual check.
Signed-off-by: Alejandro Soto Franco <[email protected]>
…ovenance translate_struct_constant followed the first provenance entry whenever one existed, assuming the by-ref shape stable_mir uses for promoted constants like &(8..16). Two defects hid in that guess. A by-value struct constant with a pointer field had its own bytes silently replaced by the first pointee's bytes before field decoding. And the new layout query ran on constant.ty(), which at the promoted-reference call site is the reference type, whose Primitive field shape the layout helper rejects, so every promoted &struct constant stopped compiling. Decide by the constant's Rust type instead: Ref/RawPtr means follow the indirection and query layout on the pointee; an aggregate type means the allocation is the struct's own memory image. Pointer relocations in the struct data itself are rejected with a diagnostic on both paths, in the same style as the existing tuple, array, and enum guards: the stored bytes of a relocation are an offset into the target allocation, not an address, and the per-field decoders carry no provenance map to resolve them. Also report the bounds-check failure in build_const_from_bytes without re-adding the offsets unchecked in the message.
nihalpasham
force-pushed
the
fix/aggregate-constant-field-offsets
branch
from
July 23, 2026 12:01
f5fb360 to
998afa1
Compare
Collaborator
|
Thanks for the PR I layered maintainer commits on top:
Validated: 810 importer tests, the full workspace suite, padded_constants (21 fields, 63 threads) on an RTX 5090, and the error lane. Closes #393. |
…railing field Add error_struct_constant_provenance (registered in the smoketest error lane) so a struct constant holding a reference fails loudly instead of decoding relocation bytes as an address. Wire the previously dead take_repr_c(_, 2) arm into padded_constants so the #[repr(C)] trailing field after interior padding is read on the device, and record the struct-constant layout rules in the supported-features appendix.
…by-ref constant paths Two remaining silent-wrong shapes in by-ref constant decoding now fail loudly. A reference-to-struct constant whose pointer stores a non-zero offset into its target allocation was decoded from byte zero, reading every field from the wrong base; the offset is now read the same way the scalar pointer path reads it, and interior references are rejected. And on the promoted-constant materializer path, a pointee that itself contains pointer relocations had those placeholder bytes decoded as addresses; the target allocation's provenance is now checked before any byte decoding, matching the struct, tuple, array, and enum guards. Plain promoted literals like &77 and pointer-free aggregates behind a reference are unaffected.
The compile_to_ptx fake-tool tests intermittently failed toolchain resolution with 'llc --version did not run successfully'. A test that forks concurrently can duplicate the script's briefly-open write fd into its child, and until that child execs, running the fresh script fails with ETXTBSY. Probe each staged tool until it executes so no toolchain-resolution assert can observe the window. This pre-existing flake hit both local runs and this PR's CI, so the fix rides along.
nihalpasham
force-pushed
the
fix/aggregate-constant-field-offsets
branch
from
July 23, 2026 12:19
998afa1 to
56eb8ec
Compare
alejandro-soto-franco
deleted the
fix/aggregate-constant-field-offsets
branch
September 9, 2026 17:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #393
Summary
An aggregate constant's bytes are the memory image of its type, so each field starts at the offset rustc's layout gives it. The three walkers that carve those bytes into fields advanced a cursor by each field's size instead, which assumes the fields are packed in declaration order. That holds for neither a type rustc pads nor one it reorders, so every field after the first padded gap was read from the wrong offset, silently.
On the device:
read back as
a = 229(the low byte ofb) andb = 0xc87d25933d707303(bshifted by a byte), with no diagnostic.Changes
translate_tuple_constantandtranslate_struct_constantread field offsets fromrust_ty's layout.build_const_from_bytestakes them from the struct type, which already carries rustc's layout.constant_storage_sizereports a struct's rustc size rather than the sum of its field sizes.aggregate_field_offsetsmoves intotranslator/layout.rsbeside the enum readers, whose module doc asks that every offset derivation live in one place.padded_constantsexample, and a table-driven unit test over the storage-size predicate.Rationale
Two silent miscompiles are fixed here, and the second is worth naming separately. A struct's recorded size is also the element stride in the array arm of
build_const_from_bytes, sostruct Batch { arr: [Padded; 2] }strode by the 9-byte field sum where the elements sit 16 apart, and every element after the first was read from the wrong offset.Whether a struct's layout is known is decided by its recorded offsets rather than by its size.
translator/types.rsstores empty offsets and a zero size whenTy::layout()fails, so keying on the size alone would also reject a struct that has fields and is genuinely zero bytes wide, such as aPhantomDatanewtype. Those reach the struct arm becauseis_zst_typecalls a struct zero-sized only when it has no fields at all.The enum constant path already read offsets from the layout and is unchanged. An enum or an
f64nested inside a struct constant still reports an unsupported aggregate field type; those error rather than miscompile and are left as they were.Found by the in-tree rustlantis differential fuzzer (
crates/fuzzer) on seed 71 of a 0..200 sweep. The fuzzer is unchanged here, so its coverage of padded aggregates stays incidental rather than guaranteed.Validation
cargo check --workspace --all-targetscargo test -p mir-importer(802 passed)cargo test -p cuda-oxide-codegen(117 passed)cargo clippy --workspace -- -D warningscargo fmt --all -- --check(cd crates/rustc-codegen-cuda && cargo fmt --all -- --check)(cd crates/rustc-codegen-cuda/examples/padded_constants && cargo fmt --all -- --check)(cd crates/rustc-codegen-cuda/examples/padded_constants && cargo clippy --all-targets -- -D warnings)RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --workspaceThe example asserts on the device and exits non-zero on mismatch. It is compiled but not executed by CI, since
examples-compile.ymlrunssmoketest.sh --compile-onlyon a runner with no GPU, so those assertions are a manual check. Run here on an RTX 5060, driver 610.43.03.The example covers a padded struct, the same shape as a tuple, a nested padded struct, a padded struct with a float field, an array of padded structs, a
#[repr(C)]struct, and a descending-alignment struct as a control. It fails on the unfixed compiler.Note: the two example-scoped gates above were failing on this branch before this revision, on code that predates it. Example crates are excluded from the workspace, so a workspace-level
cargo fmt --alldoes not reach them. Both are fixed here.