Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f971b1e

Browse files
committedApr 29, 2024·
Ensure miri only uses fallback bodies that have manually been vetted to preserve all UB that the native intrinsic would have
1 parent 1bc11e9 commit f971b1e

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed
 

‎compiler/rustc_resolve/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub(crate) fn registered_tools(tcx: TyCtxt<'_>, (): ()) -> RegisteredTools {
141141
}
142142
// We implicitly add `rustfmt`, `clippy`, `diagnostic` to known tools,
143143
// but it's not an error to register them explicitly.
144-
let predefined_tools = [sym::clippy, sym::rustfmt, sym::diagnostic];
144+
let predefined_tools = [sym::clippy, sym::rustfmt, sym::diagnostic, sym::miri];
145145
registered_tools.extend(predefined_tools.iter().cloned().map(Ident::with_dummy_span));
146146
registered_tools
147147
}

‎library/core/src/intrinsics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ pub const unsafe fn assume(b: bool) {
987987
#[unstable(feature = "core_intrinsics", issue = "none")]
988988
#[rustc_intrinsic]
989989
#[rustc_nounwind]
990+
#[cfg_attr(not(bootstrap), miri::intrinsic_fallback_checks_ub)]
990991
pub const fn likely(b: bool) -> bool {
991992
b
992993
}
@@ -1006,6 +1007,7 @@ pub const fn likely(b: bool) -> bool {
10061007
#[unstable(feature = "core_intrinsics", issue = "none")]
10071008
#[rustc_intrinsic]
10081009
#[rustc_nounwind]
1010+
#[cfg_attr(not(bootstrap), miri::intrinsic_fallback_checks_ub)]
10091011
pub const fn unlikely(b: bool) -> bool {
10101012
b
10111013
}
@@ -2479,6 +2481,7 @@ extern "rust-intrinsic" {
24792481
#[rustc_nounwind]
24802482
#[rustc_do_not_const_check]
24812483
#[inline]
2484+
#[cfg_attr(not(bootstrap), miri::intrinsic_fallback_checks_ub)]
24822485
pub const fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8 {
24832486
(ptr == other) as u8
24842487
}

‎src/tools/miri/src/shims/intrinsics/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_middle::{
1111
ty::{self, FloatTy},
1212
};
1313
use rustc_target::abi::Size;
14+
use rustc_span::{sym, Symbol};
1415

1516
use crate::*;
1617
use atomic::EvalContextExt as _;
@@ -66,6 +67,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
6667
if this.tcx.intrinsic(instance.def_id()).unwrap().must_be_overridden {
6768
throw_unsup_format!("unimplemented intrinsic: `{intrinsic_name}`")
6869
}
70+
let intrinsic_fallback_checks_ub = Symbol::intern("intrinsic_fallback_checks_ub");
71+
if !this.tcx.item_attrs(instance.def_id()).iter().any(|attr| attr.path_matches(&[sym::miri, intrinsic_fallback_checks_ub])) {
72+
throw_unsup_format!("miri can only use intrinsics that preserve UB. After verifying that `{intrinsic_name}` does so, add the `#[miri::intrinsic_fallback_checks_ub]` attribute to it");
73+
}
6974
return Ok(Some(ty::Instance {
7075
def: ty::InstanceDef::Item(instance.def_id()),
7176
args: instance.args,

0 commit comments

Comments
 (0)
Please sign in to comment.