]> git.ipfire.org Git - thirdparty/gcc.git/blob - libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc
libsanitizer merge from upstream r196489
[thirdparty/gcc.git] / libsanitizer / sanitizer_common / sanitizer_common_interceptors.inc
1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Common function interceptors for tools like AddressSanitizer,
9 // ThreadSanitizer, MemorySanitizer, etc.
10 //
11 // This file should be included into the tool's interceptor file,
12 // which has to define it's own macros:
13 // COMMON_INTERCEPTOR_ENTER
14 // COMMON_INTERCEPTOR_READ_RANGE
15 // COMMON_INTERCEPTOR_WRITE_RANGE
16 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
17 // COMMON_INTERCEPTOR_FD_ACQUIRE
18 // COMMON_INTERCEPTOR_FD_RELEASE
19 // COMMON_INTERCEPTOR_FD_ACCESS
20 // COMMON_INTERCEPTOR_SET_THREAD_NAME
21 // COMMON_INTERCEPTOR_ON_EXIT
22 // COMMON_INTERCEPTOR_MUTEX_LOCK
23 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
24 // COMMON_INTERCEPTOR_MUTEX_REPAIR
25 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
26 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
27 //===----------------------------------------------------------------------===//
28 #include "interception/interception.h"
29 #include "sanitizer_platform_interceptors.h"
30
31 #include <stdarg.h>
32
33 #if SANITIZER_WINDOWS && !defined(va_copy)
34 #define va_copy(dst, src) ((dst) = (src))
35 #endif // _WIN32
36
37 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
38 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, p, size) {}
39 #endif
40
41 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
42 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
43 #endif
44
45 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
46 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
47 #endif
48
49 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
50 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
51 #endif
52
53 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
54 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
55 #endif
56
57 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
58 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
59 #endif
60
61 #if SANITIZER_INTERCEPT_TEXTDOMAIN
62 INTERCEPTOR(char*, textdomain, const char *domainname) {
63 void *ctx;
64 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
65 char* domain = REAL(textdomain)(domainname);
66 if (domain) {
67 COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, domain,
68 REAL(strlen)(domain) + 1);
69 }
70 return domain;
71 }
72 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
73 #else
74 #define INIT_TEXTDOMAIN
75 #endif
76
77 #if SANITIZER_INTERCEPT_STRCMP
78 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
79 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
80 }
81
82 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
83 void *ctx;
84 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
85 unsigned char c1, c2;
86 uptr i;
87 for (i = 0;; i++) {
88 c1 = (unsigned char)s1[i];
89 c2 = (unsigned char)s2[i];
90 if (c1 != c2 || c1 == '\0') break;
91 }
92 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
93 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
94 return CharCmpX(c1, c2);
95 }
96
97 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
98 void *ctx;
99 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
100 unsigned char c1 = 0, c2 = 0;
101 uptr i;
102 for (i = 0; i < size; i++) {
103 c1 = (unsigned char)s1[i];
104 c2 = (unsigned char)s2[i];
105 if (c1 != c2 || c1 == '\0') break;
106 }
107 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
108 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
109 return CharCmpX(c1, c2);
110 }
111
112 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
113 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
114 #else
115 #define INIT_STRCMP
116 #define INIT_STRNCMP
117 #endif
118
119 #if SANITIZER_INTERCEPT_STRCASECMP
120 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
121 int c1_low = ToLower(c1);
122 int c2_low = ToLower(c2);
123 return c1_low - c2_low;
124 }
125
126 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
127 void *ctx;
128 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
129 unsigned char c1 = 0, c2 = 0;
130 uptr i;
131 for (i = 0;; i++) {
132 c1 = (unsigned char)s1[i];
133 c2 = (unsigned char)s2[i];
134 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
135 }
136 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
137 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
138 return CharCaseCmp(c1, c2);
139 }
140
141 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {
142 void *ctx;
143 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
144 unsigned char c1 = 0, c2 = 0;
145 uptr i;
146 for (i = 0; i < n; i++) {
147 c1 = (unsigned char)s1[i];
148 c2 = (unsigned char)s2[i];
149 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
150 }
151 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n));
152 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n));
153 return CharCaseCmp(c1, c2);
154 }
155
156 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
157 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
158 #else
159 #define INIT_STRCASECMP
160 #define INIT_STRNCASECMP
161 #endif
162
163 #if SANITIZER_INTERCEPT_FREXP
164 INTERCEPTOR(double, frexp, double x, int *exp) {
165 void *ctx;
166 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
167 double res = REAL(frexp)(x, exp);
168 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
169 return res;
170 }
171
172 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
173 #else
174 #define INIT_FREXP
175 #endif // SANITIZER_INTERCEPT_FREXP
176
177 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
178 INTERCEPTOR(float, frexpf, float x, int *exp) {
179 void *ctx;
180 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
181 float res = REAL(frexpf)(x, exp);
182 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
183 return res;
184 }
185
186 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
187 void *ctx;
188 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
189 long double res = REAL(frexpl)(x, exp);
190 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
191 return res;
192 }
193
194 #define INIT_FREXPF_FREXPL \
195 COMMON_INTERCEPT_FUNCTION(frexpf); \
196 COMMON_INTERCEPT_FUNCTION(frexpl)
197 #else
198 #define INIT_FREXPF_FREXPL
199 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
200
201 #if SI_NOT_WINDOWS
202 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
203 SIZE_T iovlen, SIZE_T maxlen) {
204 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
205 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
206 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
207 maxlen -= sz;
208 }
209 }
210
211 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
212 SIZE_T iovlen, SIZE_T maxlen) {
213 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
214 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
215 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
216 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
217 maxlen -= sz;
218 }
219 }
220 #endif
221
222 #if SANITIZER_INTERCEPT_READ
223 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
224 void *ctx;
225 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
226 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
227 SSIZE_T res = REAL(read)(fd, ptr, count);
228 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
229 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
230 return res;
231 }
232 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
233 #else
234 #define INIT_READ
235 #endif
236
237 #if SANITIZER_INTERCEPT_PREAD
238 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
239 void *ctx;
240 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
241 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
242 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
243 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
244 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
245 return res;
246 }
247 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
248 #else
249 #define INIT_PREAD
250 #endif
251
252 #if SANITIZER_INTERCEPT_PREAD64
253 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
254 void *ctx;
255 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
256 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
257 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
258 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
259 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
260 return res;
261 }
262 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
263 #else
264 #define INIT_PREAD64
265 #endif
266
267 #if SANITIZER_INTERCEPT_READV
268 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
269 int iovcnt) {
270 void *ctx;
271 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
272 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
273 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
274 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
275 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
276 return res;
277 }
278 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
279 #else
280 #define INIT_READV
281 #endif
282
283 #if SANITIZER_INTERCEPT_PREADV
284 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
285 OFF_T offset) {
286 void *ctx;
287 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
288 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
289 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
290 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
291 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
292 return res;
293 }
294 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
295 #else
296 #define INIT_PREADV
297 #endif
298
299 #if SANITIZER_INTERCEPT_PREADV64
300 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
301 OFF64_T offset) {
302 void *ctx;
303 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
304 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
305 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
306 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
307 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
308 return res;
309 }
310 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
311 #else
312 #define INIT_PREADV64
313 #endif
314
315 #if SANITIZER_INTERCEPT_WRITE
316 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
317 void *ctx;
318 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
319 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
320 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
321 SSIZE_T res = REAL(write)(fd, ptr, count);
322 // FIXME: this check should be _before_ the call to REAL(write), not after
323 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
324 return res;
325 }
326 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
327 #else
328 #define INIT_WRITE
329 #endif
330
331 #if SANITIZER_INTERCEPT_PWRITE
332 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
333 void *ctx;
334 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
335 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
336 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
337 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
338 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
339 return res;
340 }
341 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
342 #else
343 #define INIT_PWRITE
344 #endif
345
346 #if SANITIZER_INTERCEPT_PWRITE64
347 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
348 OFF64_T offset) {
349 void *ctx;
350 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
351 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
352 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
353 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
354 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
355 return res;
356 }
357 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
358 #else
359 #define INIT_PWRITE64
360 #endif
361
362 #if SANITIZER_INTERCEPT_WRITEV
363 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
364 int iovcnt) {
365 void *ctx;
366 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
367 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
368 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
369 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
370 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
371 return res;
372 }
373 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
374 #else
375 #define INIT_WRITEV
376 #endif
377
378 #if SANITIZER_INTERCEPT_PWRITEV
379 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
380 OFF_T offset) {
381 void *ctx;
382 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
383 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
384 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
385 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
386 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
387 return res;
388 }
389 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
390 #else
391 #define INIT_PWRITEV
392 #endif
393
394 #if SANITIZER_INTERCEPT_PWRITEV64
395 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
396 OFF64_T offset) {
397 void *ctx;
398 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
399 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
400 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
401 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
402 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
403 return res;
404 }
405 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
406 #else
407 #define INIT_PWRITEV64
408 #endif
409
410 #if SANITIZER_INTERCEPT_PRCTL
411 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
412 unsigned long arg3, // NOLINT
413 unsigned long arg4, unsigned long arg5) { // NOLINT
414 void *ctx;
415 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
416 static const int PR_SET_NAME = 15;
417 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
418 if (option == PR_SET_NAME) {
419 char buff[16];
420 internal_strncpy(buff, (char *)arg2, 15);
421 buff[15] = 0;
422 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
423 }
424 return res;
425 }
426 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
427 #else
428 #define INIT_PRCTL
429 #endif // SANITIZER_INTERCEPT_PRCTL
430
431 #if SANITIZER_INTERCEPT_TIME
432 INTERCEPTOR(unsigned long, time, unsigned long *t) {
433 void *ctx;
434 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
435 unsigned long res = REAL(time)(t);
436 if (t && res != (unsigned long)-1) {
437 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
438 }
439 return res;
440 }
441 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
442 #else
443 #define INIT_TIME
444 #endif // SANITIZER_INTERCEPT_TIME
445
446 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
447 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
448 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
449 if (tm->tm_zone) {
450 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
451 // can point to shared memory and tsan would report a data race.
452 COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, tm->tm_zone,
453 REAL(strlen(tm->tm_zone)) + 1);
454 }
455 }
456 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
457 void *ctx;
458 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
459 __sanitizer_tm *res = REAL(localtime)(timep);
460 if (res) {
461 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
462 unpoison_tm(ctx, res);
463 }
464 return res;
465 }
466 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
467 void *ctx;
468 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
469 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
470 if (res) {
471 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
472 unpoison_tm(ctx, res);
473 }
474 return res;
475 }
476 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
477 void *ctx;
478 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
479 __sanitizer_tm *res = REAL(gmtime)(timep);
480 if (res) {
481 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
482 unpoison_tm(ctx, res);
483 }
484 return res;
485 }
486 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
487 void *ctx;
488 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
489 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
490 if (res) {
491 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
492 unpoison_tm(ctx, res);
493 }
494 return res;
495 }
496 INTERCEPTOR(char *, ctime, unsigned long *timep) {
497 void *ctx;
498 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
499 char *res = REAL(ctime)(timep);
500 if (res) {
501 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
502 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
503 }
504 return res;
505 }
506 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
507 void *ctx;
508 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
509 char *res = REAL(ctime_r)(timep, result);
510 if (res) {
511 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
512 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
513 }
514 return res;
515 }
516 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
517 void *ctx;
518 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
519 char *res = REAL(asctime)(tm);
520 if (res) {
521 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
522 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
523 }
524 return res;
525 }
526 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
527 void *ctx;
528 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
529 char *res = REAL(asctime_r)(tm, result);
530 if (res) {
531 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
532 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
533 }
534 return res;
535 }
536 #define INIT_LOCALTIME_AND_FRIENDS \
537 COMMON_INTERCEPT_FUNCTION(localtime); \
538 COMMON_INTERCEPT_FUNCTION(localtime_r); \
539 COMMON_INTERCEPT_FUNCTION(gmtime); \
540 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
541 COMMON_INTERCEPT_FUNCTION(ctime); \
542 COMMON_INTERCEPT_FUNCTION(ctime_r); \
543 COMMON_INTERCEPT_FUNCTION(asctime); \
544 COMMON_INTERCEPT_FUNCTION(asctime_r);
545 #else
546 #define INIT_LOCALTIME_AND_FRIENDS
547 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
548
549 #if SANITIZER_INTERCEPT_STRPTIME
550 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
551 void *ctx;
552 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
553 if (format)
554 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
555 char *res = REAL(strptime)(s, format, tm);
556 if (res) {
557 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, res - s);
558 // Do not call unpoison_tm here, because strptime does not, in fact,
559 // initialize the entire struct tm. For example, tm_zone pointer is left
560 // uninitialized.
561 if (tm) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
562 }
563 return res;
564 }
565 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
566 #else
567 #define INIT_STRPTIME
568 #endif
569
570 #if SANITIZER_INTERCEPT_SCANF
571
572 #include "sanitizer_common_interceptors_scanf.inc"
573
574 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
575 { \
576 void *ctx; \
577 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
578 va_list aq; \
579 va_copy(aq, ap); \
580 int res = REAL(vname)(__VA_ARGS__); \
581 if (res > 0) \
582 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
583 va_end(aq); \
584 return res; \
585 }
586
587 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
588 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
589
590 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
591 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
592
593 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
594 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
595
596 #if SANITIZER_INTERCEPT_ISOC99_SCANF
597 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
598 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
599
600 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
601 va_list ap)
602 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
603
604 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
605 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
606 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
607
608 #define SCANF_INTERCEPTOR_IMPL(name, vname, ...) \
609 { \
610 void *ctx; \
611 va_list ap; \
612 va_start(ap, format); \
613 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
614 int res = vname(__VA_ARGS__, ap); \
615 va_end(ap); \
616 return res; \
617 }
618
619 INTERCEPTOR(int, scanf, const char *format, ...)
620 SCANF_INTERCEPTOR_IMPL(scanf, vscanf, format)
621
622 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
623 SCANF_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
624
625 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
626 SCANF_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
627
628 #if SANITIZER_INTERCEPT_ISOC99_SCANF
629 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
630 SCANF_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
631
632 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
633 SCANF_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
634
635 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
636 SCANF_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
637 #endif
638
639 #endif
640
641 #if SANITIZER_INTERCEPT_SCANF
642 #define INIT_SCANF \
643 COMMON_INTERCEPT_FUNCTION(scanf); \
644 COMMON_INTERCEPT_FUNCTION(sscanf); \
645 COMMON_INTERCEPT_FUNCTION(fscanf); \
646 COMMON_INTERCEPT_FUNCTION(vscanf); \
647 COMMON_INTERCEPT_FUNCTION(vsscanf); \
648 COMMON_INTERCEPT_FUNCTION(vfscanf);
649 #else
650 #define INIT_SCANF
651 #endif
652
653 #if SANITIZER_INTERCEPT_ISOC99_SCANF
654 #define INIT_ISOC99_SCANF \
655 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
656 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
657 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
658 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
659 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
660 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
661 #else
662 #define INIT_ISOC99_SCANF
663 #endif
664
665 #if SANITIZER_INTERCEPT_IOCTL
666 #include "sanitizer_common_interceptors_ioctl.inc"
667 INTERCEPTOR(int, ioctl, int d, unsigned request, void *arg) {
668 void *ctx;
669 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
670
671 CHECK(ioctl_initialized);
672
673 // Note: TSan does not use common flags, and they are zero-initialized.
674 // This effectively disables ioctl handling in TSan.
675 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
676
677 const ioctl_desc *desc = ioctl_lookup(request);
678 if (!desc) Printf("WARNING: unknown ioctl %x\n", request);
679
680 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
681 int res = REAL(ioctl)(d, request, arg);
682 // FIXME: some ioctls have different return values for success and failure.
683 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
684 return res;
685 }
686 #define INIT_IOCTL \
687 ioctl_init(); \
688 COMMON_INTERCEPT_FUNCTION(ioctl);
689 #else
690 #define INIT_IOCTL
691 #endif
692
693 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
694 INTERCEPTOR(void *, getpwnam, const char *name) {
695 void *ctx;
696 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
697 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
698 void *res = REAL(getpwnam)(name);
699 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_passwd_sz);
700 return res;
701 }
702 INTERCEPTOR(void *, getpwuid, u32 uid) {
703 void *ctx;
704 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
705 void *res = REAL(getpwuid)(uid);
706 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_passwd_sz);
707 return res;
708 }
709 INTERCEPTOR(void *, getgrnam, const char *name) {
710 void *ctx;
711 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
712 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
713 void *res = REAL(getgrnam)(name);
714 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_group_sz);
715 return res;
716 }
717 INTERCEPTOR(void *, getgrgid, u32 gid) {
718 void *ctx;
719 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
720 void *res = REAL(getgrgid)(gid);
721 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_group_sz);
722 return res;
723 }
724 #define INIT_GETPWNAM_AND_FRIENDS \
725 COMMON_INTERCEPT_FUNCTION(getpwnam); \
726 COMMON_INTERCEPT_FUNCTION(getpwuid); \
727 COMMON_INTERCEPT_FUNCTION(getgrnam); \
728 COMMON_INTERCEPT_FUNCTION(getgrgid);
729 #else
730 #define INIT_GETPWNAM_AND_FRIENDS
731 #endif
732
733 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
734 INTERCEPTOR(int, getpwnam_r, const char *name, void *pwd, char *buf,
735 SIZE_T buflen, void **result) {
736 void *ctx;
737 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
738 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
739 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
740 if (!res) {
741 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, struct_passwd_sz);
742 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
743 }
744 return res;
745 }
746 INTERCEPTOR(int, getpwuid_r, u32 uid, void *pwd, char *buf, SIZE_T buflen,
747 void **result) {
748 void *ctx;
749 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
750 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
751 if (!res) {
752 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, struct_passwd_sz);
753 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
754 }
755 return res;
756 }
757 INTERCEPTOR(int, getgrnam_r, const char *name, void *grp, char *buf,
758 SIZE_T buflen, void **result) {
759 void *ctx;
760 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
761 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
762 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
763 if (!res) {
764 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, struct_group_sz);
765 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
766 }
767 return res;
768 }
769 INTERCEPTOR(int, getgrgid_r, u32 gid, void *grp, char *buf, SIZE_T buflen,
770 void **result) {
771 void *ctx;
772 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
773 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
774 if (!res) {
775 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, struct_group_sz);
776 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
777 }
778 return res;
779 }
780 #define INIT_GETPWNAM_R_AND_FRIENDS \
781 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
782 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
783 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
784 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
785 #else
786 #define INIT_GETPWNAM_R_AND_FRIENDS
787 #endif
788
789 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
790 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
791 void *ctx;
792 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
793 int res = REAL(clock_getres)(clk_id, tp);
794 if (!res && tp) {
795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
796 }
797 return res;
798 }
799 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
800 void *ctx;
801 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
802 int res = REAL(clock_gettime)(clk_id, tp);
803 if (!res) {
804 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
805 }
806 return res;
807 }
808 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
809 void *ctx;
810 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
811 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
812 return REAL(clock_settime)(clk_id, tp);
813 }
814 #define INIT_CLOCK_GETTIME \
815 COMMON_INTERCEPT_FUNCTION(clock_getres); \
816 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
817 COMMON_INTERCEPT_FUNCTION(clock_settime);
818 #else
819 #define INIT_CLOCK_GETTIME
820 #endif
821
822 #if SANITIZER_INTERCEPT_GETITIMER
823 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
824 void *ctx;
825 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
826 int res = REAL(getitimer)(which, curr_value);
827 if (!res && curr_value) {
828 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
829 }
830 return res;
831 }
832 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
833 void *ctx;
834 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
835 if (new_value)
836 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
837 int res = REAL(setitimer)(which, new_value, old_value);
838 if (!res && old_value) {
839 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
840 }
841 return res;
842 }
843 #define INIT_GETITIMER \
844 COMMON_INTERCEPT_FUNCTION(getitimer); \
845 COMMON_INTERCEPT_FUNCTION(setitimer);
846 #else
847 #define INIT_GETITIMER
848 #endif
849
850 #if SANITIZER_INTERCEPT_GLOB
851 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
852 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
853 // +1 for NULL pointer at the end.
854 if (pglob->gl_pathv)
855 COMMON_INTERCEPTOR_WRITE_RANGE(
856 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
857 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
858 char *p = pglob->gl_pathv[i];
859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
860 }
861 }
862
863 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
864 static THREADLOCAL void *glob_ctx;
865
866 static void wrapped_gl_closedir(void *dir) {
867 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 1);
868 pglob_copy->gl_closedir(dir);
869 }
870
871 static void *wrapped_gl_readdir(void *dir) {
872 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 1);
873 return pglob_copy->gl_readdir(dir);
874 }
875
876 static void *wrapped_gl_opendir(const char *s) {
877 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 1);
878 COMMON_INTERCEPTOR_WRITE_RANGE(glob_ctx, s, REAL(strlen)(s) + 1);
879 return pglob_copy->gl_opendir(s);
880 }
881
882 static int wrapped_gl_lstat(const char *s, void *st) {
883 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 2);
884 COMMON_INTERCEPTOR_WRITE_RANGE(glob_ctx, s, REAL(strlen)(s) + 1);
885 return pglob_copy->gl_lstat(s, st);
886 }
887
888 static int wrapped_gl_stat(const char *s, void *st) {
889 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 2);
890 COMMON_INTERCEPTOR_WRITE_RANGE(glob_ctx, s, REAL(strlen)(s) + 1);
891 return pglob_copy->gl_stat(s, st);
892 }
893
894 INTERCEPTOR(int, glob, const char *pattern, int flags,
895 int (*errfunc)(const char *epath, int eerrno),
896 __sanitizer_glob_t *pglob) {
897 void *ctx;
898 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
899 __sanitizer_glob_t glob_copy = {
900 0, 0, 0,
901 0, wrapped_gl_closedir, wrapped_gl_readdir,
902 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
903 if (flags & glob_altdirfunc) {
904 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
905 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
906 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
907 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
908 Swap(pglob->gl_stat, glob_copy.gl_stat);
909 pglob_copy = &glob_copy;
910 glob_ctx = ctx;
911 }
912 int res = REAL(glob)(pattern, flags, errfunc, pglob);
913 if (flags & glob_altdirfunc) {
914 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
915 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
916 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
917 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
918 Swap(pglob->gl_stat, glob_copy.gl_stat);
919 }
920 pglob_copy = 0;
921 glob_ctx = 0;
922 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
923 return res;
924 }
925
926 INTERCEPTOR(int, glob64, const char *pattern, int flags,
927 int (*errfunc)(const char *epath, int eerrno),
928 __sanitizer_glob_t *pglob) {
929 void *ctx;
930 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
931 __sanitizer_glob_t glob_copy = {
932 0, 0, 0,
933 0, wrapped_gl_closedir, wrapped_gl_readdir,
934 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
935 if (flags & glob_altdirfunc) {
936 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
937 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
938 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
939 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
940 Swap(pglob->gl_stat, glob_copy.gl_stat);
941 pglob_copy = &glob_copy;
942 glob_ctx = ctx;
943 }
944 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
945 if (flags & glob_altdirfunc) {
946 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
947 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
948 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
949 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
950 Swap(pglob->gl_stat, glob_copy.gl_stat);
951 }
952 pglob_copy = 0;
953 glob_ctx = 0;
954 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
955 return res;
956 }
957 #define INIT_GLOB \
958 COMMON_INTERCEPT_FUNCTION(glob); \
959 COMMON_INTERCEPT_FUNCTION(glob64);
960 #else // SANITIZER_INTERCEPT_GLOB
961 #define INIT_GLOB
962 #endif // SANITIZER_INTERCEPT_GLOB
963
964 #if SANITIZER_INTERCEPT_WAIT
965 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
966 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
967 // details.
968 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
969 void *ctx;
970 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
971 int res = REAL(wait)(status);
972 if (res != -1 && status)
973 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
974 return res;
975 }
976 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
977 int options) {
978 void *ctx;
979 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
980 int res = REAL(waitid)(idtype, id, infop, options);
981 if (res != -1 && infop)
982 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
983 return res;
984 }
985 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
986 void *ctx;
987 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
988 int res = REAL(waitpid)(pid, status, options);
989 if (res != -1 && status)
990 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
991 return res;
992 }
993 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
994 void *ctx;
995 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
996 int res = REAL(wait3)(status, options, rusage);
997 if (res != -1) {
998 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
999 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1000 }
1001 return res;
1002 }
1003 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
1004 void *ctx;
1005 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
1006 int res = REAL(wait4)(pid, status, options, rusage);
1007 if (res != -1) {
1008 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1009 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1010 }
1011 return res;
1012 }
1013 #define INIT_WAIT \
1014 COMMON_INTERCEPT_FUNCTION(wait); \
1015 COMMON_INTERCEPT_FUNCTION(waitid); \
1016 COMMON_INTERCEPT_FUNCTION(waitpid); \
1017 COMMON_INTERCEPT_FUNCTION(wait3); \
1018 COMMON_INTERCEPT_FUNCTION(wait4);
1019 #else
1020 #define INIT_WAIT
1021 #endif
1022
1023 #if SANITIZER_INTERCEPT_INET
1024 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
1025 void *ctx;
1026 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
1027 uptr sz = __sanitizer_in_addr_sz(af);
1028 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
1029 // FIXME: figure out read size based on the address family.
1030 char *res = REAL(inet_ntop)(af, src, dst, size);
1031 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1032 return res;
1033 }
1034 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
1035 void *ctx;
1036 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
1037 // FIXME: figure out read size based on the address family.
1038 int res = REAL(inet_pton)(af, src, dst);
1039 if (res == 1) {
1040 uptr sz = __sanitizer_in_addr_sz(af);
1041 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1042 }
1043 return res;
1044 }
1045 #define INIT_INET \
1046 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
1047 COMMON_INTERCEPT_FUNCTION(inet_pton);
1048 #else
1049 #define INIT_INET
1050 #endif
1051
1052 #if SANITIZER_INTERCEPT_INET
1053 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
1054 void *ctx;
1055 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
1056 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
1057 int res = REAL(inet_aton)(cp, dst);
1058 if (res != 0) {
1059 uptr sz = __sanitizer_in_addr_sz(af_inet);
1060 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1061 }
1062 return res;
1063 }
1064 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
1065 #else
1066 #define INIT_INET_ATON
1067 #endif
1068
1069 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
1070 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
1071 void *ctx;
1072 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
1073 int res = REAL(pthread_getschedparam)(thread, policy, param);
1074 if (res == 0) {
1075 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
1076 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
1077 }
1078 return res;
1079 }
1080 #define INIT_PTHREAD_GETSCHEDPARAM \
1081 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
1082 #else
1083 #define INIT_PTHREAD_GETSCHEDPARAM
1084 #endif
1085
1086 #if SANITIZER_INTERCEPT_GETADDRINFO
1087 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
1088 struct __sanitizer_addrinfo *hints,
1089 struct __sanitizer_addrinfo **out) {
1090 void *ctx;
1091 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
1092 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
1093 if (service)
1094 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
1095 if (hints)
1096 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
1097 int res = REAL(getaddrinfo)(node, service, hints, out);
1098 if (res == 0 && out) {
1099 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
1100 struct __sanitizer_addrinfo *p = *out;
1101 while (p) {
1102 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
1103 if (p->ai_addr)
1104 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
1105 if (p->ai_canonname)
1106 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
1107 REAL(strlen)(p->ai_canonname) + 1);
1108 p = p->ai_next;
1109 }
1110 }
1111 return res;
1112 }
1113 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
1114 #else
1115 #define INIT_GETADDRINFO
1116 #endif
1117
1118 #if SANITIZER_INTERCEPT_GETNAMEINFO
1119 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
1120 unsigned hostlen, char *serv, unsigned servlen, int flags) {
1121 void *ctx;
1122 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
1123 serv, servlen, flags);
1124 // FIXME: consider adding READ_RANGE(sockaddr, salen)
1125 // There is padding in in_addr that may make this too noisy
1126 int res =
1127 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
1128 if (res == 0) {
1129 if (host && hostlen)
1130 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
1131 if (serv && servlen)
1132 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
1133 }
1134 return res;
1135 }
1136 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
1137 #else
1138 #define INIT_GETNAMEINFO
1139 #endif
1140
1141 #if SANITIZER_INTERCEPT_GETSOCKNAME
1142 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
1143 void *ctx;
1144 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
1145 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1146 int addrlen_in = *addrlen;
1147 int res = REAL(getsockname)(sock_fd, addr, addrlen);
1148 if (res == 0) {
1149 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
1150 }
1151 return res;
1152 }
1153 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
1154 #else
1155 #define INIT_GETSOCKNAME
1156 #endif
1157
1158 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1159 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
1160 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
1161 if (h->h_name)
1162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
1163 char **p = h->h_aliases;
1164 while (*p) {
1165 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
1166 ++p;
1167 }
1168 COMMON_INTERCEPTOR_WRITE_RANGE(
1169 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
1170 p = h->h_addr_list;
1171 while (*p) {
1172 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
1173 ++p;
1174 }
1175 COMMON_INTERCEPTOR_WRITE_RANGE(
1176 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
1177 }
1178 #endif
1179
1180 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
1181 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
1182 void *ctx;
1183 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
1184 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
1185 if (res) write_hostent(ctx, res);
1186 return res;
1187 }
1188
1189 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
1190 int type) {
1191 void *ctx;
1192 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
1193 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1194 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
1195 if (res) write_hostent(ctx, res);
1196 return res;
1197 }
1198
1199 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
1200 void *ctx;
1201 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
1202 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
1203 if (res) write_hostent(ctx, res);
1204 return res;
1205 }
1206
1207 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
1208 void *ctx;
1209 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
1210 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
1211 if (res) write_hostent(ctx, res);
1212 return res;
1213 }
1214 #define INIT_GETHOSTBYNAME \
1215 COMMON_INTERCEPT_FUNCTION(gethostent); \
1216 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
1217 COMMON_INTERCEPT_FUNCTION(gethostbyname); \
1218 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
1219 #else
1220 #define INIT_GETHOSTBYNAME
1221 #endif
1222
1223 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1224 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
1225 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
1226 void *ctx;
1227 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
1228 h_errnop);
1229 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
1230 if (res == 0) {
1231 if (result) {
1232 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1233 if (*result) write_hostent(ctx, *result);
1234 }
1235 if (h_errnop)
1236 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1237 }
1238 return res;
1239 }
1240
1241 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
1242 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1243 __sanitizer_hostent **result, int *h_errnop) {
1244 void *ctx;
1245 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
1246 buflen, result, h_errnop);
1247 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1248 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
1249 h_errnop);
1250 if (res == 0) {
1251 if (result) {
1252 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1253 if (*result) write_hostent(ctx, *result);
1254 }
1255 if (h_errnop)
1256 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1257 }
1258 return res;
1259 }
1260
1261 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
1262 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
1263 int *h_errnop) {
1264 void *ctx;
1265 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
1266 h_errnop);
1267 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
1268 if (res == 0) {
1269 if (result) {
1270 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1271 if (*result) write_hostent(ctx, *result);
1272 }
1273 if (h_errnop)
1274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1275 }
1276 return res;
1277 }
1278
1279 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
1280 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1281 __sanitizer_hostent **result, int *h_errnop) {
1282 void *ctx;
1283 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
1284 result, h_errnop);
1285 int res =
1286 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
1287 if (res == 0) {
1288 if (result) {
1289 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1290 if (*result) write_hostent(ctx, *result);
1291 }
1292 if (h_errnop)
1293 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1294 }
1295 return res;
1296 }
1297 #define INIT_GETHOSTBYNAME_R \
1298 COMMON_INTERCEPT_FUNCTION(gethostent_r); \
1299 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r); \
1300 COMMON_INTERCEPT_FUNCTION(gethostbyname_r); \
1301 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
1302 #else
1303 #define INIT_GETHOSTBYNAME_R
1304 #endif
1305
1306 #if SANITIZER_INTERCEPT_GETSOCKOPT
1307 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
1308 int *optlen) {
1309 void *ctx;
1310 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
1311 optlen);
1312 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
1313 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
1314 if (res == 0)
1315 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
1316 return res;
1317 }
1318 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
1319 #else
1320 #define INIT_GETSOCKOPT
1321 #endif
1322
1323 #if SANITIZER_INTERCEPT_ACCEPT
1324 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
1325 void *ctx;
1326 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
1327 unsigned addrlen0;
1328 if (addrlen) {
1329 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1330 addrlen0 = *addrlen;
1331 }
1332 int fd2 = REAL(accept)(fd, addr, addrlen);
1333 if (fd2 >= 0) {
1334 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
1335 if (addr && addrlen)
1336 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
1337 }
1338 return fd2;
1339 }
1340 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
1341 #else
1342 #define INIT_ACCEPT
1343 #endif
1344
1345 #if SANITIZER_INTERCEPT_ACCEPT4
1346 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
1347 void *ctx;
1348 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
1349 unsigned addrlen0;
1350 if (addrlen) {
1351 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1352 addrlen0 = *addrlen;
1353 }
1354 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
1355 if (fd2 >= 0) {
1356 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
1357 if (addr && addrlen)
1358 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
1359 }
1360 return fd2;
1361 }
1362 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
1363 #else
1364 #define INIT_ACCEPT4
1365 #endif
1366
1367 #if SANITIZER_INTERCEPT_MODF
1368 INTERCEPTOR(double, modf, double x, double *iptr) {
1369 void *ctx;
1370 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
1371 double res = REAL(modf)(x, iptr);
1372 if (iptr) {
1373 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
1374 }
1375 return res;
1376 }
1377 INTERCEPTOR(float, modff, float x, float *iptr) {
1378 void *ctx;
1379 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
1380 float res = REAL(modff)(x, iptr);
1381 if (iptr) {
1382 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
1383 }
1384 return res;
1385 }
1386 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
1387 void *ctx;
1388 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
1389 long double res = REAL(modfl)(x, iptr);
1390 if (iptr) {
1391 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
1392 }
1393 return res;
1394 }
1395 #define INIT_MODF \
1396 COMMON_INTERCEPT_FUNCTION(modf); \
1397 COMMON_INTERCEPT_FUNCTION(modff); \
1398 COMMON_INTERCEPT_FUNCTION(modfl);
1399 #else
1400 #define INIT_MODF
1401 #endif
1402
1403 #if SANITIZER_INTERCEPT_RECVMSG
1404 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
1405 SSIZE_T maxlen) {
1406 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
1407 if (msg->msg_name && msg->msg_namelen)
1408 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
1409 if (msg->msg_iov && msg->msg_iovlen)
1410 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
1411 sizeof(*msg->msg_iov) * msg->msg_iovlen);
1412 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
1413 if (msg->msg_control && msg->msg_controllen)
1414 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
1415 }
1416
1417 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
1418 int flags) {
1419 void *ctx;
1420 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
1421 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
1422 if (res >= 0) {
1423 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1424 if (msg) {
1425 write_msghdr(ctx, msg, res);
1426 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
1427 }
1428 }
1429 return res;
1430 }
1431 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
1432 #else
1433 #define INIT_RECVMSG
1434 #endif
1435
1436 #if SANITIZER_INTERCEPT_GETPEERNAME
1437 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
1438 void *ctx;
1439 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
1440 unsigned addr_sz;
1441 if (addrlen) addr_sz = *addrlen;
1442 int res = REAL(getpeername)(sockfd, addr, addrlen);
1443 if (!res && addr && addrlen)
1444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
1445 return res;
1446 }
1447 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
1448 #else
1449 #define INIT_GETPEERNAME
1450 #endif
1451
1452 #if SANITIZER_INTERCEPT_SYSINFO
1453 INTERCEPTOR(int, sysinfo, void *info) {
1454 void *ctx;
1455 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
1456 int res = REAL(sysinfo)(info);
1457 if (!res && info)
1458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
1459 return res;
1460 }
1461 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
1462 #else
1463 #define INIT_SYSINFO
1464 #endif
1465
1466 #if SANITIZER_INTERCEPT_READDIR
1467 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
1468 void *ctx;
1469 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
1470 __sanitizer_dirent *res = REAL(readdir)(dirp);
1471 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
1472 return res;
1473 }
1474
1475 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
1476 __sanitizer_dirent **result) {
1477 void *ctx;
1478 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
1479 int res = REAL(readdir_r)(dirp, entry, result);
1480 if (!res) {
1481 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1482 if (*result)
1483 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
1484 }
1485 return res;
1486 }
1487
1488 #define INIT_READDIR \
1489 COMMON_INTERCEPT_FUNCTION(readdir); \
1490 COMMON_INTERCEPT_FUNCTION(readdir_r);
1491 #else
1492 #define INIT_READDIR
1493 #endif
1494
1495 #if SANITIZER_INTERCEPT_READDIR64
1496 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
1497 void *ctx;
1498 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
1499 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
1500 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
1501 return res;
1502 }
1503
1504 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
1505 __sanitizer_dirent64 **result) {
1506 void *ctx;
1507 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
1508 int res = REAL(readdir64_r)(dirp, entry, result);
1509 if (!res) {
1510 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1511 if (*result)
1512 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
1513 }
1514 return res;
1515 }
1516 #define INIT_READDIR64 \
1517 COMMON_INTERCEPT_FUNCTION(readdir64); \
1518 COMMON_INTERCEPT_FUNCTION(readdir64_r);
1519 #else
1520 #define INIT_READDIR64
1521 #endif
1522
1523 #if SANITIZER_INTERCEPT_PTRACE
1524 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
1525 void *ctx;
1526 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
1527
1528 if (data) {
1529 if (request == ptrace_setregs)
1530 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
1531 else if (request == ptrace_setfpregs)
1532 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
1533 else if (request == ptrace_setfpxregs)
1534 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
1535 else if (request == ptrace_setsiginfo)
1536 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
1537 else if (request == ptrace_setregset) {
1538 __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
1539 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov->iov_base, iov->iov_len);
1540 }
1541 }
1542
1543 uptr res = REAL(ptrace)(request, pid, addr, data);
1544
1545 if (!res && data) {
1546 // Note that PEEK* requests assing different meaning to the return value.
1547 // This function does not handle them (nor does it need to).
1548 if (request == ptrace_getregs)
1549 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
1550 else if (request == ptrace_getfpregs)
1551 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
1552 else if (request == ptrace_getfpxregs)
1553 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
1554 else if (request == ptrace_getsiginfo)
1555 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
1556 else if (request == ptrace_getregset) {
1557 __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
1558 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iov->iov_base, iov->iov_len);
1559 }
1560 }
1561 return res;
1562 }
1563
1564 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
1565 #else
1566 #define INIT_PTRACE
1567 #endif
1568
1569 #if SANITIZER_INTERCEPT_SETLOCALE
1570 INTERCEPTOR(char *, setlocale, int category, char *locale) {
1571 void *ctx;
1572 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
1573 if (locale)
1574 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
1575 char *res = REAL(setlocale)(category, locale);
1576 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1577 return res;
1578 }
1579
1580 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
1581 #else
1582 #define INIT_SETLOCALE
1583 #endif
1584
1585 #if SANITIZER_INTERCEPT_GETCWD
1586 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
1587 void *ctx;
1588 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
1589 char *res = REAL(getcwd)(buf, size);
1590 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1591 return res;
1592 }
1593 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
1594 #else
1595 #define INIT_GETCWD
1596 #endif
1597
1598 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
1599 INTERCEPTOR(char *, get_current_dir_name, int fake) {
1600 void *ctx;
1601 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
1602 char *res = REAL(get_current_dir_name)(fake);
1603 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1604 return res;
1605 }
1606
1607 #define INIT_GET_CURRENT_DIR_NAME \
1608 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
1609 #else
1610 #define INIT_GET_CURRENT_DIR_NAME
1611 #endif
1612
1613 #if SANITIZER_INTERCEPT_STRTOIMAX
1614 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
1615 void *ctx;
1616 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
1617 INTMAX_T res = REAL(strtoimax)(nptr, endptr, base);
1618 if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
1619 return res;
1620 }
1621
1622 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
1623 void *ctx;
1624 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
1625 INTMAX_T res = REAL(strtoumax)(nptr, endptr, base);
1626 if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
1627 return res;
1628 }
1629
1630 #define INIT_STRTOIMAX \
1631 COMMON_INTERCEPT_FUNCTION(strtoimax); \
1632 COMMON_INTERCEPT_FUNCTION(strtoumax);
1633 #else
1634 #define INIT_STRTOIMAX
1635 #endif
1636
1637 #if SANITIZER_INTERCEPT_MBSTOWCS
1638 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
1639 void *ctx;
1640 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
1641 SIZE_T res = REAL(mbstowcs)(dest, src, len);
1642 if (res != (SIZE_T) - 1 && dest) {
1643 SIZE_T write_cnt = res + (res < len);
1644 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
1645 }
1646 return res;
1647 }
1648
1649 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
1650 void *ps) {
1651 void *ctx;
1652 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
1653 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1654 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1655 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
1656 if (res != (SIZE_T)(-1) && dest && src) {
1657 // This function, and several others, may or may not write the terminating
1658 // \0 character. They write it iff they clear *src.
1659 SIZE_T write_cnt = res + !*src;
1660 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
1661 }
1662 return res;
1663 }
1664
1665 #define INIT_MBSTOWCS \
1666 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
1667 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
1668 #else
1669 #define INIT_MBSTOWCS
1670 #endif
1671
1672 #if SANITIZER_INTERCEPT_MBSNRTOWCS
1673 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
1674 SIZE_T len, void *ps) {
1675 void *ctx;
1676 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
1677 if (src) {
1678 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1679 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
1680 }
1681 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1682 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
1683 if (res != (SIZE_T)(-1) && dest && src) {
1684 SIZE_T write_cnt = res + !*src;
1685 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
1686 }
1687 return res;
1688 }
1689
1690 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
1691 #else
1692 #define INIT_MBSNRTOWCS
1693 #endif
1694
1695 #if SANITIZER_INTERCEPT_WCSTOMBS
1696 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
1697 void *ctx;
1698 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
1699 SIZE_T res = REAL(wcstombs)(dest, src, len);
1700 if (res != (SIZE_T) - 1 && dest) {
1701 SIZE_T write_cnt = res + (res < len);
1702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
1703 }
1704 return res;
1705 }
1706
1707 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
1708 void *ps) {
1709 void *ctx;
1710 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
1711 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1712 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1713 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
1714 if (res != (SIZE_T) - 1 && dest && src) {
1715 SIZE_T write_cnt = res + !*src;
1716 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
1717 }
1718 return res;
1719 }
1720
1721 #define INIT_WCSTOMBS \
1722 COMMON_INTERCEPT_FUNCTION(wcstombs); \
1723 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
1724 #else
1725 #define INIT_WCSTOMBS
1726 #endif
1727
1728 #if SANITIZER_INTERCEPT_WCSNRTOMBS
1729 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
1730 SIZE_T len, void *ps) {
1731 void *ctx;
1732 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
1733 if (src) {
1734 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1735 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
1736 }
1737 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1738 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
1739 if (res != (SIZE_T) - 1 && dest && src) {
1740 SIZE_T write_cnt = res + !*src;
1741 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
1742 }
1743 return res;
1744 }
1745
1746 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
1747 #else
1748 #define INIT_WCSNRTOMBS
1749 #endif
1750
1751 #if SANITIZER_INTERCEPT_TCGETATTR
1752 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
1753 void *ctx;
1754 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
1755 int res = REAL(tcgetattr)(fd, termios_p);
1756 if (!res && termios_p)
1757 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
1758 return res;
1759 }
1760
1761 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
1762 #else
1763 #define INIT_TCGETATTR
1764 #endif
1765
1766 #if SANITIZER_INTERCEPT_REALPATH
1767 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
1768 void *ctx;
1769 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
1770 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
1771
1772 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
1773 // version of a versioned symbol. For realpath(), this gives us something
1774 // (called __old_realpath) that does not handle NULL in the second argument.
1775 // Handle it as part of the interceptor.
1776 char *allocated_path = 0;
1777 if (!resolved_path)
1778 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
1779
1780 char *res = REAL(realpath)(path, resolved_path);
1781 if (allocated_path && !res) WRAP(free)(allocated_path);
1782 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1783 return res;
1784 }
1785 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
1786 #else
1787 #define INIT_REALPATH
1788 #endif
1789
1790 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
1791 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
1792 void *ctx;
1793 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
1794 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
1795 char *res = REAL(canonicalize_file_name)(path);
1796 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1797 return res;
1798 }
1799 #define INIT_CANONICALIZE_FILE_NAME \
1800 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
1801 #else
1802 #define INIT_CANONICALIZE_FILE_NAME
1803 #endif
1804
1805 #if SANITIZER_INTERCEPT_CONFSTR
1806 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
1807 void *ctx;
1808 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
1809 SIZE_T res = REAL(confstr)(name, buf, len);
1810 if (buf && res)
1811 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
1812 return res;
1813 }
1814 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
1815 #else
1816 #define INIT_CONFSTR
1817 #endif
1818
1819 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
1820 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
1821 void *ctx;
1822 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
1823 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
1824 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
1825 return res;
1826 }
1827 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
1828 #else
1829 #define INIT_SCHED_GETAFFINITY
1830 #endif
1831
1832 #if SANITIZER_INTERCEPT_STRERROR
1833 INTERCEPTOR(char *, strerror, int errnum) {
1834 void *ctx;
1835 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
1836 char *res = REAL(strerror)(errnum);
1837 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1838 return res;
1839 }
1840 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
1841 #else
1842 #define INIT_STRERROR
1843 #endif
1844
1845 #if SANITIZER_INTERCEPT_STRERROR_R
1846 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
1847 void *ctx;
1848 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
1849 char *res = REAL(strerror_r)(errnum, buf, buflen);
1850 // There are 2 versions of strerror_r:
1851 // * POSIX version returns 0 on success, negative error code on failure,
1852 // writes message to buf.
1853 // * GNU version returns message pointer, which points to either buf or some
1854 // static storage.
1855 SIZE_T posix_res = (SIZE_T)res;
1856 if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
1857 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
1858 // At least on OSX, buf contents are valid even when the call fails.
1859 SIZE_T sz = internal_strnlen(buf, buflen);
1860 if (sz < buflen) ++sz;
1861 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
1862 } else {
1863 // GNU version.
1864 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1865 }
1866 return res;
1867 }
1868 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
1869 #else
1870 #define INIT_STRERROR_R
1871 #endif
1872
1873 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
1874 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
1875 void *ctx;
1876 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
1877 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
1878 // This version always returns a null-terminated string.
1879 if (buf && buflen)
1880 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
1881 return res;
1882 }
1883 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
1884 #else
1885 #define INIT_XPG_STRERROR_R
1886 #endif
1887
1888 #if SANITIZER_INTERCEPT_SCANDIR
1889 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
1890 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
1891 const struct __sanitizer_dirent **);
1892
1893 static THREADLOCAL void *scandir_ctx;
1894 static THREADLOCAL scandir_filter_f scandir_filter;
1895 static THREADLOCAL scandir_compar_f scandir_compar;
1896
1897 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
1898 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir_ctx, 1);
1899 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, dir, dir->d_reclen);
1900 return scandir_filter(dir);
1901 }
1902
1903 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
1904 const struct __sanitizer_dirent **b) {
1905 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir_ctx, 2);
1906 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, a, sizeof(*a));
1907 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, *a, (*a)->d_reclen);
1908 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, b, sizeof(*b));
1909 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, *b, (*b)->d_reclen);
1910 return scandir_compar(a, b);
1911 }
1912
1913 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
1914 scandir_filter_f filter, scandir_compar_f compar) {
1915 void *ctx;
1916 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
1917 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
1918 CHECK_EQ(0, scandir_ctx);
1919 scandir_ctx = ctx;
1920 scandir_filter = filter;
1921 scandir_compar = compar;
1922 int res = REAL(scandir)(dirp, namelist, filter ? wrapped_scandir_filter : 0,
1923 compar ? wrapped_scandir_compar : 0);
1924 scandir_ctx = 0;
1925 scandir_filter = 0;
1926 scandir_compar = 0;
1927 if (namelist && res > 0) {
1928 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
1929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
1930 for (int i = 0; i < res; ++i)
1931 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
1932 (*namelist)[i]->d_reclen);
1933 }
1934 return res;
1935 }
1936 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
1937 #else
1938 #define INIT_SCANDIR
1939 #endif
1940
1941 #if SANITIZER_INTERCEPT_SCANDIR64
1942 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
1943 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
1944 const struct __sanitizer_dirent64 **);
1945
1946 static THREADLOCAL void *scandir64_ctx;
1947 static THREADLOCAL scandir64_filter_f scandir64_filter;
1948 static THREADLOCAL scandir64_compar_f scandir64_compar;
1949
1950 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
1951 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir64_ctx, 1);
1952 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, dir, dir->d_reclen);
1953 return scandir64_filter(dir);
1954 }
1955
1956 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
1957 const struct __sanitizer_dirent64 **b) {
1958 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir64_ctx, 2);
1959 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, a, sizeof(*a));
1960 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, *a, (*a)->d_reclen);
1961 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, b, sizeof(*b));
1962 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, *b, (*b)->d_reclen);
1963 return scandir64_compar(a, b);
1964 }
1965
1966 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
1967 scandir64_filter_f filter, scandir64_compar_f compar) {
1968 void *ctx;
1969 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
1970 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
1971 CHECK_EQ(0, scandir64_ctx);
1972 scandir64_ctx = ctx;
1973 scandir64_filter = filter;
1974 scandir64_compar = compar;
1975 int res =
1976 REAL(scandir64)(dirp, namelist, filter ? wrapped_scandir64_filter : 0,
1977 compar ? wrapped_scandir64_compar : 0);
1978 scandir64_ctx = 0;
1979 scandir64_filter = 0;
1980 scandir64_compar = 0;
1981 if (namelist && res > 0) {
1982 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
1983 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
1984 for (int i = 0; i < res; ++i)
1985 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
1986 (*namelist)[i]->d_reclen);
1987 }
1988 return res;
1989 }
1990 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
1991 #else
1992 #define INIT_SCANDIR64
1993 #endif
1994
1995 #if SANITIZER_INTERCEPT_GETGROUPS
1996 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
1997 void *ctx;
1998 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
1999 int res = REAL(getgroups)(size, lst);
2000 if (res && lst) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
2001 return res;
2002 }
2003 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
2004 #else
2005 #define INIT_GETGROUPS
2006 #endif
2007
2008 #if SANITIZER_INTERCEPT_POLL
2009 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
2010 __sanitizer_nfds_t nfds) {
2011 for (unsigned i = 0; i < nfds; ++i) {
2012 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
2013 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
2014 }
2015 }
2016
2017 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
2018 __sanitizer_nfds_t nfds) {
2019 for (unsigned i = 0; i < nfds; ++i)
2020 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
2021 sizeof(fds[i].revents));
2022 }
2023
2024 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2025 int timeout) {
2026 void *ctx;
2027 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
2028 if (fds && nfds) read_pollfd(ctx, fds, nfds);
2029 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
2030 if (fds && nfds) write_pollfd(ctx, fds, nfds);
2031 return res;
2032 }
2033 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
2034 #else
2035 #define INIT_POLL
2036 #endif
2037
2038 #if SANITIZER_INTERCEPT_PPOLL
2039 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2040 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
2041 void *ctx;
2042 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
2043 if (fds && nfds) read_pollfd(ctx, fds, nfds);
2044 if (timeout_ts)
2045 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
2046 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
2047 int res =
2048 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
2049 if (fds && nfds) write_pollfd(ctx, fds, nfds);
2050 return res;
2051 }
2052 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
2053 #else
2054 #define INIT_PPOLL
2055 #endif
2056
2057 #if SANITIZER_INTERCEPT_WORDEXP
2058 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
2059 void *ctx;
2060 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
2061 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
2062 int res = REAL(wordexp)(s, p, flags);
2063 if (!res && p) {
2064 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2065 if (p->we_wordc)
2066 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
2067 sizeof(*p->we_wordv) * p->we_wordc);
2068 for (uptr i = 0; i < p->we_wordc; ++i) {
2069 char *w = p->we_wordv[i];
2070 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
2071 }
2072 }
2073 return res;
2074 }
2075 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
2076 #else
2077 #define INIT_WORDEXP
2078 #endif
2079
2080 #if SANITIZER_INTERCEPT_SIGWAIT
2081 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
2082 void *ctx;
2083 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
2084 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2085 int res = REAL(sigwait)(set, sig);
2086 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
2087 return res;
2088 }
2089 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
2090 #else
2091 #define INIT_SIGWAIT
2092 #endif
2093
2094 #if SANITIZER_INTERCEPT_SIGWAITINFO
2095 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
2096 void *ctx;
2097 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
2098 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2099 int res = REAL(sigwaitinfo)(set, info);
2100 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2101 return res;
2102 }
2103 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
2104 #else
2105 #define INIT_SIGWAITINFO
2106 #endif
2107
2108 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
2109 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
2110 void *timeout) {
2111 void *ctx;
2112 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
2113 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2114 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2115 int res = REAL(sigtimedwait)(set, info, timeout);
2116 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2117 return res;
2118 }
2119 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
2120 #else
2121 #define INIT_SIGTIMEDWAIT
2122 #endif
2123
2124 #if SANITIZER_INTERCEPT_SIGSETOPS
2125 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
2126 void *ctx;
2127 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
2128 int res = REAL(sigemptyset)(set);
2129 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2130 return res;
2131 }
2132
2133 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
2134 void *ctx;
2135 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
2136 int res = REAL(sigfillset)(set);
2137 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2138 return res;
2139 }
2140 #define INIT_SIGSETOPS \
2141 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
2142 COMMON_INTERCEPT_FUNCTION(sigfillset);
2143 #else
2144 #define INIT_SIGSETOPS
2145 #endif
2146
2147 #if SANITIZER_INTERCEPT_SIGPENDING
2148 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
2149 void *ctx;
2150 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
2151 int res = REAL(sigpending)(set);
2152 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2153 return res;
2154 }
2155 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
2156 #else
2157 #define INIT_SIGPENDING
2158 #endif
2159
2160 #if SANITIZER_INTERCEPT_SIGPROCMASK
2161 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
2162 __sanitizer_sigset_t *oldset) {
2163 void *ctx;
2164 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
2165 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2166 int res = REAL(sigprocmask)(how, set, oldset);
2167 if (!res && oldset)
2168 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
2169 return res;
2170 }
2171 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
2172 #else
2173 #define INIT_SIGPROCMASK
2174 #endif
2175
2176 #if SANITIZER_INTERCEPT_BACKTRACE
2177 INTERCEPTOR(int, backtrace, void **buffer, int size) {
2178 void *ctx;
2179 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
2180 int res = REAL(backtrace)(buffer, size);
2181 if (res && buffer)
2182 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
2183 return res;
2184 }
2185
2186 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
2187 void *ctx;
2188 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
2189 if (buffer && size)
2190 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
2191 char **res = REAL(backtrace_symbols)(buffer, size);
2192 if (res && size) {
2193 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
2194 for (int i = 0; i < size; ++i)
2195 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
2196 }
2197 return res;
2198 }
2199 #define INIT_BACKTRACE \
2200 COMMON_INTERCEPT_FUNCTION(backtrace); \
2201 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
2202 #else
2203 #define INIT_BACKTRACE
2204 #endif
2205
2206 #if SANITIZER_INTERCEPT__EXIT
2207 INTERCEPTOR(void, _exit, int status) {
2208 void *ctx;
2209 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
2210 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
2211 if (status == 0) status = status1;
2212 REAL(_exit)(status);
2213 }
2214 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
2215 #else
2216 #define INIT__EXIT
2217 #endif
2218
2219 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
2220 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
2221 void *ctx;
2222 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
2223 int res = REAL(pthread_mutex_lock)(m);
2224 if (res == errno_EOWNERDEAD)
2225 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
2226 if (res == 0 || res == errno_EOWNERDEAD)
2227 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
2228 return res;
2229 }
2230
2231 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
2232 void *ctx;
2233 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
2234 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
2235 return REAL(pthread_mutex_unlock)(m);
2236 }
2237
2238 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
2239 #define INIT_PTHREAD_MUTEX_UNLOCK \
2240 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
2241 #else
2242 #define INIT_PTHREAD_MUTEX_LOCK
2243 #define INIT_PTHREAD_MUTEX_UNLOCK
2244 #endif
2245
2246 #if SANITIZER_INTERCEPT_PTHREAD_COND
2247 INTERCEPTOR(int, pthread_cond_wait, void *c, void *m) {
2248 void *ctx;
2249 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_wait, c, m);
2250 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
2251 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, pthread_cond_t_sz);
2252 int res = REAL(pthread_cond_wait)(c, m);
2253 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
2254 return res;
2255 }
2256
2257 INTERCEPTOR(int, pthread_cond_init, void *c, void *a) {
2258 void *ctx;
2259 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_init, c, a);
2260 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, c, pthread_cond_t_sz);
2261 return REAL(pthread_cond_init)(c, a);
2262 }
2263
2264 INTERCEPTOR(int, pthread_cond_signal, void *c) {
2265 void *ctx;
2266 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_signal, c);
2267 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, pthread_cond_t_sz);
2268 return REAL(pthread_cond_signal)(c);
2269 }
2270
2271 INTERCEPTOR(int, pthread_cond_broadcast, void *c) {
2272 void *ctx;
2273 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_broadcast, c);
2274 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, pthread_cond_t_sz);
2275 return REAL(pthread_cond_broadcast)(c);
2276 }
2277
2278 #define INIT_PTHREAD_COND_WAIT \
2279 INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2")
2280 #define INIT_PTHREAD_COND_INIT \
2281 INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2")
2282 #define INIT_PTHREAD_COND_SIGNAL \
2283 INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2")
2284 #define INIT_PTHREAD_COND_BROADCAST \
2285 INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2")
2286 #else
2287 #define INIT_PTHREAD_COND_WAIT
2288 #define INIT_PTHREAD_COND_INIT
2289 #define INIT_PTHREAD_COND_SIGNAL
2290 #define INIT_PTHREAD_COND_BROADCAST
2291 #endif
2292
2293 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
2294 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
2295 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
2296 if (mnt->mnt_fsname)
2297 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
2298 REAL(strlen)(mnt->mnt_fsname) + 1);
2299 if (mnt->mnt_dir)
2300 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
2301 REAL(strlen)(mnt->mnt_dir) + 1);
2302 if (mnt->mnt_type)
2303 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
2304 REAL(strlen)(mnt->mnt_type) + 1);
2305 if (mnt->mnt_opts)
2306 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
2307 REAL(strlen)(mnt->mnt_opts) + 1);
2308 }
2309 #endif
2310
2311 #if SANITIZER_INTERCEPT_GETMNTENT
2312 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
2313 void *ctx;
2314 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
2315 __sanitizer_mntent *res = REAL(getmntent)(fp);
2316 if (res) write_mntent(ctx, res);
2317 return res;
2318 }
2319 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
2320 #else
2321 #define INIT_GETMNTENT
2322 #endif
2323
2324 #if SANITIZER_INTERCEPT_GETMNTENT_R
2325 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
2326 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
2327 void *ctx;
2328 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
2329 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
2330 if (res) write_mntent(ctx, res);
2331 return res;
2332 }
2333 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
2334 #else
2335 #define INIT_GETMNTENT_R
2336 #endif
2337
2338 #if SANITIZER_INTERCEPT_STATFS
2339 INTERCEPTOR(int, statfs, char *path, void *buf) {
2340 void *ctx;
2341 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
2342 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2343 int res = REAL(statfs)(path, buf);
2344 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
2345 return res;
2346 }
2347 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
2348 void *ctx;
2349 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
2350 int res = REAL(fstatfs)(fd, buf);
2351 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
2352 return res;
2353 }
2354 #define INIT_STATFS \
2355 COMMON_INTERCEPT_FUNCTION(statfs); \
2356 COMMON_INTERCEPT_FUNCTION(fstatfs);
2357 #else
2358 #define INIT_STATFS
2359 #endif
2360
2361 #if SANITIZER_INTERCEPT_STATFS64
2362 INTERCEPTOR(int, statfs64, char *path, void *buf) {
2363 void *ctx;
2364 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
2365 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2366 int res = REAL(statfs64)(path, buf);
2367 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
2368 return res;
2369 }
2370 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
2371 void *ctx;
2372 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
2373 int res = REAL(fstatfs64)(fd, buf);
2374 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
2375 return res;
2376 }
2377 #define INIT_STATFS64 \
2378 COMMON_INTERCEPT_FUNCTION(statfs64); \
2379 COMMON_INTERCEPT_FUNCTION(fstatfs64);
2380 #else
2381 #define INIT_STATFS64
2382 #endif
2383
2384 #if SANITIZER_INTERCEPT_STATVFS
2385 INTERCEPTOR(int, statvfs, char *path, void *buf) {
2386 void *ctx;
2387 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
2388 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2389 int res = REAL(statvfs)(path, buf);
2390 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
2391 return res;
2392 }
2393 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
2394 void *ctx;
2395 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
2396 int res = REAL(fstatvfs)(fd, buf);
2397 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
2398 return res;
2399 }
2400 #define INIT_STATVFS \
2401 COMMON_INTERCEPT_FUNCTION(statvfs); \
2402 COMMON_INTERCEPT_FUNCTION(fstatvfs);
2403 #else
2404 #define INIT_STATVFS
2405 #endif
2406
2407 #if SANITIZER_INTERCEPT_STATVFS64
2408 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
2409 void *ctx;
2410 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
2411 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2412 int res = REAL(statvfs64)(path, buf);
2413 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
2414 return res;
2415 }
2416 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
2417 void *ctx;
2418 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
2419 int res = REAL(fstatvfs64)(fd, buf);
2420 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
2421 return res;
2422 }
2423 #define INIT_STATVFS64 \
2424 COMMON_INTERCEPT_FUNCTION(statvfs64); \
2425 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
2426 #else
2427 #define INIT_STATVFS64
2428 #endif
2429
2430 #if SANITIZER_INTERCEPT_INITGROUPS
2431 INTERCEPTOR(int, initgroups, char *user, u32 group) {
2432 void *ctx;
2433 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
2434 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
2435 int res = REAL(initgroups)(user, group);
2436 return res;
2437 }
2438 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
2439 #else
2440 #define INIT_INITGROUPS
2441 #endif
2442
2443 #if SANITIZER_INTERCEPT_ETHER
2444 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
2445 void *ctx;
2446 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
2447 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
2448 char *res = REAL(ether_ntoa)(addr);
2449 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2450 return res;
2451 }
2452 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
2453 void *ctx;
2454 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
2455 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2456 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
2457 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, sizeof(*res));
2458 return res;
2459 }
2460 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
2461 void *ctx;
2462 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
2463 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
2464 int res = REAL(ether_ntohost)(hostname, addr);
2465 if (!res && hostname)
2466 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
2467 return res;
2468 }
2469 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
2470 void *ctx;
2471 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
2472 if (hostname)
2473 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
2474 int res = REAL(ether_hostton)(hostname, addr);
2475 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
2476 return res;
2477 }
2478 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
2479 char *hostname) {
2480 void *ctx;
2481 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
2482 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
2483 int res = REAL(ether_line)(line, addr, hostname);
2484 if (!res) {
2485 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
2486 if (hostname)
2487 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
2488 }
2489 return res;
2490 }
2491 #define INIT_ETHER \
2492 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
2493 COMMON_INTERCEPT_FUNCTION(ether_aton); \
2494 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
2495 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
2496 COMMON_INTERCEPT_FUNCTION(ether_line);
2497 #else
2498 #define INIT_ETHER
2499 #endif
2500
2501 #if SANITIZER_INTERCEPT_ETHER_R
2502 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
2503 void *ctx;
2504 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
2505 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
2506 char *res = REAL(ether_ntoa_r)(addr, buf);
2507 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2508 return res;
2509 }
2510 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
2511 __sanitizer_ether_addr *addr) {
2512 void *ctx;
2513 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
2514 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2515 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
2516 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
2517 return res;
2518 }
2519 #define INIT_ETHER_R \
2520 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
2521 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
2522 #else
2523 #define INIT_ETHER_R
2524 #endif
2525
2526 #if SANITIZER_INTERCEPT_SHMCTL
2527 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
2528 void *ctx;
2529 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
2530 int res = REAL(shmctl)(shmid, cmd, buf);
2531 if (res >= 0) {
2532 unsigned sz = 0;
2533 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
2534 sz = sizeof(__sanitizer_shmid_ds);
2535 else if (cmd == shmctl_ipc_info)
2536 sz = struct_shminfo_sz;
2537 else if (cmd == shmctl_shm_info)
2538 sz = struct_shm_info_sz;
2539 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
2540 }
2541 return res;
2542 }
2543 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
2544 #else
2545 #define INIT_SHMCTL
2546 #endif
2547
2548 #if SANITIZER_INTERCEPT_RANDOM_R
2549 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
2550 void *ctx;
2551 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
2552 int res = REAL(random_r)(buf, result);
2553 if (!res && result)
2554 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2555 return res;
2556 }
2557 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
2558 #else
2559 #define INIT_RANDOM_R
2560 #endif
2561
2562 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
2563 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED
2564 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
2565 INTERCEPTOR(int, pthread_attr_get##what, void *attr, void *r) { \
2566 void *ctx; \
2567 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_get##what, attr, r); \
2568 int res = REAL(pthread_attr_get##what)(attr, r); \
2569 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
2570 return res; \
2571 }
2572 #endif
2573
2574 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
2575 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
2576 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
2577 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
2578 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
2579 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
2580 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
2581 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
2582 void *ctx;
2583 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
2584 int res = REAL(pthread_attr_getstack)(attr, addr, size);
2585 if (!res) {
2586 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
2587 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
2588 }
2589 return res;
2590 }
2591
2592 // We may need to call the real pthread_attr_getstack from the run-time
2593 // in sanitizer_common, but we don't want to include the interception headers
2594 // there. So, just define this function here.
2595 int __sanitizer_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
2596 return REAL(pthread_attr_getstack)(attr, addr, size);
2597 }
2598
2599 #define INIT_PTHREAD_ATTR_GET \
2600 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
2601 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
2602 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
2603 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
2604 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
2605 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
2606 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
2607 #else
2608 #define INIT_PTHREAD_ATTR_GET
2609 #endif
2610
2611 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
2612 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
2613
2614 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
2615 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
2616 #else
2617 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
2618 #endif
2619
2620 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
2621 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
2622 void *cpuset) {
2623 void *ctx;
2624 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
2625 cpuset);
2626 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
2627 if (!res && cpusetsize && cpuset)
2628 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
2629 return res;
2630 }
2631
2632 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
2633 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
2634 #else
2635 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
2636 #endif
2637
2638 #if SANITIZER_INTERCEPT_TMPNAM
2639 INTERCEPTOR(char *, tmpnam, char *s) {
2640 void *ctx;
2641 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
2642 char *res = REAL(tmpnam)(s);
2643 if (res) {
2644 if (s)
2645 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
2646 else
2647 COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2648 }
2649 return res;
2650 }
2651 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
2652 #else
2653 #define INIT_TMPNAM
2654 #endif
2655
2656 #if SANITIZER_INTERCEPT_TMPNAM_R
2657 INTERCEPTOR(char *, tmpnam_r, char *s) {
2658 void *ctx;
2659 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
2660 char *res = REAL(tmpnam_r)(s);
2661 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
2662 return res;
2663 }
2664 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
2665 #else
2666 #define INIT_TMPNAM_R
2667 #endif
2668
2669 #if SANITIZER_INTERCEPT_TEMPNAM
2670 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
2671 void *ctx;
2672 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
2673 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
2674 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
2675 char *res = REAL(tempnam)(dir, pfx);
2676 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2677 return res;
2678 }
2679 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
2680 #else
2681 #define INIT_TEMPNAM
2682 #endif
2683
2684 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
2685 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
2686 void *ctx;
2687 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
2688 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
2689 return REAL(pthread_setname_np)(thread, name);
2690 }
2691 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
2692 #else
2693 #define INIT_PTHREAD_SETNAME_NP
2694 #endif
2695
2696 #if SANITIZER_INTERCEPT_SINCOS
2697 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
2698 void *ctx;
2699 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
2700 REAL(sincos)(x, sin, cos);
2701 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
2702 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
2703 }
2704 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
2705 void *ctx;
2706 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
2707 REAL(sincosf)(x, sin, cos);
2708 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
2709 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
2710 }
2711 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
2712 void *ctx;
2713 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
2714 REAL(sincosl)(x, sin, cos);
2715 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
2716 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
2717 }
2718 #define INIT_SINCOS \
2719 COMMON_INTERCEPT_FUNCTION(sincos); \
2720 COMMON_INTERCEPT_FUNCTION(sincosf); \
2721 COMMON_INTERCEPT_FUNCTION(sincosl);
2722 #else
2723 #define INIT_SINCOS
2724 #endif
2725
2726 #if SANITIZER_INTERCEPT_REMQUO
2727 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
2728 void *ctx;
2729 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
2730 double res = REAL(remquo)(x, y, quo);
2731 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
2732 return res;
2733 }
2734 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
2735 void *ctx;
2736 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
2737 float res = REAL(remquof)(x, y, quo);
2738 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
2739 return res;
2740 }
2741 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
2742 void *ctx;
2743 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
2744 long double res = REAL(remquol)(x, y, quo);
2745 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
2746 return res;
2747 }
2748 #define INIT_REMQUO \
2749 COMMON_INTERCEPT_FUNCTION(remquo); \
2750 COMMON_INTERCEPT_FUNCTION(remquof); \
2751 COMMON_INTERCEPT_FUNCTION(remquol);
2752 #else
2753 #define INIT_REMQUO
2754 #endif
2755
2756 #if SANITIZER_INTERCEPT_LGAMMA
2757 extern int signgam;
2758 INTERCEPTOR(double, lgamma, double x) {
2759 void *ctx;
2760 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
2761 double res = REAL(lgamma)(x);
2762 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
2763 return res;
2764 }
2765 INTERCEPTOR(float, lgammaf, float x) {
2766 void *ctx;
2767 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
2768 float res = REAL(lgammaf)(x);
2769 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
2770 return res;
2771 }
2772 INTERCEPTOR(long double, lgammal, long double x) {
2773 void *ctx;
2774 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
2775 long double res = REAL(lgammal)(x);
2776 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
2777 return res;
2778 }
2779 #define INIT_LGAMMA \
2780 COMMON_INTERCEPT_FUNCTION(lgamma); \
2781 COMMON_INTERCEPT_FUNCTION(lgammaf); \
2782 COMMON_INTERCEPT_FUNCTION(lgammal);
2783 #else
2784 #define INIT_LGAMMA
2785 #endif
2786
2787 #if SANITIZER_INTERCEPT_LGAMMA_R
2788 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
2789 void *ctx;
2790 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
2791 double res = REAL(lgamma_r)(x, signp);
2792 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
2793 return res;
2794 }
2795 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
2796 void *ctx;
2797 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
2798 float res = REAL(lgammaf_r)(x, signp);
2799 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
2800 return res;
2801 }
2802 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
2803 void *ctx;
2804 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
2805 long double res = REAL(lgammal_r)(x, signp);
2806 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
2807 return res;
2808 }
2809 #define INIT_LGAMMA_R \
2810 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
2811 COMMON_INTERCEPT_FUNCTION(lgammaf_r); \
2812 COMMON_INTERCEPT_FUNCTION(lgammal_r);
2813 #else
2814 #define INIT_LGAMMA_R
2815 #endif
2816
2817 #if SANITIZER_INTERCEPT_DRAND48_R
2818 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
2819 void *ctx;
2820 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
2821 int res = REAL(drand48_r)(buffer, result);
2822 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2823 return res;
2824 }
2825 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
2826 void *ctx;
2827 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
2828 int res = REAL(lrand48_r)(buffer, result);
2829 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2830 return res;
2831 }
2832 #define INIT_DRAND48_R \
2833 COMMON_INTERCEPT_FUNCTION(drand48_r); \
2834 COMMON_INTERCEPT_FUNCTION(lrand48_r);
2835 #else
2836 #define INIT_DRAND48_R
2837 #endif
2838
2839 #if SANITIZER_INTERCEPT_GETLINE
2840 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
2841 void *ctx;
2842 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
2843 SSIZE_T res = REAL(getline)(lineptr, n, stream);
2844 if (res > 0) {
2845 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
2846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
2847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
2848 }
2849 return res;
2850 }
2851 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
2852 void *stream) {
2853 void *ctx;
2854 COMMON_INTERCEPTOR_ENTER(ctx, getdelim, lineptr, n, delim, stream);
2855 SSIZE_T res = REAL(getdelim)(lineptr, n, delim, stream);
2856 if (res > 0) {
2857 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
2858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
2859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
2860 }
2861 return res;
2862 }
2863 #define INIT_GETLINE \
2864 COMMON_INTERCEPT_FUNCTION(getline); \
2865 COMMON_INTERCEPT_FUNCTION(getdelim);
2866 #else
2867 #define INIT_GETLINE
2868 #endif
2869
2870 #if SANITIZER_INTERCEPT_ICONV
2871 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
2872 char **outbuf, SIZE_T *outbytesleft) {
2873 void *ctx;
2874 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
2875 outbytesleft);
2876 if (inbytesleft)
2877 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
2878 if (inbuf && inbytesleft)
2879 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
2880 if (outbytesleft)
2881 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
2882 void *outbuf_orig = outbuf ? *outbuf : 0;
2883 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
2884 if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
2885 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
2886 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
2887 }
2888 return res;
2889 }
2890 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
2891 #else
2892 #define INIT_ICONV
2893 #endif
2894
2895 #if SANITIZER_INTERCEPT_TIMES
2896 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
2897 void *ctx;
2898 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
2899 __sanitizer_clock_t res = REAL(times)(tms);
2900 if (res != (__sanitizer_clock_t)-1 && tms)
2901 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
2902 return res;
2903 }
2904 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
2905 #else
2906 #define INIT_TIMES
2907 #endif
2908
2909 #define SANITIZER_COMMON_INTERCEPTORS_INIT \
2910 INIT_TEXTDOMAIN; \
2911 INIT_STRCMP; \
2912 INIT_STRNCMP; \
2913 INIT_STRCASECMP; \
2914 INIT_STRNCASECMP; \
2915 INIT_READ; \
2916 INIT_PREAD; \
2917 INIT_PREAD64; \
2918 INIT_READV; \
2919 INIT_PREADV; \
2920 INIT_PREADV64; \
2921 INIT_WRITE; \
2922 INIT_PWRITE; \
2923 INIT_PWRITE64; \
2924 INIT_WRITEV; \
2925 INIT_PWRITEV; \
2926 INIT_PWRITEV64; \
2927 INIT_PRCTL; \
2928 INIT_LOCALTIME_AND_FRIENDS; \
2929 INIT_STRPTIME; \
2930 INIT_SCANF; \
2931 INIT_ISOC99_SCANF; \
2932 INIT_FREXP; \
2933 INIT_FREXPF_FREXPL; \
2934 INIT_GETPWNAM_AND_FRIENDS; \
2935 INIT_GETPWNAM_R_AND_FRIENDS; \
2936 INIT_CLOCK_GETTIME; \
2937 INIT_GETITIMER; \
2938 INIT_TIME; \
2939 INIT_GLOB; \
2940 INIT_WAIT; \
2941 INIT_INET; \
2942 INIT_PTHREAD_GETSCHEDPARAM; \
2943 INIT_GETADDRINFO; \
2944 INIT_GETNAMEINFO; \
2945 INIT_GETSOCKNAME; \
2946 INIT_GETHOSTBYNAME; \
2947 INIT_GETHOSTBYNAME_R; \
2948 INIT_GETSOCKOPT; \
2949 INIT_ACCEPT; \
2950 INIT_ACCEPT4; \
2951 INIT_MODF; \
2952 INIT_RECVMSG; \
2953 INIT_GETPEERNAME; \
2954 INIT_IOCTL; \
2955 INIT_INET_ATON; \
2956 INIT_SYSINFO; \
2957 INIT_READDIR; \
2958 INIT_READDIR64; \
2959 INIT_PTRACE; \
2960 INIT_SETLOCALE; \
2961 INIT_GETCWD; \
2962 INIT_GET_CURRENT_DIR_NAME; \
2963 INIT_STRTOIMAX; \
2964 INIT_MBSTOWCS; \
2965 INIT_MBSNRTOWCS; \
2966 INIT_WCSTOMBS; \
2967 INIT_WCSNRTOMBS; \
2968 INIT_TCGETATTR; \
2969 INIT_REALPATH; \
2970 INIT_CANONICALIZE_FILE_NAME; \
2971 INIT_CONFSTR; \
2972 INIT_SCHED_GETAFFINITY; \
2973 INIT_STRERROR; \
2974 INIT_STRERROR_R; \
2975 INIT_XPG_STRERROR_R; \
2976 INIT_SCANDIR; \
2977 INIT_SCANDIR64; \
2978 INIT_GETGROUPS; \
2979 INIT_POLL; \
2980 INIT_PPOLL; \
2981 INIT_WORDEXP; \
2982 INIT_SIGWAIT; \
2983 INIT_SIGWAITINFO; \
2984 INIT_SIGTIMEDWAIT; \
2985 INIT_SIGSETOPS; \
2986 INIT_SIGPENDING; \
2987 INIT_SIGPROCMASK; \
2988 INIT_BACKTRACE; \
2989 INIT__EXIT; \
2990 INIT_PTHREAD_MUTEX_LOCK; \
2991 INIT_PTHREAD_MUTEX_UNLOCK; \
2992 INIT_PTHREAD_COND_WAIT; \
2993 INIT_PTHREAD_COND_INIT; \
2994 INIT_PTHREAD_COND_SIGNAL; \
2995 INIT_PTHREAD_COND_BROADCAST; \
2996 INIT_GETMNTENT; \
2997 INIT_GETMNTENT_R; \
2998 INIT_STATFS; \
2999 INIT_STATFS64; \
3000 INIT_STATVFS; \
3001 INIT_STATVFS64; \
3002 INIT_INITGROUPS; \
3003 INIT_ETHER; \
3004 INIT_ETHER_R; \
3005 INIT_SHMCTL; \
3006 INIT_RANDOM_R; \
3007 INIT_PTHREAD_ATTR_GET; \
3008 INIT_PTHREAD_ATTR_GETINHERITSCHED; \
3009 INIT_PTHREAD_ATTR_GETAFFINITY_NP; \
3010 INIT_TMPNAM; \
3011 INIT_TMPNAM_R; \
3012 INIT_TEMPNAM; \
3013 INIT_PTHREAD_SETNAME_NP; \
3014 INIT_SINCOS; \
3015 INIT_REMQUO; \
3016 INIT_LGAMMA; \
3017 INIT_LGAMMA_R; \
3018 INIT_DRAND48_R; \
3019 INIT_GETLINE; \
3020 INIT_ICONV; \
3021 INIT_TIMES; \
3022 /**/