diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-09-28 17:00:00 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-28 17:00:00 -0700 |
commit | 345b04253dcf7d42dcefabdec4e4b0edc3a3ca44 (patch) | |
tree | 846f36b84b1e59e180dda600ff0b565db6caf74a | |
parent | 093894012d86673fd6bca304da08ccfba88deb44 (diff) | |
download | binaryen-345b04253dcf7d42dcefabdec4e4b0edc3a3ca44.tar.gz binaryen-345b04253dcf7d42dcefabdec4e4b0edc3a3ca44.tar.bz2 binaryen-345b04253dcf7d42dcefabdec4e4b0edc3a3ca44.zip |
fix signed int64 LEB bug with large negative values (#719)
-rw-r--r-- | src/wasm-binary.h | 50 | ||||
-rw-r--r-- | test/unit.wast | 4 | ||||
-rw-r--r-- | test/unit.wast.fromBinary | 4 | ||||
-rw-r--r-- | test/unit.wast.fromBinary.noDebugInfo | 4 |
4 files changed, 55 insertions, 7 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 70af27856..35f84c6b4 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -48,7 +48,7 @@ struct LEB { bool hasMore(T temp, MiniT byte) { // for signed, we must ensure the last bit has the right sign, as it will zero extend - return std::is_signed<T>::value ? (temp != 0 && int32_t(temp) != -1) || (value >= 0 && (byte & 64)) || (value < 0 && !(byte & 64)): (temp != 0); + return std::is_signed<T>::value ? (temp != 0 && temp != -1) || (value >= 0 && (byte & 64)) || (value < 0 && !(byte & 64)) : (temp != 0); } void write(std::vector<uint8_t>* out) { @@ -165,23 +165,59 @@ public: return *this; } BufferWithRandomAccess& operator<<(U32LEB x) { - if (debug) std::cerr << "writeU32LEB: " << x.value << " (at " << size() << ")" << std::endl; + size_t before = -1; + if (debug) { + before = size(); + std::cerr << "writeU32LEB: " << x.value << " (at " << before << ")" << std::endl; + } x.write(this); + if (debug) { + for (size_t i = before; i < size(); i++) { + std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; + } + } return *this; } BufferWithRandomAccess& operator<<(U64LEB x) { - if (debug) std::cerr << "writeU64LEB: " << x.value << " (at " << size() << ")" << std::endl; + size_t before = -1; + if (debug) { + before = size(); + std::cerr << "writeU64LEB: " << x.value << " (at " << before << ")" << std::endl; + } x.write(this); + if (debug) { + for (size_t i = before; i < size(); i++) { + std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; + } + } return *this; } BufferWithRandomAccess& operator<<(S32LEB x) { - if (debug) std::cerr << "writeS32LEB: " << x.value << " (at " << size() << ")" << std::endl; + size_t before = -1; + if (debug) { + before = size(); + std::cerr << "writeS32LEB: " << x.value << " (at " << before << ")" << std::endl; + } x.write(this); + if (debug) { + for (size_t i = before; i < size(); i++) { + std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; + } + } return *this; } BufferWithRandomAccess& operator<<(S64LEB x) { - if (debug) std::cerr << "writeS64LEB: " << x.value << " (at " << size() << ")" << std::endl; + size_t before = -1; + if (debug) { + before = size(); + std::cerr << "writeS64LEB: " << x.value << " (at " << before << ")" << std::endl; + } x.write(this); + if (debug) { + for (size_t i = before; i < size(); i++) { + std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; + } + } return *this; } @@ -1420,7 +1456,7 @@ public: ret.read([&]() { return (int8_t)getInt8(); }); - if (debug) std::cerr << "getU32LEB: " << ret.value << " ==>" << std::endl; + if (debug) std::cerr << "getS32LEB: " << ret.value << " ==>" << std::endl; return ret.value; } int64_t getS64LEB() { @@ -1429,7 +1465,7 @@ public: ret.read([&]() { return (int8_t)getInt8(); }); - if (debug) std::cerr << "getU64LEB: " << ret.value << " ==>" << std::endl; + if (debug) std::cerr << "getS64LEB: " << ret.value << " ==>" << std::endl; return ret.value; } WasmType getWasmType() { diff --git a/test/unit.wast b/test/unit.wast index 2ba0f6612..90fab25f3 100644 --- a/test/unit.wast +++ b/test/unit.wast @@ -9,6 +9,7 @@ (type $5 (func (result i32))) (type $6 (func (param i32) (result i32))) (type $7 (func (param f64) (result f64))) + (type $8 (func (result i64))) (import "env" "_emscripten_asm_const_vi" (func $_emscripten_asm_const_vi)) (import "asm2wasm" "f64-to-int" (func $f64-to-int (param f64) (result i32))) (import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64))) @@ -436,4 +437,7 @@ (get_local $0) ) ) + (func $big-i64 (type $8) (result i64) + (i64.const -9218868437227405313) + ) ) diff --git a/test/unit.wast.fromBinary b/test/unit.wast.fromBinary index 8516d1775..4ff9f0d73 100644 --- a/test/unit.wast.fromBinary +++ b/test/unit.wast.fromBinary @@ -9,6 +9,7 @@ (type $5 (func (result i32))) (type $6 (func (param i32) (result i32))) (type $7 (func (param f64) (result f64))) + (type $8 (func (result i64))) (import "env" "_emscripten_asm_const_vi" (func $import$0)) (import "asm2wasm" "f64-to-int" (func $import$1 (param f64) (result i32))) (import "asm2wasm" "f64-rem" (func $import$2 (param f64 f64) (result f64))) @@ -446,5 +447,8 @@ (get_local $var$0) ) ) + (func $big-i64 (type $8) (result i64) + (i64.const -9218868437227405313) + ) ) diff --git a/test/unit.wast.fromBinary.noDebugInfo b/test/unit.wast.fromBinary.noDebugInfo index d6980b9be..04448e90b 100644 --- a/test/unit.wast.fromBinary.noDebugInfo +++ b/test/unit.wast.fromBinary.noDebugInfo @@ -9,6 +9,7 @@ (type $5 (func (result i32))) (type $6 (func (param i32) (result i32))) (type $7 (func (param f64) (result f64))) + (type $8 (func (result i64))) (import "env" "_emscripten_asm_const_vi" (func $import$0)) (import "asm2wasm" "f64-to-int" (func $import$1 (param f64) (result i32))) (import "asm2wasm" "f64-rem" (func $import$2 (param f64 f64) (result f64))) @@ -446,5 +447,8 @@ (get_local $var$0) ) ) + (func $21 (type $8) (result i64) + (i64.const -9218868437227405313) + ) ) |