Skip to content

Commit a725a15

Browse files
committed
Miri core engine: use throw_ub instead of throw_panic
1 parent 4007d4e commit a725a15

21 files changed

+84
-273
lines changed

src/librustc/mir/interpret/error.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ pub enum UndefinedBehaviorInfo {
371371
Unreachable,
372372
/// An enum discriminant was set to a value which was outside the range of valid values.
373373
InvalidDiscriminant(ScalarMaybeUndef),
374+
/// A slice/array index projection went out-of-bounds.
375+
BoundsCheckFailed { len: u64, index: u64 },
376+
/// Something was divided by 0 (x / 0).
377+
DivisionByZero,
378+
/// Something was "remainded" by 0 (x % 0).
379+
RemainderByZero,
380+
/// Overflowing inbounds pointer arithmetic.
381+
PointerArithOverflow,
374382
}
375383

376384
impl fmt::Debug for UndefinedBehaviorInfo {
@@ -380,9 +388,18 @@ impl fmt::Debug for UndefinedBehaviorInfo {
380388
Ub(msg) | UbExperimental(msg) =>
381389
write!(f, "{}", msg),
382390
Unreachable =>
383-
write!(f, "entered unreachable code"),
391+
write!(f, "entering unreachable code"),
384392
InvalidDiscriminant(val) =>
385-
write!(f, "encountered invalid enum discriminant {}", val),
393+
write!(f, "encountering invalid enum discriminant {}", val),
394+
BoundsCheckFailed { ref len, ref index } =>
395+
write!(f, "indexing out of bounds: the len is {:?} but the index is {:?}",
396+
len, index),
397+
DivisionByZero =>
398+
write!(f, "dividing by zero"),
399+
RemainderByZero =>
400+
write!(f, "calculating the remainder with a divisor of zero"),
401+
PointerArithOverflow =>
402+
write!(f, "overflowing in-bounds pointer arithmetic"),
386403
}
387404
}
388405
}

src/librustc/mir/interpret/pointer.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{AllocId, InterpResult};
22

3-
use crate::mir;
43
use crate::ty::layout::{self, HasDataLayout, Size};
54

65
use rustc_macros::HashStable;
@@ -88,13 +87,13 @@ pub trait PointerArithmetic: layout::HasDataLayout {
8887
#[inline]
8988
fn offset<'tcx>(&self, val: u64, i: u64) -> InterpResult<'tcx, u64> {
9089
let (res, over) = self.overflowing_offset(val, i);
91-
if over { throw_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
90+
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
9291
}
9392

9493
#[inline]
9594
fn signed_offset<'tcx>(&self, val: u64, i: i64) -> InterpResult<'tcx, u64> {
9695
let (res, over) = self.overflowing_signed_offset(val, i128::from(i));
97-
if over { throw_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
96+
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
9897
}
9998
}
10099

src/librustc_mir/interpret/operator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
177177
return Ok((Scalar::from_bool(op(&l, &r)), false, self.tcx.types.bool));
178178
}
179179
let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
180-
Div if r == 0 => throw_panic!(DivisionByZero),
181-
Rem if r == 0 => throw_panic!(RemainderByZero),
180+
Div if r == 0 => throw_ub!(DivisionByZero),
181+
Rem if r == 0 => throw_ub!(RemainderByZero),
182182
Div => Some(i128::overflowing_div),
183183
Rem => Some(i128::overflowing_rem),
184184
Add => Some(i128::overflowing_add),
@@ -234,8 +234,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
234234
Add => u128::overflowing_add,
235235
Sub => u128::overflowing_sub,
236236
Mul => u128::overflowing_mul,
237-
Div if r == 0 => throw_panic!(DivisionByZero),
238-
Rem if r == 0 => throw_panic!(RemainderByZero),
237+
Div if r == 0 => throw_ub!(DivisionByZero),
238+
Rem if r == 0 => throw_ub!(RemainderByZero),
239239
Div => u128::overflowing_div,
240240
Rem => u128::overflowing_rem,
241241
_ => bug!(),

src/librustc_mir/interpret/place.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,8 @@ where
384384
layout::FieldPlacement::Array { stride, .. } => {
385385
let len = base.len(self)?;
386386
if field >= len {
387-
// This can be violated because the index (field) can be a runtime value
388-
// provided by the user.
389-
debug!("tried to access element {} of array/slice with length {}", field, len);
390-
throw_panic!(BoundsCheck { len, index: field });
387+
// This can only be reached in ConstProp and non-rustc-MIR.
388+
throw_ub!(BoundsCheckFailed { len, index: field });
391389
}
392390
stride * field
393391
}

src/test/ui/consts/array-literal-index-oob.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error: reaching this expression at runtime will panic or abort
1212
LL | &{[1, 2, 3][4]};
1313
| --^^^^^^^^^^^^-
1414
| |
15-
| index out of bounds: the len is 3 but the index is 4
15+
| indexing out of bounds: the len is 3 but the index is 4
1616

1717
error: aborting due to 2 previous errors
1818

src/test/ui/consts/const-err2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fn main() {
2323
//~^ ERROR const_err
2424
let _e = [5u8][1];
2525
//~^ ERROR index out of bounds
26-
//~| ERROR this expression will panic at runtime
2726
black_box(a);
2827
black_box(b);
2928
black_box(c);

src/test/ui/consts/const-err2.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,5 @@ error: index out of bounds: the len is 1 but the index is 1
3434
LL | let _e = [5u8][1];
3535
| ^^^^^^^^
3636

37-
error: this expression will panic at runtime
38-
--> $DIR/const-err2.rs:24:14
39-
|
40-
LL | let _e = [5u8][1];
41-
| ^^^^^^^^ index out of bounds: the len is 1 but the index is 1
42-
43-
error: aborting due to 6 previous errors
37+
error: aborting due to 5 previous errors
4438

src/test/ui/consts/const-err3.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fn main() {
2323
//~^ ERROR const_err
2424
let _e = [5u8][1];
2525
//~^ ERROR const_err
26-
//~| ERROR this expression will panic at runtime
2726
black_box(a);
2827
black_box(b);
2928
black_box(c);

src/test/ui/consts/const-err3.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,5 @@ error: index out of bounds: the len is 1 but the index is 1
3434
LL | let _e = [5u8][1];
3535
| ^^^^^^^^
3636

37-
error: this expression will panic at runtime
38-
--> $DIR/const-err3.rs:24:14
39-
|
40-
LL | let _e = [5u8][1];
41-
| ^^^^^^^^ index out of bounds: the len is 1 but the index is 1
42-
43-
error: aborting due to 6 previous errors
37+
error: aborting due to 5 previous errors
4438

src/test/ui/consts/const-eval/promoted_errors.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ fn main() {
88
//~^ ERROR const_err
99
println!("{}", 1/(1-1));
1010
//~^ ERROR attempt to divide by zero [const_err]
11-
//~| ERROR reaching this expression at runtime will panic or abort [const_err]
11+
//~| ERROR const_err
1212
let _x = 1/(1-1);
1313
//~^ ERROR const_err
14-
//~| ERROR const_err
1514
println!("{}", 1/(false as u32));
1615
//~^ ERROR attempt to divide by zero [const_err]
17-
//~| ERROR reaching this expression at runtime will panic or abort [const_err]
16+
//~| ERROR const_err
1817
let _x = 1/(false as u32);
1918
//~^ ERROR const_err
20-
//~| ERROR const_err
2119
}

0 commit comments

Comments
 (0)