Skip to content

Commit 88cf36a

Browse files
committed
Auto merge of #153617 - JonathanBrouwer:rollup-udG4Tzr, r=JonathanBrouwer
Rollup of 4 pull requests Successful merges: - rust-lang/rust#147834 (Always make tuple elements a coercion site) - rust-lang/rust#150446 (miri/const eval: support `MaybeDangling`) - rust-lang/rust#153053 (stop marking `deref_patterns` as an incomplete feature) - rust-lang/rust#153398 (fix ICE in `const_c_variadic` when passing ZSTs)
2 parents 3d9d00c + 096332b commit 88cf36a

9 files changed

Lines changed: 143 additions & 2 deletions

File tree

src/borrow_tracker/stacked_borrows/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
917917
RetagInfo { cause: self.retag_cause, in_field: self.in_field },
918918
)?;
919919
self.ecx.write_immediate(*val, place)?;
920+
920921
interp_ok(())
921922
}
922923
}
@@ -964,6 +965,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
964965
// even if field retagging is not enabled. *shrug*)
965966
self.walk_value(place)?;
966967
}
968+
ty::Adt(adt, _) if adt.is_maybe_dangling() => {
969+
// Skip traversing for everything inside of `MaybeDangling`
970+
}
967971
_ => {
968972
// Not a reference/pointer/box. Recurse.
969973
let in_field = mem::replace(&mut self.in_field, true); // remember and restore old value

src/borrow_tracker/tree_borrows/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
523523
// even if field retagging is not enabled. *shrug*)
524524
self.walk_value(place)?;
525525
}
526+
ty::Adt(adt, _) if adt.is_maybe_dangling() => {
527+
// Skip traversing for everything inside of `MaybeDangling`
528+
}
526529
_ => {
527530
// Not a reference/pointer/box. Recurse.
528531
self.walk_value(place)?;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Test that an unaligned `MaybeDangling<&u8>` is still detected as UB.
2+
//
3+
//@compile-flags: -Zmiri-disable-stacked-borrows
4+
#![feature(maybe_dangling)]
5+
6+
use std::mem::{MaybeDangling, transmute};
7+
8+
fn main() {
9+
let a = [1u16, 0u16];
10+
unsafe {
11+
let unaligned = MaybeDangling::new(a.as_ptr().byte_add(1));
12+
transmute::<MaybeDangling<*const u16>, MaybeDangling<&u16>>(unaligned)
13+
//~^ ERROR: Undefined Behavior: constructing invalid value: encountered an unaligned reference
14+
};
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: Undefined Behavior: constructing invalid value: encountered an unaligned reference (required ALIGN byte alignment but found ALIGN)
2+
--> tests/fail/unaligned_pointers/maybe_dangling_unalighed.rs:LL:CC
3+
|
4+
LL | transmute::<MaybeDangling<*const u16>, MaybeDangling<&u16>>(unaligned)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
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: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
11+
12+
error: aborting due to 1 previous error
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Test that a null `MaybeDangling<&u8>` is still detected as UB.
2+
//
3+
//@compile-flags: -Zmiri-disable-stacked-borrows
4+
#![feature(maybe_dangling)]
5+
6+
use std::mem::{MaybeDangling, transmute};
7+
use std::ptr::null;
8+
9+
fn main() {
10+
let null = MaybeDangling::new(null());
11+
unsafe { transmute::<MaybeDangling<*const u8>, MaybeDangling<&u8>>(null) };
12+
//~^ ERROR: Undefined Behavior: constructing invalid value: encountered a null reference
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: Undefined Behavior: constructing invalid value: encountered a null reference
2+
--> tests/fail/validity/maybe_dangling_null.rs:LL:CC
3+
|
4+
LL | unsafe { transmute::<MaybeDangling<*const u8>, MaybeDangling<&u8>>(null) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
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: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
11+
12+
error: aborting due to 1 previous error
13+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Check that `MaybeDangling` actually prevents UB when it wraps dangling
2+
// boxes and references
3+
//
4+
//@revisions: stack tree
5+
//@[tree]compile-flags: -Zmiri-tree-borrows
6+
#![feature(maybe_dangling)]
7+
8+
use std::mem::{self, MaybeDangling};
9+
use std::ptr::drop_in_place;
10+
11+
fn main() {
12+
boxy();
13+
reference();
14+
write_through_shared_ref();
15+
}
16+
17+
fn boxy() {
18+
let mut x = MaybeDangling::new(Box::new(1));
19+
20+
// make the box dangle
21+
unsafe { drop_in_place(x.as_mut()) };
22+
23+
// move the dangling box (without `MaybeDangling` this causes UB)
24+
let x: MaybeDangling<Box<u32>> = x;
25+
26+
mem::forget(x);
27+
}
28+
29+
fn reference() {
30+
let x = {
31+
let local = 0;
32+
33+
// erase the lifetime to make a dangling reference
34+
unsafe {
35+
mem::transmute::<MaybeDangling<&u32>, MaybeDangling<&u32>>(MaybeDangling::new(&local))
36+
}
37+
};
38+
39+
// move the dangling reference (without `MaybeDangling` this causes UB)
40+
let _x: MaybeDangling<&u32> = x;
41+
}
42+
43+
fn write_through_shared_ref() {
44+
// Under the current models, we do not forbid writing through
45+
// `MaybeDangling<&i32>`. That's not yet finally decided, but meanwhile
46+
// ensure we document this and notice when it changes.
47+
48+
unsafe {
49+
let mutref = &mut 0;
50+
write_through_shr(mem::transmute(mutref));
51+
}
52+
53+
fn write_through_shr(x: MaybeDangling<&i32>) {
54+
unsafe {
55+
let y: *mut i32 = mem::transmute(x);
56+
y.write(1);
57+
}
58+
}
59+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ ignore-target: windows # does not ignore ZST arguments
2+
//@ ignore-target: powerpc # does not ignore ZST arguments
3+
//@ ignore-target: s390x # does not ignore ZST arguments
4+
//@ ignore-target: sparc # does not ignore ZST arguments
5+
#![feature(c_variadic)]
6+
7+
// Some platforms ignore ZSTs, meaning that the argument is not passed, even though it is part
8+
// of the callee's ABI. Test that this doesn't trip any asserts.
9+
//
10+
// NOTE: this test only succeeds when the `()` argument uses `Passmode::Ignore`. For some targets,
11+
// notably msvc, such arguments are not ignored, which would cause UB when attempting to read the
12+
// second `i32` argument while the next item in the variable argument list is `()`.
13+
14+
fn main() {
15+
unsafe extern "C" fn variadic(mut ap: ...) {
16+
ap.arg::<i32>();
17+
ap.arg::<i32>();
18+
}
19+
20+
unsafe { variadic(0i32, (), 1i32) }
21+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
0..1: [ SharedReadWrite<TAG> ]
22
0..1: [ SharedReadWrite<TAG> ]
33
0..1: [ SharedReadWrite<TAG> ]
4-
0..1: [ SharedReadWrite<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> ]
5-
0..1: [ SharedReadWrite<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> SharedReadOnly<TAG> ]
4+
0..1: [ SharedReadWrite<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> ]
5+
0..1: [ SharedReadWrite<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> SharedReadOnly<TAG> ]
66
0..1: [ unknown-bottom(..<TAG>) ]

0 commit comments

Comments
 (0)