Skip to content

Commit ae950a2

Browse files
committed
more precise message for the ptr access check on deref
1 parent 4ff353c commit ae950a2

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

compiler/rustc_middle/src/mir/interpret/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ impl fmt::Display for InvalidProgramInfo<'_> {
170170
/// Details of why a pointer had to be in-bounds.
171171
#[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
172172
pub enum CheckInAllocMsg {
173+
/// We are dereferencing a pointer (i.e., creating a place).
174+
DerefTest,
173175
/// We are access memory.
174176
MemoryAccessTest,
175177
/// We are doing pointer arithmetic.
@@ -186,6 +188,7 @@ impl fmt::Display for CheckInAllocMsg {
186188
f,
187189
"{}",
188190
match *self {
191+
CheckInAllocMsg::DerefTest => "dereferencing pointer failed: ",
189192
CheckInAllocMsg::MemoryAccessTest => "memory access failed: ",
190193
CheckInAllocMsg::PointerArithmeticTest => "pointer arithmetic failed: ",
191194
CheckInAllocMsg::InboundsTest => "",

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,11 @@ crate struct AllocMap<'tcx> {
428428

429429
impl<'tcx> AllocMap<'tcx> {
430430
crate fn new() -> Self {
431-
AllocMap { alloc_map: Default::default(), dedup: Default::default(), next_id: AllocId(NonZeroU64::new(1).unwrap()) }
431+
AllocMap {
432+
alloc_map: Default::default(),
433+
dedup: Default::default(),
434+
next_id: AllocId(NonZeroU64::new(1).unwrap()),
435+
}
432436
}
433437
fn reserve(&mut self) -> AllocId {
434438
let next = self.next_id;

compiler/rustc_mir/src/interpret/place.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ where
373373
let val = self.read_immediate(src)?;
374374
trace!("deref to {} on {:?}", val.layout.ty, *val);
375375
let mplace = self.ref_to_mplace(&val)?;
376-
self.check_mplace_access(mplace)?;
376+
self.check_mplace_access(mplace, CheckInAllocMsg::DerefTest)?;
377377
Ok(mplace)
378378
}
379379

@@ -400,18 +400,17 @@ where
400400
}
401401

402402
/// Check if this mplace is dereferencable and sufficiently aligned.
403-
pub fn check_mplace_access(&self, mplace: MPlaceTy<'tcx, M::PointerTag>) -> InterpResult<'tcx> {
403+
fn check_mplace_access(
404+
&self,
405+
mplace: MPlaceTy<'tcx, M::PointerTag>,
406+
msg: CheckInAllocMsg,
407+
) -> InterpResult<'tcx> {
404408
let (size, align) = self
405409
.size_and_align_of_mplace(&mplace)?
406410
.unwrap_or((mplace.layout.size, mplace.layout.align.abi));
407411
assert!(mplace.mplace.align <= align, "dynamic alignment less strict than static one?");
408412
let align = M::enforce_alignment(&self.memory.extra).then_some(align);
409-
self.memory.check_ptr_access_align(
410-
mplace.ptr,
411-
size,
412-
align.unwrap_or(Align::ONE),
413-
CheckInAllocMsg::MemoryAccessTest, // FIXME sth more specific?
414-
)?;
413+
self.memory.check_ptr_access_align(mplace.ptr, size, align.unwrap_or(Align::ONE), msg)?;
415414
Ok(())
416415
}
417416

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0080]: evaluation of constant value failed
1313
--> $DIR/ub-nonnull.rs:19:30
1414
|
1515
LL | let out_of_bounds_ptr = &ptr[255];
16-
| ^^^^^^^^ memory access failed: pointer must be in-bounds for 256 bytes at offset 0, but alloc11 has size 1
16+
| ^^^^^^^^ dereferencing pointer failed: pointer must be in-bounds for 256 bytes at offset 0, but alloc11 has size 1
1717

1818
error[E0080]: it is undefined behavior to use this value
1919
--> $DIR/ub-nonnull.rs:23:1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0080]: evaluation of constant value failed
1313
--> $DIR/ub-nonnull.rs:19:30
1414
|
1515
LL | let out_of_bounds_ptr = &ptr[255];
16-
| ^^^^^^^^ memory access failed: pointer must be in-bounds for 256 bytes at offset 0, but alloc11 has size 1
16+
| ^^^^^^^^ dereferencing pointer failed: pointer must be in-bounds for 256 bytes at offset 0, but alloc11 has size 1
1717

1818
error[E0080]: it is undefined behavior to use this value
1919
--> $DIR/ub-nonnull.rs:23:1

src/test/ui/consts/ptr_comparisons.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0080]: evaluation of constant value failed
1616
--> $DIR/ptr_comparisons.rs:64:33
1717
|
1818
LL | unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) };
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds for 1000 bytes at offset 0, but alloc3 has size $WORD
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: pointer must be in-bounds for 1000 bytes at offset 0, but alloc3 has size $WORD
2020

2121
error: any use of this value will cause an error
2222
--> $DIR/ptr_comparisons.rs:68:27

0 commit comments

Comments
 (0)