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 <daniel.l@hpcnt.com>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38806}
This commit is contained in:
Byoungchan Lee 2022-12-05 08:52:39 +09:00 committed by WebRTC LUCI CQ
parent 9f3114dec9
commit 1b80be352b
2 changed files with 38 additions and 16 deletions

View File

@ -13,6 +13,8 @@
#include <stdint.h> #include <stdint.h>
#include <cstring>
#if defined(WEBRTC_POSIX) && !defined(__native_client__) #if defined(WEBRTC_POSIX) && !defined(__native_client__)
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -107,51 +109,69 @@ inline uint8_t Get8(const void* memory, size_t offset) {
} }
inline void SetBE16(void* memory, uint16_t v) { inline void SetBE16(void* memory, uint16_t v) {
*static_cast<uint16_t*>(memory) = htobe16(v); uint16_t val = htobe16(v);
memcpy(memory, &val, sizeof(val));
} }
inline void SetBE32(void* memory, uint32_t v) { inline void SetBE32(void* memory, uint32_t v) {
*static_cast<uint32_t*>(memory) = htobe32(v); uint32_t val = htobe32(v);
memcpy(memory, &val, sizeof(val));
} }
inline void SetBE64(void* memory, uint64_t v) { inline void SetBE64(void* memory, uint64_t v) {
*static_cast<uint64_t*>(memory) = htobe64(v); uint64_t val = htobe64(v);
memcpy(memory, &val, sizeof(val));
} }
inline uint16_t GetBE16(const void* memory) { inline uint16_t GetBE16(const void* memory) {
return be16toh(*static_cast<const uint16_t*>(memory)); uint16_t val;
memcpy(&val, memory, sizeof(val));
return be16toh(val);
} }
inline uint32_t GetBE32(const void* memory) { inline uint32_t GetBE32(const void* memory) {
return be32toh(*static_cast<const uint32_t*>(memory)); uint32_t val;
memcpy(&val, memory, sizeof(val));
return be32toh(val);
} }
inline uint64_t GetBE64(const void* memory) { inline uint64_t GetBE64(const void* memory) {
return be64toh(*static_cast<const uint64_t*>(memory)); uint64_t val;
memcpy(&val, memory, sizeof(val));
return be64toh(val);
} }
inline void SetLE16(void* memory, uint16_t v) { inline void SetLE16(void* memory, uint16_t v) {
*static_cast<uint16_t*>(memory) = htole16(v); uint16_t val = htole16(v);
memcpy(memory, &val, sizeof(val));
} }
inline void SetLE32(void* memory, uint32_t v) { inline void SetLE32(void* memory, uint32_t v) {
*static_cast<uint32_t*>(memory) = htole32(v); uint32_t val = htole32(v);
memcpy(memory, &val, sizeof(val));
} }
inline void SetLE64(void* memory, uint64_t v) { inline void SetLE64(void* memory, uint64_t v) {
*static_cast<uint64_t*>(memory) = htole64(v); uint64_t val = htole64(v);
memcpy(memory, &val, sizeof(val));
} }
inline uint16_t GetLE16(const void* memory) { inline uint16_t GetLE16(const void* memory) {
return le16toh(*static_cast<const uint16_t*>(memory)); uint16_t val;
memcpy(&val, memory, sizeof(val));
return le16toh(val);
} }
inline uint32_t GetLE32(const void* memory) { inline uint32_t GetLE32(const void* memory) {
return le32toh(*static_cast<const uint32_t*>(memory)); uint32_t val;
memcpy(&val, memory, sizeof(val));
return le32toh(val);
} }
inline uint64_t GetLE64(const void* memory) { inline uint64_t GetLE64(const void* memory) {
return le64toh(*static_cast<const uint64_t*>(memory)); uint64_t val;
memcpy(&val, memory, sizeof(val));
return le64toh(val);
} }
// Check if the current host is big endian. // Check if the current host is big endian.

View File

@ -6,10 +6,6 @@
# the RTC_NO_SANITIZE macro. Please think twice before adding new exceptions. # 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. # OpenH264 triggers some errors that are out of our control.
src:*/third_party/ffmpeg/libavcodec/* src:*/third_party/ffmpeg/libavcodec/*
src:*/third_party/openh264/* src:*/third_party/openh264/*
@ -22,3 +18,9 @@ src:*/third_party/libvpx/source/libvpx/vp8/*
############################################################################# #############################################################################
# Ignore system libraries. # Ignore system libraries.
src:*/usr/* src:*/usr/*
#############################################################################
[alignment]
# Libaom and libsrtp are doing unaligned memory access.
src:*/third_party/libaom/source/libaom/*
src:*/third_party/libsrtp/srtp/srtp.c