1
- use super :: { AllocId , CheckInAllocMsg , Pointer , RawConst , ScalarMaybeUndef } ;
1
+ use super :: { AllocId , Pointer , RawConst , ScalarMaybeUndef } ;
2
2
3
3
use crate :: mir:: interpret:: ConstValue ;
4
4
use crate :: ty:: layout:: LayoutError ;
@@ -304,6 +304,32 @@ impl fmt::Display for InvalidProgramInfo<'_> {
304
304
}
305
305
}
306
306
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
+
307
333
/// Error information for when the program caused Undefined Behavior.
308
334
pub enum UndefinedBehaviorInfo {
309
335
/// Free-form case. Only for errors that are never caught!
@@ -333,17 +359,15 @@ pub enum UndefinedBehaviorInfo {
333
359
msg : CheckInAllocMsg ,
334
360
allocation_size : Size ,
335
361
} ,
362
+ /// Using an integer as a pointer in the wrong way.
363
+ DanglingIntPointer ( u64 , CheckInAllocMsg ) ,
336
364
/// Used a pointer with bad alignment.
337
365
AlignmentCheckFailed {
338
366
required : Align ,
339
367
has : Align ,
340
368
} ,
341
- /// Using an integer as a pointer in the wrong way.
342
- InvalidIntPointerUsage ( u64 ) ,
343
369
/// Writing to read-only memory.
344
370
WriteToReadOnly ( AllocId ) ,
345
- /// Using a pointer-not-to-a-function as function pointer.
346
- InvalidFunctionPointer ( Pointer ) ,
347
371
// Trying to access the data behind a function pointer.
348
372
DerefFunctionPointer ( AllocId ) ,
349
373
/// The value validity check found a problem.
@@ -356,6 +380,8 @@ pub enum UndefinedBehaviorInfo {
356
380
InvalidChar ( u32 ) ,
357
381
/// An enum discriminant was set to a value which was outside the range of valid values.
358
382
InvalidDiscriminant ( ScalarMaybeUndef ) ,
383
+ /// Using a pointer-not-to-a-function as function pointer.
384
+ InvalidFunctionPointer ( Pointer ) ,
359
385
/// Using uninitialized data where it is not allowed.
360
386
InvalidUndefBytes ( Option < Pointer > ) ,
361
387
/// Working with a local that is not currently live.
@@ -397,23 +423,27 @@ impl fmt::Display for UndefinedBehaviorInfo {
397
423
ptr. alloc_id,
398
424
allocation_size. bytes( )
399
425
) ,
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
+ }
402
432
AlignmentCheckFailed { required, has } => write ! (
403
433
f,
404
434
"accessing memory with alignment {}, but alignment {} is required" ,
405
435
has. bytes( ) ,
406
436
required. bytes( )
407
437
) ,
408
438
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
- }
412
439
DerefFunctionPointer ( a) => write ! ( f, "accessing {} which contains a function" , a) ,
413
440
ValidationFailure ( ref err) => write ! ( f, "type validation failed: {}" , err) ,
414
441
InvalidBool ( b) => write ! ( f, "interpreting an invalid 8-bit value as a bool: {}" , b) ,
415
442
InvalidChar ( c) => write ! ( f, "interpreting an invalid 32-bit value as a char: {}" , c) ,
416
443
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
+ }
417
447
InvalidUndefBytes ( Some ( p) ) => write ! (
418
448
f,
419
449
"reading uninitialized memory at {}, but this operation requires initialized memory" ,
0 commit comments