Skip to content

Commit b5fde0d

Browse files
committed
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).
1 parent ad96323 commit b5fde0d

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

compiler/rustc_const_eval/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ const_eval_unallowed_mutable_refs_raw =
399399
const_eval_unallowed_op_in_const_context =
400400
{$msg}
401401
402+
const_eval_unavailable_target_features_for_fn =
403+
calling a function that requires unavailable target features: {$unavailable_feats}
404+
402405
const_eval_undefined_behavior =
403406
it is undefined behavior to use this value
404407

compiler/rustc_const_eval/src/interpret/terminator.rs

+19
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,25 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
503503
}
504504
}
505505

506+
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
507+
let missing_features: Vec<_> = attrs
508+
.target_features
509+
.iter()
510+
.copied()
511+
.filter(|feature| !self.tcx.sess.target_features.contains(feature))
512+
.collect();
513+
if !missing_features.is_empty() {
514+
let mut missing_features_str = String::from(missing_features[0].as_str());
515+
for missing_feature in missing_features[1..].iter() {
516+
missing_features_str.push(',');
517+
missing_features_str.push_str(missing_feature.as_str());
518+
}
519+
throw_ub_custom!(
520+
fluent::const_eval_unavailable_target_features_for_fn,
521+
unavailable_feats = missing_features_str,
522+
);
523+
}
524+
506525
if !callee_fn_abi.can_unwind {
507526
// The callee cannot unwind, so force the `Unreachable` unwind handling.
508527
unwind = mir::UnwindAction::Unreachable;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@only-target-x86_64: uses x86 target features
2+
3+
fn main() {
4+
assert!(!is_x86_feature_detected!("ssse3"));
5+
unsafe {
6+
ssse3_fn(); //~ ERROR: calling a function that requires unavailable target features: ssse3
7+
}
8+
}
9+
10+
#[target_feature(enable = "ssse3")]
11+
unsafe fn ssse3_fn() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: calling a function that requires unavailable target features: ssse3
2+
--> $DIR/target_feature.rs:LL:CC
3+
|
4+
LL | ssse3_fn();
5+
| ^^^^^^^^^^ calling a function that requires unavailable target features: ssse3
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/target_feature.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to previous error
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@only-target-x86_64: uses x86 target features
2+
//@compile-flags: -C target-feature=+ssse3
3+
4+
fn main() {
5+
assert!(is_x86_feature_detected!("ssse3"));
6+
unsafe {
7+
ssse3_fn();
8+
}
9+
}
10+
11+
#[target_feature(enable = "ssse3")]
12+
unsafe fn ssse3_fn() {}

0 commit comments

Comments
 (0)