Skip to content

Commit 873ca6e

Browse files
author
The Miri Cronjob Bot
committed
Merge from rustc
2 parents 3e9500b + 163552b commit 873ca6e

8 files changed

+167
-2
lines changed

tests/fail/dyn-call-trait-mismatch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Validation stops this too early.
2-
//@compile-flags: -Zmiri-disable-validation
1+
// Validation and SB stop this too early.
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

44
trait T1 {
55
#[allow(dead_code)]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Make sure we find these even with many checks disabled.
2+
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation
3+
#![feature(core_intrinsics)]
4+
#![feature(custom_mir)]
5+
6+
use std::intrinsics::mir::*;
7+
8+
#[custom_mir(dialect = "runtime", phase = "optimized")]
9+
fn cast(ptr: *const char) -> u32 {
10+
mir! {
11+
{
12+
RET = *ptr as u32; //~ERROR: interpreting an invalid 32-bit value as a char
13+
Return()
14+
}
15+
}
16+
}
17+
18+
pub fn main() {
19+
let v = u32::MAX;
20+
cast(&v as *const u32 as *const char);
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: interpreting an invalid 32-bit value as a char: $HEX
2+
--> $DIR/invalid_char_cast.rs:LL:CC
3+
|
4+
LL | RET = *ptr as u32;
5+
| ^^^^^^^^^^^^^^^^^ interpreting an invalid 32-bit value as a char: $HEX
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+
= note: BACKTRACE:
10+
= note: inside `cast` at $DIR/invalid_char_cast.rs:LL:CC
11+
note: inside `main`
12+
--> $DIR/invalid_char_cast.rs:LL:CC
13+
|
14+
LL | cast(&v as *const u32 as *const char);
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
17+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
18+
19+
error: aborting due to 1 previous error
20+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Make sure we find these even with many checks disabled.
2+
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation
3+
#![feature(core_intrinsics)]
4+
#![feature(custom_mir)]
5+
6+
use std::intrinsics::mir::*;
7+
8+
#[custom_mir(dialect = "runtime", phase = "optimized")]
9+
fn switch_int(ptr: *const char) {
10+
mir! {
11+
{
12+
match *ptr { //~ERROR: interpreting an invalid 32-bit value as a char
13+
'0' => ret,
14+
_ => ret,
15+
}
16+
}
17+
ret = {
18+
Return()
19+
}
20+
}
21+
}
22+
23+
pub fn main() {
24+
let v = u32::MAX;
25+
switch_int(&v as *const u32 as *const char);
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: Undefined Behavior: interpreting an invalid 32-bit value as a char: $HEX
2+
--> $DIR/invalid_char_match.rs:LL:CC
3+
|
4+
LL | / match *ptr {
5+
LL | | '0' => ret,
6+
LL | | _ => ret,
7+
LL | | }
8+
| |_____________^ interpreting an invalid 32-bit value as a char: $HEX
9+
|
10+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
11+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
12+
= note: BACKTRACE:
13+
= note: inside `switch_int` at $DIR/invalid_char_match.rs:LL:CC
14+
note: inside `main`
15+
--> $DIR/invalid_char_match.rs:LL:CC
16+
|
17+
LL | switch_int(&v as *const u32 as *const char);
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
20+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
21+
22+
error: aborting due to 1 previous error
23+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Make sure we find these even with many checks disabled.
2+
//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation
3+
4+
#[derive(Copy, Clone)]
5+
#[allow(unused)]
6+
enum E {A, B, C }
7+
8+
fn cast(ptr: *const E) { unsafe {
9+
let _val = *ptr as u32; //~ERROR: enum value has invalid tag
10+
}}
11+
12+
pub fn main() {
13+
let v = u32::MAX;
14+
cast(&v as *const u32 as *const E);
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: enum value has invalid tag: 0xff
2+
--> $DIR/invalid_enum_cast.rs:LL:CC
3+
|
4+
LL | let _val = *ptr as u32;
5+
| ^^^^^^^^^^^ enum value has invalid tag: 0xff
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+
= note: BACKTRACE:
10+
= note: inside `cast` at $DIR/invalid_enum_cast.rs:LL:CC
11+
note: inside `main`
12+
--> $DIR/invalid_enum_cast.rs:LL:CC
13+
|
14+
LL | cast(&v as *const u32 as *const E);
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
17+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
18+
19+
error: aborting due to 1 previous error
20+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![feature(ptr_metadata)]
2+
// This test is the result of minimizing the `emplacable` crate to reproduce
3+
// <https://github.com/rust-lang/miri/issues/3541>.
4+
5+
use std::{ops::FnMut, ptr::Pointee};
6+
7+
pub type EmplacerFn<'a, T> = dyn for<'b> FnMut(<T as Pointee>::Metadata) + 'a;
8+
9+
#[repr(transparent)]
10+
pub struct Emplacer<'a, T>(EmplacerFn<'a, T>)
11+
where
12+
T: ?Sized;
13+
14+
impl<'a, T> Emplacer<'a, T>
15+
where
16+
T: ?Sized,
17+
{
18+
pub unsafe fn from_fn<'b>(emplacer_fn: &'b mut EmplacerFn<'a, T>) -> &'b mut Self {
19+
// This used to trigger:
20+
// constructing invalid value: wrong trait in wide pointer vtable: expected
21+
// `std::ops::FnMut(<[std::boxed::Box<i32>] as std::ptr::Pointee>::Metadata)`, but encountered
22+
// `std::ops::FnMut<(usize,)>`.
23+
unsafe { &mut *((emplacer_fn as *mut EmplacerFn<'a, T>) as *mut Self) }
24+
}
25+
}
26+
27+
pub fn box_new_with<T>()
28+
where
29+
T: ?Sized,
30+
{
31+
let emplacer_closure = &mut |_meta| {
32+
unreachable!();
33+
};
34+
35+
unsafe { Emplacer::<T>::from_fn(emplacer_closure) };
36+
}
37+
38+
fn main() {
39+
box_new_with::<[Box<i32>]>();
40+
}

0 commit comments

Comments
 (0)