@@ -15,23 +15,27 @@ pub type MemoryExtra = RefCell<GlobalState>;
1515pub struct GlobalState {
1616 /// This is used as a map between the address of each allocation and its `AllocId`.
1717 /// It is always sorted
18- pub int_to_ptr_map : Vec < ( u64 , AllocId ) > ,
18+ int_to_ptr_map : Vec < ( u64 , AllocId ) > ,
1919 /// The base address for each allocation. We cannot put that into
2020 /// `AllocExtra` because function pointers also have a base address, and
2121 /// they do not have an `AllocExtra`.
2222 /// This is the inverse of `int_to_ptr_map`.
23- pub base_addr : FxHashMap < AllocId , u64 > ,
23+ base_addr : FxHashMap < AllocId , u64 > ,
2424 /// This is used as a memory address when a new pointer is casted to an integer. It
2525 /// is always larger than any address that was previously made part of a block.
26- pub next_base_addr : u64 ,
26+ next_base_addr : u64 ,
27+ /// Whether to enforce "strict provenance" rules. Enabling this means int2ptr casts return
28+ /// pointers with an invalid provenance, i.e., not valid for any memory access.
29+ strict_provenance : bool ,
2730}
2831
29- impl Default for GlobalState {
30- fn default ( ) -> Self {
32+ impl GlobalState {
33+ pub fn new ( config : & MiriConfig ) -> Self {
3134 GlobalState {
3235 int_to_ptr_map : Vec :: default ( ) ,
3336 base_addr : FxHashMap :: default ( ) ,
3437 next_base_addr : STACK_ADDR ,
38+ strict_provenance : config. strict_provenance ,
3539 }
3640 }
3741}
@@ -43,8 +47,12 @@ impl<'mir, 'tcx> GlobalState {
4347 ) -> Pointer < Option < Tag > > {
4448 trace ! ( "Casting 0x{:x} to a pointer" , addr) ;
4549 let global_state = memory. extra . intptrcast . borrow ( ) ;
46- let pos = global_state. int_to_ptr_map . binary_search_by_key ( & addr, |( addr, _) | * addr) ;
4750
51+ if global_state. strict_provenance {
52+ return Pointer :: new ( None , Size :: from_bytes ( addr) ) ;
53+ }
54+
55+ let pos = global_state. int_to_ptr_map . binary_search_by_key ( & addr, |( addr, _) | * addr) ;
4856 let alloc_id = match pos {
4957 Ok ( pos) => Some ( global_state. int_to_ptr_map [ pos] . 1 ) ,
5058 Err ( 0 ) => None ,
0 commit comments