Repository navigation
Description
The gap
The transmute path refuses to let shared-memory (addrspace(3)) pointer
bytes travel through the stack-slot round trip, for good reason. But
emit_pointer_cast (crates/mir-lower/src/convert/ops/cast.rs, the
dispatches around lines 484 and 501) sends non-transmute casts of
aggregate-valued pointers down that same road with no shared-pointer check.
The reachable case is fat pointers, because a fat pointer is a two-field
value (data ptr, len) and two-field values take the memory path:
// s points into shared memory
let a: *mut [f32] = s;
let b = a as *mut [u32]; // (data ptr, len) round-trips through a
// scratch stack slot, raw addrspace(3)
// bytes and allWhy those raw bytes are dangerous
Two concrete hazards, both the same ones the transmute guard exists for:
hazard 1: physical width depends on output mode
llc / legacy NVVM: shared ptr = 8 bytes
modern NVVM (p3:32): shared ptr = 4 bytes, while Rust sizes every
pointer at 8, so the store/load bytes disagree
hazard 2: the raw bytes are an offset, not an address
first shared allocation: raw shared bytes = 0x0000 (looks like null!)
what Rust code should see: the 64-bit generic address (never 0)
Unlike the DST gaps, this one is silent: it produces bytes, not an error.
Silent-wrong is the worst category, even though the trigger pattern is
unusual. Pre-existing on main; surfaced during the #879 review.
What to fix
Give emit_pointer_cast's memory-round-trip dispatch the same policy the
transmute path has: if either side contains an addrspace(3) pointer
(llvm_type_contains_pointer_in_address_space already answers this,
recursing through structs and arrays), reject loudly with the same message
style, naming both types and the remedy (convert the scalar pointer; it
bridges through its 64-bit generic address). If a sound bridging exists for
the fat case (addrspacecast the data-pointer half, copy the metadata half),
that is a fine follow-up, but loud rejection closes the silent-wrong hole
today.
Acceptance criteria
- The fat-pointer repro above fails at compile time with the located,
actionable diagnostic instead of producing bytes. - Unit tests next to the existing transmute rejection tests, covering
fat-ptr-to-fat-ptr with a shared data pointer in source and destination
positions. - Generic and global fat-pointer casts unchanged;
cargo test -p mir-lower
green.
Activity
Ayush7614 commentedon Aug 14, 2026
I would like to take this one.
Plan (matching the existing transmute rejection style):
emit_pointer_cast's memory-round-trip dispatch (thesrc_is_struct && dst_is_structarm at cast.rs:483 and the array/int/struct arm at cast.rs:492-501), usingllvm_type_contains_pointer_in_address_spaceto detect a shared pointer on either side of a fat/aggregate value, and reject loudly with the same message style / remedy as the transmute path.transmute_involving_shared_pointer_uses_target_dependent_width/shared_pointer_to_integer_transmute_genericizes_before_ptr_to_intcovering fat-ptr→fat-ptr with a shared data pointer in both source and destination positions.cargo test -p mir-lowerstays green and generic/global fat-pointer casts are unchanged.Will follow up with a PR.
noobyogi0010 commentedon Sep 10, 2026
Heyy,
Is this issue still needs any contributions?
I see a bunch of PRs linked to it, so just wanted to confirm before starting to do something.
Thanks!
uurl commentedon Oct 8, 2026
I investigated the reachability of the
emit_pointer_castmemory fallback on commit1691162ab9, focusing on casts originating from actual Rust source rather than manually constructed MIR types.Source-level reproduction
I reproduced the fat-pointer cast described in this issue using the existing
shared_slice_unsizeexample:The generated MIR contains the expected operations:
PointerCoercionUnsize: AS3 array pointer toMirSliceType<f32>.PtrToPtr:MirSliceType<f32>toMirSliceType<u32>.However, the unsize operation converts the AS3 data pointer to the canonical generic-address-space slice representation:
{ ptr addrspace(0), i64 }Both slice types therefore lower to the same LLVM type.
Instrumentation confirmed that the
PtrToPtrcast exits through theval_ty == llvm_tyfast path inconvert(), without enteringemit_pointer_cast()oremit_transmute_via_memory().Lowering control test
I also instrumented both memory-fallback branches in
emit_pointer_cast().The existing unit test
cluster_shared_slice_fat_pointer_cast_keeps_existing_fallbacksuccessfully triggered the struct-to-struct fallback in both directions:{ ptr addrspace(7), i64 } -> { ptr addrspace(0), i64 }{ ptr addrspace(0), i64 } -> { ptr addrspace(7), i64 }This confirms that the fallback exists for artificially constructed aggregate representations, but does not establish source-level reachability.
Producer audit
I audited the known
MirCastOpconstruction sites inmir-importer,cuda-oxide-codegen,mir-transforms, andmir-lower.I did not identify a production path that generates a non-Transmute cast of an aggregate containing an AS3/AS7 pointer and reaches the memory fallback.
Conclusion
The specific source-level reproducer in this issue does not currently reach the reported unsafe path.
This does not establish that every possible path is unreachable. However, I would prefer to identify a concrete source-level reproducer before adding a rejection guard.
Is there another Rust construct or MIR transformation expected to produce an aggregate containing an AS3 pointer at this fallback?
nihalpasham commentedon Nov 10, 2026
Closing as resolved on
main:try_emit_slice_fat_pointer_castinmir-lower/src/convert/ops/cast.rs) lowers slice fat-pointer casts ({ ptr, i64 }) directly in SSA viaextractvalue+addrspacecast+insertvaluewithout round-tripping rawaddrspace(3)bytes through memory.PointerCoercionUnsizeinmir-importeralready canonicalizes shared-array slice data pointers toaddrspace(0), and the producer audit confirmed no Rust source path reaches the aggregate memory fallback inemit_pointer_cast.