-
Notifications
You must be signed in to change notification settings - Fork 13.4k
UnsafePinned: also include the effects of UnsafeCell #140638
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
Open
RalfJung
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
RalfJung:unsafe-pinned-shared-aliased
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! FIXME: This test should pass! However, `async fn` does not yet use `UnsafePinned`. | ||
//! This is a regression test for <https://github.com/rust-lang/rust/issues/137750>: | ||
//! `UnsafePinned` must include the effects of `UnsafeCell`. | ||
//@revisions: stack tree | ||
//@[tree]compile-flags: -Zmiri-tree-borrows | ||
//@normalize-stderr-test: "\[0x[a-fx\d.]+\]" -> "[OFFSET]" | ||
|
||
use core::future::Future; | ||
use core::pin::{Pin, pin}; | ||
use core::task::{Context, Poll, Waker}; | ||
|
||
fn main() { | ||
let mut f = pin!(async move { | ||
let x = &mut 0u8; | ||
core::future::poll_fn(move |_| { | ||
*x = 1; //~ERROR: write access | ||
Poll::<()>::Pending | ||
}) | ||
.await | ||
}); | ||
let mut cx = Context::from_waker(&Waker::noop()); | ||
assert_eq!(f.as_mut().poll(&mut cx), Poll::Pending); | ||
let _: Pin<&_> = f.as_ref(); // Or: `f.as_mut().into_ref()`. | ||
assert_eq!(f.as_mut().poll(&mut cx), Poll::Pending); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/tools/miri/tests/fail/async-shared-mutable.stack.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
error: Undefined Behavior: attempting a write access using <TAG> at ALLOC[OFFSET], but that tag does not exist in the borrow stack for this location | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | *x = 1; | ||
| ^^^^^^ | ||
| | | ||
| attempting a write access using <TAG> at ALLOC[OFFSET], but that tag does not exist in the borrow stack for this location | ||
| this error occurs as part of an access at ALLOC[OFFSET] | ||
| | ||
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental | ||
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information | ||
help: <TAG> was created by a Unique retag at offsets [OFFSET] | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | / core::future::poll_fn(move |_| { | ||
LL | | *x = 1; | ||
LL | | Poll::<()>::Pending | ||
LL | | }) | ||
LL | | .await | ||
| |______________^ | ||
help: <TAG> was later invalidated at offsets [OFFSET] by a SharedReadOnly retag | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | let _: Pin<&_> = f.as_ref(); // Or: `f.as_mut().into_ref()`. | ||
| ^^^^^^^^^^ | ||
= note: BACKTRACE (of the first span): | ||
= note: inside closure at tests/fail/async-shared-mutable.rs:LL:CC | ||
= note: inside `<std::future::PollFn<{closure@tests/fail/async-shared-mutable.rs:LL:CC}> as std::future::Future>::poll` at RUSTLIB/core/src/future/poll_fn.rs:LL:CC | ||
note: inside closure | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | .await | ||
| ^^^^^ | ||
note: inside `main` | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | assert_eq!(f.as_mut().poll(&mut cx), Poll::Pending); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
47 changes: 47 additions & 0 deletions
47
src/tools/miri/tests/fail/async-shared-mutable.tree.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
error: Undefined Behavior: write access through <TAG> at ALLOC[OFFSET] is forbidden | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | *x = 1; | ||
| ^^^^^^ write access through <TAG> at ALLOC[OFFSET] is forbidden | ||
| | ||
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental | ||
= help: the accessed tag <TAG> has state Frozen which forbids this child write access | ||
help: the accessed tag <TAG> was created here, in the initial state Reserved | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | / core::future::poll_fn(move |_| { | ||
LL | | *x = 1; | ||
LL | | Poll::<()>::Pending | ||
LL | | }) | ||
LL | | .await | ||
| |______________^ | ||
help: the accessed tag <TAG> later transitioned to Active due to a child write access at offsets [OFFSET] | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | *x = 1; | ||
| ^^^^^^ | ||
= help: this transition corresponds to the first write to a 2-phase borrowed mutable reference | ||
help: the accessed tag <TAG> later transitioned to Frozen due to a reborrow (acting as a foreign read access) at offsets [OFFSET] | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | let _: Pin<&_> = f.as_ref(); // Or: `f.as_mut().into_ref()`. | ||
| ^^^^^^^^^^ | ||
= help: this transition corresponds to a loss of write permissions | ||
= note: BACKTRACE (of the first span): | ||
= note: inside closure at tests/fail/async-shared-mutable.rs:LL:CC | ||
= note: inside `<std::future::PollFn<{closure@tests/fail/async-shared-mutable.rs:LL:CC}> as std::future::Future>::poll` at RUSTLIB/core/src/future/poll_fn.rs:LL:CC | ||
note: inside closure | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | .await | ||
| ^^^^^ | ||
note: inside `main` | ||
--> tests/fail/async-shared-mutable.rs:LL:CC | ||
| | ||
LL | assert_eq!(f.as_mut().poll(&mut cx), Poll::Pending); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//@revisions: stack tree | ||
//@[tree]compile-flags: -Zmiri-tree-borrows | ||
#![feature(unsafe_pinned)] | ||
|
||
use std::pin::UnsafePinned; | ||
|
||
fn mutate(x: &UnsafePinned<i32>) { | ||
let ptr = x as *const _ as *mut i32; | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsafe { ptr.write(42) }; | ||
} | ||
|
||
fn main() { | ||
let x = UnsafePinned::new(0); | ||
mutate(&x); | ||
assert_eq!(x.into_inner(), 42); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.