From 351b58aeaff98d01cc05dd45cf05eaf9e944d706 Mon Sep 17 00:00:00 2001 From: Ilya Trubachev Date: Fri, 11 Nov 2022 20:13:38 +0300 Subject: [PATCH] Fix unaligned loads Signed-off-by: Ilya Trubachev --- runtime/builtins/builtins_arraybuffer.cpp | 15 ++++++--------- runtime/js_bigint.cpp | 2 +- runtime/js_collator.h | 2 +- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/runtime/builtins/builtins_arraybuffer.cpp b/runtime/builtins/builtins_arraybuffer.cpp index 4c966c09a..a11d3fda9 100644 --- a/runtime/builtins/builtins_arraybuffer.cpp +++ b/runtime/builtins/builtins_arraybuffer.cpp @@ -478,7 +478,7 @@ JSTaggedValue BuiltinsArrayBuffer::GetValueFromBufferForFloat(uint8_t *block, in // NOLINTNEXTLINE(readability-braces-around-statements) if constexpr (std::is_same_v) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic) - unionValue.uValue = *reinterpret_cast(block + byteIndex); + unionValue.uValue = UnalignedLoad(reinterpret_cast(block + byteIndex)); // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) if (std::isnan(unionValue.value)) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) @@ -491,7 +491,7 @@ JSTaggedValue BuiltinsArrayBuffer::GetValueFromBufferForFloat(uint8_t *block, in // NOLINTNEXTLINE(readability-misleading-indentation) } else if constexpr (std::is_same_v) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic) - unionValue.uValue = *reinterpret_cast(block + byteIndex); + unionValue.uValue = UnalignedLoad(reinterpret_cast(block + byteIndex)); // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) if (std::isnan(unionValue.value) && !JSTaggedValue::IsImpureNaN(unionValue.value)) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) @@ -512,7 +512,7 @@ JSTaggedValue BuiltinsArrayBuffer::GetValueFromBufferForBigInt(JSThread *thread, { static_assert(std::is_same_v || std::is_same_v, "T must be uint64_t/int64_t"); // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - auto pTmp = *reinterpret_cast(block + byteIndex); + auto pTmp = UnalignedLoad(reinterpret_cast(block + byteIndex)); if (!littleEndian) { pTmp = BSWAP(pTmp); } @@ -534,8 +534,7 @@ void BuiltinsArrayBuffer::SetValueInBufferForByte(double val, uint8_t *block, in return; } auto int64Val = static_cast(val); - auto *resArr = reinterpret_cast(&int64Val); - res = *resArr; + res = UnalignedLoad(reinterpret_cast(&int64Val)); SetTypeData(block, res, byteIndex); } @@ -568,12 +567,10 @@ void BuiltinsArrayBuffer::SetValueInBufferForInteger(double val, uint8_t *block, auto int64Val = static_cast(val); // NOLINTNEXTLINE(readability-braces-around-statements) if constexpr (std::is_same_v) { - auto *pTmp = reinterpret_cast(&int64Val); - int16_t tmp = *pTmp; + auto tmp = UnalignedLoad(reinterpret_cast(&int64Val)); res = static_cast(tmp); } else { // NOLINTNEXTLINE(readability-braces-around-statements) - auto *pTmp = reinterpret_cast(&int64Val); - res = *pTmp; + res = UnalignedLoad(reinterpret_cast(&int64Val)); } if (!littleEndian) { diff --git a/runtime/js_bigint.cpp b/runtime/js_bigint.cpp index 9e035ef26..b4e389746 100644 --- a/runtime/js_bigint.cpp +++ b/runtime/js_bigint.cpp @@ -604,7 +604,7 @@ void BigInt::BigIntToUint64(JSThread *thread, JSHandle bigInt, ui auto len = static_cast(bigUint64->GetLength()); for (int index = len - 1; index >= 0; --index) { // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - *(addr + index) = bigUint64->GetDigit(index); + UnalignedStore(addr + index, bigUint64->GetDigit(index)); } } diff --git a/runtime/js_collator.h b/runtime/js_collator.h index 72c872102..c08fe8291 100644 --- a/runtime/js_collator.h +++ b/runtime/js_collator.h @@ -64,7 +64,7 @@ public: return; } auto icuCollator = reinterpret_cast(pointer); - icuCollator->~Collator(); + delete icuCollator; } static void SetIcuCollator(JSThread *thread, const JSHandle &collator, icu::Collator *icuCollator, -- Gitee