blob: 1b94e1edf47d0ab797ee01034c5d88638c3b2b0d [file] [log] [blame]
kwibergac554ee2016-09-02 00:39:33 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_SANITIZER_H_
12#define RTC_BASE_SANITIZER_H_
kwibergac554ee2016-09-02 00:39:33 -070013
oprypin8ad0e582017-09-05 03:00:37 -070014#include <stddef.h> // for size_t
15
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#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
kwibergac554ee2016-09-02 00:39:33 -070030
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031#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.
50static 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.
61static 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.
71static 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).
82static 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
92namespace rtc {
93
94template <typename T>
95inline void AsanPoison(const T& mem) {
96 rtc_AsanPoison(mem.data(), sizeof(mem.data()[0]), mem.size());
97}
98
99template <typename T>
100inline void AsanUnpoison(const T& mem) {
101 rtc_AsanUnpoison(mem.data(), sizeof(mem.data()[0]), mem.size());
102}
103
104template <typename T>
105inline void MsanMarkUninitialized(const T& mem) {
106 rtc_MsanMarkUninitialized(mem.data(), sizeof(mem.data()[0]), mem.size());
107}
108
109template <typename T>
110inline 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
kwibergac554ee2016-09-02 00:39:33 -0700117
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200118#endif // RTC_BASE_SANITIZER_H_