Skip to content

Commit 7f85148

Browse files
author
Ellen Arteca
committed
dont need to track fn_sig: nice simplification, thanks @oli-obk!
1 parent 48c179d commit 7f85148

File tree

2 files changed

+21
-49
lines changed

2 files changed

+21
-49
lines changed

src/shims/ffi_support.rs

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6565
/// store output, depending on return type in the function signature.
6666
fn call_external_c_and_store_return<'a>(
6767
&mut self,
68-
external_fct_defn: ExternalCFuncDeclRep<'tcx>,
68+
link_name: Symbol,
6969
dest: &PlaceTy<'tcx, Tag>,
7070
ptr: CodePtr,
7171
libffi_args: Vec<libffi::high::Arg<'a>>,
@@ -79,7 +79,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7979
// If the return type of a function is a primitive integer type,
8080
// then call the function (`ptr`) with arguments `libffi_args`, store the return value as the specified
8181
// primitive integer type, and then write this value out to the miri memory as an integer.
82-
match external_fct_defn.output_type.kind() {
82+
match dest.layout.ty.kind() {
8383
// ints
8484
TyKind::Int(IntTy::I8) => {
8585
let x = call::<i8>(ptr, libffi_args.as_slice());
@@ -136,19 +136,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
136136
this.write_int(u64::try_from(x).unwrap(), dest)?;
137137
return Ok(());
138138
}
139+
// Functions with no declared return type (i.e., the default return)
140+
// have the output_type `Tuple([])`.
141+
TyKind::Tuple(t_list) =>
142+
if t_list.len() == 0 {
143+
call::<()>(ptr, libffi_args.as_slice());
144+
return Ok(());
145+
},
139146
_ => {}
140147
}
141-
// Functions with no declared return type (i.e., the default return)
142-
// have the output_type `Tuple([])`.
143-
if let TyKind::Tuple(t_list) = external_fct_defn.output_type.kind() && t_list.len() == 0{
144-
call::<()>(ptr, libffi_args.as_slice());
145-
return Ok(());
146-
}
147148
// TODO ellen! deal with all the other return types
148-
throw_unsup_format!(
149-
"unsupported return type to external C function: {:?}",
150-
external_fct_defn.link_name
151-
);
149+
throw_unsup_format!("unsupported return type to external C function: {:?}", link_name);
152150
}
153151
}
154152

@@ -159,12 +157,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
159157
/// can be stored in Miri internal memory.
160158
fn call_and_add_external_c_fct_to_context(
161159
&mut self,
162-
external_fct_defn: ExternalCFuncDeclRep<'tcx>,
160+
link_name: Symbol,
163161
dest: &PlaceTy<'tcx, Tag>,
164162
args: &[OpTy<'tcx, Tag>],
165163
) -> InterpResult<'tcx, bool> {
166164
let this = self.eval_context_mut();
167-
let link_name = external_fct_defn.link_name;
168165
let (lib, lib_path) = this.machine.external_so_lib.as_ref().unwrap();
169166

170167
// Load the C function from the library.
@@ -196,22 +193,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
196193
!= lib_path.to_str().unwrap()
197194
{
198195
return Ok(false);
199-
}
196+
}
200197
}
201198
}
202199

203200
// Get the function arguments, and convert them to `libffi`-compatible form.
204-
if args.len() != external_fct_defn.inputs_types.len() {
205-
throw_ub_format!(
206-
"calling function {:?} with {} arguments; expected {}",
207-
link_name,
208-
args.len(),
209-
external_fct_defn.inputs_types.len()
210-
);
211-
}
212201
let mut libffi_args = Vec::<CArg>::with_capacity(args.len());
213-
for (cur_arg, arg_type) in args.iter().zip(external_fct_defn.inputs_types.iter()) {
214-
libffi_args.push(Self::scalar_to_carg(this.read_scalar(cur_arg)?, arg_type, this)?);
202+
for cur_arg in args.iter() {
203+
libffi_args.push(Self::scalar_to_carg(
204+
this.read_scalar(cur_arg)?,
205+
&cur_arg.layout.ty,
206+
this,
207+
)?);
215208
}
216209

217210
// Convert them to `libffi::high::Arg` type.
@@ -223,22 +216,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
223216
// Code pointer to C function.
224217
let ptr = CodePtr(*func.deref() as *mut _);
225218
// Call the functio and store output, depending on return type in the function signature.
226-
self.call_external_c_and_store_return(external_fct_defn, dest, ptr, libffi_args)?;
219+
self.call_external_c_and_store_return(link_name, dest, ptr, libffi_args)?;
227220
Ok(true)
228221
}
229222
}
230223

231-
#[derive(Debug)]
232-
/// Signature of an external C function.
233-
pub struct ExternalCFuncDeclRep<'tcx> {
234-
/// Function name.
235-
pub link_name: Symbol,
236-
/// Argument types.
237-
pub inputs_types: &'tcx [Ty<'tcx>],
238-
/// Return type.
239-
pub output_type: Ty<'tcx>,
240-
}
241-
242224
#[derive(Debug, Clone)]
243225
/// Enum of supported arguments to external C functions.
244226
pub enum CArg {

src/shims/foreign_items.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rustc_target::{
2424
use super::backtrace::EvalContextExt as _;
2525
use crate::helpers::{convert::Truncate, target_os_is_unix};
2626
use crate::shims::ffi_support::EvalContextExt as _;
27-
use crate::shims::ffi_support::ExternalCFuncDeclRep;
2827
use crate::*;
2928

3029
/// Returned by `emulate_foreign_item_by_name`.
@@ -300,7 +299,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
300299
};
301300

302301
// Second: functions that return immediately.
303-
match this.emulate_foreign_item_by_name(def_id, link_name, abi, args, dest)? {
302+
match this.emulate_foreign_item_by_name(link_name, abi, args, dest)? {
304303
EmulateByNameResult::NeedsJumping => {
305304
trace!("{:?}", this.dump_place(**dest));
306305
this.go_to_block(ret);
@@ -353,29 +352,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
353352
/// Emulates calling a foreign item using its name.
354353
fn emulate_foreign_item_by_name(
355354
&mut self,
356-
def_id: DefId,
357355
link_name: Symbol,
358356
abi: Abi,
359357
args: &[OpTy<'tcx, Tag>],
360358
dest: &PlaceTy<'tcx, Tag>,
361359
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
362360
let this = self.eval_context_mut();
363361

364-
let tcx = this.tcx.tcx;
365-
366362
// First deal with any external C functions in linked .so file
367363
// (if any SO file is specified).
368364
if this.machine.external_so_lib.as_ref().is_some() {
369-
let fn_sig = &tcx.fn_sig(def_id);
370-
let extern_c_fct_rep = ExternalCFuncDeclRep {
371-
link_name,
372-
inputs_types: fn_sig.inputs().skip_binder(),
373-
output_type: fn_sig.output().skip_binder(),
374-
};
375365
// An Ok(false) here means that the function being called was not exported
376366
// by the specified SO file; we should continue and check if it corresponds to
377367
// a provided shim.
378-
if this.call_and_add_external_c_fct_to_context(extern_c_fct_rep, dest, args)? {
368+
if this.call_and_add_external_c_fct_to_context(link_name, dest, args)? {
379369
return Ok(EmulateByNameResult::NeedsJumping);
380370
}
381371
}

0 commit comments

Comments
 (0)