A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Plumb table/memory type fallibility to C++ (#15172)
Alex Crichton687670f7323e
diff --git a/crates/c-api/include/wasmtime/types/memory.hh b/crates/c-api/include/wasmtime/types/memory.hhindex 4de2e4896b..36c156305b 100644--- a/crates/c-api/include/wasmtime/types/memory.hh+++ b/crates/c-api/include/wasmtime/types/memory.hh@@ -80,44 +80,18 @@ private:public:/// Creates a new 32-bit wasm memory type with the specified minimum number of- /// pages for the minimum size. The created type will have no maximum memory- /// size.- explicit MemoryType(uint32_t min) {- wasm_memorytype_t *p = nullptr;- auto *err = wasmtime_memorytype_new(min, false, 0, false, false, 16, &p);- assert(err == nullptr);- assert(p != nullptr);- ptr.reset(p);- ref.ptr = p;+ /// pages and optional maximum number of pages.+ ///+ /// Returns an error if the limits are invalid.+ static Result<MemoryType> create(uint32_t min,+ std::optional<uint32_t> max = std::nullopt) {+ return Builder().min(min).max(max).build();}- /// Creates a new 32-bit wasm memory type with the specified minimum number of- /// pages for the minimum size, and maximum number of pages for the max size.- MemoryType(uint32_t min, uint32_t max) {- wasm_memorytype_t *p = nullptr;- auto *err = wasmtime_memorytype_new(min, true, max, false, false, 16, &p);- assert(err == nullptr);- assert(p != nullptr);- ptr.reset(p);- ref.ptr = p;- }-- /// Same as the `MemoryType` constructor, except creates a 64-bit memory.- static MemoryType New64(uint64_t min) {- wasm_memorytype_t *ptr = nullptr;- auto *err = wasmtime_memorytype_new(min, false, 0, true, false, 16, &ptr);- assert(err == nullptr);- assert(ptr != nullptr);- return MemoryType(ptr);- }-- /// Same as the `MemoryType` constructor, except creates a 64-bit memory.- static MemoryType New64(uint64_t min, uint64_t max) {- wasm_memorytype_t *ptr = nullptr;- auto *err = wasmtime_memorytype_new(min, true, max, true, false, 16, &ptr);- assert(err == nullptr);- assert(ptr != nullptr);- return MemoryType(ptr);+ /// Same as `MemoryType::create`, except creates a 64-bit memory.+ static Result<MemoryType>+ create64(uint64_t min, std::optional<uint64_t> max = std::nullopt) {+ return Builder().min(min).max(max).memory64(true).build();}/// Creates a new wasm memory type from the specified ref, making a freshdiff --git a/crates/c-api/include/wasmtime/types/table.hh b/crates/c-api/include/wasmtime/types/table.hhindex 8a8561e0c8..f1257f73b3 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/error.hh>#include <wasmtime/table.h>#include <wasmtime/types/val.hh>@@ -61,24 +62,21 @@ private:TableType(wasm_tabletype_t *ptr) : ptr(ptr), ref(ptr) {}public:- /// Creates a new table type from the specified value type and minimum size.- /// The returned table will have no maximum size.- TableType(ValType ty, uint32_t min) : ptr(nullptr), ref(nullptr) {+ /// Creates a new table type from the specified element type, minimum size,+ /// and optional maximum size.+ ///+ /// Returns an error if `ty` isn't a reference type or if `min` is greater+ /// than `max`.+ static Result<TableType> create(ValType ty, uint32_t min,+ std::optional<uint32_t> max = std::nullopt) {wasm_limits_t limits;limits.min = min;- limits.max = wasm_limits_max_default;- ptr.reset(wasm_tabletype_new(ty.ptr.release(), &limits));- ref = ptr.get();- }- /// Creates a new table type from the specified value type, minimum size, and- /// maximum size.- TableType(ValType ty, uint32_t min, uint32_t max) // NOLINT- : ptr(nullptr), ref(nullptr) {- wasm_limits_t limits;- limits.min = min;- limits.max = max;- ptr.reset(wasm_tabletype_new(ty.ptr.release(), &limits));- ref = ptr.get();+ limits.max = max.value_or(wasm_limits_max_default);+ wasm_tabletype_t *ptr = wasm_tabletype_new(ty.ptr.release(), &limits);+ if (ptr == nullptr) {+ return Error("invalid table type");+ }+ return TableType(ptr);}/// Clones the given reference into a new table type.TableType(Ref other) : TableType(wasm_tabletype_copy(other.ptr)) {}diff --git a/crates/c-api/tests/instance.cc b/crates/c-api/tests/instance.ccindex 72ff552d21..689096b178 100644--- a/crates/c-api/tests/instance.cc+++ b/crates/c-api/tests/instance.cc@@ -7,12 +7,13 @@ using namespace wasmtime;TEST(Instance, Smoke) {Engine engine;Store store(engine);- Memory m = Memory::create(store, MemoryType(1)).unwrap();+ Memory m = Memory::create(store, MemoryType::create(1).unwrap()).unwrap();Global g =Global::create(store, GlobalType(ValType::i32(), false), 1).unwrap();- Table t = Table::create(store, TableType(ValType::funcref(), 1),- std::optional<Func>())- .unwrap();+ Table t =+ Table::create(store, TableType::create(ValType::funcref(), 1).unwrap(),+ std::optional<Func>())+ .unwrap();Func f(store, FuncType({}, {}),[](auto caller, auto params, auto results) -> auto {return std::monostate();diff --git a/crates/c-api/tests/memory.cc b/crates/c-api/tests/memory.ccindex c0e0b59b6b..b747378225 100644--- a/crates/c-api/tests/memory.cc+++ b/crates/c-api/tests/memory.cc@@ -8,7 +8,7 @@ using namespace wasmtime;TEST(Memory, Smoke) {Engine engine;Store store(engine);- Memory m = Memory::create(store, MemoryType(1)).unwrap();+ Memory m = Memory::create(store, MemoryType::create(1).unwrap()).unwrap();EXPECT_EQ(m.size(store), 1);EXPECT_EQ(m.grow(store, 1).unwrap(), 1);EXPECT_EQ(m.data(store).size(), 2 << 16);diff --git a/crates/c-api/tests/memory_type.cc b/crates/c-api/tests/memory_type.ccindex 5d030c88e2..2c6a007da0 100644--- a/crates/c-api/tests/memory_type.cc+++ b/crates/c-api/tests/memory_type.cc@@ -4,7 +4,7 @@using namespace wasmtime;TEST(MemoryType, Simple) {- MemoryType ty(1);+ MemoryType ty = MemoryType::create(1).unwrap();EXPECT_EQ(ty->min(), 1);EXPECT_EQ(ty->max(), std::nullopt);EXPECT_FALSE(ty->is_64());@@ -14,7 +14,7 @@ TEST(MemoryType, Simple) {}TEST(MemoryType, WithMax) {- MemoryType ty(1, 2);+ MemoryType ty = MemoryType::create(1, 2).unwrap();EXPECT_EQ(ty->min(), 1);EXPECT_EQ(ty->max(), 2);EXPECT_FALSE(ty->is_64());@@ -22,13 +22,18 @@ TEST(MemoryType, WithMax) {EXPECT_EQ(ty->page_size_log2(), 16);EXPECT_EQ(ty->page_size(), 1 << 16);- MemoryType other(3);+ MemoryType other = MemoryType::create(3).unwrap();other = ty;EXPECT_EQ(other->min(), 1);}+TEST(MemoryType, Invalid) {+ EXPECT_FALSE(MemoryType::create(2, 1));+ EXPECT_FALSE(MemoryType::create64(2, 1));+}+TEST(MemoryType, Mem64) {- MemoryType ty = MemoryType::New64(1);+ MemoryType ty = MemoryType::create64(1).unwrap();EXPECT_EQ(ty->min(), 1);EXPECT_EQ(ty->max(), std::nullopt);EXPECT_TRUE(ty->is_64());@@ -36,7 +41,7 @@ TEST(MemoryType, Mem64) {EXPECT_EQ(ty->page_size_log2(), 16);EXPECT_EQ(ty->page_size(), 1 << 16);- ty = MemoryType::New64(1, 2);+ ty = MemoryType::create64(1, 2).unwrap();EXPECT_EQ(ty->min(), 1);EXPECT_EQ(ty->max(), 2);EXPECT_TRUE(ty->is_64());diff --git a/crates/c-api/tests/table.cc b/crates/c-api/tests/table.ccindex d84ecb218b..2839a4b805 100644--- a/crates/c-api/tests/table.cc+++ b/crates/c-api/tests/table.cc@@ -8,11 +8,13 @@ using namespace wasmtime;TEST(Table, Smoke) {Engine engine;Store store(engine);- Table::create(store, TableType(ValType::funcref(), 1), 3.0).err();+ Table::create(store, TableType::create(ValType::funcref(), 1).unwrap(), 3.0)+ .err();Val null = std::optional<Func>();- Table t =- Table::create(store, TableType(ValType::funcref(), 1), null).unwrap();+ Table t = Table::create(+ store, TableType::create(ValType::funcref(), 1).unwrap(), null)+ .unwrap();EXPECT_FALSE(t.get(store, 1));EXPECT_TRUE(t.get(store, 0));Val val = *t.get(store, 0);diff --git a/crates/c-api/tests/table_type.cc b/crates/c-api/tests/table_type.ccindex 693af69b88..9500687490 100644--- a/crates/c-api/tests/table_type.cc+++ b/crates/c-api/tests/table_type.cc@@ -6,22 +6,27 @@using namespace wasmtime;TEST(TableType, Simple) {- TableType ty(ValType::funcref(), 1);+ TableType ty = TableType::create(ValType::funcref(), 1).unwrap();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);+ ty = TableType::create(ValType::externref(), 2, 3).unwrap();EXPECT_EQ(ty->min(), 2);EXPECT_EQ(ty->max(), 3);EXPECT_EQ(ty->element(), ValType::externref());- TableType other(ValType::funcref(), 1);+ TableType other = TableType::create(ValType::funcref(), 1).unwrap();other = ty;EXPECT_EQ(other->min(), 2);}+TEST(TableType, Invalid) {+ EXPECT_FALSE(TableType::create(ValType::funcref(), 2, 1));+ EXPECT_FALSE(TableType::create(ValType::i32(), 1));+}+TEST(TableType, Table64) {Engine engine;Module module =diff --git a/crates/c-api/tests/types.cc b/crates/c-api/tests/types.ccindex 159ab8db08..07279911f3 100644--- a/crates/c-api/tests/types.cc+++ b/crates/c-api/tests/types.cc@@ -33,7 +33,7 @@ TEST(ValType, Smoke) {}TEST(MemoryType, Smoke) {- MemoryType t(1);+ MemoryType t = MemoryType::create(1).unwrap();EXPECT_EQ(t->min(), 1);EXPECT_EQ(t->max(), std::nullopt);@@ -42,7 +42,7 @@ TEST(MemoryType, Smoke) {}TEST(TableType, Smoke) {- TableType t(ValType::funcref(), 1);+ TableType t = TableType::create(ValType::funcref(), 1).unwrap();EXPECT_EQ(t->min(), 1);EXPECT_EQ(t->max(), std::nullopt);@@ -107,14 +107,14 @@ TEST(ModuleType, Smoke) {}TEST(MemoryType, SixtyFour) {- MemoryType t(1);+ MemoryType t = MemoryType::create(1).unwrap();EXPECT_FALSE(t->is_64());- t = MemoryType::New64(1);+ t = MemoryType::create64(1).unwrap();EXPECT_TRUE(t->is_64());EXPECT_EQ(t->min(), 1);EXPECT_EQ(t->max(), std::nullopt);- t = MemoryType::New64(0x100000000, 0x100000001);+ t = MemoryType::create64(0x100000000, 0x100000001).unwrap();EXPECT_TRUE(t->is_64());EXPECT_EQ(t->min(), 0x100000000);EXPECT_EQ(t->max(), 0x100000001);diff --git a/examples/memory.cc b/examples/memory.ccindex d2802a74a6..8ac40bfca0 100644--- a/examples/memory.cc+++ b/examples/memory.cc@@ -71,7 +71,7 @@ int main() {memory.grow(store, 0).ok();std::cout << "Creating stand-alone memory...\n";- MemoryType ty(5, 5);+ MemoryType ty = MemoryType::create(5, 5).unwrap();Memory memory2 = Memory::create(store, ty).unwrap();assert(memory2.size(store) == 5);memory2.grow(store, 1).err();