From 6688a1e486e31721601fac4591339ce9e2393ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20S=C3=A1nchez=20Mu=C3=B1oz?= Date: Sun, 16 Jul 2023 00:23:17 +0200 Subject: [PATCH 1/5] miri: fail when calling a function that requires an unavailable target feature miri will report an UB when calling a function that has a `#[target_feature(enable = ...)]` attribute is called and the required feature is not available. "Available features" are the same that `is_x86_feature_detected!` (or equivalent) reports to be available during miri execution (which can be enabled or disabled with the `-C target-feature` flag). --- tests/fail/function_calls/target_feature.rs | 11 +++++++++++ tests/fail/function_calls/target_feature.stderr | 15 +++++++++++++++ tests/pass/function_calls/target_feature.rs | 12 ++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 tests/fail/function_calls/target_feature.rs create mode 100644 tests/fail/function_calls/target_feature.stderr create mode 100644 tests/pass/function_calls/target_feature.rs diff --git a/tests/fail/function_calls/target_feature.rs b/tests/fail/function_calls/target_feature.rs new file mode 100644 index 0000000000..be95241ea6 --- /dev/null +++ b/tests/fail/function_calls/target_feature.rs @@ -0,0 +1,11 @@ +//@only-target-x86_64: uses x86 target features + +fn main() { + assert!(!is_x86_feature_detected!("ssse3")); + unsafe { + ssse3_fn(); //~ ERROR: calling a function that requires unavailable target features: ssse3 + } +} + +#[target_feature(enable = "ssse3")] +unsafe fn ssse3_fn() {} diff --git a/tests/fail/function_calls/target_feature.stderr b/tests/fail/function_calls/target_feature.stderr new file mode 100644 index 0000000000..bddc6e7e89 --- /dev/null +++ b/tests/fail/function_calls/target_feature.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: calling a function that requires unavailable target features: ssse3 + --> $DIR/target_feature.rs:LL:CC + | +LL | ssse3_fn(); + | ^^^^^^^^^^ calling a function that requires unavailable target features: ssse3 + | + = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior + = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information + = note: BACKTRACE: + = note: inside `main` at $DIR/target_feature.rs:LL:CC + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + diff --git a/tests/pass/function_calls/target_feature.rs b/tests/pass/function_calls/target_feature.rs new file mode 100644 index 0000000000..0be86ba373 --- /dev/null +++ b/tests/pass/function_calls/target_feature.rs @@ -0,0 +1,12 @@ +//@only-target-x86_64: uses x86 target features +//@compile-flags: -C target-feature=+ssse3 + +fn main() { + assert!(is_x86_feature_detected!("ssse3")); + unsafe { + ssse3_fn(); + } +} + +#[target_feature(enable = "ssse3")] +unsafe fn ssse3_fn() {} From 3a2b0aad805158e676bbf78d70f0a74fb937f9d1 Mon Sep 17 00:00:00 2001 From: The Miri Conjob Bot Date: Tue, 18 Jul 2023 06:58:26 +0000 Subject: [PATCH 2/5] Preparing for merge from rustc --- rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-version b/rust-version index 18b17451bb..2cc690989d 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -ffb9b61294b96c389d343a4c55b15400249d74e6 +ec362f0ae8a05618b75727cfdca853540cb2950e From c12e8b5b6ffbee16675b81902994a16742676220 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 18 Jul 2023 11:25:06 +0200 Subject: [PATCH 3/5] make './miri toolchain' work even if we cannot write to rustup dir --- miri | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/miri b/miri index ace3d17ae2..1bc4e254ad 100755 --- a/miri +++ b/miri @@ -97,7 +97,9 @@ toolchain) CUR_COMMIT=$(rustc +miri --version -v 2>/dev/null | grep "^commit-hash: " | cut -d " " -f 2) if [[ "$CUR_COMMIT" == "$NEW_COMMIT" ]]; then echo "miri toolchain is already at commit $CUR_COMMIT." - rustup override set miri + if [[ "$TOOLCHAIN" != "miri" ]]; then + rustup override set miri + fi exit 0 fi # Install and setup new toolchain. From efa610fba70481a22a0c3acdbf4562c204cac10d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 18 Jul 2023 11:25:17 +0200 Subject: [PATCH 4/5] fix clippy warnings --- src/borrow_tracker/mod.rs | 4 ++-- src/borrow_tracker/stacked_borrows/mod.rs | 6 +++--- src/shims/unix/sync.rs | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/borrow_tracker/mod.rs b/src/borrow_tracker/mod.rs index fcfa8f6457..b6dfd9944e 100644 --- a/src/borrow_tracker/mod.rs +++ b/src/borrow_tracker/mod.rs @@ -413,7 +413,7 @@ impl AllocState { alloc_id: AllocId, prov_extra: ProvenanceExtra, range: AllocRange, - machine: &mut MiriMachine<'_, 'tcx>, + machine: &MiriMachine<'_, 'tcx>, ) -> InterpResult<'tcx> { match self { AllocState::StackedBorrows(sb) => @@ -434,7 +434,7 @@ impl AllocState { alloc_id: AllocId, prov_extra: ProvenanceExtra, range: AllocRange, - machine: &mut MiriMachine<'_, 'tcx>, + machine: &MiriMachine<'_, 'tcx>, ) -> InterpResult<'tcx> { match self { AllocState::StackedBorrows(sb) => diff --git a/src/borrow_tracker/stacked_borrows/mod.rs b/src/borrow_tracker/stacked_borrows/mod.rs index 15a7d72edf..7e89d3a0e8 100644 --- a/src/borrow_tracker/stacked_borrows/mod.rs +++ b/src/borrow_tracker/stacked_borrows/mod.rs @@ -244,7 +244,7 @@ impl<'tcx> Stack { fn item_invalidated( item: &Item, global: &GlobalStateInner, - dcx: &mut DiagnosticCx<'_, '_, '_, 'tcx>, + dcx: &DiagnosticCx<'_, '_, '_, 'tcx>, cause: ItemInvalidationCause, ) -> InterpResult<'tcx> { if !global.tracked_pointer_tags.is_empty() { @@ -575,7 +575,7 @@ impl Stacks { alloc_id: AllocId, tag: ProvenanceExtra, range: AllocRange, - machine: &mut MiriMachine<'_, 'tcx>, + machine: &MiriMachine<'_, 'tcx>, ) -> InterpResult<'tcx> { trace!( "write access with tag {:?}: {:?}, size {}", @@ -596,7 +596,7 @@ impl Stacks { alloc_id: AllocId, tag: ProvenanceExtra, range: AllocRange, - machine: &mut MiriMachine<'_, 'tcx>, + machine: &MiriMachine<'_, 'tcx>, ) -> InterpResult<'tcx> { trace!("deallocation with tag {:?}: {:?}, size {}", tag, alloc_id, range.size.bytes()); let dcx = DiagnosticCxBuilder::dealloc(machine, tag); diff --git a/src/shims/unix/sync.rs b/src/shims/unix/sync.rs index 94b831da5c..1fa0ffd8ee 100644 --- a/src/shims/unix/sync.rs +++ b/src/shims/unix/sync.rs @@ -18,14 +18,14 @@ use crate::*; const PTHREAD_MUTEX_NORMAL_FLAG: i32 = 0x8000000; fn is_mutex_kind_default<'mir, 'tcx: 'mir>( - ecx: &mut MiriInterpCx<'mir, 'tcx>, + ecx: &MiriInterpCx<'mir, 'tcx>, kind: i32, ) -> InterpResult<'tcx, bool> { Ok(kind == ecx.eval_libc_i32("PTHREAD_MUTEX_DEFAULT")) } fn is_mutex_kind_normal<'mir, 'tcx: 'mir>( - ecx: &mut MiriInterpCx<'mir, 'tcx>, + ecx: &MiriInterpCx<'mir, 'tcx>, kind: i32, ) -> InterpResult<'tcx, bool> { let mutex_normal_kind = ecx.eval_libc_i32("PTHREAD_MUTEX_NORMAL"); From 0bb86034aa25a9591e3c0e039d98d008538d0e45 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 18 Jul 2023 11:26:25 +0200 Subject: [PATCH 5/5] ignore test on apple --- tests/fail/function_calls/target_feature.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/fail/function_calls/target_feature.rs b/tests/fail/function_calls/target_feature.rs index be95241ea6..84e01eb480 100644 --- a/tests/fail/function_calls/target_feature.rs +++ b/tests/fail/function_calls/target_feature.rs @@ -1,4 +1,5 @@ //@only-target-x86_64: uses x86 target features +//@ignore-target-x86_64-apple-darwin: that target actually has ssse3 fn main() { assert!(!is_x86_feature_detected!("ssse3"));