Skip to content

Commit 2a738bb

Browse files
committed
Improve miri's error reporting in check_in_alloc
1 parent 3449fa9 commit 2a738bb

File tree

5 files changed

+23
-39
lines changed

5 files changed

+23
-39
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,19 @@ pub enum InboundsCheck {
2525
/// Used by `check_in_alloc` to indicate context of check
2626
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
2727
pub enum CheckInAllocMsg {
28-
ReadCStr,
29-
CheckBytes,
30-
WriteBytes,
31-
WriteRepeat,
32-
ReadScalar,
33-
WriteScalar,
34-
SlicePatCoveredByConst,
35-
ReadDiscriminant,
36-
CheckAlign,
37-
ReadBytes,
38-
CopyRepeatedly,
39-
CheckBounds,
28+
MemoryAccess,
29+
NullPointer,
30+
PointerArithmetic,
31+
OutOfBounds,
4032
}
4133

4234
impl Display for CheckInAllocMsg {
4335
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4436
write!(f, "{}", match *self {
45-
CheckInAllocMsg::ReadCStr => "read C str",
46-
CheckInAllocMsg::CheckBytes => "check bytes",
47-
CheckInAllocMsg::WriteBytes => "write bytes",
48-
CheckInAllocMsg::WriteRepeat => "write repeat",
49-
CheckInAllocMsg::ReadScalar => "read scalar",
50-
CheckInAllocMsg::WriteScalar => "write scalar",
51-
CheckInAllocMsg::SlicePatCoveredByConst => "slice pat covered by const",
52-
CheckInAllocMsg::ReadDiscriminant => "read discriminant",
53-
CheckInAllocMsg::CheckAlign => "check align",
54-
CheckInAllocMsg::ReadBytes => "read bytes",
55-
CheckInAllocMsg::CopyRepeatedly => "copy repeatedly",
56-
CheckInAllocMsg::CheckBounds => "check bounds",
37+
CheckInAllocMsg::MemoryAccess => "memory access",
38+
CheckInAllocMsg::NullPointer => "null pointer",
39+
CheckInAllocMsg::PointerArithmetic => "pointer arithmetic",
40+
CheckInAllocMsg::OutOfBounds => "out of bounds",
5741
})
5842
}
5943
}
@@ -311,7 +295,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
311295
// Go through `get_bytes` for checks and AllocationExtra hooks.
312296
// We read the null, so we include it in the request, but we want it removed
313297
// from the result!
314-
Ok(&self.get_bytes(cx, ptr, size_with_null, CheckInAllocMsg::ReadCStr)?[..size])
298+
Ok(&self.get_bytes(cx, ptr, size_with_null, CheckInAllocMsg::NullPointer)?[..size])
315299
}
316300
None => err!(UnterminatedCString(ptr.erase_tag())),
317301
}
@@ -331,7 +315,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
331315
where Extra: AllocationExtra<Tag, MemoryExtra>
332316
{
333317
// Check bounds and relocations on the edges
334-
self.get_bytes_with_undef_and_ptr(cx, ptr, size, CheckInAllocMsg::CheckBytes)?;
318+
self.get_bytes_with_undef_and_ptr(cx, ptr, size, CheckInAllocMsg::OutOfBounds)?;
335319
// Check undef and ptr
336320
if !allow_ptr_and_undef {
337321
self.check_defined(ptr, size)?;
@@ -353,7 +337,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
353337
where Extra: AllocationExtra<Tag, MemoryExtra>
354338
{
355339
let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(src.len() as u64),
356-
CheckInAllocMsg::WriteBytes)?;
340+
CheckInAllocMsg::MemoryAccess)?;
357341
bytes.clone_from_slice(src);
358342
Ok(())
359343
}
@@ -369,7 +353,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
369353
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
370354
where Extra: AllocationExtra<Tag, MemoryExtra>
371355
{
372-
let bytes = self.get_bytes_mut(cx, ptr, count, CheckInAllocMsg::WriteRepeat)?;
356+
let bytes = self.get_bytes_mut(cx, ptr, count, CheckInAllocMsg::MemoryAccess)?;
373357
for b in bytes {
374358
*b = val;
375359
}
@@ -394,7 +378,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
394378
where Extra: AllocationExtra<Tag, MemoryExtra>
395379
{
396380
// get_bytes_unchecked tests relocation edges
397-
let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size, CheckInAllocMsg::ReadScalar)?;
381+
let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size, CheckInAllocMsg::PointerArithmetic)?;
398382
// Undef check happens *after* we established that the alignment is correct.
399383
// We must not return Ok() for unaligned pointers!
400384
if self.check_defined(ptr, size).is_err() {
@@ -471,7 +455,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
471455
};
472456

473457
let endian = cx.data_layout().endian;
474-
let dst = self.get_bytes_mut(cx, ptr, type_size, CheckInAllocMsg::WriteScalar)?;
458+
let dst = self.get_bytes_mut(cx, ptr, type_size, CheckInAllocMsg::PointerArithmetic)?;
475459
write_target_uint(endian, dst, bytes).unwrap();
476460

477461
// See if we have to also write a relocation

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ fn slice_pat_covered_by_const<'tcx>(
14191419
}
14201420
let n = n.assert_usize(tcx).unwrap();
14211421
alloc.get_bytes(&tcx, ptr, Size::from_bytes(n),
1422-
CheckInAllocMsg::SlicePatCoveredByConst).unwrap()
1422+
CheckInAllocMsg::OutOfBounds).unwrap()
14231423
},
14241424
// a slice fat pointer to a zero length slice
14251425
(ConstValue::Slice(Scalar::Bits { .. }, 0), ty::Slice(t)) => {
@@ -1444,7 +1444,7 @@ fn slice_pat_covered_by_const<'tcx>(
14441444
tcx.alloc_map
14451445
.lock()
14461446
.unwrap_memory(ptr.alloc_id)
1447-
.get_bytes(&tcx, ptr, Size::from_bytes(n), CheckInAllocMsg::SlicePatCoveredByConst)
1447+
.get_bytes(&tcx, ptr, Size::from_bytes(n), CheckInAllocMsg::OutOfBounds)
14481448
.unwrap()
14491449
},
14501450
_ => bug!(

src/librustc_mir/interpret/memory.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
252252
Scalar::Ptr(ptr) => {
253253
// check this is not NULL -- which we can ensure only if this is in-bounds
254254
// of some (potentially dead) allocation.
255-
let align = self.check_bounds_ptr(ptr, CheckInAllocMsg::CheckAlign)?;
255+
let align = self.check_bounds_ptr(ptr, CheckInAllocMsg::NullPointer)?;
256256
(ptr.offset.bytes(), align)
257257
}
258258
Scalar::Bits { bits, size } => {
@@ -440,7 +440,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
440440
Ok((layout.size, layout.align.abi))
441441
}
442442
_ => match msg {
443-
CheckInAllocMsg::CheckAlign | CheckInAllocMsg::ReadDiscriminant => {
443+
CheckInAllocMsg::NullPointer | CheckInAllocMsg::OutOfBounds => {
444444
// Must be a deallocated pointer
445445
Ok(*self.dead_alloc_map.get(&id).expect(
446446
"allocation missing in dead_alloc_map"
@@ -604,7 +604,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
604604
Ok(&[])
605605
} else {
606606
let ptr = ptr.to_ptr()?;
607-
self.get(ptr.alloc_id)?.get_bytes(self, ptr, size, CheckInAllocMsg::ReadBytes)
607+
self.get(ptr.alloc_id)?.get_bytes(self, ptr, size, CheckInAllocMsg::MemoryAccess)
608608
}
609609
}
610610
}
@@ -729,10 +729,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
729729

730730
// This checks relocation edges on the src.
731731
let src_bytes = self.get(src.alloc_id)?
732-
.get_bytes_with_undef_and_ptr(&tcx, src, size, CheckInAllocMsg::CopyRepeatedly)?
732+
.get_bytes_with_undef_and_ptr(&tcx, src, size, CheckInAllocMsg::MemoryAccess)?
733733
.as_ptr();
734734
let dest_bytes = self.get_mut(dest.alloc_id)?
735-
.get_bytes_mut(&tcx, dest, size * length, CheckInAllocMsg::CopyRepeatedly)?
735+
.get_bytes_mut(&tcx, dest, size * length, CheckInAllocMsg::MemoryAccess)?
736736
.as_mut_ptr();
737737

738738
// SAFE: The above indexing would have panicked if there weren't at least `size` bytes

src/librustc_mir/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
668668
// The niche must be just 0 (which an inbounds pointer value never is)
669669
let ptr_valid = niche_start == 0 && variants_start == variants_end &&
670670
self.memory.check_bounds_ptr(ptr,
671-
CheckInAllocMsg::ReadDiscriminant).is_ok();
671+
CheckInAllocMsg::OutOfBounds).is_ok();
672672
if !ptr_valid {
673673
return err!(InvalidDiscriminant(raw_discr.erase_tag()));
674674
}

src/librustc_mir/interpret/validity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>>
394394
try_validation!(
395395
self.ecx.memory
396396
.get(ptr.alloc_id)?
397-
.check_bounds(self.ecx, ptr, size, CheckInAllocMsg::CheckBounds),
397+
.check_bounds(self.ecx, ptr, size, CheckInAllocMsg::OutOfBounds),
398398
"dangling (not entirely in bounds) reference", self.path);
399399
}
400400
// Check if we have encountered this pointer+layout combination

0 commit comments

Comments
 (0)