Skip to content

Commit

Permalink
Fix a bug in Memory size type
Browse files Browse the repository at this point in the history
Signed-off-by: HyukWoo Park <[email protected]>
  • Loading branch information
clover2123 committed Oct 31, 2023
1 parent 4055596 commit 566a2f7
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/api/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ struct wasm_memorytype_t : wasm_externtype_t {

wasm_memorytype_t(const MemoryType* type)
: wasm_externtype_t(WASM_EXTERN_MEMORY)
, limits{ type->initialSize() / (uint32_t)MEMORY_PAGE_SIZE, type->maximumSize() / (uint32_t)MEMORY_PAGE_SIZE }
, limits{ (uint32_t)type->initialSize() / (uint32_t)MEMORY_PAGE_SIZE, (uint32_t)type->maximumSize() / (uint32_t)MEMORY_PAGE_SIZE }
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/WASMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
m_result.m_memoryTypes.reserve(count);
}

virtual void OnMemory(Index index, size_t initialSize, size_t maximumSize) override
virtual void OnMemory(Index index, uint64_t initialSize, uint64_t maximumSize) override
{
ASSERT(index == m_result.m_memoryTypes.size());
m_result.m_memoryTypes.push_back(new Walrus::MemoryType(initialSize, maximumSize));
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

namespace Walrus {

Memory* Memory::createMemory(Store* store, uint32_t initialSizeInByte, uint32_t maximumSizeInByte)
Memory* Memory::createMemory(Store* store, uint64_t initialSizeInByte, uint64_t maximumSizeInByte)
{
Memory* mem = new Memory(initialSizeInByte, maximumSizeInByte);
store->appendExtern(mem);
return mem;
}

Memory::Memory(uint32_t initialSizeInByte, uint32_t maximumSizeInByte)
Memory::Memory(uint64_t initialSizeInByte, uint64_t maximumSizeInByte)
: m_sizeInByte(initialSizeInByte)
, m_maximumSizeInByte(maximumSizeInByte)
, m_buffer(reinterpret_cast<uint8_t*>(calloc(1, initialSizeInByte)))
Expand Down
16 changes: 8 additions & 8 deletions src/runtime/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Memory : public Extern {
public:
static const uint32_t s_memoryPageSize = 1024 * 64;

static Memory* createMemory(Store* store, uint32_t initialSizeInByte, uint32_t maximumSizeInByte = std::numeric_limits<uint32_t>::max());
static Memory* createMemory(Store* store, uint64_t initialSizeInByte, uint64_t maximumSizeInByte);

~Memory();

Expand All @@ -49,22 +49,22 @@ class Memory : public Extern {
return m_buffer;
}

uint32_t sizeInByte() const
uint64_t sizeInByte() const
{
return m_sizeInByte;
}

uint32_t sizeInPageSize() const
uint64_t sizeInPageSize() const
{
return sizeInByte() / s_memoryPageSize;
}

uint32_t maximumSizeInByte() const
uint64_t maximumSizeInByte() const
{
return m_maximumSizeInByte;
}

uint32_t maximumSizeInPageSize() const
uint64_t maximumSizeInPageSize() const
{
return m_maximumSizeInByte / s_memoryPageSize;
}
Expand Down Expand Up @@ -114,7 +114,7 @@ class Memory : public Extern {
void fill(ExecutionState& state, uint32_t start, uint8_t value, uint32_t size);

private:
Memory(uint32_t initialSizeInByte, uint32_t maximumSizeInByte);
Memory(uint64_t initialSizeInByte, uint64_t maximumSizeInByte);

void throwException(ExecutionState& state, uint32_t offset, uint32_t addend, uint32_t size) const;
inline bool checkAccess(uint32_t offset, uint32_t size, uint32_t addend = 0) const
Expand All @@ -132,8 +132,8 @@ class Memory : public Extern {
inline void copyMemory(uint32_t dstStart, uint32_t srcStart, uint32_t size);
inline void fillMemory(uint32_t start, uint8_t value, uint32_t size);

uint32_t m_sizeInByte;
uint32_t m_maximumSizeInByte;
uint64_t m_sizeInByte;
uint64_t m_maximumSizeInByte;
uint8_t* m_buffer;
};

Expand Down
14 changes: 5 additions & 9 deletions src/runtime/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,8 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
Trap::throwException(state, "incompatible import type");
}

if (m_imports[i]->tableType()->maximumSize() != std::numeric_limits<uint32_t>::max()) {
if (imports[i]->asTable()->maximumSize() == std::numeric_limits<uint32_t>::max()
|| imports[i]->asTable()->maximumSize() > m_imports[i]->tableType()->maximumSize())
Trap::throwException(state, "incompatible import type");
if (imports[i]->asTable()->maximumSize() > m_imports[i]->tableType()->maximumSize()) {
Trap::throwException(state, "incompatible import type");
}
instance->m_tables[tableIndex++] = imports[i]->asTable();
break;
Expand All @@ -168,10 +166,8 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
Trap::throwException(state, "incompatible import type");
}

if (m_imports[i]->memoryType()->maximumSize() != std::numeric_limits<uint32_t>::max()) {
if (imports[i]->asMemory()->maximumSizeInPageSize() == std::numeric_limits<uint32_t>::max()
|| imports[i]->asMemory()->maximumSizeInPageSize() > m_imports[i]->memoryType()->maximumSize())
Trap::throwException(state, "incompatible import type");
if (imports[i]->asMemory()->maximumSizeInPageSize() > m_imports[i]->memoryType()->maximumSize()) {
Trap::throwException(state, "incompatible import type");
}
instance->m_memories[memIndex++] = imports[i]->asMemory();
break;
Expand Down Expand Up @@ -282,7 +278,7 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
&data);
}

if (UNLIKELY(elem->tableIndex() >= numberOfTableTypes() || index >= instance->m_tables[elem->tableIndex()]->size() || index + elem->functionIndex().size() > instance->m_tables[elem->tableIndex()]->size())) {
if (UNLIKELY(elem->tableIndex() >= numberOfTableTypes() || index + elem->functionIndex().size() > instance->m_tables[elem->tableIndex()]->size())) {
Trap::throwException(state, "out of bounds table access");
}

Expand Down
11 changes: 6 additions & 5 deletions src/runtime/ObjectType.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,20 @@ class TableType : public ObjectType {

class MemoryType : public ObjectType {
public:
MemoryType(uint32_t initSize, uint32_t maxSize)
MemoryType(uint64_t initSize, uint64_t maxSize)
: ObjectType(ObjectType::MemoryKind)
, m_initialSize(initSize)
, m_maximumSize(maxSize)
{
}

uint32_t initialSize() const { return m_initialSize; }
uint32_t maximumSize() const { return m_maximumSize; }
uint64_t initialSize() const { return m_initialSize; }
uint64_t maximumSize() const { return m_maximumSize; }

private:
uint32_t m_initialSize;
uint32_t m_maximumSize;
// size should be uint64_t type
uint64_t m_initialSize;
uint64_t m_maximumSize;
};

class TagType : public ObjectType {
Expand Down
4 changes: 3 additions & 1 deletion src/shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,8 @@ static void executeWAST(Store* store, const std::string& filename, const std::ve
case wabt::CommandType::ScriptModule: {
auto* moduleCommand = static_cast<wabt::ModuleCommand*>(command.get());
auto buf = readModuleData(&moduleCommand->module);
executeWASM(store, filename, buf->data, functionTypes, &registeredInstanceMap);
auto trapResult = executeWASM(store, filename, buf->data, functionTypes, &registeredInstanceMap);
RELEASE_ASSERT(!trapResult.exception);
instanceMap[commandCount] = store->getLastInstance();
if (moduleCommand->module.name.size()) {
registeredInstanceMap[moduleCommand->module.name] = store->getLastInstance();
Expand Down Expand Up @@ -767,6 +768,7 @@ static void executeWAST(Store* store, const std::string& filename, const std::ve
RELEASE_ASSERT(tsm);
auto buf = readModuleData(&tsm->module);
auto trapResult = executeWASM(store, filename, buf->data, functionTypes, &registeredInstanceMap);
RELEASE_ASSERT(trapResult.exception);
std::string& s = trapResult.exception->message();
RELEASE_ASSERT(s.find(assertModuleUninstantiable->text) == 0);
printf("assertModuleUninstantiable (expect exception: %s(line: %d)) : OK\n", assertModuleUninstantiable->text.data(), assertModuleUninstantiable->module->location().line);
Expand Down
6 changes: 6 additions & 0 deletions test/basic/memory.wast
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@
(assert_return (invoke "func1") (i32.const 3))
(assert_return (invoke "func2" (i32.const 5)) (i32.const 3))
(assert_return (invoke "func1") (i32.const 8))

(module
(memory 0)
(func (export "grow") (param i32) (result i32) (memory.grow (local.get 0)))
)
(assert_return (invoke "grow" (i32.const 0x10000)) (i32.const 0))
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class WASMBinaryReaderDelegate {
virtual void OnExport(int kind, Index exportIndex, std::string name, Index itemIndex) = 0;

virtual void OnMemoryCount(Index count) = 0;
virtual void OnMemory(Index index, size_t initialSize, size_t maximumSize) = 0;
virtual void OnMemory(Index index, uint64_t initialSize, uint64_t maximumSize) = 0;

virtual void OnDataSegmentCount(Index count) = 0;
virtual void BeginDataSegment(Index index, Index memoryIndex, uint8_t flags) = 0;
Expand Down
4 changes: 2 additions & 2 deletions third_party/wabt/src/walrus/binary-reader-walrus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnImportMemory(Index import_index, std::string_view module_name, std::string_view field_name, Index memory_index, const Limits *page_limits) override {
CHECK_RESULT(m_validator.OnMemory(GetLocation(), *page_limits));
m_externalDelegate->OnImportMemory(import_index, std::string(module_name), std::string(field_name), memory_index, page_limits->initial, page_limits->has_max ? page_limits->max : (std::numeric_limits<size_t>::max() / (1024 * 64)));
m_externalDelegate->OnImportMemory(import_index, std::string(module_name), std::string(field_name), memory_index, page_limits->initial, page_limits->has_max ? page_limits->max : (page_limits->is_64? WABT_MAX_PAGES64 : WABT_MAX_PAGES32));
return Result::Ok;
}
Result OnImportGlobal(Index import_index, std::string_view module_name, std::string_view field_name, Index global_index, Type type, bool mutable_) override {
Expand Down Expand Up @@ -307,7 +307,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnMemory(Index index, const Limits *limits) override {
CHECK_RESULT(m_validator.OnMemory(GetLocation(), *limits));
m_externalDelegate->OnMemory(index, limits->initial, limits->has_max ? limits->max : (std::numeric_limits<size_t>::max() / (1024 * 64)));
m_externalDelegate->OnMemory(index, limits->initial, limits->has_max ? limits->max : (limits->is_64? WABT_MAX_PAGES64 : WABT_MAX_PAGES32));
return Result::Ok;
}
Result EndMemorySection() override {
Expand Down

0 comments on commit 566a2f7

Please sign in to comment.