1 //===-- asan_interceptors.cc ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // Intercept various libc functions.
13 //===----------------------------------------------------------------------===//
14 #include "asan_interceptors.h"
16 #include "asan_allocator.h"
17 #include "asan_internal.h"
18 #include "asan_mapping.h"
19 #include "asan_poisoning.h"
20 #include "asan_report.h"
21 #include "asan_stack.h"
22 #include "asan_stats.h"
23 #include "sanitizer_common/sanitizer_libc.h"
27 // Return true if we can quickly decide that the region is unpoisoned.
28 static inline bool QuickCheckForUnpoisonedRegion(uptr beg
, uptr size
) {
29 if (size
== 0) return true;
31 return !AddressIsPoisoned(beg
) &&
32 !AddressIsPoisoned(beg
+ size
- 1) &&
33 !AddressIsPoisoned(beg
+ size
/ 2);
37 // We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
38 // and ASAN_WRITE_RANGE as macro instead of function so
39 // that no extra frames are created, and stack trace contains
40 // relevant information only.
41 // We check all shadow bytes.
42 #define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \
43 uptr __offset = (uptr)(offset); \
44 uptr __size = (uptr)(size); \
46 if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \
47 (__bad = __asan_region_is_poisoned(__offset, __size))) { \
48 GET_CURRENT_PC_BP_SP; \
49 __asan_report_error(pc, bp, sp, __bad, isWrite, __size); \
53 #define ASAN_READ_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, false)
54 #define ASAN_WRITE_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, true)
56 // Behavior of functions like "memcpy" or "strcpy" is undefined
57 // if memory intervals overlap. We report error in this case.
58 // Macro is used to avoid creation of new frames.
59 static inline bool RangesOverlap(const char *offset1
, uptr length1
,
60 const char *offset2
, uptr length2
) {
61 return !((offset1
+ length1
<= offset2
) || (offset2
+ length2
<= offset1
));
63 #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
64 const char *offset1 = (const char*)_offset1; \
65 const char *offset2 = (const char*)_offset2; \
66 if (RangesOverlap(offset1, length1, offset2, length2)) { \
67 GET_STACK_TRACE_FATAL_HERE; \
68 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
69 offset2, length2, &stack); \
73 static inline uptr
MaybeRealStrnlen(const char *s
, uptr maxlen
) {
74 #if ASAN_INTERCEPT_STRNLEN
75 if (REAL(strnlen
) != 0) {
76 return REAL(strnlen
)(s
, maxlen
);
79 return internal_strnlen(s
, maxlen
);
82 void SetThreadName(const char *name
) {
83 AsanThread
*t
= GetCurrentThread();
85 asanThreadRegistry().SetThreadName(t
->tid(), name
);
89 // FIXME: ask frontend whether we need to return failure.
95 // ---------------------- Wrappers ---------------- {{{1
96 using namespace __asan
; // NOLINT
98 DECLARE_REAL_AND_INTERCEPTOR(void *, malloc
, uptr
)
99 DECLARE_REAL_AND_INTERCEPTOR(void, free
, void *)
102 #define ASAN_INTERCEPT_FUNC(name) \
104 if ((!INTERCEPT_FUNCTION(name) || !REAL(name))) \
105 VReport(1, "AddressSanitizer: failed to intercept '" #name "'\n"); \
108 // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
109 #define ASAN_INTERCEPT_FUNC(name)
110 #endif // SANITIZER_MAC
112 #define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name)
113 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
114 ASAN_WRITE_RANGE(ptr, size)
115 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) ASAN_READ_RANGE(ptr, size)
116 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
118 if (asan_init_is_running) return REAL(func)(__VA_ARGS__); \
121 if (SANITIZER_MAC && !asan_inited) return REAL(func)(__VA_ARGS__); \
122 ENSURE_ASAN_INITED(); \
124 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
127 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
130 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
133 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
134 // Should be asanThreadRegistry().SetThreadNameByUserId(thread, name)
135 // But asan does not remember UserId's for threads (pthread_t);
136 // and remembers all ever existed threads, so the linear search by UserId
138 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
141 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
142 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
143 #include "sanitizer_common/sanitizer_common_interceptors.inc"
145 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(p, s)
146 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(p, s)
147 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
152 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
157 #include "sanitizer_common/sanitizer_common_syscalls.inc"
159 static thread_return_t THREAD_CALLING_CONV
asan_thread_start(void *arg
) {
160 AsanThread
*t
= (AsanThread
*)arg
;
162 return t
->ThreadStart(GetTid());
165 #if ASAN_INTERCEPT_PTHREAD_CREATE
166 INTERCEPTOR(int, pthread_create
, void *thread
,
167 void *attr
, void *(*start_routine
)(void*), void *arg
) {
168 EnsureMainThreadIDIsCorrect();
169 // Strict init-order checking in thread-hostile.
170 if (flags()->strict_init_order
)
171 StopInitOrderChecking();
172 GET_STACK_TRACE_THREAD
;
175 REAL(pthread_attr_getdetachstate
)(attr
, &detached
);
177 u32 current_tid
= GetCurrentTidOrInvalid();
178 AsanThread
*t
= AsanThread::Create(start_routine
, arg
);
179 CreateThreadContextArgs args
= { t
, &stack
};
180 asanThreadRegistry().CreateThread(*(uptr
*)t
, detached
, current_tid
, &args
);
181 return REAL(pthread_create
)(thread
, attr
, asan_thread_start
, t
);
183 #endif // ASAN_INTERCEPT_PTHREAD_CREATE
185 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
187 #if SANITIZER_ANDROID
188 INTERCEPTOR(void*, bsd_signal
, int signum
, void *handler
) {
189 if (!AsanInterceptsSignal(signum
) ||
190 common_flags()->allow_user_segv_handler
) {
191 return REAL(bsd_signal
)(signum
, handler
);
196 INTERCEPTOR(void*, signal
, int signum
, void *handler
) {
197 if (!AsanInterceptsSignal(signum
) ||
198 common_flags()->allow_user_segv_handler
) {
199 return REAL(signal
)(signum
, handler
);
205 INTERCEPTOR(int, sigaction
, int signum
, const struct sigaction
*act
,
206 struct sigaction
*oldact
) {
207 if (!AsanInterceptsSignal(signum
) ||
208 common_flags()->allow_user_segv_handler
) {
209 return REAL(sigaction
)(signum
, act
, oldact
);
214 namespace __sanitizer
{
215 int real_sigaction(int signum
, const void *act
, void *oldact
) {
216 return REAL(sigaction
)(signum
,
217 (struct sigaction
*)act
, (struct sigaction
*)oldact
);
219 } // namespace __sanitizer
221 #elif SANITIZER_POSIX
222 // We need to have defined REAL(sigaction) on posix systems.
223 DEFINE_REAL(int, sigaction
, int signum
, const struct sigaction
*act
,
224 struct sigaction
*oldact
)
225 #endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
227 #if ASAN_INTERCEPT_SWAPCONTEXT
228 static void ClearShadowMemoryForContextStack(uptr stack
, uptr ssize
) {
229 // Align to page size.
230 uptr PageSize
= GetPageSizeCached();
231 uptr bottom
= stack
& ~(PageSize
- 1);
232 ssize
+= stack
- bottom
;
233 ssize
= RoundUpTo(ssize
, PageSize
);
234 static const uptr kMaxSaneContextStackSize
= 1 << 22; // 4 Mb
235 if (ssize
&& ssize
<= kMaxSaneContextStackSize
) {
236 PoisonShadow(bottom
, ssize
, 0);
240 INTERCEPTOR(int, swapcontext
, struct ucontext_t
*oucp
,
241 struct ucontext_t
*ucp
) {
242 static bool reported_warning
= false;
243 if (!reported_warning
) {
244 Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
245 "functions and may produce false positives in some cases!\n");
246 reported_warning
= true;
248 // Clear shadow memory for new context (it may share stack
249 // with current context).
251 ReadContextStack(ucp
, &stack
, &ssize
);
252 ClearShadowMemoryForContextStack(stack
, ssize
);
253 int res
= REAL(swapcontext
)(oucp
, ucp
);
254 // swapcontext technically does not return, but program may swap context to
255 // "oucp" later, that would look as if swapcontext() returned 0.
256 // We need to clear shadow for ucp once again, as it may be in arbitrary
258 ClearShadowMemoryForContextStack(stack
, ssize
);
261 #endif // ASAN_INTERCEPT_SWAPCONTEXT
263 INTERCEPTOR(void, longjmp
, void *env
, int val
) {
264 __asan_handle_no_return();
265 REAL(longjmp
)(env
, val
);
268 #if ASAN_INTERCEPT__LONGJMP
269 INTERCEPTOR(void, _longjmp
, void *env
, int val
) {
270 __asan_handle_no_return();
271 REAL(_longjmp
)(env
, val
);
275 #if ASAN_INTERCEPT_SIGLONGJMP
276 INTERCEPTOR(void, siglongjmp
, void *env
, int val
) {
277 __asan_handle_no_return();
278 REAL(siglongjmp
)(env
, val
);
282 #if ASAN_INTERCEPT___CXA_THROW
283 INTERCEPTOR(void, __cxa_throw
, void *a
, void *b
, void *c
) {
284 CHECK(REAL(__cxa_throw
));
285 __asan_handle_no_return();
286 REAL(__cxa_throw
)(a
, b
, c
);
290 // intercept mlock and friends.
291 // Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
292 // All functions return 0 (success).
293 static void MlockIsUnsupported() {
294 static bool printed
= false;
298 "INFO: AddressSanitizer ignores "
299 "mlock/mlockall/munlock/munlockall\n");
302 INTERCEPTOR(int, mlock
, const void *addr
, uptr len
) {
303 MlockIsUnsupported();
307 INTERCEPTOR(int, munlock
, const void *addr
, uptr len
) {
308 MlockIsUnsupported();
312 INTERCEPTOR(int, mlockall
, int flags
) {
313 MlockIsUnsupported();
317 INTERCEPTOR(int, munlockall
, void) {
318 MlockIsUnsupported();
322 static inline int CharCmp(unsigned char c1
, unsigned char c2
) {
323 return (c1
== c2
) ? 0 : (c1
< c2
) ? -1 : 1;
326 INTERCEPTOR(int, memcmp
, const void *a1
, const void *a2
, uptr size
) {
327 if (!asan_inited
) return internal_memcmp(a1
, a2
, size
);
328 ENSURE_ASAN_INITED();
329 if (flags()->replace_intrin
) {
330 if (flags()->strict_memcmp
) {
331 // Check the entire regions even if the first bytes of the buffers are
333 ASAN_READ_RANGE(a1
, size
);
334 ASAN_READ_RANGE(a2
, size
);
335 // Fallthrough to REAL(memcmp) below.
337 unsigned char c1
= 0, c2
= 0;
338 const unsigned char *s1
= (const unsigned char*)a1
;
339 const unsigned char *s2
= (const unsigned char*)a2
;
341 for (i
= 0; i
< size
; i
++) {
346 ASAN_READ_RANGE(s1
, Min(i
+ 1, size
));
347 ASAN_READ_RANGE(s2
, Min(i
+ 1, size
));
348 return CharCmp(c1
, c2
);
351 return REAL(memcmp(a1
, a2
, size
));
354 #define MEMMOVE_BODY { \
355 if (!asan_inited) return internal_memmove(to, from, size); \
356 if (asan_init_is_running) { \
357 return REAL(memmove)(to, from, size); \
359 ENSURE_ASAN_INITED(); \
360 if (flags()->replace_intrin) { \
361 ASAN_READ_RANGE(from, size); \
362 ASAN_WRITE_RANGE(to, size); \
364 return internal_memmove(to, from, size); \
367 INTERCEPTOR(void*, memmove
, void *to
, const void *from
, uptr size
) MEMMOVE_BODY
369 INTERCEPTOR(void*, memcpy
, void *to
, const void *from
, uptr size
) {
371 if (!asan_inited
) return internal_memcpy(to
, from
, size
);
372 // memcpy is called during __asan_init() from the internals
374 if (asan_init_is_running
) {
375 return REAL(memcpy
)(to
, from
, size
);
377 ENSURE_ASAN_INITED();
378 if (flags()->replace_intrin
) {
380 // We do not treat memcpy with to==from as a bug.
381 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
382 CHECK_RANGES_OVERLAP("memcpy", to
, size
, from
, size
);
384 ASAN_READ_RANGE(from
, size
);
385 ASAN_WRITE_RANGE(to
, size
);
387 return REAL(memcpy
)(to
, from
, size
);
389 // At least on 10.7 and 10.8 both memcpy() and memmove() are being replaced
390 // with WRAP(memcpy). As a result, false positives are reported for memmove()
391 // calls. If we just disable error reporting with
392 // ASAN_OPTIONS=replace_intrin=0, memmove() is still replaced with
393 // internal_memcpy(), which may lead to crashes, see
394 // http://llvm.org/bugs/show_bug.cgi?id=16362.
396 #endif // !SANITIZER_MAC
399 INTERCEPTOR(void*, memset
, void *block
, int c
, uptr size
) {
400 if (!asan_inited
) return internal_memset(block
, c
, size
);
401 // memset is called inside Printf.
402 if (asan_init_is_running
) {
403 return REAL(memset
)(block
, c
, size
);
405 ENSURE_ASAN_INITED();
406 if (flags()->replace_intrin
) {
407 ASAN_WRITE_RANGE(block
, size
);
409 return REAL(memset
)(block
, c
, size
);
412 INTERCEPTOR(char*, strchr
, const char *str
, int c
) {
413 if (!asan_inited
) return internal_strchr(str
, c
);
414 // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is
416 if (asan_init_is_running
) {
417 return REAL(strchr
)(str
, c
);
419 ENSURE_ASAN_INITED();
420 char *result
= REAL(strchr
)(str
, c
);
421 if (flags()->replace_str
) {
422 uptr bytes_read
= (result
? result
- str
: REAL(strlen
)(str
)) + 1;
423 ASAN_READ_RANGE(str
, bytes_read
);
428 #if ASAN_INTERCEPT_INDEX
429 # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
430 INTERCEPTOR(char*, index
, const char *string
, int c
)
431 ALIAS(WRAPPER_NAME(strchr
));
434 DECLARE_REAL(char*, index
, const char *string
, int c
)
435 OVERRIDE_FUNCTION(index
, strchr
);
437 DEFINE_REAL(char*, index
, const char *string
, int c
)
440 #endif // ASAN_INTERCEPT_INDEX
442 // For both strcat() and strncat() we need to check the validity of |to|
443 // argument irrespective of the |from| length.
444 INTERCEPTOR(char*, strcat
, char *to
, const char *from
) { // NOLINT
445 ENSURE_ASAN_INITED();
446 if (flags()->replace_str
) {
447 uptr from_length
= REAL(strlen
)(from
);
448 ASAN_READ_RANGE(from
, from_length
+ 1);
449 uptr to_length
= REAL(strlen
)(to
);
450 ASAN_READ_RANGE(to
, to_length
);
451 ASAN_WRITE_RANGE(to
+ to_length
, from_length
+ 1);
452 // If the copying actually happens, the |from| string should not overlap
453 // with the resulting string starting at |to|, which has a length of
454 // to_length + from_length + 1.
455 if (from_length
> 0) {
456 CHECK_RANGES_OVERLAP("strcat", to
, from_length
+ to_length
+ 1,
457 from
, from_length
+ 1);
460 return REAL(strcat
)(to
, from
); // NOLINT
463 INTERCEPTOR(char*, strncat
, char *to
, const char *from
, uptr size
) {
464 ENSURE_ASAN_INITED();
465 if (flags()->replace_str
) {
466 uptr from_length
= MaybeRealStrnlen(from
, size
);
467 uptr copy_length
= Min(size
, from_length
+ 1);
468 ASAN_READ_RANGE(from
, copy_length
);
469 uptr to_length
= REAL(strlen
)(to
);
470 ASAN_READ_RANGE(to
, to_length
);
471 ASAN_WRITE_RANGE(to
+ to_length
, from_length
+ 1);
472 if (from_length
> 0) {
473 CHECK_RANGES_OVERLAP("strncat", to
, to_length
+ copy_length
+ 1,
477 return REAL(strncat
)(to
, from
, size
);
480 INTERCEPTOR(char*, strcpy
, char *to
, const char *from
) { // NOLINT
482 if (!asan_inited
) return REAL(strcpy
)(to
, from
); // NOLINT
484 // strcpy is called from malloc_default_purgeable_zone()
485 // in __asan::ReplaceSystemAlloc() on Mac.
486 if (asan_init_is_running
) {
487 return REAL(strcpy
)(to
, from
); // NOLINT
489 ENSURE_ASAN_INITED();
490 if (flags()->replace_str
) {
491 uptr from_size
= REAL(strlen
)(from
) + 1;
492 CHECK_RANGES_OVERLAP("strcpy", to
, from_size
, from
, from_size
);
493 ASAN_READ_RANGE(from
, from_size
);
494 ASAN_WRITE_RANGE(to
, from_size
);
496 return REAL(strcpy
)(to
, from
); // NOLINT
499 #if ASAN_INTERCEPT_STRDUP
500 INTERCEPTOR(char*, strdup
, const char *s
) {
501 if (!asan_inited
) return internal_strdup(s
);
502 ENSURE_ASAN_INITED();
503 uptr length
= REAL(strlen
)(s
);
504 if (flags()->replace_str
) {
505 ASAN_READ_RANGE(s
, length
+ 1);
507 GET_STACK_TRACE_MALLOC
;
508 void *new_mem
= asan_malloc(length
+ 1, &stack
);
509 REAL(memcpy
)(new_mem
, s
, length
+ 1);
510 return reinterpret_cast<char*>(new_mem
);
514 INTERCEPTOR(uptr
, strlen
, const char *s
) {
515 if (!asan_inited
) return internal_strlen(s
);
516 // strlen is called from malloc_default_purgeable_zone()
517 // in __asan::ReplaceSystemAlloc() on Mac.
518 if (asan_init_is_running
) {
519 return REAL(strlen
)(s
);
521 ENSURE_ASAN_INITED();
522 uptr length
= REAL(strlen
)(s
);
523 if (flags()->replace_str
) {
524 ASAN_READ_RANGE(s
, length
+ 1);
529 INTERCEPTOR(uptr
, wcslen
, const wchar_t *s
) {
530 uptr length
= REAL(wcslen
)(s
);
531 if (!asan_init_is_running
) {
532 ENSURE_ASAN_INITED();
533 ASAN_READ_RANGE(s
, (length
+ 1) * sizeof(wchar_t));
538 INTERCEPTOR(char*, strncpy
, char *to
, const char *from
, uptr size
) {
539 ENSURE_ASAN_INITED();
540 if (flags()->replace_str
) {
541 uptr from_size
= Min(size
, MaybeRealStrnlen(from
, size
) + 1);
542 CHECK_RANGES_OVERLAP("strncpy", to
, from_size
, from
, from_size
);
543 ASAN_READ_RANGE(from
, from_size
);
544 ASAN_WRITE_RANGE(to
, size
);
546 return REAL(strncpy
)(to
, from
, size
);
549 #if ASAN_INTERCEPT_STRNLEN
550 INTERCEPTOR(uptr
, strnlen
, const char *s
, uptr maxlen
) {
551 ENSURE_ASAN_INITED();
552 uptr length
= REAL(strnlen
)(s
, maxlen
);
553 if (flags()->replace_str
) {
554 ASAN_READ_RANGE(s
, Min(length
+ 1, maxlen
));
558 #endif // ASAN_INTERCEPT_STRNLEN
560 static inline bool IsValidStrtolBase(int base
) {
561 return (base
== 0) || (2 <= base
&& base
<= 36);
564 static inline void FixRealStrtolEndptr(const char *nptr
, char **endptr
) {
566 if (nptr
== *endptr
) {
567 // No digits were found at strtol call, we need to find out the last
568 // symbol accessed by strtoll on our own.
569 // We get this symbol by skipping leading blanks and optional +/- sign.
570 while (IsSpace(*nptr
)) nptr
++;
571 if (*nptr
== '+' || *nptr
== '-') nptr
++;
572 *endptr
= (char*)nptr
;
574 CHECK(*endptr
>= nptr
);
577 INTERCEPTOR(long, strtol
, const char *nptr
, // NOLINT
578 char **endptr
, int base
) {
579 ENSURE_ASAN_INITED();
580 if (!flags()->replace_str
) {
581 return REAL(strtol
)(nptr
, endptr
, base
);
584 long result
= REAL(strtol
)(nptr
, &real_endptr
, base
); // NOLINT
586 *endptr
= real_endptr
;
588 if (IsValidStrtolBase(base
)) {
589 FixRealStrtolEndptr(nptr
, &real_endptr
);
590 ASAN_READ_RANGE(nptr
, (real_endptr
- nptr
) + 1);
595 INTERCEPTOR(int, atoi
, const char *nptr
) {
597 if (!asan_inited
) return REAL(atoi
)(nptr
);
599 ENSURE_ASAN_INITED();
600 if (!flags()->replace_str
) {
601 return REAL(atoi
)(nptr
);
604 // "man atoi" tells that behavior of atoi(nptr) is the same as
605 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
606 // parsed integer can't be stored in *long* type (even if it's
607 // different from int). So, we just imitate this behavior.
608 int result
= REAL(strtol
)(nptr
, &real_endptr
, 10);
609 FixRealStrtolEndptr(nptr
, &real_endptr
);
610 ASAN_READ_RANGE(nptr
, (real_endptr
- nptr
) + 1);
614 INTERCEPTOR(long, atol
, const char *nptr
) { // NOLINT
616 if (!asan_inited
) return REAL(atol
)(nptr
);
618 ENSURE_ASAN_INITED();
619 if (!flags()->replace_str
) {
620 return REAL(atol
)(nptr
);
623 long result
= REAL(strtol
)(nptr
, &real_endptr
, 10); // NOLINT
624 FixRealStrtolEndptr(nptr
, &real_endptr
);
625 ASAN_READ_RANGE(nptr
, (real_endptr
- nptr
) + 1);
629 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
630 INTERCEPTOR(long long, strtoll
, const char *nptr
, // NOLINT
631 char **endptr
, int base
) {
632 ENSURE_ASAN_INITED();
633 if (!flags()->replace_str
) {
634 return REAL(strtoll
)(nptr
, endptr
, base
);
637 long long result
= REAL(strtoll
)(nptr
, &real_endptr
, base
); // NOLINT
639 *endptr
= real_endptr
;
641 // If base has unsupported value, strtoll can exit with EINVAL
642 // without reading any characters. So do additional checks only
644 if (IsValidStrtolBase(base
)) {
645 FixRealStrtolEndptr(nptr
, &real_endptr
);
646 ASAN_READ_RANGE(nptr
, (real_endptr
- nptr
) + 1);
651 INTERCEPTOR(long long, atoll
, const char *nptr
) { // NOLINT
652 ENSURE_ASAN_INITED();
653 if (!flags()->replace_str
) {
654 return REAL(atoll
)(nptr
);
657 long long result
= REAL(strtoll
)(nptr
, &real_endptr
, 10); // NOLINT
658 FixRealStrtolEndptr(nptr
, &real_endptr
);
659 ASAN_READ_RANGE(nptr
, (real_endptr
- nptr
) + 1);
662 #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
664 static void AtCxaAtexit(void *unused
) {
666 StopInitOrderChecking();
669 #if ASAN_INTERCEPT___CXA_ATEXIT
670 INTERCEPTOR(int, __cxa_atexit
, void (*func
)(void *), void *arg
,
673 if (!asan_inited
) return REAL(__cxa_atexit
)(func
, arg
, dso_handle
);
675 ENSURE_ASAN_INITED();
676 int res
= REAL(__cxa_atexit
)(func
, arg
, dso_handle
);
677 REAL(__cxa_atexit
)(AtCxaAtexit
, 0, 0);
680 #endif // ASAN_INTERCEPT___CXA_ATEXIT
682 #if SANITIZER_WINDOWS
683 INTERCEPTOR_WINAPI(DWORD
, CreateThread
,
684 void* security
, uptr stack_size
,
685 DWORD (__stdcall
*start_routine
)(void*), void* arg
,
686 DWORD thr_flags
, void* tid
) {
687 // Strict init-order checking in thread-hostile.
688 if (flags()->strict_init_order
)
689 StopInitOrderChecking();
690 GET_STACK_TRACE_THREAD
;
691 u32 current_tid
= GetCurrentTidOrInvalid();
692 AsanThread
*t
= AsanThread::Create(start_routine
, arg
);
693 CreateThreadContextArgs args
= { t
, &stack
};
694 bool detached
= false; // FIXME: how can we determine it on Windows?
695 asanThreadRegistry().CreateThread(*(uptr
*)t
, detached
, current_tid
, &args
);
696 return REAL(CreateThread
)(security
, stack_size
,
697 asan_thread_start
, t
, thr_flags
, tid
);
701 void InitializeWindowsInterceptors() {
702 ASAN_INTERCEPT_FUNC(CreateThread
);
705 } // namespace __asan
708 // ---------------------- InitializeAsanInterceptors ---------------- {{{1
710 void InitializeAsanInterceptors() {
711 static bool was_called_once
;
712 CHECK(was_called_once
== false);
713 was_called_once
= true;
714 SANITIZER_COMMON_INTERCEPTORS_INIT
;
716 // Intercept mem* functions.
717 ASAN_INTERCEPT_FUNC(memcmp
);
718 ASAN_INTERCEPT_FUNC(memmove
);
719 ASAN_INTERCEPT_FUNC(memset
);
720 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE
) {
721 ASAN_INTERCEPT_FUNC(memcpy
);
724 // Intercept str* functions.
725 ASAN_INTERCEPT_FUNC(strcat
); // NOLINT
726 ASAN_INTERCEPT_FUNC(strchr
);
727 ASAN_INTERCEPT_FUNC(strcpy
); // NOLINT
728 ASAN_INTERCEPT_FUNC(strlen
);
729 ASAN_INTERCEPT_FUNC(wcslen
);
730 ASAN_INTERCEPT_FUNC(strncat
);
731 ASAN_INTERCEPT_FUNC(strncpy
);
732 #if ASAN_INTERCEPT_STRDUP
733 ASAN_INTERCEPT_FUNC(strdup
);
735 #if ASAN_INTERCEPT_STRNLEN
736 ASAN_INTERCEPT_FUNC(strnlen
);
738 #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
739 ASAN_INTERCEPT_FUNC(index
);
742 ASAN_INTERCEPT_FUNC(atoi
);
743 ASAN_INTERCEPT_FUNC(atol
);
744 ASAN_INTERCEPT_FUNC(strtol
);
745 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
746 ASAN_INTERCEPT_FUNC(atoll
);
747 ASAN_INTERCEPT_FUNC(strtoll
);
750 #if ASAN_INTERCEPT_MLOCKX
751 // Intercept mlock/munlock.
752 ASAN_INTERCEPT_FUNC(mlock
);
753 ASAN_INTERCEPT_FUNC(munlock
);
754 ASAN_INTERCEPT_FUNC(mlockall
);
755 ASAN_INTERCEPT_FUNC(munlockall
);
758 // Intecept signal- and jump-related functions.
759 ASAN_INTERCEPT_FUNC(longjmp
);
760 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
761 ASAN_INTERCEPT_FUNC(sigaction
);
762 #if SANITIZER_ANDROID
763 ASAN_INTERCEPT_FUNC(bsd_signal
);
765 ASAN_INTERCEPT_FUNC(signal
);
768 #if ASAN_INTERCEPT_SWAPCONTEXT
769 ASAN_INTERCEPT_FUNC(swapcontext
);
771 #if ASAN_INTERCEPT__LONGJMP
772 ASAN_INTERCEPT_FUNC(_longjmp
);
774 #if ASAN_INTERCEPT_SIGLONGJMP
775 ASAN_INTERCEPT_FUNC(siglongjmp
);
778 // Intercept exception handling functions.
779 #if ASAN_INTERCEPT___CXA_THROW
780 INTERCEPT_FUNCTION(__cxa_throw
);
783 // Intercept threading-related functions
784 #if ASAN_INTERCEPT_PTHREAD_CREATE
785 ASAN_INTERCEPT_FUNC(pthread_create
);
788 // Intercept atexit function.
789 #if ASAN_INTERCEPT___CXA_ATEXIT
790 ASAN_INTERCEPT_FUNC(__cxa_atexit
);
793 // Some Windows-specific interceptors.
794 #if SANITIZER_WINDOWS
795 InitializeWindowsInterceptors();
798 VReport(1, "AddressSanitizer: libc interceptors initialized\n");
801 } // namespace __asan