Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_const_eval/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ const_eval_unallowed_mutable_refs_raw =
const_eval_unallowed_op_in_const_context =
{$msg}

const_eval_unavailable_target_features_for_fn =
calling a function that requires unavailable target features: {$unavailable_feats}

const_eval_undefined_behavior =
it is undefined behavior to use this value

Expand Down
30 changes: 30 additions & 0 deletions compiler/rustc_const_eval/src/interpret/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}
}

// Check that all target features required by the callee (i.e., from
// the attribute `#[target_feature(enable = ...)]`) are enabled at
// compile time.
self.check_fn_target_features(instance)?;

if !callee_fn_abi.can_unwind {
// The callee cannot unwind, so force the `Unreachable` unwind handling.
unwind = mir::UnwindAction::Unreachable;
Expand Down Expand Up @@ -786,6 +791,31 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}
}

fn check_fn_target_features(&self, instance: ty::Instance<'tcx>) -> InterpResult<'tcx, ()> {
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
if attrs
.target_features
.iter()
.any(|feature| !self.tcx.sess.target_features.contains(feature))
{
throw_ub_custom!(
fluent::const_eval_unavailable_target_features_for_fn,
unavailable_feats = attrs
.target_features
.iter()
.filter(|&feature| !self.tcx.sess.target_features.contains(feature))
.fold(String::new(), |mut s, feature| {
if !s.is_empty() {
s.push_str(", ");
}
s.push_str(feature.as_str());
s
}),
);
}
Ok(())
}

fn drop_in_place(
&mut self,
place: &PlaceTy<'tcx, M::Provenance>,
Expand Down
11 changes: 11 additions & 0 deletions src/tools/miri/tests/fail/function_calls/target_feature.rs
Original file line number Diff line number Diff line change
@@ -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() {}
15 changes: 15 additions & 0 deletions src/tools/miri/tests/fail/function_calls/target_feature.stderr
Original file line number Diff line number Diff line change
@@ -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

12 changes: 12 additions & 0 deletions src/tools/miri/tests/pass/function_calls/target_feature.rs
Original file line number Diff line number Diff line change
@@ -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() {}
17 changes: 17 additions & 0 deletions tests/ui/consts/const-eval/const_fn_target_feature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// only-x86_64
// compile-flags:-C target-feature=+ssse3

#![crate_type = "lib"]

// ok (ssse3 enabled at compile time)
const A: () = unsafe { ssse3_fn() };

// error (avx2 not enabled at compile time)
const B: () = unsafe { avx2_fn() };
//~^ ERROR evaluation of constant value failed

#[target_feature(enable = "ssse3")]
const unsafe fn ssse3_fn() {}

#[target_feature(enable = "avx2")]
const unsafe fn avx2_fn() {}
9 changes: 9 additions & 0 deletions tests/ui/consts/const-eval/const_fn_target_feature.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0080]: evaluation of constant value failed
--> $DIR/const_fn_target_feature.rs:10:24
|
LL | const B: () = unsafe { avx2_fn() };
| ^^^^^^^^^ calling a function that requires unavailable target features: avx2

error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.