Skip to content

Support relocation-free pointer/integer union constants via byte images #1058

Closed

Description

@uurl

Summary

Pointer/integer union constants are currently rejected because a byte-only
representation cannot preserve pointer provenance when the evaluated allocation
contains a pointer relocation.

There is, however, a narrower case where provenance is not involved: a
pointer/integer union whose evaluated storage contains no overlapping
relocations already has a complete byte representation. One common example is
a union initialized through its full-width integer alternative.

In that case, rustc's evaluated allocation already contains the complete storage
representation, so the union can be materialized using the existing byte-image
path.

Proposed scope

Support relocation-free mixed pointer/integer unions when:

  • the union storage is exactly one pointer word,
  • the union has natural pointer alignment,
  • all pointer alternatives are compatible thin pointers,
  • all integer alternatives are full pointer-width integers, and
  • no relocation overlaps the union storage.

For example:

#[repr(C)]
union PointerOrBits {
    pointer: *const u32,
    bits: u64,
}

const VALUE: PointerOrBits = PointerOrBits {
    bits: 0x1122_3344_5566_7788,
};

On a 64-bit target, this constant has no pointer relocation. Its evaluated byte
image is therefore sufficient to reconstruct the union storage exactly.

Non-goals

This should remain fail-closed for:

  • pointer/integer unions containing an actual pointer relocation,
  • fat pointers,
  • nested pointer aggregates,
  • partial-width integer alternatives,
  • mixed pointer address spaces,
  • padded or over-aligned pointer unions, and
  • mixed pointer/integer device-global union initializers.

Thin-pointer-only unions with relocations should continue to use the existing
provenance-preserving pointer carrier path.

Implementation direction

Keep the existing provenance-aware union classifier unchanged.

Before invoking it during allocation-backed union constant translation, detect
the relocation-free pointer/integer shape described above and route it through
the existing byte-image materializer.

This keeps relocation-bearing behavior unchanged while admitting the case where
the evaluated bytes are the complete storage truth.

Regression coverage

Add coverage for:

  • direct relocation-free pointer/integer union constants,
  • [U; N] relocation-free pointer/integer union constants,
  • optimized and non-optimized GPU execution,
  • rejection of pointer-width mismatches and partial-width integer alternatives,
  • LLVM code shape requiring byte-image materialization, and
  • absence of inttoptr reconstruction.

This is a follow-up to the pointer-union constant work in #927.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions