Skip to content

Miscompile: r as *const i32 with r: &i32 reads the value and turns it into a pointer (a facade callback with scalar userdata segfaults in safe code) #1777

Closed

Description

@ehartford

Found by the D71 facade work (#1611). Minimal repro:

fn addr(r: &i32) -> *const i32: r as *const i32
fn main:
    let x = 42
    let p = addr(&x)
    print(unsafe { *p })

Expected: 42 (the cast yields the reference's address). Actual: the cast loads the i32 and reinterprets its value as a pointer, so the dereference faults. Through a c facade whose callback userdata is a scalar (&i32), the compiler performs this cast itself, so the segfault happens in safe code. Present on main. The handle tests use a struct userdata to avoid it.

Spec: §15.1 (&T is a reference value), §16.3 (pointer casts). Owner: the cast lowering for &T as *const T (MirLower/codegen), which must take the reference's address value, not load through it.

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