Skip to content

Commit 382295c

Browse files
author
hyd-dev
committed
Move check_abi() into EvalContextExt
1 parent ddc2ee9 commit 382295c

File tree

8 files changed

+181
-187
lines changed

8 files changed

+181
-187
lines changed

src/helpers.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
616616

617617
Ok(wchars)
618618
}
619+
620+
/// Check that the ABI is what we expect.
621+
fn check_abi<'a>(&self, abi: Abi, exp_abi: Abi) -> InterpResult<'a, ()> {
622+
if self.eval_context_ref().machine.enforce_abi && abi != exp_abi {
623+
throw_ub_format!(
624+
"calling a function with ABI {} using caller ABI {}",
625+
exp_abi.name(),
626+
abi.name()
627+
)
628+
}
629+
Ok(())
630+
}
619631
}
620632

621633
/// Check that the number of args is what we expect.
@@ -631,22 +643,6 @@ where
631643
throw_ub_format!("incorrect number of arguments: got {}, expected {}", args.len(), N)
632644
}
633645

634-
/// Check that the ABI is what we expect.
635-
pub fn check_abi<'a>(
636-
this: &MiriEvalContext<'_, '_>,
637-
abi: Abi,
638-
exp_abi: Abi,
639-
) -> InterpResult<'a, ()> {
640-
if this.machine.enforce_abi && abi != exp_abi {
641-
throw_ub_format!(
642-
"calling a function with ABI {} using caller ABI {}",
643-
exp_abi.name(),
644-
abi.name()
645-
)
646-
}
647-
Ok(())
648-
}
649-
650646
pub fn isolation_error(name: &str) -> InterpResult<'static> {
651647
throw_machine_stop!(TerminationInfo::UnsupportedInIsolation(format!(
652648
"{} not available when isolation is enabled",

src/shims/foreign_items.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_target::{
2525

2626
use super::backtrace::EvalContextExt as _;
2727
use crate::*;
28-
use helpers::{check_abi, check_arg_count};
28+
use helpers::check_arg_count;
2929

3030
/// Returned by `emulate_foreign_item_by_name`.
3131
pub enum EmulateByNameResult {
@@ -226,14 +226,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
226226
let (dest, ret) = match ret {
227227
None => match link_name {
228228
"miri_start_panic" => {
229-
check_abi(this, abi, Abi::Rust)?;
229+
this.check_abi(abi, Abi::Rust)?;
230230
this.handle_miri_start_panic(args, unwind)?;
231231
return Ok(None);
232232
}
233233
// This matches calls to the foreign item `panic_impl`.
234234
// The implementation is provided by the function with the `#[panic_handler]` attribute.
235235
"panic_impl" => {
236-
check_abi(this, abi, Abi::Rust)?;
236+
this.check_abi(abi, Abi::Rust)?;
237237
let panic_impl_id = tcx.lang_items().panic_impl().unwrap();
238238
let panic_impl_instance = ty::Instance::mono(tcx, panic_impl_id);
239239
return Ok(Some(&*this.load_mir(panic_impl_instance.def, None)?));
@@ -242,14 +242,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
242242
| "exit"
243243
| "ExitProcess"
244244
=> {
245-
check_abi(this, abi, if link_name == "exit" { Abi::C { unwind: false } } else { Abi::System { unwind: false } })?;
245+
this.check_abi(abi, if link_name == "exit" { Abi::C { unwind: false } } else { Abi::System { unwind: false } })?;
246246
let &[ref code] = check_arg_count(args)?;
247247
// it's really u32 for ExitProcess, but we have to put it into the `Exit` variant anyway
248248
let code = this.read_scalar(code)?.to_i32()?;
249249
throw_machine_stop!(TerminationInfo::Exit(code.into()));
250250
}
251251
"abort" => {
252-
check_abi(this, abi, Abi::C { unwind: false })?;
252+
this.check_abi(abi, Abi::C { unwind: false })?;
253253
throw_machine_stop!(TerminationInfo::Abort(
254254
"the program aborted execution".to_owned()
255255
))
@@ -298,7 +298,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
298298
match link_name {
299299
// Miri-specific extern functions
300300
"miri_static_root" => {
301-
check_abi(this, abi, Abi::Rust)?;
301+
this.check_abi(abi, Abi::Rust)?;
302302
let &[ref ptr] = check_arg_count(args)?;
303303
let ptr = this.read_scalar(ptr)?.check_init()?;
304304
let ptr = this.force_ptr(ptr)?;
@@ -310,27 +310,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
310310

311311
// Obtains a Miri backtrace. See the README for details.
312312
"miri_get_backtrace" => {
313-
check_abi(this, abi, Abi::Rust)?;
313+
this.check_abi(abi, Abi::Rust)?;
314314
this.handle_miri_get_backtrace(args, dest)?;
315315
}
316316

317317
// Resolves a Miri backtrace frame. See the README for details.
318318
"miri_resolve_frame" => {
319-
check_abi(this, abi, Abi::Rust)?;
319+
this.check_abi(abi, Abi::Rust)?;
320320
this.handle_miri_resolve_frame(args, dest)?;
321321
}
322322

323323

324324
// Standard C allocation
325325
"malloc" => {
326-
check_abi(this, abi, Abi::C { unwind: false })?;
326+
this.check_abi(abi, Abi::C { unwind: false })?;
327327
let &[ref size] = check_arg_count(args)?;
328328
let size = this.read_scalar(size)?.to_machine_usize(this)?;
329329
let res = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C);
330330
this.write_scalar(res, dest)?;
331331
}
332332
"calloc" => {
333-
check_abi(this, abi, Abi::C { unwind: false })?;
333+
this.check_abi(abi, Abi::C { unwind: false })?;
334334
let &[ref items, ref len] = check_arg_count(args)?;
335335
let items = this.read_scalar(items)?.to_machine_usize(this)?;
336336
let len = this.read_scalar(len)?.to_machine_usize(this)?;
@@ -340,13 +340,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
340340
this.write_scalar(res, dest)?;
341341
}
342342
"free" => {
343-
check_abi(this, abi, Abi::C { unwind: false })?;
343+
this.check_abi(abi, Abi::C { unwind: false })?;
344344
let &[ref ptr] = check_arg_count(args)?;
345345
let ptr = this.read_scalar(ptr)?.check_init()?;
346346
this.free(ptr, MiriMemoryKind::C)?;
347347
}
348348
"realloc" => {
349-
check_abi(this, abi, Abi::C { unwind: false })?;
349+
this.check_abi(abi, Abi::C { unwind: false })?;
350350
let &[ref old_ptr, ref new_size] = check_arg_count(args)?;
351351
let old_ptr = this.read_scalar(old_ptr)?.check_init()?;
352352
let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
@@ -358,7 +358,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
358358
// (Usually these would be forwarded to to `#[global_allocator]`; we instead implement a generic
359359
// allocation that also checks that all conditions are met, such as not permitting zero-sized allocations.)
360360
"__rust_alloc" => {
361-
check_abi(this, abi, Abi::Rust)?;
361+
this.check_abi(abi, Abi::Rust)?;
362362
let &[ref size, ref align] = check_arg_count(args)?;
363363
let size = this.read_scalar(size)?.to_machine_usize(this)?;
364364
let align = this.read_scalar(align)?.to_machine_usize(this)?;
@@ -371,7 +371,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
371371
this.write_scalar(ptr, dest)?;
372372
}
373373
"__rust_alloc_zeroed" => {
374-
check_abi(this, abi, Abi::Rust)?;
374+
this.check_abi(abi, Abi::Rust)?;
375375
let &[ref size, ref align] = check_arg_count(args)?;
376376
let size = this.read_scalar(size)?.to_machine_usize(this)?;
377377
let align = this.read_scalar(align)?.to_machine_usize(this)?;
@@ -386,7 +386,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
386386
this.write_scalar(ptr, dest)?;
387387
}
388388
"__rust_dealloc" => {
389-
check_abi(this, abi, Abi::Rust)?;
389+
this.check_abi(abi, Abi::Rust)?;
390390
let &[ref ptr, ref old_size, ref align] = check_arg_count(args)?;
391391
let ptr = this.read_scalar(ptr)?.check_init()?;
392392
let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
@@ -400,7 +400,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
400400
)?;
401401
}
402402
"__rust_realloc" => {
403-
check_abi(this, abi, Abi::Rust)?;
403+
this.check_abi(abi, Abi::Rust)?;
404404
let &[ref ptr, ref old_size, ref align, ref new_size] = check_arg_count(args)?;
405405
let ptr = this.force_ptr(this.read_scalar(ptr)?.check_init()?)?;
406406
let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
@@ -421,7 +421,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
421421

422422
// C memory handling functions
423423
"memcmp" => {
424-
check_abi(this, abi, Abi::C { unwind: false })?;
424+
this.check_abi(abi, Abi::C { unwind: false })?;
425425
let &[ref left, ref right, ref n] = check_arg_count(args)?;
426426
let left = this.read_scalar(left)?.check_init()?;
427427
let right = this.read_scalar(right)?.check_init()?;
@@ -442,7 +442,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
442442
this.write_scalar(Scalar::from_i32(result), dest)?;
443443
}
444444
"memrchr" => {
445-
check_abi(this, abi, Abi::C { unwind: false })?;
445+
this.check_abi(abi, Abi::C { unwind: false })?;
446446
let &[ref ptr, ref val, ref num] = check_arg_count(args)?;
447447
let ptr = this.read_scalar(ptr)?.check_init()?;
448448
let val = this.read_scalar(val)?.to_i32()? as u8;
@@ -461,7 +461,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
461461
}
462462
}
463463
"memchr" => {
464-
check_abi(this, abi, Abi::C { unwind: false })?;
464+
this.check_abi(abi, Abi::C { unwind: false })?;
465465
let &[ref ptr, ref val, ref num] = check_arg_count(args)?;
466466
let ptr = this.read_scalar(ptr)?.check_init()?;
467467
let val = this.read_scalar(val)?.to_i32()? as u8;
@@ -479,7 +479,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
479479
}
480480
}
481481
"strlen" => {
482-
check_abi(this, abi, Abi::C { unwind: false })?;
482+
this.check_abi(abi, Abi::C { unwind: false })?;
483483
let &[ref ptr] = check_arg_count(args)?;
484484
let ptr = this.read_scalar(ptr)?.check_init()?;
485485
let n = this.read_c_str(ptr)?.len();
@@ -496,7 +496,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
496496
| "asinf"
497497
| "atanf"
498498
=> {
499-
check_abi(this, abi, Abi::C { unwind: false })?;
499+
this.check_abi(abi, Abi::C { unwind: false })?;
500500
let &[ref f] = check_arg_count(args)?;
501501
// FIXME: Using host floats.
502502
let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
@@ -517,7 +517,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
517517
| "hypotf"
518518
| "atan2f"
519519
=> {
520-
check_abi(this, abi, Abi::C { unwind: false })?;
520+
this.check_abi(abi, Abi::C { unwind: false })?;
521521
let &[ref f1, ref f2] = check_arg_count(args)?;
522522
// underscore case for windows, here and below
523523
// (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
@@ -540,7 +540,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
540540
| "asin"
541541
| "atan"
542542
=> {
543-
check_abi(this, abi, Abi::C { unwind: false })?;
543+
this.check_abi(abi, Abi::C { unwind: false })?;
544544
let &[ref f] = check_arg_count(args)?;
545545
// FIXME: Using host floats.
546546
let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
@@ -561,7 +561,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
561561
| "hypot"
562562
| "atan2"
563563
=> {
564-
check_abi(this, abi, Abi::C { unwind: false })?;
564+
this.check_abi(abi, Abi::C { unwind: false })?;
565565
let &[ref f1, ref f2] = check_arg_count(args)?;
566566
// FIXME: Using host floats.
567567
let f1 = f64::from_bits(this.read_scalar(f1)?.to_u64()?);
@@ -578,7 +578,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
578578
| "ldexp"
579579
| "scalbn"
580580
=> {
581-
check_abi(this, abi, Abi::C { unwind: false })?;
581+
this.check_abi(abi, Abi::C { unwind: false })?;
582582
let &[ref x, ref exp] = check_arg_count(args)?;
583583
// For radix-2 (binary) systems, `ldexp` and `scalbn` are the same.
584584
let x = this.read_scalar(x)?.to_f64()?;
@@ -600,12 +600,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
600600

601601
// Architecture-specific shims
602602
"llvm.x86.sse2.pause" if this.tcx.sess.target.arch == "x86" || this.tcx.sess.target.arch == "x86_64" => {
603-
check_abi(this, abi, Abi::C { unwind: false })?;
603+
this.check_abi(abi, Abi::C { unwind: false })?;
604604
let &[] = check_arg_count(args)?;
605605
this.yield_active_thread();
606606
}
607607
"llvm.aarch64.isb" if this.tcx.sess.target.arch == "aarch64" => {
608-
check_abi(this, abi, Abi::C { unwind: false })?;
608+
this.check_abi(abi, Abi::C { unwind: false })?;
609609
let &[ref arg] = check_arg_count(args)?;
610610
let arg = this.read_scalar(arg)?.to_i32()?;
611611
match arg {

src/shims/posix/dlsym.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use rustc_middle::mir;
22
use rustc_target::spec::abi::Abi;
33

44
use crate::*;
5-
use helpers::check_abi;
65
use shims::posix::linux::dlsym as linux;
76
use shims::posix::macos::dlsym as macos;
87

@@ -35,7 +34,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
3534
) -> InterpResult<'tcx> {
3635
let this = self.eval_context_mut();
3736

38-
check_abi(this, abi, Abi::C { unwind: false })?;
37+
this.check_abi(abi, Abi::C { unwind: false })?;
3938

4039
match dlsym {
4140
Dlsym::Linux(dlsym) => linux::EvalContextExt::call_dlsym(this, dlsym, args, ret),

0 commit comments

Comments
 (0)