From 7a39460e2488fe654b0b6f370e652a42c600e53e Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Wed, 25 Sep 2019 01:19:41 +0200 Subject: [PATCH 1/4] Use the upstream `exact_div` implementation --- src/shims/intrinsics.rs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/shims/intrinsics.rs b/src/shims/intrinsics.rs index 9e47c095d6..0c6e5f0af6 100644 --- a/src/shims/intrinsics.rs +++ b/src/shims/intrinsics.rs @@ -313,23 +313,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx this.write_scalar(Scalar::from_f64(res), dest)?; } - "exact_div" => { - // Performs an exact division, resulting in undefined behavior where - // `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1` - let a = this.read_immediate(args[0])?; - let b = this.read_immediate(args[1])?; - // check x % y != 0 - if this.overflowing_binary_op(mir::BinOp::Rem, a, b)?.0.to_bits(dest.layout.size)? != 0 { - // Check if `b` is -1, which is the "min_value / -1" case. - let minus1 = Scalar::from_int(-1, dest.layout.size); - return Err(if b.to_scalar().unwrap() == minus1 { - err_ub_format!("exact_div: result of dividing MIN by -1 cannot be represented") - } else { - err_ub_format!("exact_div: {:?} cannot be divided by {:?} without remainder", *a, *b) - }.into()); - } - this.binop_ignore_overflow(mir::BinOp::Div, a, b, dest)?; - }, + "exact_div" => this.exact_div( + this.read_immediate(args[0])?, + this.read_immediate(args[1])?, + dest, + )?, "forget" => {} From 47bfd84c982b43a3e7458a38bd1c432c195f352f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 3 Nov 2019 10:02:32 +0100 Subject: [PATCH 2/4] rustup --- rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-version b/rust-version index 8fb4c09873..1a58c80128 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -aa69777ea2902208b24b3fd77767d577ceaf6386 +6c1b220fd747bf244f04b380e4d4ae005068f706 From 5a5fa1538229b0304b74ae961af157a7d4aed7d3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 3 Nov 2019 10:03:30 +0100 Subject: [PATCH 3/4] style --- src/shims/intrinsics.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/shims/intrinsics.rs b/src/shims/intrinsics.rs index 0c6e5f0af6..cdd31c9298 100644 --- a/src/shims/intrinsics.rs +++ b/src/shims/intrinsics.rs @@ -313,11 +313,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx this.write_scalar(Scalar::from_f64(res), dest)?; } - "exact_div" => this.exact_div( - this.read_immediate(args[0])?, - this.read_immediate(args[1])?, - dest, - )?, + "exact_div" => + this.exact_div( + this.read_immediate(args[0])?, + this.read_immediate(args[1])?, + dest, + )?, "forget" => {} From fcf0f886d46050ecbb64c068d62725b1b4ef1aca Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 3 Nov 2019 10:15:55 +0100 Subject: [PATCH 4/4] adjust tests --- tests/compile-fail/exact_div2.rs | 2 +- tests/compile-fail/exact_div3.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/compile-fail/exact_div2.rs b/tests/compile-fail/exact_div2.rs index 22bcf027dd..f4400d0d8a 100644 --- a/tests/compile-fail/exact_div2.rs +++ b/tests/compile-fail/exact_div2.rs @@ -1,5 +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 + unsafe { std::intrinsics::exact_div(2u16, 3); } //~ ERROR 2 cannot be divided by 3 without remainder } diff --git a/tests/compile-fail/exact_div3.rs b/tests/compile-fail/exact_div3.rs index 2db62e0092..0460e412e4 100644 --- a/tests/compile-fail/exact_div3.rs +++ b/tests/compile-fail/exact_div3.rs @@ -1,5 +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 + unsafe { std::intrinsics::exact_div(-19i8, 2); } //~ ERROR 237 cannot be divided by 2 without remainder }