• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- asan_interceptors.cc ----------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of AddressSanitizer, an address sanity checker.
11 //
12 // Intercept various libc functions.
13 //===----------------------------------------------------------------------===//
14 #include "asan_interceptors.h"
15 
16 #include "asan_allocator.h"
17 #include "asan_intercepted_functions.h"
18 #include "asan_internal.h"
19 #include "asan_mapping.h"
20 #include "asan_report.h"
21 #include "asan_stack.h"
22 #include "asan_stats.h"
23 #include "asan_thread_registry.h"
24 #include "interception/interception.h"
25 #include "sanitizer/asan_interface.h"
26 #include "sanitizer_common/sanitizer_libc.h"
27 
28 namespace __asan {
29 
30 // Instruments read/write access to a single byte in memory.
31 // On error calls __asan_report_error, which aborts the program.
32 #define ACCESS_ADDRESS(address, isWrite)   do {         \
33   if (!AddrIsInMem(address) || AddressIsPoisoned(address)) {                \
34     GET_CURRENT_PC_BP_SP;                               \
35     __asan_report_error(pc, bp, sp, address, isWrite, /* access_size */ 1); \
36   } \
37 } while (0)
38 
39 // We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
40 // and ASAN_WRITE_RANGE as macro instead of function so
41 // that no extra frames are created, and stack trace contains
42 // relevant information only.
43 
44 // Instruments read/write access to a memory range.
45 // More complex implementation is possible, for now just
46 // checking the first and the last byte of a range.
47 #define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \
48   if (size > 0) { \
49     uptr ptr = (uptr)(offset); \
50     ACCESS_ADDRESS(ptr, isWrite); \
51     ACCESS_ADDRESS(ptr + (size) - 1, isWrite); \
52   } \
53 } while (0)
54 
55 #define ASAN_READ_RANGE(offset, size) do { \
56   ACCESS_MEMORY_RANGE(offset, size, false); \
57 } while (0)
58 
59 #define ASAN_WRITE_RANGE(offset, size) do { \
60   ACCESS_MEMORY_RANGE(offset, size, true); \
61 } while (0)
62 
63 // Behavior of functions like "memcpy" or "strcpy" is undefined
64 // if memory intervals overlap. We report error in this case.
65 // Macro is used to avoid creation of new frames.
RangesOverlap(const char * offset1,uptr length1,const char * offset2,uptr length2)66 static inline bool RangesOverlap(const char *offset1, uptr length1,
67                                  const char *offset2, uptr length2) {
68   return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
69 }
70 #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
71   const char *offset1 = (const char*)_offset1; \
72   const char *offset2 = (const char*)_offset2; \
73   if (RangesOverlap(offset1, length1, offset2, length2)) { \
74     GET_STACK_TRACE_HERE(kStackTraceMax); \
75     ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
76                                             offset2, length2, &stack); \
77   } \
78 } while (0)
79 
80 #define ENSURE_ASAN_INITED() do { \
81   CHECK(!asan_init_is_running); \
82   if (!asan_inited) { \
83     __asan_init(); \
84   } \
85 } while (0)
86 
MaybeRealStrnlen(const char * s,uptr maxlen)87 static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
88 #if ASAN_INTERCEPT_STRNLEN
89   if (REAL(strnlen) != 0) {
90     return REAL(strnlen)(s, maxlen);
91   }
92 #endif
93   return internal_strnlen(s, maxlen);
94 }
95 
96 }  // namespace __asan
97 
98 // ---------------------- Wrappers ---------------- {{{1
99 using namespace __asan;  // NOLINT
100 
asan_thread_start(void * arg)101 static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
102   AsanThread *t = (AsanThread*)arg;
103   asanThreadRegistry().SetCurrent(t);
104   return t->ThreadStart();
105 }
106 
107 #if ASAN_INTERCEPT_PTHREAD_CREATE
INTERCEPTOR(int,pthread_create,void * thread,void * attr,void * (* start_routine)(void *),void * arg)108 INTERCEPTOR(int, pthread_create, void *thread,
109     void *attr, void *(*start_routine)(void*), void *arg) {
110   GET_STACK_TRACE_HERE(kStackTraceMax);
111   u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
112   AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
113   asanThreadRegistry().RegisterThread(t);
114   return REAL(pthread_create)(thread, attr, asan_thread_start, t);
115 }
116 #endif  // ASAN_INTERCEPT_PTHREAD_CREATE
117 
118 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
INTERCEPTOR(void *,signal,int signum,void * handler)119 INTERCEPTOR(void*, signal, int signum, void *handler) {
120   if (!AsanInterceptsSignal(signum)) {
121     return REAL(signal)(signum, handler);
122   }
123   return 0;
124 }
125 
INTERCEPTOR(int,sigaction,int signum,const struct sigaction * act,struct sigaction * oldact)126 INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
127                             struct sigaction *oldact) {
128   if (!AsanInterceptsSignal(signum)) {
129     return REAL(sigaction)(signum, act, oldact);
130   }
131   return 0;
132 }
133 #elif ASAN_POSIX
134 // We need to have defined REAL(sigaction) on posix systems.
135 DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
136     struct sigaction *oldact);
137 #endif  // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
138 
INTERCEPTOR(void,longjmp,void * env,int val)139 INTERCEPTOR(void, longjmp, void *env, int val) {
140   __asan_handle_no_return();
141   REAL(longjmp)(env, val);
142 }
143 
144 #if ASAN_INTERCEPT__LONGJMP
INTERCEPTOR(void,_longjmp,void * env,int val)145 INTERCEPTOR(void, _longjmp, void *env, int val) {
146   __asan_handle_no_return();
147   REAL(_longjmp)(env, val);
148 }
149 #endif
150 
151 #if ASAN_INTERCEPT_SIGLONGJMP
INTERCEPTOR(void,siglongjmp,void * env,int val)152 INTERCEPTOR(void, siglongjmp, void *env, int val) {
153   __asan_handle_no_return();
154   REAL(siglongjmp)(env, val);
155 }
156 #endif
157 
158 #if ASAN_INTERCEPT___CXA_THROW
INTERCEPTOR(void,__cxa_throw,void * a,void * b,void * c)159 INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
160   CHECK(REAL(__cxa_throw));
161   __asan_handle_no_return();
162   REAL(__cxa_throw)(a, b, c);
163 }
164 #endif
165 
166 // intercept mlock and friends.
167 // Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
168 // All functions return 0 (success).
MlockIsUnsupported()169 static void MlockIsUnsupported() {
170   static bool printed = 0;
171   if (printed) return;
172   printed = true;
173   Printf("INFO: AddressSanitizer ignores mlock/mlockall/munlock/munlockall\n");
174 }
175 
176 extern "C" {
INTERCEPTOR(int,mlock,const void * addr,uptr len)177 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
178   MlockIsUnsupported();
179   return 0;
180 }
181 
INTERCEPTOR(int,munlock,const void * addr,uptr len)182 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
183   MlockIsUnsupported();
184   return 0;
185 }
186 
INTERCEPTOR(int,mlockall,int flags)187 INTERCEPTOR(int, mlockall, int flags) {
188   MlockIsUnsupported();
189   return 0;
190 }
191 
INTERCEPTOR(int,munlockall,void)192 INTERCEPTOR(int, munlockall, void) {
193   MlockIsUnsupported();
194   return 0;
195 }
196 }  // extern "C"
197 
CharCmp(unsigned char c1,unsigned char c2)198 static inline int CharCmp(unsigned char c1, unsigned char c2) {
199   return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
200 }
201 
CharCaseCmp(unsigned char c1,unsigned char c2)202 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
203   int c1_low = ToLower(c1);
204   int c2_low = ToLower(c2);
205   return c1_low - c2_low;
206 }
207 
INTERCEPTOR(int,memcmp,const void * a1,const void * a2,uptr size)208 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
209 #if MAC_INTERPOSE_FUNCTIONS
210   if (!asan_inited) return REAL(memcmp)(a1, a2, size);
211 #endif
212   ENSURE_ASAN_INITED();
213   unsigned char c1 = 0, c2 = 0;
214   const unsigned char *s1 = (const unsigned char*)a1;
215   const unsigned char *s2 = (const unsigned char*)a2;
216   uptr i;
217   for (i = 0; i < size; i++) {
218     c1 = s1[i];
219     c2 = s2[i];
220     if (c1 != c2) break;
221   }
222   ASAN_READ_RANGE(s1, Min(i + 1, size));
223   ASAN_READ_RANGE(s2, Min(i + 1, size));
224   return CharCmp(c1, c2);
225 }
226 
INTERCEPTOR(void *,memcpy,void * to,const void * from,uptr size)227 INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
228   // memcpy is called during __asan_init() from the internals
229   // of printf(...).
230   if (asan_init_is_running) {
231     return REAL(memcpy)(to, from, size);
232   }
233   ENSURE_ASAN_INITED();
234   if (flags()->replace_intrin) {
235     if (to != from) {
236       // We do not treat memcpy with to==from as a bug.
237       // See http://llvm.org/bugs/show_bug.cgi?id=11763.
238       CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
239     }
240     ASAN_WRITE_RANGE(from, size);
241     ASAN_READ_RANGE(to, size);
242   }
243   return REAL(memcpy)(to, from, size);
244 }
245 
INTERCEPTOR(void *,memmove,void * to,const void * from,uptr size)246 INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
247   if (asan_init_is_running) {
248     return REAL(memmove)(to, from, size);
249   }
250   ENSURE_ASAN_INITED();
251   if (flags()->replace_intrin) {
252     ASAN_WRITE_RANGE(from, size);
253     ASAN_READ_RANGE(to, size);
254   }
255   return REAL(memmove)(to, from, size);
256 }
257 
INTERCEPTOR(void *,memset,void * block,int c,uptr size)258 INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
259   // memset is called inside Printf.
260   if (asan_init_is_running) {
261     return REAL(memset)(block, c, size);
262   }
263   ENSURE_ASAN_INITED();
264   if (flags()->replace_intrin) {
265     ASAN_WRITE_RANGE(block, size);
266   }
267   return REAL(memset)(block, c, size);
268 }
269 
INTERCEPTOR(char *,strchr,const char * str,int c)270 INTERCEPTOR(char*, strchr, const char *str, int c) {
271 #if MAC_INTERPOSE_FUNCTIONS
272   if (!asan_inited) return REAL(strchr)(str, c);
273 #endif
274   // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is
275   // used.
276   if (asan_init_is_running) {
277     return REAL(strchr)(str, c);
278   }
279   ENSURE_ASAN_INITED();
280   char *result = REAL(strchr)(str, c);
281   if (flags()->replace_str) {
282     uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
283     ASAN_READ_RANGE(str, bytes_read);
284   }
285   return result;
286 }
287 
288 #if ASAN_INTERCEPT_INDEX
289 # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
290 INTERCEPTOR(char*, index, const char *string, int c)
291   ALIAS(WRAPPER_NAME(strchr));
292 # else
DEFINE_REAL(char *,index,const char * string,int c)293 DEFINE_REAL(char*, index, const char *string, int c)
294 # endif
295 #endif  // ASAN_INTERCEPT_INDEX
296 
297 // For both strcat() and strncat() we need to check the validity of |to|
298 // argument irrespective of the |from| length.
299 INTERCEPTOR(char*, strcat, char *to, const char *from) {  // NOLINT
300   ENSURE_ASAN_INITED();
301   if (flags()->replace_str) {
302     uptr from_length = REAL(strlen)(from);
303     ASAN_READ_RANGE(from, from_length + 1);
304     uptr to_length = REAL(strlen)(to);
305     ASAN_READ_RANGE(to, to_length);
306     ASAN_WRITE_RANGE(to + to_length, from_length + 1);
307     // If the copying actually happens, the |from| string should not overlap
308     // with the resulting string starting at |to|, which has a length of
309     // to_length + from_length + 1.
310     if (from_length > 0) {
311       CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
312                            from, from_length + 1);
313     }
314   }
315   return REAL(strcat)(to, from);  // NOLINT
316 }
317 
INTERCEPTOR(char *,strncat,char * to,const char * from,uptr size)318 INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
319   ENSURE_ASAN_INITED();
320   if (flags()->replace_str) {
321     uptr from_length = MaybeRealStrnlen(from, size);
322     uptr copy_length = Min(size, from_length + 1);
323     ASAN_READ_RANGE(from, copy_length);
324     uptr to_length = REAL(strlen)(to);
325     ASAN_READ_RANGE(to, to_length);
326     ASAN_WRITE_RANGE(to + to_length, from_length + 1);
327     if (from_length > 0) {
328       CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
329                            from, copy_length);
330     }
331   }
332   return REAL(strncat)(to, from, size);
333 }
334 
INTERCEPTOR(int,strcmp,const char * s1,const char * s2)335 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
336 #if MAC_INTERPOSE_FUNCTIONS
337   if (!asan_inited) return REAL(strcmp)(s1, s2);
338 #endif
339   if (asan_init_is_running) {
340     return REAL(strcmp)(s1, s2);
341   }
342   ENSURE_ASAN_INITED();
343   unsigned char c1, c2;
344   uptr i;
345   for (i = 0; ; i++) {
346     c1 = (unsigned char)s1[i];
347     c2 = (unsigned char)s2[i];
348     if (c1 != c2 || c1 == '\0') break;
349   }
350   ASAN_READ_RANGE(s1, i + 1);
351   ASAN_READ_RANGE(s2, i + 1);
352   return CharCmp(c1, c2);
353 }
354 
INTERCEPTOR(char *,strcpy,char * to,const char * from)355 INTERCEPTOR(char*, strcpy, char *to, const char *from) {  // NOLINT
356 #if MAC_INTERPOSE_FUNCTIONS
357   if (!asan_inited) return REAL(strcpy)(to, from);  // NOLINT
358 #endif
359   // strcpy is called from malloc_default_purgeable_zone()
360   // in __asan::ReplaceSystemAlloc() on Mac.
361   if (asan_init_is_running) {
362     return REAL(strcpy)(to, from);  // NOLINT
363   }
364   ENSURE_ASAN_INITED();
365   if (flags()->replace_str) {
366     uptr from_size = REAL(strlen)(from) + 1;
367     CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
368     ASAN_READ_RANGE(from, from_size);
369     ASAN_WRITE_RANGE(to, from_size);
370   }
371   return REAL(strcpy)(to, from);  // NOLINT
372 }
373 
374 #if ASAN_INTERCEPT_STRDUP
INTERCEPTOR(char *,strdup,const char * s)375 INTERCEPTOR(char*, strdup, const char *s) {
376 #if MAC_INTERPOSE_FUNCTIONS
377   if (!asan_inited) return REAL(strdup)(s);
378 #endif
379   ENSURE_ASAN_INITED();
380   if (flags()->replace_str) {
381     uptr length = REAL(strlen)(s);
382     ASAN_READ_RANGE(s, length + 1);
383   }
384   return REAL(strdup)(s);
385 }
386 #endif
387 
INTERCEPTOR(uptr,strlen,const char * s)388 INTERCEPTOR(uptr, strlen, const char *s) {
389 #if MAC_INTERPOSE_FUNCTIONS
390   if (!asan_inited) return REAL(strlen)(s);
391 #endif
392   // strlen is called from malloc_default_purgeable_zone()
393   // in __asan::ReplaceSystemAlloc() on Mac.
394   if (asan_init_is_running) {
395     return REAL(strlen)(s);
396   }
397   ENSURE_ASAN_INITED();
398   uptr length = REAL(strlen)(s);
399   if (flags()->replace_str) {
400     ASAN_READ_RANGE(s, length + 1);
401   }
402   return length;
403 }
404 
405 #if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
INTERCEPTOR(int,strcasecmp,const char * s1,const char * s2)406 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
407   ENSURE_ASAN_INITED();
408   unsigned char c1, c2;
409   uptr i;
410   for (i = 0; ; i++) {
411     c1 = (unsigned char)s1[i];
412     c2 = (unsigned char)s2[i];
413     if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
414   }
415   ASAN_READ_RANGE(s1, i + 1);
416   ASAN_READ_RANGE(s2, i + 1);
417   return CharCaseCmp(c1, c2);
418 }
419 
INTERCEPTOR(int,strncasecmp,const char * s1,const char * s2,uptr n)420 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, uptr n) {
421   ENSURE_ASAN_INITED();
422   unsigned char c1 = 0, c2 = 0;
423   uptr i;
424   for (i = 0; i < n; i++) {
425     c1 = (unsigned char)s1[i];
426     c2 = (unsigned char)s2[i];
427     if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
428   }
429   ASAN_READ_RANGE(s1, Min(i + 1, n));
430   ASAN_READ_RANGE(s2, Min(i + 1, n));
431   return CharCaseCmp(c1, c2);
432 }
433 #endif  // ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
434 
INTERCEPTOR(int,strncmp,const char * s1,const char * s2,uptr size)435 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
436 #if MAC_INTERPOSE_FUNCTIONS
437   if (!asan_inited) return REAL(strncmp)(s1, s2, size);
438 #endif
439   // strncmp is called from malloc_default_purgeable_zone()
440   // in __asan::ReplaceSystemAlloc() on Mac.
441   if (asan_init_is_running) {
442     return REAL(strncmp)(s1, s2, size);
443   }
444   ENSURE_ASAN_INITED();
445   unsigned char c1 = 0, c2 = 0;
446   uptr i;
447   for (i = 0; i < size; i++) {
448     c1 = (unsigned char)s1[i];
449     c2 = (unsigned char)s2[i];
450     if (c1 != c2 || c1 == '\0') break;
451   }
452   ASAN_READ_RANGE(s1, Min(i + 1, size));
453   ASAN_READ_RANGE(s2, Min(i + 1, size));
454   return CharCmp(c1, c2);
455 }
456 
INTERCEPTOR(char *,strncpy,char * to,const char * from,uptr size)457 INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
458   ENSURE_ASAN_INITED();
459   if (flags()->replace_str) {
460     uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
461     CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
462     ASAN_READ_RANGE(from, from_size);
463     ASAN_WRITE_RANGE(to, size);
464   }
465   return REAL(strncpy)(to, from, size);
466 }
467 
468 #if ASAN_INTERCEPT_STRNLEN
INTERCEPTOR(uptr,strnlen,const char * s,uptr maxlen)469 INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) {
470   ENSURE_ASAN_INITED();
471   uptr length = REAL(strnlen)(s, maxlen);
472   if (flags()->replace_str) {
473     ASAN_READ_RANGE(s, Min(length + 1, maxlen));
474   }
475   return length;
476 }
477 #endif  // ASAN_INTERCEPT_STRNLEN
478 
IsValidStrtolBase(int base)479 static inline bool IsValidStrtolBase(int base) {
480   return (base == 0) || (2 <= base && base <= 36);
481 }
482 
FixRealStrtolEndptr(const char * nptr,char ** endptr)483 static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
484   CHECK(endptr != 0);
485   if (nptr == *endptr) {
486     // No digits were found at strtol call, we need to find out the last
487     // symbol accessed by strtoll on our own.
488     // We get this symbol by skipping leading blanks and optional +/- sign.
489     while (IsSpace(*nptr)) nptr++;
490     if (*nptr == '+' || *nptr == '-') nptr++;
491     *endptr = (char*)nptr;
492   }
493   CHECK(*endptr >= nptr);
494 }
495 
INTERCEPTOR(long,strtol,const char * nptr,char ** endptr,int base)496 INTERCEPTOR(long, strtol, const char *nptr,  // NOLINT
497             char **endptr, int base) {
498   ENSURE_ASAN_INITED();
499   if (!flags()->replace_str) {
500     return REAL(strtol)(nptr, endptr, base);
501   }
502   char *real_endptr;
503   long result = REAL(strtol)(nptr, &real_endptr, base);  // NOLINT
504   if (endptr != 0) {
505     *endptr = real_endptr;
506   }
507   if (IsValidStrtolBase(base)) {
508     FixRealStrtolEndptr(nptr, &real_endptr);
509     ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
510   }
511   return result;
512 }
513 
INTERCEPTOR(int,atoi,const char * nptr)514 INTERCEPTOR(int, atoi, const char *nptr) {
515 #if MAC_INTERPOSE_FUNCTIONS
516   if (!asan_inited) return REAL(atoi)(nptr);
517 #endif
518   ENSURE_ASAN_INITED();
519   if (!flags()->replace_str) {
520     return REAL(atoi)(nptr);
521   }
522   char *real_endptr;
523   // "man atoi" tells that behavior of atoi(nptr) is the same as
524   // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
525   // parsed integer can't be stored in *long* type (even if it's
526   // different from int). So, we just imitate this behavior.
527   int result = REAL(strtol)(nptr, &real_endptr, 10);
528   FixRealStrtolEndptr(nptr, &real_endptr);
529   ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
530   return result;
531 }
532 
INTERCEPTOR(long,atol,const char * nptr)533 INTERCEPTOR(long, atol, const char *nptr) {  // NOLINT
534 #if MAC_INTERPOSE_FUNCTIONS
535   if (!asan_inited) return REAL(atol)(nptr);
536 #endif
537   ENSURE_ASAN_INITED();
538   if (!flags()->replace_str) {
539     return REAL(atol)(nptr);
540   }
541   char *real_endptr;
542   long result = REAL(strtol)(nptr, &real_endptr, 10);  // NOLINT
543   FixRealStrtolEndptr(nptr, &real_endptr);
544   ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
545   return result;
546 }
547 
548 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
INTERCEPTOR(long long,strtoll,const char * nptr,char ** endptr,int base)549 INTERCEPTOR(long long, strtoll, const char *nptr,  // NOLINT
550             char **endptr, int base) {
551   ENSURE_ASAN_INITED();
552   if (!flags()->replace_str) {
553     return REAL(strtoll)(nptr, endptr, base);
554   }
555   char *real_endptr;
556   long long result = REAL(strtoll)(nptr, &real_endptr, base);  // NOLINT
557   if (endptr != 0) {
558     *endptr = real_endptr;
559   }
560   // If base has unsupported value, strtoll can exit with EINVAL
561   // without reading any characters. So do additional checks only
562   // if base is valid.
563   if (IsValidStrtolBase(base)) {
564     FixRealStrtolEndptr(nptr, &real_endptr);
565     ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
566   }
567   return result;
568 }
569 
INTERCEPTOR(long long,atoll,const char * nptr)570 INTERCEPTOR(long long, atoll, const char *nptr) {  // NOLINT
571   ENSURE_ASAN_INITED();
572   if (!flags()->replace_str) {
573     return REAL(atoll)(nptr);
574   }
575   char *real_endptr;
576   long long result = REAL(strtoll)(nptr, &real_endptr, 10);  // NOLINT
577   FixRealStrtolEndptr(nptr, &real_endptr);
578   ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
579   return result;
580 }
581 #endif  // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
582 
583 #define ASAN_INTERCEPT_FUNC(name) do { \
584       if (!INTERCEPT_FUNCTION(name) && flags()->verbosity > 0) \
585         Report("AddressSanitizer: failed to intercept '" #name "'\n"); \
586     } while (0)
587 
588 #if defined(_WIN32)
INTERCEPTOR_WINAPI(DWORD,CreateThread,void * security,uptr stack_size,DWORD (__stdcall * start_routine)(void *),void * arg,DWORD flags,void * tid)589 INTERCEPTOR_WINAPI(DWORD, CreateThread,
590                    void* security, uptr stack_size,
591                    DWORD (__stdcall *start_routine)(void*), void* arg,
592                    DWORD flags, void* tid) {
593   GET_STACK_TRACE_HERE(kStackTraceMax);
594   u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
595   AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
596   asanThreadRegistry().RegisterThread(t);
597   return REAL(CreateThread)(security, stack_size,
598                             asan_thread_start, t, flags, tid);
599 }
600 
601 namespace __asan {
InitializeWindowsInterceptors()602 void InitializeWindowsInterceptors() {
603   ASAN_INTERCEPT_FUNC(CreateThread);
604 }
605 
606 }  // namespace __asan
607 #endif
608 
609 // ---------------------- InitializeAsanInterceptors ---------------- {{{1
610 namespace __asan {
InitializeAsanInterceptors()611 void InitializeAsanInterceptors() {
612   static bool was_called_once;
613   CHECK(was_called_once == false);
614   was_called_once = true;
615 #if MAC_INTERPOSE_FUNCTIONS
616   return;
617 #endif
618   // Intercept mem* functions.
619   ASAN_INTERCEPT_FUNC(memcmp);
620   ASAN_INTERCEPT_FUNC(memmove);
621   ASAN_INTERCEPT_FUNC(memset);
622   if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
623     ASAN_INTERCEPT_FUNC(memcpy);
624   } else {
625 #if !MAC_INTERPOSE_FUNCTIONS
626     // If we're using dynamic interceptors on Mac, these two are just plain
627     // functions.
628     *(uptr*)&REAL(memcpy) = (uptr)REAL(memmove);
629 #endif
630   }
631 
632   // Intercept str* functions.
633   ASAN_INTERCEPT_FUNC(strcat);  // NOLINT
634   ASAN_INTERCEPT_FUNC(strchr);
635   ASAN_INTERCEPT_FUNC(strcmp);
636   ASAN_INTERCEPT_FUNC(strcpy);  // NOLINT
637   ASAN_INTERCEPT_FUNC(strlen);
638   ASAN_INTERCEPT_FUNC(strncat);
639   ASAN_INTERCEPT_FUNC(strncmp);
640   ASAN_INTERCEPT_FUNC(strncpy);
641 #if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
642   ASAN_INTERCEPT_FUNC(strcasecmp);
643   ASAN_INTERCEPT_FUNC(strncasecmp);
644 #endif
645 #if ASAN_INTERCEPT_STRDUP
646   ASAN_INTERCEPT_FUNC(strdup);
647 #endif
648 #if ASAN_INTERCEPT_STRNLEN
649   ASAN_INTERCEPT_FUNC(strnlen);
650 #endif
651 #if ASAN_INTERCEPT_INDEX
652 # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
653   ASAN_INTERCEPT_FUNC(index);
654 # else
655   CHECK(OVERRIDE_FUNCTION(index, WRAP(strchr)));
656 # endif
657 #endif
658 
659   ASAN_INTERCEPT_FUNC(atoi);
660   ASAN_INTERCEPT_FUNC(atol);
661   ASAN_INTERCEPT_FUNC(strtol);
662 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
663   ASAN_INTERCEPT_FUNC(atoll);
664   ASAN_INTERCEPT_FUNC(strtoll);
665 #endif
666 
667 #if ASAN_INTERCEPT_MLOCKX
668   // Intercept mlock/munlock.
669   ASAN_INTERCEPT_FUNC(mlock);
670   ASAN_INTERCEPT_FUNC(munlock);
671   ASAN_INTERCEPT_FUNC(mlockall);
672   ASAN_INTERCEPT_FUNC(munlockall);
673 #endif
674 
675   // Intecept signal- and jump-related functions.
676   ASAN_INTERCEPT_FUNC(longjmp);
677 #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
678   ASAN_INTERCEPT_FUNC(sigaction);
679   ASAN_INTERCEPT_FUNC(signal);
680 #endif
681 #if ASAN_INTERCEPT__LONGJMP
682   ASAN_INTERCEPT_FUNC(_longjmp);
683 #endif
684 #if ASAN_INTERCEPT_SIGLONGJMP
685   ASAN_INTERCEPT_FUNC(siglongjmp);
686 #endif
687 
688   // Intercept exception handling functions.
689 #if ASAN_INTERCEPT___CXA_THROW
690   INTERCEPT_FUNCTION(__cxa_throw);
691 #endif
692 
693   // Intercept threading-related functions
694 #if ASAN_INTERCEPT_PTHREAD_CREATE
695   ASAN_INTERCEPT_FUNC(pthread_create);
696 #endif
697 
698   // Some Windows-specific interceptors.
699 #if defined(_WIN32)
700   InitializeWindowsInterceptors();
701 #endif
702 
703   // Some Mac-specific interceptors.
704 #if defined(__APPLE__)
705   InitializeMacInterceptors();
706 #endif
707 
708   if (flags()->verbosity > 0) {
709     Report("AddressSanitizer: libc interceptors initialized\n");
710   }
711 }
712 
713 }  // namespace __asan
714