Skip to content

Commit 104a3c3

Browse files
committed
Auto merge of rust-lang#85557 - hyd-dev:abi, r=RalfJung
Add `rustc_mir::interpret::Machine::enforce_abi()` To specify whether to skip the [ABI](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/abi/enum.Abi.html) check for function calls, so that we could test unwinding out of a `extern "C"` function call in Miri by disabling the check: rust-lang/miri#1776 (comment) I have tested that it works in Miri with a `-Zmiri-disable-abi-check` command line flag.
2 parents 70cb58c + 7e42c97 commit 104a3c3

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

compiler/rustc_mir/src/interpret/machine.rs

+5
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ pub trait Machine<'mir, 'tcx>: Sized {
132132
/// Whether to enforce the validity invariant
133133
fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
134134

135+
/// Whether function calls should be [ABI](Abi)-checked.
136+
fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
137+
true
138+
}
139+
135140
/// Entry point for obtaining the MIR of anything that should get evaluated.
136141
/// So not just functions and shims, but also const/static initializers, anonymous
137142
/// constants, ...

compiler/rustc_mir/src/interpret/terminator.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -232,26 +232,28 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
232232

233233
// ABI check
234234
let check_abi = |this: &Self, instance_ty: Ty<'tcx>| -> InterpResult<'tcx> {
235-
let callee_abi = match instance_ty.kind() {
236-
ty::FnDef(..) => instance_ty.fn_sig(*this.tcx).abi(),
237-
ty::Closure(..) => Abi::RustCall,
238-
ty::Generator(..) => Abi::Rust,
239-
_ => span_bug!(this.cur_span(), "unexpected callee ty: {:?}", instance_ty),
240-
};
241-
let normalize_abi = |abi| match abi {
242-
Abi::Rust | Abi::RustCall | Abi::RustIntrinsic | Abi::PlatformIntrinsic =>
243-
// These are all the same ABI, really.
244-
{
245-
Abi::Rust
235+
if M::enforce_abi(this) {
236+
let callee_abi = match instance_ty.kind() {
237+
ty::FnDef(..) => instance_ty.fn_sig(*this.tcx).abi(),
238+
ty::Closure(..) => Abi::RustCall,
239+
ty::Generator(..) => Abi::Rust,
240+
_ => span_bug!(this.cur_span(), "unexpected callee ty: {:?}", instance_ty),
241+
};
242+
let normalize_abi = |abi| match abi {
243+
Abi::Rust | Abi::RustCall | Abi::RustIntrinsic | Abi::PlatformIntrinsic =>
244+
// These are all the same ABI, really.
245+
{
246+
Abi::Rust
247+
}
248+
abi => abi,
249+
};
250+
if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
251+
throw_ub_format!(
252+
"calling a function with ABI {} using caller ABI {}",
253+
callee_abi.name(),
254+
caller_abi.name()
255+
)
246256
}
247-
abi => abi,
248-
};
249-
if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
250-
throw_ub_format!(
251-
"calling a function with ABI {} using caller ABI {}",
252-
callee_abi.name(),
253-
caller_abi.name()
254-
)
255257
}
256258
Ok(())
257259
};

0 commit comments

Comments
 (0)