Skip to content

mir-lower: fat-pointer casts can round-trip raw shared-memory bytes (emit_pointer_cast lacks the transmute guard) #883

Closed

Description

@nihalpasham

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 all

Why 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

  1. added
    bug
    miscompile
    IR-lowering
    on Aug 14, 2026
  2. Ayush7614 commented on Aug 14, 2026

    @Ayush7614
    Contributor

    I would like to take this one.

    Plan (matching the existing transmute rejection style):

    • Add the same address-space(3) policy to emit_pointer_cast's memory-round-trip dispatch (the src_is_struct && dst_is_struct arm at cast.rs:483 and the array/int/struct arm at cast.rs:492-501), using llvm_type_contains_pointer_in_address_space to 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.
    • Add unit tests next to transmute_involving_shared_pointer_uses_target_dependent_width / shared_pointer_to_integer_transmute_genericizes_before_ptr_to_int covering fat-ptr→fat-ptr with a shared data pointer in both source and destination positions.
    • Confirm cargo test -p mir-lower stays green and generic/global fat-pointer casts are unchanged.

    Will follow up with a PR.

  3. noobyogi0010 commented on Sep 10, 2026

    @noobyogi0010

    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!

  4. uurl commented on Oct 8, 2026

    @uurl
    Contributor

    I investigated the reachability of the emit_pointer_cast memory fallback on commit 1691162ab9, 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_unsize example:

    let raw: *const [f32] = values;
    let recast: *const [u32] = raw as *const [u32];

    The generated MIR contains the expected operations:

    • PointerCoercionUnsize: AS3 array pointer to MirSliceType<f32>.
    • PtrToPtr: MirSliceType<f32> to MirSliceType<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 PtrToPtr cast exits through the val_ty == llvm_ty fast path in convert(), without entering emit_pointer_cast() or emit_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_fallback successfully 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 MirCastOp construction sites in mir-importer, cuda-oxide-codegen, mir-transforms, and mir-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?

  5. added this to the cuda oxide v0.4.0 milestone on Oct 25, 2026
  6. added
    cuda-oxide
    on Oct 29, 2026
  7. nihalpasham commented on Nov 10, 2026

    @nihalpasham
    CollaboratorAuthor

    Closing as resolved on main:

    1. PR fix(mir-lower): legalize address-space 3 slice fat-pointer casts in SSA #1061 (try_emit_slice_fat_pointer_cast in mir-lower/src/convert/ops/cast.rs) lowers slice fat-pointer casts ({ ptr, i64 }) directly in SSA via extractvalue + addrspacecast + insertvalue without round-tripping raw addrspace(3) bytes through memory.
    2. PointerCoercionUnsize in mir-importer already canonicalizes shared-array slice data pointers to addrspace(0), and the producer audit confirmed no Rust source path reaches the aggregate memory fallback in emit_pointer_cast.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    IR-loweringbugcuda-oxidemiscompile

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions