From 210f18d6c7962ae5125947e89d38cac34c65b6a8 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 7 Sep 2020 10:24:04 +0200 Subject: [PATCH 1/5] work around rustc optimizations becoming too smart --- ci.sh | 2 +- rust-version | 2 +- tests/compile-fail/invalid_bool.rs | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ci.sh b/ci.sh index c2c36e1efc..a1d0a38c4f 100755 --- a/ci.sh +++ b/ci.sh @@ -25,7 +25,7 @@ function run_tests { ./miri test --locked if ! [ -n "${MIRI_TEST_TARGET+exists}" ]; then # Only for host architecture: tests with MIR optimizations - # FIXME:only testing level 1 because of . + # FIXME: only testing level 1 because of . MIRI_TEST_FLAGS="-Z mir-opt-level=1" ./miri test --locked fi # "miri test" has built the sysroot for us, now this should pass without diff --git a/rust-version b/rust-version index 797f6e825a..d2cf18f80f 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -d2454643e137bde519786ee9e650c455d7ad6f34 +e114d6228b948ce056de0bcdec2603c8e89d3727 diff --git a/tests/compile-fail/invalid_bool.rs b/tests/compile-fail/invalid_bool.rs index 6dee9ec3c9..933ee91c7d 100644 --- a/tests/compile-fail/invalid_bool.rs +++ b/tests/compile-fail/invalid_bool.rs @@ -2,7 +2,9 @@ // Make sure we find these even with many checks disabled. // compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation +fn dont_optimize(x: T) -> T { x } + fn main() { let b = unsafe { std::mem::transmute::(2) }; - let _x = b == true; //~ ERROR interpreting an invalid 8-bit value as a bool: 0x02 + let _x = b == dont_optimize(true); //~ ERROR interpreting an invalid 8-bit value as a bool: 0x02 } From d9bc19a7b078e75052167496fff3c4d91c7bbd66 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 7 Sep 2020 10:35:39 +0200 Subject: [PATCH 2/5] test opt-level 2 --- ci.sh | 4 ++-- tests/run-pass/float.rs | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ci.sh b/ci.sh index a1d0a38c4f..915a4cf2fd 100755 --- a/ci.sh +++ b/ci.sh @@ -25,8 +25,8 @@ function run_tests { ./miri test --locked if ! [ -n "${MIRI_TEST_TARGET+exists}" ]; then # Only for host architecture: tests with MIR optimizations - # FIXME: only testing level 1 because of . - MIRI_TEST_FLAGS="-Z mir-opt-level=1" ./miri test --locked + # FIXME: only testing level 2 because of . + MIRI_TEST_FLAGS="-Z mir-opt-level=2" ./miri test --locked fi # "miri test" has built the sysroot for us, now this should pass without # any interactive questions. diff --git a/tests/run-pass/float.rs b/tests/run-pass/float.rs index 0b89f11b06..ea6269c22f 100644 --- a/tests/run-pass/float.rs +++ b/tests/run-pass/float.rs @@ -1,3 +1,5 @@ +// compile-flags: -Zmir-opt-level=0 +// FIXME: Using opt-level 2 here makes the test take forever (https://github.com/rust-lang/rust/issues/76433). #![feature(stmt_expr_attributes)] use std::fmt::Debug; From 088af66f85ef5e885202ad258670936f8edc3c53 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 7 Sep 2020 11:16:16 +0200 Subject: [PATCH 3/5] better optimization suppression Co-authored-by: bjorn3 --- tests/compile-fail/invalid_bool.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/compile-fail/invalid_bool.rs b/tests/compile-fail/invalid_bool.rs index 933ee91c7d..8d8cce1c7e 100644 --- a/tests/compile-fail/invalid_bool.rs +++ b/tests/compile-fail/invalid_bool.rs @@ -2,6 +2,7 @@ // Make sure we find these even with many checks disabled. // compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation +#[inline(never)] fn dont_optimize(x: T) -> T { x } fn main() { From 029c851d7c0eee03178c96c890ec974262b2ee21 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 7 Sep 2020 11:22:49 +0200 Subject: [PATCH 4/5] another optimization work-around --- tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs index 169e98abf3..a9db5ff7df 100644 --- a/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs @@ -2,8 +2,8 @@ // compile-flags: -Zmiri-disable-validation fn main() { - for _ in 0..10 { // Try many times as this might work by chance. - let x = 2u8; + for i in 0..10 { // Try many times as this might work by chance. + let x = i as u8; let x = &x as *const _ as *const [u32; 0]; // This must fail because alignment is violated. Test specifically for loading ZST. let _x = unsafe { *x }; //~ERROR alignment 4 is required From 3ba1035d279e0acfc3aaf68e8bd055f2d1ffb205 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 7 Sep 2020 11:26:24 +0200 Subject: [PATCH 5/5] use standard black_box function --- tests/compile-fail/invalid_bool.rs | 6 ++---- tests/run-pass/float.rs | 7 ++----- tests/run-pass/u128.rs | 3 ++- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/compile-fail/invalid_bool.rs b/tests/compile-fail/invalid_bool.rs index 8d8cce1c7e..796d8220dc 100644 --- a/tests/compile-fail/invalid_bool.rs +++ b/tests/compile-fail/invalid_bool.rs @@ -1,11 +1,9 @@ // Validation makes this fail in the wrong place // Make sure we find these even with many checks disabled. // compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation - -#[inline(never)] -fn dont_optimize(x: T) -> T { x } +#![feature(test)] fn main() { let b = unsafe { std::mem::transmute::(2) }; - let _x = b == dont_optimize(true); //~ ERROR interpreting an invalid 8-bit value as a bool: 0x02 + let _x = b == std::hint::black_box(true); //~ ERROR interpreting an invalid 8-bit value as a bool: 0x02 } diff --git a/tests/run-pass/float.rs b/tests/run-pass/float.rs index ea6269c22f..327ea17731 100644 --- a/tests/run-pass/float.rs +++ b/tests/run-pass/float.rs @@ -1,7 +1,8 @@ // compile-flags: -Zmir-opt-level=0 // FIXME: Using opt-level 2 here makes the test take forever (https://github.com/rust-lang/rust/issues/76433). -#![feature(stmt_expr_attributes)] +#![feature(stmt_expr_attributes, test)] use std::fmt::Debug; +use std::hint::black_box; // Helper function to avoid promotion so that this tests "run-time" casts, not CTFE. // Doesn't make a big difference when running this in Miri, but it means we can compare this @@ -341,10 +342,6 @@ fn ops() { /// Tests taken from rustc test suite. /// -// Poor-man's black-box -#[inline(never)] -fn black_box(x: T) -> T { x } - macro_rules! test { ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ( // black_box disables constant evaluation to test run-time conversions: diff --git a/tests/run-pass/u128.rs b/tests/run-pass/u128.rs index a2ca7746b1..bbc667c5dd 100644 --- a/tests/run-pass/u128.rs +++ b/tests/run-pass/u128.rs @@ -1,4 +1,5 @@ -fn b(t: T) -> T { t } +#![feature(test)] +use std::hint::black_box as b; fn main() { let x: u128 = 0xFFFF_FFFF_FFFF_FFFF__FFFF_FFFF_FFFF_FFFF;