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
2223namespace 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
2567class RuntimeMemory {
2668public:
@@ -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
3986protected:
4087 const Memory memoryDefinition;
88+
89+ private:
90+ std::vector<uint8_t > memory;
4191};
4292
4393class RealRuntimeMemory : public RuntimeMemory {};
0 commit comments