Skip to content

Commit b4dde36

Browse files
committed
let caller of check_ptr_access_align control the error message
1 parent f49f388 commit b4dde36

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/librustc_mir/interpret/memory.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
314314
align: Align,
315315
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
316316
let align = if M::CHECK_ALIGN { Some(align) } else { None };
317-
self.check_ptr_access_align(sptr, size, align)
317+
self.check_ptr_access_align(sptr, size, align, CheckInAllocMsg::MemoryAccessTest)
318318
}
319319

320320
/// Like `check_ptr_access`, but *definitely* checks alignment when `align`
321-
/// is `Some` (overriding `M::CHECK_ALIGN`).
322-
pub(super) fn check_ptr_access_align(
321+
/// is `Some` (overriding `M::CHECK_ALIGN`). Also lets the caller control
322+
/// the error message for the out-of-bounds case.
323+
pub fn check_ptr_access_align(
323324
&self,
324325
sptr: Scalar<M::PointerTag>,
325326
size: Size,
326327
align: Option<Align>,
328+
msg: CheckInAllocMsg,
327329
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
328330
fn check_offset_align(offset: u64, align: Align) -> InterpResult<'static> {
329331
if offset % align.bytes() == 0 {
@@ -368,7 +370,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
368370
// It is sufficient to check this for the end pointer. The addition
369371
// checks for overflow.
370372
let end_ptr = ptr.offset(size, self)?;
371-
end_ptr.check_inbounds_alloc(allocation_size, CheckInAllocMsg::MemoryAccessTest)?;
373+
end_ptr.check_inbounds_alloc(allocation_size, msg)?;
372374
// Test align. Check this last; if both bounds and alignment are violated
373375
// we want the error to be about the bounds.
374376
if let Some(align) = align {

src/librustc_mir/interpret/validity.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_data_structures::fx::FxHashSet;
1616
use std::hash::Hash;
1717

1818
use super::{
19-
GlobalAlloc, InterpResult,
19+
GlobalAlloc, InterpResult, CheckInAllocMsg,
2020
Scalar, OpTy, Machine, InterpCx, ValueVisitor, MPlaceTy,
2121
};
2222

@@ -424,7 +424,12 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
424424
// alignment should take attributes into account).
425425
.unwrap_or_else(|| (layout.size, layout.align.abi));
426426
let ptr: Option<_> = match
427-
self.ecx.memory.check_ptr_access_align(ptr, size, Some(align))
427+
self.ecx.memory.check_ptr_access_align(
428+
ptr,
429+
size,
430+
Some(align),
431+
CheckInAllocMsg::InboundsTest,
432+
)
428433
{
429434
Ok(ptr) => ptr,
430435
Err(err) => {

0 commit comments

Comments
 (0)