Skip to content

Commit 844ada8

Browse files
committed
Auto merge of #2195 - RalfJung:vtable-validation, r=RalfJung
test for Stacked Borrows error during vtable validation Fixes #2123 Needs rust-lang/rust#97761
2 parents 96ee9a0 + d9f8312 commit 844ada8

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
09d52bc5d4260bac8b9a2ea8ac7a07c5c72906f1
1+
99930ac7f8cbb5d9b319b2e2e92794fd6f24f556

src/diagnostics.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ pub enum TerminationInfo {
1616
Exit(i64),
1717
Abort(String),
1818
UnsupportedInIsolation(String),
19-
ExperimentalUb {
19+
StackedBorrowsUb {
2020
msg: String,
2121
help: Option<String>,
22-
url: String,
2322
history: Option<TagHistory>,
2423
},
2524
Deadlock,
@@ -43,7 +42,7 @@ impl fmt::Display for TerminationInfo {
4342
Exit(code) => write!(f, "the evaluated program completed with exit code {}", code),
4443
Abort(msg) => write!(f, "{}", msg),
4544
UnsupportedInIsolation(msg) => write!(f, "{}", msg),
46-
ExperimentalUb { msg, .. } => write!(f, "{}", msg),
45+
StackedBorrowsUb { msg, .. } => write!(f, "{}", msg),
4746
Deadlock => write!(f, "the evaluated program deadlocked"),
4847
MultipleSymbolDefinitions { link_name, .. } =>
4948
write!(f, "multiple definitions of symbol `{}`", link_name),
@@ -146,7 +145,7 @@ pub fn report_error<'tcx, 'mir>(
146145
Exit(code) => return Some(*code),
147146
Abort(_) => Some("abnormal termination"),
148147
UnsupportedInIsolation(_) => Some("unsupported operation"),
149-
ExperimentalUb { .. } => Some("Undefined Behavior"),
148+
StackedBorrowsUb { .. } => Some("Undefined Behavior"),
150149
Deadlock => Some("deadlock"),
151150
MultipleSymbolDefinitions { .. } | SymbolShimClashing { .. } => None,
152151
};
@@ -157,11 +156,12 @@ pub fn report_error<'tcx, 'mir>(
157156
(None, format!("pass the flag `-Zmiri-disable-isolation` to disable isolation;")),
158157
(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")),
159158
],
160-
ExperimentalUb { url, help, history, .. } => {
159+
StackedBorrowsUb { help, history, .. } => {
160+
let url = "https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md";
161161
msg.extend(help.clone());
162162
let mut helps = vec![
163-
(None, format!("this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental")),
164-
(None, format!("see {} for further information", url)),
163+
(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")),
164+
(None, format!("see {url} for further information")),
165165
];
166166
match history {
167167
Some(TagHistory::Tagged {tag, created: (created_range, created_span), invalidated, protected }) => {

src/stacked_borrows.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,7 @@ pub fn err_sb_ub<'tcx>(
250250
help: Option<String>,
251251
history: Option<TagHistory>,
252252
) -> InterpError<'tcx> {
253-
err_machine_stop!(TerminationInfo::ExperimentalUb {
254-
msg,
255-
help,
256-
url: format!(
257-
"https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md"
258-
),
259-
history
260-
})
253+
err_machine_stop!(TerminationInfo::StackedBorrowsUb { msg, help, history })
261254
}
262255

263256
// # Stacked Borrows Core Begin

tests/fail/stacked_borrows/vtable.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// error-pattern: vtable pointer does not have permission
2+
#![feature(ptr_metadata)]
3+
4+
trait Foo {}
5+
6+
impl Foo for u32 {}
7+
8+
fn uwu(thin: *const (), meta: &'static ()) -> *const dyn Foo {
9+
core::ptr::from_raw_parts(thin, unsafe { core::mem::transmute(meta) })
10+
}
11+
12+
fn main() {
13+
unsafe {
14+
let orig = 1_u32;
15+
let x = &orig as &dyn Foo;
16+
let (ptr, meta) = (x as *const dyn Foo).to_raw_parts();
17+
let _ = uwu(ptr, core::mem::transmute(meta));
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: Undefined Behavior: type validation failed: encountered vtable pointer does not have permission to read drop function pointer
2+
--> RUSTLIB/core/src/ptr/metadata.rs:LL:CC
3+
|
4+
LL | unsafe { PtrRepr { components: PtrComponents { data_address, metadata } }.const_ptr }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered vtable pointer does not have permission to read drop function pointer
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
10+
= note: inside `std::ptr::from_raw_parts::<dyn Foo>` at RUSTLIB/core/src/ptr/metadata.rs:LL:CC
11+
note: inside `uwu` at $DIR/vtable.rs:LL:CC
12+
--> $DIR/vtable.rs:LL:CC
13+
|
14+
LL | core::ptr::from_raw_parts(thin, unsafe { core::mem::transmute(meta) })
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
note: inside `main` at $DIR/vtable.rs:LL:CC
17+
--> $DIR/vtable.rs:LL:CC
18+
|
19+
LL | let _ = uwu(ptr, core::mem::transmute(meta));
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
22+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
23+
24+
error: aborting due to previous error
25+

0 commit comments

Comments
 (0)