@@ -7,7 +7,7 @@ use super::{
77
88use crate :: ty:: layout:: { Size , Align } ;
99use syntax:: ast:: Mutability ;
10- use std:: iter;
10+ use std:: { iter, fmt :: { self , Display } } ;
1111use crate :: mir;
1212use std:: ops:: { Deref , DerefMut } ;
1313use rustc_data_structures:: sorted_map:: SortedMap ;
@@ -22,6 +22,28 @@ pub enum InboundsCheck {
2222 MaybeDead ,
2323}
2424
25+ /// Used by `check_in_alloc` to indicate context of check
26+ #[ derive( Debug , Copy , Clone , RustcEncodable , RustcDecodable , HashStable ) ]
27+ pub enum CheckInAllocMsg {
28+ MemoryAccessTest ,
29+ NullPointerTest ,
30+ PointerArithmeticTest ,
31+ InboundsTest ,
32+ }
33+
34+ impl Display for CheckInAllocMsg {
35+ /// When this is printed as an error the context looks like this
36+ /// "{test name} failed: pointer must be in-bounds at offset..."
37+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
38+ write ! ( f, "{}" , match * self {
39+ CheckInAllocMsg :: MemoryAccessTest => "Memory access" ,
40+ CheckInAllocMsg :: NullPointerTest => "Null pointer test" ,
41+ CheckInAllocMsg :: PointerArithmeticTest => "Pointer arithmetic" ,
42+ CheckInAllocMsg :: InboundsTest => "Inbounds test" ,
43+ } )
44+ }
45+ }
46+
2547#[ derive( Clone , Debug , Eq , PartialEq , PartialOrd , Ord , Hash , RustcEncodable , RustcDecodable ) ]
2648pub struct Allocation < Tag =( ) , Extra =( ) > {
2749 /// The actual bytes of the allocation.
@@ -131,9 +153,10 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
131153 fn check_bounds_ptr (
132154 & self ,
133155 ptr : Pointer < Tag > ,
156+ msg : CheckInAllocMsg ,
134157 ) -> EvalResult < ' tcx > {
135158 let allocation_size = self . bytes . len ( ) as u64 ;
136- ptr. check_in_alloc ( Size :: from_bytes ( allocation_size) , InboundsCheck :: Live )
159+ ptr. check_in_alloc ( Size :: from_bytes ( allocation_size) , msg )
137160 }
138161
139162 /// Checks if the memory range beginning at `ptr` and of size `Size` is "in-bounds".
@@ -143,9 +166,10 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
143166 cx : & impl HasDataLayout ,
144167 ptr : Pointer < Tag > ,
145168 size : Size ,
169+ msg : CheckInAllocMsg ,
146170 ) -> EvalResult < ' tcx > {
147171 // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
148- self . check_bounds_ptr ( ptr. offset ( size, cx) ?)
172+ self . check_bounds_ptr ( ptr. offset ( size, cx) ?, msg )
149173 }
150174}
151175
@@ -164,9 +188,10 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
164188 ptr : Pointer < Tag > ,
165189 size : Size ,
166190 check_defined_and_ptr : bool ,
191+ msg : CheckInAllocMsg ,
167192 ) -> EvalResult < ' tcx , & [ u8 ] >
168193 {
169- self . check_bounds ( cx, ptr, size) ?;
194+ self . check_bounds ( cx, ptr, size, msg ) ?;
170195
171196 if check_defined_and_ptr {
172197 self . check_defined ( ptr, size) ?;
@@ -192,7 +217,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
192217 size : Size ,
193218 ) -> EvalResult < ' tcx , & [ u8 ] >
194219 {
195- self . get_bytes_internal ( cx, ptr, size, true )
220+ self . get_bytes_internal ( cx, ptr, size, true , CheckInAllocMsg :: MemoryAccessTest )
196221 }
197222
198223 /// It is the caller's responsibility to handle undefined and pointer bytes.
@@ -205,7 +230,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
205230 size : Size ,
206231 ) -> EvalResult < ' tcx , & [ u8 ] >
207232 {
208- self . get_bytes_internal ( cx, ptr, size, false )
233+ self . get_bytes_internal ( cx, ptr, size, false , CheckInAllocMsg :: MemoryAccessTest )
209234 }
210235
211236 /// Just calling this already marks everything as defined and removes relocations,
@@ -218,7 +243,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
218243 ) -> EvalResult < ' tcx , & mut [ u8 ] >
219244 {
220245 assert_ne ! ( size. bytes( ) , 0 , "0-sized accesses should never even get a `Pointer`" ) ;
221- self . check_bounds ( cx, ptr, size) ?;
246+ self . check_bounds ( cx, ptr, size, CheckInAllocMsg :: MemoryAccessTest ) ?;
222247
223248 self . mark_definedness ( ptr, size, true ) ?;
224249 self . clear_relocations ( cx, ptr, size) ?;
0 commit comments