Skip to content

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

Expose custom page sizes in the C and C++ APIs (#11890)

Nick Fitzgerald7948e0ff623e
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 1d7132b1dd..9a20ac8756 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -76,6 +76,10 @@ jobs:
grep -v wasmtime-platform.h | \
grep -v wasm.h | \
xargs clang-format-18 --dry-run --Werror --verbose
+ # Make sure our `clangformat.sh` helper script keeps running and stays
+ # in-sync with the previous check.
+ - run: crates/c-api/clangformat.sh
+ - run: git diff --exit-code
# Lint dependency graph for security advisories, duplicate versions, and
# incompatible licences
diff --git a/crates/c-api/README.md b/crates/c-api/README.md
index 7c7854489b..c6afc53bbf 100644
--- a/crates/c-api/README.md
+++ b/crates/c-api/README.md
@@ -22,7 +22,7 @@ toolchain](https://www.rust-lang.org/tools/install) installed.
From the root of the Wasmtime repository, run the following commands:
-```
+```shell-session
$ cmake -S crates/c-api -B target/c-api --install-prefix "$(pwd)/artifacts"
$ cmake --build target/c-api
$ cmake --install target/c-api
@@ -77,8 +77,34 @@ fn main() {
Running tests for the C API can be done using cmake from the root of the repo:
-```sh
-$ cmake -S examples -B examples/build -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=ON
+```shell-session
+$ cmake -S examples -B examples/build -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug
$ cmake --build examples/build --config Debug
$ CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --config Debug --target test
```
+
+To build and run a subset of tests by name:
+
+```shell-session
+$ cmake --build examples/build --config Debug
+$ ctest --test-dir examples/build --output-on-failure -R $MY_TEST
+```
+
+where `$MY_TEST` is the test name or glob pattern; for example,
+`MemoryType.Simple` to run that one particular test or `TypedFunc.*` to run all
+typed function tests.
+
+To run under `gdb`:
+
+```shell-session
+$ gdb --args ctest --test-dir examples/build/ --output-on-failure -R $MY_TEST
+(gdb) set follow-fork-mode child
+(gdb) run
+```
+
+To run under `rr`:
+
+```
+$ rr record ctest --test-dir examples/build/ --output-on-failure -R $MY_TEST
+$ rr replay
+```
diff --git a/crates/c-api/clangformat.sh b/crates/c-api/clangformat.sh
new file mode 100755
index 0000000000..69ce57d504
--- /dev/null
+++ b/crates/c-api/clangformat.sh
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+
+set -eu
+
+cd $(dirname $0)/../..
+
+git ls-files '*.h' '*.c' '*.cpp' '*.hh' '*.cc' | \
+ grep -v wasmtime-platform.h | \
+ grep -v wasm.h | \
+ xargs clang-format-18 -i
diff --git a/crates/c-api/include/wasmtime/config.h b/crates/c-api/include/wasmtime/config.h
index 67414f79db..172366f6a1 100644
--- a/crates/c-api/include/wasmtime/config.h
+++ b/crates/c-api/include/wasmtime/config.h
@@ -255,6 +255,14 @@ WASMTIME_CONFIG_PROP(void, wasm_wide_arithmetic, bool)
*/
WASMTIME_CONFIG_PROP(void, wasm_exceptions, bool)
+/**
+ * \brief Configures whether the WebAssembly custom-page-sizes proposal is
+ * enabled.
+ *
+ * This setting is `false` by default.
+ */
+WASMTIME_CONFIG_PROP(void, wasm_custom_page_sizes, bool)
+
#ifdef WASMTIME_FEATURE_COMPILER
/**
diff --git a/crates/c-api/include/wasmtime/config.hh b/crates/c-api/include/wasmtime/config.hh
index 491e17eb4f..6f66dd6de1 100644
--- a/crates/c-api/include/wasmtime/config.hh
+++ b/crates/c-api/include/wasmtime/config.hh
@@ -398,6 +398,14 @@ public:
wasmtime_config_wasm_exceptions_set(ptr.get(), enable);
}
+ /// \brief Configures whether the WebAssembly custom-page-sizes proposal will
+ /// be enabled
+ ///
+ /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_custom_page_sizes
+ void wasm_custom_page_sizes(bool enable) {
+ wasmtime_config_wasm_custom_page_sizes_set(ptr.get(), enable);
+ }
+
#ifdef WASMTIME_FEATURE_COMPONENT_MODEL
/// \brief Configures whether the WebAssembly component model proposal will be
/// enabled
diff --git a/crates/c-api/include/wasmtime/memory.h b/crates/c-api/include/wasmtime/memory.h
index 50ce7fa7ba..2aac6b7289 100644
--- a/crates/c-api/include/wasmtime/memory.h
+++ b/crates/c-api/include/wasmtime/memory.h
@@ -22,9 +22,10 @@ extern "C" {
* Note that this function is preferred over #wasm_memorytype_new for
* compatibility with the memory64 proposal.
*/
-WASM_API_EXTERN wasm_memorytype_t *
+WASM_API_EXTERN wasmtime_error_t *
wasmtime_memorytype_new(uint64_t min, bool max_present, uint64_t max,
- bool is_64, bool shared);
+ bool is_64, bool shared, uint8_t page_size_log2,
+ wasm_memorytype_t **ret);
/**
* \brief Returns the minimum size, in pages, of the specified memory type.
@@ -58,6 +59,18 @@ WASM_API_EXTERN bool wasmtime_memorytype_is64(const wasm_memorytype_t *ty);
*/
WASM_API_EXTERN bool wasmtime_memorytype_isshared(const wasm_memorytype_t *ty);
+/**
+ * \brief Returns the page size, in bytes, of this memory type.
+ */
+WASM_API_EXTERN uint64_t
+wasmtime_memorytype_page_size(const wasm_memorytype_t *ty);
+
+/**
+ * \brief Returns the log2 of this memory type's page size, in bytes.
+ */
+WASM_API_EXTERN uint8_t
+wasmtime_memorytype_page_size_log2(const wasm_memorytype_t *ty);
+
/**
* \brief Creates a new WebAssembly linear memory
*
@@ -113,6 +126,47 @@ WASM_API_EXTERN wasmtime_error_t *
wasmtime_memory_grow(wasmtime_context_t *store, const wasmtime_memory_t *memory,
uint64_t delta, uint64_t *prev_size);
+/**
+ * \brief Returns the size of a page, in bytes, for this memory.
+ *
+ * \param store the store that owns `memory`
+ * \param memory the memory to get the page size of
+ *
+ * WebAssembly memories are made up of a whole number of pages, so the byte size
+ * (as returned by #wasmtime_memory_data_size) will always be a multiple of
+ * their page size. Different Wasm memories may have different page sizes.
+ *
+ * By default this is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or `65536`)
+ * but [the custom-page-sizes proposal] allows opting into a page size of
+ * `1`. Future extensions might allow any power of two as a page size.
+ *
+ * [the custom-page-sizes proposal]:
+ * https://github.com/WebAssembly/custom-page-sizes
+ */
+WASM_API_EXTERN uint64_t wasmtime_memory_page_size(
+ wasmtime_context_t *store, const wasmtime_memory_t *memory);
+
+/**
+ * \brief Returns the log2 of this memory's page size, in bytes.
+ *
+ * \param store the store that owns `memory`
+ * \param memory the memory to get the page size of
+ *
+ * WebAssembly memories are made up of a whole number of pages, so the byte size
+ * (as returned by #wasmtime_memory_data_size) will always be a multiple of
+ * their page size. Different Wasm memories may have different page sizes.
+ *
+ * By default the page size is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or
+ * `65536`) but [the custom-page-sizes proposal] allows opting into a page
+ * size of `1`. Future extensions might allow any power of two as a page
+ * size.
+ *
+ * [the custom-page-sizes proposal]:
+ * https://github.com/WebAssembly/custom-page-sizes
+ */
+WASM_API_EXTERN uint8_t wasmtime_memory_page_size_log2(
+ wasmtime_context_t *store, const wasmtime_memory_t *memory);
+
#ifdef __cplusplus
} // extern "C"
#endif
diff --git a/crates/c-api/include/wasmtime/memory.hh b/crates/c-api/include/wasmtime/memory.hh
index 3198d17828..364f23e0a8 100644
--- a/crates/c-api/include/wasmtime/memory.hh
+++ b/crates/c-api/include/wasmtime/memory.hh
@@ -76,6 +76,39 @@ public:
return prev;
}
+ /// Returns the size of a page, in bytes, for this memory.
+ ///
+ /// WebAssembly memories are made up of a whole number of pages, so the byte
+ /// size will always be a multiple of their page size. Different Wasm memories
+ /// may have different page sizes.
+ ///
+ /// By default this is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or `65536`)
+ /// but [the custom-page-sizes proposal] allows opting into a page size of
+ /// `1`. Future extensions might allow any power of two as a page size.
+ ///
+ /// [the custom-page-sizes proposal]:
+ /// https://github.com/WebAssembly/custom-page-sizes
+ uint64_t page_size(Store::Context cx) const {
+ return wasmtime_memory_page_size(cx.ptr, &memory);
+ }
+
+ /// Returns the log2 of this memory's page size, in bytes.
+ ///
+ /// WebAssembly memories are made up of a whole number of pages, so the byte
+ /// size will always be a multiple of their page size. Different Wasm memories
+ /// may have different page sizes.
+ ///
+ /// By default the page size is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or
+ /// `65536`) but [the custom-page-sizes proposal] allows opting into a page
+ /// size of `1`. Future extensions might allow any power of two as a page
+ /// size.
+ ///
+ /// [the custom-page-sizes proposal]:
+ /// https://github.com/WebAssembly/custom-page-sizes
+ uint8_t page_size_log2(Store::Context cx) const {
+ return wasmtime_memory_page_size_log2(cx.ptr, &memory);
+ }
+
/// Returns the raw underlying C API memory this is using.
const wasmtime_memory_t &capi() const { return memory; }
};
diff --git a/crates/c-api/include/wasmtime/types/memory.hh b/crates/c-api/include/wasmtime/types/memory.hh
index 52343dc832..3d08b9f685 100644
--- a/crates/c-api/include/wasmtime/types/memory.hh
+++ b/crates/c-api/include/wasmtime/types/memory.hh
@@ -8,6 +8,7 @@
#include <memory>
#include <optional>
#include <wasm.h>
+#include <wasmtime/error.hh>
#include <wasmtime/memory.h>
namespace wasmtime {
@@ -19,7 +20,10 @@ class MemoryType {
friend class Memory;
struct deleter {
- void operator()(wasm_memorytype_t *p) const { wasm_memorytype_delete(p); }
+ void operator()(wasm_memorytype_t *p) const {
+ assert(p != nullptr);
+ wasm_memorytype_delete(p);
+ }
};
std::unique_ptr<wasm_memorytype_t, deleter> ptr;
@@ -32,6 +36,9 @@ public:
const wasm_memorytype_t *ptr;
+ private:
+ Ref() : ptr(nullptr) {}
+
public:
/// Creates a reference from the raw C API representation.
Ref(const wasm_memorytype_t *ptr) : ptr(ptr) {}
@@ -57,6 +64,14 @@ public:
/// Returns whether or not this is a shared memory type.
bool is_shared() const { return wasmtime_memorytype_isshared(ptr); }
+
+ /// Returns the memory's page size, in bytes.
+ uint64_t page_size() const { return wasmtime_memorytype_page_size(ptr); }
+
+ /// Returns the log2 of the memory's page size, in bytes.
+ uint8_t page_size_log2() const {
+ return wasmtime_memorytype_page_size_log2(ptr);
+ }
};
private:
@@ -67,21 +82,42 @@ 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)
- : MemoryType(wasmtime_memorytype_new(min, false, 0, false, false)) {}
+ 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;
+ }
+
/// 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)
- : MemoryType(wasmtime_memorytype_new(min, true, max, false, false)) {}
+ 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) {
- return MemoryType(wasmtime_memorytype_new(min, false, 0, true, false));
+ 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) {
- return MemoryType(wasmtime_memorytype_new(min, true, max, true, false));
+ 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);
}
/// Creates a new wasm memory type from the specified ref, making a fresh
@@ -114,10 +150,12 @@ public:
std::optional<uint64_t> _max;
bool _memory64;
bool _shared;
+ uint8_t _page_size_log2;
public:
/// \brief Default constructor for a memory type with 0 initial size.
- Builder() : _min(0), _memory64(false), _shared(false) {}
+ Builder()
+ : _min(0), _memory64(false), _shared(false), _page_size_log2(16) {}
/// \brief Configure the minimum size, in pages, of linear memory.
Builder &min(uint64_t min) {
@@ -143,11 +181,33 @@ public:
return *this;
}
+ /// \brief Configure the page size (in bytes) of this memory type, via its
+ /// log2 value.
+ ///
+ /// The default Wasm page size is `65536` (or `2**16`).
+ ///
+ /// With [the custom-page-sizes proposal], you can configure `1`-byte (or
+ /// `2**0`) pages by passing `0` to this method. Future extensions may
+ /// enable any power-of-two page size.
+ ///
+ /// [the custom-page-sizes proposal]:
+ /// https://github.com/WebAssembly/custom-page-sizes/blob/main/proposals/custom-page-sizes/Overview.md
+ Builder &page_size_log2(uint8_t page_size_log2) {
+ _page_size_log2 = page_size_log2;
+ return *this;
+ }
+
/// \brief Construct the final `MemoryType` value.
- MemoryType build() const {
- return MemoryType(wasmtime_memorytype_new(_min, _max.has_value(),
- _max.has_value() ? *_max : 0,
- _memory64, _shared));
+ Result<MemoryType> build() const {
+ wasm_memorytype_t *p = nullptr;
+ auto *err = wasmtime_memorytype_new(
+ _min, _max.has_value(), _max.has_value() ? *_max : 0, _memory64,
+ _shared, _page_size_log2, &p);
+ if (err != nullptr) {
+ return Error(err);
+ }
+ assert(p != nullptr);
+ return MemoryType(p);
}
};
};
diff --git a/crates/c-api/src/config.rs b/crates/c-api/src/config.rs
index 887ddbc35f..1eb1c11038 100644
--- a/crates/c-api/src/config.rs
+++ b/crates/c-api/src/config.rs
@@ -470,6 +470,11 @@ pub extern "C" fn wasmtime_config_wasm_exceptions_set(c: &mut wasm_config_t, ena
c.config.wasm_exceptions(enable);
}
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_config_wasm_custom_page_sizes_set(c: &mut wasm_config_t, enable: bool) {
+ c.config.wasm_custom_page_sizes(enable);
+}
+
#[repr(C)]
#[derive(Clone)]
#[cfg(feature = "pooling-allocator")]
diff --git a/crates/c-api/src/memory.rs b/crates/c-api/src/memory.rs
index 72f9e2968d..4c6b2fc685 100644
--- a/crates/c-api/src/memory.rs
+++ b/crates/c-api/src/memory.rs
@@ -130,3 +130,16 @@ pub extern "C" fn wasmtime_memory_grow(
) -> Option<Box<wasmtime_error_t>> {
handle_result(mem.grow(store, delta), |prev| *prev_size = prev)
}
+
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_memory_page_size(store: WasmtimeStoreContext<'_>, mem: &Memory) -> u64 {
+ mem.page_size(store)
+}
+
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_memory_page_size_log2(
+ store: WasmtimeStoreContext<'_>,
+ mem: &Memory,
+) -> u8 {
+ mem.page_size_log2(store)
+}
diff --git a/crates/c-api/src/types/memory.rs b/crates/c-api/src/types/memory.rs
index 5521413ff8..d4d4c2edcb 100644
--- a/crates/c-api/src/types/memory.rs
+++ b/crates/c-api/src/types/memory.rs
@@ -1,6 +1,6 @@
-use crate::{CExternType, wasm_externtype_t, wasm_limits_t};
-use std::cell::OnceCell;
+use crate::{CExternType, handle_result, wasm_externtype_t, wasm_limits_t, wasmtime_error_t};
use std::convert::TryFrom;
+use std::{cell::OnceCell, mem::MaybeUninit};
use wasmtime::{MemoryType, MemoryTypeBuilder};
#[repr(transparent)]
@@ -72,22 +72,27 @@ pub extern "C" fn wasmtime_memorytype_new(
maximum: u64,
memory64: bool,
shared: bool,
-) -> Box<wasm_memorytype_t> {
+ page_size_log2: u8,
+ ret: &mut MaybeUninit<Box<wasm_memorytype_t>>,
+) -> Option<Box<wasmtime_error_t>> {
let maximum = if maximum_specified {
Some(maximum)
} else {
None
};
- Box::new(wasm_memorytype_t::new(
+ handle_result(
MemoryTypeBuilder::default()
.min(minimum)
.max(maximum)
.memory64(memory64)
.shared(shared)
- .build()
- .unwrap(),
- ))
+ .page_size_log2(page_size_log2)
+ .build(),
+ |ty| {
+ ret.write(Box::new(wasm_memorytype_t::new(ty)));
+ },
+ )
}
#[unsafe(no_mangle)]
@@ -116,6 +121,16 @@ pub extern "C" fn wasmtime_memorytype_isshared(mt: &wasm_memorytype_t) -> bool {
mt.ty().ty.is_shared()
}
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_memorytype_page_size(mt: &wasm_memorytype_t) -> u64 {
+ mt.ty().ty.page_size()
+}
+
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_memorytype_page_size_log2(mt: &wasm_memorytype_t) -> u8 {
+ mt.ty().ty.page_size_log2()
+}
+
#[unsafe(no_mangle)]
pub extern "C" fn wasm_memorytype_as_externtype(ty: &wasm_memorytype_t) -> &wasm_externtype_t {
&ty.ext
diff --git a/crates/c-api/tests/config.cc b/crates/c-api/tests/config.cc
index 5bd53489de..1d61d232a8 100644
--- a/crates/c-api/tests/config.cc
+++ b/crates/c-api/tests/config.cc
@@ -52,6 +52,7 @@ TEST(Config, Smoke) {
config.wasm_wide_arithmetic(false);
config.wasm_component_model(false);
config.wasm_exceptions(false);
+ config.wasm_custom_page_sizes(false);
config.strategy(Strategy::Auto);
config.cranelift_debug_verifier(false);
config.cranelift_opt_level(OptLevel::Speed);
diff --git a/crates/c-api/tests/memory.cc b/crates/c-api/tests/memory.cc
index 77885caf40..c0e0b59b6b 100644
--- a/crates/c-api/tests/memory.cc
+++ b/crates/c-api/tests/memory.cc
@@ -13,4 +13,33 @@ TEST(Memory, Smoke) {
EXPECT_EQ(m.grow(store, 1).unwrap(), 1);
EXPECT_EQ(m.data(store).size(), 2 << 16);
EXPECT_EQ(m.type(store)->min(), 1);
+ EXPECT_EQ(m.page_size_log2(store), 16);
+ EXPECT_EQ(m.page_size(store), 1 << 16);
+}
+
+TEST(Memory, OneBytePageSize) {
+ Engine engine;
+ Store store(engine);
+ MemoryType mem_ty =
+ MemoryType::Builder().min(1).max(2).page_size_log2(0).build().unwrap();
+ Memory mem = Memory::create(store, mem_ty).unwrap();
+
+ // Has expected page size and limits.
+ EXPECT_EQ(mem.page_size_log2(store), 0);
+ EXPECT_EQ(mem.page_size(store), 1);
+ EXPECT_EQ(mem.type(store)->min(), 1);
+ EXPECT_EQ(*mem.type(store)->max(), 2);
+
+ // Has expected initial size.
+ EXPECT_EQ(mem.size(store), 1);
+
+ // Can grow to max and has expected new size.
+ EXPECT_EQ(mem.grow(store, 1).unwrap(), 1);
+ EXPECT_EQ(mem.size(store), 2);
+ EXPECT_EQ(mem.data(store).size(), 2);
+
+ // Fails to grow beyond max, size remains unchanged.
+ EXPECT_FALSE(mem.grow(store, 1));
+ EXPECT_EQ(mem.size(store), 2);
+ EXPECT_EQ(mem.data(store).size(), 2);
}
diff --git a/crates/c-api/tests/memory_type.cc b/crates/c-api/tests/memory_type.cc
index 1f77967890..173c8cabc0 100644
--- a/crates/c-api/tests/memory_type.cc
+++ b/crates/c-api/tests/memory_type.cc
@@ -9,6 +9,8 @@ TEST(MemoryType, Simple) {
EXPECT_EQ(ty->max(), std::nullopt);
EXPECT_FALSE(ty->is_64());
EXPECT_FALSE(ty->is_shared());
+ EXPECT_EQ(ty->page_size_log2(), 16);
+ EXPECT_EQ(ty->page_size(), 1 << 16);
}
TEST(MemoryType, WithMax) {
@@ -17,6 +19,8 @@ TEST(MemoryType, WithMax) {
EXPECT_EQ(ty->max(), 2);
EXPECT_FALSE(ty->is_64());
EXPECT_FALSE(ty->is_shared());
+ EXPECT_EQ(ty->page_size_log2(), 16);
+ EXPECT_EQ(ty->page_size(), 1 << 16);
}
TEST(MemoryType, Mem64) {
@@ -25,35 +29,64 @@ TEST(MemoryType, Mem64) {
EXPECT_EQ(ty->max(), std::nullopt);
EXPECT_TRUE(ty->is_64());
EXPECT_FALSE(ty->is_shared());
+ EXPECT_EQ(ty->page_size_log2(), 16);
+ EXPECT_EQ(ty->page_size(), 1 << 16);
ty = MemoryType::New64(1, 2);
EXPECT_EQ(ty->min(), 1);
EXPECT_EQ(ty->max(), 2);
EXPECT_TRUE(ty->is_64());
EXPECT_FALSE(ty->is_shared());
+ EXPECT_EQ(ty->page_size_log2(), 16);
+ EXPECT_EQ(ty->page_size(), 1 << 16);
}
TEST(MemoryType, Builder) {
- MemoryType ty = MemoryType::Builder().build();
+ MemoryType ty = MemoryType::Builder().build().unwrap();
EXPECT_EQ(ty->min(), 0);
EXPECT_EQ(ty->max(), std::nullopt);
EXPECT_FALSE(ty->is_64());
EXPECT_FALSE(ty->is_shared());
+ EXPECT_EQ(ty->page_size_log2(), 16);
+ EXPECT_EQ(ty->page_size(), 1 << 16);
- ty = MemoryType::Builder().max(4).shared(true).memory64(true).build();
+ ty =
+ MemoryType::Builder().max(4).shared(true).memory64(true).build().unwrap();
EXPECT_EQ(ty->min(), 0);
EXPECT_EQ(ty->max(), 4);
EXPECT_TRUE(ty->is_64());
EXPECT_TRUE(ty->is_shared());
+ EXPECT_EQ(ty->page_size_log2(), 16);
+ EXPECT_EQ(ty->page_size(), 1 << 16);
ty = MemoryType::Builder()
.min(5)
.max(500)
.shared(true)
.memory64(false)
- .build();
+ .build()
+ .unwrap();
EXPECT_EQ(ty->min(), 5);
EXPECT_EQ(ty->max(), 500);
EXPECT_FALSE(ty->is_64());
EXPECT_TRUE(ty->is_shared());
+ EXPECT_EQ(ty->page_size_log2(), 16);
+ EXPECT_EQ(ty->page_size(), 1 << 16);
+
+ // 1-byte custom page size.
+ ty = MemoryType::Builder()
+ .min(1 << 16)
+ .max(1 << 17)
+ .page_size_log2(0)
+ .build()
+ .unwrap();
+ EXPECT_EQ(ty->min(), 1 << 16);
+ EXPECT_EQ(ty->max(), 1 << 17);
+ EXPECT_FALSE(ty->is_64());
+ EXPECT_FALSE(ty->is_shared());
+ EXPECT_EQ(ty->page_size_log2(), 0);
+ EXPECT_EQ(ty->page_size(), 1);
+
+ // Invalid custom page size.
+ EXPECT_FALSE(MemoryType::Builder().min(1).page_size_log2(4).build());
}