Skip to content

Commit 9a95b01

Browse files
committed
generalize InvalidNullPointerUsage to InvalidIntPointerUsage
1 parent d02543a commit 9a95b01

File tree

8 files changed

+32
-26
lines changed

8 files changed

+32
-26
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub struct Allocation<Tag = (), Extra = ()> {
4141
/// The size of the allocation. Currently, must always equal `bytes.len()`.
4242
pub size: Size,
4343
/// The alignment of the allocation to detect unaligned reads.
44+
/// (`Align` guarantees that this is a power of two.)
4445
pub align: Align,
4546
/// `true` if the allocation is mutable.
4647
/// Also used by codegen to determine if a static should be put into mutable memory,

src/librustc/mir/interpret/error.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,6 @@ pub enum UndefinedBehaviorInfo {
342342
UnterminatedCString(Pointer),
343343
/// Dereferencing a dangling pointer after it got freed.
344344
PointerUseAfterFree(AllocId),
345-
/// Using a NULL pointer in the wrong way.
346-
InvalidNullPointerUsage,
347345
/// Used a pointer outside the bounds it is valid for.
348346
PointerOutOfBounds {
349347
ptr: Pointer,
@@ -355,6 +353,8 @@ pub enum UndefinedBehaviorInfo {
355353
required: Align,
356354
has: Align,
357355
},
356+
/// Using an integer as a pointer in the wrong way.
357+
InvalidIntPointerUsage(u64),
358358
/// Writing to read-only memory.
359359
WriteToReadOnly(AllocId),
360360
/// Using a pointer-not-to-a-function as function pointer.
@@ -401,7 +401,6 @@ impl fmt::Debug for UndefinedBehaviorInfo {
401401
PointerUseAfterFree(a) => {
402402
write!(f, "pointer to {:?} was dereferenced after this allocation got freed", a)
403403
}
404-
InvalidNullPointerUsage => write!(f, "invalid use of NULL pointer"),
405404
PointerOutOfBounds { ptr, msg, allocation_size } => write!(
406405
f,
407406
"{} failed: pointer must be in-bounds at offset {}, \
@@ -411,6 +410,8 @@ impl fmt::Debug for UndefinedBehaviorInfo {
411410
ptr.alloc_id,
412411
allocation_size.bytes()
413412
),
413+
InvalidIntPointerUsage(0) => write!(f, "invalid use of NULL pointer"),
414+
InvalidIntPointerUsage(i) => write!(f, "invalid use of {} as a pointer", i),
414415
AlignmentCheckFailed { required, has } => write!(
415416
f,
416417
"accessing memory with alignment {}, but alignment {} is required",
@@ -450,24 +451,18 @@ impl fmt::Debug for UndefinedBehaviorInfo {
450451
pub enum UnsupportedOpInfo {
451452
/// Free-form case. Only for errors that are never caught!
452453
Unsupported(String),
453-
454454
/// When const-prop encounters a situation it does not support, it raises this error.
455455
/// This must not allocate for performance reasons (hence `str`, not `String`).
456456
ConstPropUnsupported(&'static str),
457-
458457
/// Accessing an unsupported foreign static.
459458
ReadForeignStatic(DefId),
460-
461459
/// Could not find MIR for a function.
462460
NoMirFor(DefId),
463-
464461
/// Modified a static during const-eval.
465462
/// FIXME: move this to `ConstEvalErrKind` through a machine hook.
466463
ModifiedStatic,
467-
468464
/// Encountered a pointer where we needed raw bytes.
469465
ReadPointerAsBytes,
470-
471466
/// Encountered raw bytes where we needed a pointer.
472467
ReadBytesAsPointer,
473468
}

src/librustc_mir/interpret/machine.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
281281
int: u64,
282282
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
283283
Err((if int == 0 {
284-
err_ub!(InvalidNullPointerUsage)
284+
// This is UB, seriously.
285+
err_ub!(InvalidIntPointerUsage(0))
285286
} else {
287+
// This is just something we cannot support during const-eval.
286288
err_unsup!(ReadBytesAsPointer)
287289
})
288290
.into())

src/librustc_mir/interpret/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
369369
assert!(size.bytes() == 0);
370370
// Must be non-NULL.
371371
if bits == 0 {
372-
throw_ub!(InvalidNullPointerUsage)
372+
throw_ub!(InvalidIntPointerUsage(0))
373373
}
374374
// Must be aligned.
375375
if let Some(align) = align {

src/librustc_mir/interpret/terminator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
171171
return Ok(());
172172
}
173173
let caller_arg = caller_arg.next().ok_or_else(|| {
174-
err_ub_format!("calling a function passing fewer arguments than it requires")
174+
err_ub_format!("calling a function with fewer arguments than it requires")
175175
})?;
176176
if rust_abi {
177177
assert!(!caller_arg.layout.is_zst(), "ZSTs must have been already filtered out");
@@ -341,7 +341,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
341341
// Now we should have no more caller args
342342
if caller_iter.next().is_some() {
343343
throw_ub_format!(
344-
"calling a function passing more arguments than it expected"
344+
"calling a function with more arguments than it expected"
345345
)
346346
}
347347
// Don't forget to check the return type!

src/librustc_mir/interpret/validity.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,16 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
353353
place.ptr, size, align
354354
);
355355
match err.kind {
356-
err_ub!(InvalidNullPointerUsage) => {
356+
err_ub!(InvalidIntPointerUsage(0)) => {
357357
throw_validation_failure!(format_args!("a NULL {}", kind), self.path)
358358
}
359+
err_ub!(InvalidIntPointerUsage(i)) => throw_validation_failure!(
360+
format_args!("a {} to unallocated address {}", kind, i),
361+
self.path
362+
),
359363
err_ub!(AlignmentCheckFailed { required, has }) => throw_validation_failure!(
360364
format_args!(
361-
"an unaligned {} \
362-
(required {} byte alignment but found {})",
365+
"an unaligned {} (required {} byte alignment but found {})",
363366
kind,
364367
required.bytes(),
365368
has.bytes()
@@ -370,12 +373,17 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
370373
format_args!("a dangling {} (created from integer)", kind),
371374
self.path
372375
),
373-
err_ub!(PointerOutOfBounds { .. }) | err_ub!(PointerUseAfterFree(_)) => {
374-
throw_validation_failure!(
375-
format_args!("a dangling {} (not entirely in bounds)", kind),
376-
self.path
377-
)
378-
}
376+
err_ub!(PointerOutOfBounds { .. }) => throw_validation_failure!(
377+
format_args!(
378+
"a dangling {} (going beyond the bounds of its allocation)",
379+
kind
380+
),
381+
self.path
382+
),
383+
err_ub!(PointerUseAfterFree(_)) => throw_validation_failure!(
384+
format_args!("a dangling {} (use-after-free)", kind),
385+
self.path
386+
),
379387
_ => bug!("Unexpected error during ptr inbounds test: {}", err),
380388
}
381389
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: it is undefined behavior to use this value
22
--> $DIR/ub-wide-ptr.rs:32:1
33
|
44
LL | const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (not entirely in bounds)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (going beyond the bounds of its allocation)
66
|
77
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
88

@@ -70,7 +70,7 @@ error[E0080]: it is undefined behavior to use this value
7070
--> $DIR/ub-wide-ptr.rs:62:1
7171
|
7272
LL | const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
73-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (not entirely in bounds)
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (going beyond the bounds of its allocation)
7474
|
7575
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
7676

@@ -86,7 +86,7 @@ error[E0080]: it is undefined behavior to use this value
8686
--> $DIR/ub-wide-ptr.rs:68:1
8787
|
8888
LL | const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
89-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling box (not entirely in bounds)
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling box (going beyond the bounds of its allocation)
9090
|
9191
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
9292

src/test/ui/error-codes/E0396-fixed.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: any use of this value will cause an error
44
LL | const VALUE: u8 = unsafe { *REG_ADDR };
55
| ---------------------------^^^^^^^^^---
66
| |
7-
| a memory access tried to interpret some bytes as a pointer
7+
| unable to turn these bytes into a pointer
88
|
99
= note: `#[deny(const_err)]` on by default
1010

0 commit comments

Comments
 (0)