Skip to content

let x = arr[i] binds a live view instead of snapshotting a Copy element (D27, #740) #887

Closed

Description

@ehartford

Summary

let x = arr[i] where arr is an array/slice of a Copy element type binds a
live view into the element place instead of reading (snapshotting) the value.
A subsequent write to arr[i] is then observed through x, silently changing a
value the programmer already "read out".

Plain locals and struct fields correctly snapshot; only indexed-element access is
affected. This is the D27 "element access observes / xs[i] denotes the element
place" path (#740) failing to materialize a Copy value into a let.

Minimal repro

use std.builtins.print_i32
fn main:
    var v: [u8; 2] = [0xABu8, 0u8]
    let cur = v[0]       // binds a view, not a snapshot
    v[0] = 0x11u8
    print_i32(cur as i32)   // prints 17 (0x11); should print 171 (0xAB)

Blast radius (measured)

binding result
let x = local_var snapshot (correct)
let x = struct.field snapshot (correct)
let x = v[i] (array elem) live view (BUG)
let x = v[i] as T (cast) snapshot (correct)

Impact

This silently corrupted lib/std/crypto/gcm.w's GHASH shift loop
(let cur = v[j]; v[j] = …>>1; carry = cur & 1), producing wrong AES-GCM tags
for all dense data → bad_record_mac on every TLS record → all of std.tls
(and thus std.http HTTPS) broken. See #883. Worked around there by binding the
cast result (let old = v[j] as u32).

Expected

let x = <copy-typed place> reads the value at the point of binding (snapshot),
consistent with locals/fields and with "exactly as safe as Rust" (let c = v[i]
copies a Copy element).

Cross-ref: #740 (D27 element-access campaign), #883.

Activity

  1. ehartford commented on Aug 30, 2026

    @ehartford
    ContributorAuthor

    Resolved in a6a3e62c (battery green tree-wide, reseeded) — with a staleness correction first:

    The issue's Expected (snapshot on plain let) is non-conforming. D27 Ruling 3 rules the opposite: "Unannotated let binds the view unchanged; a typed binding establishes an owned-value demand." Binding-the-view stands; the demand spellings (let cur = v[j] as u32, or an annotation) are the ruled snapshot forms — exactly the GHASH workaround already in gcm.w.

    But the repro program was illegal With that the compiler failed to reject. §3.2/§21.1 rule 1: "The compiler rejects code that uses a borrow after the borrowed place has been mutated." The enforcement existed for explicit &v[0] but the let-bound element view never registered its borrow — the exact "origins-through-bindings can catch a let-bound view consumed later" gap D27 R3 named as pending when it retired the #730 stand-in gate.

    Fix: a view-bound binding whose initializer is a bare index place registers the same borrow &v[0] does (empty-dep fallback to the place root in record_view_binding_from_expr, scoped to index places — a broader first cut manufactured 16 phantom receiver-views from call expressions; 0 after scoping). Inherits the existing NLL machinery, so use-before-mutate stays legal.

    The repro now errors cannot mutate vwhilecur is a live view into it; fixtures pin the rejection, the NLL-legal ordering, and the cast-snapshot GHASH shape. The #883 corruption class (silent stale reads through invalidated element views) is closed loud. Both crypto/TLS footguns from that incident are now retired (#886, #887).

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