From 069d8fdb71a9a46a76552e8eea7d427b4d6f08a1 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 5 Jun 2022 12:25:46 -0400 Subject: [PATCH 1/3] test for Stacked Borrows error during vtable validation --- tests/fail/stacked_borrows/vtable.rs | 19 ++++++++++++++++++ tests/fail/stacked_borrows/vtable.stderr | 25 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/fail/stacked_borrows/vtable.rs create mode 100644 tests/fail/stacked_borrows/vtable.stderr diff --git a/tests/fail/stacked_borrows/vtable.rs b/tests/fail/stacked_borrows/vtable.rs new file mode 100644 index 0000000000..dd9ba1dfb2 --- /dev/null +++ b/tests/fail/stacked_borrows/vtable.rs @@ -0,0 +1,19 @@ +// error-pattern: vtable pointer does not have permission +#![feature(ptr_metadata)] + +trait Foo {} + +impl Foo for u32 {} + +fn uwu(thin: *const (), meta: &'static ()) -> *const dyn Foo { + core::ptr::from_raw_parts(thin, unsafe { core::mem::transmute(meta) }) +} + +fn main() { + unsafe { + let orig = 1_u32; + let x = &orig as &dyn Foo; + let (ptr, meta) = (x as *const dyn Foo).to_raw_parts(); + let _ = uwu(ptr, core::mem::transmute(meta)); + } +} diff --git a/tests/fail/stacked_borrows/vtable.stderr b/tests/fail/stacked_borrows/vtable.stderr new file mode 100644 index 0000000000..ac3d71045f --- /dev/null +++ b/tests/fail/stacked_borrows/vtable.stderr @@ -0,0 +1,25 @@ +error: Undefined Behavior: type validation failed: encountered vtable pointer does not have permission to read drop function pointer + --> RUSTLIB/core/src/ptr/metadata.rs:LL:CC + | +LL | unsafe { PtrRepr { components: PtrComponents { data_address, metadata } }.const_ptr } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered vtable pointer does not have permission to read drop function pointer + | + = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior + = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information + + = note: inside `std::ptr::from_raw_parts::` at RUSTLIB/core/src/ptr/metadata.rs:LL:CC +note: inside `uwu` at $DIR/vtable.rs:LL:CC + --> $DIR/vtable.rs:LL:CC + | +LL | core::ptr::from_raw_parts(thin, unsafe { core::mem::transmute(meta) }) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: inside `main` at $DIR/vtable.rs:LL:CC + --> $DIR/vtable.rs:LL:CC + | +LL | let _ = uwu(ptr, core::mem::transmute(meta)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + From c3b8509654451e746d6a33d2009c0c68a60c34ad Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 5 Jun 2022 12:26:16 -0400 Subject: [PATCH 2/3] =?UTF-8?q?rename=20ExperimentalUb=20=E2=86=92=20Stack?= =?UTF-8?q?edBorrowsUb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/diagnostics.rs | 14 +++++++------- src/stacked_borrows.rs | 9 +-------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/diagnostics.rs b/src/diagnostics.rs index 52f93a6cea..0e3e693e33 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -16,10 +16,9 @@ pub enum TerminationInfo { Exit(i64), Abort(String), UnsupportedInIsolation(String), - ExperimentalUb { + StackedBorrowsUb { msg: String, help: Option, - url: String, history: Option, }, Deadlock, @@ -43,7 +42,7 @@ impl fmt::Display for TerminationInfo { Exit(code) => write!(f, "the evaluated program completed with exit code {}", code), Abort(msg) => write!(f, "{}", msg), UnsupportedInIsolation(msg) => write!(f, "{}", msg), - ExperimentalUb { msg, .. } => write!(f, "{}", msg), + StackedBorrowsUb { msg, .. } => write!(f, "{}", msg), Deadlock => write!(f, "the evaluated program deadlocked"), MultipleSymbolDefinitions { link_name, .. } => write!(f, "multiple definitions of symbol `{}`", link_name), @@ -146,7 +145,7 @@ pub fn report_error<'tcx, 'mir>( Exit(code) => return Some(*code), Abort(_) => Some("abnormal termination"), UnsupportedInIsolation(_) => Some("unsupported operation"), - ExperimentalUb { .. } => Some("Undefined Behavior"), + StackedBorrowsUb { .. } => Some("Undefined Behavior"), Deadlock => Some("deadlock"), MultipleSymbolDefinitions { .. } | SymbolShimClashing { .. } => None, }; @@ -157,11 +156,12 @@ pub fn report_error<'tcx, 'mir>( (None, format!("pass the flag `-Zmiri-disable-isolation` to disable isolation;")), (None, format!("or pass `-Zmiri-isolation-error=warn` to configure Miri to return an error code from isolated operations (if supported for that operation) and continue with a warning")), ], - ExperimentalUb { url, help, history, .. } => { + StackedBorrowsUb { help, history, .. } => { + let url = "https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md"; msg.extend(help.clone()); let mut helps = vec![ - (None, format!("this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental")), - (None, format!("see {} for further information", url)), + (None, format!("this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental")), + (None, format!("see {url} for further information")), ]; match history { Some(TagHistory::Tagged {tag, created: (created_range, created_span), invalidated, protected }) => { diff --git a/src/stacked_borrows.rs b/src/stacked_borrows.rs index 88d1b1f105..c78741499c 100644 --- a/src/stacked_borrows.rs +++ b/src/stacked_borrows.rs @@ -250,14 +250,7 @@ pub fn err_sb_ub<'tcx>( help: Option, history: Option, ) -> InterpError<'tcx> { - err_machine_stop!(TerminationInfo::ExperimentalUb { - msg, - help, - url: format!( - "https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md" - ), - history - }) + err_machine_stop!(TerminationInfo::StackedBorrowsUb { msg, help, history }) } // # Stacked Borrows Core Begin From d9f8312d9ab86828ae7e1890630197d742b7ee3f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 11 Jun 2022 17:42:53 -0700 Subject: [PATCH 3/3] rustup --- rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-version b/rust-version index 85b2e88ac3..83da802cfc 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -09d52bc5d4260bac8b9a2ea8ac7a07c5c72906f1 +99930ac7f8cbb5d9b319b2e2e92794fd6f24f556