Skip to content

Commit c69fba9

Browse files
author
hyd-dev
committed
Add rustc_mir::interpret::Machine::enforce_abi()
1 parent f36b137 commit c69fba9

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

compiler/rustc_mir/src/interpret/machine.rs

+8
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ 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+
135138
/// Entry point for obtaining the MIR of anything that should get evaluated.
136139
/// So not just functions and shims, but also const/static initializers, anonymous
137140
/// constants, ...
@@ -444,6 +447,11 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
444447
false // for now, we don't enforce validity
445448
}
446449

450+
#[inline(always)]
451+
fn enforce_abi(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
452+
true
453+
}
454+
447455
#[inline(always)]
448456
fn call_extra_fn(
449457
_ecx: &mut InterpCx<$mir, $tcx, Self>,

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)