xref: /wasmtime-44.0.1/crates/c-api/src/func.rs (revision bcf35449)
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};
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 #[no_mangle]
212 pub unsafe extern "C" fn wasmtime_func_new(
213     store: CStoreContextMut<'_>,
214     ty: &wasm_functype_t,
215     callback: wasmtime_func_callback_t,
216     data: *mut c_void,
217     finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
218     func: &mut Func,
219 ) {
220     let ty = ty.ty().ty.clone();
221     let cb = c_callback_to_rust_fn(callback, data, finalizer);
222     let f = Func::new(store, ty, cb);
223     *func = f;
224 }
225 
226 pub(crate) unsafe fn c_callback_to_rust_fn(
227     callback: wasmtime_func_callback_t,
228     data: *mut c_void,
229     finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
230 ) -> impl Fn(Caller<'_, crate::StoreData>, &[Val], &mut [Val]) -> Result<(), Trap> {
231     let foreign = crate::ForeignData { data, finalizer };
232     move |mut caller, params, results| {
233         // Convert `params/results` to `wasmtime_val_t`. Use the previous
234         // storage in `hostcall_val_storage` to help avoid allocations all the
235         // time.
236         let mut vals = mem::take(&mut caller.data_mut().hostcall_val_storage);
237         debug_assert!(vals.is_empty());
238         vals.reserve(params.len() + results.len());
239         vals.extend(params.iter().cloned().map(|p| wasmtime_val_t::from_val(p)));
240         vals.extend((0..results.len()).map(|_| wasmtime_val_t {
241             kind: crate::WASMTIME_I32,
242             of: wasmtime_val_union { i32: 0 },
243         }));
244         let (params, out_results) = vals.split_at_mut(params.len());
245 
246         // Invoke the C function pointer, getting the results.
247         let mut caller = wasmtime_caller_t { caller };
248         let out = callback(
249             foreign.data,
250             &mut caller,
251             params.as_ptr(),
252             params.len(),
253             out_results.as_mut_ptr(),
254             out_results.len(),
255         );
256         if let Some(trap) = out {
257             return Err(trap.trap);
258         }
259 
260         // Translate the `wasmtime_val_t` results into the `results` space
261         for (i, result) in out_results.iter().enumerate() {
262             results[i] = unsafe { result.to_val() };
263         }
264 
265         // Move our `vals` storage back into the store now that we no longer
266         // need it. This'll get picked up by the next hostcall and reuse our
267         // same storage.
268         vals.truncate(0);
269         caller.caller.data_mut().hostcall_val_storage = vals;
270         Ok(())
271     }
272 }
273 
274 #[no_mangle]
275 pub unsafe extern "C" fn wasmtime_func_call(
276     mut store: CStoreContextMut<'_>,
277     func: &Func,
278     args: *const wasmtime_val_t,
279     nargs: usize,
280     results: *mut MaybeUninit<wasmtime_val_t>,
281     nresults: usize,
282     trap_ret: &mut *mut wasm_trap_t,
283 ) -> Option<Box<wasmtime_error_t>> {
284     let mut store = store.as_context_mut();
285     let mut params = mem::take(&mut store.data_mut().wasm_val_storage);
286     let (wt_params, wt_results) = translate_args(
287         &mut params,
288         crate::slice_from_raw_parts(args, nargs)
289             .iter()
290             .map(|i| i.to_val()),
291         nresults,
292     );
293 
294     // We're calling arbitrary code here most of the time, and we in general
295     // want to try to insulate callers against bugs in wasmtime/wasi/etc if we
296     // can. As a result we catch panics here and transform them to traps to
297     // allow the caller to have any insulation possible against Rust panics.
298     let result = panic::catch_unwind(AssertUnwindSafe(|| {
299         func.call(&mut store, wt_params, wt_results)
300     }));
301     match result {
302         Ok(Ok(())) => {
303             let results = crate::slice_from_raw_parts_mut(results, nresults);
304             for (slot, val) in results.iter_mut().zip(wt_results.iter()) {
305                 crate::initialize(slot, wasmtime_val_t::from_val(val.clone()));
306             }
307             params.truncate(0);
308             store.data_mut().wasm_val_storage = params;
309             None
310         }
311         Ok(Err(trap)) => match trap.downcast::<Trap>() {
312             Ok(trap) => {
313                 *trap_ret = Box::into_raw(Box::new(wasm_trap_t::new(trap)));
314                 None
315             }
316             Err(err) => Some(Box::new(wasmtime_error_t::from(err))),
317         },
318         Err(panic) => {
319             let trap = if let Some(msg) = panic.downcast_ref::<String>() {
320                 Trap::new(msg)
321             } else if let Some(msg) = panic.downcast_ref::<&'static str>() {
322                 Trap::new(*msg)
323             } else {
324                 Trap::new("rust panic happened")
325             };
326             *trap_ret = Box::into_raw(Box::new(wasm_trap_t::new(trap)));
327             None
328         }
329     }
330 }
331 
332 #[no_mangle]
333 pub extern "C" fn wasmtime_func_type(
334     store: CStoreContext<'_>,
335     func: &Func,
336 ) -> Box<wasm_functype_t> {
337     Box::new(wasm_functype_t::new(func.ty(store)))
338 }
339 
340 #[no_mangle]
341 pub extern "C" fn wasmtime_caller_context<'a>(
342     caller: &'a mut wasmtime_caller_t,
343 ) -> CStoreContextMut<'a> {
344     caller.caller.as_context_mut()
345 }
346 
347 #[no_mangle]
348 pub unsafe extern "C" fn wasmtime_caller_export_get(
349     caller: &mut wasmtime_caller_t,
350     name: *const u8,
351     name_len: usize,
352     item: &mut MaybeUninit<wasmtime_extern_t>,
353 ) -> bool {
354     let name = match str::from_utf8(crate::slice_from_raw_parts(name, name_len)) {
355         Ok(name) => name,
356         Err(_) => return false,
357     };
358     let which = match caller.caller.get_export(name) {
359         Some(item) => item,
360         None => return false,
361     };
362     crate::initialize(item, which.into());
363     true
364 }
365