]> git.ipfire.org Git - thirdparty/gcc.git/blob - libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc
libsanitizer/
[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 its own macros:
13 // COMMON_INTERCEPTOR_ENTER
14 // COMMON_INTERCEPTOR_ENTER_NOIGNORE
15 // COMMON_INTERCEPTOR_READ_RANGE
16 // COMMON_INTERCEPTOR_WRITE_RANGE
17 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
18 // COMMON_INTERCEPTOR_DIR_ACQUIRE
19 // COMMON_INTERCEPTOR_FD_ACQUIRE
20 // COMMON_INTERCEPTOR_FD_RELEASE
21 // COMMON_INTERCEPTOR_FD_ACCESS
22 // COMMON_INTERCEPTOR_SET_THREAD_NAME
23 // COMMON_INTERCEPTOR_ON_DLOPEN
24 // COMMON_INTERCEPTOR_ON_EXIT
25 // COMMON_INTERCEPTOR_MUTEX_LOCK
26 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
27 // COMMON_INTERCEPTOR_MUTEX_REPAIR
28 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
29 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
30 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
31 //===----------------------------------------------------------------------===//
32
33 #include "interception/interception.h"
34 #include "sanitizer_addrhashmap.h"
35 #include "sanitizer_placement_new.h"
36 #include "sanitizer_platform_interceptors.h"
37 #include "sanitizer_tls_get_addr.h"
38
39 #include <stdarg.h>
40
41 #if SANITIZER_INTERCEPTOR_HOOKS
42 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) \
43 do { \
44 if (f) \
45 f(__VA_ARGS__); \
46 } while (false);
47 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
48 extern "C" { \
49 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void f(__VA_ARGS__); \
50 } // extern "C"
51 #else
52 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
53 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
54
55 #endif // SANITIZER_INTERCEPTOR_HOOKS
56
57 #if SANITIZER_WINDOWS && !defined(va_copy)
58 #define va_copy(dst, src) ((dst) = (src))
59 #endif // _WIN32
60
61 #if SANITIZER_FREEBSD
62 #define pthread_setname_np pthread_set_name_np
63 #define inet_aton __inet_aton
64 #define inet_pton __inet_pton
65 #define iconv __bsd_iconv
66 #endif
67
68 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
69 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
70 #endif
71
72 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
73 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
74 #endif
75
76 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
77 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
78 #endif
79
80 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
81 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
82 #endif
83
84 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
85 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
86 #endif
87
88 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
89 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
90 #endif
91
92 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
93 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
94 #endif
95
96 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
97 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
98 #endif
99
100 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
101 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
102 #endif
103
104 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
105 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
106 #endif
107
108 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
109 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
110 #endif
111
112 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
113 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
114 #endif
115
116 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
117 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
118 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
119 #endif
120
121 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
122 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
123 #endif
124
125 #define COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, n) \
126 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
127 common_flags()->strict_string_checks ? (len) + 1 : (n) )
128
129 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
130 COMMON_INTERCEPTOR_READ_STRING_OF_LEN((ctx), (s), REAL(strlen)(s), (n))
131
132 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
133 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) {}
134 #endif
135
136 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
137 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
138 #endif
139
140 #ifndef COMMON_INTERCEPTOR_ACQUIRE
141 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
142 #endif
143
144 #ifndef COMMON_INTERCEPTOR_RELEASE
145 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
146 #endif
147
148 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
149 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
150 #endif
151
152 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
153 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
154 #endif
155
156 #ifdef SANITIZER_NLDBL_VERSION
157 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
158 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
159 #else
160 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
161 COMMON_INTERCEPT_FUNCTION(fn)
162 #endif
163
164 struct FileMetadata {
165 // For open_memstream().
166 char **addr;
167 SIZE_T *size;
168 };
169
170 struct CommonInterceptorMetadata {
171 enum {
172 CIMT_INVALID = 0,
173 CIMT_FILE
174 } type;
175 union {
176 FileMetadata file;
177 };
178 };
179
180 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
181
182 static MetadataHashMap *interceptor_metadata_map;
183
184 #if SI_NOT_WINDOWS
185 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
186 const FileMetadata &file) {
187 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
188 CHECK(h.created());
189 h->type = CommonInterceptorMetadata::CIMT_FILE;
190 h->file = file;
191 }
192
193 UNUSED static const FileMetadata *GetInterceptorMetadata(
194 __sanitizer_FILE *addr) {
195 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
196 /* remove */ false,
197 /* create */ false);
198 if (h.exists()) {
199 CHECK(!h.created());
200 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
201 return &h->file;
202 } else {
203 return 0;
204 }
205 }
206
207 UNUSED static void DeleteInterceptorMetadata(void *addr) {
208 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
209 CHECK(h.exists());
210 }
211 #endif // SI_NOT_WINDOWS
212
213 #if SANITIZER_INTERCEPT_STRLEN
214 INTERCEPTOR(SIZE_T, strlen, const char *s) {
215 // Sometimes strlen is called prior to InitializeCommonInterceptors,
216 // in which case the REAL(strlen) typically used in
217 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
218 // to handle that.
219 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
220 return internal_strlen(s);
221 void *ctx;
222 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
223 SIZE_T result = REAL(strlen)(s);
224 if (common_flags()->intercept_strlen)
225 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
226 return result;
227 }
228 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
229 #else
230 #define INIT_STRLEN
231 #endif
232
233 #if SANITIZER_INTERCEPT_STRNLEN
234 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
235 void *ctx;
236 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
237 SIZE_T length = REAL(strnlen)(s, maxlen);
238 if (common_flags()->intercept_strlen)
239 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
240 return length;
241 }
242 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
243 #else
244 #define INIT_STRNLEN
245 #endif
246
247 #if SANITIZER_INTERCEPT_TEXTDOMAIN
248 INTERCEPTOR(char*, textdomain, const char *domainname) {
249 void *ctx;
250 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
251 COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
252 char *domain = REAL(textdomain)(domainname);
253 if (domain) {
254 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
255 }
256 return domain;
257 }
258 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
259 #else
260 #define INIT_TEXTDOMAIN
261 #endif
262
263 #if SANITIZER_INTERCEPT_STRCMP
264 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
265 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
266 }
267
268 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
269 const char *s1, const char *s2, int result)
270
271 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
272 void *ctx;
273 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
274 unsigned char c1, c2;
275 uptr i;
276 for (i = 0;; i++) {
277 c1 = (unsigned char)s1[i];
278 c2 = (unsigned char)s2[i];
279 if (c1 != c2 || c1 == '\0') break;
280 }
281 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
282 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
283 int result = CharCmpX(c1, c2);
284 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
285 s2, result);
286 return result;
287 }
288
289 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
290 const char *s1, const char *s2, uptr n,
291 int result)
292
293 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
294 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
295 return internal_strncmp(s1, s2, size);
296 void *ctx;
297 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
298 unsigned char c1 = 0, c2 = 0;
299 uptr i;
300 for (i = 0; i < size; i++) {
301 c1 = (unsigned char)s1[i];
302 c2 = (unsigned char)s2[i];
303 if (c1 != c2 || c1 == '\0') break;
304 }
305 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, Min(i + 1, size));
306 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, Min(i + 1, size));
307 int result = CharCmpX(c1, c2);
308 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
309 s2, size, result);
310 return result;
311 }
312
313 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
314 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
315 #else
316 #define INIT_STRCMP
317 #define INIT_STRNCMP
318 #endif
319
320 #if SANITIZER_INTERCEPT_STRCASECMP
321 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
322 int c1_low = ToLower(c1);
323 int c2_low = ToLower(c2);
324 return c1_low - c2_low;
325 }
326
327 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
328 const char *s1, const char *s2, int result)
329
330 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
331 void *ctx;
332 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
333 unsigned char c1 = 0, c2 = 0;
334 uptr i;
335 for (i = 0;; i++) {
336 c1 = (unsigned char)s1[i];
337 c2 = (unsigned char)s2[i];
338 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
339 }
340 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
341 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
342 int result = CharCaseCmp(c1, c2);
343 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
344 s1, s2, result);
345 return result;
346 }
347
348 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
349 const char *s1, const char *s2, uptr n,
350 int result)
351
352 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {
353 void *ctx;
354 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
355 unsigned char c1 = 0, c2 = 0;
356 uptr i;
357 for (i = 0; i < n; i++) {
358 c1 = (unsigned char)s1[i];
359 c2 = (unsigned char)s2[i];
360 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
361 }
362 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n));
363 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n));
364 int result = CharCaseCmp(c1, c2);
365 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
366 s1, s2, n, result);
367 return result;
368 }
369
370 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
371 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
372 #else
373 #define INIT_STRCASECMP
374 #define INIT_STRNCASECMP
375 #endif
376
377 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
378 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
379 const char *s2) {
380 uptr len1 = REAL(strlen)(s1);
381 uptr len2 = REAL(strlen)(s2);
382 COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s1, len1,
383 r ? r - s1 + len2 : len1 + 1);
384 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
385 }
386 #endif
387
388 #if SANITIZER_INTERCEPT_STRSTR
389
390 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
391 const char *s1, const char *s2, char *result)
392
393 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
394 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
395 return internal_strstr(s1, s2);
396 void *ctx;
397 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
398 char *r = REAL(strstr)(s1, s2);
399 if (common_flags()->intercept_strstr)
400 StrstrCheck(ctx, r, s1, s2);
401 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
402 s2, r);
403 return r;
404 }
405
406 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
407 #else
408 #define INIT_STRSTR
409 #endif
410
411 #if SANITIZER_INTERCEPT_STRCASESTR
412
413 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
414 const char *s1, const char *s2, char *result)
415
416 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
417 void *ctx;
418 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
419 char *r = REAL(strcasestr)(s1, s2);
420 if (common_flags()->intercept_strstr)
421 StrstrCheck(ctx, r, s1, s2);
422 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
423 s1, s2, r);
424 return r;
425 }
426
427 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
428 #else
429 #define INIT_STRCASESTR
430 #endif
431
432 #if SANITIZER_INTERCEPT_MEMMEM
433 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
434 const void *s1, SIZE_T len1, const void *s2,
435 SIZE_T len2, void *result)
436
437 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
438 SIZE_T len2) {
439 void *ctx;
440 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
441 void *r = REAL(memmem)(s1, len1, s2, len2);
442 if (common_flags()->intercept_memmem) {
443 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
444 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
445 }
446 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
447 s1, len1, s2, len2, r);
448 return r;
449 }
450
451 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
452 #else
453 #define INIT_MEMMEM
454 #endif // SANITIZER_INTERCEPT_MEMMEM
455
456 #if SANITIZER_INTERCEPT_STRCHR
457 INTERCEPTOR(char*, strchr, const char *s, int c) {
458 void *ctx;
459 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
460 return internal_strchr(s, c);
461 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
462 char *result = REAL(strchr)(s, c);
463 uptr len = internal_strlen(s);
464 uptr n = result ? result - s + 1 : len + 1;
465 if (common_flags()->intercept_strchr)
466 COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, n);
467 return result;
468 }
469 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
470 #else
471 #define INIT_STRCHR
472 #endif
473
474 #if SANITIZER_INTERCEPT_STRCHRNUL
475 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
476 void *ctx;
477 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
478 char *result = REAL(strchrnul)(s, c);
479 uptr len = result - s + 1;
480 if (common_flags()->intercept_strchr)
481 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
482 return result;
483 }
484 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
485 #else
486 #define INIT_STRCHRNUL
487 #endif
488
489 #if SANITIZER_INTERCEPT_STRRCHR
490 INTERCEPTOR(char*, strrchr, const char *s, int c) {
491 void *ctx;
492 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
493 return internal_strrchr(s, c);
494 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
495 uptr len = internal_strlen(s);
496 if (common_flags()->intercept_strchr)
497 COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, len + 1);
498 return REAL(strrchr)(s, c);
499 }
500 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
501 #else
502 #define INIT_STRRCHR
503 #endif
504
505 #if SANITIZER_INTERCEPT_STRSPN
506 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
507 void *ctx;
508 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
509 SIZE_T r = REAL(strspn)(s1, s2);
510 if (common_flags()->intercept_strspn) {
511 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
512 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
513 }
514 return r;
515 }
516
517 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
518 void *ctx;
519 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
520 SIZE_T r = REAL(strcspn)(s1, s2);
521 if (common_flags()->intercept_strspn) {
522 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
523 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
524 }
525 return r;
526 }
527
528 #define INIT_STRSPN \
529 COMMON_INTERCEPT_FUNCTION(strspn); \
530 COMMON_INTERCEPT_FUNCTION(strcspn);
531 #else
532 #define INIT_STRSPN
533 #endif
534
535 #if SANITIZER_INTERCEPT_STRPBRK
536 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
537 void *ctx;
538 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
539 char *r = REAL(strpbrk)(s1, s2);
540 if (common_flags()->intercept_strpbrk) {
541 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
542 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
543 r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
544 }
545 return r;
546 }
547
548 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
549 #else
550 #define INIT_STRPBRK
551 #endif
552
553 #if SANITIZER_INTERCEPT_MEMSET
554 INTERCEPTOR(void*, memset, void *dst, int v, uptr size) {
555 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
556 return internal_memset(dst, v, size);
557 void *ctx;
558 COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size);
559 if (common_flags()->intercept_intrin)
560 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);
561 return REAL(memset)(dst, v, size);
562 }
563
564 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
565 #else
566 #define INIT_MEMSET
567 #endif
568
569 #if SANITIZER_INTERCEPT_MEMMOVE
570 INTERCEPTOR(void*, memmove, void *dst, const void *src, uptr size) {
571 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
572 return internal_memmove(dst, src, size);
573 void *ctx;
574 COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size);
575 if (common_flags()->intercept_intrin) {
576 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);
577 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);
578 }
579 return REAL(memmove)(dst, src, size);
580 }
581
582 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
583 #else
584 #define INIT_MEMMOVE
585 #endif
586
587 #if SANITIZER_INTERCEPT_MEMCPY
588 INTERCEPTOR(void*, memcpy, void *dst, const void *src, uptr size) {
589 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) {
590 // On OS X, calling internal_memcpy here will cause memory corruptions,
591 // because memcpy and memmove are actually aliases of the same
592 // implementation. We need to use internal_memmove here.
593 return internal_memmove(dst, src, size);
594 }
595 void *ctx;
596 COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size);
597 if (common_flags()->intercept_intrin) {
598 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);
599 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);
600 }
601 // N.B.: If we switch this to internal_ we'll have to use internal_memmove
602 // due to memcpy being an alias of memmove on OS X.
603 return REAL(memcpy)(dst, src, size);
604 }
605
606 #define INIT_MEMCPY COMMON_INTERCEPT_FUNCTION(memcpy)
607 #else
608 #define INIT_MEMCPY
609 #endif
610
611 #if SANITIZER_INTERCEPT_MEMCMP
612
613 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
614 const void *s1, const void *s2, uptr n,
615 int result)
616
617 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
618 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
619 return internal_memcmp(a1, a2, size);
620 void *ctx;
621 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
622 if (common_flags()->intercept_memcmp) {
623 if (common_flags()->strict_memcmp) {
624 // Check the entire regions even if the first bytes of the buffers are
625 // different.
626 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
627 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
628 // Fallthrough to REAL(memcmp) below.
629 } else {
630 unsigned char c1 = 0, c2 = 0;
631 const unsigned char *s1 = (const unsigned char*)a1;
632 const unsigned char *s2 = (const unsigned char*)a2;
633 uptr i;
634 for (i = 0; i < size; i++) {
635 c1 = s1[i];
636 c2 = s2[i];
637 if (c1 != c2) break;
638 }
639 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
640 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
641 int r = CharCmpX(c1, c2);
642 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
643 a1, a2, size, r);
644 return r;
645 }
646 }
647 int result = REAL(memcmp(a1, a2, size));
648 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
649 a2, size, result);
650 return result;
651 }
652
653 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
654 #else
655 #define INIT_MEMCMP
656 #endif
657
658 #if SANITIZER_INTERCEPT_MEMCHR
659 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
660 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
661 return internal_memchr(s, c, n);
662 void *ctx;
663 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
664 #if SANITIZER_WINDOWS
665 void *res;
666 if (REAL(memchr)) {
667 res = REAL(memchr)(s, c, n);
668 } else {
669 res = internal_memchr(s, c, n);
670 }
671 #else
672 void *res = REAL(memchr)(s, c, n);
673 #endif
674 uptr len = res ? (char *)res - (const char *)s + 1 : n;
675 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
676 return res;
677 }
678
679 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
680 #else
681 #define INIT_MEMCHR
682 #endif
683
684 #if SANITIZER_INTERCEPT_MEMRCHR
685 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
686 void *ctx;
687 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
688 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
689 return REAL(memrchr)(s, c, n);
690 }
691
692 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
693 #else
694 #define INIT_MEMRCHR
695 #endif
696
697 #if SANITIZER_INTERCEPT_FREXP
698 INTERCEPTOR(double, frexp, double x, int *exp) {
699 void *ctx;
700 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
701 // Assuming frexp() always writes to |exp|.
702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
703 double res = REAL(frexp)(x, exp);
704 return res;
705 }
706
707 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
708 #else
709 #define INIT_FREXP
710 #endif // SANITIZER_INTERCEPT_FREXP
711
712 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
713 INTERCEPTOR(float, frexpf, float x, int *exp) {
714 void *ctx;
715 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
716 // FIXME: under ASan the call below may write to freed memory and corrupt
717 // its metadata. See
718 // https://github.com/google/sanitizers/issues/321.
719 float res = REAL(frexpf)(x, exp);
720 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
721 return res;
722 }
723
724 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
725 void *ctx;
726 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
727 // FIXME: under ASan the call below may write to freed memory and corrupt
728 // its metadata. See
729 // https://github.com/google/sanitizers/issues/321.
730 long double res = REAL(frexpl)(x, exp);
731 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
732 return res;
733 }
734
735 #define INIT_FREXPF_FREXPL \
736 COMMON_INTERCEPT_FUNCTION(frexpf); \
737 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
738 #else
739 #define INIT_FREXPF_FREXPL
740 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
741
742 #if SI_NOT_WINDOWS
743 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
744 SIZE_T iovlen, SIZE_T maxlen) {
745 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
746 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
747 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
748 maxlen -= sz;
749 }
750 }
751
752 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
753 SIZE_T iovlen, SIZE_T maxlen) {
754 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
755 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
756 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
757 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
758 maxlen -= sz;
759 }
760 }
761 #endif
762
763 #if SANITIZER_INTERCEPT_READ
764 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
765 void *ctx;
766 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
767 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
768 // FIXME: under ASan the call below may write to freed memory and corrupt
769 // its metadata. See
770 // https://github.com/google/sanitizers/issues/321.
771 SSIZE_T res = REAL(read)(fd, ptr, count);
772 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
773 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
774 return res;
775 }
776 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
777 #else
778 #define INIT_READ
779 #endif
780
781 #if SANITIZER_INTERCEPT_PREAD
782 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
783 void *ctx;
784 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
785 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
786 // FIXME: under ASan the call below may write to freed memory and corrupt
787 // its metadata. See
788 // https://github.com/google/sanitizers/issues/321.
789 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
790 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
791 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
792 return res;
793 }
794 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
795 #else
796 #define INIT_PREAD
797 #endif
798
799 #if SANITIZER_INTERCEPT_PREAD64
800 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
801 void *ctx;
802 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
803 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
804 // FIXME: under ASan the call below may write to freed memory and corrupt
805 // its metadata. See
806 // https://github.com/google/sanitizers/issues/321.
807 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
808 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
809 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
810 return res;
811 }
812 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
813 #else
814 #define INIT_PREAD64
815 #endif
816
817 #if SANITIZER_INTERCEPT_READV
818 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
819 int iovcnt) {
820 void *ctx;
821 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
822 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
823 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
824 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
825 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
826 return res;
827 }
828 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
829 #else
830 #define INIT_READV
831 #endif
832
833 #if SANITIZER_INTERCEPT_PREADV
834 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
835 OFF_T offset) {
836 void *ctx;
837 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
838 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
839 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
840 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
841 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
842 return res;
843 }
844 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
845 #else
846 #define INIT_PREADV
847 #endif
848
849 #if SANITIZER_INTERCEPT_PREADV64
850 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
851 OFF64_T offset) {
852 void *ctx;
853 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
854 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
855 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
856 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
857 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
858 return res;
859 }
860 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
861 #else
862 #define INIT_PREADV64
863 #endif
864
865 #if SANITIZER_INTERCEPT_WRITE
866 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
867 void *ctx;
868 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
869 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
870 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
871 SSIZE_T res = REAL(write)(fd, ptr, count);
872 // FIXME: this check should be _before_ the call to REAL(write), not after
873 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
874 return res;
875 }
876 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
877 #else
878 #define INIT_WRITE
879 #endif
880
881 #if SANITIZER_INTERCEPT_PWRITE
882 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
883 void *ctx;
884 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
885 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
886 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
887 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
888 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
889 return res;
890 }
891 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
892 #else
893 #define INIT_PWRITE
894 #endif
895
896 #if SANITIZER_INTERCEPT_PWRITE64
897 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
898 OFF64_T offset) {
899 void *ctx;
900 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
901 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
902 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
903 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
904 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
905 return res;
906 }
907 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
908 #else
909 #define INIT_PWRITE64
910 #endif
911
912 #if SANITIZER_INTERCEPT_WRITEV
913 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
914 int iovcnt) {
915 void *ctx;
916 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
917 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
918 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
919 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
920 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
921 return res;
922 }
923 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
924 #else
925 #define INIT_WRITEV
926 #endif
927
928 #if SANITIZER_INTERCEPT_PWRITEV
929 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
930 OFF_T offset) {
931 void *ctx;
932 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
933 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
934 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
935 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
936 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
937 return res;
938 }
939 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
940 #else
941 #define INIT_PWRITEV
942 #endif
943
944 #if SANITIZER_INTERCEPT_PWRITEV64
945 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
946 OFF64_T offset) {
947 void *ctx;
948 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
949 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
950 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
951 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
952 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
953 return res;
954 }
955 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
956 #else
957 #define INIT_PWRITEV64
958 #endif
959
960 #if SANITIZER_INTERCEPT_PRCTL
961 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
962 unsigned long arg3, // NOLINT
963 unsigned long arg4, unsigned long arg5) { // NOLINT
964 void *ctx;
965 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
966 static const int PR_SET_NAME = 15;
967 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
968 if (option == PR_SET_NAME) {
969 char buff[16];
970 internal_strncpy(buff, (char *)arg2, 15);
971 buff[15] = 0;
972 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
973 }
974 return res;
975 }
976 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
977 #else
978 #define INIT_PRCTL
979 #endif // SANITIZER_INTERCEPT_PRCTL
980
981 #if SANITIZER_INTERCEPT_TIME
982 INTERCEPTOR(unsigned long, time, unsigned long *t) {
983 void *ctx;
984 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
985 unsigned long local_t;
986 unsigned long res = REAL(time)(&local_t);
987 if (t && res != (unsigned long)-1) {
988 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
989 *t = local_t;
990 }
991 return res;
992 }
993 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
994 #else
995 #define INIT_TIME
996 #endif // SANITIZER_INTERCEPT_TIME
997
998 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
999 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1001 if (tm->tm_zone) {
1002 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1003 // can point to shared memory and tsan would report a data race.
1004 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1005 REAL(strlen(tm->tm_zone)) + 1);
1006 }
1007 }
1008 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1009 void *ctx;
1010 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1011 __sanitizer_tm *res = REAL(localtime)(timep);
1012 if (res) {
1013 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1014 unpoison_tm(ctx, res);
1015 }
1016 return res;
1017 }
1018 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1019 void *ctx;
1020 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1021 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1022 if (res) {
1023 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1024 unpoison_tm(ctx, res);
1025 }
1026 return res;
1027 }
1028 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1029 void *ctx;
1030 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1031 __sanitizer_tm *res = REAL(gmtime)(timep);
1032 if (res) {
1033 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1034 unpoison_tm(ctx, res);
1035 }
1036 return res;
1037 }
1038 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1039 void *ctx;
1040 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1041 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1042 if (res) {
1043 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1044 unpoison_tm(ctx, res);
1045 }
1046 return res;
1047 }
1048 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1049 void *ctx;
1050 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1051 // FIXME: under ASan the call below may write to freed memory and corrupt
1052 // its metadata. See
1053 // https://github.com/google/sanitizers/issues/321.
1054 char *res = REAL(ctime)(timep);
1055 if (res) {
1056 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1057 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1058 }
1059 return res;
1060 }
1061 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1062 void *ctx;
1063 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1064 // FIXME: under ASan the call below may write to freed memory and corrupt
1065 // its metadata. See
1066 // https://github.com/google/sanitizers/issues/321.
1067 char *res = REAL(ctime_r)(timep, result);
1068 if (res) {
1069 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1071 }
1072 return res;
1073 }
1074 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1075 void *ctx;
1076 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1077 // FIXME: under ASan the call below may write to freed memory and corrupt
1078 // its metadata. See
1079 // https://github.com/google/sanitizers/issues/321.
1080 char *res = REAL(asctime)(tm);
1081 if (res) {
1082 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1083 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1084 }
1085 return res;
1086 }
1087 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1088 void *ctx;
1089 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1090 // FIXME: under ASan the call below may write to freed memory and corrupt
1091 // its metadata. See
1092 // https://github.com/google/sanitizers/issues/321.
1093 char *res = REAL(asctime_r)(tm, result);
1094 if (res) {
1095 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1096 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1097 }
1098 return res;
1099 }
1100 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1101 void *ctx;
1102 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1103 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1104 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1105 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1106 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1107 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1108 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1109 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1110 long res = REAL(mktime)(tm);
1111 if (res != -1) unpoison_tm(ctx, tm);
1112 return res;
1113 }
1114 #define INIT_LOCALTIME_AND_FRIENDS \
1115 COMMON_INTERCEPT_FUNCTION(localtime); \
1116 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1117 COMMON_INTERCEPT_FUNCTION(gmtime); \
1118 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1119 COMMON_INTERCEPT_FUNCTION(ctime); \
1120 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1121 COMMON_INTERCEPT_FUNCTION(asctime); \
1122 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1123 COMMON_INTERCEPT_FUNCTION(mktime);
1124 #else
1125 #define INIT_LOCALTIME_AND_FRIENDS
1126 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1127
1128 #if SANITIZER_INTERCEPT_STRPTIME
1129 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1130 void *ctx;
1131 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1132 if (format)
1133 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1134 // FIXME: under ASan the call below may write to freed memory and corrupt
1135 // its metadata. See
1136 // https://github.com/google/sanitizers/issues/321.
1137 char *res = REAL(strptime)(s, format, tm);
1138 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1139 if (res && tm) {
1140 // Do not call unpoison_tm here, because strptime does not, in fact,
1141 // initialize the entire struct tm. For example, tm_zone pointer is left
1142 // uninitialized.
1143 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1144 }
1145 return res;
1146 }
1147 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1148 #else
1149 #define INIT_STRPTIME
1150 #endif
1151
1152 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1153 #include "sanitizer_common_interceptors_format.inc"
1154
1155 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1156 { \
1157 void *ctx; \
1158 va_list ap; \
1159 va_start(ap, format); \
1160 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1161 int res = WRAP(vname)(__VA_ARGS__, ap); \
1162 va_end(ap); \
1163 return res; \
1164 }
1165
1166 #endif
1167
1168 #if SANITIZER_INTERCEPT_SCANF
1169
1170 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1171 { \
1172 void *ctx; \
1173 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1174 va_list aq; \
1175 va_copy(aq, ap); \
1176 int res = REAL(vname)(__VA_ARGS__); \
1177 if (res > 0) \
1178 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1179 va_end(aq); \
1180 return res; \
1181 }
1182
1183 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1184 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1185
1186 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1187 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1188
1189 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1190 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1191
1192 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1193 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1194 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1195
1196 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1197 va_list ap)
1198 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1199
1200 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1201 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1202 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1203
1204 INTERCEPTOR(int, scanf, const char *format, ...)
1205 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1206
1207 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1208 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1209
1210 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1211 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1212
1213 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1214 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1215 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1216
1217 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1218 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1219
1220 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1221 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1222 #endif
1223
1224 #endif
1225
1226 #if SANITIZER_INTERCEPT_SCANF
1227 #define INIT_SCANF \
1228 COMMON_INTERCEPT_FUNCTION(scanf); \
1229 COMMON_INTERCEPT_FUNCTION(sscanf); \
1230 COMMON_INTERCEPT_FUNCTION(fscanf); \
1231 COMMON_INTERCEPT_FUNCTION(vscanf); \
1232 COMMON_INTERCEPT_FUNCTION(vsscanf); \
1233 COMMON_INTERCEPT_FUNCTION(vfscanf);
1234 #else
1235 #define INIT_SCANF
1236 #endif
1237
1238 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1239 #define INIT_ISOC99_SCANF \
1240 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1241 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1242 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1243 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1244 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1245 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1246 #else
1247 #define INIT_ISOC99_SCANF
1248 #endif
1249
1250 #if SANITIZER_INTERCEPT_PRINTF
1251
1252 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1253 void *ctx; \
1254 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1255 va_list aq; \
1256 va_copy(aq, ap);
1257
1258 #define VPRINTF_INTERCEPTOR_RETURN() \
1259 va_end(aq);
1260
1261 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1262 { \
1263 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1264 if (common_flags()->check_printf) \
1265 printf_common(ctx, format, aq); \
1266 int res = REAL(vname)(__VA_ARGS__); \
1267 VPRINTF_INTERCEPTOR_RETURN(); \
1268 return res; \
1269 }
1270
1271 // FIXME: under ASan the REAL() call below may write to freed memory and
1272 // corrupt its metadata. See
1273 // https://github.com/google/sanitizers/issues/321.
1274 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1275 { \
1276 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1277 if (common_flags()->check_printf) { \
1278 printf_common(ctx, format, aq); \
1279 } \
1280 int res = REAL(vname)(str, __VA_ARGS__); \
1281 if (res >= 0) { \
1282 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1283 } \
1284 VPRINTF_INTERCEPTOR_RETURN(); \
1285 return res; \
1286 }
1287
1288 // FIXME: under ASan the REAL() call below may write to freed memory and
1289 // corrupt its metadata. See
1290 // https://github.com/google/sanitizers/issues/321.
1291 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1292 { \
1293 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1294 if (common_flags()->check_printf) { \
1295 printf_common(ctx, format, aq); \
1296 } \
1297 int res = REAL(vname)(str, size, __VA_ARGS__); \
1298 if (res >= 0) { \
1299 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1300 } \
1301 VPRINTF_INTERCEPTOR_RETURN(); \
1302 return res; \
1303 }
1304
1305 // FIXME: under ASan the REAL() call below may write to freed memory and
1306 // corrupt its metadata. See
1307 // https://github.com/google/sanitizers/issues/321.
1308 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1309 { \
1310 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1311 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1312 if (common_flags()->check_printf) { \
1313 printf_common(ctx, format, aq); \
1314 } \
1315 int res = REAL(vname)(strp, __VA_ARGS__); \
1316 if (res >= 0) { \
1317 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1318 } \
1319 VPRINTF_INTERCEPTOR_RETURN(); \
1320 return res; \
1321 }
1322
1323 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1324 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1325
1326 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1327 va_list ap)
1328 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1329
1330 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1331 va_list ap)
1332 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1333
1334 #if SANITIZER_INTERCEPT_PRINTF_L
1335 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1336 const char *format, va_list ap)
1337 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1338
1339 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1340 const char *format, ...)
1341 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1342 #endif // SANITIZER_INTERCEPT_PRINTF_L
1343
1344 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1345 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1346
1347 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1348 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1349
1350 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1351 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1352 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1353
1354 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1355 const char *format, va_list ap)
1356 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1357
1358 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1359 va_list ap)
1360 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1361
1362 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1363 va_list ap)
1364 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1365 ap)
1366
1367 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1368
1369 INTERCEPTOR(int, printf, const char *format, ...)
1370 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1371
1372 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1373 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1374
1375 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1376 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1377
1378 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1379 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1380
1381 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1382 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1383
1384 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1385 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1386 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1387
1388 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1389 ...)
1390 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1391
1392 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1393 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1394
1395 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1396 const char *format, ...)
1397 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1398 format)
1399
1400 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1401
1402 #endif // SANITIZER_INTERCEPT_PRINTF
1403
1404 #if SANITIZER_INTERCEPT_PRINTF
1405 #define INIT_PRINTF \
1406 COMMON_INTERCEPT_FUNCTION(printf); \
1407 COMMON_INTERCEPT_FUNCTION(sprintf); \
1408 COMMON_INTERCEPT_FUNCTION(snprintf); \
1409 COMMON_INTERCEPT_FUNCTION(asprintf); \
1410 COMMON_INTERCEPT_FUNCTION(fprintf); \
1411 COMMON_INTERCEPT_FUNCTION(vprintf); \
1412 COMMON_INTERCEPT_FUNCTION(vsprintf); \
1413 COMMON_INTERCEPT_FUNCTION(vsnprintf); \
1414 COMMON_INTERCEPT_FUNCTION(vasprintf); \
1415 COMMON_INTERCEPT_FUNCTION(vfprintf);
1416 #else
1417 #define INIT_PRINTF
1418 #endif
1419
1420 #if SANITIZER_INTERCEPT_PRINTF_L
1421 #define INIT_PRINTF_L \
1422 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1423 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1424 #else
1425 #define INIT_PRINTF_L
1426 #endif
1427
1428 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1429 #define INIT_ISOC99_PRINTF \
1430 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1431 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1432 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1433 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1434 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1435 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1436 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1437 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1438 #else
1439 #define INIT_ISOC99_PRINTF
1440 #endif
1441
1442 #if SANITIZER_INTERCEPT_IOCTL
1443 #include "sanitizer_common_interceptors_ioctl.inc"
1444 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1445 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1446 // can trigger a report and we need to be able to unwind through this
1447 // function. On Mac in debug mode we might not have a frame pointer, because
1448 // ioctl_common_[pre|post] doesn't get inlined here.
1449 ENABLE_FRAME_POINTER;
1450
1451 void *ctx;
1452 va_list ap;
1453 va_start(ap, request);
1454 void *arg = va_arg(ap, void *);
1455 va_end(ap);
1456 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1457
1458 CHECK(ioctl_initialized);
1459
1460 // Note: TSan does not use common flags, and they are zero-initialized.
1461 // This effectively disables ioctl handling in TSan.
1462 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1463
1464 // Although request is unsigned long, the rest of the interceptor uses it
1465 // as just "unsigned" to save space, because we know that all values fit in
1466 // "unsigned" - they are compile-time constants.
1467
1468 const ioctl_desc *desc = ioctl_lookup(request);
1469 ioctl_desc decoded_desc;
1470 if (!desc) {
1471 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1472 if (!ioctl_decode(request, &decoded_desc))
1473 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1474 else
1475 desc = &decoded_desc;
1476 }
1477
1478 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1479 int res = REAL(ioctl)(d, request, arg);
1480 // FIXME: some ioctls have different return values for success and failure.
1481 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1482 return res;
1483 }
1484 #define INIT_IOCTL \
1485 ioctl_init(); \
1486 COMMON_INTERCEPT_FUNCTION(ioctl);
1487 #else
1488 #define INIT_IOCTL
1489 #endif
1490
1491 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1492 SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1493 SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1494 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1495 if (pwd) {
1496 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1497 if (pwd->pw_name)
1498 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1499 REAL(strlen)(pwd->pw_name) + 1);
1500 if (pwd->pw_passwd)
1501 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1502 REAL(strlen)(pwd->pw_passwd) + 1);
1503 #if !SANITIZER_ANDROID
1504 if (pwd->pw_gecos)
1505 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1506 REAL(strlen)(pwd->pw_gecos) + 1);
1507 #endif
1508 #if SANITIZER_MAC
1509 if (pwd->pw_class)
1510 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1511 REAL(strlen)(pwd->pw_class) + 1);
1512 #endif
1513 if (pwd->pw_dir)
1514 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1515 REAL(strlen)(pwd->pw_dir) + 1);
1516 if (pwd->pw_shell)
1517 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1518 REAL(strlen)(pwd->pw_shell) + 1);
1519 }
1520 }
1521
1522 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1523 if (grp) {
1524 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1525 if (grp->gr_name)
1526 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1527 REAL(strlen)(grp->gr_name) + 1);
1528 if (grp->gr_passwd)
1529 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1530 REAL(strlen)(grp->gr_passwd) + 1);
1531 char **p = grp->gr_mem;
1532 for (; *p; ++p) {
1533 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1534 }
1535 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1536 (p - grp->gr_mem + 1) * sizeof(*p));
1537 }
1538 }
1539 #endif // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1540 // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1541 // SANITIZER_INTERCEPT_GETPWENT_R ||
1542 // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1543
1544 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1545 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1546 void *ctx;
1547 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1548 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1549 __sanitizer_passwd *res = REAL(getpwnam)(name);
1550 if (res) unpoison_passwd(ctx, res);
1551 return res;
1552 }
1553 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1554 void *ctx;
1555 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1556 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1557 if (res) unpoison_passwd(ctx, res);
1558 return res;
1559 }
1560 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1561 void *ctx;
1562 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1563 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1564 __sanitizer_group *res = REAL(getgrnam)(name);
1565 if (res) unpoison_group(ctx, res);
1566 return res;
1567 }
1568 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1569 void *ctx;
1570 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1571 __sanitizer_group *res = REAL(getgrgid)(gid);
1572 if (res) unpoison_group(ctx, res);
1573 return res;
1574 }
1575 #define INIT_GETPWNAM_AND_FRIENDS \
1576 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1577 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1578 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1579 COMMON_INTERCEPT_FUNCTION(getgrgid);
1580 #else
1581 #define INIT_GETPWNAM_AND_FRIENDS
1582 #endif
1583
1584 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1585 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1586 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1587 void *ctx;
1588 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1589 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1590 // FIXME: under ASan the call below may write to freed memory and corrupt
1591 // its metadata. See
1592 // https://github.com/google/sanitizers/issues/321.
1593 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1594 if (!res) {
1595 if (result && *result) unpoison_passwd(ctx, *result);
1596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1597 }
1598 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1599 return res;
1600 }
1601 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1602 SIZE_T buflen, __sanitizer_passwd **result) {
1603 void *ctx;
1604 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1605 // FIXME: under ASan the call below may write to freed memory and corrupt
1606 // its metadata. See
1607 // https://github.com/google/sanitizers/issues/321.
1608 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1609 if (!res) {
1610 if (result && *result) unpoison_passwd(ctx, *result);
1611 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1612 }
1613 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1614 return res;
1615 }
1616 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1617 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1618 void *ctx;
1619 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1620 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1621 // FIXME: under ASan the call below may write to freed memory and corrupt
1622 // its metadata. See
1623 // https://github.com/google/sanitizers/issues/321.
1624 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1625 if (!res) {
1626 if (result && *result) unpoison_group(ctx, *result);
1627 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1628 }
1629 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1630 return res;
1631 }
1632 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1633 SIZE_T buflen, __sanitizer_group **result) {
1634 void *ctx;
1635 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1636 // FIXME: under ASan the call below may write to freed memory and corrupt
1637 // its metadata. See
1638 // https://github.com/google/sanitizers/issues/321.
1639 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1640 if (!res) {
1641 if (result && *result) unpoison_group(ctx, *result);
1642 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1643 }
1644 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1645 return res;
1646 }
1647 #define INIT_GETPWNAM_R_AND_FRIENDS \
1648 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1649 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1650 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1651 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1652 #else
1653 #define INIT_GETPWNAM_R_AND_FRIENDS
1654 #endif
1655
1656 #if SANITIZER_INTERCEPT_GETPWENT
1657 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1658 void *ctx;
1659 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1660 __sanitizer_passwd *res = REAL(getpwent)(dummy);
1661 if (res) unpoison_passwd(ctx, res);
1662 return res;
1663 }
1664 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1665 void *ctx;
1666 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1667 __sanitizer_group *res = REAL(getgrent)(dummy);
1668 if (res) unpoison_group(ctx, res);;
1669 return res;
1670 }
1671 #define INIT_GETPWENT \
1672 COMMON_INTERCEPT_FUNCTION(getpwent); \
1673 COMMON_INTERCEPT_FUNCTION(getgrent);
1674 #else
1675 #define INIT_GETPWENT
1676 #endif
1677
1678 #if SANITIZER_INTERCEPT_FGETPWENT
1679 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
1680 void *ctx;
1681 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
1682 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
1683 if (res) unpoison_passwd(ctx, res);
1684 return res;
1685 }
1686 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
1687 void *ctx;
1688 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
1689 __sanitizer_group *res = REAL(fgetgrent)(fp);
1690 if (res) unpoison_group(ctx, res);
1691 return res;
1692 }
1693 #define INIT_FGETPWENT \
1694 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
1695 COMMON_INTERCEPT_FUNCTION(fgetgrent);
1696 #else
1697 #define INIT_FGETPWENT
1698 #endif
1699
1700 #if SANITIZER_INTERCEPT_GETPWENT_R
1701 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
1702 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1703 void *ctx;
1704 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
1705 // FIXME: under ASan the call below may write to freed memory and corrupt
1706 // its metadata. See
1707 // https://github.com/google/sanitizers/issues/321.
1708 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
1709 if (!res) {
1710 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1711 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1712 }
1713 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1714 return res;
1715 }
1716 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
1717 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1718 void *ctx;
1719 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
1720 // FIXME: under ASan the call below may write to freed memory and corrupt
1721 // its metadata. See
1722 // https://github.com/google/sanitizers/issues/321.
1723 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
1724 if (!res) {
1725 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1726 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1727 }
1728 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1729 return res;
1730 }
1731 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
1732 __sanitizer_group **pwbufp) {
1733 void *ctx;
1734 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
1735 // FIXME: under ASan the call below may write to freed memory and corrupt
1736 // its metadata. See
1737 // https://github.com/google/sanitizers/issues/321.
1738 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
1739 if (!res) {
1740 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1741 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1742 }
1743 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1744 return res;
1745 }
1746 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
1747 SIZE_T buflen, __sanitizer_group **pwbufp) {
1748 void *ctx;
1749 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
1750 // FIXME: under ASan the call below may write to freed memory and corrupt
1751 // its metadata. See
1752 // https://github.com/google/sanitizers/issues/321.
1753 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
1754 if (!res) {
1755 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1756 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1757 }
1758 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1759 return res;
1760 }
1761 #define INIT_GETPWENT_R \
1762 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
1763 COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
1764 COMMON_INTERCEPT_FUNCTION(getgrent_r); \
1765 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
1766 #else
1767 #define INIT_GETPWENT_R
1768 #endif
1769
1770 #if SANITIZER_INTERCEPT_SETPWENT
1771 // The only thing these interceptors do is disable any nested interceptors.
1772 // These functions may open nss modules and call uninstrumented functions from
1773 // them, and we don't want things like strlen() to trigger.
1774 INTERCEPTOR(void, setpwent, int dummy) {
1775 void *ctx;
1776 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
1777 REAL(setpwent)(dummy);
1778 }
1779 INTERCEPTOR(void, endpwent, int dummy) {
1780 void *ctx;
1781 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
1782 REAL(endpwent)(dummy);
1783 }
1784 INTERCEPTOR(void, setgrent, int dummy) {
1785 void *ctx;
1786 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
1787 REAL(setgrent)(dummy);
1788 }
1789 INTERCEPTOR(void, endgrent, int dummy) {
1790 void *ctx;
1791 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
1792 REAL(endgrent)(dummy);
1793 }
1794 #define INIT_SETPWENT \
1795 COMMON_INTERCEPT_FUNCTION(setpwent); \
1796 COMMON_INTERCEPT_FUNCTION(endpwent); \
1797 COMMON_INTERCEPT_FUNCTION(setgrent); \
1798 COMMON_INTERCEPT_FUNCTION(endgrent);
1799 #else
1800 #define INIT_SETPWENT
1801 #endif
1802
1803 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
1804 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
1805 void *ctx;
1806 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
1807 // FIXME: under ASan the call below may write to freed memory and corrupt
1808 // its metadata. See
1809 // https://github.com/google/sanitizers/issues/321.
1810 int res = REAL(clock_getres)(clk_id, tp);
1811 if (!res && tp) {
1812 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1813 }
1814 return res;
1815 }
1816 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
1817 void *ctx;
1818 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
1819 // FIXME: under ASan the call below may write to freed memory and corrupt
1820 // its metadata. See
1821 // https://github.com/google/sanitizers/issues/321.
1822 int res = REAL(clock_gettime)(clk_id, tp);
1823 if (!res) {
1824 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1825 }
1826 return res;
1827 }
1828 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
1829 void *ctx;
1830 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
1831 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
1832 return REAL(clock_settime)(clk_id, tp);
1833 }
1834 #define INIT_CLOCK_GETTIME \
1835 COMMON_INTERCEPT_FUNCTION(clock_getres); \
1836 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
1837 COMMON_INTERCEPT_FUNCTION(clock_settime);
1838 #else
1839 #define INIT_CLOCK_GETTIME
1840 #endif
1841
1842 #if SANITIZER_INTERCEPT_GETITIMER
1843 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
1844 void *ctx;
1845 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
1846 // FIXME: under ASan the call below may write to freed memory and corrupt
1847 // its metadata. See
1848 // https://github.com/google/sanitizers/issues/321.
1849 int res = REAL(getitimer)(which, curr_value);
1850 if (!res && curr_value) {
1851 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
1852 }
1853 return res;
1854 }
1855 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
1856 void *ctx;
1857 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
1858 if (new_value)
1859 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
1860 // FIXME: under ASan the call below may write to freed memory and corrupt
1861 // its metadata. See
1862 // https://github.com/google/sanitizers/issues/321.
1863 int res = REAL(setitimer)(which, new_value, old_value);
1864 if (!res && old_value) {
1865 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
1866 }
1867 return res;
1868 }
1869 #define INIT_GETITIMER \
1870 COMMON_INTERCEPT_FUNCTION(getitimer); \
1871 COMMON_INTERCEPT_FUNCTION(setitimer);
1872 #else
1873 #define INIT_GETITIMER
1874 #endif
1875
1876 #if SANITIZER_INTERCEPT_GLOB
1877 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
1878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
1879 // +1 for NULL pointer at the end.
1880 if (pglob->gl_pathv)
1881 COMMON_INTERCEPTOR_WRITE_RANGE(
1882 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
1883 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
1884 char *p = pglob->gl_pathv[i];
1885 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
1886 }
1887 }
1888
1889 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
1890
1891 static void wrapped_gl_closedir(void *dir) {
1892 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1893 pglob_copy->gl_closedir(dir);
1894 }
1895
1896 static void *wrapped_gl_readdir(void *dir) {
1897 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1898 return pglob_copy->gl_readdir(dir);
1899 }
1900
1901 static void *wrapped_gl_opendir(const char *s) {
1902 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1903 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1904 return pglob_copy->gl_opendir(s);
1905 }
1906
1907 static int wrapped_gl_lstat(const char *s, void *st) {
1908 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1909 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1910 return pglob_copy->gl_lstat(s, st);
1911 }
1912
1913 static int wrapped_gl_stat(const char *s, void *st) {
1914 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1915 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1916 return pglob_copy->gl_stat(s, st);
1917 }
1918
1919 static const __sanitizer_glob_t kGlobCopy = {
1920 0, 0, 0,
1921 0, wrapped_gl_closedir, wrapped_gl_readdir,
1922 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
1923
1924 INTERCEPTOR(int, glob, const char *pattern, int flags,
1925 int (*errfunc)(const char *epath, int eerrno),
1926 __sanitizer_glob_t *pglob) {
1927 void *ctx;
1928 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
1929 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
1930 __sanitizer_glob_t glob_copy;
1931 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
1932 if (flags & glob_altdirfunc) {
1933 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1934 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1935 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1936 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1937 Swap(pglob->gl_stat, glob_copy.gl_stat);
1938 pglob_copy = &glob_copy;
1939 }
1940 int res = REAL(glob)(pattern, flags, errfunc, pglob);
1941 if (flags & glob_altdirfunc) {
1942 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1943 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1944 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1945 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1946 Swap(pglob->gl_stat, glob_copy.gl_stat);
1947 }
1948 pglob_copy = 0;
1949 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1950 return res;
1951 }
1952
1953 INTERCEPTOR(int, glob64, const char *pattern, int flags,
1954 int (*errfunc)(const char *epath, int eerrno),
1955 __sanitizer_glob_t *pglob) {
1956 void *ctx;
1957 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
1958 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
1959 __sanitizer_glob_t glob_copy;
1960 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
1961 if (flags & glob_altdirfunc) {
1962 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1963 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1964 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1965 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1966 Swap(pglob->gl_stat, glob_copy.gl_stat);
1967 pglob_copy = &glob_copy;
1968 }
1969 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
1970 if (flags & glob_altdirfunc) {
1971 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1972 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1973 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1974 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1975 Swap(pglob->gl_stat, glob_copy.gl_stat);
1976 }
1977 pglob_copy = 0;
1978 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1979 return res;
1980 }
1981 #define INIT_GLOB \
1982 COMMON_INTERCEPT_FUNCTION(glob); \
1983 COMMON_INTERCEPT_FUNCTION(glob64);
1984 #else // SANITIZER_INTERCEPT_GLOB
1985 #define INIT_GLOB
1986 #endif // SANITIZER_INTERCEPT_GLOB
1987
1988 #if SANITIZER_INTERCEPT_WAIT
1989 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
1990 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
1991 // details.
1992 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
1993 void *ctx;
1994 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
1995 // FIXME: under ASan the call below may write to freed memory and corrupt
1996 // its metadata. See
1997 // https://github.com/google/sanitizers/issues/321.
1998 int res = REAL(wait)(status);
1999 if (res != -1 && status)
2000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2001 return res;
2002 }
2003 // On FreeBSD id_t is always 64-bit wide.
2004 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2005 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2006 int options) {
2007 #else
2008 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2009 int options) {
2010 #endif
2011 void *ctx;
2012 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2013 // FIXME: under ASan the call below may write to freed memory and corrupt
2014 // its metadata. See
2015 // https://github.com/google/sanitizers/issues/321.
2016 int res = REAL(waitid)(idtype, id, infop, options);
2017 if (res != -1 && infop)
2018 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2019 return res;
2020 }
2021 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2022 void *ctx;
2023 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2024 // FIXME: under ASan the call below may write to freed memory and corrupt
2025 // its metadata. See
2026 // https://github.com/google/sanitizers/issues/321.
2027 int res = REAL(waitpid)(pid, status, options);
2028 if (res != -1 && status)
2029 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2030 return res;
2031 }
2032 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2033 void *ctx;
2034 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2035 // FIXME: under ASan the call below may write to freed memory and corrupt
2036 // its metadata. See
2037 // https://github.com/google/sanitizers/issues/321.
2038 int res = REAL(wait3)(status, options, rusage);
2039 if (res != -1) {
2040 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2041 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2042 }
2043 return res;
2044 }
2045 #if SANITIZER_ANDROID
2046 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2047 void *ctx;
2048 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2049 // FIXME: under ASan the call below may write to freed memory and corrupt
2050 // its metadata. See
2051 // https://github.com/google/sanitizers/issues/321.
2052 int res = REAL(__wait4)(pid, status, options, rusage);
2053 if (res != -1) {
2054 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2055 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2056 }
2057 return res;
2058 }
2059 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2060 #else
2061 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2062 void *ctx;
2063 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2064 // FIXME: under ASan the call below may write to freed memory and corrupt
2065 // its metadata. See
2066 // https://github.com/google/sanitizers/issues/321.
2067 int res = REAL(wait4)(pid, status, options, rusage);
2068 if (res != -1) {
2069 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2070 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2071 }
2072 return res;
2073 }
2074 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2075 #endif // SANITIZER_ANDROID
2076 #define INIT_WAIT \
2077 COMMON_INTERCEPT_FUNCTION(wait); \
2078 COMMON_INTERCEPT_FUNCTION(waitid); \
2079 COMMON_INTERCEPT_FUNCTION(waitpid); \
2080 COMMON_INTERCEPT_FUNCTION(wait3);
2081 #else
2082 #define INIT_WAIT
2083 #define INIT_WAIT4
2084 #endif
2085
2086 #if SANITIZER_INTERCEPT_INET
2087 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2088 void *ctx;
2089 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2090 uptr sz = __sanitizer_in_addr_sz(af);
2091 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2092 // FIXME: figure out read size based on the address family.
2093 // FIXME: under ASan the call below may write to freed memory and corrupt
2094 // its metadata. See
2095 // https://github.com/google/sanitizers/issues/321.
2096 char *res = REAL(inet_ntop)(af, src, dst, size);
2097 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2098 return res;
2099 }
2100 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2101 void *ctx;
2102 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2103 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2104 // FIXME: figure out read size based on the address family.
2105 // FIXME: under ASan the call below may write to freed memory and corrupt
2106 // its metadata. See
2107 // https://github.com/google/sanitizers/issues/321.
2108 int res = REAL(inet_pton)(af, src, dst);
2109 if (res == 1) {
2110 uptr sz = __sanitizer_in_addr_sz(af);
2111 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2112 }
2113 return res;
2114 }
2115 #define INIT_INET \
2116 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2117 COMMON_INTERCEPT_FUNCTION(inet_pton);
2118 #else
2119 #define INIT_INET
2120 #endif
2121
2122 #if SANITIZER_INTERCEPT_INET
2123 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2124 void *ctx;
2125 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2126 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2127 // FIXME: under ASan the call below may write to freed memory and corrupt
2128 // its metadata. See
2129 // https://github.com/google/sanitizers/issues/321.
2130 int res = REAL(inet_aton)(cp, dst);
2131 if (res != 0) {
2132 uptr sz = __sanitizer_in_addr_sz(af_inet);
2133 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2134 }
2135 return res;
2136 }
2137 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2138 #else
2139 #define INIT_INET_ATON
2140 #endif
2141
2142 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2143 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2144 void *ctx;
2145 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2146 // FIXME: under ASan the call below may write to freed memory and corrupt
2147 // its metadata. See
2148 // https://github.com/google/sanitizers/issues/321.
2149 int res = REAL(pthread_getschedparam)(thread, policy, param);
2150 if (res == 0) {
2151 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2152 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2153 }
2154 return res;
2155 }
2156 #define INIT_PTHREAD_GETSCHEDPARAM \
2157 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2158 #else
2159 #define INIT_PTHREAD_GETSCHEDPARAM
2160 #endif
2161
2162 #if SANITIZER_INTERCEPT_GETADDRINFO
2163 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2164 struct __sanitizer_addrinfo *hints,
2165 struct __sanitizer_addrinfo **out) {
2166 void *ctx;
2167 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2168 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2169 if (service)
2170 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2171 if (hints)
2172 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2173 // FIXME: under ASan the call below may write to freed memory and corrupt
2174 // its metadata. See
2175 // https://github.com/google/sanitizers/issues/321.
2176 int res = REAL(getaddrinfo)(node, service, hints, out);
2177 if (res == 0 && out) {
2178 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2179 struct __sanitizer_addrinfo *p = *out;
2180 while (p) {
2181 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2182 if (p->ai_addr)
2183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2184 if (p->ai_canonname)
2185 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2186 REAL(strlen)(p->ai_canonname) + 1);
2187 p = p->ai_next;
2188 }
2189 }
2190 return res;
2191 }
2192 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2193 #else
2194 #define INIT_GETADDRINFO
2195 #endif
2196
2197 #if SANITIZER_INTERCEPT_GETNAMEINFO
2198 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2199 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2200 void *ctx;
2201 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2202 serv, servlen, flags);
2203 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2204 // There is padding in in_addr that may make this too noisy
2205 // FIXME: under ASan the call below may write to freed memory and corrupt
2206 // its metadata. See
2207 // https://github.com/google/sanitizers/issues/321.
2208 int res =
2209 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2210 if (res == 0) {
2211 if (host && hostlen)
2212 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2213 if (serv && servlen)
2214 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2215 }
2216 return res;
2217 }
2218 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2219 #else
2220 #define INIT_GETNAMEINFO
2221 #endif
2222
2223 #if SANITIZER_INTERCEPT_GETSOCKNAME
2224 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2225 void *ctx;
2226 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2227 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2228 int addrlen_in = *addrlen;
2229 // FIXME: under ASan the call below may write to freed memory and corrupt
2230 // its metadata. See
2231 // https://github.com/google/sanitizers/issues/321.
2232 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2233 if (res == 0) {
2234 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2235 }
2236 return res;
2237 }
2238 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2239 #else
2240 #define INIT_GETSOCKNAME
2241 #endif
2242
2243 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2244 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2245 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2246 if (h->h_name)
2247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2248 char **p = h->h_aliases;
2249 while (*p) {
2250 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2251 ++p;
2252 }
2253 COMMON_INTERCEPTOR_WRITE_RANGE(
2254 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2255 p = h->h_addr_list;
2256 while (*p) {
2257 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2258 ++p;
2259 }
2260 COMMON_INTERCEPTOR_WRITE_RANGE(
2261 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2262 }
2263 #endif
2264
2265 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2266 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2267 void *ctx;
2268 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2269 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2270 if (res) write_hostent(ctx, res);
2271 return res;
2272 }
2273
2274 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2275 int type) {
2276 void *ctx;
2277 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2278 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2279 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2280 if (res) write_hostent(ctx, res);
2281 return res;
2282 }
2283
2284 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2285 void *ctx;
2286 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2287 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2288 if (res) write_hostent(ctx, res);
2289 return res;
2290 }
2291
2292 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2293 void *ctx;
2294 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2295 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2296 if (res) write_hostent(ctx, res);
2297 return res;
2298 }
2299 #define INIT_GETHOSTBYNAME \
2300 COMMON_INTERCEPT_FUNCTION(gethostent); \
2301 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2302 COMMON_INTERCEPT_FUNCTION(gethostbyname); \
2303 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2304 #else
2305 #define INIT_GETHOSTBYNAME
2306 #endif
2307
2308 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2309 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2310 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2311 int *h_errnop) {
2312 void *ctx;
2313 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2314 h_errnop);
2315 // FIXME: under ASan the call below may write to freed memory and corrupt
2316 // its metadata. See
2317 // https://github.com/google/sanitizers/issues/321.
2318 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2319 if (result) {
2320 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2321 if (res == 0 && *result) write_hostent(ctx, *result);
2322 }
2323 if (h_errnop)
2324 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2325 return res;
2326 }
2327 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2328 #else
2329 #define INIT_GETHOSTBYNAME_R
2330 #endif
2331
2332 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2333 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2334 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2335 void *ctx;
2336 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2337 h_errnop);
2338 // FIXME: under ASan the call below may write to freed memory and corrupt
2339 // its metadata. See
2340 // https://github.com/google/sanitizers/issues/321.
2341 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2342 if (result) {
2343 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2344 if (res == 0 && *result) write_hostent(ctx, *result);
2345 }
2346 if (h_errnop)
2347 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2348 return res;
2349 }
2350 #define INIT_GETHOSTENT_R \
2351 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2352 #else
2353 #define INIT_GETHOSTENT_R
2354 #endif
2355
2356 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2357 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2358 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2359 __sanitizer_hostent **result, int *h_errnop) {
2360 void *ctx;
2361 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2362 buflen, result, h_errnop);
2363 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2364 // FIXME: under ASan the call below may write to freed memory and corrupt
2365 // its metadata. See
2366 // https://github.com/google/sanitizers/issues/321.
2367 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2368 h_errnop);
2369 if (result) {
2370 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2371 if (res == 0 && *result) write_hostent(ctx, *result);
2372 }
2373 if (h_errnop)
2374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2375 return res;
2376 }
2377 #define INIT_GETHOSTBYADDR_R \
2378 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2379 #else
2380 #define INIT_GETHOSTBYADDR_R
2381 #endif
2382
2383 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2384 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2385 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2386 __sanitizer_hostent **result, int *h_errnop) {
2387 void *ctx;
2388 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2389 result, h_errnop);
2390 // FIXME: under ASan the call below may write to freed memory and corrupt
2391 // its metadata. See
2392 // https://github.com/google/sanitizers/issues/321.
2393 int res =
2394 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2395 if (result) {
2396 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2397 if (res == 0 && *result) write_hostent(ctx, *result);
2398 }
2399 if (h_errnop)
2400 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2401 return res;
2402 }
2403 #define INIT_GETHOSTBYNAME2_R \
2404 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2405 #else
2406 #define INIT_GETHOSTBYNAME2_R
2407 #endif
2408
2409 #if SANITIZER_INTERCEPT_GETSOCKOPT
2410 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2411 int *optlen) {
2412 void *ctx;
2413 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2414 optlen);
2415 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2416 // FIXME: under ASan the call below may write to freed memory and corrupt
2417 // its metadata. See
2418 // https://github.com/google/sanitizers/issues/321.
2419 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2420 if (res == 0)
2421 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2422 return res;
2423 }
2424 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2425 #else
2426 #define INIT_GETSOCKOPT
2427 #endif
2428
2429 #if SANITIZER_INTERCEPT_ACCEPT
2430 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2431 void *ctx;
2432 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2433 unsigned addrlen0 = 0;
2434 if (addrlen) {
2435 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2436 addrlen0 = *addrlen;
2437 }
2438 int fd2 = REAL(accept)(fd, addr, addrlen);
2439 if (fd2 >= 0) {
2440 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2441 if (addr && addrlen)
2442 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2443 }
2444 return fd2;
2445 }
2446 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2447 #else
2448 #define INIT_ACCEPT
2449 #endif
2450
2451 #if SANITIZER_INTERCEPT_ACCEPT4
2452 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2453 void *ctx;
2454 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2455 unsigned addrlen0 = 0;
2456 if (addrlen) {
2457 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2458 addrlen0 = *addrlen;
2459 }
2460 // FIXME: under ASan the call below may write to freed memory and corrupt
2461 // its metadata. See
2462 // https://github.com/google/sanitizers/issues/321.
2463 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2464 if (fd2 >= 0) {
2465 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2466 if (addr && addrlen)
2467 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2468 }
2469 return fd2;
2470 }
2471 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2472 #else
2473 #define INIT_ACCEPT4
2474 #endif
2475
2476 #if SANITIZER_INTERCEPT_MODF
2477 INTERCEPTOR(double, modf, double x, double *iptr) {
2478 void *ctx;
2479 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2480 // FIXME: under ASan the call below may write to freed memory and corrupt
2481 // its metadata. See
2482 // https://github.com/google/sanitizers/issues/321.
2483 double res = REAL(modf)(x, iptr);
2484 if (iptr) {
2485 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2486 }
2487 return res;
2488 }
2489 INTERCEPTOR(float, modff, float x, float *iptr) {
2490 void *ctx;
2491 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2492 // FIXME: under ASan the call below may write to freed memory and corrupt
2493 // its metadata. See
2494 // https://github.com/google/sanitizers/issues/321.
2495 float res = REAL(modff)(x, iptr);
2496 if (iptr) {
2497 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2498 }
2499 return res;
2500 }
2501 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2502 void *ctx;
2503 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2504 // FIXME: under ASan the call below may write to freed memory and corrupt
2505 // its metadata. See
2506 // https://github.com/google/sanitizers/issues/321.
2507 long double res = REAL(modfl)(x, iptr);
2508 if (iptr) {
2509 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2510 }
2511 return res;
2512 }
2513 #define INIT_MODF \
2514 COMMON_INTERCEPT_FUNCTION(modf); \
2515 COMMON_INTERCEPT_FUNCTION(modff); \
2516 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2517 #else
2518 #define INIT_MODF
2519 #endif
2520
2521 #if SANITIZER_INTERCEPT_RECVMSG
2522 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2523 SSIZE_T maxlen) {
2524 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2525 if (msg->msg_name && msg->msg_namelen)
2526 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2527 if (msg->msg_iov && msg->msg_iovlen)
2528 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2529 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2530 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2531 if (msg->msg_control && msg->msg_controllen)
2532 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2533 }
2534
2535 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2536 int flags) {
2537 void *ctx;
2538 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2539 // FIXME: under ASan the call below may write to freed memory and corrupt
2540 // its metadata. See
2541 // https://github.com/google/sanitizers/issues/321.
2542 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2543 if (res >= 0) {
2544 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2545 if (msg) {
2546 write_msghdr(ctx, msg, res);
2547 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2548 }
2549 }
2550 return res;
2551 }
2552 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2553 #else
2554 #define INIT_RECVMSG
2555 #endif
2556
2557 #if SANITIZER_INTERCEPT_SENDMSG
2558 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2559 const unsigned kCmsgDataOffset =
2560 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2561
2562 char *p = (char *)control;
2563 char *const control_end = p + controllen;
2564 while (true) {
2565 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
2566 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
2567 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
2568
2569 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
2570
2571 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
2572 sizeof(cmsg->cmsg_level));
2573 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
2574 sizeof(cmsg->cmsg_type));
2575
2576 if (cmsg->cmsg_len > kCmsgDataOffset) {
2577 char *data = p + kCmsgDataOffset;
2578 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
2579 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
2580 }
2581
2582 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
2583 }
2584 }
2585
2586 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2587 SSIZE_T maxlen) {
2588 #define R(f) \
2589 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
2590 R(name);
2591 R(namelen);
2592 R(iov);
2593 R(iovlen);
2594 R(control);
2595 R(controllen);
2596 R(flags);
2597 #undef R
2598 if (msg->msg_name && msg->msg_namelen)
2599 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2600 if (msg->msg_iov && msg->msg_iovlen)
2601 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
2602 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2603 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2604 if (msg->msg_control && msg->msg_controllen)
2605 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
2606 }
2607
2608 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
2609 int flags) {
2610 void *ctx;
2611 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
2612 if (fd >= 0) {
2613 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
2614 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
2615 }
2616 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
2617 if (common_flags()->intercept_send && res >= 0 && msg)
2618 read_msghdr(ctx, msg, res);
2619 return res;
2620 }
2621 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
2622 #else
2623 #define INIT_SENDMSG
2624 #endif
2625
2626 #if SANITIZER_INTERCEPT_GETPEERNAME
2627 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
2628 void *ctx;
2629 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
2630 unsigned addr_sz;
2631 if (addrlen) addr_sz = *addrlen;
2632 // FIXME: under ASan the call below may write to freed memory and corrupt
2633 // its metadata. See
2634 // https://github.com/google/sanitizers/issues/321.
2635 int res = REAL(getpeername)(sockfd, addr, addrlen);
2636 if (!res && addr && addrlen)
2637 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2638 return res;
2639 }
2640 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
2641 #else
2642 #define INIT_GETPEERNAME
2643 #endif
2644
2645 #if SANITIZER_INTERCEPT_SYSINFO
2646 INTERCEPTOR(int, sysinfo, void *info) {
2647 void *ctx;
2648 // FIXME: under ASan the call below may write to freed memory and corrupt
2649 // its metadata. See
2650 // https://github.com/google/sanitizers/issues/321.
2651 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
2652 int res = REAL(sysinfo)(info);
2653 if (!res && info)
2654 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
2655 return res;
2656 }
2657 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
2658 #else
2659 #define INIT_SYSINFO
2660 #endif
2661
2662 #if SANITIZER_INTERCEPT_READDIR
2663 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
2664 void *ctx;
2665 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
2666 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2667 __sanitizer_dirent *res = REAL(opendir)(path);
2668 if (res)
2669 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
2670 return res;
2671 }
2672
2673 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
2674 void *ctx;
2675 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
2676 // FIXME: under ASan the call below may write to freed memory and corrupt
2677 // its metadata. See
2678 // https://github.com/google/sanitizers/issues/321.
2679 __sanitizer_dirent *res = REAL(readdir)(dirp);
2680 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2681 return res;
2682 }
2683
2684 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
2685 __sanitizer_dirent **result) {
2686 void *ctx;
2687 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
2688 // FIXME: under ASan the call below may write to freed memory and corrupt
2689 // its metadata. See
2690 // https://github.com/google/sanitizers/issues/321.
2691 int res = REAL(readdir_r)(dirp, entry, result);
2692 if (!res) {
2693 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2694 if (*result)
2695 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2696 }
2697 return res;
2698 }
2699
2700 #define INIT_READDIR \
2701 COMMON_INTERCEPT_FUNCTION(opendir); \
2702 COMMON_INTERCEPT_FUNCTION(readdir); \
2703 COMMON_INTERCEPT_FUNCTION(readdir_r);
2704 #else
2705 #define INIT_READDIR
2706 #endif
2707
2708 #if SANITIZER_INTERCEPT_READDIR64
2709 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
2710 void *ctx;
2711 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
2712 // FIXME: under ASan the call below may write to freed memory and corrupt
2713 // its metadata. See
2714 // https://github.com/google/sanitizers/issues/321.
2715 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
2716 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2717 return res;
2718 }
2719
2720 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
2721 __sanitizer_dirent64 **result) {
2722 void *ctx;
2723 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
2724 // FIXME: under ASan the call below may write to freed memory and corrupt
2725 // its metadata. See
2726 // https://github.com/google/sanitizers/issues/321.
2727 int res = REAL(readdir64_r)(dirp, entry, result);
2728 if (!res) {
2729 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2730 if (*result)
2731 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2732 }
2733 return res;
2734 }
2735 #define INIT_READDIR64 \
2736 COMMON_INTERCEPT_FUNCTION(readdir64); \
2737 COMMON_INTERCEPT_FUNCTION(readdir64_r);
2738 #else
2739 #define INIT_READDIR64
2740 #endif
2741
2742 #if SANITIZER_INTERCEPT_PTRACE
2743 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
2744 void *ctx;
2745 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
2746 __sanitizer_iovec local_iovec;
2747
2748 if (data) {
2749 if (request == ptrace_setregs)
2750 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
2751 else if (request == ptrace_setfpregs)
2752 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2753 else if (request == ptrace_setfpxregs)
2754 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2755 else if (request == ptrace_setvfpregs)
2756 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2757 else if (request == ptrace_setsiginfo)
2758 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
2759 // Some kernel might zero the iovec::iov_base in case of invalid
2760 // write access. In this case copy the invalid address for further
2761 // inspection.
2762 else if (request == ptrace_setregset || request == ptrace_getregset) {
2763 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2764 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
2765 local_iovec = *iovec;
2766 if (request == ptrace_setregset)
2767 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
2768 }
2769 }
2770
2771 // FIXME: under ASan the call below may write to freed memory and corrupt
2772 // its metadata. See
2773 // https://github.com/google/sanitizers/issues/321.
2774 uptr res = REAL(ptrace)(request, pid, addr, data);
2775
2776 if (!res && data) {
2777 // Note that PEEK* requests assign different meaning to the return value.
2778 // This function does not handle them (nor does it need to).
2779 if (request == ptrace_getregs)
2780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
2781 else if (request == ptrace_getfpregs)
2782 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2783 else if (request == ptrace_getfpxregs)
2784 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2785 else if (request == ptrace_getvfpregs)
2786 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2787 else if (request == ptrace_getsiginfo)
2788 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
2789 else if (request == ptrace_geteventmsg)
2790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
2791 else if (request == ptrace_getregset) {
2792 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
2794 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
2795 local_iovec.iov_len);
2796 }
2797 }
2798 return res;
2799 }
2800
2801 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
2802 #else
2803 #define INIT_PTRACE
2804 #endif
2805
2806 #if SANITIZER_INTERCEPT_SETLOCALE
2807 INTERCEPTOR(char *, setlocale, int category, char *locale) {
2808 void *ctx;
2809 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
2810 if (locale)
2811 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
2812 char *res = REAL(setlocale)(category, locale);
2813 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2814 return res;
2815 }
2816
2817 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
2818 #else
2819 #define INIT_SETLOCALE
2820 #endif
2821
2822 #if SANITIZER_INTERCEPT_GETCWD
2823 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
2824 void *ctx;
2825 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
2826 // FIXME: under ASan the call below may write to freed memory and corrupt
2827 // its metadata. See
2828 // https://github.com/google/sanitizers/issues/321.
2829 char *res = REAL(getcwd)(buf, size);
2830 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2831 return res;
2832 }
2833 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
2834 #else
2835 #define INIT_GETCWD
2836 #endif
2837
2838 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
2839 INTERCEPTOR(char *, get_current_dir_name, int fake) {
2840 void *ctx;
2841 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
2842 // FIXME: under ASan the call below may write to freed memory and corrupt
2843 // its metadata. See
2844 // https://github.com/google/sanitizers/issues/321.
2845 char *res = REAL(get_current_dir_name)(fake);
2846 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2847 return res;
2848 }
2849
2850 #define INIT_GET_CURRENT_DIR_NAME \
2851 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
2852 #else
2853 #define INIT_GET_CURRENT_DIR_NAME
2854 #endif
2855
2856 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
2857 CHECK(endptr);
2858 if (nptr == *endptr) {
2859 // No digits were found at strtol call, we need to find out the last
2860 // symbol accessed by strtoll on our own.
2861 // We get this symbol by skipping leading blanks and optional +/- sign.
2862 while (IsSpace(*nptr)) nptr++;
2863 if (*nptr == '+' || *nptr == '-') nptr++;
2864 *endptr = const_cast<char *>(nptr);
2865 }
2866 CHECK(*endptr >= nptr);
2867 }
2868
2869 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
2870 char **endptr, char *real_endptr, int base) {
2871 if (endptr) {
2872 *endptr = real_endptr;
2873 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
2874 }
2875 // If base has unsupported value, strtol can exit with EINVAL
2876 // without reading any characters. So do additional checks only
2877 // if base is valid.
2878 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
2879 if (is_valid_base) {
2880 FixRealStrtolEndptr(nptr, &real_endptr);
2881 }
2882 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
2883 (real_endptr - nptr) + 1 : 0);
2884 }
2885
2886
2887 #if SANITIZER_INTERCEPT_STRTOIMAX
2888 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
2889 void *ctx;
2890 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
2891 // FIXME: under ASan the call below may write to freed memory and corrupt
2892 // its metadata. See
2893 // https://github.com/google/sanitizers/issues/321.
2894 char *real_endptr;
2895 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
2896 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
2897 return res;
2898 }
2899
2900 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
2901 void *ctx;
2902 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
2903 // FIXME: under ASan the call below may write to freed memory and corrupt
2904 // its metadata. See
2905 // https://github.com/google/sanitizers/issues/321.
2906 char *real_endptr;
2907 INTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
2908 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
2909 return res;
2910 }
2911
2912 #define INIT_STRTOIMAX \
2913 COMMON_INTERCEPT_FUNCTION(strtoimax); \
2914 COMMON_INTERCEPT_FUNCTION(strtoumax);
2915 #else
2916 #define INIT_STRTOIMAX
2917 #endif
2918
2919 #if SANITIZER_INTERCEPT_MBSTOWCS
2920 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
2921 void *ctx;
2922 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
2923 // FIXME: under ASan the call below may write to freed memory and corrupt
2924 // its metadata. See
2925 // https://github.com/google/sanitizers/issues/321.
2926 SIZE_T res = REAL(mbstowcs)(dest, src, len);
2927 if (res != (SIZE_T) - 1 && dest) {
2928 SIZE_T write_cnt = res + (res < len);
2929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2930 }
2931 return res;
2932 }
2933
2934 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
2935 void *ps) {
2936 void *ctx;
2937 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
2938 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2939 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2940 // FIXME: under ASan the call below may write to freed memory and corrupt
2941 // its metadata. See
2942 // https://github.com/google/sanitizers/issues/321.
2943 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
2944 if (res != (SIZE_T)(-1) && dest && src) {
2945 // This function, and several others, may or may not write the terminating
2946 // \0 character. They write it iff they clear *src.
2947 SIZE_T write_cnt = res + !*src;
2948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2949 }
2950 return res;
2951 }
2952
2953 #define INIT_MBSTOWCS \
2954 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
2955 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
2956 #else
2957 #define INIT_MBSTOWCS
2958 #endif
2959
2960 #if SANITIZER_INTERCEPT_MBSNRTOWCS
2961 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
2962 SIZE_T len, void *ps) {
2963 void *ctx;
2964 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
2965 if (src) {
2966 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2967 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
2968 }
2969 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2970 // FIXME: under ASan the call below may write to freed memory and corrupt
2971 // its metadata. See
2972 // https://github.com/google/sanitizers/issues/321.
2973 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
2974 if (res != (SIZE_T)(-1) && dest && src) {
2975 SIZE_T write_cnt = res + !*src;
2976 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2977 }
2978 return res;
2979 }
2980
2981 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
2982 #else
2983 #define INIT_MBSNRTOWCS
2984 #endif
2985
2986 #if SANITIZER_INTERCEPT_WCSTOMBS
2987 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
2988 void *ctx;
2989 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
2990 // FIXME: under ASan the call below may write to freed memory and corrupt
2991 // its metadata. See
2992 // https://github.com/google/sanitizers/issues/321.
2993 SIZE_T res = REAL(wcstombs)(dest, src, len);
2994 if (res != (SIZE_T) - 1 && dest) {
2995 SIZE_T write_cnt = res + (res < len);
2996 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
2997 }
2998 return res;
2999 }
3000
3001 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3002 void *ps) {
3003 void *ctx;
3004 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3005 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3006 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3007 // FIXME: under ASan the call below may write to freed memory and corrupt
3008 // its metadata. See
3009 // https://github.com/google/sanitizers/issues/321.
3010 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3011 if (res != (SIZE_T) - 1 && dest && src) {
3012 SIZE_T write_cnt = res + !*src;
3013 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3014 }
3015 return res;
3016 }
3017
3018 #define INIT_WCSTOMBS \
3019 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3020 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3021 #else
3022 #define INIT_WCSTOMBS
3023 #endif
3024
3025 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3026 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3027 SIZE_T len, void *ps) {
3028 void *ctx;
3029 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3030 if (src) {
3031 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3032 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3033 }
3034 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3035 // FIXME: under ASan the call below may write to freed memory and corrupt
3036 // its metadata. See
3037 // https://github.com/google/sanitizers/issues/321.
3038 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3039 if (res != ((SIZE_T)-1) && dest && src) {
3040 SIZE_T write_cnt = res + !*src;
3041 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3042 }
3043 return res;
3044 }
3045
3046 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3047 #else
3048 #define INIT_WCSNRTOMBS
3049 #endif
3050
3051
3052 #if SANITIZER_INTERCEPT_WCRTOMB
3053 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3054 void *ctx;
3055 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3056 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3057 // FIXME: under ASan the call below may write to freed memory and corrupt
3058 // its metadata. See
3059 // https://github.com/google/sanitizers/issues/321.
3060 SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3061 if (res != ((SIZE_T)-1) && dest) {
3062 SIZE_T write_cnt = res;
3063 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3064 }
3065 return res;
3066 }
3067
3068 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3069 #else
3070 #define INIT_WCRTOMB
3071 #endif
3072
3073 #if SANITIZER_INTERCEPT_TCGETATTR
3074 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3075 void *ctx;
3076 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3077 // FIXME: under ASan the call below may write to freed memory and corrupt
3078 // its metadata. See
3079 // https://github.com/google/sanitizers/issues/321.
3080 int res = REAL(tcgetattr)(fd, termios_p);
3081 if (!res && termios_p)
3082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3083 return res;
3084 }
3085
3086 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3087 #else
3088 #define INIT_TCGETATTR
3089 #endif
3090
3091 #if SANITIZER_INTERCEPT_REALPATH
3092 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3093 void *ctx;
3094 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3095 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3096
3097 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3098 // version of a versioned symbol. For realpath(), this gives us something
3099 // (called __old_realpath) that does not handle NULL in the second argument.
3100 // Handle it as part of the interceptor.
3101 char *allocated_path = nullptr;
3102 if (!resolved_path)
3103 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3104
3105 char *res = REAL(realpath)(path, resolved_path);
3106 if (allocated_path && !res) WRAP(free)(allocated_path);
3107 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3108 return res;
3109 }
3110 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3111 #else
3112 #define INIT_REALPATH
3113 #endif
3114
3115 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3116 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3117 void *ctx;
3118 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3119 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3120 char *res = REAL(canonicalize_file_name)(path);
3121 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3122 return res;
3123 }
3124 #define INIT_CANONICALIZE_FILE_NAME \
3125 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3126 #else
3127 #define INIT_CANONICALIZE_FILE_NAME
3128 #endif
3129
3130 #if SANITIZER_INTERCEPT_CONFSTR
3131 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3132 void *ctx;
3133 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3134 // FIXME: under ASan the call below may write to freed memory and corrupt
3135 // its metadata. See
3136 // https://github.com/google/sanitizers/issues/321.
3137 SIZE_T res = REAL(confstr)(name, buf, len);
3138 if (buf && res)
3139 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3140 return res;
3141 }
3142 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3143 #else
3144 #define INIT_CONFSTR
3145 #endif
3146
3147 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3148 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3149 void *ctx;
3150 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3151 // FIXME: under ASan the call below may write to freed memory and corrupt
3152 // its metadata. See
3153 // https://github.com/google/sanitizers/issues/321.
3154 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3155 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3156 return res;
3157 }
3158 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3159 #else
3160 #define INIT_SCHED_GETAFFINITY
3161 #endif
3162
3163 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3164 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3165 void *ctx;
3166 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3167 int res = REAL(sched_getparam)(pid, param);
3168 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3169 return res;
3170 }
3171 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3172 #else
3173 #define INIT_SCHED_GETPARAM
3174 #endif
3175
3176 #if SANITIZER_INTERCEPT_STRERROR
3177 INTERCEPTOR(char *, strerror, int errnum) {
3178 void *ctx;
3179 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3180 char *res = REAL(strerror)(errnum);
3181 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3182 return res;
3183 }
3184 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3185 #else
3186 #define INIT_STRERROR
3187 #endif
3188
3189 #if SANITIZER_INTERCEPT_STRERROR_R
3190 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3191 void *ctx;
3192 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3193 // FIXME: under ASan the call below may write to freed memory and corrupt
3194 // its metadata. See
3195 // https://github.com/google/sanitizers/issues/321.
3196 char *res = REAL(strerror_r)(errnum, buf, buflen);
3197 // There are 2 versions of strerror_r:
3198 // * POSIX version returns 0 on success, negative error code on failure,
3199 // writes message to buf.
3200 // * GNU version returns message pointer, which points to either buf or some
3201 // static storage.
3202 SIZE_T posix_res = (SIZE_T)res;
3203 if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
3204 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3205 // At least on OSX, buf contents are valid even when the call fails.
3206 SIZE_T sz = internal_strnlen(buf, buflen);
3207 if (sz < buflen) ++sz;
3208 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3209 } else {
3210 // GNU version.
3211 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3212 }
3213 return res;
3214 }
3215 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3216 #else
3217 #define INIT_STRERROR_R
3218 #endif
3219
3220 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3221 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3222 void *ctx;
3223 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3224 // FIXME: under ASan the call below may write to freed memory and corrupt
3225 // its metadata. See
3226 // https://github.com/google/sanitizers/issues/321.
3227 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3228 // This version always returns a null-terminated string.
3229 if (buf && buflen)
3230 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3231 return res;
3232 }
3233 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3234 #else
3235 #define INIT_XPG_STRERROR_R
3236 #endif
3237
3238 #if SANITIZER_INTERCEPT_SCANDIR
3239 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3240 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3241 const struct __sanitizer_dirent **);
3242
3243 static THREADLOCAL scandir_filter_f scandir_filter;
3244 static THREADLOCAL scandir_compar_f scandir_compar;
3245
3246 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3247 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3248 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3249 return scandir_filter(dir);
3250 }
3251
3252 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3253 const struct __sanitizer_dirent **b) {
3254 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3255 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3256 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3257 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3258 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3259 return scandir_compar(a, b);
3260 }
3261
3262 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3263 scandir_filter_f filter, scandir_compar_f compar) {
3264 void *ctx;
3265 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3266 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3267 scandir_filter = filter;
3268 scandir_compar = compar;
3269 // FIXME: under ASan the call below may write to freed memory and corrupt
3270 // its metadata. See
3271 // https://github.com/google/sanitizers/issues/321.
3272 int res = REAL(scandir)(dirp, namelist,
3273 filter ? wrapped_scandir_filter : nullptr,
3274 compar ? wrapped_scandir_compar : nullptr);
3275 scandir_filter = nullptr;
3276 scandir_compar = nullptr;
3277 if (namelist && res > 0) {
3278 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3279 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3280 for (int i = 0; i < res; ++i)
3281 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3282 (*namelist)[i]->d_reclen);
3283 }
3284 return res;
3285 }
3286 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3287 #else
3288 #define INIT_SCANDIR
3289 #endif
3290
3291 #if SANITIZER_INTERCEPT_SCANDIR64
3292 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3293 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3294 const struct __sanitizer_dirent64 **);
3295
3296 static THREADLOCAL scandir64_filter_f scandir64_filter;
3297 static THREADLOCAL scandir64_compar_f scandir64_compar;
3298
3299 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3300 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3301 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3302 return scandir64_filter(dir);
3303 }
3304
3305 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3306 const struct __sanitizer_dirent64 **b) {
3307 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3308 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3309 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3310 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3311 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3312 return scandir64_compar(a, b);
3313 }
3314
3315 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3316 scandir64_filter_f filter, scandir64_compar_f compar) {
3317 void *ctx;
3318 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3319 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3320 scandir64_filter = filter;
3321 scandir64_compar = compar;
3322 // FIXME: under ASan the call below may write to freed memory and corrupt
3323 // its metadata. See
3324 // https://github.com/google/sanitizers/issues/321.
3325 int res =
3326 REAL(scandir64)(dirp, namelist,
3327 filter ? wrapped_scandir64_filter : nullptr,
3328 compar ? wrapped_scandir64_compar : nullptr);
3329 scandir64_filter = nullptr;
3330 scandir64_compar = nullptr;
3331 if (namelist && res > 0) {
3332 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3333 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3334 for (int i = 0; i < res; ++i)
3335 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3336 (*namelist)[i]->d_reclen);
3337 }
3338 return res;
3339 }
3340 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3341 #else
3342 #define INIT_SCANDIR64
3343 #endif
3344
3345 #if SANITIZER_INTERCEPT_GETGROUPS
3346 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3347 void *ctx;
3348 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3349 // FIXME: under ASan the call below may write to freed memory and corrupt
3350 // its metadata. See
3351 // https://github.com/google/sanitizers/issues/321.
3352 int res = REAL(getgroups)(size, lst);
3353 if (res && lst) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3354 return res;
3355 }
3356 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3357 #else
3358 #define INIT_GETGROUPS
3359 #endif
3360
3361 #if SANITIZER_INTERCEPT_POLL
3362 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3363 __sanitizer_nfds_t nfds) {
3364 for (unsigned i = 0; i < nfds; ++i) {
3365 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3366 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3367 }
3368 }
3369
3370 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3371 __sanitizer_nfds_t nfds) {
3372 for (unsigned i = 0; i < nfds; ++i)
3373 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3374 sizeof(fds[i].revents));
3375 }
3376
3377 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3378 int timeout) {
3379 void *ctx;
3380 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3381 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3382 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3383 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3384 return res;
3385 }
3386 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3387 #else
3388 #define INIT_POLL
3389 #endif
3390
3391 #if SANITIZER_INTERCEPT_PPOLL
3392 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3393 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3394 void *ctx;
3395 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3396 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3397 if (timeout_ts)
3398 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3399 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
3400 int res =
3401 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3402 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3403 return res;
3404 }
3405 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3406 #else
3407 #define INIT_PPOLL
3408 #endif
3409
3410 #if SANITIZER_INTERCEPT_WORDEXP
3411 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3412 void *ctx;
3413 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3414 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3415 // FIXME: under ASan the call below may write to freed memory and corrupt
3416 // its metadata. See
3417 // https://github.com/google/sanitizers/issues/321.
3418 int res = REAL(wordexp)(s, p, flags);
3419 if (!res && p) {
3420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3421 if (p->we_wordc)
3422 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3423 sizeof(*p->we_wordv) * p->we_wordc);
3424 for (uptr i = 0; i < p->we_wordc; ++i) {
3425 char *w = p->we_wordv[i];
3426 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3427 }
3428 }
3429 return res;
3430 }
3431 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3432 #else
3433 #define INIT_WORDEXP
3434 #endif
3435
3436 #if SANITIZER_INTERCEPT_SIGWAIT
3437 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3438 void *ctx;
3439 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3440 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3441 // FIXME: under ASan the call below may write to freed memory and corrupt
3442 // its metadata. See
3443 // https://github.com/google/sanitizers/issues/321.
3444 int res = REAL(sigwait)(set, sig);
3445 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3446 return res;
3447 }
3448 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3449 #else
3450 #define INIT_SIGWAIT
3451 #endif
3452
3453 #if SANITIZER_INTERCEPT_SIGWAITINFO
3454 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3455 void *ctx;
3456 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3457 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3458 // FIXME: under ASan the call below may write to freed memory and corrupt
3459 // its metadata. See
3460 // https://github.com/google/sanitizers/issues/321.
3461 int res = REAL(sigwaitinfo)(set, info);
3462 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3463 return res;
3464 }
3465 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3466 #else
3467 #define INIT_SIGWAITINFO
3468 #endif
3469
3470 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3471 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3472 void *timeout) {
3473 void *ctx;
3474 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3475 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3476 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3477 // FIXME: under ASan the call below may write to freed memory and corrupt
3478 // its metadata. See
3479 // https://github.com/google/sanitizers/issues/321.
3480 int res = REAL(sigtimedwait)(set, info, timeout);
3481 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3482 return res;
3483 }
3484 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3485 #else
3486 #define INIT_SIGTIMEDWAIT
3487 #endif
3488
3489 #if SANITIZER_INTERCEPT_SIGSETOPS
3490 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3491 void *ctx;
3492 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3493 // FIXME: under ASan the call below may write to freed memory and corrupt
3494 // its metadata. See
3495 // https://github.com/google/sanitizers/issues/321.
3496 int res = REAL(sigemptyset)(set);
3497 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3498 return res;
3499 }
3500
3501 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3502 void *ctx;
3503 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3504 // FIXME: under ASan the call below may write to freed memory and corrupt
3505 // its metadata. See
3506 // https://github.com/google/sanitizers/issues/321.
3507 int res = REAL(sigfillset)(set);
3508 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3509 return res;
3510 }
3511 #define INIT_SIGSETOPS \
3512 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3513 COMMON_INTERCEPT_FUNCTION(sigfillset);
3514 #else
3515 #define INIT_SIGSETOPS
3516 #endif
3517
3518 #if SANITIZER_INTERCEPT_SIGPENDING
3519 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3520 void *ctx;
3521 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3522 // FIXME: under ASan the call below may write to freed memory and corrupt
3523 // its metadata. See
3524 // https://github.com/google/sanitizers/issues/321.
3525 int res = REAL(sigpending)(set);
3526 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3527 return res;
3528 }
3529 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3530 #else
3531 #define INIT_SIGPENDING
3532 #endif
3533
3534 #if SANITIZER_INTERCEPT_SIGPROCMASK
3535 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
3536 __sanitizer_sigset_t *oldset) {
3537 void *ctx;
3538 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
3539 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3540 // FIXME: under ASan the call below may write to freed memory and corrupt
3541 // its metadata. See
3542 // https://github.com/google/sanitizers/issues/321.
3543 int res = REAL(sigprocmask)(how, set, oldset);
3544 if (!res && oldset)
3545 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
3546 return res;
3547 }
3548 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
3549 #else
3550 #define INIT_SIGPROCMASK
3551 #endif
3552
3553 #if SANITIZER_INTERCEPT_BACKTRACE
3554 INTERCEPTOR(int, backtrace, void **buffer, int size) {
3555 void *ctx;
3556 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
3557 // FIXME: under ASan the call below may write to freed memory and corrupt
3558 // its metadata. See
3559 // https://github.com/google/sanitizers/issues/321.
3560 int res = REAL(backtrace)(buffer, size);
3561 if (res && buffer)
3562 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3563 return res;
3564 }
3565
3566 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3567 void *ctx;
3568 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3569 if (buffer && size)
3570 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3571 // FIXME: under ASan the call below may write to freed memory and corrupt
3572 // its metadata. See
3573 // https://github.com/google/sanitizers/issues/321.
3574 char **res = REAL(backtrace_symbols)(buffer, size);
3575 if (res && size) {
3576 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3577 for (int i = 0; i < size; ++i)
3578 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3579 }
3580 return res;
3581 }
3582 #define INIT_BACKTRACE \
3583 COMMON_INTERCEPT_FUNCTION(backtrace); \
3584 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3585 #else
3586 #define INIT_BACKTRACE
3587 #endif
3588
3589 #if SANITIZER_INTERCEPT__EXIT
3590 INTERCEPTOR(void, _exit, int status) {
3591 void *ctx;
3592 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3593 COMMON_INTERCEPTOR_USER_CALLBACK_START();
3594 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3595 COMMON_INTERCEPTOR_USER_CALLBACK_END();
3596 if (status == 0) status = status1;
3597 REAL(_exit)(status);
3598 }
3599 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3600 #else
3601 #define INIT__EXIT
3602 #endif
3603
3604 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3605 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3606 void *ctx;
3607 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3608 int res = REAL(pthread_mutex_lock)(m);
3609 if (res == errno_EOWNERDEAD)
3610 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3611 if (res == 0 || res == errno_EOWNERDEAD)
3612 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
3613 if (res == errno_EINVAL)
3614 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3615 return res;
3616 }
3617
3618 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3619 void *ctx;
3620 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3621 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3622 int res = REAL(pthread_mutex_unlock)(m);
3623 if (res == errno_EINVAL)
3624 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3625 return res;
3626 }
3627
3628 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3629 #define INIT_PTHREAD_MUTEX_UNLOCK \
3630 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3631 #else
3632 #define INIT_PTHREAD_MUTEX_LOCK
3633 #define INIT_PTHREAD_MUTEX_UNLOCK
3634 #endif
3635
3636 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3637 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3639 if (mnt->mnt_fsname)
3640 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3641 REAL(strlen)(mnt->mnt_fsname) + 1);
3642 if (mnt->mnt_dir)
3643 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3644 REAL(strlen)(mnt->mnt_dir) + 1);
3645 if (mnt->mnt_type)
3646 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3647 REAL(strlen)(mnt->mnt_type) + 1);
3648 if (mnt->mnt_opts)
3649 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3650 REAL(strlen)(mnt->mnt_opts) + 1);
3651 }
3652 #endif
3653
3654 #if SANITIZER_INTERCEPT_GETMNTENT
3655 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3656 void *ctx;
3657 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3658 __sanitizer_mntent *res = REAL(getmntent)(fp);
3659 if (res) write_mntent(ctx, res);
3660 return res;
3661 }
3662 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3663 #else
3664 #define INIT_GETMNTENT
3665 #endif
3666
3667 #if SANITIZER_INTERCEPT_GETMNTENT_R
3668 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3669 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3670 void *ctx;
3671 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3672 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3673 if (res) write_mntent(ctx, res);
3674 return res;
3675 }
3676 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3677 #else
3678 #define INIT_GETMNTENT_R
3679 #endif
3680
3681 #if SANITIZER_INTERCEPT_STATFS
3682 INTERCEPTOR(int, statfs, char *path, void *buf) {
3683 void *ctx;
3684 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3685 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3686 // FIXME: under ASan the call below may write to freed memory and corrupt
3687 // its metadata. See
3688 // https://github.com/google/sanitizers/issues/321.
3689 int res = REAL(statfs)(path, buf);
3690 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3691 return res;
3692 }
3693 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3694 void *ctx;
3695 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3696 // FIXME: under ASan the call below may write to freed memory and corrupt
3697 // its metadata. See
3698 // https://github.com/google/sanitizers/issues/321.
3699 int res = REAL(fstatfs)(fd, buf);
3700 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3701 return res;
3702 }
3703 #define INIT_STATFS \
3704 COMMON_INTERCEPT_FUNCTION(statfs); \
3705 COMMON_INTERCEPT_FUNCTION(fstatfs);
3706 #else
3707 #define INIT_STATFS
3708 #endif
3709
3710 #if SANITIZER_INTERCEPT_STATFS64
3711 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3712 void *ctx;
3713 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3714 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3715 // FIXME: under ASan the call below may write to freed memory and corrupt
3716 // its metadata. See
3717 // https://github.com/google/sanitizers/issues/321.
3718 int res = REAL(statfs64)(path, buf);
3719 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3720 return res;
3721 }
3722 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3723 void *ctx;
3724 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3725 // FIXME: under ASan the call below may write to freed memory and corrupt
3726 // its metadata. See
3727 // https://github.com/google/sanitizers/issues/321.
3728 int res = REAL(fstatfs64)(fd, buf);
3729 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3730 return res;
3731 }
3732 #define INIT_STATFS64 \
3733 COMMON_INTERCEPT_FUNCTION(statfs64); \
3734 COMMON_INTERCEPT_FUNCTION(fstatfs64);
3735 #else
3736 #define INIT_STATFS64
3737 #endif
3738
3739 #if SANITIZER_INTERCEPT_STATVFS
3740 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3741 void *ctx;
3742 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3743 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3744 // FIXME: under ASan the call below may write to freed memory and corrupt
3745 // its metadata. See
3746 // https://github.com/google/sanitizers/issues/321.
3747 int res = REAL(statvfs)(path, buf);
3748 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3749 return res;
3750 }
3751 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3752 void *ctx;
3753 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3754 // FIXME: under ASan the call below may write to freed memory and corrupt
3755 // its metadata. See
3756 // https://github.com/google/sanitizers/issues/321.
3757 int res = REAL(fstatvfs)(fd, buf);
3758 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3759 return res;
3760 }
3761 #define INIT_STATVFS \
3762 COMMON_INTERCEPT_FUNCTION(statvfs); \
3763 COMMON_INTERCEPT_FUNCTION(fstatvfs);
3764 #else
3765 #define INIT_STATVFS
3766 #endif
3767
3768 #if SANITIZER_INTERCEPT_STATVFS64
3769 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3770 void *ctx;
3771 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3772 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3773 // FIXME: under ASan the call below may write to freed memory and corrupt
3774 // its metadata. See
3775 // https://github.com/google/sanitizers/issues/321.
3776 int res = REAL(statvfs64)(path, buf);
3777 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3778 return res;
3779 }
3780 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3781 void *ctx;
3782 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3783 // FIXME: under ASan the call below may write to freed memory and corrupt
3784 // its metadata. See
3785 // https://github.com/google/sanitizers/issues/321.
3786 int res = REAL(fstatvfs64)(fd, buf);
3787 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3788 return res;
3789 }
3790 #define INIT_STATVFS64 \
3791 COMMON_INTERCEPT_FUNCTION(statvfs64); \
3792 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3793 #else
3794 #define INIT_STATVFS64
3795 #endif
3796
3797 #if SANITIZER_INTERCEPT_INITGROUPS
3798 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3799 void *ctx;
3800 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3801 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3802 int res = REAL(initgroups)(user, group);
3803 return res;
3804 }
3805 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3806 #else
3807 #define INIT_INITGROUPS
3808 #endif
3809
3810 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
3811 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
3812 void *ctx;
3813 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
3814 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3815 char *res = REAL(ether_ntoa)(addr);
3816 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3817 return res;
3818 }
3819 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
3820 void *ctx;
3821 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
3822 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3823 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
3824 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
3825 return res;
3826 }
3827 #define INIT_ETHER_NTOA_ATON \
3828 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
3829 COMMON_INTERCEPT_FUNCTION(ether_aton);
3830 #else
3831 #define INIT_ETHER_NTOA_ATON
3832 #endif
3833
3834 #if SANITIZER_INTERCEPT_ETHER_HOST
3835 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
3836 void *ctx;
3837 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
3838 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3839 // FIXME: under ASan the call below may write to freed memory and corrupt
3840 // its metadata. See
3841 // https://github.com/google/sanitizers/issues/321.
3842 int res = REAL(ether_ntohost)(hostname, addr);
3843 if (!res && hostname)
3844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3845 return res;
3846 }
3847 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
3848 void *ctx;
3849 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
3850 if (hostname)
3851 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3852 // FIXME: under ASan the call below may write to freed memory and corrupt
3853 // its metadata. See
3854 // https://github.com/google/sanitizers/issues/321.
3855 int res = REAL(ether_hostton)(hostname, addr);
3856 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3857 return res;
3858 }
3859 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
3860 char *hostname) {
3861 void *ctx;
3862 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
3863 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
3864 // FIXME: under ASan the call below may write to freed memory and corrupt
3865 // its metadata. See
3866 // https://github.com/google/sanitizers/issues/321.
3867 int res = REAL(ether_line)(line, addr, hostname);
3868 if (!res) {
3869 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3870 if (hostname)
3871 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3872 }
3873 return res;
3874 }
3875 #define INIT_ETHER_HOST \
3876 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
3877 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
3878 COMMON_INTERCEPT_FUNCTION(ether_line);
3879 #else
3880 #define INIT_ETHER_HOST
3881 #endif
3882
3883 #if SANITIZER_INTERCEPT_ETHER_R
3884 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
3885 void *ctx;
3886 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
3887 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3888 // FIXME: under ASan the call below may write to freed memory and corrupt
3889 // its metadata. See
3890 // https://github.com/google/sanitizers/issues/321.
3891 char *res = REAL(ether_ntoa_r)(addr, buf);
3892 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3893 return res;
3894 }
3895 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
3896 __sanitizer_ether_addr *addr) {
3897 void *ctx;
3898 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
3899 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3900 // FIXME: under ASan the call below may write to freed memory and corrupt
3901 // its metadata. See
3902 // https://github.com/google/sanitizers/issues/321.
3903 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
3904 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
3905 return res;
3906 }
3907 #define INIT_ETHER_R \
3908 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
3909 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
3910 #else
3911 #define INIT_ETHER_R
3912 #endif
3913
3914 #if SANITIZER_INTERCEPT_SHMCTL
3915 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
3916 void *ctx;
3917 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
3918 // FIXME: under ASan the call below may write to freed memory and corrupt
3919 // its metadata. See
3920 // https://github.com/google/sanitizers/issues/321.
3921 int res = REAL(shmctl)(shmid, cmd, buf);
3922 if (res >= 0) {
3923 unsigned sz = 0;
3924 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
3925 sz = sizeof(__sanitizer_shmid_ds);
3926 else if (cmd == shmctl_ipc_info)
3927 sz = struct_shminfo_sz;
3928 else if (cmd == shmctl_shm_info)
3929 sz = struct_shm_info_sz;
3930 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3931 }
3932 return res;
3933 }
3934 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
3935 #else
3936 #define INIT_SHMCTL
3937 #endif
3938
3939 #if SANITIZER_INTERCEPT_RANDOM_R
3940 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
3941 void *ctx;
3942 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
3943 // FIXME: under ASan the call below may write to freed memory and corrupt
3944 // its metadata. See
3945 // https://github.com/google/sanitizers/issues/321.
3946 int res = REAL(random_r)(buf, result);
3947 if (!res && result)
3948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3949 return res;
3950 }
3951 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
3952 #else
3953 #define INIT_RANDOM_R
3954 #endif
3955
3956 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
3957 // its metadata. See
3958 // https://github.com/google/sanitizers/issues/321.
3959 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
3960 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
3961 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
3962 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
3963 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
3964 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
3965 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
3966 INTERCEPTOR(int, fn, void *attr, void *r) { \
3967 void *ctx; \
3968 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
3969 int res = REAL(fn)(attr, r); \
3970 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
3971 return res; \
3972 }
3973 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
3974 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
3975 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
3976 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
3977 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
3978 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
3979 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
3980 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
3981 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
3982 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
3983 #endif
3984
3985 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
3986 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
3987 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
3988 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
3989 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
3990 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
3991 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
3992 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
3993 void *ctx;
3994 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
3995 // FIXME: under ASan the call below may write to freed memory and corrupt
3996 // its metadata. See
3997 // https://github.com/google/sanitizers/issues/321.
3998 int res = REAL(pthread_attr_getstack)(attr, addr, size);
3999 if (!res) {
4000 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4001 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4002 }
4003 return res;
4004 }
4005
4006 // We may need to call the real pthread_attr_getstack from the run-time
4007 // in sanitizer_common, but we don't want to include the interception headers
4008 // there. So, just define this function here.
4009 namespace __sanitizer {
4010 extern "C" {
4011 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4012 return REAL(pthread_attr_getstack)(attr, addr, size);
4013 }
4014 } // extern "C"
4015 } // namespace __sanitizer
4016
4017 #define INIT_PTHREAD_ATTR_GET \
4018 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4019 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4020 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4021 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
4022 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4023 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4024 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4025 #else
4026 #define INIT_PTHREAD_ATTR_GET
4027 #endif
4028
4029 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4030 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4031
4032 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4033 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4034 #else
4035 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4036 #endif
4037
4038 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4039 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4040 void *cpuset) {
4041 void *ctx;
4042 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4043 cpuset);
4044 // FIXME: under ASan the call below may write to freed memory and corrupt
4045 // its metadata. See
4046 // https://github.com/google/sanitizers/issues/321.
4047 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4048 if (!res && cpusetsize && cpuset)
4049 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4050 return res;
4051 }
4052
4053 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4054 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4055 #else
4056 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4057 #endif
4058
4059 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4060 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4061 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4062 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4063 #else
4064 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4065 #endif
4066
4067 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4068 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4069 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4070 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4071 #else
4072 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4073 #endif
4074
4075 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4076 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4077 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4078 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4079 #else
4080 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4081 #endif
4082
4083 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4084 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4085 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4086 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4087 #else
4088 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4089 #endif
4090
4091 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4092 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4093 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4094 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4095 #else
4096 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4097 #endif
4098
4099 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4100 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4101 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4102 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4103 #else
4104 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4105 #endif
4106
4107 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4108 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4109 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4110 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4111 #else
4112 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4113 #endif
4114
4115 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4116 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4117 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4118 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4119 #else
4120 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4121 #endif
4122
4123 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4124 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4125 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4126 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4127 #else
4128 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4129 #endif
4130
4131 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4132 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4133 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4134 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4135 #else
4136 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4137 #endif
4138
4139 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4140 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4141 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4142 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4143 #else
4144 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4145 #endif
4146
4147 #if SANITIZER_INTERCEPT_TMPNAM
4148 INTERCEPTOR(char *, tmpnam, char *s) {
4149 void *ctx;
4150 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4151 char *res = REAL(tmpnam)(s);
4152 if (res) {
4153 if (s)
4154 // FIXME: under ASan the call below may write to freed memory and corrupt
4155 // its metadata. See
4156 // https://github.com/google/sanitizers/issues/321.
4157 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4158 else
4159 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4160 }
4161 return res;
4162 }
4163 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4164 #else
4165 #define INIT_TMPNAM
4166 #endif
4167
4168 #if SANITIZER_INTERCEPT_TMPNAM_R
4169 INTERCEPTOR(char *, tmpnam_r, char *s) {
4170 void *ctx;
4171 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4172 // FIXME: under ASan the call below may write to freed memory and corrupt
4173 // its metadata. See
4174 // https://github.com/google/sanitizers/issues/321.
4175 char *res = REAL(tmpnam_r)(s);
4176 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4177 return res;
4178 }
4179 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4180 #else
4181 #define INIT_TMPNAM_R
4182 #endif
4183
4184 #if SANITIZER_INTERCEPT_TTYNAME_R
4185 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4186 void *ctx;
4187 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4188 int res = REAL(ttyname_r)(fd, name, namesize);
4189 if (res == 0)
4190 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4191 return res;
4192 }
4193 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4194 #else
4195 #define INIT_TTYNAME_R
4196 #endif
4197
4198 #if SANITIZER_INTERCEPT_TEMPNAM
4199 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4200 void *ctx;
4201 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4202 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4203 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4204 char *res = REAL(tempnam)(dir, pfx);
4205 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4206 return res;
4207 }
4208 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4209 #else
4210 #define INIT_TEMPNAM
4211 #endif
4212
4213 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
4214 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4215 void *ctx;
4216 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4217 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4218 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4219 return REAL(pthread_setname_np)(thread, name);
4220 }
4221 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4222 #else
4223 #define INIT_PTHREAD_SETNAME_NP
4224 #endif
4225
4226 #if SANITIZER_INTERCEPT_SINCOS
4227 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4228 void *ctx;
4229 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4230 // FIXME: under ASan the call below may write to freed memory and corrupt
4231 // its metadata. See
4232 // https://github.com/google/sanitizers/issues/321.
4233 REAL(sincos)(x, sin, cos);
4234 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4235 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4236 }
4237 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4238 void *ctx;
4239 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4240 // FIXME: under ASan the call below may write to freed memory and corrupt
4241 // its metadata. See
4242 // https://github.com/google/sanitizers/issues/321.
4243 REAL(sincosf)(x, sin, cos);
4244 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4245 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4246 }
4247 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4248 void *ctx;
4249 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4250 // FIXME: under ASan the call below may write to freed memory and corrupt
4251 // its metadata. See
4252 // https://github.com/google/sanitizers/issues/321.
4253 REAL(sincosl)(x, sin, cos);
4254 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4255 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4256 }
4257 #define INIT_SINCOS \
4258 COMMON_INTERCEPT_FUNCTION(sincos); \
4259 COMMON_INTERCEPT_FUNCTION(sincosf); \
4260 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4261 #else
4262 #define INIT_SINCOS
4263 #endif
4264
4265 #if SANITIZER_INTERCEPT_REMQUO
4266 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4267 void *ctx;
4268 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4269 // FIXME: under ASan the call below may write to freed memory and corrupt
4270 // its metadata. See
4271 // https://github.com/google/sanitizers/issues/321.
4272 double res = REAL(remquo)(x, y, quo);
4273 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4274 return res;
4275 }
4276 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4277 void *ctx;
4278 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4279 // FIXME: under ASan the call below may write to freed memory and corrupt
4280 // its metadata. See
4281 // https://github.com/google/sanitizers/issues/321.
4282 float res = REAL(remquof)(x, y, quo);
4283 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4284 return res;
4285 }
4286 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4287 void *ctx;
4288 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4289 // FIXME: under ASan the call below may write to freed memory and corrupt
4290 // its metadata. See
4291 // https://github.com/google/sanitizers/issues/321.
4292 long double res = REAL(remquol)(x, y, quo);
4293 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4294 return res;
4295 }
4296 #define INIT_REMQUO \
4297 COMMON_INTERCEPT_FUNCTION(remquo); \
4298 COMMON_INTERCEPT_FUNCTION(remquof); \
4299 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4300 #else
4301 #define INIT_REMQUO
4302 #endif
4303
4304 #if SANITIZER_INTERCEPT_LGAMMA
4305 extern int signgam;
4306 INTERCEPTOR(double, lgamma, double x) {
4307 void *ctx;
4308 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4309 double res = REAL(lgamma)(x);
4310 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4311 return res;
4312 }
4313 INTERCEPTOR(float, lgammaf, float x) {
4314 void *ctx;
4315 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4316 float res = REAL(lgammaf)(x);
4317 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4318 return res;
4319 }
4320 INTERCEPTOR(long double, lgammal, long double x) {
4321 void *ctx;
4322 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4323 long double res = REAL(lgammal)(x);
4324 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4325 return res;
4326 }
4327 #define INIT_LGAMMA \
4328 COMMON_INTERCEPT_FUNCTION(lgamma); \
4329 COMMON_INTERCEPT_FUNCTION(lgammaf); \
4330 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4331 #else
4332 #define INIT_LGAMMA
4333 #endif
4334
4335 #if SANITIZER_INTERCEPT_LGAMMA_R
4336 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4337 void *ctx;
4338 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4339 // FIXME: under ASan the call below may write to freed memory and corrupt
4340 // its metadata. See
4341 // https://github.com/google/sanitizers/issues/321.
4342 double res = REAL(lgamma_r)(x, signp);
4343 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4344 return res;
4345 }
4346 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4347 void *ctx;
4348 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4349 // FIXME: under ASan the call below may write to freed memory and corrupt
4350 // its metadata. See
4351 // https://github.com/google/sanitizers/issues/321.
4352 float res = REAL(lgammaf_r)(x, signp);
4353 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4354 return res;
4355 }
4356 #define INIT_LGAMMA_R \
4357 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4358 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4359 #else
4360 #define INIT_LGAMMA_R
4361 #endif
4362
4363 #if SANITIZER_INTERCEPT_LGAMMAL_R
4364 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4365 void *ctx;
4366 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4367 // FIXME: under ASan the call below may write to freed memory and corrupt
4368 // its metadata. See
4369 // https://github.com/google/sanitizers/issues/321.
4370 long double res = REAL(lgammal_r)(x, signp);
4371 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4372 return res;
4373 }
4374 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4375 #else
4376 #define INIT_LGAMMAL_R
4377 #endif
4378
4379 #if SANITIZER_INTERCEPT_DRAND48_R
4380 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4381 void *ctx;
4382 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4383 // FIXME: under ASan the call below may write to freed memory and corrupt
4384 // its metadata. See
4385 // https://github.com/google/sanitizers/issues/321.
4386 int res = REAL(drand48_r)(buffer, result);
4387 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4388 return res;
4389 }
4390 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4391 void *ctx;
4392 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4393 // FIXME: under ASan the call below may write to freed memory and corrupt
4394 // its metadata. See
4395 // https://github.com/google/sanitizers/issues/321.
4396 int res = REAL(lrand48_r)(buffer, result);
4397 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4398 return res;
4399 }
4400 #define INIT_DRAND48_R \
4401 COMMON_INTERCEPT_FUNCTION(drand48_r); \
4402 COMMON_INTERCEPT_FUNCTION(lrand48_r);
4403 #else
4404 #define INIT_DRAND48_R
4405 #endif
4406
4407 #if SANITIZER_INTERCEPT_RAND_R
4408 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4409 void *ctx;
4410 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4411 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4412 return REAL(rand_r)(seedp);
4413 }
4414 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4415 #else
4416 #define INIT_RAND_R
4417 #endif
4418
4419 #if SANITIZER_INTERCEPT_GETLINE
4420 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4421 void *ctx;
4422 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4423 // FIXME: under ASan the call below may write to freed memory and corrupt
4424 // its metadata. See
4425 // https://github.com/google/sanitizers/issues/321.
4426 SSIZE_T res = REAL(getline)(lineptr, n, stream);
4427 if (res > 0) {
4428 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4429 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4430 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4431 }
4432 return res;
4433 }
4434
4435 // FIXME: under ASan the call below may write to freed memory and corrupt its
4436 // metadata. See
4437 // https://github.com/google/sanitizers/issues/321.
4438 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
4439 { \
4440 void *ctx; \
4441 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
4442 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
4443 if (res > 0) { \
4444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
4445 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
4446 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
4447 } \
4448 return res; \
4449 }
4450
4451 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4452 void *stream)
4453 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4454
4455 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4456 // with its own body.
4457 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4458 void *stream)
4459 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4460
4461 #define INIT_GETLINE \
4462 COMMON_INTERCEPT_FUNCTION(getline); \
4463 COMMON_INTERCEPT_FUNCTION(__getdelim); \
4464 COMMON_INTERCEPT_FUNCTION(getdelim);
4465 #else
4466 #define INIT_GETLINE
4467 #endif
4468
4469 #if SANITIZER_INTERCEPT_ICONV
4470 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
4471 char **outbuf, SIZE_T *outbytesleft) {
4472 void *ctx;
4473 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
4474 outbytesleft);
4475 if (inbytesleft)
4476 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
4477 if (inbuf && inbytesleft)
4478 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
4479 if (outbytesleft)
4480 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
4481 void *outbuf_orig = outbuf ? *outbuf : nullptr;
4482 // FIXME: under ASan the call below may write to freed memory and corrupt
4483 // its metadata. See
4484 // https://github.com/google/sanitizers/issues/321.
4485 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
4486 if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
4487 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
4488 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
4489 }
4490 return res;
4491 }
4492 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
4493 #else
4494 #define INIT_ICONV
4495 #endif
4496
4497 #if SANITIZER_INTERCEPT_TIMES
4498 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
4499 void *ctx;
4500 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
4501 // FIXME: under ASan the call below may write to freed memory and corrupt
4502 // its metadata. See
4503 // https://github.com/google/sanitizers/issues/321.
4504 __sanitizer_clock_t res = REAL(times)(tms);
4505 if (res != (__sanitizer_clock_t)-1 && tms)
4506 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
4507 return res;
4508 }
4509 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
4510 #else
4511 #define INIT_TIMES
4512 #endif
4513
4514 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
4515 #if !SANITIZER_S390
4516 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
4517 // If you see any crashes around this functions, there are 2 known issues with
4518 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
4519 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
4520 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
4521 // to access thread local variables (it should not happen normally,
4522 // because sanitizers use initial-exec tls model).
4523 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
4524 void *ctx;
4525 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
4526 void *res = REAL(__tls_get_addr)(arg);
4527 uptr tls_begin, tls_end;
4528 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4529 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
4530 if (dtv) {
4531 // New DTLS block has been allocated.
4532 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4533 }
4534 return res;
4535 }
4536 #if SANITIZER_PPC
4537 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
4538 // mostly the same semantics as __tls_get_addr, but its presence enables
4539 // some optimizations in linker (which are safe to ignore here).
4540 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
4541 visibility("default")))
4542 void *__tls_get_addr_opt(void *arg);
4543 #endif
4544 #else // SANITIZER_S390
4545 // On s390, we have to intercept two functions here:
4546 // - __tls_get_addr_internal, which is a glibc-internal function that is like
4547 // the usual __tls_get_addr, but returns a TP-relative offset instead of
4548 // a proper pointer. It is used by dlsym for TLS symbols.
4549 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
4550 // descriptor offset as an argument instead of a pointer. GOT address
4551 // is passed in r12, so it's necessary to write it in assembly. This is
4552 // the function used by the compiler.
4553 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr_internal)
4554 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
4555 void *ctx;
4556 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
4557 uptr res = REAL(__tls_get_addr_internal)(arg);
4558 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
4559 void *ptr = reinterpret_cast<void *>(res + tp);
4560 uptr tls_begin, tls_end;
4561 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4562 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
4563 if (dtv) {
4564 // New DTLS block has been allocated.
4565 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4566 }
4567 return res;
4568 }
4569 // We need a protected symbol aliasing the above, so that we can jump
4570 // directly to it from the assembly below.
4571 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
4572 visibility("protected")))
4573 uptr __interceptor___tls_get_addr_internal_protected(void *arg);
4574 // Now carefully intercept __tls_get_offset.
4575 asm(
4576 ".text\n"
4577 ".global __tls_get_offset\n"
4578 "__tls_get_offset:\n"
4579 // The __intercept_ version has to exist, so that gen_dynamic_list.py
4580 // exports our symbol.
4581 ".global __interceptor___tls_get_offset\n"
4582 "__interceptor___tls_get_offset:\n"
4583 #ifdef __s390x__
4584 "la %r2, 0(%r2,%r12)\n"
4585 "jg __interceptor___tls_get_addr_internal_protected\n"
4586 #else
4587 "basr %r3,0\n"
4588 "0: la %r2,0(%r2,%r12)\n"
4589 "l %r4,1f-0b(%r3)\n"
4590 "b 0(%r4,%r3)\n"
4591 "1: .long __interceptor___tls_get_addr_internal_protected - 0b\n"
4592 #endif
4593 ".type __tls_get_offset, @function\n"
4594 ".size __tls_get_offset, .-__tls_get_offset\n"
4595 );
4596 #endif // SANITIZER_S390
4597 #else
4598 #define INIT_TLS_GET_ADDR
4599 #endif
4600
4601 #if SANITIZER_INTERCEPT_LISTXATTR
4602 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
4603 void *ctx;
4604 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
4605 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4606 // FIXME: under ASan the call below may write to freed memory and corrupt
4607 // its metadata. See
4608 // https://github.com/google/sanitizers/issues/321.
4609 SSIZE_T res = REAL(listxattr)(path, list, size);
4610 // Here and below, size == 0 is a special case where nothing is written to the
4611 // buffer, and res contains the desired buffer size.
4612 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4613 return res;
4614 }
4615 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
4616 void *ctx;
4617 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
4618 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4619 // FIXME: under ASan the call below may write to freed memory and corrupt
4620 // its metadata. See
4621 // https://github.com/google/sanitizers/issues/321.
4622 SSIZE_T res = REAL(llistxattr)(path, list, size);
4623 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4624 return res;
4625 }
4626 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
4627 void *ctx;
4628 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
4629 // FIXME: under ASan the call below may write to freed memory and corrupt
4630 // its metadata. See
4631 // https://github.com/google/sanitizers/issues/321.
4632 SSIZE_T res = REAL(flistxattr)(fd, list, size);
4633 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4634 return res;
4635 }
4636 #define INIT_LISTXATTR \
4637 COMMON_INTERCEPT_FUNCTION(listxattr); \
4638 COMMON_INTERCEPT_FUNCTION(llistxattr); \
4639 COMMON_INTERCEPT_FUNCTION(flistxattr);
4640 #else
4641 #define INIT_LISTXATTR
4642 #endif
4643
4644 #if SANITIZER_INTERCEPT_GETXATTR
4645 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
4646 SIZE_T size) {
4647 void *ctx;
4648 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
4649 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4650 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4651 // FIXME: under ASan the call below may write to freed memory and corrupt
4652 // its metadata. See
4653 // https://github.com/google/sanitizers/issues/321.
4654 SSIZE_T res = REAL(getxattr)(path, name, value, size);
4655 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4656 return res;
4657 }
4658 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4659 SIZE_T size) {
4660 void *ctx;
4661 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4662 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4663 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4664 // FIXME: under ASan the call below may write to freed memory and corrupt
4665 // its metadata. See
4666 // https://github.com/google/sanitizers/issues/321.
4667 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4668 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4669 return res;
4670 }
4671 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4672 SIZE_T size) {
4673 void *ctx;
4674 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4675 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4676 // FIXME: under ASan the call below may write to freed memory and corrupt
4677 // its metadata. See
4678 // https://github.com/google/sanitizers/issues/321.
4679 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4680 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4681 return res;
4682 }
4683 #define INIT_GETXATTR \
4684 COMMON_INTERCEPT_FUNCTION(getxattr); \
4685 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4686 COMMON_INTERCEPT_FUNCTION(fgetxattr);
4687 #else
4688 #define INIT_GETXATTR
4689 #endif
4690
4691 #if SANITIZER_INTERCEPT_GETRESID
4692 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4693 void *ctx;
4694 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4695 // FIXME: under ASan the call below may write to freed memory and corrupt
4696 // its metadata. See
4697 // https://github.com/google/sanitizers/issues/321.
4698 int res = REAL(getresuid)(ruid, euid, suid);
4699 if (res >= 0) {
4700 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4701 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4702 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4703 }
4704 return res;
4705 }
4706 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4707 void *ctx;
4708 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
4709 // FIXME: under ASan the call below may write to freed memory and corrupt
4710 // its metadata. See
4711 // https://github.com/google/sanitizers/issues/321.
4712 int res = REAL(getresgid)(rgid, egid, sgid);
4713 if (res >= 0) {
4714 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4715 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4716 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4717 }
4718 return res;
4719 }
4720 #define INIT_GETRESID \
4721 COMMON_INTERCEPT_FUNCTION(getresuid); \
4722 COMMON_INTERCEPT_FUNCTION(getresgid);
4723 #else
4724 #define INIT_GETRESID
4725 #endif
4726
4727 #if SANITIZER_INTERCEPT_GETIFADDRS
4728 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4729 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4730 // intercept it to poison the memory again.
4731 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4732 void *ctx;
4733 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4734 // FIXME: under ASan the call below may write to freed memory and corrupt
4735 // its metadata. See
4736 // https://github.com/google/sanitizers/issues/321.
4737 int res = REAL(getifaddrs)(ifap);
4738 if (res == 0 && ifap) {
4739 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4740 __sanitizer_ifaddrs *p = *ifap;
4741 while (p) {
4742 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4743 if (p->ifa_name)
4744 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4745 REAL(strlen)(p->ifa_name) + 1);
4746 if (p->ifa_addr)
4747 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4748 if (p->ifa_netmask)
4749 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4750 // On Linux this is a union, but the other member also points to a
4751 // struct sockaddr, so the following is sufficient.
4752 if (p->ifa_dstaddr)
4753 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4754 // FIXME(smatveev): Unpoison p->ifa_data as well.
4755 p = p->ifa_next;
4756 }
4757 }
4758 return res;
4759 }
4760 #define INIT_GETIFADDRS \
4761 COMMON_INTERCEPT_FUNCTION(getifaddrs);
4762 #else
4763 #define INIT_GETIFADDRS
4764 #endif
4765
4766 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4767 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4768 void *ctx;
4769 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4770 // FIXME: under ASan the call below may write to freed memory and corrupt
4771 // its metadata. See
4772 // https://github.com/google/sanitizers/issues/321.
4773 char *res = REAL(if_indextoname)(ifindex, ifname);
4774 if (res && ifname)
4775 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4776 return res;
4777 }
4778 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4779 void *ctx;
4780 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4781 if (ifname)
4782 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4783 return REAL(if_nametoindex)(ifname);
4784 }
4785 #define INIT_IF_INDEXTONAME \
4786 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4787 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4788 #else
4789 #define INIT_IF_INDEXTONAME
4790 #endif
4791
4792 #if SANITIZER_INTERCEPT_CAPGET
4793 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
4794 void *ctx;
4795 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
4796 if (hdrp)
4797 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4798 // FIXME: under ASan the call below may write to freed memory and corrupt
4799 // its metadata. See
4800 // https://github.com/google/sanitizers/issues/321.
4801 int res = REAL(capget)(hdrp, datap);
4802 if (res == 0 && datap)
4803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
4804 // We can also return -1 and write to hdrp->version if the version passed in
4805 // hdrp->version is unsupported. But that's not a trivial condition to check,
4806 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
4807 return res;
4808 }
4809 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
4810 void *ctx;
4811 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
4812 if (hdrp)
4813 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4814 if (datap)
4815 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
4816 return REAL(capset)(hdrp, datap);
4817 }
4818 #define INIT_CAPGET \
4819 COMMON_INTERCEPT_FUNCTION(capget); \
4820 COMMON_INTERCEPT_FUNCTION(capset);
4821 #else
4822 #define INIT_CAPGET
4823 #endif
4824
4825 #if SANITIZER_INTERCEPT_AEABI_MEM
4826 DECLARE_REAL_AND_INTERCEPTOR(void *, memmove, void *, const void *, uptr)
4827 DECLARE_REAL_AND_INTERCEPTOR(void *, memcpy, void *, const void *, uptr)
4828 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr)
4829
4830 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
4831 return WRAP(memmove)(to, from, size);
4832 }
4833 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
4834 return WRAP(memmove)(to, from, size);
4835 }
4836 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
4837 return WRAP(memmove)(to, from, size);
4838 }
4839 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
4840 return WRAP(memcpy)(to, from, size);
4841 }
4842 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
4843 return WRAP(memcpy)(to, from, size);
4844 }
4845 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
4846 return WRAP(memcpy)(to, from, size);
4847 }
4848 // Note the argument order.
4849 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
4850 return WRAP(memset)(block, c, size);
4851 }
4852 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
4853 return WRAP(memset)(block, c, size);
4854 }
4855 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
4856 return WRAP(memset)(block, c, size);
4857 }
4858 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
4859 return WRAP(memset)(block, 0, size);
4860 }
4861 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
4862 return WRAP(memset)(block, 0, size);
4863 }
4864 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
4865 return WRAP(memset)(block, 0, size);
4866 }
4867 #define INIT_AEABI_MEM \
4868 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
4869 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
4870 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
4871 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
4872 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
4873 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
4874 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
4875 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
4876 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
4877 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
4878 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
4879 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
4880 #else
4881 #define INIT_AEABI_MEM
4882 #endif // SANITIZER_INTERCEPT_AEABI_MEM
4883
4884 #if SANITIZER_INTERCEPT___BZERO
4885 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
4886
4887 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
4888 return WRAP(memset)(block, 0, size);
4889 }
4890 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
4891 #else
4892 #define INIT___BZERO
4893 #endif // SANITIZER_INTERCEPT___BZERO
4894
4895 #if SANITIZER_INTERCEPT_FTIME
4896 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
4897 void *ctx;
4898 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
4899 // FIXME: under ASan the call below may write to freed memory and corrupt
4900 // its metadata. See
4901 // https://github.com/google/sanitizers/issues/321.
4902 int res = REAL(ftime)(tp);
4903 if (tp)
4904 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
4905 return res;
4906 }
4907 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
4908 #else
4909 #define INIT_FTIME
4910 #endif // SANITIZER_INTERCEPT_FTIME
4911
4912 #if SANITIZER_INTERCEPT_XDR
4913 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
4914 unsigned size, int op) {
4915 void *ctx;
4916 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
4917 // FIXME: under ASan the call below may write to freed memory and corrupt
4918 // its metadata. See
4919 // https://github.com/google/sanitizers/issues/321.
4920 REAL(xdrmem_create)(xdrs, addr, size, op);
4921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4922 if (op == __sanitizer_XDR_ENCODE) {
4923 // It's not obvious how much data individual xdr_ routines write.
4924 // Simply unpoison the entire target buffer in advance.
4925 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
4926 }
4927 }
4928
4929 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
4930 void *ctx;
4931 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
4932 // FIXME: under ASan the call below may write to freed memory and corrupt
4933 // its metadata. See
4934 // https://github.com/google/sanitizers/issues/321.
4935 REAL(xdrstdio_create)(xdrs, file, op);
4936 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4937 }
4938
4939 // FIXME: under ASan the call below may write to freed memory and corrupt
4940 // its metadata. See
4941 // https://github.com/google/sanitizers/issues/321.
4942 #define XDR_INTERCEPTOR(F, T) \
4943 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
4944 void *ctx; \
4945 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
4946 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
4947 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
4948 int res = REAL(F)(xdrs, p); \
4949 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
4950 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
4951 return res; \
4952 }
4953
4954 XDR_INTERCEPTOR(xdr_short, short)
4955 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
4956 XDR_INTERCEPTOR(xdr_int, int)
4957 XDR_INTERCEPTOR(xdr_u_int, unsigned)
4958 XDR_INTERCEPTOR(xdr_long, long)
4959 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
4960 XDR_INTERCEPTOR(xdr_hyper, long long)
4961 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
4962 XDR_INTERCEPTOR(xdr_longlong_t, long long)
4963 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
4964 XDR_INTERCEPTOR(xdr_int8_t, u8)
4965 XDR_INTERCEPTOR(xdr_uint8_t, u8)
4966 XDR_INTERCEPTOR(xdr_int16_t, u16)
4967 XDR_INTERCEPTOR(xdr_uint16_t, u16)
4968 XDR_INTERCEPTOR(xdr_int32_t, u32)
4969 XDR_INTERCEPTOR(xdr_uint32_t, u32)
4970 XDR_INTERCEPTOR(xdr_int64_t, u64)
4971 XDR_INTERCEPTOR(xdr_uint64_t, u64)
4972 XDR_INTERCEPTOR(xdr_quad_t, long long)
4973 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
4974 XDR_INTERCEPTOR(xdr_bool, bool)
4975 XDR_INTERCEPTOR(xdr_enum, int)
4976 XDR_INTERCEPTOR(xdr_char, char)
4977 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
4978 XDR_INTERCEPTOR(xdr_float, float)
4979 XDR_INTERCEPTOR(xdr_double, double)
4980
4981 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
4982 // wrapstring, sizeof
4983
4984 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
4985 unsigned maxsize) {
4986 void *ctx;
4987 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
4988 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
4989 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
4990 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
4991 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
4992 }
4993 // FIXME: under ASan the call below may write to freed memory and corrupt
4994 // its metadata. See
4995 // https://github.com/google/sanitizers/issues/321.
4996 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
4997 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
4998 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4999 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5000 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5001 }
5002 return res;
5003 }
5004
5005 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5006 unsigned maxsize) {
5007 void *ctx;
5008 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5009 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5010 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5011 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5012 }
5013 // FIXME: under ASan the call below may write to freed memory and corrupt
5014 // its metadata. See
5015 // https://github.com/google/sanitizers/issues/321.
5016 int res = REAL(xdr_string)(xdrs, p, maxsize);
5017 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5018 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5019 if (res && *p)
5020 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5021 }
5022 return res;
5023 }
5024
5025 #define INIT_XDR \
5026 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5027 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5028 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5029 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5030 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5031 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5032 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5033 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5034 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5035 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5036 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5037 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5038 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5039 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5040 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5041 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5042 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5043 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5044 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5045 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5046 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5047 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5048 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5049 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5050 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5051 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5052 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5053 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5054 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5055 COMMON_INTERCEPT_FUNCTION(xdr_string);
5056 #else
5057 #define INIT_XDR
5058 #endif // SANITIZER_INTERCEPT_XDR
5059
5060 #if SANITIZER_INTERCEPT_TSEARCH
5061 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5062 int (*compar)(const void *, const void *)) {
5063 void *ctx;
5064 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5065 // FIXME: under ASan the call below may write to freed memory and corrupt
5066 // its metadata. See
5067 // https://github.com/google/sanitizers/issues/321.
5068 void *res = REAL(tsearch)(key, rootp, compar);
5069 if (res && *(void **)res == key)
5070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5071 return res;
5072 }
5073 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5074 #else
5075 #define INIT_TSEARCH
5076 #endif
5077
5078 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5079 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5080 void unpoison_file(__sanitizer_FILE *fp) {
5081 #if SANITIZER_HAS_STRUCT_FILE
5082 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5083 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5084 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5085 fp->_IO_read_end - fp->_IO_read_base);
5086 #endif // SANITIZER_HAS_STRUCT_FILE
5087 }
5088 #endif
5089
5090 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5091 // These guys are called when a .c source is built with -O2.
5092 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5093 void *ctx;
5094 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5095 int res = REAL(__uflow)(fp);
5096 unpoison_file(fp);
5097 return res;
5098 }
5099 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5100 void *ctx;
5101 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5102 int res = REAL(__underflow)(fp);
5103 unpoison_file(fp);
5104 return res;
5105 }
5106 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5107 void *ctx;
5108 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5109 int res = REAL(__overflow)(fp, ch);
5110 unpoison_file(fp);
5111 return res;
5112 }
5113 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5114 void *ctx;
5115 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5116 int res = REAL(__wuflow)(fp);
5117 unpoison_file(fp);
5118 return res;
5119 }
5120 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5121 void *ctx;
5122 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5123 int res = REAL(__wunderflow)(fp);
5124 unpoison_file(fp);
5125 return res;
5126 }
5127 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5128 void *ctx;
5129 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5130 int res = REAL(__woverflow)(fp, ch);
5131 unpoison_file(fp);
5132 return res;
5133 }
5134 #define INIT_LIBIO_INTERNALS \
5135 COMMON_INTERCEPT_FUNCTION(__uflow); \
5136 COMMON_INTERCEPT_FUNCTION(__underflow); \
5137 COMMON_INTERCEPT_FUNCTION(__overflow); \
5138 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5139 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5140 COMMON_INTERCEPT_FUNCTION(__woverflow);
5141 #else
5142 #define INIT_LIBIO_INTERNALS
5143 #endif
5144
5145 #if SANITIZER_INTERCEPT_FOPEN
5146 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5147 void *ctx;
5148 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5149 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5150 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5151 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5152 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5153 if (res) unpoison_file(res);
5154 return res;
5155 }
5156 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5157 void *ctx;
5158 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5159 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5160 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5161 if (res) unpoison_file(res);
5162 return res;
5163 }
5164 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5165 __sanitizer_FILE *fp) {
5166 void *ctx;
5167 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5168 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5169 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5170 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5171 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5172 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5173 if (res) unpoison_file(res);
5174 return res;
5175 }
5176 #define INIT_FOPEN \
5177 COMMON_INTERCEPT_FUNCTION(fopen); \
5178 COMMON_INTERCEPT_FUNCTION(fdopen); \
5179 COMMON_INTERCEPT_FUNCTION(freopen);
5180 #else
5181 #define INIT_FOPEN
5182 #endif
5183
5184 #if SANITIZER_INTERCEPT_FOPEN64
5185 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5186 void *ctx;
5187 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5188 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5189 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5190 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5191 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5192 if (res) unpoison_file(res);
5193 return res;
5194 }
5195 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5196 __sanitizer_FILE *fp) {
5197 void *ctx;
5198 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5199 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5200 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5201 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5202 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5203 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5204 if (res) unpoison_file(res);
5205 return res;
5206 }
5207 #define INIT_FOPEN64 \
5208 COMMON_INTERCEPT_FUNCTION(fopen64); \
5209 COMMON_INTERCEPT_FUNCTION(freopen64);
5210 #else
5211 #define INIT_FOPEN64
5212 #endif
5213
5214 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5215 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5216 void *ctx;
5217 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5218 // FIXME: under ASan the call below may write to freed memory and corrupt
5219 // its metadata. See
5220 // https://github.com/google/sanitizers/issues/321.
5221 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5222 if (res) {
5223 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5224 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5225 unpoison_file(res);
5226 FileMetadata file = {ptr, sizeloc};
5227 SetInterceptorMetadata(res, file);
5228 }
5229 return res;
5230 }
5231 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5232 SIZE_T *sizeloc) {
5233 void *ctx;
5234 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5235 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5236 if (res) {
5237 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5238 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5239 unpoison_file(res);
5240 FileMetadata file = {(char **)ptr, sizeloc};
5241 SetInterceptorMetadata(res, file);
5242 }
5243 return res;
5244 }
5245 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5246 const char *mode) {
5247 void *ctx;
5248 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5249 // FIXME: under ASan the call below may write to freed memory and corrupt
5250 // its metadata. See
5251 // https://github.com/google/sanitizers/issues/321.
5252 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5253 if (res) unpoison_file(res);
5254 return res;
5255 }
5256 #define INIT_OPEN_MEMSTREAM \
5257 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5258 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5259 COMMON_INTERCEPT_FUNCTION(fmemopen);
5260 #else
5261 #define INIT_OPEN_MEMSTREAM
5262 #endif
5263
5264 #if SANITIZER_INTERCEPT_OBSTACK
5265 static void initialize_obstack(__sanitizer_obstack *obstack) {
5266 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5267 if (obstack->chunk)
5268 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5269 sizeof(*obstack->chunk));
5270 }
5271
5272 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5273 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5274 void (*free_fn)(uptr arg, void *p)) {
5275 void *ctx;
5276 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5277 free_fn);
5278 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5279 if (res) initialize_obstack(obstack);
5280 return res;
5281 }
5282 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5283 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5284 void *ctx;
5285 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5286 free_fn);
5287 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5288 if (res) initialize_obstack(obstack);
5289 return res;
5290 }
5291 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5292 void *ctx;
5293 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5294 REAL(_obstack_newchunk)(obstack, length);
5295 if (obstack->chunk)
5296 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5297 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5298 }
5299 #define INIT_OBSTACK \
5300 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5301 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5302 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5303 #else
5304 #define INIT_OBSTACK
5305 #endif
5306
5307 #if SANITIZER_INTERCEPT_FFLUSH
5308 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5309 void *ctx;
5310 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5311 int res = REAL(fflush)(fp);
5312 // FIXME: handle fp == NULL
5313 if (fp) {
5314 const FileMetadata *m = GetInterceptorMetadata(fp);
5315 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5316 }
5317 return res;
5318 }
5319 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5320 #else
5321 #define INIT_FFLUSH
5322 #endif
5323
5324 #if SANITIZER_INTERCEPT_FCLOSE
5325 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5326 void *ctx;
5327 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5328 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5329 const FileMetadata *m = GetInterceptorMetadata(fp);
5330 int res = REAL(fclose)(fp);
5331 if (m) {
5332 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5333 DeleteInterceptorMetadata(fp);
5334 }
5335 return res;
5336 }
5337 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5338 #else
5339 #define INIT_FCLOSE
5340 #endif
5341
5342 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5343 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5344 void *ctx;
5345 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5346 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5347 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5348 void *res = REAL(dlopen)(filename, flag);
5349 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5350 return res;
5351 }
5352
5353 INTERCEPTOR(int, dlclose, void *handle) {
5354 void *ctx;
5355 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5356 int res = REAL(dlclose)(handle);
5357 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5358 return res;
5359 }
5360 #define INIT_DLOPEN_DLCLOSE \
5361 COMMON_INTERCEPT_FUNCTION(dlopen); \
5362 COMMON_INTERCEPT_FUNCTION(dlclose);
5363 #else
5364 #define INIT_DLOPEN_DLCLOSE
5365 #endif
5366
5367 #if SANITIZER_INTERCEPT_GETPASS
5368 INTERCEPTOR(char *, getpass, const char *prompt) {
5369 void *ctx;
5370 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5371 if (prompt)
5372 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5373 char *res = REAL(getpass)(prompt);
5374 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5375 return res;
5376 }
5377
5378 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5379 #else
5380 #define INIT_GETPASS
5381 #endif
5382
5383 #if SANITIZER_INTERCEPT_TIMERFD
5384 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5385 void *old_value) {
5386 void *ctx;
5387 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5388 old_value);
5389 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5390 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5391 if (res != -1 && old_value)
5392 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5393 return res;
5394 }
5395
5396 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5397 void *ctx;
5398 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5399 int res = REAL(timerfd_gettime)(fd, curr_value);
5400 if (res != -1 && curr_value)
5401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5402 return res;
5403 }
5404 #define INIT_TIMERFD \
5405 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5406 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5407 #else
5408 #define INIT_TIMERFD
5409 #endif
5410
5411 #if SANITIZER_INTERCEPT_MLOCKX
5412 // Linux kernel has a bug that leads to kernel deadlock if a process
5413 // maps TBs of memory and then calls mlock().
5414 static void MlockIsUnsupported() {
5415 static atomic_uint8_t printed;
5416 if (atomic_exchange(&printed, 1, memory_order_relaxed))
5417 return;
5418 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5419 SanitizerToolName);
5420 }
5421
5422 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5423 MlockIsUnsupported();
5424 return 0;
5425 }
5426
5427 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
5428 MlockIsUnsupported();
5429 return 0;
5430 }
5431
5432 INTERCEPTOR(int, mlockall, int flags) {
5433 MlockIsUnsupported();
5434 return 0;
5435 }
5436
5437 INTERCEPTOR(int, munlockall, void) {
5438 MlockIsUnsupported();
5439 return 0;
5440 }
5441
5442 #define INIT_MLOCKX \
5443 COMMON_INTERCEPT_FUNCTION(mlock); \
5444 COMMON_INTERCEPT_FUNCTION(munlock); \
5445 COMMON_INTERCEPT_FUNCTION(mlockall); \
5446 COMMON_INTERCEPT_FUNCTION(munlockall);
5447
5448 #else
5449 #define INIT_MLOCKX
5450 #endif // SANITIZER_INTERCEPT_MLOCKX
5451
5452 #if SANITIZER_INTERCEPT_FOPENCOOKIE
5453 struct WrappedCookie {
5454 void *real_cookie;
5455 __sanitizer_cookie_io_functions_t real_io_funcs;
5456 };
5457
5458 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
5459 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5460 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5461 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
5462 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
5463 }
5464
5465 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
5466 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5467 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5468 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
5469 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
5470 }
5471
5472 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
5473 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5474 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
5475 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5476 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
5477 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
5478 : -1;
5479 }
5480
5481 static int wrapped_close(void *cookie) {
5482 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
5483 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5484 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
5485 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
5486 InternalFree(wrapped_cookie);
5487 return res;
5488 }
5489
5490 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
5491 __sanitizer_cookie_io_functions_t io_funcs) {
5492 void *ctx;
5493 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
5494 WrappedCookie *wrapped_cookie =
5495 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
5496 wrapped_cookie->real_cookie = cookie;
5497 wrapped_cookie->real_io_funcs = io_funcs;
5498 __sanitizer_FILE *res =
5499 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
5500 wrapped_seek, wrapped_close});
5501 return res;
5502 }
5503
5504 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
5505 #else
5506 #define INIT_FOPENCOOKIE
5507 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
5508
5509 #if SANITIZER_INTERCEPT_SEM
5510 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
5511 void *ctx;
5512 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
5513 // Workaround a bug in glibc's "old" semaphore implementation by
5514 // zero-initializing the sem_t contents. This has to be done here because
5515 // interceptors bind to the lowest symbols version by default, hitting the
5516 // buggy code path while the non-sanitized build of the same code works fine.
5517 REAL(memset)(s, 0, sizeof(*s));
5518 int res = REAL(sem_init)(s, pshared, value);
5519 return res;
5520 }
5521
5522 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
5523 void *ctx;
5524 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
5525 int res = REAL(sem_destroy)(s);
5526 return res;
5527 }
5528
5529 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
5530 void *ctx;
5531 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
5532 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
5533 if (res == 0) {
5534 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5535 }
5536 return res;
5537 }
5538
5539 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
5540 void *ctx;
5541 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
5542 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
5543 if (res == 0) {
5544 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5545 }
5546 return res;
5547 }
5548
5549 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
5550 void *ctx;
5551 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
5552 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
5553 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
5554 if (res == 0) {
5555 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5556 }
5557 return res;
5558 }
5559
5560 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
5561 void *ctx;
5562 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
5563 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
5564 int res = REAL(sem_post)(s);
5565 return res;
5566 }
5567
5568 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
5569 void *ctx;
5570 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
5571 int res = REAL(sem_getvalue)(s, sval);
5572 if (res == 0) {
5573 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5574 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
5575 }
5576 return res;
5577 }
5578 #define INIT_SEM \
5579 COMMON_INTERCEPT_FUNCTION(sem_init); \
5580 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
5581 COMMON_INTERCEPT_FUNCTION(sem_wait); \
5582 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
5583 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
5584 COMMON_INTERCEPT_FUNCTION(sem_post); \
5585 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
5586 #else
5587 #define INIT_SEM
5588 #endif // SANITIZER_INTERCEPT_SEM
5589
5590 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
5591 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
5592 void *ctx;
5593 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
5594 int res = REAL(pthread_setcancelstate)(state, oldstate);
5595 if (res == 0)
5596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
5597 return res;
5598 }
5599
5600 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
5601 void *ctx;
5602 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
5603 int res = REAL(pthread_setcanceltype)(type, oldtype);
5604 if (res == 0)
5605 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
5606 return res;
5607 }
5608 #define INIT_PTHREAD_SETCANCEL \
5609 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
5610 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
5611 #else
5612 #define INIT_PTHREAD_SETCANCEL
5613 #endif
5614
5615 #if SANITIZER_INTERCEPT_MINCORE
5616 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
5617 void *ctx;
5618 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
5619 int res = REAL(mincore)(addr, length, vec);
5620 if (res == 0) {
5621 uptr page_size = GetPageSizeCached();
5622 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
5623 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
5624 }
5625 return res;
5626 }
5627 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
5628 #else
5629 #define INIT_MINCORE
5630 #endif
5631
5632 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
5633 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
5634 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5635 uptr flags) {
5636 void *ctx;
5637 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
5638 remote_iov, riovcnt, flags);
5639 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
5640 riovcnt, flags);
5641 if (res > 0)
5642 write_iovec(ctx, local_iov, liovcnt, res);
5643 return res;
5644 }
5645
5646 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
5647 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5648 uptr flags) {
5649 void *ctx;
5650 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
5651 remote_iov, riovcnt, flags);
5652 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
5653 riovcnt, flags);
5654 if (res > 0)
5655 read_iovec(ctx, local_iov, liovcnt, res);
5656 return res;
5657 }
5658 #define INIT_PROCESS_VM_READV \
5659 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
5660 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
5661 #else
5662 #define INIT_PROCESS_VM_READV
5663 #endif
5664
5665 #if SANITIZER_INTERCEPT_CTERMID
5666 INTERCEPTOR(char *, ctermid, char *s) {
5667 void *ctx;
5668 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
5669 char *res = REAL(ctermid)(s);
5670 if (res) {
5671 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5672 }
5673 return res;
5674 }
5675 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
5676 #else
5677 #define INIT_CTERMID
5678 #endif
5679
5680 #if SANITIZER_INTERCEPT_CTERMID_R
5681 INTERCEPTOR(char *, ctermid_r, char *s) {
5682 void *ctx;
5683 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
5684 char *res = REAL(ctermid_r)(s);
5685 if (res) {
5686 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5687 }
5688 return res;
5689 }
5690 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
5691 #else
5692 #define INIT_CTERMID_R
5693 #endif
5694
5695 #if SANITIZER_INTERCEPT_RECV_RECVFROM
5696 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
5697 void *ctx;
5698 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
5699 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5700 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
5701 if (res > 0) {
5702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5703 }
5704 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5705 return res;
5706 }
5707
5708 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
5709 void *srcaddr, int *addrlen) {
5710 void *ctx;
5711 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
5712 addrlen);
5713 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5714 SIZE_T srcaddr_sz;
5715 if (srcaddr) srcaddr_sz = *addrlen;
5716 (void)srcaddr_sz; // prevent "set but not used" warning
5717 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
5718 if (res > 0) {
5719 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5720 if (srcaddr)
5721 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
5722 Min((SIZE_T)*addrlen, srcaddr_sz));
5723 }
5724 return res;
5725 }
5726 #define INIT_RECV_RECVFROM \
5727 COMMON_INTERCEPT_FUNCTION(recv); \
5728 COMMON_INTERCEPT_FUNCTION(recvfrom);
5729 #else
5730 #define INIT_RECV_RECVFROM
5731 #endif
5732
5733 #if SANITIZER_INTERCEPT_SEND_SENDTO
5734 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
5735 void *ctx;
5736 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
5737 if (fd >= 0) {
5738 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5739 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5740 }
5741 SSIZE_T res = REAL(send)(fd, buf, len, flags);
5742 if (common_flags()->intercept_send && res > 0)
5743 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5744 return res;
5745 }
5746
5747 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
5748 void *dstaddr, int addrlen) {
5749 void *ctx;
5750 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
5751 if (fd >= 0) {
5752 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5753 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5754 }
5755 // Can't check dstaddr as it may have uninitialized padding at the end.
5756 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
5757 if (common_flags()->intercept_send && res > 0)
5758 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5759 return res;
5760 }
5761 #define INIT_SEND_SENDTO \
5762 COMMON_INTERCEPT_FUNCTION(send); \
5763 COMMON_INTERCEPT_FUNCTION(sendto);
5764 #else
5765 #define INIT_SEND_SENDTO
5766 #endif
5767
5768 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
5769 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
5770 void *ctx;
5771 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
5772 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5773 int res = REAL(eventfd_read)(fd, value);
5774 if (res == 0) {
5775 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
5776 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5777 }
5778 return res;
5779 }
5780 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
5781 void *ctx;
5782 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
5783 if (fd >= 0) {
5784 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5785 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5786 }
5787 int res = REAL(eventfd_write)(fd, value);
5788 return res;
5789 }
5790 #define INIT_EVENTFD_READ_WRITE \
5791 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
5792 COMMON_INTERCEPT_FUNCTION(eventfd_write)
5793 #else
5794 #define INIT_EVENTFD_READ_WRITE
5795 #endif
5796
5797 #if SANITIZER_INTERCEPT_STAT
5798 INTERCEPTOR(int, stat, const char *path, void *buf) {
5799 void *ctx;
5800 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
5801 if (common_flags()->intercept_stat)
5802 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5803 int res = REAL(stat)(path, buf);
5804 if (!res)
5805 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5806 return res;
5807 }
5808 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
5809 #else
5810 #define INIT_STAT
5811 #endif
5812
5813 #if SANITIZER_INTERCEPT___XSTAT
5814 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
5815 void *ctx;
5816 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
5817 if (common_flags()->intercept_stat)
5818 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5819 int res = REAL(__xstat)(version, path, buf);
5820 if (!res)
5821 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5822 return res;
5823 }
5824 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
5825 #else
5826 #define INIT___XSTAT
5827 #endif
5828
5829 #if SANITIZER_INTERCEPT___XSTAT64
5830 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
5831 void *ctx;
5832 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
5833 if (common_flags()->intercept_stat)
5834 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5835 int res = REAL(__xstat64)(version, path, buf);
5836 if (!res)
5837 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5838 return res;
5839 }
5840 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
5841 #else
5842 #define INIT___XSTAT64
5843 #endif
5844
5845 #if SANITIZER_INTERCEPT___LXSTAT
5846 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
5847 void *ctx;
5848 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
5849 if (common_flags()->intercept_stat)
5850 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5851 int res = REAL(__lxstat)(version, path, buf);
5852 if (!res)
5853 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5854 return res;
5855 }
5856 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
5857 #else
5858 #define INIT___LXSTAT
5859 #endif
5860
5861 #if SANITIZER_INTERCEPT___LXSTAT64
5862 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
5863 void *ctx;
5864 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
5865 if (common_flags()->intercept_stat)
5866 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5867 int res = REAL(__lxstat64)(version, path, buf);
5868 if (!res)
5869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5870 return res;
5871 }
5872 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
5873 #else
5874 #define INIT___LXSTAT64
5875 #endif
5876
5877 // FIXME: add other *stat interceptor
5878
5879 static void InitializeCommonInterceptors() {
5880 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
5881 interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
5882
5883 INIT_TEXTDOMAIN;
5884 INIT_STRLEN;
5885 INIT_STRNLEN;
5886 INIT_STRCMP;
5887 INIT_STRNCMP;
5888 INIT_STRCASECMP;
5889 INIT_STRNCASECMP;
5890 INIT_STRSTR;
5891 INIT_STRCASESTR;
5892 INIT_STRCHR;
5893 INIT_STRCHRNUL;
5894 INIT_STRRCHR;
5895 INIT_STRSPN;
5896 INIT_STRPBRK;
5897 INIT_MEMSET;
5898 INIT_MEMMOVE;
5899 INIT_MEMCPY;
5900 INIT_MEMCHR;
5901 INIT_MEMCMP;
5902 INIT_MEMRCHR;
5903 INIT_MEMMEM;
5904 INIT_READ;
5905 INIT_PREAD;
5906 INIT_PREAD64;
5907 INIT_READV;
5908 INIT_PREADV;
5909 INIT_PREADV64;
5910 INIT_WRITE;
5911 INIT_PWRITE;
5912 INIT_PWRITE64;
5913 INIT_WRITEV;
5914 INIT_PWRITEV;
5915 INIT_PWRITEV64;
5916 INIT_PRCTL;
5917 INIT_LOCALTIME_AND_FRIENDS;
5918 INIT_STRPTIME;
5919 INIT_SCANF;
5920 INIT_ISOC99_SCANF;
5921 INIT_PRINTF;
5922 INIT_PRINTF_L;
5923 INIT_ISOC99_PRINTF;
5924 INIT_FREXP;
5925 INIT_FREXPF_FREXPL;
5926 INIT_GETPWNAM_AND_FRIENDS;
5927 INIT_GETPWNAM_R_AND_FRIENDS;
5928 INIT_GETPWENT;
5929 INIT_FGETPWENT;
5930 INIT_GETPWENT_R;
5931 INIT_SETPWENT;
5932 INIT_CLOCK_GETTIME;
5933 INIT_GETITIMER;
5934 INIT_TIME;
5935 INIT_GLOB;
5936 INIT_WAIT;
5937 INIT_WAIT4;
5938 INIT_INET;
5939 INIT_PTHREAD_GETSCHEDPARAM;
5940 INIT_GETADDRINFO;
5941 INIT_GETNAMEINFO;
5942 INIT_GETSOCKNAME;
5943 INIT_GETHOSTBYNAME;
5944 INIT_GETHOSTBYNAME_R;
5945 INIT_GETHOSTBYNAME2_R;
5946 INIT_GETHOSTBYADDR_R;
5947 INIT_GETHOSTENT_R;
5948 INIT_GETSOCKOPT;
5949 INIT_ACCEPT;
5950 INIT_ACCEPT4;
5951 INIT_MODF;
5952 INIT_RECVMSG;
5953 INIT_SENDMSG;
5954 INIT_GETPEERNAME;
5955 INIT_IOCTL;
5956 INIT_INET_ATON;
5957 INIT_SYSINFO;
5958 INIT_READDIR;
5959 INIT_READDIR64;
5960 INIT_PTRACE;
5961 INIT_SETLOCALE;
5962 INIT_GETCWD;
5963 INIT_GET_CURRENT_DIR_NAME;
5964 INIT_STRTOIMAX;
5965 INIT_MBSTOWCS;
5966 INIT_MBSNRTOWCS;
5967 INIT_WCSTOMBS;
5968 INIT_WCSNRTOMBS;
5969 INIT_WCRTOMB;
5970 INIT_TCGETATTR;
5971 INIT_REALPATH;
5972 INIT_CANONICALIZE_FILE_NAME;
5973 INIT_CONFSTR;
5974 INIT_SCHED_GETAFFINITY;
5975 INIT_SCHED_GETPARAM;
5976 INIT_STRERROR;
5977 INIT_STRERROR_R;
5978 INIT_XPG_STRERROR_R;
5979 INIT_SCANDIR;
5980 INIT_SCANDIR64;
5981 INIT_GETGROUPS;
5982 INIT_POLL;
5983 INIT_PPOLL;
5984 INIT_WORDEXP;
5985 INIT_SIGWAIT;
5986 INIT_SIGWAITINFO;
5987 INIT_SIGTIMEDWAIT;
5988 INIT_SIGSETOPS;
5989 INIT_SIGPENDING;
5990 INIT_SIGPROCMASK;
5991 INIT_BACKTRACE;
5992 INIT__EXIT;
5993 INIT_PTHREAD_MUTEX_LOCK;
5994 INIT_PTHREAD_MUTEX_UNLOCK;
5995 INIT_GETMNTENT;
5996 INIT_GETMNTENT_R;
5997 INIT_STATFS;
5998 INIT_STATFS64;
5999 INIT_STATVFS;
6000 INIT_STATVFS64;
6001 INIT_INITGROUPS;
6002 INIT_ETHER_NTOA_ATON;
6003 INIT_ETHER_HOST;
6004 INIT_ETHER_R;
6005 INIT_SHMCTL;
6006 INIT_RANDOM_R;
6007 INIT_PTHREAD_ATTR_GET;
6008 INIT_PTHREAD_ATTR_GETINHERITSCHED;
6009 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
6010 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
6011 INIT_PTHREAD_MUTEXATTR_GETTYPE;
6012 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
6013 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
6014 INIT_PTHREAD_MUTEXATTR_GETROBUST;
6015 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
6016 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
6017 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
6018 INIT_PTHREAD_CONDATTR_GETPSHARED;
6019 INIT_PTHREAD_CONDATTR_GETCLOCK;
6020 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
6021 INIT_TMPNAM;
6022 INIT_TMPNAM_R;
6023 INIT_TTYNAME_R;
6024 INIT_TEMPNAM;
6025 INIT_PTHREAD_SETNAME_NP;
6026 INIT_SINCOS;
6027 INIT_REMQUO;
6028 INIT_LGAMMA;
6029 INIT_LGAMMA_R;
6030 INIT_LGAMMAL_R;
6031 INIT_DRAND48_R;
6032 INIT_RAND_R;
6033 INIT_GETLINE;
6034 INIT_ICONV;
6035 INIT_TIMES;
6036 INIT_TLS_GET_ADDR;
6037 INIT_LISTXATTR;
6038 INIT_GETXATTR;
6039 INIT_GETRESID;
6040 INIT_GETIFADDRS;
6041 INIT_IF_INDEXTONAME;
6042 INIT_CAPGET;
6043 INIT_AEABI_MEM;
6044 INIT___BZERO;
6045 INIT_FTIME;
6046 INIT_XDR;
6047 INIT_TSEARCH;
6048 INIT_LIBIO_INTERNALS;
6049 INIT_FOPEN;
6050 INIT_FOPEN64;
6051 INIT_OPEN_MEMSTREAM;
6052 INIT_OBSTACK;
6053 INIT_FFLUSH;
6054 INIT_FCLOSE;
6055 INIT_DLOPEN_DLCLOSE;
6056 INIT_GETPASS;
6057 INIT_TIMERFD;
6058 INIT_MLOCKX;
6059 INIT_FOPENCOOKIE;
6060 INIT_SEM;
6061 INIT_PTHREAD_SETCANCEL;
6062 INIT_MINCORE;
6063 INIT_PROCESS_VM_READV;
6064 INIT_CTERMID;
6065 INIT_CTERMID_R;
6066 INIT_RECV_RECVFROM;
6067 INIT_SEND_SENDTO;
6068 INIT_STAT;
6069 INIT_EVENTFD_READ_WRITE;
6070 INIT___XSTAT;
6071 INIT___XSTAT64;
6072 INIT___LXSTAT;
6073 INIT___LXSTAT64;
6074 // FIXME: add other *stat interceptors.
6075 }