Skip to content

Commit 821d23b

Browse files
committed
Ensure miri only uses fallback bodies that have manually been vetted to preserve all UB that the native intrinsic would have
1 parent 351658a commit 821d23b

File tree

8 files changed

+44
-2
lines changed

8 files changed

+44
-2
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
468468

469469
// CTFE-specific intrinsics.
470470
let Some(ret) = target else {
471-
// Handle diverging intrinsics.
471+
// Handle diverging intrinsics. We can't handle any of them (that are not already
472+
// handled above), but check if there is a fallback body.
472473
if ecx.tcx.intrinsic(instance.def_id()).unwrap().must_be_overridden {
473474
throw_unsup_format!(
474475
"intrinsic `{intrinsic_name}` is not supported at compile-time"

compiler/rustc_resolve/src/macros.rs

+1-1
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

+3
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
}
@@ -2526,6 +2528,7 @@ extern "rust-intrinsic" {
25262528
#[rustc_nounwind]
25272529
#[rustc_do_not_const_check]
25282530
#[inline]
2531+
#[cfg_attr(not(bootstrap), miri::intrinsic_fallback_checks_ub)]
25292532
pub const fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8 {
25302533
(ptr == other) as u8
25312534
}

src/tools/miri/src/shims/intrinsics/atomic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub enum AtomicOp {
1414
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
1515
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1616
/// Calls the atomic intrinsic `intrinsic`; the `atomic_` prefix has already been removed.
17+
/// Returns `Ok(true)` if the intrinsic was handled.
1718
fn emulate_atomic_intrinsic(
1819
&mut self,
1920
intrinsic_name: &str,

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

+8
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 _;
@@ -48,6 +49,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
4849

4950
// All remaining supported intrinsics have a return place.
5051
let ret = match ret {
52+
// FIXME: add fallback body support once we actually have a diverging intrinsic with a fallback body
5153
None => throw_unsup_format!("unimplemented (diverging) intrinsic: `{intrinsic_name}`"),
5254
Some(p) => p,
5355
};
@@ -63,9 +65,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
6365

6466
// The rest jumps to `ret` immediately.
6567
if !this.emulate_intrinsic_by_name(intrinsic_name, instance.args, args, dest)? {
68+
// We haven't handled the intrinsic, let's see if we can use a fallback body.
6669
if this.tcx.intrinsic(instance.def_id()).unwrap().must_be_overridden {
6770
throw_unsup_format!("unimplemented intrinsic: `{intrinsic_name}`")
6871
}
72+
let intrinsic_fallback_checks_ub = Symbol::intern("intrinsic_fallback_checks_ub");
73+
if this.tcx.get_attrs_by_path(instance.def_id(), &[sym::miri, intrinsic_fallback_checks_ub]).next().is_none() {
74+
throw_unsup_format!("miri can only use intrinsic fallback bodies that check UB. After verifying that `{intrinsic_name}` does so, add the `#[miri::intrinsic_fallback_checks_ub]` attribute to it; also ping @rust-lang/miri when you do that");
75+
}
6976
return Ok(Some(ty::Instance {
7077
def: ty::InstanceDef::Item(instance.def_id()),
7178
args: instance.args,
@@ -78,6 +85,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
7885
}
7986

8087
/// Emulates a Miri-supported intrinsic (not supported by the core engine).
88+
/// Returns `Ok(true)` if the intrinsic was handled.
8189
fn emulate_intrinsic_by_name(
8290
&mut self,
8391
intrinsic_name: &str,

src/tools/miri/src/shims/intrinsics/simd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub(crate) enum MinMax {
1616
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
1717
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1818
/// Calls the simd intrinsic `intrinsic`; the `simd_` prefix has already been removed.
19+
/// Returns `Ok(true)` if the intrinsic was handled.
1920
fn emulate_simd_intrinsic(
2021
&mut self,
2122
intrinsic_name: &str,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(rustc_attrs, effects)]
2+
3+
#[rustc_intrinsic]
4+
#[rustc_nounwind]
5+
#[rustc_do_not_const_check]
6+
#[inline]
7+
pub const fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8 {
8+
(ptr == other) as u8
9+
}
10+
11+
fn main() {
12+
ptr_guaranteed_cmp::<()>(std::ptr::null(), std::ptr::null());
13+
//~^ ERROR: can only use intrinsic fallback bodies that check UB.
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unsupported operation: miri can only use intrinsic fallback bodies that check UB. After verifying that `ptr_guaranteed_cmp` does so, add the `#[miri::intrinsic_fallback_checks_ub]` attribute to it; also ping @rust-lang/miri when you do that
2+
--> $DIR/intrinsic_fallback_checks_ub.rs:LL:CC
3+
|
4+
LL | ptr_guaranteed_cmp::<()>(std::ptr::null(), std::ptr::null());
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ miri can only use intrinsic fallback bodies that check UB. After verifying that `ptr_guaranteed_cmp` does so, add the `#[miri::intrinsic_fallback_checks_ub]` attribute to it; also ping @rust-lang/miri when you do that
6+
|
7+
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
8+
= note: BACKTRACE:
9+
= note: inside `main` at $DIR/intrinsic_fallback_checks_ub.rs:LL:CC
10+
11+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)