You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Compiler: reference returned from raw-pointer deref compiles but segfaults at runtime #687
Found by spell-and-run while designing #681. Minimal repro (SIGSEGV, exit 139):
type T { xs: Vec[i64] }
fn view(p: i64) -> &T: unsafe { &(*(p as *const T)) }
fn main:
var t = T { xs: Vec.new() }
t.xs.push(9)
let p = &raw const t as i64
print(f"{view(p).xs.get(0)} {view(p).xs.len()}")
The in-place form works correctly (prints fields fine inside the same unsafe block):
unsafe:
let n = (*(p as *const T)).xs.len() // OK
Two acceptable resolutions, maintainer's call which:
If a reference constructed from a raw pointer cannot carry the generational/view-origin state the checker needs, the compiler should REJECT the ref-return at compile time (escape/view-origin rule), not emit code that crashes.
If the construction is meant to be legal inside unsafe, the emitted deref/generation check is miscompiled and should be fixed.
Spec refs: §22 (view-origin/escape tracking) and the generational-reference model (docs/decisions.md niche model; .reference/Vale). Impact: blocks any pattern that shares large read-only compiler state (Sema) across worker threads via a ref-returning accessor — #681 has been redesigned to avoid needing it (serial per-unit generation), but the crash itself is a real soundness/codegen defect.
TRIAGED — mechanism pinned by crash backtrace: the SEGV is a near-null read in with_vec_get_ptr (address=0x8), i.e. the returned &T never points at t. Codegen lowers &(*(p as *const T)) by materializing the deref into a callee-local temporary and returning a reference to the temp — dangling the moment view returns (the in-place form works because no temp escapes). Generational/view machinery is not involved at runtime; With refs here are plain pointers.
Decision brief (resolution 1 reject vs resolution 2 fix-codegen):
References: Rust blesses exactly this idiom — &*ptr in unsafe is THE raw-to-ref reborrow, a no-op forwarding the pointer value; validity is the programmer's unsafe obligation. Vale's generational model would demand more ceremony at the boundary, but With's runtime representation of &T is a plain pointer, so there is no metadata to synthesize — the no-op reborrow is available for free.
Mission fit: "raw C stays explicit; modeled C becomes humane" — the pattern is the standard FFI/shared-state accessor idiom, written inside unsafe exactly as the model requires. Rejecting it (resolution 1) forces contortions in precisely the library-maintainer-tier code that needs it (Compiler: window codegen-unit fan-out; dispose base LLVM module and MIR before fan-out (study delta 2) #681's shared-Sema accessor was the original motivation).
Most with-y: resolution 2 — make the obvious unsafe idiom work; the unsafe block is already the programmer's explicit acceptance of the validity obligation. A compile-time rejection would be a rule that restricts a legitimate pattern.
BDFL prediction: resolution 2 — the issue's own framing treats "compiles but segfaults" as the offense, and the wanted-use-case is recorded in the same breath.
Fix shape: in the deref lowering, when the operand of & is a deref of a raw pointer (through unsafe), forward the pointer value as the reference — no temp materialization. Repro becomes the fixture (prints 9 1); the escape/view-origin question stays untouched (raw-derived refs remain the programmer's obligation, documented).
FIXED (f58b956, battery-green, fixpoint byte-identical, drop-audit 60/0, reseeded g f58b956). Root cause was a MIR type-lowering gap, not the codegen framing in the triage: fallback_expr_type had a DEREF arm (ptr/ref operand -> pointee) but no inverse REF arm, so a raw-derived &(*(p as *const T)) whose node wasn't in typed_expr_types fell through to void — the ref temp was void-typed and the fn's return slot got const () instead of the reference (near-null crash in with_vec_get_ptr downstream). Added UOP_REF / UOP_RAW_REF_CONST / UOP_RAW_REF_MUT arms as a pure frozen find_exact_type lookup, symmetric with the existing DEREF inverse; no codegen change. Resolution 2 per the brief. Repro now prints 9 1; behav_raw_ptr_ref_return.w pins struct/scalar/&raw-mut reborrows.
Activity
Compiler: codegen reads MIR through a raw pointer, not an owned copy (#…
ehartford commentedon Jul 20, 2026
TRIAGED — mechanism pinned by crash backtrace: the SEGV is a near-null read in
with_vec_get_ptr(address=0x8), i.e. the returned&Tnever points att. Codegen lowers&(*(p as *const T))by materializing the deref into a callee-local temporary and returning a reference to the temp — dangling the momentviewreturns (the in-place form works because no temp escapes). Generational/view machinery is not involved at runtime; With refs here are plain pointers.Decision brief (resolution 1 reject vs resolution 2 fix-codegen):
&*ptrin unsafe is THE raw-to-ref reborrow, a no-op forwarding the pointer value; validity is the programmer's unsafe obligation. Vale's generational model would demand more ceremony at the boundary, but With's runtime representation of&Tis a plain pointer, so there is no metadata to synthesize — the no-op reborrow is available for free.unsafeexactly as the model requires. Rejecting it (resolution 1) forces contortions in precisely the library-maintainer-tier code that needs it (Compiler: window codegen-unit fan-out; dispose base LLVM module and MIR before fan-out (study delta 2) #681's shared-Sema accessor was the original motivation).unsafeblock is already the programmer's explicit acceptance of the validity obligation. A compile-time rejection would be a rule that restricts a legitimate pattern.Fix shape: in the deref lowering, when the operand of
&is a deref of a raw pointer (through unsafe), forward the pointer value as the reference — no temp materialization. Repro becomes the fixture (prints9 1); the escape/view-origin question stays untouched (raw-derived refs remain the programmer's obligation, documented).ehartford commentedon Jul 20, 2026
FIXED (f58b956, battery-green, fixpoint byte-identical, drop-audit 60/0, reseeded g f58b956). Root cause was a MIR type-lowering gap, not the codegen framing in the triage: fallback_expr_type had a DEREF arm (ptr/ref operand -> pointee) but no inverse REF arm, so a raw-derived
&(*(p as *const T))whose node wasn't in typed_expr_types fell through to void — the ref temp was void-typed and the fn's return slot gotconst ()instead of the reference (near-null crash in with_vec_get_ptr downstream). Added UOP_REF / UOP_RAW_REF_CONST / UOP_RAW_REF_MUT arms as a pure frozen find_exact_type lookup, symmetric with the existing DEREF inverse; no codegen change. Resolution 2 per the brief. Repro now prints9 1; behav_raw_ptr_ref_return.w pins struct/scalar/&raw-mut reborrows.MIR: fallback_expr_type builds ref/ptr types for &x / &raw x (#687)