xref: /wasmtime-44.0.1/crates/c-api/src/func.rs (revision bfdbd10a)
1 use crate::wasm_trap_t;
2 use crate::{
3     wasm_extern_t, wasm_functype_t, wasm_store_t, wasm_val_t, wasm_val_vec_t, wasmtime_error_t,
4     wasmtime_extern_t, wasmtime_val_t, wasmtime_val_union, CStoreContext, CStoreContextMut,
5 };
6 use std::ffi::c_void;
7 use std::mem::{self, MaybeUninit};
8 use std::panic::{self, AssertUnwindSafe};
9 use std::ptr;
10 use std::str;
11 use wasmtime::{AsContextMut, Caller, Extern, Func, Trap, Val, ValRaw};
12 
13 #[derive(Clone)]
14 #[repr(transparent)]
15 pub struct wasm_func_t {
16     ext: wasm_extern_t,
17 }
18 
19 wasmtime_c_api_macros::declare_ref!(wasm_func_t);
20 
21 pub type wasm_func_callback_t = extern "C" fn(
22     args: *const wasm_val_vec_t,
23     results: *mut wasm_val_vec_t,
24 ) -> Option<Box<wasm_trap_t>>;
25 
26 pub type wasm_func_callback_with_env_t = extern "C" fn(
27     env: *mut std::ffi::c_void,
28     args: *const wasm_val_vec_t,
29     results: *mut wasm_val_vec_t,
30 ) -> Option<Box<wasm_trap_t>>;
31 
32 impl wasm_func_t {
33     pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_func_t> {
34         match &e.which {
35             Extern::Func(_) => Some(unsafe { &*(e as *const _ as *const _) }),
36             _ => None,
37         }
38     }
39 
40     pub(crate) fn func(&self) -> Func {
41         match self.ext.which {
42             Extern::Func(f) => f,
43             _ => unsafe { std::hint::unreachable_unchecked() },
44         }
45     }
46 }
47 
48 unsafe fn create_function(
49     store: &mut wasm_store_t,
50     ty: &wasm_functype_t,
51     func: impl Fn(*const wasm_val_vec_t, *mut wasm_val_vec_t) -> Option<Box<wasm_trap_t>>
52         + Send
53         + Sync
54         + 'static,
55 ) -> Box<wasm_func_t> {
56     let ty = ty.ty().ty.clone();
57     let func = Func::new(
58         store.store.context_mut(),
59         ty,
60         move |_caller, params, results| {
61             let params: wasm_val_vec_t = params
62                 .iter()
63                 .cloned()
64                 .map(|p| wasm_val_t::from_val(p))
65                 .collect::<Vec<_>>()
66                 .into();
67             let mut out_results: wasm_val_vec_t = vec![wasm_val_t::default(); results.len()].into();
68             let out = func(&params, &mut out_results);
69             if let Some(trap) = out {
70                 return Err(trap.trap.clone());
71             }
72 
73             let out_results = out_results.as_slice();
74             for i in 0..results.len() {
75                 results[i] = out_results[i].val();
76             }
77             Ok(())
78         },
79     );
80     Box::new(wasm_func_t {
81         ext: wasm_extern_t {
82             store: store.store.clone(),
83             which: func.into(),
84         },
85     })
86 }
87 
88 #[no_mangle]
89 pub unsafe extern "C" fn wasm_func_new(
90     store: &mut wasm_store_t,
91     ty: &wasm_functype_t,
92     callback: wasm_func_callback_t,
93 ) -> Box<wasm_func_t> {
94     create_function(store, ty, move |params, results| callback(params, results))
95 }
96 
97 #[no_mangle]
98 pub unsafe extern "C" fn wasm_func_new_with_env(
99     store: &mut wasm_store_t,
100     ty: &wasm_functype_t,
101     callback: wasm_func_callback_with_env_t,
102     data: *mut c_void,
103     finalizer: Option<extern "C" fn(arg1: *mut std::ffi::c_void)>,
104 ) -> Box<wasm_func_t> {
105     let finalizer = crate::ForeignData { data, finalizer };
106     create_function(store, ty, move |params, results| {
107         callback(finalizer.data, params, results)
108     })
109 }
110 
111 /// Places the `args` into `dst` and additionally reserves space in `dst` for `results_size`
112 /// returns. The params/results slices are then returned separately.
113 fn translate_args<'a>(
114     dst: &'a mut Vec<Val>,
115     args: impl ExactSizeIterator<Item = Val>,
116     results_size: usize,
117 ) -> (&'a [Val], &'a mut [Val]) {
118     debug_assert!(dst.is_empty());
119     let num_args = args.len();
120     dst.reserve(args.len() + results_size);
121     dst.extend(args);
122     dst.extend((0..results_size).map(|_| Val::null()));
123     let (a, b) = dst.split_at_mut(num_args);
124     (a, b)
125 }
126 
127 #[no_mangle]
128 pub unsafe extern "C" fn wasm_func_call(
129     func: &mut wasm_func_t,
130     args: *const wasm_val_vec_t,
131     results: *mut wasm_val_vec_t,
132 ) -> *mut wasm_trap_t {
133     let f = func.func();
134     let results = (*results).as_uninit_slice();
135     let args = (*args).as_slice();
136     let mut dst = Vec::new();
137     let (wt_params, wt_results) =
138         translate_args(&mut dst, args.iter().map(|i| i.val()), results.len());
139 
140     // We're calling arbitrary code here most of the time, and we in general
141     // want to try to insulate callers against bugs in wasmtime/wasi/etc if we
142     // can. As a result we catch panics here and transform them to traps to
143     // allow the caller to have any insulation possible against Rust panics.
144     let result = panic::catch_unwind(AssertUnwindSafe(|| {
145         f.call(func.ext.store.context_mut(), wt_params, wt_results)
146     }));
147     match result {
148         Ok(Ok(())) => {
149             for (slot, val) in results.iter_mut().zip(wt_results.iter().cloned()) {
150                 crate::initialize(slot, wasm_val_t::from_val(val));
151             }
152             ptr::null_mut()
153         }
154         Ok(Err(trap)) => match trap.downcast::<Trap>() {
155             Ok(trap) => Box::into_raw(Box::new(wasm_trap_t::new(trap))),
156             Err(err) => Box::into_raw(Box::new(wasm_trap_t::new(err.into()))),
157         },
158         Err(panic) => {
159             let trap = if let Some(msg) = panic.downcast_ref::<String>() {
160                 Trap::new(msg)
161             } else if let Some(msg) = panic.downcast_ref::<&'static str>() {
162                 Trap::new(*msg)
163             } else {
164                 Trap::new("rust panic happened")
165             };
166             let trap = Box::new(wasm_trap_t::new(trap));
167             Box::into_raw(trap)
168         }
169     }
170 }
171 
172 #[no_mangle]
173 pub unsafe extern "C" fn wasm_func_type(f: &wasm_func_t) -> Box<wasm_functype_t> {
174     Box::new(wasm_functype_t::new(f.func().ty(f.ext.store.context())))
175 }
176 
177 #[no_mangle]
178 pub unsafe extern "C" fn wasm_func_param_arity(f: &wasm_func_t) -> usize {
179     f.func().ty(f.ext.store.context()).params().len()
180 }
181 
182 #[no_mangle]
183 pub unsafe extern "C" fn wasm_func_result_arity(f: &wasm_func_t) -> usize {
184     f.func().ty(f.ext.store.context()).results().len()
185 }
186 
187 #[no_mangle]
188 pub extern "C" fn wasm_func_as_extern(f: &mut wasm_func_t) -> &mut wasm_extern_t {
189     &mut (*f).ext
190 }
191 
192 #[no_mangle]
193 pub extern "C" fn wasm_func_as_extern_const(f: &wasm_func_t) -> &wasm_extern_t {
194     &(*f).ext
195 }
196 
197 #[repr(C)]
198 pub struct wasmtime_caller_t<'a> {
199     caller: Caller<'a, crate::StoreData>,
200 }
201 
202 pub type wasmtime_func_callback_t = extern "C" fn(
203     *mut c_void,
204     *mut wasmtime_caller_t,
205     *const wasmtime_val_t,
206     usize,
207     *mut wasmtime_val_t,
208     usize,
209 ) -> Option<Box<wasm_trap_t>>;
210 
211 pub type wasmtime_func_unchecked_callback_t =
212     extern "C" fn(*mut c_void, *mut wasmtime_caller_t, *mut ValRaw) -> Option<Box<wasm_trap_t>>;
213 
214 #[no_mangle]
215 pub unsafe extern "C" fn wasmtime_func_new(
216     store: CStoreContextMut<'_>,
217     ty: &wasm_functype_t,
218     callback: wasmtime_func_callback_t,
219     data: *mut c_void,
220     finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
221     func: &mut Func,
222 ) {
223     let ty = ty.ty().ty.clone();
224     let cb = c_callback_to_rust_fn(callback, data, finalizer);
225     let f = Func::new(store, ty, cb);
226     *func = f;
227 }
228 
229 pub(crate) unsafe fn c_callback_to_rust_fn(
230     callback: wasmtime_func_callback_t,
231     data: *mut c_void,
232     finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
233 ) -> impl Fn(Caller<'_, crate::StoreData>, &[Val], &mut [Val]) -> Result<(), Trap> {
234     let foreign = crate::ForeignData { data, finalizer };
235     move |mut caller, params, results| {
236         // Convert `params/results` to `wasmtime_val_t`. Use the previous
237         // storage in `hostcall_val_storage` to help avoid allocations all the
238         // time.
239         let mut vals = mem::take(&mut caller.data_mut().hostcall_val_storage);
240         debug_assert!(vals.is_empty());
241         vals.reserve(params.len() + results.len());
242         vals.extend(params.iter().cloned().map(|p| wasmtime_val_t::from_val(p)));
243         vals.extend((0..results.len()).map(|_| wasmtime_val_t {
244             kind: crate::WASMTIME_I32,
245             of: wasmtime_val_union { i32: 0 },
246         }));
247         let (params, out_results) = vals.split_at_mut(params.len());
248 
249         // Invoke the C function pointer, getting the results.
250         let mut caller = wasmtime_caller_t { caller };
251         let out = callback(
252             foreign.data,
253             &mut caller,
254             params.as_ptr(),
255             params.len(),
256             out_results.as_mut_ptr(),
257             out_results.len(),
258         );
259         if let Some(trap) = out {
260             return Err(trap.trap);
261         }
262 
263         // Translate the `wasmtime_val_t` results into the `results` space
264         for (i, result) in out_results.iter().enumerate() {
265             results[i] = unsafe { result.to_val() };
266         }
267 
268         // Move our `vals` storage back into the store now that we no longer
269         // need it. This'll get picked up by the next hostcall and reuse our
270         // same storage.
271         vals.truncate(0);
272         caller.caller.data_mut().hostcall_val_storage = vals;
273         Ok(())
274     }
275 }
276 
277 #[no_mangle]
278 pub unsafe extern "C" fn wasmtime_func_new_unchecked(
279     store: CStoreContextMut<'_>,
280     ty: &wasm_functype_t,
281     callback: wasmtime_func_unchecked_callback_t,
282     data: *mut c_void,
283     finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
284     func: &mut Func,
285 ) {
286     let ty = ty.ty().ty.clone();
287     let cb = c_unchecked_callback_to_rust_fn(callback, data, finalizer);
288     *func = Func::new_unchecked(store, ty, cb);
289 }
290 
291 pub(crate) unsafe fn c_unchecked_callback_to_rust_fn(
292     callback: wasmtime_func_unchecked_callback_t,
293     data: *mut c_void,
294     finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
295 ) -> impl Fn(Caller<'_, crate::StoreData>, *mut ValRaw) -> Result<(), Trap> {
296     let foreign = crate::ForeignData { data, finalizer };
297     move |caller, values| {
298         let mut caller = wasmtime_caller_t { caller };
299         match callback(foreign.data, &mut caller, values) {
300             None => Ok(()),
301             Some(trap) => Err(trap.trap),
302         }
303     }
304 }
305 
306 #[no_mangle]
307 pub unsafe extern "C" fn wasmtime_func_call(
308     mut store: CStoreContextMut<'_>,
309     func: &Func,
310     args: *const wasmtime_val_t,
311     nargs: usize,
312     results: *mut MaybeUninit<wasmtime_val_t>,
313     nresults: usize,
314     trap_ret: &mut *mut wasm_trap_t,
315 ) -> Option<Box<wasmtime_error_t>> {
316     let mut store = store.as_context_mut();
317     let mut params = mem::take(&mut store.data_mut().wasm_val_storage);
318     let (wt_params, wt_results) = translate_args(
319         &mut params,
320         crate::slice_from_raw_parts(args, nargs)
321             .iter()
322             .map(|i| i.to_val()),
323         nresults,
324     );
325 
326     // We're calling arbitrary code here most of the time, and we in general
327     // want to try to insulate callers against bugs in wasmtime/wasi/etc if we
328     // can. As a result we catch panics here and transform them to traps to
329     // allow the caller to have any insulation possible against Rust panics.
330     let result = panic::catch_unwind(AssertUnwindSafe(|| {
331         func.call(&mut store, wt_params, wt_results)
332     }));
333     match result {
334         Ok(Ok(())) => {
335             let results = crate::slice_from_raw_parts_mut(results, nresults);
336             for (slot, val) in results.iter_mut().zip(wt_results.iter()) {
337                 crate::initialize(slot, wasmtime_val_t::from_val(val.clone()));
338             }
339             params.truncate(0);
340             store.data_mut().wasm_val_storage = params;
341             None
342         }
343         Ok(Err(trap)) => match trap.downcast::<Trap>() {
344             Ok(trap) => {
345                 *trap_ret = Box::into_raw(Box::new(wasm_trap_t::new(trap)));
346                 None
347             }
348             Err(err) => Some(Box::new(wasmtime_error_t::from(err))),
349         },
350         Err(panic) => {
351             let trap = if let Some(msg) = panic.downcast_ref::<String>() {
352                 Trap::new(msg)
353             } else if let Some(msg) = panic.downcast_ref::<&'static str>() {
354                 Trap::new(*msg)
355             } else {
356                 Trap::new("rust panic happened")
357             };
358             *trap_ret = Box::into_raw(Box::new(wasm_trap_t::new(trap)));
359             None
360         }
361     }
362 }
363 
364 #[no_mangle]
365 pub unsafe extern "C" fn wasmtime_func_call_unchecked(
366     store: CStoreContextMut<'_>,
367     func: &Func,
368     args_and_results: *mut ValRaw,
369 ) -> *mut wasm_trap_t {
370     match func.call_unchecked(store, args_and_results) {
371         Ok(()) => ptr::null_mut(),
372         Err(trap) => Box::into_raw(Box::new(wasm_trap_t::new(trap))),
373     }
374 }
375 
376 #[no_mangle]
377 pub extern "C" fn wasmtime_func_type(
378     store: CStoreContext<'_>,
379     func: &Func,
380 ) -> Box<wasm_functype_t> {
381     Box::new(wasm_functype_t::new(func.ty(store)))
382 }
383 
384 #[no_mangle]
385 pub extern "C" fn wasmtime_caller_context<'a>(
386     caller: &'a mut wasmtime_caller_t,
387 ) -> CStoreContextMut<'a> {
388     caller.caller.as_context_mut()
389 }
390 
391 #[no_mangle]
392 pub unsafe extern "C" fn wasmtime_caller_export_get(
393     caller: &mut wasmtime_caller_t,
394     name: *const u8,
395     name_len: usize,
396     item: &mut MaybeUninit<wasmtime_extern_t>,
397 ) -> bool {
398     let name = match str::from_utf8(crate::slice_from_raw_parts(name, name_len)) {
399         Ok(name) => name,
400         Err(_) => return false,
401     };
402     let which = match caller.caller.get_export(name) {
403         Some(item) => item,
404         None => return false,
405     };
406     crate::initialize(item, which.into());
407     true
408 }
409 
410 #[no_mangle]
411 pub unsafe extern "C" fn wasmtime_func_from_raw(
412     store: CStoreContextMut<'_>,
413     raw: usize,
414     func: &mut Func,
415 ) {
416     *func = Func::from_raw(store, raw).unwrap();
417 }
418 
419 #[no_mangle]
420 pub unsafe extern "C" fn wasmtime_func_to_raw(store: CStoreContextMut<'_>, func: &Func) -> usize {
421     func.to_raw(store)
422 }
423