From 136cb714d38df62f7f100904a8088a425ff6ae93 Mon Sep 17 00:00:00 2001 From: JF Bastien Date: Wed, 3 Feb 2016 07:32:22 -0800 Subject: Shell: improve memory trap This makes it easier to debug, the message looks like: [trap final > memory: 1 > 0] --- src/wasm-interpreter.h | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index e0006f37b..8b9c408f1 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -24,6 +24,7 @@ #define wasm_wasm_interpreter_h #include +#include #include "support/bits.h" #include "wasm.h" @@ -775,14 +776,21 @@ private: size_t memorySize; - template - size_t getFinalAddress(LS *curr, Literal ptr) { + template + size_t getFinalAddress(LS* curr, Literal ptr) { + auto trapIfGt = [this](size_t lhs, size_t rhs, const char* msg) { + if (lhs > rhs) { + std::stringstream ss; + ss << msg << ": " << lhs << " > " << rhs; + externalInterface->trap(ss.str().c_str()); + } + }; uint64_t addr = ptr.type == i32 ? ptr.geti32() : ptr.geti64(); - if (memorySize < curr->offset) externalInterface->trap("offset > memory"); - if (addr > memorySize - curr->offset) externalInterface->trap("final > memory"); + trapIfGt(curr->offset, memorySize, "offset > memory"); + trapIfGt(addr, memorySize - curr->offset, "final > memory"); addr += curr->offset; - if (curr->bytes > memorySize) externalInterface->trap("bytes > memory"); - if (addr > memorySize - curr->bytes) externalInterface->trap("highest > memory"); + trapIfGt(curr->bytes, memorySize, "bytes > memory"); + trapIfGt(addr, memorySize - curr->bytes, "highest > memory"); return addr; } -- cgit v1.2.3