test exact_div UB detection
This commit is contained in:
parent
14c41925c6
commit
fc90124489
@ -272,7 +272,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
||||
let b = this.read_immediate(args[1])?;
|
||||
// check x % y != 0
|
||||
if this.binary_op(mir::BinOp::Rem, a, b)?.0.to_bits(dest.layout.size)? != 0 {
|
||||
return err!(ValidationFailure(format!("exact_div: {:?} cannot be divided by {:?}", a, b)));
|
||||
// Check if `b` is -1, which is the "min_value / -1" case.
|
||||
let minus1 = Scalar::from_int(-1, dest.layout.size);
|
||||
return if b.to_scalar().unwrap() == minus1 {
|
||||
err!(Intrinsic(format!("exact_div: result of dividing MIN by -1 cannot be represented")))
|
||||
} else {
|
||||
err!(Intrinsic(format!("exact_div: {:?} cannot be divided by {:?} without remainder", *a, *b)))
|
||||
};
|
||||
}
|
||||
this.binop_ignore_overflow(mir::BinOp::Div, a, b, dest)?;
|
||||
},
|
||||
|
5
tests/compile-fail/exact_div1.rs
Normal file
5
tests/compile-fail/exact_div1.rs
Normal file
@ -0,0 +1,5 @@
|
||||
#![feature(core_intrinsics)]
|
||||
fn main() {
|
||||
// divison by 0
|
||||
unsafe { std::intrinsics::exact_div(2, 0); } //~ ERROR divisor of zero
|
||||
}
|
5
tests/compile-fail/exact_div2.rs
Normal file
5
tests/compile-fail/exact_div2.rs
Normal file
@ -0,0 +1,5 @@
|
||||
#![feature(core_intrinsics)]
|
||||
fn main() {
|
||||
// divison with a remainder
|
||||
unsafe { std::intrinsics::exact_div(2u16, 3); } //~ ERROR Scalar(0x0002) cannot be divided by Scalar(0x0003) without remainder
|
||||
}
|
5
tests/compile-fail/exact_div3.rs
Normal file
5
tests/compile-fail/exact_div3.rs
Normal file
@ -0,0 +1,5 @@
|
||||
#![feature(core_intrinsics)]
|
||||
fn main() {
|
||||
// signed divison with a remainder
|
||||
unsafe { std::intrinsics::exact_div(-19i8, 2); } //~ ERROR Scalar(0xed) cannot be divided by Scalar(0x02) without remainder
|
||||
}
|
5
tests/compile-fail/exact_div4.rs
Normal file
5
tests/compile-fail/exact_div4.rs
Normal file
@ -0,0 +1,5 @@
|
||||
#![feature(core_intrinsics)]
|
||||
fn main() {
|
||||
// divison of min_value by -1
|
||||
unsafe { std::intrinsics::exact_div(i64::min_value(), -1); } //~ ERROR result of dividing MIN by -1 cannot be represented
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user