-
Notifications
You must be signed in to change notification settings - Fork 9
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
Example 2 in Unsafe3 is not UB by Miri with tree-borrows #11
Comments
should probably specify that all the UB examples assume stacked borrows |
I am allergic to trees, I can't borrow this issue. |
@zjp-CN do you have an idea of why this is allowed with tree borrows? |
I'm not familiar with aliasing rules. But https://perso.crans.org/vanille/treebor/range.html says
Example2 seems able to be reduced to rust-lang/unsafe-code-guidelines#134 // UB under stack borrows
//+ TB: NOT UB (Delayed initialization)
//+ Common pattern, it would be PREFERABLY NOT UB.
let val = [1u8, 2];
// --- val: [Active, Active]
let ptr = &val[0] as *const u8;
// --- val: [Active, Active]
// |--- ptr: [Frozen, Frozen?]
let _val = unsafe { *ptr.add(1) };
// --- val: [Active, Active]
// |--- ptr: [Frozen, Frozen] exactly what // v.get_unchecked_mut(n)
unsafe impl<T> SliceIndex<[T]> for usize {
#[inline]
unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
assert_unsafe_precondition!(
check_language_ub,
"slice::get_unchecked requires that the index is within the slice",
(this: usize = self, len: usize = slice.len()) => this < len
);
// SAFETY: the caller guarantees that `slice` is not dangling, so it
// cannot be longer than `isize::MAX`. They also guarantee that
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
// so the call to `add` is safe.
unsafe {
// Use intrinsics::assume instead of hint::assert_unchecked so that we don't check the
// precondition of this function twice.
crate::intrinsics::assume(self < slice.len());
slice.as_ptr().add(self)
}
}
#[inline]
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
assert_unsafe_precondition!(
check_library_ub,
"slice::get_unchecked_mut requires that the index is within the slice",
(this: usize = self, len: usize = slice.len()) => this < len
);
// SAFETY: see comments for `get_unchecked` above.
unsafe { slice.as_mut_ptr().add(self) }
}
} I think rust-quiz can include some code examples w.r.t stack & tree borrows. |
The text was updated successfully, but these errors were encountered: