@@ -65,7 +65,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
65
65
/// store output, depending on return type in the function signature.
66
66
fn call_external_c_and_store_return < ' a > (
67
67
& mut self ,
68
- external_fct_defn : ExternalCFuncDeclRep < ' tcx > ,
68
+ link_name : Symbol ,
69
69
dest : & PlaceTy < ' tcx , Tag > ,
70
70
ptr : CodePtr ,
71
71
libffi_args : Vec < libffi:: high:: Arg < ' a > > ,
@@ -79,7 +79,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
79
79
// If the return type of a function is a primitive integer type,
80
80
// then call the function (`ptr`) with arguments `libffi_args`, store the return value as the specified
81
81
// 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 ( ) {
83
83
// ints
84
84
TyKind :: Int ( IntTy :: I8 ) => {
85
85
let x = call :: < i8 > ( ptr, libffi_args. as_slice ( ) ) ;
@@ -136,19 +136,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
136
136
this. write_int ( u64:: try_from ( x) . unwrap ( ) , dest) ?;
137
137
return Ok ( ( ) ) ;
138
138
}
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
+ } ,
139
146
_ => { }
140
147
}
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
- }
147
148
// 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) ;
152
150
}
153
151
}
154
152
@@ -159,12 +157,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
159
157
/// can be stored in Miri internal memory.
160
158
fn call_and_add_external_c_fct_to_context (
161
159
& mut self ,
162
- external_fct_defn : ExternalCFuncDeclRep < ' tcx > ,
160
+ link_name : Symbol ,
163
161
dest : & PlaceTy < ' tcx , Tag > ,
164
162
args : & [ OpTy < ' tcx , Tag > ] ,
165
163
) -> InterpResult < ' tcx , bool > {
166
164
let this = self . eval_context_mut ( ) ;
167
- let link_name = external_fct_defn. link_name ;
168
165
let ( lib, lib_path) = this. machine . external_so_lib . as_ref ( ) . unwrap ( ) ;
169
166
170
167
// Load the C function from the library.
@@ -196,22 +193,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
196
193
!= lib_path. to_str ( ) . unwrap ( )
197
194
{
198
195
return Ok ( false ) ;
199
- }
196
+ }
200
197
}
201
198
}
202
199
203
200
// 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
- }
212
201
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
+ ) ?) ;
215
208
}
216
209
217
210
// Convert them to `libffi::high::Arg` type.
@@ -223,22 +216,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
223
216
// Code pointer to C function.
224
217
let ptr = CodePtr ( * func. deref ( ) as * mut _ ) ;
225
218
// 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) ?;
227
220
Ok ( true )
228
221
}
229
222
}
230
223
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
-
242
224
#[ derive( Debug , Clone ) ]
243
225
/// Enum of supported arguments to external C functions.
244
226
pub enum CArg {
0 commit comments