kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_SANITIZER_H_ |
| 12 | #define RTC_BASE_SANITIZER_H_ |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 13 | |
oprypin | 8ad0e58 | 2017-09-05 03:00:37 -0700 | [diff] [blame] | 14 | #include <stddef.h> // for size_t |
| 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #if defined(__has_feature) |
| 17 | #if __has_feature(address_sanitizer) |
| 18 | #define RTC_HAS_ASAN 1 |
| 19 | #endif |
| 20 | #if __has_feature(memory_sanitizer) |
| 21 | #define RTC_HAS_MSAN 1 |
| 22 | #endif |
| 23 | #endif |
| 24 | #ifndef RTC_HAS_ASAN |
| 25 | #define RTC_HAS_ASAN 0 |
| 26 | #endif |
| 27 | #ifndef RTC_HAS_MSAN |
| 28 | #define RTC_HAS_MSAN 0 |
| 29 | #endif |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 30 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 31 | #if RTC_HAS_ASAN |
| 32 | #include <sanitizer/asan_interface.h> |
| 33 | #endif |
| 34 | #if RTC_HAS_MSAN |
| 35 | #include <sanitizer/msan_interface.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifdef __has_attribute |
| 39 | #if __has_attribute(no_sanitize) |
| 40 | #define RTC_NO_SANITIZE(what) __attribute__((no_sanitize(what))) |
| 41 | #endif |
| 42 | #endif |
| 43 | #ifndef RTC_NO_SANITIZE |
| 44 | #define RTC_NO_SANITIZE(what) |
| 45 | #endif |
| 46 | |
| 47 | // Ask ASan to mark the memory range [ptr, ptr + element_size * num_elements) |
| 48 | // as being unaddressable, so that reads and writes are not allowed. ASan may |
| 49 | // narrow the range to the nearest alignment boundaries. |
| 50 | static inline void rtc_AsanPoison(const volatile void* ptr, |
| 51 | size_t element_size, |
| 52 | size_t num_elements) { |
| 53 | #if RTC_HAS_ASAN |
| 54 | ASAN_POISON_MEMORY_REGION(ptr, element_size * num_elements); |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | // Ask ASan to mark the memory range [ptr, ptr + element_size * num_elements) |
| 59 | // as being addressable, so that reads and writes are allowed. ASan may widen |
| 60 | // the range to the nearest alignment boundaries. |
| 61 | static inline void rtc_AsanUnpoison(const volatile void* ptr, |
| 62 | size_t element_size, |
| 63 | size_t num_elements) { |
| 64 | #if RTC_HAS_ASAN |
| 65 | ASAN_UNPOISON_MEMORY_REGION(ptr, element_size * num_elements); |
| 66 | #endif |
| 67 | } |
| 68 | |
| 69 | // Ask MSan to mark the memory range [ptr, ptr + element_size * num_elements) |
| 70 | // as being uninitialized. |
| 71 | static inline void rtc_MsanMarkUninitialized(const volatile void* ptr, |
| 72 | size_t element_size, |
| 73 | size_t num_elements) { |
| 74 | #if RTC_HAS_MSAN |
| 75 | __msan_poison(ptr, element_size * num_elements); |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | // Force an MSan check (if any bits in the memory range [ptr, ptr + |
| 80 | // element_size * num_elements) are uninitialized the call will crash with an |
| 81 | // MSan report). |
| 82 | static inline void rtc_MsanCheckInitialized(const volatile void* ptr, |
| 83 | size_t element_size, |
| 84 | size_t num_elements) { |
| 85 | #if RTC_HAS_MSAN |
| 86 | __msan_check_mem_is_initialized(ptr, element_size * num_elements); |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | #ifdef __cplusplus |
| 91 | |
| 92 | namespace rtc { |
| 93 | |
| 94 | template <typename T> |
| 95 | inline void AsanPoison(const T& mem) { |
| 96 | rtc_AsanPoison(mem.data(), sizeof(mem.data()[0]), mem.size()); |
| 97 | } |
| 98 | |
| 99 | template <typename T> |
| 100 | inline void AsanUnpoison(const T& mem) { |
| 101 | rtc_AsanUnpoison(mem.data(), sizeof(mem.data()[0]), mem.size()); |
| 102 | } |
| 103 | |
| 104 | template <typename T> |
| 105 | inline void MsanMarkUninitialized(const T& mem) { |
| 106 | rtc_MsanMarkUninitialized(mem.data(), sizeof(mem.data()[0]), mem.size()); |
| 107 | } |
| 108 | |
| 109 | template <typename T> |
| 110 | inline void MsanCheckInitialized(const T& mem) { |
| 111 | rtc_MsanCheckInitialized(mem.data(), sizeof(mem.data()[0]), mem.size()); |
| 112 | } |
| 113 | |
| 114 | } // namespace rtc |
| 115 | |
| 116 | #endif // __cplusplus |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 117 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 118 | #endif // RTC_BASE_SANITIZER_H_ |