-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
miri/const eval: support MaybeDangling
#150446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tree Borrows will need the same patch. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -917,6 +917,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { | |
| RetagInfo { cause: self.retag_cause, in_field: self.in_field }, | ||
| )?; | ||
| self.ecx.write_immediate(*val, place)?; | ||
|
|
||
| interp_ok(()) | ||
| } | ||
| } | ||
|
|
@@ -964,6 +965,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { | |
| // even if field retagging is not enabled. *shrug*) | ||
| self.walk_value(place)?; | ||
| } | ||
| ty::Adt(adt, _) if adt.is_maybe_dangling() => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can just entirely skip traversing the references inside |
||
| // Skip traversing for everything inside of `MaybeDangling` | ||
| } | ||
| _ => { | ||
| // Not a reference/pointer/box. Recurse. | ||
| let in_field = mem::replace(&mut self.in_field, true); // remember and restore old value | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #![feature(maybe_dangling)] | ||
| use std::mem::{transmute, MaybeDangling}; | ||
| use std::ptr::null; | ||
|
|
||
| fn main() { | ||
| unsafe { transmute::<MaybeDangling<*const u8>, MaybeDangling<&u8>>(MaybeDangling::new(null())) }; | ||
| //~^ ERROR: Undefined Behavior: constructing invalid value: encountered a null reference | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| error: Undefined Behavior: constructing invalid value: encountered a null reference | ||
| --> tests/fail/both_borrows/maybe_dangling_null.rs:LL:CC | ||
| | | ||
| LL | unsafe { transmute::<MaybeDangling<*const u8>, MaybeDangling<&u8>>(MaybeDangling::new(null())) }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here | ||
| | | ||
| = 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: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #![feature(maybe_dangling)] | ||
| use std::mem::{transmute, MaybeDangling}; | ||
|
|
||
| fn main() { | ||
| let a = [1u16, 0u16]; | ||
| unsafe { transmute::<MaybeDangling<*const u16>, MaybeDangling<&u16>>(MaybeDangling::new(a.as_ptr().byte_add(1))) }; | ||
| //~^ ERROR: Undefined Behavior: constructing invalid value: encountered an unaligned reference | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| error: Undefined Behavior: constructing invalid value: encountered an unaligned reference (required ALIGN byte alignment but found ALIGN) | ||
| --> tests/fail/both_borrows/maybe_dangling_unalighed.rs:LL:CC | ||
| | | ||
| LL | unsafe { transmute::<MaybeDangling<*const u16>, MaybeDangling<&u16>>(MaybeDangling::new(a.as_ptr().byte_add(1))) }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here | ||
| | | ||
| = 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: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Check that `MaybeDangling` actually prevents UB when it wraps dangling | ||
| // boxes and references | ||
| #![feature(maybe_dangling)] | ||
| use std::mem::{self, MaybeDangling}; | ||
| use std::ptr::drop_in_place; | ||
|
|
||
| fn main() { | ||
| boxy(); | ||
| reference(); | ||
| } | ||
|
|
||
| fn boxy() { | ||
| let mut x = MaybeDangling::new(Box::new(1)); | ||
|
|
||
| // make the box dangle | ||
| unsafe { drop_in_place(x.as_mut()) }; | ||
|
|
||
| // move the dangling box (without `MaybeDangling` this causes UB) | ||
| let x: MaybeDangling<Box<u32>> = x; | ||
|
|
||
| mem::forget(x); | ||
| } | ||
|
|
||
| fn reference() { | ||
| let x = { | ||
| let local = 0; | ||
|
|
||
| // erase the lifetime to make a dangling reference | ||
| unsafe { mem::transmute::<MaybeDangling<&u32>, MaybeDangling<&u32>>(MaybeDangling::new(&local)) } | ||
| }; | ||
|
|
||
| // move the dangling reference (without `MaybeDangling` this causes UB) | ||
| let _x: MaybeDangling<&u32> = x; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| 0..1: [ SharedReadWrite<TAG> ] | ||
| 0..1: [ SharedReadWrite<TAG> ] | ||
| 0..1: [ SharedReadWrite<TAG> ] | ||
| 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> ] | ||
| 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> ] | ||
| 0..1: [ SharedReadWrite<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> ] | ||
| 0..1: [ SharedReadWrite<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> SharedReadOnly<TAG> ] | ||
| 0..1: [ unknown-bottom(..<TAG>) ] |
Uh oh!
There was an error while loading. Please reload this page.