Skip to content

Commit

Permalink
Use uint32_t for memory size limits
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Sep 3, 2020
1 parent 066b565 commit 6b39f2b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ std::tuple<table_ptr, Limits> allocate_table(
}

std::tuple<bytes_ptr, Limits> allocate_memory(const std::vector<Memory>& module_memories,
const std::vector<ExternalMemory>& imported_memories, unsigned memory_pages_limit)
const std::vector<ExternalMemory>& imported_memories, uint32_t memory_pages_limit)
{
static const auto bytes_delete = [](bytes* b) noexcept { delete b; };
static const auto null_delete = [](bytes*) noexcept {};
Expand Down Expand Up @@ -677,7 +677,7 @@ std::optional<uint32_t> find_export(const Module& module, ExternalKind kind, std
std::unique_ptr<Instance> instantiate(Module module,
std::vector<ExternalFunction> imported_functions, std::vector<ExternalTable> imported_tables,
std::vector<ExternalMemory> imported_memories, std::vector<ExternalGlobal> imported_globals,
unsigned memory_pages_limit /*= DefaultMemoryPagesLimit*/)
uint32_t memory_pages_limit /*= DefaultMemoryPagesLimit*/)
{
assert(module.funcsec.size() == module.codesec.size());

Expand Down
6 changes: 3 additions & 3 deletions lib/fizzy/execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct Instance
bytes_ptr memory = {nullptr, [](bytes*) {}};
Limits memory_limits;
// Hard limit for memory growth in pages, checked when memory is defined as unbounded in module
unsigned memory_pages_limit = 0;
uint32_t memory_pages_limit = 0;
// Table is either allocated and owned by the instance or imported and owned externally.
// For these cases unique_ptr would either have a normal deleter or noop deleter respectively.
table_ptr table = {nullptr, [](table_elements*) {}};
Expand All @@ -84,7 +84,7 @@ struct Instance
std::vector<ExternalFunction> imported_functions;
std::vector<ExternalGlobal> imported_globals;

Instance(Module _module, bytes_ptr _memory, Limits _memory_limits, unsigned _memory_pages_limit,
Instance(Module _module, bytes_ptr _memory, Limits _memory_limits, uint32_t _memory_pages_limit,
table_ptr _table, Limits _table_limits, std::vector<Value> _globals,
std::vector<ExternalFunction> _imported_functions,
std::vector<ExternalGlobal> _imported_globals)
Expand All @@ -106,7 +106,7 @@ std::unique_ptr<Instance> instantiate(Module module,
std::vector<ExternalTable> imported_tables = {},
std::vector<ExternalMemory> imported_memories = {},
std::vector<ExternalGlobal> imported_globals = {},
unsigned memory_pages_limit = DefaultMemoryPagesLimit);
uint32_t memory_pages_limit = DefaultMemoryPagesLimit);

// Execute a function on an instance.
ExecutionResult execute(
Expand Down
8 changes: 5 additions & 3 deletions lib/fizzy/limits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

#pragma once

#include <cstdint>

namespace fizzy
{
// The page size as defined by the WebAssembly 1.0 specification.
constexpr unsigned PageSize = 65536;
constexpr uint32_t PageSize = 65536;

// The maximum memory page limit as defined by the specification.
// It is only possible to address 4 GB (32-bit) of memory.
constexpr unsigned MemoryPagesValidationLimit = (4 * 1024 * 1024 * 1024ULL) / PageSize;
constexpr uint32_t MemoryPagesValidationLimit = (4 * 1024 * 1024 * 1024ULL) / PageSize;
static_assert(MemoryPagesValidationLimit == 65536);

// The default hard limit of the memory size (256MB) as number of pages.
constexpr unsigned DefaultMemoryPagesLimit = (256 * 1024 * 1024ULL) / PageSize;
constexpr uint32_t DefaultMemoryPagesLimit = (256 * 1024 * 1024ULL) / PageSize;

// Call depth limit is set to default limit in wabt.
// https://github.com/WebAssembly/wabt/blob/ae2140ddc6969ef53599fe2fab81818de65db875/src/interp/interp.h#L1007
Expand Down

0 comments on commit 6b39f2b

Please sign in to comment.