rust-lang / rust-lang/rust

GVN pass introduces UB by reborrowing from disabled reference

Open
#160,004 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-mir-opt A-mir-opt-GVN C-bug I-miscompile I-unsound P-high T-compiler T-opsem
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

fn fmt_mutref_string(s: & &mut String) {
    let _str = &**s;
}

fn main() {
    let mut s = String::from("hello");
    let ptr = &mut s as *mut String;

    let a = unsafe { &mut (*ptr) };
    a.push_str(" world");

    fmt_mutref_string(&a);
}

This program is fine according to both TB and SB, but if I run this with Miri with -Zmir-opt-level=1 -Zmir-enable-passes=+GVN, it reports UB in the final line:

error: Undefined Behavior: reborrow through <452> at alloc311[0x0] is forbidden
  --> gvn.rs:2:16
   |
 2 |     let _str = &**s;
   |                ^^^^ Undefined Behavior occurred here
   |
   = 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 <452> has state Disabled which forbids this reborrow (acting as a child read access)
help: the accessed tag <452> was created here, in the initial state Reserved
  --> gvn.rs:9:22
   |
 9 |     let a = unsafe { &mut (*ptr) };
   |                      ^^^^^^^^^^^
help: the accessed tag <452> later transitioned to Disabled due to a foreign write access at offsets [0x0..0x8]
  --> gvn.rs:10:5
   |
10 |     a.push_str(" world");
   |     ^^^^^^^^^^^^^^^^^^^^
   = help: this transition corresponds to a loss of read and write permissions
   = note: stack backtrace:
           0: fmt_mutref_string
               at gvn.rs:2:16: 2:20
           1: main
               at gvn.rs:12:5: 12:26

Cc @rust-lang/wg-mir-opt

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the gvn.rs reproducer and run it under Miri with -Zmir-opt-level=1 -Zmir-enable-passes=+GVN to confirm the failure. Trace the GVN pass's handling of the reborrow and the disabled reference, then verify that the reproducer no longer reports undefined behavior while retaining the stated TB and SB behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.