Rust codegen: Map::set mutates a shared ValueMap in place, causing use-after-free on ValueRef::property borrows

Aperta
#240 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp, rust
Ambito
backend

Direzione di ricerca

Start with valdi_core/src/valdi_core/rust/collections.rs and trace Map::set, then read the RustValdi.cpp functions valdi_rust_value_ref_get_property and valdi_rust_value_map_set described in the issue. Check how map handles share their ValueMap and how references are kept valid during mutation. Done means Map::set uses copy-on-write so changes do not mutate shared maps or invalidate live ValueRef::property borrows.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Found while reviewing https://github.com/Snapchat/Valdi/pull/211 (native Rust code generation). Not blocking that PR.

Map::set mutates a shared ValueMap in place, causing use-after-free on ValueRef::property borrows

File: valdi_core/src/valdi_core/rust/collections.rs

When a ValueRef pointing to a map entry is live (via ValueRef::property) alongside a Map sharing the same underlying ValueMap, calling Map::set can reallocate the map's backing storage and leave that ValueRef dangling (Use-After-Free). The problem starts in Map::set in collections.rs, which mutates the ref-counted ValueMap in place despite Map::clone, Map::to_valdi, and Map::from_valdi all sharing the same Ref<Valdi::ValueMap>.

In RustValdi.cpp, valdi_rust_value_ref_get_property returns &iterator->second — a direct pointer into ValueMap's phmap::flat_hash_map bucket array — as a ValueRef<'a>, while valdi_rust_value_map_set calls map->insert_or_assign(...) directly on the shared map. Inserting enough keys through one Map handle rehashes the FlatMap and frees the old buckets while another Map, Value, or borrowed ValueRef still references them, causing Use-After-Free in safe Rust as well as unexpected mutation across clones.

Make Map::set copy-on-write (clone the ValueMap into a fresh ValdiRustHandle when the handle or underlying ValueMap is shared) so mutating &mut Map never invalidates other handles or borrowed ValueRefs.

Lingua principale
C++
Stelle
16.4k
Fork
539
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Snapchat/Valdi

Tutte le issue di Snapchat/Valdi

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.