diff options
author | Thomas Lively <tlively@google.com> | 2024-12-20 16:43:02 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-20 16:43:02 -0800 |
commit | 4d8a933e1136159160f2b45ad3a9a1c82021a75b (patch) | |
tree | 3cd2878e4a938b1878f4adbae9a2967200e31cb9 | |
parent | 5ed6cf191aa88b424f6784ba27ac2ab069234fd7 (diff) | |
download | binaryen-4d8a933e1136159160f2b45ad3a9a1c82021a75b.tar.gz binaryen-4d8a933e1136159160f2b45ad3a9a1c82021a75b.tar.bz2 binaryen-4d8a933e1136159160f2b45ad3a9a1c82021a75b.zip |
Fix UBSan on CI (#7173)
The UBSan builder started failing with an error about a misaligned store
in wasm-ctor-eval.cpp. The store was already done via `memcpy` to avoid
alignment issues, but apparently this is no longer enough. Use `void*`
as the destination type to further avoid giving the impression of
guaranteed alignment.
Also fix UB when executing std::abs on minimum negative integers in
literal.cpp.
-rw-r--r-- | src/tools/wasm-ctor-eval.cpp | 14 | ||||
-rw-r--r-- | src/wasm/literal.cpp | 6 |
2 files changed, 13 insertions, 7 deletions
diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index 89727d012..17927f5a6 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -462,30 +462,30 @@ private: const size_t MaximumMemory = 100 * 1024 * 1024; // TODO: handle unaligned too, see shell-interface - template<typename T> T* getMemory(Address address, Name memoryName) { + void* getMemory(Address address, Name memoryName, size_t size) { auto it = memories.find(memoryName); assert(it != memories.end()); auto& memory = it->second; // resize the memory buffer as needed. - auto max = address + sizeof(T); + auto max = address + size; if (max > memory.size()) { if (max > MaximumMemory) { throw FailToEvalException("excessively high memory address accessed"); } memory.resize(max); } - return (T*)(&memory[address]); + return &memory[address]; } template<typename T> void doStore(Address address, T value, Name memoryName) { - // do a memcpy to avoid undefined behavior if unaligned - memcpy(getMemory<T>(address, memoryName), &value, sizeof(T)); + // Use memcpy to avoid UB if unaligned. + memcpy(getMemory(address, memoryName, sizeof(T)), &value, sizeof(T)); } template<typename T> T doLoad(Address address, Name memoryName) { - // do a memcpy to avoid undefined behavior if unaligned + // Use memcpy to avoid UB if unaligned. T ret; - memcpy(&ret, getMemory<T>(address, memoryName), sizeof(T)); + memcpy(&ret, getMemory(address, memoryName, sizeof(T)), sizeof(T)); return ret; } diff --git a/src/wasm/literal.cpp b/src/wasm/literal.cpp index b53378cfa..05027ee6b 100644 --- a/src/wasm/literal.cpp +++ b/src/wasm/literal.cpp @@ -978,8 +978,14 @@ Literal Literal::neg() const { Literal Literal::abs() const { switch (type.getBasic()) { case Type::i32: + if (i32 == std::numeric_limits<int32_t>::min()) { + return *this; + } return Literal(std::abs(i32)); case Type::i64: + if (i64 == std::numeric_limits<int64_t>::min()) { + return *this; + } return Literal(std::abs(i64)); case Type::f32: return Literal(i32 & 0x7fffffff).castToF32(); |