Skip to content

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

Handle errors when creating C API struct types (#15038)

Alex Crichtonf032c65d5aee
diff --git a/crates/c-api/include/wasmtime/types/_structref_class.hh b/crates/c-api/include/wasmtime/types/_structref_class.hh
index 40301c4f40..e56e9cb1da 100644
--- a/crates/c-api/include/wasmtime/types/_structref_class.hh
+++ b/crates/c-api/include/wasmtime/types/_structref_class.hh
@@ -8,6 +8,7 @@
#include <memory>
#include <vector>
#include <wasmtime/engine.hh>
+#include <wasmtime/error.hh>
#include <wasmtime/types/_val_class.hh>
#include <wasmtime/types/structref.h>
@@ -147,14 +148,17 @@ class StructType {
#undef wasmtime_struct_type_clone
/// Create a new struct type with the given fields.
- StructType(const Engine &engine, const std::vector<FieldType> &fields)
- : ptr(wasmtime_struct_type_new(
- engine.capi(),
- reinterpret_cast<const wasmtime_field_type_t *>(fields.data()),
- fields.size()))
-
- {
+ static Result<StructType> create(const Engine &engine,
+ const std::vector<FieldType> &fields) {
static_assert(sizeof(FieldType) == sizeof(wasmtime_field_type_t));
+ wasmtime_struct_type_t *result = nullptr;
+ auto error = wasmtime_struct_type_new(
+ engine.capi(),
+ reinterpret_cast<const wasmtime_field_type_t *>(fields.data()),
+ fields.size(), &result);
+ if (error)
+ return Error(error);
+ return StructType(result);
}
size_t num_fields() const {
diff --git a/crates/c-api/include/wasmtime/types/structref.h b/crates/c-api/include/wasmtime/types/structref.h
index 5d56592579..4aa1001210 100644
--- a/crates/c-api/include/wasmtime/types/structref.h
+++ b/crates/c-api/include/wasmtime/types/structref.h
@@ -8,6 +8,7 @@
#define WASMTIME_TYPES_STRUCTREF_H
#include <wasm.h>
+#include <wasmtime/error.h>
#ifdef __cplusplus
extern "C" {
@@ -85,13 +86,18 @@ typedef struct wasmtime_struct_type wasmtime_struct_type_t;
* \param engine The engine to register the type with.
* \param fields Pointer to an array of field type descriptors.
* \param nfields Number of fields.
+ * \param out On success, filled in with the new struct type.
*
- * \return Returns a new struct type, or NULL on error (e.g. invalid field
- * types).
+ * Fills in `out` on success and returns `NULL`. Otherwise returns an error
+ * (e.g. there are too many fields) and does not modify `out`.
+ *
+ * This function aborts the process if any field's type comes from an engine
+ * other than `engine`.
*/
-WASM_API_EXTERN wasmtime_struct_type_t *
+WASM_API_EXTERN wasmtime_error_t *
wasmtime_struct_type_new(const wasm_engine_t *engine,
- const wasmtime_field_type_t *fields, size_t nfields);
+ const wasmtime_field_type_t *fields, size_t nfields,
+ wasmtime_struct_type_t **out);
/**
* \brief Clone a struct type.
diff --git a/crates/c-api/src/types/structref.rs b/crates/c-api/src/types/structref.rs
index d33e94bca8..0ce370daa2 100644
--- a/crates/c-api/src/types/structref.rs
+++ b/crates/c-api/src/types/structref.rs
@@ -1,4 +1,4 @@
-use crate::wasm_valtype_t;
+use crate::{wasm_valtype_t, wasmtime_error_t};
use std::mem::{ManuallyDrop, MaybeUninit};
use wasmtime::{FieldType, Mutability, StorageType, StructType};
@@ -106,11 +106,13 @@ pub extern "C" fn wasmtime_struct_type_new(
engine: &crate::wasm_engine_t,
fields: *const wasmtime_field_type_t,
nfields: usize,
-) -> Box<wasmtime_struct_type_t> {
+ out: &mut MaybeUninit<Box<wasmtime_struct_type_t>>,
+) -> Option<Box<wasmtime_error_t>> {
let fields = unsafe { crate::slice_from_raw_parts(fields, nfields) };
let field_types: Vec<FieldType> = fields.iter().map(|f| f.to_wasmtime()).collect();
- let ty = StructType::new(&engine.engine, field_types).expect("failed to create struct type");
- Box::new(wasmtime_struct_type_t { ty })
+ crate::handle_result(StructType::new(&engine.engine, field_types), |ty| {
+ out.write(Box::new(wasmtime_struct_type_t { ty }));
+ })
}
#[unsafe(no_mangle)]
diff --git a/crates/c-api/tests/gc.cc b/crates/c-api/tests/gc.cc
index af28bd9395..9188706494 100644
--- a/crates/c-api/tests/gc.cc
+++ b/crates/c-api/tests/gc.cc
@@ -101,10 +101,12 @@ TEST(StructRef, CreateAndReadFields) {
auto cx = store.context();
// Create a struct type with two mutable i32 fields.
- auto ty = StructType(engine, {
+ auto ty = StructType::create(engine,
+ {
FieldType::mut_(ValType::i32()),
FieldType::mut_(ValType::i32()),
- });
+ })
+ .unwrap();
auto pre = StructRefPre::create(cx, ty);
// Allocate a struct with field values 10 and 20.
@@ -132,6 +134,15 @@ TEST(StructRef, CreateAndReadFields) {
EXPECT_EQ(v0b.ok().i32(), 42);
}
+TEST(StructType, TooManyFields) {
+ Config config;
+ config.wasm_gc(true);
+ Engine engine(std::move(config));
+
+ std::vector<FieldType> fields(10001, FieldType::const_(ValType::i32()));
+ EXPECT_FALSE(StructType::create(engine, fields));
+}
+
TEST(StructRef, UpcastAndDowncast) {
Config config;
config.wasm_gc(true);
@@ -139,9 +150,11 @@ TEST(StructRef, UpcastAndDowncast) {
Store store(engine);
auto cx = store.context();
- auto ty = StructType(engine, {
+ auto ty = StructType::create(engine,
+ {
FieldType::const_(ValType::i32()),
- });
+ })
+ .unwrap();
auto pre = StructRefPre::create(cx, ty);
auto result = StructRef::create(cx, pre, {Val(int32_t(99))});
@@ -309,7 +322,8 @@ TEST(AnyRef, DowncastStruct) {
Store store(engine);
auto cx = store.context();
- auto ty = StructType(engine, {FieldType::const_(ValType::i32())});
+ auto ty =
+ StructType::create(engine, {FieldType::const_(ValType::i32())}).unwrap();
auto pre = StructRefPre::create(cx, ty);
auto result = StructRef::create(cx, pre, {Val(int32_t(77))});
ASSERT_TRUE(result);
diff --git a/crates/c-api/tests/val_type.cc b/crates/c-api/tests/val_type.cc
index 8af5e93fb2..2ce4bc35fa 100644
--- a/crates/c-api/tests/val_type.cc
+++ b/crates/c-api/tests/val_type.cc
@@ -104,7 +104,7 @@ TEST(ExnType, Smoke) {
TEST(StructType, Smoke) {
Engine engine;
- StructType sty(engine, {});
+ StructType sty = StructType::create(engine, {}).unwrap();
HeapType hty(sty);
auto struct_ty = hty.as_concrete_struct();
ASSERT_NE(struct_ty, nullptr);
@@ -116,7 +116,8 @@ TEST(StructType, Smoke) {
rty = RefType(false, sty);
ValType vty(engine, rty);
- sty = StructType(engine, {FieldType::mut_(StorageType::i8())});
+ sty =
+ StructType::create(engine, {FieldType::mut_(StorageType::i8())}).unwrap();
EXPECT_EQ(sty.field(0)->is_mutable(), true);
EXPECT_TRUE(sty.field(0)->storage_type().is_i8());
EXPECT_FALSE(sty.field(0)->storage_type().as_valtype());