-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Allow testing pointers for inboundedness while forbidding dangling pointers #56985
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
Merged
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,7 +262,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
Scalar::Ptr(ptr) => { | ||
// check this is not NULL -- which we can ensure only if this is in-bounds | ||
// of some (potentially dead) allocation. | ||
let align = self.check_bounds_ptr_maybe_dead(ptr)?; | ||
let align = self.check_bounds_ptr(ptr, InboundsCheck::MaybeDead)?; | ||
(ptr.offset.bytes(), align) | ||
} | ||
Scalar::Bits { bits, size } => { | ||
|
@@ -297,17 +297,15 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
/// Check if the pointer is "in-bounds". Notice that a pointer pointing at the end | ||
/// of an allocation (i.e., at the first *inaccessible* location) *is* considered | ||
/// in-bounds! This follows C's/LLVM's rules. | ||
/// This function also works for deallocated allocations. | ||
/// Use `.get(ptr.alloc_id)?.check_bounds_ptr(ptr)` if you want to force the allocation | ||
/// to still be live. | ||
/// If you want to check bounds before doing a memory access, better first obtain | ||
/// an `Allocation` and call `check_bounds`. | ||
pub fn check_bounds_ptr_maybe_dead( | ||
pub fn check_bounds_ptr( | ||
&self, | ||
ptr: Pointer<M::PointerTag>, | ||
liveness: InboundsCheck, | ||
) -> EvalResult<'tcx, Align> { | ||
let (allocation_size, align) = self.get_size_and_align(ptr.alloc_id); | ||
ptr.check_in_alloc(allocation_size, InboundsCheck::MaybeDead)?; | ||
let (allocation_size, align) = self.get_size_and_align(ptr.alloc_id, liveness)?; | ||
ptr.check_in_alloc(allocation_size, liveness)?; | ||
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. Is |
||
Ok(align) | ||
} | ||
} | ||
|
@@ -429,27 +427,37 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
} | ||
} | ||
|
||
pub fn get_size_and_align(&self, id: AllocId) -> (Size, Align) { | ||
/// Obtain the size and alignment of an allocation, even if that allocation has been deallocated | ||
/// | ||
/// If `liveness` is `InboundsCheck::Dead`, this function always returns `Ok` | ||
pub fn get_size_and_align( | ||
&self, | ||
id: AllocId, | ||
liveness: InboundsCheck, | ||
) -> EvalResult<'static, (Size, Align)> { | ||
if let Ok(alloc) = self.get(id) { | ||
return (Size::from_bytes(alloc.bytes.len() as u64), alloc.align); | ||
return Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align)); | ||
} | ||
// Could also be a fn ptr or extern static | ||
match self.tcx.alloc_map.lock().get(id) { | ||
Some(AllocKind::Function(..)) => (Size::ZERO, Align::from_bytes(1).unwrap()), | ||
Some(AllocKind::Function(..)) => Ok((Size::ZERO, Align::from_bytes(1).unwrap())), | ||
Some(AllocKind::Static(did)) => { | ||
// The only way `get` couldn't have worked here is if this is an extern static | ||
assert!(self.tcx.is_foreign_item(did)); | ||
// Use size and align of the type | ||
let ty = self.tcx.type_of(did); | ||
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap(); | ||
(layout.size, layout.align.abi) | ||
} | ||
_ => { | ||
// Must be a deallocated pointer | ||
*self.dead_alloc_map.get(&id).expect( | ||
"allocation missing in dead_alloc_map" | ||
) | ||
Ok((layout.size, layout.align.abi)) | ||
} | ||
_ => match liveness { | ||
InboundsCheck::MaybeDead => { | ||
// Must be a deallocated pointer | ||
Ok(*self.dead_alloc_map.get(&id).expect( | ||
"allocation missing in dead_alloc_map" | ||
)) | ||
}, | ||
InboundsCheck::Live => err!(DanglingPointerDeref), | ||
}, | ||
} | ||
} | ||
|
||
|
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
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.