From 1b80be352b32433a9491e6c4dd312f226072a0dd Mon Sep 17 00:00:00 2001 From: Byoungchan Lee Date: Mon, 5 Dec 2022 08:52:39 +0900 Subject: [PATCH] Fix unaligned memory access detected by UBSAN Recently, Chromium added -fsanitize=alignment for `is_ubsan=true`, and due to this, unaligned memory access was found in several places, and chromium roll is currently blocked. Modify unaligned memory access in rtc_base/byte_order.h to use memcpy. Since libaom and libsrtp perform unaligned memory accesses, add them to the suppression list. Also, remove any mention of yasm from the UBSAN suppression list, as yasm is no longer used. Bug: chromium:1057551 Change-Id: I4961b66831750f4fa7b6de0b80b2052fe6ef27c5 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/286200 Commit-Queue: Daniel.L (Byoungchan) Lee Reviewed-by: Mirko Bonadei Cr-Commit-Position: refs/heads/main@{#38806} --- rtc_base/byte_order.h | 44 +++++++++++++++++++++-------- tools_webrtc/ubsan/suppressions.txt | 10 ++++--- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/rtc_base/byte_order.h b/rtc_base/byte_order.h index ae1c6345ba..b8f8ae9f7a 100644 --- a/rtc_base/byte_order.h +++ b/rtc_base/byte_order.h @@ -13,6 +13,8 @@ #include +#include + #if defined(WEBRTC_POSIX) && !defined(__native_client__) #include #endif @@ -107,51 +109,69 @@ inline uint8_t Get8(const void* memory, size_t offset) { } inline void SetBE16(void* memory, uint16_t v) { - *static_cast(memory) = htobe16(v); + uint16_t val = htobe16(v); + memcpy(memory, &val, sizeof(val)); } inline void SetBE32(void* memory, uint32_t v) { - *static_cast(memory) = htobe32(v); + uint32_t val = htobe32(v); + memcpy(memory, &val, sizeof(val)); } inline void SetBE64(void* memory, uint64_t v) { - *static_cast(memory) = htobe64(v); + uint64_t val = htobe64(v); + memcpy(memory, &val, sizeof(val)); } inline uint16_t GetBE16(const void* memory) { - return be16toh(*static_cast(memory)); + uint16_t val; + memcpy(&val, memory, sizeof(val)); + return be16toh(val); } inline uint32_t GetBE32(const void* memory) { - return be32toh(*static_cast(memory)); + uint32_t val; + memcpy(&val, memory, sizeof(val)); + return be32toh(val); } inline uint64_t GetBE64(const void* memory) { - return be64toh(*static_cast(memory)); + uint64_t val; + memcpy(&val, memory, sizeof(val)); + return be64toh(val); } inline void SetLE16(void* memory, uint16_t v) { - *static_cast(memory) = htole16(v); + uint16_t val = htole16(v); + memcpy(memory, &val, sizeof(val)); } inline void SetLE32(void* memory, uint32_t v) { - *static_cast(memory) = htole32(v); + uint32_t val = htole32(v); + memcpy(memory, &val, sizeof(val)); } inline void SetLE64(void* memory, uint64_t v) { - *static_cast(memory) = htole64(v); + uint64_t val = htole64(v); + memcpy(memory, &val, sizeof(val)); } inline uint16_t GetLE16(const void* memory) { - return le16toh(*static_cast(memory)); + uint16_t val; + memcpy(&val, memory, sizeof(val)); + return le16toh(val); } inline uint32_t GetLE32(const void* memory) { - return le32toh(*static_cast(memory)); + uint32_t val; + memcpy(&val, memory, sizeof(val)); + return le32toh(val); } inline uint64_t GetLE64(const void* memory) { - return le64toh(*static_cast(memory)); + uint64_t val; + memcpy(&val, memory, sizeof(val)); + return le64toh(val); } // Check if the current host is big endian. diff --git a/tools_webrtc/ubsan/suppressions.txt b/tools_webrtc/ubsan/suppressions.txt index dc76f38c20..2ece795570 100644 --- a/tools_webrtc/ubsan/suppressions.txt +++ b/tools_webrtc/ubsan/suppressions.txt @@ -6,10 +6,6 @@ # the RTC_NO_SANITIZE macro. Please think twice before adding new exceptions. ############################################################################# -# YASM does some funny things that UBsan doesn't like. -# https://crbug.com/489901 -src:*/third_party/yasm/* - # OpenH264 triggers some errors that are out of our control. src:*/third_party/ffmpeg/libavcodec/* src:*/third_party/openh264/* @@ -22,3 +18,9 @@ src:*/third_party/libvpx/source/libvpx/vp8/* ############################################################################# # Ignore system libraries. src:*/usr/* + +############################################################################# +[alignment] +# Libaom and libsrtp are doing unaligned memory access. +src:*/third_party/libaom/source/libaom/* +src:*/third_party/libsrtp/srtp/srtp.c