-
Notifications
You must be signed in to change notification settings - Fork 386
Implement intptrcast methods #779
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
7 commits
Select commit
Hold shift + click to select a range
fd3a291
Implement intptrcast methods
pvdrz e574470
Duplicate compile-fail tests for intptrcast
pvdrz dd732e5
Force intptrcast for binary operations
pvdrz 2861ceb
Rename new fields and move rng to MemoryExtra
pvdrz 84cfbb0
Reorganize MemoryExtra and AllocExtra structures
pvdrz 792d665
Fix merge conflicts
pvdrz 7fbf8e5
Fix alignment of base addresses
pvdrz 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,100 @@ | ||
use std::cell::{Cell, RefCell}; | ||
|
||
use rustc::mir::interpret::{AllocId, Pointer, InterpResult}; | ||
use rustc_mir::interpret::Memory; | ||
use rustc_target::abi::Size; | ||
|
||
use crate::stacked_borrows::Tag; | ||
use crate::Evaluator; | ||
|
||
pub type MemoryExtra = RefCell<GlobalState>; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct AllocExtra { | ||
base_addr: Cell<Option<u64>> | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct GlobalState { | ||
/// This is used as a map between the address of each allocation and its `AllocId`. | ||
/// It is always sorted | ||
pub int_to_ptr_map: Vec<(u64, AllocId)>, | ||
/// This is used as a memory address when a new pointer is casted to an integer. It | ||
/// is always larger than any address that was previously made part of a block. | ||
pub next_base_addr: u64, | ||
} | ||
|
||
impl Default for GlobalState { | ||
// FIXME: Query the page size in the future | ||
fn default() -> Self { | ||
GlobalState { | ||
int_to_ptr_map: Vec::default(), | ||
next_base_addr: 2u64.pow(16) | ||
} | ||
} | ||
} | ||
|
||
impl<'mir, 'tcx> GlobalState { | ||
pub fn int_to_ptr( | ||
int: u64, | ||
memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>, | ||
) -> InterpResult<'tcx, Pointer<Tag>> { | ||
let global_state = memory.extra.intptrcast.borrow(); | ||
|
||
match global_state.int_to_ptr_map.binary_search_by_key(&int, |(addr, _)| *addr) { | ||
Ok(pos) => { | ||
let (_, alloc_id) = global_state.int_to_ptr_map[pos]; | ||
// `int` is equal to the starting address for an allocation, the offset should be | ||
// zero. The pointer is untagged because it was created from a cast | ||
Ok(Pointer::new_with_tag(alloc_id, Size::from_bytes(0), Tag::Untagged)) | ||
}, | ||
Err(0) => err!(DanglingPointerDeref), | ||
Err(pos) => { | ||
// This is the largest of the adresses smaller than `int`, | ||
// i.e. the greatest lower bound (glb) | ||
let (glb, alloc_id) = global_state.int_to_ptr_map[pos - 1]; | ||
// This never overflows because `int >= glb` | ||
let offset = int - glb; | ||
// If the offset exceeds the size of the allocation, this access is illegal | ||
if offset <= memory.get(alloc_id)?.bytes.len() as u64 { | ||
// This pointer is untagged because it was created from a cast | ||
Ok(Pointer::new_with_tag(alloc_id, Size::from_bytes(offset), Tag::Untagged)) | ||
} else { | ||
err!(DanglingPointerDeref) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn ptr_to_int( | ||
ptr: Pointer<Tag>, | ||
memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>, | ||
) -> InterpResult<'tcx, u64> { | ||
let mut global_state = memory.extra.intptrcast.borrow_mut(); | ||
|
||
let alloc = memory.get(ptr.alloc_id)?; | ||
|
||
let base_addr = match alloc.extra.intptrcast.base_addr.get() { | ||
Some(base_addr) => base_addr, | ||
None => { | ||
// This allocation does not have a base address yet, pick one. | ||
let base_addr = Self::align_addr(global_state.next_base_addr, alloc.align.bytes()); | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
global_state.next_base_addr = base_addr + alloc.bytes.len() as u64; | ||
alloc.extra.intptrcast.base_addr.set(Some(base_addr)); | ||
// Given that `next_base_addr` increases in each allocation, pushing the | ||
// corresponding tuple keeps `int_to_ptr_map` sorted | ||
global_state.int_to_ptr_map.push((base_addr, ptr.alloc_id)); | ||
|
||
base_addr | ||
} | ||
}; | ||
|
||
Ok(base_addr + ptr.offset.bytes()) | ||
} | ||
|
||
/// Shifts `addr` to make it aligned with `align` by rounding `addr` to the smallest multiple | ||
/// of `align` that is strictly larger to `addr` | ||
fn align_addr(addr: u64, align: u64) -> u64 { | ||
addr + align - addr % align | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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,51 @@ | ||
use rand::rngs::StdRng; | ||
|
||
use rustc_mir::interpret::{Pointer, Allocation, AllocationExtra, InterpResult}; | ||
use rustc_target::abi::Size; | ||
|
||
use crate::{stacked_borrows, intptrcast}; | ||
use crate::stacked_borrows::Tag; | ||
|
||
#[derive(Default, Clone, Debug)] | ||
pub struct MemoryExtra { | ||
pub stacked_borrows: stacked_borrows::MemoryExtra, | ||
pub intptrcast: intptrcast::MemoryExtra, | ||
/// The random number generator to use if Miri is running in non-deterministic mode and to | ||
/// enable intptrcast | ||
pub(crate) rng: Option<StdRng> | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct AllocExtra { | ||
pub stacked_borrows: stacked_borrows::AllocExtra, | ||
pub intptrcast: intptrcast::AllocExtra, | ||
} | ||
|
||
impl AllocationExtra<Tag> for AllocExtra { | ||
#[inline(always)] | ||
fn memory_read<'tcx>( | ||
alloc: &Allocation<Tag, AllocExtra>, | ||
ptr: Pointer<Tag>, | ||
size: Size, | ||
) -> InterpResult<'tcx> { | ||
alloc.extra.stacked_borrows.memory_read(ptr, size) | ||
} | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#[inline(always)] | ||
fn memory_written<'tcx>( | ||
alloc: &mut Allocation<Tag, AllocExtra>, | ||
ptr: Pointer<Tag>, | ||
size: Size, | ||
) -> InterpResult<'tcx> { | ||
alloc.extra.stacked_borrows.memory_written(ptr, size) | ||
} | ||
|
||
#[inline(always)] | ||
fn memory_deallocated<'tcx>( | ||
alloc: &mut Allocation<Tag, AllocExtra>, | ||
ptr: Pointer<Tag>, | ||
size: Size, | ||
) -> InterpResult<'tcx> { | ||
alloc.extra.stacked_borrows.memory_deallocated(ptr, size) | ||
} | ||
} |
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
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.