Skip to content

Commit 707bd7b

Browse files
committed
rename InvalidIntPtrUsage
1 parent 2feeb88 commit 707bd7b

File tree

9 files changed

+54
-50
lines changed

9 files changed

+54
-50
lines changed

src/librustc_middle/mir/interpret/error.rs

+40-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{AllocId, CheckInAllocMsg, Pointer, RawConst, ScalarMaybeUndef};
1+
use super::{AllocId, Pointer, RawConst, ScalarMaybeUndef};
22

33
use crate::mir::interpret::ConstValue;
44
use 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.
308334
pub 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",

src/librustc_middle/mir/interpret/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ use crate::ty::subst::GenericArgKind;
117117
use crate::ty::{self, Instance, Ty, TyCtxt};
118118

119119
pub use self::error::{
120-
struct_error, ConstEvalErr, ConstEvalRawResult, ConstEvalResult, ErrorHandled, FrameInfo,
121-
InterpError, InterpErrorInfo, InterpResult, InvalidProgramInfo, MachineStopType,
120+
struct_error, CheckInAllocMsg, ConstEvalErr, ConstEvalRawResult, ConstEvalResult, ErrorHandled,
121+
FrameInfo, InterpError, InterpErrorInfo, InterpResult, InvalidProgramInfo, MachineStopType,
122122
ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo,
123123
};
124124

125125
pub use self::value::{get_slice_bytes, ConstValue, RawConst, Scalar, ScalarMaybeUndef};
126126

127127
pub use self::allocation::{Allocation, AllocationExtra, Relocations, UndefMask};
128128

129-
pub use self::pointer::{CheckInAllocMsg, Pointer, PointerArithmetic};
129+
pub use self::pointer::{Pointer, PointerArithmetic};
130130

131131
/// Uniquely identifies one of the following:
132132
/// - A constant

src/librustc_middle/mir/interpret/pointer.rs

+1-27
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,7 @@ use rustc_macros::HashStable;
44
use rustc_target::abi::{HasDataLayout, Size};
55

66
use std::convert::TryFrom;
7-
use std::fmt::{self, Display};
8-
9-
/// Used by `check_in_alloc` to indicate context of check
10-
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
11-
pub enum CheckInAllocMsg {
12-
MemoryAccessTest,
13-
NullPointerTest,
14-
PointerArithmeticTest,
15-
InboundsTest,
16-
}
17-
18-
impl Display for CheckInAllocMsg {
19-
/// When this is printed as an error the context looks like this
20-
/// "{test name} failed: pointer must be in-bounds at offset..."
21-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22-
write!(
23-
f,
24-
"{}",
25-
match *self {
26-
CheckInAllocMsg::MemoryAccessTest => "Memory access",
27-
CheckInAllocMsg::NullPointerTest => "Null pointer test",
28-
CheckInAllocMsg::PointerArithmeticTest => "Pointer arithmetic",
29-
CheckInAllocMsg::InboundsTest => "Inbounds test",
30-
}
31-
)
32-
}
33-
}
7+
use std::fmt;
348

359
////////////////////////////////////////////////////////////////////////////////
3610
// Pointer arithmetic

src/librustc_mir/interpret/machine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_middle::ty::{self, Ty};
1010
use rustc_span::def_id::DefId;
1111

1212
use super::{
13-
AllocId, Allocation, AllocationExtra, Frame, ImmTy, InterpCx, InterpResult, Memory, MemoryKind,
14-
OpTy, Operand, PlaceTy, Pointer, Scalar,
13+
AllocId, Allocation, AllocationExtra, CheckInAllocMsg, Frame, ImmTy, InterpCx, InterpResult,
14+
Memory, MemoryKind, OpTy, Operand, PlaceTy, Pointer, Scalar,
1515
};
1616

1717
/// Data returned by Machine::stack_pop,
@@ -346,7 +346,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
346346
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
347347
Err((if int == 0 {
348348
// This is UB, seriously.
349-
err_ub!(InvalidIntPointerUsage(0))
349+
err_ub!(DanglingIntPointer(0, CheckInAllocMsg::InboundsTest))
350350
} else {
351351
// This is just something we cannot support during const-eval.
352352
err_unsup!(ReadBytesAsPointer)

src/librustc_mir/interpret/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
365365
assert!(size.bytes() == 0);
366366
// Must be non-NULL.
367367
if bits == 0 {
368-
throw_ub!(InvalidIntPointerUsage(0))
368+
throw_ub!(DanglingIntPointer(0, msg))
369369
}
370370
// Must be aligned.
371371
if let Some(align) = align {

src/librustc_mir/interpret/validity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,10 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
360360
place.ptr, size, align
361361
);
362362
match err.kind {
363-
err_ub!(InvalidIntPointerUsage(0)) => {
363+
err_ub!(DanglingIntPointer(0, _)) => {
364364
throw_validation_failure!(format_args!("a NULL {}", kind), self.path)
365365
}
366-
err_ub!(InvalidIntPointerUsage(i)) => throw_validation_failure!(
366+
err_ub!(DanglingIntPointer(i, _)) => throw_validation_failure!(
367367
format_args!("a {} to unallocated address {}", kind, i),
368368
self.path
369369
),

src/test/ui/consts/const-eval/ub-nonnull.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | / const OUT_OF_BOUNDS_PTR: NonNull<u8> = { unsafe {
1313
LL | | let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
1414
LL | | // Use address-of-element for pointer arithmetic. This could wrap around to NULL!
1515
LL | | let out_of_bounds_ptr = &ptr[255];
16-
| | ^^^^^^^^^ Memory access failed: pointer must be in-bounds at offset 256, but is outside bounds of alloc11 which has size 1
16+
| | ^^^^^^^^^ memory access failed: pointer must be in-bounds at offset 256, but is outside bounds of alloc11 which has size 1
1717
LL | | mem::transmute(out_of_bounds_ptr)
1818
LL | | } };
1919
| |____-

src/test/ui/consts/const-eval/ub-wide-ptr.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ error[E0080]: could not evaluate static initializer
186186
--> $DIR/ub-wide-ptr.rs:121:5
187187
|
188188
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
189-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing NULL pointer
189+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ inbounds test failed: 0x0 is not a valid pointer
190190

191191
error[E0080]: could not evaluate static initializer
192192
--> $DIR/ub-wide-ptr.rs:125:5
193193
|
194194
LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
195-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Memory access failed: pointer must be in-bounds at offset N, but is outside bounds of allocN which has size N
195+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds at offset N, but is outside bounds of allocN which has size N
196196

197197
error: aborting due to 24 previous errors
198198

src/test/ui/consts/offset_from_ub.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ error: any use of this value will cause an error
6666
LL | intrinsics::ptr_offset_from(self, origin)
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6868
| |
69-
| dereferencing NULL pointer
69+
| inbounds test failed: 0x0 is not a valid pointer
7070
| inside `std::ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
7171
| inside `OFFSET_FROM_NULL` at $DIR/offset_from_ub.rs:37:14
7272
|

0 commit comments

Comments
 (0)