Skip to content

Commit e646e70

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 7b7e96d commit e646e70

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

src/shims/intrinsics/atomic.rs

Lines changed: 1 addition & 0 deletions
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/shims/intrinsics/mod.rs

Lines changed: 8 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 _;
@@ -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/shims/intrinsics/simd.rs

Lines changed: 1 addition & 0 deletions
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,
Lines changed: 14 additions & 0 deletions
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+
}
Lines changed: 14 additions & 0 deletions
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)