Skip to content

Commit 71ef841

Browse files
committed
Add checks and tests for computing abs(offset_bytes)
The previous code paniced if offset_bytes == i64::MIN. This commit: - Properly computes the absoulte value to avoid this panic - Adds a test for this edge case Signed-off-by: Joe Richey <[email protected]>
1 parent 6367b54 commit 71ef841

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/librustc_mir/interpret/intrinsics.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::convert::TryFrom;
77
use rustc_hir::def_id::DefId;
88
use rustc_middle::mir::{
99
self,
10-
interpret::{ConstValue, GlobalId, InterpResult, Scalar},
10+
interpret::{uabs, ConstValue, GlobalId, InterpResult, Scalar},
1111
BinOp,
1212
};
1313
use rustc_middle::ty;
@@ -438,6 +438,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
438438
pointee_ty: Ty<'tcx>,
439439
offset_count: i64,
440440
) -> InterpResult<'tcx, Scalar<M::PointerTag>> {
441+
// We cannot overflow i64 as a type's size must be <= isize::MAX.
441442
let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
442443
// The computed offset, in bytes, cannot overflow an isize.
443444
let offset_bytes = offset_count
@@ -450,7 +451,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
450451
// memory between these pointers must be accessible. Note that we do not require the
451452
// pointers to be properly aligned (unlike a read/write operation).
452453
let min_ptr = if offset_bytes >= 0 { ptr } else { offset_ptr };
453-
let size = offset_bytes.checked_abs().unwrap();
454+
let size: u64 = uabs(offset_bytes);
454455
// This call handles checking for integer/NULL pointers.
455456
self.memory.check_ptr_access_align(
456457
min_ptr,

src/test/ui/consts/offset_ub.rs

+3
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ pub const DANGLING: *const u8 = unsafe { ptr::NonNull::<u8>::dangling().as_ptr()
1919
// Right now, a zero offset from null is UB
2020
pub const NULL_OFFSET_ZERO: *const u8 = unsafe { ptr::null::<u8>().offset(0) }; //~NOTE
2121

22+
// Make sure that we don't panic when computing abs(offset*size_of::<T>())
23+
pub const UNDERFLOW_ABS: *const u8 = unsafe { (usize::MAX as *const u8).offset(isize::MIN) }; //~NOTE
24+
2225
fn main() {}

src/test/ui/consts/offset_ub.stderr

+16-1
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,20 @@ LL | intrinsics::offset(self, count)
150150
LL | pub const NULL_OFFSET_ZERO: *const u8 = unsafe { ptr::null::<u8>().offset(0) };
151151
| -------------------------------------------------------------------------------
152152

153-
error: aborting due to 10 previous errors
153+
error: any use of this value will cause an error
154+
--> $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
155+
|
156+
LL | intrinsics::offset(self, count)
157+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
158+
| |
159+
| unable to turn bytes into a pointer
160+
| inside `std::ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/libcore/ptr/const_ptr.rs:LL:COL
161+
| inside `UNDERFLOW_ABS` at $DIR/offset_ub.rs:23:47
162+
|
163+
::: $DIR/offset_ub.rs:23:1
164+
|
165+
LL | pub const UNDERFLOW_ABS: *const u8 = unsafe { (usize::MAX as *const u8).offset(isize::MIN) };
166+
| ---------------------------------------------------------------------------------------------
167+
168+
error: aborting due to 11 previous errors
154169

0 commit comments

Comments
 (0)