Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Document, adjust, and refactor some aborts in the C API (#14943)

Alex Crichtonefcc95eb2e98
diff --git a/crates/c-api/include/doc-wasm.h b/crates/c-api/include/doc-wasm.h
index 24c3e283ed..787f6ea574 100644
--- a/crates/c-api/include/doc-wasm.h
+++ b/crates/c-api/include/doc-wasm.h
@@ -130,8 +130,9 @@
* This function will take ownership of the configuration specified regardless
* of the outcome of this function. You do not need to call #wasm_config_delete
* on the argument. The object returned is owned by the caller and will need to
- * be deleted with #wasm_engine_delete. This may return `NULL` if the engine
- * could not be allocated.
+ * be deleted with #wasm_engine_delete. This returns `NULL` if the
+ * configuration is invalid, for example if an unknown Cranelift setting was
+ * passed to #wasmtime_config_cranelift_flag_set.
*
* \fn void wasm_engine_delete(wasm_engine_t*);
* \brief Deletes an engine.
@@ -298,6 +299,9 @@
*
* \fn wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t *);
* \brief Returns the associated kind for this value type.
+ *
+ * Aborts the process if this is a `v128` type or a reference type other than
+ * `externref` and `funcref`, which have no #wasm_valkind_t.
*/
/**
@@ -526,6 +530,11 @@
*
* The returned #wasm_limits_t is owned by the #wasm_tabletype_t parameter, the
* caller should not deallocate it.
+ *
+ * This function aborts the process if the table's minimum or maximum doesn't
+ * fit in 32 bits, which a 64-bit table's limits may not. For compatibility
+ * with 64-bit tables use #wasmtime_tabletype_maximum and
+ * #wasmtime_tabletype_minimum instead.
*/
/**
@@ -613,8 +622,10 @@
* The returned #wasm_limits_t is owned by the #wasm_memorytype_t parameter, the
* caller should not deallocate it.
*
- * For compatibility with memory64 it's recommended to use
- * #wasmtime_memorytype_maximum or #wasmtime_memorytype_minimum instead.
+ * This function aborts the process if the memory's minimum or maximum doesn't
+ * fit in 32 bits, which a 64-bit memory's limits may not. For compatibility
+ * with memory64 use #wasmtime_memorytype_maximum and
+ * #wasmtime_memorytype_minimum instead.
*/
/**
@@ -1061,6 +1072,13 @@
* #wasm_val_delete function does not delete the memory holding the #wasm_val_t
* itself, but only the memory pointed to by #wasm_val_t.
*
+ * Wasmtime only supports `i32`, `i64`, `f32`, `f64` and `funcref` values in
+ * #wasm_val_t. APIs that produce or consume a #wasm_val_t, such as
+ * #wasm_func_call, #wasm_global_get and host functions created with
+ * #wasm_func_new, abort the process when they encounter any other kind of
+ * value, such as a `v128` or `externref`. Use the `wasmtime_*` APIs and
+ * #wasmtime_val_t for those.
+ *
* \var wasm_val_t::kind
* \brief The kind of this value, or which of the fields in the `of` payload
* contains the actual value.
@@ -1562,6 +1580,9 @@
* The returned #wasm_func_t is expected to be deleted by the caller. This
* function does not take ownership of its arguments.
*
+ * Calling the function aborts the process if its parameters or results
+ * include a kind of value #wasm_val_t doesn't support (see #wasm_val_t).
+ *
* \fn wasm_func_t *wasm_func_new_with_env(
* wasm_store_t *,
* const wasm_functype_t *type,
@@ -1612,6 +1633,9 @@
* If a trap happens during execution or some other error then a non-`NULL` trap
* is returned. In this situation the `results` are is unmodified.
*
+ * Aborts the process if any argument or result is a kind of value
+ * #wasm_val_t doesn't support (see #wasm_val_t).
+ *
* Does not take ownership of `wasm_val_t` arguments. Gives ownership of
* `wasm_val_t` results.
*/
@@ -1673,6 +1697,9 @@
* This function does not take ownership of any of its arguments. The caller is
* expected to deallocate the returned value.
*
+ * Aborts the process if the value is a kind of value #wasm_val_t doesn't
+ * support (see #wasm_val_t).
+ *
* \fn wasm_globaltype_t *wasm_global_type(const wasm_global_t *);
* \brief Returns the type of this global.
*
@@ -1684,6 +1711,9 @@
* The caller is expected to deallocate the returned #wasm_val_t. The provided
* `out` argument is treated as uninitialized on input.
*
+ * Aborts the process if the global's value is a kind of value #wasm_val_t
+ * doesn't support, such as a `v128` or `externref` (see #wasm_val_t).
+ *
* \fn void wasm_global_set(wasm_global_t *, const wasm_val_t *);
* \brief Sets the value of this global.
*
@@ -1692,6 +1722,9 @@
* the wrong type, or if the provided value comes from a different store as the
* #wasm_global_t.
*
+ * Aborts the process if the value is a kind of value #wasm_val_t doesn't
+ * support (see #wasm_val_t).
+ *
* This function does not take ownership of its arguments.
*/
diff --git a/crates/c-api/include/wasmtime/async.h b/crates/c-api/include/wasmtime/async.h
index d65dedd089..0da84996e5 100644
--- a/crates/c-api/include/wasmtime/async.h
+++ b/crates/c-api/include/wasmtime/async.h
@@ -142,6 +142,8 @@ typedef struct wasmtime_async_continuation_t {
* trap is transferred to wasmtime.
* \param continuation_ret the returned continuation
* that determines when the async function has completed executing.
+ * This doesn't need to be set if `trap_ret` is set before this
+ * function returns.
*
* Only supported for async stores.
*
diff --git a/crates/c-api/include/wasmtime/engine.hh b/crates/c-api/include/wasmtime/engine.hh
index c8682c22f0..ba7174d2b9 100644
--- a/crates/c-api/include/wasmtime/engine.hh
+++ b/crates/c-api/include/wasmtime/engine.hh
@@ -5,6 +5,7 @@
#ifndef WASMTIME_ENGINE_HH
#define WASMTIME_ENGINE_HH
+#include <cstdlib>
#include <memory>
#include <wasmtime/config.hh>
#include <wasmtime/engine.h>
@@ -28,8 +29,14 @@ class Engine {
/// \brief Creates an engine with default compilation settings.
Engine() : ptr(wasm_engine_new()) {}
/// \brief Creates an engine with the specified compilation settings.
+ ///
+ /// Aborts the process if the configuration is invalid.
explicit Engine(Config config)
- : ptr(wasm_engine_new_with_config(config.capi_release())) {}
+ : ptr(wasm_engine_new_with_config(config.capi_release())) {
+ if (!ptr) {
+ std::abort();
+ }
+ }
/// \brief Increments the current epoch which may result in interrupting
/// currently executing WebAssembly in connected stores if the epoch is now
diff --git a/crates/c-api/include/wasmtime/func.h b/crates/c-api/include/wasmtime/func.h
index 41ff907c37..513608e769 100644
--- a/crates/c-api/include/wasmtime/func.h
+++ b/crates/c-api/include/wasmtime/func.h
@@ -176,7 +176,9 @@ wasmtime_func_type(const wasmtime_context_t *store,
* wasn't written to and `trap` will have `NULL` written to it. This state
* means that programmer error happened when calling the function, for
* example when the size of the arguments/results was wrong, the types of the
- * arguments were wrong, or arguments may come from the wrong store.
+ * arguments were wrong, or a `funcref` argument came from the wrong store.
+ * Note that GC reference arguments (`externref`, `anyref` and `exnref`)
+ * from the wrong store abort the process instead (see #wasmtime_val_t).
* 2. The trap pointer is filled in. This means the returned error is `NULL` and
* `results` was not written to. This state means that the function was
* executing but hit a wasm trap while executing.
diff --git a/crates/c-api/include/wasmtime/table.h b/crates/c-api/include/wasmtime/table.h
index 7a648691ca..42c71af1d6 100644
--- a/crates/c-api/include/wasmtime/table.h
+++ b/crates/c-api/include/wasmtime/table.h
@@ -17,6 +17,32 @@
extern "C" {
#endif
+/**
+ * \brief Returns the minimum size, in elements, of the specified table type.
+ *
+ * Note that this function is preferred over #wasm_tabletype_limits for
+ * compatibility with 64-bit tables.
+ */
+WASM_API_EXTERN uint64_t wasmtime_tabletype_minimum(const wasm_tabletype_t *ty);
+
+/**
+ * \brief Returns the maximum size, in elements, of the specified table type.
+ *
+ * If this table type doesn't have a maximum size listed then `false` is
+ * returned. Otherwise `true` is returned and the `max` pointer is filled in
+ * with the specified maximum size, in elements.
+ *
+ * Note that this function is preferred over #wasm_tabletype_limits for
+ * compatibility with 64-bit tables.
+ */
+WASM_API_EXTERN bool wasmtime_tabletype_maximum(const wasm_tabletype_t *ty,
+ uint64_t *max);
+
+/**
+ * \brief Returns whether this type of table represents a 64-bit table.
+ */
+WASM_API_EXTERN bool wasmtime_tabletype_is64(const wasm_tabletype_t *ty);
+
/**
* \brief Creates a new host-defined wasm table.
*
diff --git a/crates/c-api/include/wasmtime/types/table.hh b/crates/c-api/include/wasmtime/types/table.hh
index 1bb8b68609..dd898bc5ab 100644
--- a/crates/c-api/include/wasmtime/types/table.hh
+++ b/crates/c-api/include/wasmtime/types/table.hh
@@ -6,6 +6,7 @@
#define WASMTIME_TYPES_TABLE_HH
#include <optional>
+#include <wasmtime/table.h>
#include <wasmtime/types/val.hh>
namespace wasmtime {
@@ -37,17 +38,20 @@ public:
Ref(const TableType &ty) : Ref(ty.ptr.get()) {}
/// Returns the minimum size of this table type, in elements.
- uint32_t min() const { return wasm_tabletype_limits(ptr)->min; }
+ uint64_t min() const { return wasmtime_tabletype_minimum(ptr); }
/// Returns the maximum size of this table type, in elements, if present.
- std::optional<uint32_t> max() const {
- const auto *limits = wasm_tabletype_limits(ptr);
- if (limits->max == wasm_limits_max_default) {
- return std::nullopt;
+ std::optional<uint64_t> max() const {
+ uint64_t max = 0;
+ if (wasmtime_tabletype_maximum(ptr, &max)) {
+ return max;
}
- return limits->max;
+ return std::nullopt;
}
+ /// Returns whether or not this is a 64-bit table type.
+ bool is_64() const { return wasmtime_tabletype_is64(ptr); }
+
/// Returns the type of value that is stored in this table.
ValType::Ref element() const { return wasm_tabletype_element(ptr); }
};
diff --git a/crates/c-api/include/wasmtime/val.h b/crates/c-api/include/wasmtime/val.h
index 54fe6b9dc9..79cb7b3460 100644
--- a/crates/c-api/include/wasmtime/val.h
+++ b/crates/c-api/include/wasmtime/val.h
@@ -391,6 +391,10 @@ static inline void __wasmtime_val_assertions() {
* If you do not unroot the value, *even if you free the corresponding
* Store*, there will be some memory leaked, because GC roots use a
* separate allocation to track liveness.
+ *
+ * GC references (`externref`, `anyref` and `exnref`) cannot interoperate
+ * between #wasmtime_store_t instances. Passing one to an API of a store other
+ * than the one it was created in will abort the process.
*/
typedef struct wasmtime_val {
/// Discriminant of which field of #of is valid.
diff --git a/crates/c-api/src/async.rs b/crates/c-api/src/async.rs
index 6e7b68bbd7..97ad552771 100644
--- a/crates/c-api/src/async.rs
+++ b/crates/c-api/src/async.rs
@@ -140,7 +140,9 @@ async fn invoke_c_async_callback<'a>(
&mut trap,
&mut continuation,
);
- continuation.await;
+ if trap.is_none() {
+ continuation.await;
+ }
if let Some(trap) = trap {
return Err(trap.error);
diff --git a/crates/c-api/src/component/func.rs b/crates/c-api/src/component/func.rs
index b879a56447..ba9eb0098c 100644
--- a/crates/c-api/src/component/func.rs
+++ b/crates/c-api/src/component/func.rs
@@ -14,10 +14,12 @@ pub unsafe extern "C" fn wasmtime_component_func_call(
let c_args = unsafe { crate::slice_from_raw_parts(args, args_len) };
let c_results = unsafe { crate::slice_from_raw_parts_mut(results, results_len) };
- let args = c_args.iter().map(Val::from).collect::<Vec<_>>();
let mut results = vec![Val::Bool(false); results_len];
-
- let result = func.call(&mut context, &args, &mut results);
+ let result = c_args
+ .iter()
+ .map(Val::try_from)
+ .collect::<wasmtime::Result<Vec<_>>>()
+ .and_then(|args| func.call(&mut context, &args, &mut results));
crate::handle_result(result, |_| {
for (c_val, rust_val) in std::iter::zip(c_results, results) {
@@ -40,7 +42,17 @@ pub unsafe extern "C" fn wasmtime_component_func_call_async<'a>(
let c_args = unsafe { crate::slice_from_raw_parts(args, args_len) };
let c_results = unsafe { crate::slice_from_raw_parts_mut(results, results_len) };
let fut = Box::pin(async move {
- let args = c_args.iter().map(Val::from).collect::<Vec<_>>();
+ let args = match c_args
+ .iter()
+ .map(Val::try_from)
+ .collect::<wasmtime::Result<Vec<_>>>()
+ {
+ Ok(args) => args,
+ Err(err) => {
+ *err_ret = Box::into_raw(Box::new(wasmtime_error_t::from(err)));
+ return;
+ }
+ };
let mut results = vec![Val::Bool(false); c_results.len()];
match func.call_async(&mut context, &args, &mut results).await {
diff --git a/crates/c-api/src/component/linker.rs b/crates/c-api/src/component/linker.rs
index b4c1d88b72..7e1c3ecfcf 100644
--- a/crates/c-api/src/component/linker.rs
+++ b/crates/c-api/src/component/linker.rs
@@ -149,7 +149,7 @@ pub unsafe extern "C" fn wasmtime_component_linker_instance_add_func(
}
for (rust_val, c_val) in std::iter::zip(rets, c_rets) {
- *rust_val = Val::from(&c_val);
+ *rust_val = Val::try_from(&c_val)?;
}
Ok(())
@@ -229,7 +229,7 @@ pub unsafe extern "C" fn wasmtime_component_linker_instance_add_func_async(
Box::new(async move {
continuation.await;
for (rust_val, c_val) in std::iter::zip(rets, c_rets) {
- *rust_val = Val::from(&c_val);
+ *rust_val = Val::try_from(&c_val)?;
}
Ok(())
})
diff --git a/crates/c-api/src/component/val.rs b/crates/c-api/src/component/val.rs
index c57365493f..e0274038bc 100644
--- a/crates/c-api/src/component/val.rs
+++ b/crates/c-api/src/component/val.rs
@@ -7,6 +7,7 @@ use std::mem::{ManuallyDrop, MaybeUninit};
use std::ptr;
use std::slice;
use wasmtime::component::{ResourceAny, ResourceDynamic, Val};
+use wasmtime::{Error, Result, format_err};
crate::declare_vecs! {
(
@@ -56,9 +57,10 @@ crate::declare_vecs! {
)
}
-impl From<&wasmtime_component_vallist_t> for Vec<Val> {
- fn from(value: &wasmtime_component_vallist_t) -> Self {
- value.as_slice().iter().map(Val::from).collect()
+impl TryFrom<&wasmtime_component_vallist_t> for Vec<Val> {
+ type Error = Error;
+ fn try_from(value: &wasmtime_component_vallist_t) -> Result<Self> {
+ value.as_slice().iter().map(Val::try_from).collect()
}
}
@@ -72,12 +74,13 @@ impl From<&[Val]> for wasmtime_component_vallist_t {
}
}
-impl From<&wasmtime_component_valmap_t> for Vec<(Val, Val)> {
- fn from(value: &wasmtime_component_valmap_t) -> Self {
+impl TryFrom<&wasmtime_component_valmap_t> for Vec<(Val, Val)> {
+ type Error = Error;
+ fn try_from(value: &wasmtime_component_valmap_t) -> Result<Self> {
value
.as_slice()
.iter()
- .map(|entry| (Val::from(&entry.key), Val::from(&entry.value)))
+ .map(|entry| Ok((Val::try_from(&entry.key)?, Val::try_from(&entry.value)?)))
.collect()
}
}
@@ -118,12 +121,10 @@ impl Default for wasmtime_component_valrecord_entry_t {
}
}
-impl From<&wasmtime_component_valrecord_entry_t> for (String, Val) {
- fn from(value: &wasmtime_component_valrecord_entry_t) -> Self {
- (
- String::from_utf8(value.name.clone().take()).unwrap(),
- Val::from(&value.val),
- )
+impl TryFrom<&wasmtime_component_valrecord_entry_t> for (String, Val) {
+ type Error = Error;
+ fn try_from(value: &wasmtime_component_valrecord_entry_t) -> Result<Self> {
+ Ok((name_to_string(&value.name)?, Val::try_from(&value.val)?))
}
}
@@ -136,9 +137,10 @@ impl From<&(String, Val)> for wasmtime_component_valrecord_entry_t {
}
}
-impl From<&wasmtime_component_valrecord_t> for Vec<(String, Val)> {
- fn from(value: &wasmtime_component_valrecord_t) -> Self {
- value.as_slice().iter().map(Into::into).collect()
+impl TryFrom<&wasmtime_component_valrecord_t> for Vec<(String, Val)> {
+ type Error = Error;
+ fn try_from(value: &wasmtime_component_valrecord_t) -> Result<Self> {
+ value.as_slice().iter().map(TryInto::try_into).collect()
}
}
@@ -152,9 +154,10 @@ impl From<&[(String, Val)]> for wasmtime_component_valrecord_t {
}
}
-impl From<&wasmtime_component_valtuple_t> for Vec<Val> {
- fn from(value: &wasmtime_component_valtuple_t) -> Self {
- value.as_slice().iter().map(Val::from).collect()
+impl TryFrom<&wasmtime_component_valtuple_t> for Vec<Val> {
+ type Error = Error;
+ fn try_from(value: &wasmtime_component_valtuple_t) -> Result<Self> {
+ value.as_slice().iter().map(Val::try_from).collect()
}
}
@@ -168,15 +171,10 @@ impl From<&[Val]> for wasmtime_component_valtuple_t {
}
}
-impl From<&wasmtime_component_valflags_t> for Vec<String> {
- fn from(value: &wasmtime_component_valflags_t) -> Self {
- value
- .clone()
- .take()
- .into_iter()
- .map(|mut x| String::from_utf8(x.take()))
- .collect::<Result<Vec<_>, _>>()
- .unwrap()
+impl TryFrom<&wasmtime_component_valflags_t> for Vec<String> {
+ type Error = Error;
+ fn try_from(value: &wasmtime_component_valflags_t) -> Result<Self> {
+ value.as_slice().iter().map(name_to_string).collect()
}
}
@@ -208,12 +206,13 @@ impl From<(&String, &Option<Box<Val>>)> for wasmtime_component_valvariant_t {
}
}
-impl From<&wasmtime_component_valvariant_t> for (String, Option<Box<Val>>) {
- fn from(value: &wasmtime_component_valvariant_t) -> Self {
- (
- String::from_utf8(value.discriminant.clone().take()).unwrap(),
- value.val.as_ref().map(|x| Box::new(Val::from(x.as_ref()))),
- )
+impl TryFrom<&wasmtime_component_valvariant_t> for (String, Option<Box<Val>>) {
+ type Error = Error;
+ fn try_from(value: &wasmtime_component_valvariant_t) -> Result<Self> {
+ Ok((
+ name_to_string(&value.discriminant)?,
+ option_box_try_from(&value.val)?,
+ ))
}
}
@@ -224,19 +223,24 @@ pub struct wasmtime_component_valresult_t {
val: Option<Box<wasmtime_component_val_t>>,
}
-impl From<&wasmtime_component_valresult_t> for Result<Option<Box<Val>>, Option<Box<Val>>> {
- fn from(value: &wasmtime_component_valresult_t) -> Self {
- let val = value.val.as_ref().map(|x| Box::new(Val::from(x.as_ref())));
+impl TryFrom<&wasmtime_component_valresult_t>
+ for core::result::Result<Option<Box<Val>>, Option<Box<Val>>>
+{
+ type Error = Error;
+ fn try_from(value: &wasmtime_component_valresult_t) -> Result<Self> {
+ let val = option_box_try_from(&value.val)?;
- match value.is_ok {
+ Ok(match value.is_ok {
true => Ok(val),
false => Err(val),
- }
+ })
}
}
-impl From<&Result<Option<Box<Val>>, Option<Box<Val>>>> for wasmtime_component_valresult_t {
- fn from(value: &Result<Option<Box<Val>>, Option<Box<Val>>>) -> Self {
+impl From<&core::result::Result<Option<Box<Val>>, Option<Box<Val>>>>
+ for wasmtime_component_valresult_t
+{
+ fn from(value: &core::result::Result<Option<Box<Val>>, Option<Box<Val>>>) -> Self {
let (Ok(x) | Err(x)) = value;
Self {
@@ -287,9 +291,20 @@ impl Default for wasmtime_component_val_t {
}
}
-impl From<&wasmtime_component_val_t> for Val {
- fn from(value: &wasmtime_component_val_t) -> Self {
- match value {
+fn name_to_string(name: &wasm_name_t) -> Result<String> {
+ Ok(String::from_utf8(name.clone().take())?)
+}
+
+fn option_box_try_from(val: &Option<Box<wasmtime_component_val_t>>) -> Result<Option<Box<Val>>> {
+ val.as_ref()
+ .map(|x| Val::try_from(x.as_ref()).map(Box::new))
+ .transpose()
+}
+
+impl TryFrom<&wasmtime_component_val_t> for Val {
+ type Error = Error;
+ fn try_from(value: &wasmtime_component_val_t) -> Result<Self> {
+ Ok(match value {
wasmtime_component_val_t::Bool(x) => Val::Bool(*x),
wasmtime_component_val_t::S8(x) => Val::S8(*x),
wasmtime_component_val_t::U8(x) => Val::U8(*x),
@@ -301,29 +316,25 @@ impl From<&wasmtime_component_val_t> for Val {
wasmtime_component_val_t::U64(x) => Val::U64(*x),
wasmtime_component_val_t::F32(x) => Val::Float32(*x),
wasmtime_component_val_t::F64(x) => Val::Float64(*x),
- wasmtime_component_val_t::Char(x) => Val::Char(char::from_u32(*x).unwrap()),
- wasmtime_component_val_t::String(x) => {
- Val::String(String::from_utf8(x.clone().take()).unwrap())
- }
- wasmtime_component_val_t::List(x) => Val::List(x.into()),
- wasmtime_component_val_t::Record(x) => Val::Record(x.into()),
- wasmtime_component_val_t::Tuple(x) => Val::Tuple(x.into()),
+ wasmtime_component_val_t::Char(x) => Val::Char(
+ char::from_u32(*x).ok_or_else(|| format_err!("invalid char value: {x:#x}"))?,
+ ),
+ wasmtime_component_val_t::String(x) => Val::String(name_to_string(x)?),
+ wasmtime_component_val_t::List(x) => Val::List(x.try_into()?),
+ wasmtime_component_val_t::Record(x) => Val::Record(x.try_into()?),
+ wasmtime_component_val_t::Tuple(x) => Val::Tuple(x.try_into()?),
wasmtime_component_val_t::Variant(x) => {
- let (a, b) = x.into();
+ let (a, b) = x.try_into()?;
Val::Variant(a, b)
}
- wasmtime_component_val_t::Enum(x) => {
- Val::Enum(String::from_utf8(x.clone().take()).unwrap())
- }
- wasmtime_component_val_t::Option(x) => {
- Val::Option(x.as_ref().map(|x| Box::new(Val::from(x.as_ref()))))
- }
- wasmtime_component_val_t::Result(x) => Val::Result(x.into()),
- wasmtime_component_val_t::Flags(x) => Val::Flags(x.into()),
- wasmtime_component_val_t::Map(x) => Val::Map(x.into()),
+ wasmtime_component_val_t::Enum(x) => Val::Enum(name_to_string(x)?),
+ wasmtime_component_val_t::Option(x) => Val::Option(option_box_try_from(x)?),
+ wasmtime_component_val_t::Result(x) => Val::Result(x.try_into()?),
+ wasmtime_component_val_t::Flags(x) => Val::Flags(x.try_into()?),
+ wasmtime_component_val_t::Map(x) => Val::Map(x.try_into()?),
wasmtime_component_val_t::Resource(x) => Val::Resource(x.resource),
- wasmtime_component_val_t::FixedLengthList(x) => Val::FixedLengthList(x.into()),
- }
+ wasmtime_component_val_t::FixedLengthList(x) => Val::FixedLengthList(x.try_into()?),
+ })
}
}
diff --git a/crates/c-api/src/engine.rs b/crates/c-api/src/engine.rs
index 986a5b5793..1deb8fdb15 100644
--- a/crates/c-api/src/engine.rs
+++ b/crates/c-api/src/engine.rs
@@ -28,14 +28,14 @@ pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
}
#[unsafe(no_mangle)]
-pub extern "C" fn wasm_engine_new_with_config(c: Box<wasm_config_t>) -> Box<wasm_engine_t> {
+pub extern "C" fn wasm_engine_new_with_config(c: Box<wasm_config_t>) -> Option<Box<wasm_engine_t>> {
#[cfg(feature = "logging")]
drop(env_logger::try_init());
let config = c.config;
- Box::new(wasm_engine_t {
- engine: Engine::new(&config).unwrap(),
- })
+ Some(Box::new(wasm_engine_t {
+ engine: Engine::new(&config).ok()?,
+ }))
}
#[unsafe(no_mangle)]
diff --git a/crates/c-api/src/types/table.rs b/crates/c-api/src/types/table.rs
index 55b057a94f..f634b08478 100644
--- a/crates/c-api/src/types/table.rs
+++ b/crates/c-api/src/types/table.rs
@@ -75,6 +75,27 @@ pub extern "C" fn wasm_tabletype_limits(tt: &wasm_tabletype_t) -> &wasm_limits_t
})
}
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_tabletype_minimum(tt: &wasm_tabletype_t) -> u64 {
+ tt.ty().ty.minimum()
+}
+
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_tabletype_maximum(tt: &wasm_tabletype_t, out: &mut u64) -> bool {
+ match tt.ty().ty.maximum() {
+ Some(max) => {
+ *out = max;
+ true
+ }
+ None => false,
+ }
+}
+
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_tabletype_is64(tt: &wasm_tabletype_t) -> bool {
+ tt.ty().ty.is_64()
+}
+
#[unsafe(no_mangle)]
pub extern "C" fn wasm_tabletype_as_externtype(ty: &wasm_tabletype_t) -> &wasm_externtype_t {
&ty.ext
diff --git a/crates/c-api/tests/async.cc b/crates/c-api/tests/async.cc
index 7785ed1664..ea53f391c5 100644
--- a/crates/c-api/tests/async.cc
+++ b/crates/c-api/tests/async.cc
@@ -14,6 +14,13 @@ void async_callback(void *, wasmtime_caller_t *, const wasmtime_val_t *, size_t,
void finalize(void *data) { *static_cast<bool *>(data) = true; }
+void trapping_async_callback(void *, wasmtime_caller_t *,
+ const wasmtime_val_t *, size_t, wasmtime_val_t *,
+ size_t, wasm_trap_t **trap_ret,
+ wasmtime_async_continuation_t *) {
+ *trap_ret = wasmtime_trap_new("host trap", 9);
+}
+
} // namespace
TEST(async, finalizes_callback_when_name_parsing_fails) {
@@ -92,4 +99,29 @@ TEST(async, instantiate_async) {
EXPECT_EQ(error, nullptr);
}
+TEST(async, callback_traps_without_continuation) {
+ Engine engine;
+ Store store(engine);
+ Linker linker(engine);
+ auto *ty = wasm_functype_new_0_0();
+ auto *error = wasmtime_linker_define_async_func(linker.capi(), "", 0, "f", 1,
+ ty, trapping_async_callback,
+ nullptr, nullptr);
+ ASSERT_EQ(error, nullptr);
+ wasm_functype_delete(ty);
+
+ auto f = std::get<Func>(*linker.get(store, "", "f"));
+
+ wasm_trap_t *trap = nullptr;
+ auto *future = wasmtime_func_call_async(
+ store.context().capi(), &f.capi(), nullptr, 0, nullptr, 0, &trap, &error);
+ while (!wasmtime_call_future_poll(future)) {
+ }
+ wasmtime_call_future_delete(future);
+
+ EXPECT_EQ(trap, nullptr);
+ ASSERT_NE(error, nullptr);
+ EXPECT_EQ(Error(error).message(), "host trap");
+}
+
#endif // WASMTIME_FEATURE_ASYNC
diff --git a/crates/c-api/tests/component/values.cc b/crates/c-api/tests/component/values.cc
index c257f906e3..c7ae5459f8 100644
--- a/crates/c-api/tests/component/values.cc
+++ b/crates/c-api/tests/component/values.cc
@@ -1170,3 +1170,62 @@ TEST(component, resources) {
EXPECT_TRUE(r5.owned());
EXPECT_EQ(r5.type(), ResourceType(2));
}
+
+TEST(component, value_invalid_char_and_string) {
+ // A surrogate isn't a valid `char`, as an argument or a host result.
+ auto ctx = create(
+ R"(char)", R"(
+(param $x i32)
+(result i32)
+local.get $x
+call $do
+ )",
+ "(param i32) (result i32)",
+ +[](Store::Context, const FuncType &, Span<const Val>,
+ Span<Val> rets) -> Result<std::monostate> {
+ rets[0] = Val::char_(0xd800);
+ return std::monostate();
+ });
+
+ auto arg = Val::char_(0xd800);
+ auto res = Val(false);
+ EXPECT_FALSE(
+ ctx.func.call(ctx.context, Span<const Val>(&arg, 1), Span<Val>(&res, 1)));
+
+ arg = Val::char_('a');
+ EXPECT_FALSE(
+ ctx.func.call(ctx.context, Span<const Val>(&arg, 1), Span<Val>(&res, 1)));
+
+ // Strings must be valid UTF-8, as an argument or a host result.
+ ctx = create(
+ R"(string)", R"(
+(param $x i32)
+(param $y i32)
+(result i32)
+(local $res i32)
+local.get $x
+local.get $y
+(call $realloc
+ (i32.const 0)
+ (i32.const 0)
+ (i32.const 4)
+ (i32.const 8))
+local.tee $res
+call $do
+local.get $res
+ )",
+ "(param i32 i32 i32)",
+ +[](Store::Context, const FuncType &, Span<const Val>,
+ Span<Val> rets) -> Result<std::monostate> {
+ rets[0] = Val::string("\xff\xfe");
+ return std::monostate();
+ });
+
+ arg = Val::string("\xff\xfe");
+ EXPECT_FALSE(
+ ctx.func.call(ctx.context, Span<const Val>(&arg, 1), Span<Val>(&res, 1)));
+
+ arg = Val::string("ok");
+ EXPECT_FALSE(
+ ctx.func.call(ctx.context, Span<const Val>(&arg, 1), Span<Val>(&res, 1)));
+}
diff --git a/crates/c-api/tests/engine.cc b/crates/c-api/tests/engine.cc
index e43b2129d1..40f6e0b834 100644
--- a/crates/c-api/tests/engine.cc
+++ b/crates/c-api/tests/engine.cc
@@ -13,3 +13,9 @@ TEST(Engine, Simple) {
engine2 = Engine();
engine.is_pulley();
}
+
+TEST(Engine, InvalidConfig) {
+ wasm_config_t *config = wasm_config_new();
+ wasmtime_config_cranelift_flag_set(config, "no_such_flag", "true");
+ EXPECT_EQ(wasm_engine_new_with_config(config), nullptr);
+}
diff --git a/crates/c-api/tests/table_type.cc b/crates/c-api/tests/table_type.cc
index 3f3cb0bd59..83c8b2c0d5 100644
--- a/crates/c-api/tests/table_type.cc
+++ b/crates/c-api/tests/table_type.cc
@@ -1,3 +1,4 @@
+#include <wasmtime.hh>
#include <wasmtime/types/table.hh>
#include <gtest/gtest.h>
@@ -8,6 +9,7 @@ TEST(TableType, Simple) {
TableType ty(ValType::funcref(), 1);
EXPECT_EQ(ty->min(), 1);
EXPECT_EQ(ty->max(), std::nullopt);
+ EXPECT_FALSE(ty->is_64());
EXPECT_EQ(ty->element(), ValType::funcref());
ty = TableType(ValType::externref(), 2, 3);
@@ -15,3 +17,20 @@ TEST(TableType, Simple) {
EXPECT_EQ(ty->max(), 3);
EXPECT_EQ(ty->element(), ValType::externref());
}
+
+TEST(TableType, Table64) {
+ Engine engine;
+ Module module =
+ Module::compile(
+ engine,
+ "(module"
+ "(import \"\" \"t\" (table i64 0x100000000 0x200000000 funcref))"
+ ")")
+ .unwrap();
+ auto imports = module.imports();
+ auto import = *imports.begin();
+ auto ty = std::get<TableType::Ref>(ExternType::from_import(import));
+ EXPECT_EQ(ty.min(), 0x100000000);
+ EXPECT_EQ(ty.max(), 0x200000000);
+ EXPECT_TRUE(ty.is_64());
+}