Skip to content

Commit bfdbd10

Browse files
authored
Add *_unchecked variants of Func APIs for the C API (#3350)
* Add `*_unchecked` variants of `Func` APIs for the C API This commit is what is hopefully going to be my last installment within the saga of optimizing function calls in/out of WebAssembly modules in the C API. This is yet another alternative approach to #3345 (sorry) but also contains everything necessary to make the C API fast. As in #3345 the general idea is just moving checks out of the call path in the same style of `TypedFunc`. This new strategy takes inspiration from previously learned attempts effectively "just" exposes how we previously passed `*mut u128` through trampolines for arguments/results. This storage format is formalized through a new `ValRaw` union that is exposed from the `wasmtime` crate. By doing this it made it relatively easy to expose two new APIs: * `Func::new_unchecked` * `Func::call_unchecked` These are the same as their checked equivalents except that they're `unsafe` and they work with `*mut ValRaw` rather than safe slices of `Val`. Working with these eschews type checks and such and requires callers/embedders to do the right thing. These two new functions are then exposed via the C API with new functions, enabling C to have a fast-path of calling/defining functions. This fast path is akin to `Func::wrap` in Rust, although that API can't be built in C due to C not having generics in the same way that Rust has. For some benchmarks, the benchmarks here are: * `nop` - Call a wasm function from the host that does nothing and returns nothing. * `i64` - Call a wasm function from the host, the wasm function calls a host function, and the host function returns an `i64` all the way out to the original caller. * `many` - Call a wasm function from the host, the wasm calls host function with 5 `i32` parameters, and then an `i64` result is returned back to the original host * `i64` host - just the overhead of the wasm calling the host, so the wasm calls the host function in a loop. * `many` host - same as `i64` host, but calling the `many` host function. All numbers in this table are in nanoseconds, and this is just one measurement as well so there's bound to be some variation in the precise numbers here. | Name | Rust | C (before) | C (after) | |-----------|------|------------|-----------| | nop | 19 | 112 | 25 | | i64 | 22 | 207 | 32 | | many | 27 | 189 | 34 | | i64 host | 2 | 38 | 5 | | many host | 7 | 75 | 8 | The main conclusion here is that the C API is significantly faster than before when using the `*_unchecked` variants of APIs. The Rust implementation is still the ceiling (or floor I guess?) for performance The main reason that C is slower than Rust is that a little bit more has to travel through memory where on the Rust side of things we can monomorphize and inline a bit more to get rid of that. Overall though the costs are way way down from where they were originally and I don't plan on doing a whole lot more myself at this time. There's various things we theoretically could do I've considered but implementation-wise I think they'll be much more weighty. * Tweak `wasmtime_externref_t` API comments
1 parent 344a219 commit bfdbd10

File tree

16 files changed

+657
-215
lines changed

16 files changed

+657
-215
lines changed

crates/c-api/include/wasmtime/func.h

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,75 @@ WASM_API_EXTERN void wasmtime_func_new(
8787
wasmtime_func_t *ret
8888
);
8989

90+
/**
91+
* \brief Callback signature for #wasmtime_func_new_unchecked.
92+
*
93+
* This is the function signature for host functions that can be made accessible
94+
* to WebAssembly. The arguments to this function are:
95+
*
96+
* \param env user-provided argument passed to #wasmtime_func_new_unchecked
97+
* \param caller a temporary object that can only be used during this function
98+
* call. Used to acquire #wasmtime_context_t or caller's state
99+
* \param args_and_results storage space for both the parameters to the
100+
* function as well as the results of the function. The size of this
101+
* array depends on the function type that the host function is created
102+
* with, but it will be the maximum of the number of parameters and
103+
* number of results.
104+
*
105+
* This callback can optionally return a #wasm_trap_t indicating that a trap
106+
* should be raised in WebAssembly. It's expected that in this case the caller
107+
* relinquishes ownership of the trap and it is passed back to the engine.
108+
*
109+
* This differs from #wasmtime_func_callback_t in that the payload of
110+
* `args_and_results` does not have type information, nor does it have sizing
111+
* information. This is especially unsafe because it's only valid within the
112+
* particular #wasm_functype_t that the function was created with. The onus is
113+
* on the embedder to ensure that `args_and_results` are all read correctly
114+
* for parameters and all written for results within the execution of a
115+
* function.
116+
*
117+
* Parameters will be listed starting at index 0 in the `args_and_results`
118+
* array. Results are also written starting at index 0, which will overwrite
119+
* the arguments.
120+
*/
121+
typedef wasm_trap_t* (*wasmtime_func_unchecked_callback_t)(
122+
void *env,
123+
wasmtime_caller_t* caller,
124+
wasmtime_val_raw_t *args_and_results);
125+
126+
/**
127+
* \brief Creates a new host function in the same manner of #wasmtime_func_new,
128+
* but the function-to-call has no type information available at runtime.
129+
*
130+
* This function is very similar to #wasmtime_func_new. The difference is that
131+
* this version is "more unsafe" in that when the host callback is invoked there
132+
* is no type information and no checks that the right types of values are
133+
* produced. The onus is on the consumer of this API to ensure that all
134+
* invariants are upheld such as:
135+
*
136+
* * The host callback reads parameters correctly and interprets their types
137+
* correctly.
138+
* * If a trap doesn't happen then all results are written to the results
139+
* pointer. All results must have the correct type.
140+
* * Types such as `funcref` cannot cross stores.
141+
* * Types such as `externref` have valid reference counts.
142+
*
143+
* It's generally only recommended to use this if your application can wrap
144+
* this in a safe embedding. This should not be frequently used due to the
145+
* number of invariants that must be upheld on the wasm<->host boundary. On the
146+
* upside, though, this flavor of host function will be faster to call than
147+
* those created by #wasmtime_func_new (hence the reason for this function's
148+
* existence).
149+
*/
150+
WASM_API_EXTERN void wasmtime_func_new_unchecked(
151+
wasmtime_context_t *store,
152+
const wasm_functype_t* type,
153+
wasmtime_func_unchecked_callback_t callback,
154+
void *env,
155+
void (*finalizer)(void*),
156+
wasmtime_func_t *ret
157+
);
158+
90159
/**
91160
* \brief Returns the type of the function specified
92161
*
@@ -142,6 +211,39 @@ WASM_API_EXTERN wasmtime_error_t *wasmtime_func_call(
142211
wasm_trap_t **trap
143212
);
144213

214+
/**
215+
* \brief Call a WebAssembly function in an "unchecked" fashion.
216+
*
217+
* This function is similar to #wasmtime_func_call except that there is no type
218+
* information provided with the arguments (or sizing information). Consequently
219+
* this is less safe to call since it's up to the caller to ensure that `args`
220+
* has an appropriate size and all the parameters are configured with their
221+
* appropriate values/types. Additionally all the results must be interpreted
222+
* correctly if this function returns successfully.
223+
*
224+
* Parameters must be specified starting at index 0 in the `args_and_results`
225+
* array. Results are written starting at index 0, which will overwrite
226+
* the arguments.
227+
*
228+
* Callers must ensure that various correctness variants are upheld when this
229+
* API is called such as:
230+
*
231+
* * The `args_and_results` pointer has enough space to hold all the parameters
232+
* and all the results (but not at the same time).
233+
* * Parameters must all be configured as if they were the correct type.
234+
* * Values such as `externref` and `funcref` are valid within the store being
235+
* called.
236+
*
237+
* When in doubt it's much safer to call #wasmtime_func_call. This function is
238+
* faster than that function, but the tradeoff is that embeddings must uphold
239+
* more invariants rather than relying on Wasmtime to check them for you.
240+
*/
241+
WASM_API_EXTERN wasm_trap_t *wasmtime_func_call_unchecked(
242+
wasmtime_context_t *store,
243+
const wasmtime_func_t *func,
244+
wasmtime_val_raw_t *args_and_results
245+
);
246+
145247
/**
146248
* \brief Loads a #wasmtime_extern_t from the caller's context
147249
*
@@ -172,6 +274,32 @@ WASM_API_EXTERN bool wasmtime_caller_export_get(
172274
*/
173275
WASM_API_EXTERN wasmtime_context_t* wasmtime_caller_context(wasmtime_caller_t* caller);
174276

277+
/**
278+
* \brief Converts a `raw` nonzero `funcref` value from #wasmtime_val_raw_t
279+
* into a #wasmtime_func_t.
280+
*
281+
* This function can be used to interpret nonzero values of the `funcref` field
282+
* of the #wasmtime_val_raw_t structure. It is assumed that `raw` does not have
283+
* a value of 0, or otherwise the program will abort.
284+
*
285+
* Note that this function is unchecked and unsafe. It's only safe to pass
286+
* values learned from #wasmtime_val_raw_t with the same corresponding
287+
* #wasmtime_context_t that they were produced from. Providing arbitrary values
288+
* to `raw` here or cross-context values with `context` is UB.
289+
*/
290+
WASM_API_EXTERN void wasmtime_func_from_raw(
291+
wasmtime_context_t* context,
292+
size_t raw,
293+
wasmtime_func_t *ret);
294+
295+
/**
296+
* \brief Converts a `func` which belongs to `context` into a `usize`
297+
* parameter that is suitable for insertion into a #wasmtime_val_raw_t.
298+
*/
299+
WASM_API_EXTERN size_t wasmtime_func_to_raw(
300+
wasmtime_context_t* context,
301+
const wasmtime_func_t *func);
302+
175303
#ifdef __cplusplus
176304
} // extern "C"
177305
#endif

crates/c-api/include/wasmtime/linker.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ WASM_API_EXTERN wasmtime_error_t* wasmtime_linker_define(
102102
* Note that this function does not create a #wasmtime_func_t. This creates a
103103
* store-independent function within the linker, allowing this function
104104
* definition to be used with multiple stores.
105+
*
106+
* For more information about host callbacks see #wasmtime_func_new.
105107
*/
106108
WASM_API_EXTERN wasmtime_error_t* wasmtime_linker_define_func(
107109
wasmtime_linker_t *linker,
@@ -115,6 +117,27 @@ WASM_API_EXTERN wasmtime_error_t* wasmtime_linker_define_func(
115117
void (*finalizer)(void*)
116118
);
117119

120+
/**
121+
* \brief Defines a new function in this linker.
122+
*
123+
* This is the same as #wasmtime_linker_define_func except that it's the analog
124+
* of #wasmtime_func_new_unchecked instead of #wasmtime_func_new. Be sure to
125+
* consult the documentation of #wasmtime_linker_define_func for argument
126+
* information as well as #wasmtime_func_new_unchecked for why this is an
127+
* unsafe API.
128+
*/
129+
WASM_API_EXTERN wasmtime_error_t* wasmtime_linker_define_func_unchecked(
130+
wasmtime_linker_t *linker,
131+
const char *module,
132+
size_t module_len,
133+
const char *name,
134+
size_t name_len,
135+
const wasm_functype_t *ty,
136+
wasmtime_func_unchecked_callback_t cb,
137+
void *data,
138+
void (*finalizer)(void*)
139+
);
140+
118141
/**
119142
* \brief Defines WASI functions in this linker.
120143
*

crates/c-api/include/wasmtime/val.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ WASM_API_EXTERN wasmtime_externref_t *wasmtime_externref_clone(wasmtime_externre
6363
*/
6464
WASM_API_EXTERN void wasmtime_externref_delete(wasmtime_externref_t *ref);
6565

66+
/**
67+
* \brief Converts a raw `externref` value coming from #wasmtime_val_raw_t into
68+
* a #wasmtime_externref_t.
69+
*
70+
* Note that the returned #wasmtime_externref_t is an owned value that must be
71+
* deleted via #wasmtime_externref_delete by the caller if it is non-null.
72+
*/
73+
WASM_API_EXTERN wasmtime_externref_t *wasmtime_externref_from_raw(wasmtime_context_t *context, size_t raw);
74+
75+
/**
76+
* \brief Converts a #wasmtime_externref_t to a raw value suitable for storing
77+
* into a #wasmtime_val_raw_t.
78+
*
79+
* Note that the returned underlying value is not tracked by Wasmtime's garbage
80+
* collector until it enters WebAssembly. This means that a GC may release the
81+
* context's reference to the raw value, making the raw value invalid within the
82+
* context of the store. Do not perform a GC between calling this function and
83+
* passing it to WebAssembly.
84+
*/
85+
WASM_API_EXTERN size_t wasmtime_externref_to_raw(
86+
wasmtime_context_t *context,
87+
const wasmtime_externref_t *ref);
88+
6689
/// \brief Discriminant stored in #wasmtime_val::kind
6790
typedef uint8_t wasmtime_valkind_t;
6891
/// \brief Value of #wasmtime_valkind_t meaning that #wasmtime_val_t is an i32
@@ -117,6 +140,43 @@ typedef union wasmtime_valunion {
117140
wasmtime_v128 v128;
118141
} wasmtime_valunion_t;
119142

143+
/**
144+
* \typedef wasmtime_val_raw_t
145+
* \brief Convenience alias for #wasmtime_val_raw
146+
*
147+
* \union wasmtime_val_raw
148+
* \brief Container for possible wasm values.
149+
*
150+
* This type is used on conjunction with #wasmtime_func_new_unchecked as well
151+
* as #wasmtime_func_call_unchecked. Instances of this type do not have type
152+
* information associated with them, it's up to the embedder to figure out
153+
* how to interpret the bits contained within, often using some other channel
154+
* to determine the type.
155+
*/
156+
typedef union wasmtime_val_raw {
157+
/// Field for when this val is a WebAssembly `i32` value.
158+
int32_t i32;
159+
/// Field for when this val is a WebAssembly `i64` value.
160+
int64_t i64;
161+
/// Field for when this val is a WebAssembly `f32` value.
162+
float32_t f32;
163+
/// Field for when this val is a WebAssembly `f64` value.
164+
float64_t f64;
165+
/// Field for when this val is a WebAssembly `v128` value.
166+
wasmtime_v128 v128;
167+
/// Field for when this val is a WebAssembly `funcref` value.
168+
///
169+
/// If this is set to 0 then it's a null funcref, otherwise this must be
170+
/// passed to `wasmtime_func_from_raw` to determine the `wasmtime_func_t`.
171+
size_t funcref;
172+
/// Field for when this val is a WebAssembly `externref` value.
173+
///
174+
/// If this is set to 0 then it's a null externref, otherwise this must be
175+
/// passed to `wasmtime_externref_from_raw` to determine the
176+
/// `wasmtime_externref_t`.
177+
size_t externref;
178+
} wasmtime_val_raw_t;
179+
120180
/**
121181
* \typedef wasmtime_val_t
122182
* \brief Convenience alias for #wasmtime_val_t

crates/c-api/src/func.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::mem::{self, MaybeUninit};
88
use std::panic::{self, AssertUnwindSafe};
99
use std::ptr;
1010
use std::str;
11-
use wasmtime::{AsContextMut, Caller, Extern, Func, Trap, Val};
11+
use wasmtime::{AsContextMut, Caller, Extern, Func, Trap, Val, ValRaw};
1212

1313
#[derive(Clone)]
1414
#[repr(transparent)]
@@ -208,6 +208,9 @@ pub type wasmtime_func_callback_t = extern "C" fn(
208208
usize,
209209
) -> Option<Box<wasm_trap_t>>;
210210

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+
211214
#[no_mangle]
212215
pub unsafe extern "C" fn wasmtime_func_new(
213216
store: CStoreContextMut<'_>,
@@ -271,6 +274,35 @@ pub(crate) unsafe fn c_callback_to_rust_fn(
271274
}
272275
}
273276

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+
274306
#[no_mangle]
275307
pub unsafe extern "C" fn wasmtime_func_call(
276308
mut store: CStoreContextMut<'_>,
@@ -329,6 +361,18 @@ pub unsafe extern "C" fn wasmtime_func_call(
329361
}
330362
}
331363

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+
332376
#[no_mangle]
333377
pub extern "C" fn wasmtime_func_type(
334378
store: CStoreContext<'_>,
@@ -362,3 +406,17 @@ pub unsafe extern "C" fn wasmtime_caller_export_get(
362406
crate::initialize(item, which.into());
363407
true
364408
}
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+
}

0 commit comments

Comments
 (0)