Skip to content

Commit

Permalink
Add support for the custom-page-sizes proposal
Browse files Browse the repository at this point in the history
This adds support in the binary/text parsers and writers,
the validator and interpreter, and objdump (but not wasm2c).
  • Loading branch information
keithw committed Nov 7, 2024
1 parent c1d97e9 commit e2429a9
Show file tree
Hide file tree
Showing 43 changed files with 469 additions and 128 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Wabt has been compiled to JavaScript via emscripten. Some of the functionality i
| [multi-memory][] | `--enable-multi-memory` | ||||||
| [extended-const][] | `--enable-extended-const` | ||||||
| [relaxed-simd][] | `--enable-relaxed-simd` | ||||| |
| [custom-page-sizes][] | `--enable-custom-page-sizes`| ||||| |

[exception handling]: https://github.com/WebAssembly/exception-handling
[mutable globals]: https://github.com/WebAssembly/mutable-global
Expand All @@ -78,6 +79,7 @@ Wabt has been compiled to JavaScript via emscripten. Some of the functionality i
[multi-memory]: https://github.com/WebAssembly/multi-memory
[extended-const]: https://github.com/WebAssembly/extended-const
[relaxed-simd]: https://github.com/WebAssembly/relaxed-simd
[custom-page-sizes]: https://github.com/WebAssembly/custom-page-sizes

## Cloning

Expand Down
7 changes: 5 additions & 2 deletions include/wabt/binary-reader-logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
std::string_view module_name,
std::string_view field_name,
Index memory_index,
const Limits* page_limits) override;
const Limits* page_limits,
uint32_t page_size) override;
Result OnImportGlobal(Index import_index,
std::string_view module_name,
std::string_view field_name,
Expand Down Expand Up @@ -102,7 +103,9 @@ class BinaryReaderLogging : public BinaryReaderDelegate {

Result BeginMemorySection(Offset size) override;
Result OnMemoryCount(Index count) override;
Result OnMemory(Index index, const Limits* limits) override;
Result OnMemory(Index index,
const Limits* limits,
uint32_t page_size) override;
Result EndMemorySection() override;

Result BeginGlobalSection(Offset size) override;
Expand Down
7 changes: 5 additions & 2 deletions include/wabt/binary-reader-nop.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class BinaryReaderNop : public BinaryReaderDelegate {
std::string_view module_name,
std::string_view field_name,
Index memory_index,
const Limits* page_limits) override {
const Limits* page_limits,
uint32_t page_size) override {
return Result::Ok;
}
Result OnImportGlobal(Index import_index,
Expand Down Expand Up @@ -130,7 +131,9 @@ class BinaryReaderNop : public BinaryReaderDelegate {
/* Memory section */
Result BeginMemorySection(Offset size) override { return Result::Ok; }
Result OnMemoryCount(Index count) override { return Result::Ok; }
Result OnMemory(Index index, const Limits* limits) override {
Result OnMemory(Index index,
const Limits* limits,
uint32_t page_size) override {
return Result::Ok;
}
Result EndMemorySection() override { return Result::Ok; }
Expand Down
7 changes: 5 additions & 2 deletions include/wabt/binary-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class BinaryReaderDelegate {
std::string_view module_name,
std::string_view field_name,
Index memory_index,
const Limits* page_limits) = 0;
const Limits* page_limits,
uint32_t page_size) = 0;
virtual Result OnImportGlobal(Index import_index,
std::string_view module_name,
std::string_view field_name,
Expand Down Expand Up @@ -156,7 +157,9 @@ class BinaryReaderDelegate {
/* Memory section */
virtual Result BeginMemorySection(Offset size) = 0;
virtual Result OnMemoryCount(Index count) = 0;
virtual Result OnMemory(Index index, const Limits* limits) = 0;
virtual Result OnMemory(Index index,
const Limits* limits,
uint32_t page_size) = 0;
virtual Result EndMemorySection() = 0;

/* Global section */
Expand Down
7 changes: 6 additions & 1 deletion include/wabt/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
#define WABT_BINARY_LIMITS_HAS_MAX_FLAG 0x1
#define WABT_BINARY_LIMITS_IS_SHARED_FLAG 0x2
#define WABT_BINARY_LIMITS_IS_64_FLAG 0x4
#define WABT_BINARY_LIMITS_ALL_FLAGS \
#define WABT_BINARY_LIMITS_HAS_CUSTOM_PAGE_SIZE_FLAG 0x8
#define WABT_BINARY_LIMITS_ALL_MEMORY_FLAGS \
(WABT_BINARY_LIMITS_HAS_MAX_FLAG | WABT_BINARY_LIMITS_IS_SHARED_FLAG | \
WABT_BINARY_LIMITS_IS_64_FLAG | \
WABT_BINARY_LIMITS_HAS_CUSTOM_PAGE_SIZE_FLAG)
#define WABT_BINARY_LIMITS_ALL_TABLE_FLAGS \
(WABT_BINARY_LIMITS_HAS_MAX_FLAG | WABT_BINARY_LIMITS_IS_SHARED_FLAG | \
WABT_BINARY_LIMITS_IS_64_FLAG)

Expand Down
12 changes: 6 additions & 6 deletions include/wabt/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
#define WABT_USE(x) static_cast<void>(x)

// 64k
#define WABT_PAGE_SIZE 0x10000
#define WABT_DEFAULT_PAGE_SIZE 0x10000
// # of pages that fit in 32-bit address space
#define WABT_MAX_PAGES32 0x10000
#define WABT_MAX_DEFAULT_PAGES32 0x10000
// # of pages that fit in 64-bit address space
#define WABT_MAX_PAGES64 0x1000000000000
#define WABT_BYTES_TO_PAGES(x) ((x) >> 16)
#define WABT_ALIGN_UP_TO_PAGE(x) \
(((x) + WABT_PAGE_SIZE - 1) & ~(WABT_PAGE_SIZE - 1))
#define WABT_MAX_DEFAULT_PAGES64 0x1000000000000
#define WABT_BYTES_TO_DEFAULT_PAGES(x) ((x) >> 16)
#define WABT_ALIGN_UP_TO_DEFAULT_PAGE(x) \
(((x) + WABT_DEFAULT_PAGE_SIZE - 1) & ~(WABT_DEFAULT_PAGE_SIZE - 1))

#define WABT_ENUM_COUNT(name) \
(static_cast<int>(name::Last) - static_cast<int>(name::First) + 1)
Expand Down
1 change: 1 addition & 0 deletions include/wabt/feature.def
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ WABT_FEATURE(memory64, "memory64", false, "64-bit me
WABT_FEATURE(multi_memory, "multi-memory", false, "Multi-memory")
WABT_FEATURE(extended_const, "extended-const", false, "Extended constant expressions")
WABT_FEATURE(relaxed_simd, "relaxed-simd", false, "Relaxed SIMD")
WABT_FEATURE(custom_page_sizes, "custom-page-sizes", false, "Custom page sizes")
11 changes: 8 additions & 3 deletions include/wabt/interp/interp-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@ inline bool MemoryType::classof(const ExternType* type) {
return type->kind == skind;
}

inline MemoryType::MemoryType(Limits limits)
: ExternType(ExternKind::Memory), limits(limits) {
inline MemoryType::MemoryType(Limits limits, uint32_t page_size)
: ExternType(ExternKind::Memory), limits(limits), page_size(page_size) {
// Always set max.
if (!limits.has_max) {
this->limits.max = limits.is_64 ? WABT_MAX_PAGES64 : WABT_MAX_PAGES32;
this->limits.max =
limits.is_64 ? WABT_MAX_DEFAULT_PAGES64 : WABT_MAX_DEFAULT_PAGES32;
if (page_size == 1) {
this->limits.max = limits.is_64 ? std::numeric_limits<uint64_t>::max()
: std::numeric_limits<uint32_t>::max();
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion include/wabt/interp/interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ struct MemoryType : ExternType {
static const ExternKind skind = ExternKind::Memory;
static bool classof(const ExternType* type);

explicit MemoryType(Limits);
explicit MemoryType(Limits, uint32_t);

std::unique_ptr<ExternType> Clone() const override;

Expand All @@ -215,6 +215,7 @@ struct MemoryType : ExternType {
std::string* out_msg);

Limits limits;
uint32_t page_size;
};

struct GlobalType : ExternType {
Expand Down
1 change: 1 addition & 0 deletions include/wabt/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ struct Memory {

std::string name;
Limits page_limits;
uint32_t page_size;
};

struct DataSegment {
Expand Down
2 changes: 1 addition & 1 deletion include/wabt/shared-validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SharedValidator {

Result OnFunction(const Location&, Var sig_var);
Result OnTable(const Location&, Type elem_type, const Limits&);
Result OnMemory(const Location&, const Limits&);
Result OnMemory(const Location&, const Limits&, uint32_t page_size);
Result OnGlobalImport(const Location&, Type type, bool mutable_);
Result OnGlobal(const Location&, Type type, bool mutable_);
Result OnTag(const Location&, Var sig_var);
Expand Down
1 change: 1 addition & 0 deletions include/wabt/token.def
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ WABT_TOKEN(NanArithmetic, "nan:arithmetic")
WABT_TOKEN(NanCanonical, "nan:canonical")
WABT_TOKEN(Offset, "offset")
WABT_TOKEN(Output, "output")
WABT_TOKEN(PageSize, "pagesize")
WABT_TOKEN(Param, "param")
WABT_TOKEN(Ref, "ref")
WABT_TOKEN(Quote, "quote")
Expand Down
1 change: 1 addition & 0 deletions include/wabt/wast-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class WastParser {
Result ParseMemidx(Location loc, Var* memidx);
Result ParseLimitsIndex(Limits*);
Result ParseLimits(Limits*);
Result ParsePageSize(uint32_t*);
Result ParseNat(uint64_t*, bool is_64);

Result ParseModuleFieldList(Module*);
Expand Down
16 changes: 12 additions & 4 deletions src/binary-reader-ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class BinaryReaderIR : public BinaryReaderNop {
std::string_view module_name,
std::string_view field_name,
Index memory_index,
const Limits* page_limits) override;
const Limits* page_limits,
uint32_t page_size) override;
Result OnImportGlobal(Index import_index,
std::string_view module_name,
std::string_view field_name,
Expand All @@ -148,7 +149,9 @@ class BinaryReaderIR : public BinaryReaderNop {
const Limits* elem_limits) override;

Result OnMemoryCount(Index count) override;
Result OnMemory(Index index, const Limits* limits) override;
Result OnMemory(Index index,
const Limits* limits,
uint32_t page_size) override;

Result OnGlobalCount(Index count) override;
Result BeginGlobal(Index index, Type type, bool mutable_) override;
Expand Down Expand Up @@ -620,11 +623,13 @@ Result BinaryReaderIR::OnImportMemory(Index import_index,
std::string_view module_name,
std::string_view field_name,
Index memory_index,
const Limits* page_limits) {
const Limits* page_limits,
uint32_t page_size) {
auto import = std::make_unique<MemoryImport>();
import->module_name = module_name;
import->field_name = field_name;
import->memory.page_limits = *page_limits;
import->memory.page_size = page_size;
if (import->memory.page_limits.is_shared) {
module_->features_used.threads = true;
}
Expand Down Expand Up @@ -707,10 +712,13 @@ Result BinaryReaderIR::OnMemoryCount(Index count) {
return Result::Ok;
}

Result BinaryReaderIR::OnMemory(Index index, const Limits* page_limits) {
Result BinaryReaderIR::OnMemory(Index index,
const Limits* page_limits,
uint32_t page_size) {
auto field = std::make_unique<MemoryModuleField>(GetLocation());
Memory& memory = field->memory;
memory.page_limits = *page_limits;
memory.page_size = page_size;
if (memory.page_limits.is_shared) {
module_->features_used.threads = true;
}
Expand Down
11 changes: 7 additions & 4 deletions src/binary-reader-logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,15 @@ Result BinaryReaderLogging::OnImportMemory(Index import_index,
std::string_view module_name,
std::string_view field_name,
Index memory_index,
const Limits* page_limits) {
const Limits* page_limits,
uint32_t page_size) {
char buf[100];
SPrintLimits(buf, sizeof(buf), page_limits);
LOGF("OnImportMemory(import_index: %" PRIindex ", memory_index: %" PRIindex
", %s)\n",
import_index, memory_index, buf);
return reader_->OnImportMemory(import_index, module_name, field_name,
memory_index, page_limits);
memory_index, page_limits, page_size);
}

Result BinaryReaderLogging::OnImportGlobal(Index import_index,
Expand Down Expand Up @@ -264,11 +265,13 @@ Result BinaryReaderLogging::OnTable(Index index,
return reader_->OnTable(index, elem_type, elem_limits);
}

Result BinaryReaderLogging::OnMemory(Index index, const Limits* page_limits) {
Result BinaryReaderLogging::OnMemory(Index index,
const Limits* page_limits,
uint32_t page_size) {
char buf[100];
SPrintLimits(buf, sizeof(buf), page_limits);
LOGF("OnMemory(index: %" PRIindex ", %s)\n", index, buf);
return reader_->OnMemory(index, page_limits);
return reader_->OnMemory(index, page_limits, page_size);
}

Result BinaryReaderLogging::BeginGlobal(Index index, Type type, bool mutable_) {
Expand Down
20 changes: 16 additions & 4 deletions src/binary-reader-objdump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase {
std::string_view module_name,
std::string_view field_name,
Index memory_index,
const Limits* page_limits) override;
const Limits* page_limits,
uint32_t page_size) override;
Result OnImportGlobal(Index import_index,
std::string_view module_name,
std::string_view field_name,
Expand All @@ -1081,7 +1082,9 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase {
const Limits* elem_limits) override;

Result OnMemoryCount(Index count) override;
Result OnMemory(Index index, const Limits* limits) override;
Result OnMemory(Index index,
const Limits* limits,
uint32_t page_size) override;

Result OnGlobalCount(Index count) override;
Result BeginGlobal(Index index, Type type, bool mutable_) override;
Expand Down Expand Up @@ -1563,7 +1566,8 @@ Result BinaryReaderObjdump::OnImportMemory(Index import_index,
std::string_view module_name,
std::string_view field_name,
Index memory_index,
const Limits* page_limits) {
const Limits* page_limits,
uint32_t page_size) {
PrintDetails(" - memory[%" PRIindex "] pages: initial=%" PRId64, memory_index,
page_limits->initial);
if (page_limits->has_max) {
Expand All @@ -1575,6 +1579,9 @@ Result BinaryReaderObjdump::OnImportMemory(Index import_index,
if (page_limits->is_64) {
PrintDetails(" i64");
}
if (page_size != WABT_DEFAULT_PAGE_SIZE) {
PrintDetails(" (pagesize %u)", page_size);
}
PrintDetails(" <- " PRIstringview "." PRIstringview "\n",
WABT_PRINTF_STRING_VIEW_ARG(module_name),
WABT_PRINTF_STRING_VIEW_ARG(field_name));
Expand Down Expand Up @@ -1615,7 +1622,9 @@ Result BinaryReaderObjdump::OnMemoryCount(Index count) {
return OnCount(count);
}

Result BinaryReaderObjdump::OnMemory(Index index, const Limits* page_limits) {
Result BinaryReaderObjdump::OnMemory(Index index,
const Limits* page_limits,
uint32_t page_size) {
PrintDetails(" - memory[%" PRIindex "] pages: initial=%" PRId64, index,
page_limits->initial);
if (page_limits->has_max) {
Expand All @@ -1627,6 +1636,9 @@ Result BinaryReaderObjdump::OnMemory(Index index, const Limits* page_limits) {
if (page_limits->is_64) {
PrintDetails(" i64");
}
if (page_size != WABT_DEFAULT_PAGE_SIZE) {
PrintDetails(" (pagesize %u)", page_size);
}
PrintDetails("\n");
return Result::Ok;
}
Expand Down
Loading

0 comments on commit e2429a9

Please sign in to comment.