Skip to content

Commit fb3094d

Browse files
committed
Merge from rustc
2 parents e400f8c + ae30826 commit fb3094d

File tree

22 files changed

+183
-223
lines changed

22 files changed

+183
-223
lines changed

src/intrinsics/atomic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
2020
intrinsic_name: &str,
2121
args: &[OpTy<'tcx, Provenance>],
2222
dest: &MPlaceTy<'tcx, Provenance>,
23-
) -> InterpResult<'tcx, bool> {
23+
) -> InterpResult<'tcx, EmulateItemResult> {
2424
let this = self.eval_context_mut();
2525

2626
let intrinsic_structure: Vec<_> = intrinsic_name.split('_').collect();
@@ -114,9 +114,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
114114
this.atomic_rmw_op(args, dest, AtomicOp::Max, rw_ord(ord)?)?;
115115
}
116116

117-
_ => return Ok(false),
117+
_ => return Ok(EmulateItemResult::NotSupported),
118118
}
119-
Ok(true)
119+
Ok(EmulateItemResult::NeedsJumping)
120120
}
121121
}
122122

src/intrinsics/mod.rs

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,58 +37,38 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
3737
let intrinsic_name = this.tcx.item_name(instance.def_id());
3838
let intrinsic_name = intrinsic_name.as_str();
3939

40-
// Handle intrinsics without return place.
41-
match intrinsic_name {
42-
"abort" => {
43-
throw_machine_stop!(TerminationInfo::Abort(
44-
"the program aborted execution".to_owned()
45-
))
46-
}
47-
_ => {}
48-
}
49-
50-
// All remaining supported intrinsics have a return place.
51-
let ret = match ret {
52-
// FIXME: add fallback body support once we actually have a diverging intrinsic with a fallback body
53-
None => throw_unsup_format!("unimplemented (diverging) intrinsic: `{intrinsic_name}`"),
54-
Some(p) => p,
55-
};
56-
57-
// Some intrinsics are special and need the "ret".
58-
match intrinsic_name {
59-
"catch_unwind" => {
60-
this.handle_catch_unwind(args, dest, ret)?;
61-
return Ok(None);
62-
}
63-
_ => {}
64-
}
65-
66-
// The rest jumps to `ret` immediately.
67-
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.
69-
if this.tcx.intrinsic(instance.def_id()).unwrap().must_be_overridden {
70-
throw_unsup_format!("unimplemented intrinsic: `{intrinsic_name}`")
40+
match this.emulate_intrinsic_by_name(intrinsic_name, instance.args, args, dest, ret)? {
41+
EmulateItemResult::NotSupported => {
42+
// We haven't handled the intrinsic, let's see if we can use a fallback body.
43+
if this.tcx.intrinsic(instance.def_id()).unwrap().must_be_overridden {
44+
throw_unsup_format!("unimplemented intrinsic: `{intrinsic_name}`")
45+
}
46+
let intrinsic_fallback_checks_ub = Symbol::intern("intrinsic_fallback_checks_ub");
47+
if this
48+
.tcx
49+
.get_attrs_by_path(
50+
instance.def_id(),
51+
&[sym::miri, intrinsic_fallback_checks_ub],
52+
)
53+
.next()
54+
.is_none()
55+
{
56+
throw_unsup_format!(
57+
"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"
58+
);
59+
}
60+
Ok(Some(ty::Instance {
61+
def: ty::InstanceDef::Item(instance.def_id()),
62+
args: instance.args,
63+
}))
7164
}
72-
let intrinsic_fallback_checks_ub = Symbol::intern("intrinsic_fallback_checks_ub");
73-
if this
74-
.tcx
75-
.get_attrs_by_path(instance.def_id(), &[sym::miri, intrinsic_fallback_checks_ub])
76-
.next()
77-
.is_none()
78-
{
79-
throw_unsup_format!(
80-
"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"
81-
);
65+
EmulateItemResult::NeedsJumping => {
66+
trace!("{:?}", this.dump_place(&dest.clone().into()));
67+
this.return_to_block(ret)?;
68+
Ok(None)
8269
}
83-
return Ok(Some(ty::Instance {
84-
def: ty::InstanceDef::Item(instance.def_id()),
85-
args: instance.args,
86-
}));
70+
EmulateItemResult::AlreadyJumped => Ok(None),
8771
}
88-
89-
trace!("{:?}", this.dump_place(&dest.clone().into()));
90-
this.go_to_block(ret);
91-
Ok(None)
9272
}
9373

9474
/// Emulates a Miri-supported intrinsic (not supported by the core engine).
@@ -99,7 +79,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
9979
generic_args: ty::GenericArgsRef<'tcx>,
10080
args: &[OpTy<'tcx, Provenance>],
10181
dest: &MPlaceTy<'tcx, Provenance>,
102-
) -> InterpResult<'tcx, bool> {
82+
ret: Option<mir::BasicBlock>,
83+
) -> InterpResult<'tcx, EmulateItemResult> {
10384
let this = self.eval_context_mut();
10485

10586
if let Some(name) = intrinsic_name.strip_prefix("atomic_") {
@@ -110,6 +91,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
11091
}
11192

11293
match intrinsic_name {
94+
"abort" => {
95+
throw_machine_stop!(TerminationInfo::Abort(
96+
"the program aborted execution".to_owned()
97+
));
98+
}
99+
"catch_unwind" => {
100+
this.handle_catch_unwind(args, dest, ret)?;
101+
// THis pushed a stack frame, don't jump to `ret`.
102+
return Ok(EmulateItemResult::AlreadyJumped);
103+
}
104+
113105
// Raw memory accesses
114106
"volatile_load" => {
115107
let [place] = check_arg_count(args)?;
@@ -433,9 +425,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
433425
throw_machine_stop!(TerminationInfo::Abort(format!("trace/breakpoint trap")))
434426
}
435427

436-
_ => return Ok(false),
428+
_ => return Ok(EmulateItemResult::NotSupported),
437429
}
438430

439-
Ok(true)
431+
Ok(EmulateItemResult::NeedsJumping)
440432
}
441433
}

src/intrinsics/simd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
2323
generic_args: ty::GenericArgsRef<'tcx>,
2424
args: &[OpTy<'tcx, Provenance>],
2525
dest: &MPlaceTy<'tcx, Provenance>,
26-
) -> InterpResult<'tcx, bool> {
26+
) -> InterpResult<'tcx, EmulateItemResult> {
2727
let this = self.eval_context_mut();
2828
match intrinsic_name {
2929
#[rustfmt::skip]
@@ -744,9 +744,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
744744
}
745745
}
746746

747-
_ => return Ok(false),
747+
_ => return Ok(EmulateItemResult::NotSupported),
748748
}
749-
Ok(true)
749+
Ok(EmulateItemResult::NeedsJumping)
750750
}
751751

752752
fn fminmax_op(

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub use crate::shims::os_str::EvalContextExt as _;
103103
pub use crate::shims::panic::{CatchUnwindData, EvalContextExt as _};
104104
pub use crate::shims::time::EvalContextExt as _;
105105
pub use crate::shims::tls::TlsData;
106+
pub use crate::shims::EmulateItemResult;
106107

107108
pub use crate::alloc_addresses::{EvalContextExt as _, ProvenanceMode};
108109
pub use crate::borrow_tracker::stacked_borrows::{

src/shims/alloc.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_ast::expand::allocator::AllocatorKind;
44
use rustc_target::abi::{Align, Size};
55

66
use crate::*;
7-
use shims::foreign_items::EmulateForeignItemResult;
87

98
/// Check some basic requirements for this allocation request:
109
/// non-zero size, power-of-two alignment.
@@ -55,12 +54,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
5554
fn emulate_allocator(
5655
&mut self,
5756
default: impl FnOnce(&mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx>,
58-
) -> InterpResult<'tcx, EmulateForeignItemResult> {
57+
) -> InterpResult<'tcx, EmulateItemResult> {
5958
let this = self.eval_context_mut();
6059

6160
let Some(allocator_kind) = this.tcx.allocator_kind(()) else {
6261
// in real code, this symbol does not exist without an allocator
63-
return Ok(EmulateForeignItemResult::NotSupported);
62+
return Ok(EmulateItemResult::NotSupported);
6463
};
6564

6665
match allocator_kind {
@@ -70,11 +69,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
7069
// and not execute any Miri shim. Somewhat unintuitively doing so is done
7170
// by returning `NotSupported`, which triggers the `lookup_exported_symbol`
7271
// fallback case in `emulate_foreign_item`.
73-
return Ok(EmulateForeignItemResult::NotSupported);
72+
return Ok(EmulateItemResult::NotSupported);
7473
}
7574
AllocatorKind::Default => {
7675
default(this)?;
77-
Ok(EmulateForeignItemResult::NeedsJumping)
76+
Ok(EmulateItemResult::NeedsJumping)
7877
}
7978
}
8079
}

0 commit comments

Comments
 (0)