diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-04 21:33:02 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-04 21:33:02 -0800 |
commit | e2b0a2a26eac53661964739c2be344c90850626d (patch) | |
tree | d0a7d2a70909a2ee6b1cae4f7a79aee4332ed0cb /src | |
parent | 9db25c0958470f3da763861967c842f42fd112c5 (diff) | |
download | binaryen-e2b0a2a26eac53661964739c2be344c90850626d.tar.gz binaryen-e2b0a2a26eac53661964739c2be344c90850626d.tar.bz2 binaryen-e2b0a2a26eac53661964739c2be344c90850626d.zip |
trap on i2f overflow
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm-interpreter.h | 9 | ||||
-rw-r--r-- | src/wasm-js.cpp | 5 | ||||
-rw-r--r-- | src/wasm-shell.cpp | 12 |
3 files changed, 19 insertions, 7 deletions
diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 7af64e336..a22a631b1 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -3,6 +3,8 @@ // can function as a polyfill. // +#include <limits.h> + #include "wasm.h" namespace wasm { @@ -27,6 +29,7 @@ public: virtual Literal callImport(Import* import, LiteralList& arguments) = 0; virtual Literal load(Load* load, Literal ptr) = 0; virtual void store(Store* store, Literal ptr, Literal value) = 0; + virtual void trap() = 0; }; ModuleInstance(Module& wasm, ExternalInterface* externalInterface) : wasm(wasm), externalInterface(externalInterface) { @@ -356,7 +359,11 @@ public: case ExtendSInt32: return Flow(Literal(int64_t(value.geti32()))); case ExtendUInt32: return Flow(Literal(uint64_t((uint32_t)value.geti32()))); case WrapInt64: return Flow(Literal(int32_t(value.geti64()))); - case TruncSFloat32: return Flow(Literal(int32_t(value.getf32()))); + case TruncSFloat32: { + float val = value.getf32(); + if (val > (double)INT_MAX || val < (double)INT_MIN) instance.externalInterface->trap(); + return Flow(Literal(int32_t(val))); + } case TruncUFloat32: return Flow(Literal(uint32_t(value.getf32()))); case TruncSFloat64: return Flow(Literal(int32_t(value.getf64()))); case TruncUFloat64: return Flow(Literal(int32_t((uint32_t)value.getf64()))); diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp index 6a3e05725..50b35d464 100644 --- a/src/wasm-js.cpp +++ b/src/wasm-js.cpp @@ -174,6 +174,11 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_asm(char *input) { } } } + + void trap() { + std::cerr << "wasm trap!\n"; + abort(); + } }; instance = new ModuleInstance(*module, new JSExternalInterface()); diff --git a/src/wasm-shell.cpp b/src/wasm-shell.cpp index 81e6112ce..1f8d1373c 100644 --- a/src/wasm-shell.cpp +++ b/src/wasm-shell.cpp @@ -35,12 +35,6 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface { } } - jmp_buf trapState; - - void trap() { - longjmp(trapState, 1); - } - Literal callImport(Import *import, ModuleInstance::LiteralList& arguments) override { if (import->name == PRINT) { for (auto argument : arguments) { @@ -97,6 +91,12 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface { default: abort(); } } + + jmp_buf trapState; + + void trap() override { + longjmp(trapState, 1); + } }; int main(int argc, char **argv) { |