diff options
author | JF Bastien <jfb@chromium.org> | 2016-02-03 07:32:22 -0800 |
---|---|---|
committer | JF Bastien <jfb@chromium.org> | 2016-02-03 07:32:22 -0800 |
commit | 136cb714d38df62f7f100904a8088a425ff6ae93 (patch) | |
tree | a75d2418a03b88541db33268b45e4c96d4bede0b | |
parent | ff497f7a261331b3aa6a1e7a1f8a5e476f856d3c (diff) | |
download | binaryen-136cb714d38df62f7f100904a8088a425ff6ae93.tar.gz binaryen-136cb714d38df62f7f100904a8088a425ff6ae93.tar.bz2 binaryen-136cb714d38df62f7f100904a8088a425ff6ae93.zip |
Shell: improve memory trap
This makes it easier to debug, the message looks like:
[trap final > memory: 1 > 0]
-rw-r--r-- | src/wasm-interpreter.h | 20 |
1 files changed, 14 insertions, 6 deletions
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 <limits.h> +#include <sstream> #include "support/bits.h" #include "wasm.h" @@ -775,14 +776,21 @@ private: size_t memorySize; - template<class LS> - size_t getFinalAddress(LS *curr, Literal ptr) { + template <class LS> + 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; } |