Comments (4)
I am allergic to trees, I can't borrow this issue.
from rust-quiz.
should probably specify that all the UB examples assume stacked borrows
from rust-quiz.
@zjp-CN do you have an idea of why this is allowed with tree borrows?
from rust-quiz.
I'm not familiar with aliasing rules. But https://perso.crans.org/vanille/treebor/range.html says
The first step to this is to observe that the tree of pointers is the same for an entire allocation since the parent-child relationship is global, and only the permissions have to be managed on a per-location basis. When an access is performed, pointers have their permissions updated only on the affected locations, so pointers performing accesses on disjoint ranges of memory do not cause aliasing UB.
...
All of these involve using a pointer out of the bounds of its reborrow (but still within the bounds of its allocation), so in order to allow these Tree Borrows must have some tolerance with regards to using a pointer outside of the range it was reborrowed for. Tree Borrows’ solution is to not reborrow for the locations outside the range, but to maintain enough information so that whenever the location is accessed through a pointer for which it is out of range it can be initialized with a delay and receive the permissions it would have had if it had been reborrowed from the start.
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 get_unchecked_mut
in question does
// 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.
from rust-quiz.
Related Issues (17)
- Code is too wide for mobile
- auto generate the MD files from the examples?
- Rename borrowck to borrow checker
- solver 2 and 5 have no explanation
- solver 7 is named type system 7 in the page
- misc, solver, and borrowck should have notes like unsafe
- the intro page should actually be explained
- Assign difficulties
- question idea: typing of diverging blocks.
- question idea: type/value amgiguity
- question idea: legacy const generics
- Get names in order
- Make unsafe 3 worse HOT 1
- Make unsafe 1 worse
- Struct update syntax for unit and tuple structs
- question idea: rustdoc re-export: HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-quiz.