Skip to content

Commit 27a50e9

Browse files
wip
1 parent e21bb7b commit 27a50e9

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

src/ir/runtime-memory.h

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,52 @@
1717
#ifndef wasm_ir_runtime_memory_h
1818
#define wasm_ir_runtime_memory_h
1919

20+
#include "interpreter/exception.h"
2021
#include "wasm.h"
2122

2223
namespace wasm {
2324

25+
namespace {
26+
27+
void trapIfGt(uint64_t lhs, uint64_t rhs, const char* msg) {
28+
if (lhs > rhs) {
29+
std::stringstream ss;
30+
ss << msg << ": " << lhs << " > " << rhs;
31+
// ss.str();
32+
throw TrapException{};
33+
}
34+
}
35+
36+
void checkLoadAddress(Address addr, Index bytes, Address memorySize) {
37+
Address memorySizeBytes = memorySize * Memory::kPageSize;
38+
trapIfGt(addr, memorySizeBytes - bytes, "highest > memory");
39+
}
40+
41+
// void checkAtomicAddress(Address addr, Index bytes, Address memorySize) {
42+
// checkLoadAddress(addr, bytes, memorySize);
43+
// // Unaligned atomics trap.
44+
// if (bytes > 1) {
45+
// if (addr & (bytes - 1)) {
46+
// // "unaligned atomic operation"
47+
// throw TrapException{};
48+
// }
49+
// }
50+
// }
51+
52+
Address
53+
getFinalAddress(uint64_t offset, Literal ptr, Index bytes, Address memorySize) {
54+
Address memorySizeBytes = memorySize * Memory::kPageSize;
55+
uint64_t addr = ptr.type == Type::i32 ? ptr.geti32() : ptr.geti64();
56+
trapIfGt(offset, memorySizeBytes, "offset > memory");
57+
trapIfGt(addr, memorySizeBytes - offset, "final > memory");
58+
addr += offset;
59+
trapIfGt(bytes, memorySizeBytes, "bytes > memory");
60+
checkLoadAddress(addr, bytes, memorySize);
61+
return addr;
62+
}
63+
64+
} // namespace
65+
2466
// TODO split into pure virtual class
2567
class RuntimeMemory {
2668
public:
@@ -31,13 +73,21 @@ class RuntimeMemory {
3173
// variants for load8 etc?
3274
// Do we care about the order here?
3375
// todo: address types? Address::address32_t is strange
34-
virtual Literal load(uint32_t addr) const { return {}; }
76+
// todo: type of offset?
77+
virtual Literal
78+
load(uint32_t addr, uint64_t offset, MemoryOrder order) const {
79+
Address address = getFinalAddress(offset, Literal(addr), 4, 1);
80+
return {};
81+
}
3582
virtual Literal load(uint64_t addr) const { return {}; }
3683

3784
const Memory* getDefinition() const { return &memoryDefinition; }
3885

3986
protected:
4087
const Memory memoryDefinition;
88+
89+
private:
90+
std::vector<uint8_t> memory;
4191
};
4292

4393
class RealRuntimeMemory : public RuntimeMemory {};

src/wasm-interpreter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4081,9 +4081,12 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
40814081
}
40824082

40834083
Flow visitLoad(Load* curr) {
4084+
WASM_UNREACHABLE("??");
40844085
VISIT(flow, curr->ptr)
40854086
auto* memory = allMemories[curr->memory];
4086-
return memory->load(static_cast<uint32_t>(flow.getSingleValue().geti32()));
4087+
return memory->load(static_cast<uint32_t>(flow.getSingleValue().geti32()),
4088+
curr->offset,
4089+
curr->order);
40874090
// auto info = getMemoryInstanceInfo(curr->memory);
40884091
// auto memorySize = info.instance->getMemorySize(info.name);
40894092
// auto addr =

0 commit comments

Comments
 (0)