1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#pragma once
#include "defaults.h"
extern "C" { // sanitizers API
#if defined(_asan_enabled_) || defined(_lsan_enabled_)
void __lsan_ignore_object(const void* p);
#endif
#if defined(_asan_enabled_)
void __sanitizer_start_switch_fiber(void** fake_stack_save, const void* bottom, size_t size);
void __sanitizer_finish_switch_fiber(void* fake_stack_save, const void** old_bottom, size_t* old_size);
#endif
#if defined(_msan_enabled_)
void __msan_unpoison(const volatile void* a, size_t size);
void __msan_poison(const volatile void* a, size_t size);
void __msan_check_mem_is_initialized(const volatile void* x, size_t size);
#endif
#if defined(_tsan_enabled_)
void __tsan_acquire(void* a);
void __tsan_release(void* a);
void* __tsan_get_current_fiber(void);
void __tsan_destroy_fiber(void* fiber);
void __tsan_switch_to_fiber(void* fiber, unsigned flags);
#endif
} // sanitizers API
namespace NSan {
class TFiberContext {
public:
TFiberContext() noexcept;
TFiberContext(const void* stack, size_t len, const char* contName) noexcept;
~TFiberContext() noexcept {
if (!IsMainFiber_) {
#if defined(_asan_enabled_)
if (Token_) {
// destroy saved FakeStack
void* activeFakeStack = nullptr;
const void* activeStack = nullptr;
size_t activeStackSize = 0;
__sanitizer_start_switch_fiber(&activeFakeStack, (char*)Stack_, Len_);
__sanitizer_finish_switch_fiber(Token_, &activeStack, &activeStackSize);
__sanitizer_start_switch_fiber(nullptr, activeStack, activeStackSize);
__sanitizer_finish_switch_fiber(activeFakeStack, nullptr, nullptr);
}
#endif
#if defined(_tsan_enabled_)
__tsan_destroy_fiber(CurrentTSanFiberContext_);
#endif
}
}
Y_FORCE_INLINE void BeforeSwitch(TFiberContext* old) noexcept {
#if defined(_asan_enabled_)
__sanitizer_start_switch_fiber(old ? &old->Token_ : nullptr, (char*)Stack_, Len_);
#else
(void)old;
#endif
#if defined(_tsan_enabled_)
__tsan_switch_to_fiber(CurrentTSanFiberContext_, /*flags =*/0);
#endif
}
void AfterSwitch() noexcept {
#if defined(_asan_enabled_)
__sanitizer_finish_switch_fiber(Token_, nullptr, nullptr);
#endif
}
private:
void* Token_;
const void* Stack_;
size_t Len_;
const bool IsMainFiber_;
#if defined(_tsan_enabled_)
void* const CurrentTSanFiberContext_;
#endif
};
// Returns plain if no sanitizer enabled or sanitized otherwise
// Ment to be used in test code for constants (timeouts, etc)
template <typename T>
inline constexpr static T PlainOrUnderSanitizer(T plain, T sanitized) noexcept {
#if defined(_tsan_enabled_) || defined(_msan_enabled_) || defined(_asan_enabled_)
Y_UNUSED(plain);
return sanitized;
#else
Y_UNUSED(sanitized);
return plain;
#endif
}
// Determines if asan present
inline constexpr static bool ASanIsOn() noexcept {
#if defined(_asan_enabled_)
return true;
#else
return false;
#endif
}
// Determines if tsan present
inline constexpr static bool TSanIsOn() noexcept {
#if defined(_tsan_enabled_)
return true;
#else
return false;
#endif
}
// Determines if msan present
inline constexpr static bool MSanIsOn() noexcept {
#if defined(_msan_enabled_)
return true;
#else
return false;
#endif
}
// Make memory region fully initialized (without changing its contents).
inline static void Unpoison(const volatile void* a, size_t size) noexcept {
#if defined(_msan_enabled_)
__msan_unpoison(a, size);
#else
Y_UNUSED(a);
Y_UNUSED(size);
#endif
}
// Make memory region fully uninitialized (without changing its contents).
// This is a legacy interface that does not update origin information. Use __msan_allocated_memory() instead.
inline static void Poison(const volatile void* a, size_t size) noexcept {
#if defined(_msan_enabled_)
__msan_poison(a, size);
#else
Y_UNUSED(a);
Y_UNUSED(size);
#endif
}
// Checks that memory range is fully initialized, and reports an error if it is not.
inline static void CheckMemIsInitialized(const volatile void* a, size_t size) noexcept {
#if defined(_msan_enabled_)
__msan_check_mem_is_initialized(a, size);
#else
Y_UNUSED(a);
Y_UNUSED(size);
#endif
}
inline static void MarkAsIntentionallyLeaked(const void* ptr) noexcept {
#if defined(_asan_enabled_) || defined(_lsan_enabled_)
__lsan_ignore_object(ptr);
#else
Y_UNUSED(ptr);
#endif
}
#if defined(_tsan_enabled_)
// defined in .cpp to avoid exposing problematic C-linkage version of AnnotateBenignRaceSized(...)
void AnnotateBenignRaceSized(const char* file, int line,
const volatile void* address,
size_t size,
const char* description) noexcept;
#else
inline static void AnnotateBenignRaceSized(const char* file, int line,
const volatile void* address,
size_t size,
const char* description) noexcept {
Y_UNUSED(file);
Y_UNUSED(line);
Y_UNUSED(address);
Y_UNUSED(size);
Y_UNUSED(description);
}
#endif
inline static void Acquire(void* a) {
#if defined(_tsan_enabled_)
__tsan_acquire(a);
#else
Y_UNUSED(a);
#endif
}
inline static void Release(void* a) {
#if defined(_tsan_enabled_)
__tsan_release(a);
#else
Y_UNUSED(a);
#endif
}
} // namespace NSan
|