From 00993002b7c7088801c63088c48e11a97717593b Mon Sep 17 00:00:00 2001 From: geetanshjuneja Date: Thu, 22 May 2025 13:55:58 +0530 Subject: [PATCH 1/2] implement shim_sig macro --- src/helpers.rs | 33 +++++++++++++++++++++++++++++ src/shims/unix/foreign_items.rs | 37 +++++++++++---------------------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index ff2ec1b3e6..7db87962dc 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1435,3 +1435,36 @@ impl ToU64 for usize { self.try_into().unwrap() } } + +#[macro_export] +macro_rules! shim_sig { + ($this:ident, extern $abi:literal fn($($arg:tt),*) -> $ret:tt) => { + ( + std::str::FromStr::from_str($abi).expect("incorrect abi specified"), + [$(layout!($this, $arg)),*], + layout!($this, $ret) + ) + }; + + // default Rust abi + ($this:ident, extern $abi:literal fn($($arg:tt),*) -> $ret:tt) => { + shim_sig!($this, extern "Rust" fn($($arg),*) -> $ret) + }; +} + +#[macro_export] +macro_rules! layout { + ($this:ident, ) => {}; + ($this:ident, (*const _)) => { + $this.machine.layouts.const_raw_ptr.ty + }; + ($this:ident, (*mut _)) => { + $this.machine.layouts.mut_raw_ptr.ty + }; + ($this:ident, $arg:ident) => { + $this.tcx.types.$arg + }; + ($this:ident, $arg:literal) => { + $this.libc_ty_layout($arg).ty + }; +} \ No newline at end of file diff --git a/src/shims/unix/foreign_items.rs b/src/shims/unix/foreign_items.rs index 026aa1f950..6932921077 100644 --- a/src/shims/unix/foreign_items.rs +++ b/src/shims/unix/foreign_items.rs @@ -15,7 +15,7 @@ use self::shims::unix::solarish::foreign_items as solarish; use crate::concurrency::cpu_affinity::CpuAffinityMask; use crate::shims::alloc::EvalContextExt as _; use crate::shims::unix::*; -use crate::*; +use crate::{shim_sig, *}; pub fn is_dyn_sym(name: &str, target_os: &str) -> bool { match name { @@ -112,26 +112,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { match link_name.as_str() { // Environment related shims "getenv" => { - let [name] = this.check_shim_abi( - link_name, - abi, - ExternAbi::C { unwind: false }, - [this.machine.layouts.const_raw_ptr.ty], - this.machine.layouts.mut_raw_ptr.ty, - args, - )?; + let (callee_abi, arg_types, ret_type) = + shim_sig!(this, extern "C" fn((*const _)) -> (*const _)); + let [name] = + this.check_shim_abi(link_name, abi, callee_abi, arg_types, ret_type, args)?; let result = this.getenv(name)?; this.write_pointer(result, dest)?; } "unsetenv" => { - let [name] = this.check_shim_abi( - link_name, - abi, - ExternAbi::C { unwind: false }, - [this.machine.layouts.const_raw_ptr.ty], - this.tcx.types.i32, - args, - )?; + let (callee_abi, arg_types, ret_type) = + shim_sig!(this, extern "C" fn((*const _)) -> i32); + let [name] = + this.check_shim_abi(link_name, abi, callee_abi, arg_types, ret_type, args)?; let result = this.unsetenv(name)?; this.write_scalar(result, dest)?; } @@ -177,14 +169,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { this.write_scalar(result, dest)?; } "getpid" => { - let [] = this.check_shim_abi( - link_name, - abi, - ExternAbi::C { unwind: false }, - [], - this.libc_ty_layout("pid_t").ty, - args, - )?; + let (callee_abi, arg_types, ret_type) = shim_sig!(this, extern "C" fn() -> "pid_t"); + let [] = + this.check_shim_abi(link_name, abi, callee_abi, arg_types, ret_type, args)?; let result = this.getpid()?; this.write_scalar(result, dest)?; } From 21bd1b5c0a23ce2d3bd11b0d86d3b1a8fac2ddf1 Mon Sep 17 00:00:00 2001 From: geetanshjuneja Date: Thu, 22 May 2025 14:12:47 +0530 Subject: [PATCH 2/2] fmt --- src/helpers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.rs b/src/helpers.rs index 7db87962dc..0b1f05903e 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1467,4 +1467,4 @@ macro_rules! layout { ($this:ident, $arg:literal) => { $this.libc_ty_layout($arg).ty }; -} \ No newline at end of file +}