-
Notifications
You must be signed in to change notification settings - Fork 360
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
false positive with SB+TB: "Shifting" of a value #3774
Comments
Ultra small reproducer: fn main() {
let mut allocations = vec![];
// allocate
let alloc = b"hello".to_vec().into_boxed_slice();
let (ptr, len) = (alloc.as_ptr(), alloc.len());
allocations.push(alloc);
// deref
assert_eq!(
unsafe {
// UNSAFE(@ohsayan): allocation still in scope. ptr still valid
std::slice::from_raw_parts(ptr, len)
},
b"hello"
);
} Throws this:
|
Even smaller reproducer It appears that you just need to pass ownership to trigger this error. fn main() {
let mut _blob = None;
// allocate
let alloc = Vec::from("hello").into_boxed_slice();
let (ptr, len) = (alloc.as_ptr(), alloc.len());
_blob = Some(alloc);
// deref
assert_eq!(
unsafe {
// UNSAFE(@ohsayan): allocator still in scope. ptr still valid
std::slice::from_raw_parts(ptr, len)
},
b"hello"
);
} Edit 1: Further simplify |
Thanks for the report! This is currently expected behavior: moving a rust-lang/unsafe-code-guidelines#326 is the issue where we are tracking whether the rules should be changed (which would require compiler changes). But Miri does correctly implement the rules as they are today. |
@RalfJung thanks for the explainer. However, I think that this is a pretty significant detail that is easy to miss and should be highlighted in some noticeable place in the docs. Also, I think in the future relaxing these rules might be a good idea (I checked rust-lang/unsafe-code-guidelines#326) because right now we have to basically make a custom box impl for all of this (instead of a simple |
The docs call this out here: https://doc.rust-lang.org/stable/std/boxed/index.html#considerations-for-unsafe-code |
Once rust-lang/rust#118166 is implemented, using |
Consider the following code:
Edit: Small reproducer
Edit: Original code is below
Running
cargo miri run
with both tree and stacked borrows throws the following error;I'd have not expected to see this being reported as a TB/SB violation.
The text was updated successfully, but these errors were encountered: