1- use super :: { AllocId , CheckInAllocMsg , Pointer , RawConst , ScalarMaybeUndef } ;
1+ use super :: { AllocId , Pointer , RawConst , ScalarMaybeUndef } ;
22
33use crate :: mir:: interpret:: ConstValue ;
44use crate :: ty:: layout:: LayoutError ;
@@ -304,6 +304,32 @@ impl fmt::Display for InvalidProgramInfo<'_> {
304304 }
305305}
306306
307+ /// Details of why a pointer had to be in-bounds.
308+ #[ derive( Debug , Copy , Clone , RustcEncodable , RustcDecodable , HashStable ) ]
309+ pub enum CheckInAllocMsg {
310+ MemoryAccessTest ,
311+ NullPointerTest ,
312+ PointerArithmeticTest ,
313+ InboundsTest ,
314+ }
315+
316+ impl fmt:: Display for CheckInAllocMsg {
317+ /// When this is printed as an error the context looks like this
318+ /// "{test name} failed: pointer must be in-bounds at offset..."
319+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
320+ write ! (
321+ f,
322+ "{}" ,
323+ match * self {
324+ CheckInAllocMsg :: MemoryAccessTest => "memory access" ,
325+ CheckInAllocMsg :: NullPointerTest => "NULL pointer test" ,
326+ CheckInAllocMsg :: PointerArithmeticTest => "pointer arithmetic" ,
327+ CheckInAllocMsg :: InboundsTest => "inbounds test" ,
328+ }
329+ )
330+ }
331+ }
332+
307333/// Error information for when the program caused Undefined Behavior.
308334pub enum UndefinedBehaviorInfo {
309335 /// Free-form case. Only for errors that are never caught!
@@ -333,17 +359,15 @@ pub enum UndefinedBehaviorInfo {
333359 msg : CheckInAllocMsg ,
334360 allocation_size : Size ,
335361 } ,
362+ /// Using an integer as a pointer in the wrong way.
363+ DanglingIntPointer ( u64 , CheckInAllocMsg ) ,
336364 /// Used a pointer with bad alignment.
337365 AlignmentCheckFailed {
338366 required : Align ,
339367 has : Align ,
340368 } ,
341- /// Using an integer as a pointer in the wrong way.
342- InvalidIntPointerUsage ( u64 ) ,
343369 /// Writing to read-only memory.
344370 WriteToReadOnly ( AllocId ) ,
345- /// Using a pointer-not-to-a-function as function pointer.
346- InvalidFunctionPointer ( Pointer ) ,
347371 // Trying to access the data behind a function pointer.
348372 DerefFunctionPointer ( AllocId ) ,
349373 /// The value validity check found a problem.
@@ -356,6 +380,8 @@ pub enum UndefinedBehaviorInfo {
356380 InvalidChar ( u32 ) ,
357381 /// An enum discriminant was set to a value which was outside the range of valid values.
358382 InvalidDiscriminant ( ScalarMaybeUndef ) ,
383+ /// Using a pointer-not-to-a-function as function pointer.
384+ InvalidFunctionPointer ( Pointer ) ,
359385 /// Using uninitialized data where it is not allowed.
360386 InvalidUndefBytes ( Option < Pointer > ) ,
361387 /// Working with a local that is not currently live.
@@ -397,23 +423,27 @@ impl fmt::Display for UndefinedBehaviorInfo {
397423 ptr. alloc_id,
398424 allocation_size. bytes( )
399425 ) ,
400- InvalidIntPointerUsage ( 0 ) => write ! ( f, "dereferencing NULL pointer" ) ,
401- InvalidIntPointerUsage ( i) => write ! ( f, "dereferencing dangling pointer to 0x{:x}" , i) ,
426+ DanglingIntPointer ( _, CheckInAllocMsg :: NullPointerTest ) => {
427+ write ! ( f, "NULL pointer is not allowed for this operation" )
428+ }
429+ DanglingIntPointer ( i, msg) => {
430+ write ! ( f, "{} failed: 0x{:x} is not a valid pointer" , msg, i)
431+ }
402432 AlignmentCheckFailed { required, has } => write ! (
403433 f,
404434 "accessing memory with alignment {}, but alignment {} is required" ,
405435 has. bytes( ) ,
406436 required. bytes( )
407437 ) ,
408438 WriteToReadOnly ( a) => write ! ( f, "writing to {} which is read-only" , a) ,
409- InvalidFunctionPointer ( p) => {
410- write ! ( f, "using {} as function pointer but it does not point to a function" , p)
411- }
412439 DerefFunctionPointer ( a) => write ! ( f, "accessing {} which contains a function" , a) ,
413440 ValidationFailure ( ref err) => write ! ( f, "type validation failed: {}" , err) ,
414441 InvalidBool ( b) => write ! ( f, "interpreting an invalid 8-bit value as a bool: {}" , b) ,
415442 InvalidChar ( c) => write ! ( f, "interpreting an invalid 32-bit value as a char: {}" , c) ,
416443 InvalidDiscriminant ( val) => write ! ( f, "enum value has invalid discriminant: {}" , val) ,
444+ InvalidFunctionPointer ( p) => {
445+ write ! ( f, "using {} as function pointer but it does not point to a function" , p)
446+ }
417447 InvalidUndefBytes ( Some ( p) ) => write ! (
418448 f,
419449 "reading uninitialized memory at {}, but this operation requires initialized memory" ,
0 commit comments