Skip to content

Commit fee4836

Browse files
authored
[compiler-rt][rtsan] NFC: Introduce __rtsan_expect_not_realtime helper (#106314)
We are extracting this function into the C API so we can eventually install it when a user marks a function [[clang::blocking]].
1 parent 140381d commit fee4836

File tree

3 files changed

+62
-59
lines changed

3 files changed

+62
-59
lines changed

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,11 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_on() {
6666
__rtsan::GetContextForThisThread().BypassPop();
6767
}
6868

69+
SANITIZER_INTERFACE_ATTRIBUTE void
70+
__rtsan_expect_not_realtime(const char *intercepted_function_name) {
71+
__rtsan_ensure_initialized();
72+
__rtsan::GetContextForThisThread().ExpectNotRealtime(
73+
intercepted_function_name);
74+
}
75+
6976
} // extern "C"

compiler-rt/lib/rtsan/rtsan.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_off();
4444
// The counterpart to `__rtsan_off`.
4545
SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_on();
4646

47+
SANITIZER_INTERFACE_ATTRIBUTE void
48+
__rtsan_expect_not_realtime(const char *intercepted_function_name);
49+
4750
} // extern "C"

compiler-rt/lib/rtsan/rtsan_interceptors.cpp

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,12 @@ struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
5757
};
5858
} // namespace
5959

60-
void ExpectNotRealtime(const char *intercepted_function_name) {
61-
__rtsan_ensure_initialized();
62-
63-
__rtsan::GetContextForThisThread().ExpectNotRealtime(
64-
intercepted_function_name);
65-
}
66-
6760
// Filesystem
6861

6962
INTERCEPTOR(int, open, const char *path, int oflag, ...) {
7063
// TODO Establish whether we should intercept here if the flag contains
7164
// O_NONBLOCK
72-
ExpectNotRealtime("open");
65+
__rtsan_expect_not_realtime("open");
7366

7467
va_list args;
7568
va_start(args, oflag);
@@ -83,7 +76,7 @@ INTERCEPTOR(int, open, const char *path, int oflag, ...) {
8376
INTERCEPTOR(int, openat, int fd, const char *path, int oflag, ...) {
8477
// TODO Establish whether we should intercept here if the flag contains
8578
// O_NONBLOCK
86-
ExpectNotRealtime("openat");
79+
__rtsan_expect_not_realtime("openat");
8780

8881
va_list args;
8982
va_start(args, oflag);
@@ -97,13 +90,13 @@ INTERCEPTOR(int, openat, int fd, const char *path, int oflag, ...) {
9790
INTERCEPTOR(int, creat, const char *path, mode_t mode) {
9891
// TODO Establish whether we should intercept here if the flag contains
9992
// O_NONBLOCK
100-
ExpectNotRealtime("creat");
93+
__rtsan_expect_not_realtime("creat");
10194
const int result = REAL(creat)(path, mode);
10295
return result;
10396
}
10497

10598
INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {
106-
ExpectNotRealtime("fcntl");
99+
__rtsan_expect_not_realtime("fcntl");
107100

108101
va_list args;
109102
va_start(args, cmd);
@@ -124,71 +117,71 @@ INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {
124117
}
125118

126119
INTERCEPTOR(int, close, int filedes) {
127-
ExpectNotRealtime("close");
120+
__rtsan_expect_not_realtime("close");
128121
return REAL(close)(filedes);
129122
}
130123

131124
INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {
132-
ExpectNotRealtime("fopen");
125+
__rtsan_expect_not_realtime("fopen");
133126
return REAL(fopen)(path, mode);
134127
}
135128

136129
INTERCEPTOR(size_t, fread, void *ptr, size_t size, size_t nitems,
137130
FILE *stream) {
138-
ExpectNotRealtime("fread");
131+
__rtsan_expect_not_realtime("fread");
139132
return REAL(fread)(ptr, size, nitems, stream);
140133
}
141134

142135
INTERCEPTOR(size_t, fwrite, const void *ptr, size_t size, size_t nitems,
143136
FILE *stream) {
144-
ExpectNotRealtime("fwrite");
137+
__rtsan_expect_not_realtime("fwrite");
145138
return REAL(fwrite)(ptr, size, nitems, stream);
146139
}
147140

148141
INTERCEPTOR(int, fclose, FILE *stream) {
149-
ExpectNotRealtime("fclose");
142+
__rtsan_expect_not_realtime("fclose");
150143
return REAL(fclose)(stream);
151144
}
152145

153146
INTERCEPTOR(int, fputs, const char *s, FILE *stream) {
154-
ExpectNotRealtime("fputs");
147+
__rtsan_expect_not_realtime("fputs");
155148
return REAL(fputs)(s, stream);
156149
}
157150

158151
// Streams
159152
INTERCEPTOR(int, puts, const char *s) {
160-
ExpectNotRealtime("puts");
153+
__rtsan_expect_not_realtime("puts");
161154
return REAL(puts)(s);
162155
}
163156

164157
INTERCEPTOR(ssize_t, read, int fd, void *buf, size_t count) {
165-
ExpectNotRealtime("read");
158+
__rtsan_expect_not_realtime("read");
166159
return REAL(read)(fd, buf, count);
167160
}
168161

169162
INTERCEPTOR(ssize_t, write, int fd, const void *buf, size_t count) {
170-
ExpectNotRealtime("write");
163+
__rtsan_expect_not_realtime("write");
171164
return REAL(write)(fd, buf, count);
172165
}
173166

174167
INTERCEPTOR(ssize_t, pread, int fd, void *buf, size_t count, off_t offset) {
175-
ExpectNotRealtime("pread");
168+
__rtsan_expect_not_realtime("pread");
176169
return REAL(pread)(fd, buf, count, offset);
177170
}
178171

179172
INTERCEPTOR(ssize_t, readv, int fd, const struct iovec *iov, int iovcnt) {
180-
ExpectNotRealtime("readv");
173+
__rtsan_expect_not_realtime("readv");
181174
return REAL(readv)(fd, iov, iovcnt);
182175
}
183176

184177
INTERCEPTOR(ssize_t, pwrite, int fd, const void *buf, size_t count,
185178
off_t offset) {
186-
ExpectNotRealtime("pwrite");
179+
__rtsan_expect_not_realtime("pwrite");
187180
return REAL(pwrite)(fd, buf, count, offset);
188181
}
189182

190183
INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) {
191-
ExpectNotRealtime("writev");
184+
__rtsan_expect_not_realtime("writev");
192185
return REAL(writev)(fd, iov, iovcnt);
193186
}
194187

@@ -198,95 +191,95 @@ INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) {
198191
// OSSpinLockLock is deprecated, but still in use in libc++
199192
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
200193
INTERCEPTOR(void, OSSpinLockLock, volatile OSSpinLock *lock) {
201-
ExpectNotRealtime("OSSpinLockLock");
194+
__rtsan_expect_not_realtime("OSSpinLockLock");
202195
return REAL(OSSpinLockLock)(lock);
203196
}
204197
#pragma clang diagnostic pop
205198

206199
INTERCEPTOR(void, os_unfair_lock_lock, os_unfair_lock_t lock) {
207-
ExpectNotRealtime("os_unfair_lock_lock");
200+
__rtsan_expect_not_realtime("os_unfair_lock_lock");
208201
return REAL(os_unfair_lock_lock)(lock);
209202
}
210203
#elif SANITIZER_LINUX
211204
INTERCEPTOR(int, pthread_spin_lock, pthread_spinlock_t *spinlock) {
212-
ExpectNotRealtime("pthread_spin_lock");
205+
__rtsan_expect_not_realtime("pthread_spin_lock");
213206
return REAL(pthread_spin_lock)(spinlock);
214207
}
215208
#endif
216209

217210
INTERCEPTOR(int, pthread_create, pthread_t *thread, const pthread_attr_t *attr,
218211
void *(*start_routine)(void *), void *arg) {
219-
ExpectNotRealtime("pthread_create");
212+
__rtsan_expect_not_realtime("pthread_create");
220213
return REAL(pthread_create)(thread, attr, start_routine, arg);
221214
}
222215

223216
INTERCEPTOR(int, pthread_mutex_lock, pthread_mutex_t *mutex) {
224-
ExpectNotRealtime("pthread_mutex_lock");
217+
__rtsan_expect_not_realtime("pthread_mutex_lock");
225218
return REAL(pthread_mutex_lock)(mutex);
226219
}
227220

228221
INTERCEPTOR(int, pthread_mutex_unlock, pthread_mutex_t *mutex) {
229-
ExpectNotRealtime("pthread_mutex_unlock");
222+
__rtsan_expect_not_realtime("pthread_mutex_unlock");
230223
return REAL(pthread_mutex_unlock)(mutex);
231224
}
232225

233226
INTERCEPTOR(int, pthread_join, pthread_t thread, void **value_ptr) {
234-
ExpectNotRealtime("pthread_join");
227+
__rtsan_expect_not_realtime("pthread_join");
235228
return REAL(pthread_join)(thread, value_ptr);
236229
}
237230

238231
INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *cond) {
239-
ExpectNotRealtime("pthread_cond_signal");
232+
__rtsan_expect_not_realtime("pthread_cond_signal");
240233
return REAL(pthread_cond_signal)(cond);
241234
}
242235

243236
INTERCEPTOR(int, pthread_cond_broadcast, pthread_cond_t *cond) {
244-
ExpectNotRealtime("pthread_cond_broadcast");
237+
__rtsan_expect_not_realtime("pthread_cond_broadcast");
245238
return REAL(pthread_cond_broadcast)(cond);
246239
}
247240

248241
INTERCEPTOR(int, pthread_cond_wait, pthread_cond_t *cond,
249242
pthread_mutex_t *mutex) {
250-
ExpectNotRealtime("pthread_cond_wait");
243+
__rtsan_expect_not_realtime("pthread_cond_wait");
251244
return REAL(pthread_cond_wait)(cond, mutex);
252245
}
253246

254247
INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *cond,
255248
pthread_mutex_t *mutex, const timespec *ts) {
256-
ExpectNotRealtime("pthread_cond_timedwait");
249+
__rtsan_expect_not_realtime("pthread_cond_timedwait");
257250
return REAL(pthread_cond_timedwait)(cond, mutex, ts);
258251
}
259252

260253
INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *lock) {
261-
ExpectNotRealtime("pthread_rwlock_rdlock");
254+
__rtsan_expect_not_realtime("pthread_rwlock_rdlock");
262255
return REAL(pthread_rwlock_rdlock)(lock);
263256
}
264257

265258
INTERCEPTOR(int, pthread_rwlock_unlock, pthread_rwlock_t *lock) {
266-
ExpectNotRealtime("pthread_rwlock_unlock");
259+
__rtsan_expect_not_realtime("pthread_rwlock_unlock");
267260
return REAL(pthread_rwlock_unlock)(lock);
268261
}
269262

270263
INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *lock) {
271-
ExpectNotRealtime("pthread_rwlock_wrlock");
264+
__rtsan_expect_not_realtime("pthread_rwlock_wrlock");
272265
return REAL(pthread_rwlock_wrlock)(lock);
273266
}
274267

275268
// Sleeping
276269

277270
INTERCEPTOR(unsigned int, sleep, unsigned int s) {
278-
ExpectNotRealtime("sleep");
271+
__rtsan_expect_not_realtime("sleep");
279272
return REAL(sleep)(s);
280273
}
281274

282275
INTERCEPTOR(int, usleep, useconds_t u) {
283-
ExpectNotRealtime("usleep");
276+
__rtsan_expect_not_realtime("usleep");
284277
return REAL(usleep)(u);
285278
}
286279

287280
INTERCEPTOR(int, nanosleep, const struct timespec *rqtp,
288281
struct timespec *rmtp) {
289-
ExpectNotRealtime("nanosleep");
282+
__rtsan_expect_not_realtime("nanosleep");
290283
return REAL(nanosleep)(rqtp, rmtp);
291284
}
292285

@@ -296,7 +289,7 @@ INTERCEPTOR(void *, calloc, SIZE_T num, SIZE_T size) {
296289
if (DlsymAlloc::Use())
297290
return DlsymAlloc::Callocate(num, size);
298291

299-
ExpectNotRealtime("calloc");
292+
__rtsan_expect_not_realtime("calloc");
300293
return REAL(calloc)(num, size);
301294
}
302295

@@ -305,7 +298,7 @@ INTERCEPTOR(void, free, void *ptr) {
305298
return DlsymAlloc::Free(ptr);
306299

307300
if (ptr != NULL) {
308-
ExpectNotRealtime("free");
301+
__rtsan_expect_not_realtime("free");
309302
}
310303
return REAL(free)(ptr);
311304
}
@@ -314,31 +307,31 @@ INTERCEPTOR(void *, malloc, SIZE_T size) {
314307
if (DlsymAlloc::Use())
315308
return DlsymAlloc::Allocate(size);
316309

317-
ExpectNotRealtime("malloc");
310+
__rtsan_expect_not_realtime("malloc");
318311
return REAL(malloc)(size);
319312
}
320313

321314
INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
322315
if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
323316
return DlsymAlloc::Realloc(ptr, size);
324317

325-
ExpectNotRealtime("realloc");
318+
__rtsan_expect_not_realtime("realloc");
326319
return REAL(realloc)(ptr, size);
327320
}
328321

329322
INTERCEPTOR(void *, reallocf, void *ptr, SIZE_T size) {
330-
ExpectNotRealtime("reallocf");
323+
__rtsan_expect_not_realtime("reallocf");
331324
return REAL(reallocf)(ptr, size);
332325
}
333326

334327
INTERCEPTOR(void *, valloc, SIZE_T size) {
335-
ExpectNotRealtime("valloc");
328+
__rtsan_expect_not_realtime("valloc");
336329
return REAL(valloc)(size);
337330
}
338331

339332
#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
340333
INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
341-
ExpectNotRealtime("aligned_alloc");
334+
__rtsan_expect_not_realtime("aligned_alloc");
342335
return REAL(aligned_alloc)(alignment, size);
343336
}
344337
#define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
@@ -347,65 +340,65 @@ INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
347340
#endif
348341

349342
INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
350-
ExpectNotRealtime("posix_memalign");
343+
__rtsan_expect_not_realtime("posix_memalign");
351344
return REAL(posix_memalign)(memptr, alignment, size);
352345
}
353346

354347
#if SANITIZER_INTERCEPT_MEMALIGN
355348
INTERCEPTOR(void *, memalign, size_t alignment, size_t size) {
356-
ExpectNotRealtime("memalign");
349+
__rtsan_expect_not_realtime("memalign");
357350
return REAL(memalign)(alignment, size);
358351
}
359352
#endif
360353

361354
#if SANITIZER_INTERCEPT_PVALLOC
362355
INTERCEPTOR(void *, pvalloc, size_t size) {
363-
ExpectNotRealtime("pvalloc");
356+
__rtsan_expect_not_realtime("pvalloc");
364357
return REAL(pvalloc)(size);
365358
}
366359
#endif
367360

368361
// Sockets
369362
INTERCEPTOR(int, socket, int domain, int type, int protocol) {
370-
ExpectNotRealtime("socket");
363+
__rtsan_expect_not_realtime("socket");
371364
return REAL(socket)(domain, type, protocol);
372365
}
373366

374367
INTERCEPTOR(ssize_t, send, int sockfd, const void *buf, size_t len, int flags) {
375-
ExpectNotRealtime("send");
368+
__rtsan_expect_not_realtime("send");
376369
return REAL(send)(sockfd, buf, len, flags);
377370
}
378371

379372
INTERCEPTOR(ssize_t, sendmsg, int socket, const struct msghdr *message,
380373
int flags) {
381-
ExpectNotRealtime("sendmsg");
374+
__rtsan_expect_not_realtime("sendmsg");
382375
return REAL(sendmsg)(socket, message, flags);
383376
}
384377

385378
INTERCEPTOR(ssize_t, sendto, int socket, const void *buffer, size_t length,
386379
int flags, const struct sockaddr *dest_addr, socklen_t dest_len) {
387-
ExpectNotRealtime("sendto");
380+
__rtsan_expect_not_realtime("sendto");
388381
return REAL(sendto)(socket, buffer, length, flags, dest_addr, dest_len);
389382
}
390383

391384
INTERCEPTOR(ssize_t, recv, int socket, void *buffer, size_t length, int flags) {
392-
ExpectNotRealtime("recv");
385+
__rtsan_expect_not_realtime("recv");
393386
return REAL(recv)(socket, buffer, length, flags);
394387
}
395388

396389
INTERCEPTOR(ssize_t, recvfrom, int socket, void *buffer, size_t length,
397390
int flags, struct sockaddr *address, socklen_t *address_len) {
398-
ExpectNotRealtime("recvfrom");
391+
__rtsan_expect_not_realtime("recvfrom");
399392
return REAL(recvfrom)(socket, buffer, length, flags, address, address_len);
400393
}
401394

402395
INTERCEPTOR(ssize_t, recvmsg, int socket, struct msghdr *message, int flags) {
403-
ExpectNotRealtime("recvmsg");
396+
__rtsan_expect_not_realtime("recvmsg");
404397
return REAL(recvmsg)(socket, message, flags);
405398
}
406399

407400
INTERCEPTOR(int, shutdown, int socket, int how) {
408-
ExpectNotRealtime("shutdown");
401+
__rtsan_expect_not_realtime("shutdown");
409402
return REAL(shutdown)(socket, how);
410403
}
411404

0 commit comments

Comments
 (0)