GVN does not account for implicit retags during assignment

Aperta
#165,327 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
8/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
compilers

Direzione di ricerca

Reproduce the snippet in the issue under Miri with -Zmir-opt-level=1 -Zmir-enable-passes=+GVN -Zmiri-tree-borrows to confirm the Tree Borrows error at borrow_field_a. The open question is whether the fix belongs in MIR semantics for implicit retags or in the GVN pass, and the maintainers have not decided that. Done means the reproducer passes Miri under both Stacked Borrows and Tree Borrows, which needs maintainer agreement on the approach first.

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

Descrizione

A-mir-opt A-mir-opt-GVN I-miscompile I-unsound P-medium T-compiler

This code is fine under our current aliasing models, but GVN optimizes it into something that has UB under both Stacked Borrows and Tree Borrows:

fn main() {
    struct Foo {
        a: String,
        b: Vec<u32>,
    }

    unsafe fn borrow_field_a<'a>(this: *mut Foo) -> &'a mut String {
        &mut (*this).a
    }

    unsafe fn borrow_field_b<'a>(this: *mut Foo) -> &'a mut Vec<u32> {
        &mut (*this).b
    }

    let mut foo = Foo { a: "hello".into(), b: vec![0, 1, 2] };

    let ptr = &mut foo as *mut Foo;

    let a = unsafe { borrow_field_a(ptr) };
    let b = unsafe { borrow_field_b(ptr) };
    b.push(4);
    a.push_str(" world");
    assert_eq!(format!("{:?} {:?}", a, b), r#""hello world" [0, 1, 2, 4]"#);
}

Running this in Miri with -Zmir-opt-level=1 -Zmir-enable-passes=+GVN -Zmiri-tree-borrows reports UB:

error: Undefined Behavior: reborrow through <605> at alloc406[0x0] is forbidden
  --> /opt/compiler-explorer/rust-miri-nightly/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:2918:70
   |
   = note: Undefined Behavior occurred here
  ::: /opt/compiler-explorer/rust-miri-nightly/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:2924:0
   |
   = note: in this macro invocation
   |
   = help: this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental
   = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/tree-borrows.md for further information
   = help: the accessed tag <605> has state Disabled which forbids this reborrow (acting as a child read access)
help: the accessed tag <605> was created here, in the initial state Reserved
  --> <source>:19:22
   |
19 |     let a = unsafe { borrow_field_a(ptr) };
   |                      ^^^^^^^^^^^^^^^^^^^
help: the accessed tag <605> later transitioned to Disabled due to a foreign write access at offsets [0x0..0x8]
  --> <source>:22:5
   |
22 |     a.push_str(" world");
   |     ^^^^^^^^^^^^^^^^^^^^

https://rust.godbolt.org/z/z5nqqhfxn

Cc @rust-lang/wg-mir-opt
I'm not sure if the fix here is a change in MIR semantics or a change in GVN, but something surely needs to be fixed.

Lingua principale
Rust
Stelle
121k
Fork
17.3k
Merge medio
2g 21h
PR unite (30g)
531

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 rust-lang/rust

Tutte le issue di rust-lang/rust

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.