From d81651e8e9af5c8c40a4bd9b6b7becf12486bdad Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Thu, 17 May 2018 12:07:00 +0200 Subject: [PATCH 1/3] Release mode overflows should not cause const eval to error --- src/librustc_mir/interpret/eval_context.rs | 12 ++------ src/test/run-fail/promoted_div_by_zero.rs | 17 +++++++++++ src/test/run-fail/promoted_overflow.rs | 17 +++++++++++ src/test/run-pass/promoted_overflow_opt.rs | 18 ++++++++++++ src/test/ui/const-eval/promoted_errors.rs | 2 -- src/test/ui/const-eval/promoted_errors.stderr | 28 ++++++------------- 6 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 src/test/run-fail/promoted_div_by_zero.rs create mode 100644 src/test/run-fail/promoted_overflow.rs create mode 100644 src/test/run-pass/promoted_overflow_opt.rs diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 03137619edaf4..067f9ce0248f8 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -522,21 +522,13 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M BinaryOp(bin_op, ref left, ref right) => { let left = self.eval_operand(left)?; let right = self.eval_operand(right)?; - if self.intrinsic_overflowing( + self.intrinsic_overflowing( bin_op, left, right, dest, dest_ty, - )? - { - // There was an overflow in an unchecked binop. Right now, we consider this an error and bail out. - // The rationale is that the reason rustc emits unchecked binops in release mode (vs. the checked binops - // it emits in debug mode) is performance, but it doesn't cost us any performance in miri. - // If, however, the compiler ever starts transforming unchecked intrinsics into unchecked binops, - // we have to go back to just ignoring the overflow here. - return err!(Overflow(bin_op)); - } + )?; } CheckedBinaryOp(bin_op, ref left, ref right) => { diff --git a/src/test/run-fail/promoted_div_by_zero.rs b/src/test/run-fail/promoted_div_by_zero.rs new file mode 100644 index 0000000000000..385fd5092328b --- /dev/null +++ b/src/test/run-fail/promoted_div_by_zero.rs @@ -0,0 +1,17 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(const_err)] + +// error-pattern: attempt to divide by zero + +fn main() { + let x = &(1 / (1 - 1)); +} diff --git a/src/test/run-fail/promoted_overflow.rs b/src/test/run-fail/promoted_overflow.rs new file mode 100644 index 0000000000000..5169dcbf5f4ec --- /dev/null +++ b/src/test/run-fail/promoted_overflow.rs @@ -0,0 +1,17 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(const_err)] + +// error-pattern: overflow + +fn main() { + let x: &'static u32 = &(0u32 - 1); +} diff --git a/src/test/run-pass/promoted_overflow_opt.rs b/src/test/run-pass/promoted_overflow_opt.rs new file mode 100644 index 0000000000000..6b2f1c6d3c8a2 --- /dev/null +++ b/src/test/run-pass/promoted_overflow_opt.rs @@ -0,0 +1,18 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(const_err)] + +// compile-flags: -O + +fn main() { + let x = &(0u32 - 1); + assert_eq!(*x, u32::max_value()) +} diff --git a/src/test/ui/const-eval/promoted_errors.rs b/src/test/ui/const-eval/promoted_errors.rs index 7385860abae28..a39afb9bdd4a4 100644 --- a/src/test/ui/const-eval/promoted_errors.rs +++ b/src/test/ui/const-eval/promoted_errors.rs @@ -14,8 +14,6 @@ // compile-flags: -O fn main() { println!("{}", 0u32 - 1); - //~^ WARN const_err - //~| WARN const_err let _x = 0u32 - 1; //~^ WARN const_err println!("{}", 1/(1-1)); diff --git a/src/test/ui/const-eval/promoted_errors.stderr b/src/test/ui/const-eval/promoted_errors.stderr index 8e9a0ea43a43b..683ee5375fbf6 100644 --- a/src/test/ui/const-eval/promoted_errors.stderr +++ b/src/test/ui/const-eval/promoted_errors.stderr @@ -1,8 +1,8 @@ warning: constant evaluation error - --> $DIR/promoted_errors.rs:16:20 + --> $DIR/promoted_errors.rs:17:14 | -LL | println!("{}", 0u32 - 1); - | ^^^^^^^^ attempt to subtract with overflow +LL | let _x = 0u32 - 1; + | ^^^^^^^^ attempt to subtract with overflow | note: lint level defined here --> $DIR/promoted_errors.rs:11:9 @@ -10,44 +10,32 @@ note: lint level defined here LL | #![warn(const_err)] | ^^^^^^^^^ -warning: constant evaluation error - --> $DIR/promoted_errors.rs:16:20 - | -LL | println!("{}", 0u32 - 1); - | ^^^^^^^^ attempt to subtract with overflow - -warning: constant evaluation error - --> $DIR/promoted_errors.rs:19:14 - | -LL | let _x = 0u32 - 1; - | ^^^^^^^^ attempt to subtract with overflow - warning: attempt to divide by zero - --> $DIR/promoted_errors.rs:21:20 + --> $DIR/promoted_errors.rs:19:20 | LL | println!("{}", 1/(1-1)); | ^^^^^^^ warning: constant evaluation error - --> $DIR/promoted_errors.rs:21:20 + --> $DIR/promoted_errors.rs:19:20 | LL | println!("{}", 1/(1-1)); | ^^^^^^^ attempt to divide by zero warning: attempt to divide by zero - --> $DIR/promoted_errors.rs:24:14 + --> $DIR/promoted_errors.rs:22:14 | LL | let _x = 1/(1-1); | ^^^^^^^ warning: constant evaluation error - --> $DIR/promoted_errors.rs:24:14 + --> $DIR/promoted_errors.rs:22:14 | LL | let _x = 1/(1-1); | ^^^^^^^ attempt to divide by zero warning: constant evaluation error - --> $DIR/promoted_errors.rs:27:20 + --> $DIR/promoted_errors.rs:25:20 | LL | println!("{}", 1/(false as u32)); | ^^^^^^^^^^^^^^^^ attempt to divide by zero From 27e710f55744534d6b0d54f507eea87bdbfe1916 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 18 May 2018 09:41:24 +0200 Subject: [PATCH 2/3] Add a test showing the erroneous promoted bug --- src/librustc_codegen_llvm/mir/operand.rs | 4 +- .../ui/const-eval/promoted_const_fn_fail.rs | 38 +++++++++++++++++++ .../const-eval/promoted_const_fn_fail.stderr | 31 +++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/const-eval/promoted_const_fn_fail.rs create mode 100644 src/test/ui/const-eval/promoted_const_fn_fail.stderr diff --git a/src/librustc_codegen_llvm/mir/operand.rs b/src/librustc_codegen_llvm/mir/operand.rs index 62ef58f825504..caa67ef01c2ba 100644 --- a/src/librustc_codegen_llvm/mir/operand.rs +++ b/src/librustc_codegen_llvm/mir/operand.rs @@ -407,10 +407,10 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> { .unwrap_or_else(|err| { match constant.literal { mir::Literal::Promoted { .. } => { - // don't report errors inside promoteds, just warnings. + // FIXME: generate a panic here }, mir::Literal::Value { .. } => { - err.report(bx.tcx(), constant.span, "const operand") + err.report(bx.tcx(), constant.span, "const operand"); }, } // We've errored, so we don't have to produce working code. diff --git a/src/test/ui/const-eval/promoted_const_fn_fail.rs b/src/test/ui/const-eval/promoted_const_fn_fail.rs new file mode 100644 index 0000000000000..5ced2c9dd8f59 --- /dev/null +++ b/src/test/ui/const-eval/promoted_const_fn_fail.rs @@ -0,0 +1,38 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(const_fn)] + +#![deny(const_err)] + +union Bar { + a: &'static u8, + b: usize, +} + +const fn bar() -> u8 { + unsafe { + // this will error as long as this test + // is run on a system whose pointers need more + // than 8 bits + Bar { a: &42 }.b as u8 + //~^ constant evaluation error + //~| constant evaluation error + } +} + +fn main() { + // FIXME(oli-obk): this should compile but panic at runtime + // if we change the `const_err` lint to allow this will actually compile, but then + // continue with undefined values. + let x: &'static u8 = &(bar() + 1); + let y = *x; + unreachable!(); +} diff --git a/src/test/ui/const-eval/promoted_const_fn_fail.stderr b/src/test/ui/const-eval/promoted_const_fn_fail.stderr new file mode 100644 index 0000000000000..f910705bb7b3c --- /dev/null +++ b/src/test/ui/const-eval/promoted_const_fn_fail.stderr @@ -0,0 +1,31 @@ +error: constant evaluation error + --> $DIR/promoted_const_fn_fail.rs:25:9 + | +LL | Bar { a: &42 }.b as u8 + | ^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes + | +note: lint level defined here + --> $DIR/promoted_const_fn_fail.rs:13:9 + | +LL | #![deny(const_err)] + | ^^^^^^^^^ +note: inside call to `bar` + --> $DIR/promoted_const_fn_fail.rs:35:28 + | +LL | let x: &'static u8 = &(bar() + 1); + | ^^^^^ + +error: constant evaluation error + --> $DIR/promoted_const_fn_fail.rs:25:9 + | +LL | Bar { a: &42 }.b as u8 + | ^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes + | +note: inside call to `bar` + --> $DIR/promoted_const_fn_fail.rs:35:28 + | +LL | let x: &'static u8 = &(bar() + 1); + | ^^^^^ + +error: aborting due to 2 previous errors + From 8753d0f12f8fa44b5ace568c03b9bf59f7863e25 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 18 May 2018 10:59:44 +0200 Subject: [PATCH 3/3] Overflows only panic in debug mode --- src/test/run-fail/promoted_overflow.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/run-fail/promoted_overflow.rs b/src/test/run-fail/promoted_overflow.rs index 5169dcbf5f4ec..0e1b0117a8c52 100644 --- a/src/test/run-fail/promoted_overflow.rs +++ b/src/test/run-fail/promoted_overflow.rs @@ -11,6 +11,7 @@ #![allow(const_err)] // error-pattern: overflow +// compile-flags: -C overflow-checks=yes fn main() { let x: &'static u32 = &(0u32 - 1);