about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-08-26 17:01:46 +0200
committerRalf Jung <post@ralfj.de>2024-08-26 17:08:52 +0200
commit7a290fce907da1cda0687fe5e48372c08fe40ab8 (patch)
treeead12271778144c209ce507f29379eb1cd4e5aa9 /src
parent22572d0994593197593e2a1b7b18d720a9a349a7 (diff)
interpret: do not make const-eval query result depend on tcx.sess
Diffstat (limited to 'src')
-rw-r--r--src/tools/miri/src/machine.rs42
-rw-r--r--src/tools/miri/tests/panic/target_feature_wasm.rs (renamed from src/tools/miri/tests/pass/function_calls/target_feature_wasm.rs)4
2 files changed, 40 insertions, 6 deletions
diff --git a/src/tools/miri/src/machine.rs b/src/tools/miri/src/machine.rs
index a7493d48d6a..8d4866517f2 100644
--- a/src/tools/miri/src/machine.rs
+++ b/src/tools/miri/src/machine.rs
@@ -947,15 +947,47 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
     }
 
     #[inline(always)]
-    fn enforce_abi(_ecx: &MiriInterpCx<'tcx>) -> bool {
-        true
-    }
-
-    #[inline(always)]
     fn ignore_optional_overflow_checks(ecx: &MiriInterpCx<'tcx>) -> bool {
         !ecx.tcx.sess.overflow_checks()
     }
 
+    fn check_fn_target_features(
+        ecx: &MiriInterpCx<'tcx>,
+        instance: ty::Instance<'tcx>,
+    ) -> InterpResult<'tcx> {
+        let attrs = ecx.tcx.codegen_fn_attrs(instance.def_id());
+        if attrs
+            .target_features
+            .iter()
+            .any(|feature| !ecx.tcx.sess.target_features.contains(&feature.name))
+        {
+            let unavailable = attrs
+                .target_features
+                .iter()
+                .filter(|&feature| {
+                    !feature.implied && !ecx.tcx.sess.target_features.contains(&feature.name)
+                })
+                .fold(String::new(), |mut s, feature| {
+                    if !s.is_empty() {
+                        s.push_str(", ");
+                    }
+                    s.push_str(feature.name.as_str());
+                    s
+                });
+            let msg = format!(
+                "calling a function that requires unavailable target features: {unavailable}"
+            );
+            // On WASM, this is not UB, but instead gets rejected during validation of the module
+            // (see #84988).
+            if ecx.tcx.sess.target.is_like_wasm {
+                throw_machine_stop!(TerminationInfo::Abort(msg));
+            } else {
+                throw_ub_format!("{msg}");
+            }
+        }
+        Ok(())
+    }
+
     #[inline(always)]
     fn find_mir_or_eval_fn(
         ecx: &mut MiriInterpCx<'tcx>,
diff --git a/src/tools/miri/tests/pass/function_calls/target_feature_wasm.rs b/src/tools/miri/tests/panic/target_feature_wasm.rs
index 5056f32de44..c67d2983f78 100644
--- a/src/tools/miri/tests/pass/function_calls/target_feature_wasm.rs
+++ b/src/tools/miri/tests/panic/target_feature_wasm.rs
@@ -2,7 +2,9 @@
 //@compile-flags: -C target-feature=-simd128
 
 fn main() {
-    // Calling functions with `#[target_feature]` is not unsound on WASM, see #84988
+    // Calling functions with `#[target_feature]` is not unsound on WASM, see #84988.
+    // But if the compiler actually uses the target feature, it will lead to an error when the module is loaded.
+    // We emulate this with an "unsupported" error.
     assert!(!cfg!(target_feature = "simd128"));
     simd128_fn();
 }