2012-06-04 21:50:10 +08:00
|
|
|
//===-- asan_interceptors.cc ----------------------------------------------===//
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
2012-01-11 10:32:40 +08:00
|
|
|
// Intercept various libc functions.
|
2011-11-30 09:07:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
|
|
|
|
#include "asan_allocator.h"
|
2012-08-15 23:24:48 +08:00
|
|
|
#include "asan_intercepted_functions.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_mapping.h"
|
2012-08-09 16:32:33 +08:00
|
|
|
#include "asan_report.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_stack.h"
|
|
|
|
#include "asan_stats.h"
|
2012-01-10 02:53:15 +08:00
|
|
|
#include "asan_thread_registry.h"
|
2012-02-09 03:52:01 +08:00
|
|
|
#include "interception/interception.h"
|
2012-08-29 10:27:54 +08:00
|
|
|
#include "sanitizer/asan_interface.h"
|
2012-06-04 21:27:49 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
// Instruments read/write access to a single byte in memory.
|
|
|
|
// On error calls __asan_report_error, which aborts the program.
|
2012-03-15 09:36:00 +08:00
|
|
|
#define ACCESS_ADDRESS(address, isWrite) do { \
|
2012-07-23 16:22:27 +08:00
|
|
|
if (!AddrIsInMem(address) || AddressIsPoisoned(address)) { \
|
2012-03-15 09:36:00 +08:00
|
|
|
GET_CURRENT_PC_BP_SP; \
|
|
|
|
__asan_report_error(pc, bp, sp, address, isWrite, /* access_size */ 1); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
// We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
|
|
|
|
// and ASAN_WRITE_RANGE as macro instead of function so
|
|
|
|
// that no extra frames are created, and stack trace contains
|
|
|
|
// relevant information only.
|
|
|
|
|
|
|
|
// Instruments read/write access to a memory range.
|
|
|
|
// More complex implementation is possible, for now just
|
|
|
|
// checking the first and the last byte of a range.
|
|
|
|
#define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \
|
|
|
|
if (size > 0) { \
|
2012-12-12 17:54:35 +08:00
|
|
|
uptr _ptr = (uptr)(offset); \
|
|
|
|
ACCESS_ADDRESS(_ptr, isWrite); \
|
|
|
|
ACCESS_ADDRESS(_ptr + (size) - 1, isWrite); \
|
2011-11-30 09:07:02 +08:00
|
|
|
} \
|
2011-12-06 02:56:29 +08:00
|
|
|
} while (0)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
#define ASAN_READ_RANGE(offset, size) do { \
|
|
|
|
ACCESS_MEMORY_RANGE(offset, size, false); \
|
2011-12-06 02:56:29 +08:00
|
|
|
} while (0)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
#define ASAN_WRITE_RANGE(offset, size) do { \
|
|
|
|
ACCESS_MEMORY_RANGE(offset, size, true); \
|
2011-12-06 02:56:29 +08:00
|
|
|
} while (0)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
// Behavior of functions like "memcpy" or "strcpy" is undefined
|
|
|
|
// if memory intervals overlap. We report error in this case.
|
|
|
|
// Macro is used to avoid creation of new frames.
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool RangesOverlap(const char *offset1, uptr length1,
|
|
|
|
const char *offset2, uptr length2) {
|
2011-12-29 03:08:49 +08:00
|
|
|
return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2011-12-29 03:24:31 +08:00
|
|
|
#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
|
2011-11-30 09:07:02 +08:00
|
|
|
const char *offset1 = (const char*)_offset1; \
|
|
|
|
const char *offset2 = (const char*)_offset2; \
|
2011-12-29 03:08:49 +08:00
|
|
|
if (RangesOverlap(offset1, length1, offset2, length2)) { \
|
2012-08-09 16:32:33 +08:00
|
|
|
GET_STACK_TRACE_HERE(kStackTraceMax); \
|
|
|
|
ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
|
|
|
|
offset2, length2, &stack); \
|
2011-11-30 09:07:02 +08:00
|
|
|
} \
|
2011-12-06 02:56:29 +08:00
|
|
|
} while (0)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2011-12-06 02:56:29 +08:00
|
|
|
#define ENSURE_ASAN_INITED() do { \
|
|
|
|
CHECK(!asan_init_is_running); \
|
|
|
|
if (!asan_inited) { \
|
|
|
|
__asan_init(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-06-15 21:09:52 +08:00
|
|
|
static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
|
2012-03-24 17:10:50 +08:00
|
|
|
#if ASAN_INTERCEPT_STRNLEN
|
2012-05-31 22:35:53 +08:00
|
|
|
if (REAL(strnlen) != 0) {
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(strnlen)(s, maxlen);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-02-02 18:39:40 +08:00
|
|
|
#endif
|
2012-06-15 21:09:52 +08:00
|
|
|
return internal_strnlen(s, maxlen);
|
2012-01-10 06:20:49 +08:00
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
// ---------------------- Wrappers ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
2012-12-12 17:54:35 +08:00
|
|
|
#define COMMON_INTERCEPTOR_WRITE_RANGE(ptr, size) ASAN_WRITE_RANGE(ptr, size)
|
|
|
|
#define COMMON_INTERCEPTOR_READ_RANGE(ptr, size) ASAN_READ_RANGE(ptr, size)
|
|
|
|
#define COMMON_INTERCEPTOR_ENTER(func, ...) ENSURE_ASAN_INITED()
|
|
|
|
#include "sanitizer_common/sanitizer_common_interceptors.h"
|
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
|
2012-01-10 02:53:15 +08:00
|
|
|
AsanThread *t = (AsanThread*)arg;
|
|
|
|
asanThreadRegistry().SetCurrent(t);
|
|
|
|
return t->ThreadStart();
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT_PTHREAD_CREATE
|
2012-02-22 20:31:25 +08:00
|
|
|
INTERCEPTOR(int, pthread_create, void *thread,
|
|
|
|
void *attr, void *(*start_routine)(void*), void *arg) {
|
2012-01-19 19:34:18 +08:00
|
|
|
GET_STACK_TRACE_HERE(kStackTraceMax);
|
2012-06-06 23:06:58 +08:00
|
|
|
u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
|
2012-01-17 14:35:31 +08:00
|
|
|
AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
|
|
|
|
asanThreadRegistry().RegisterThread(t);
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(pthread_create)(thread, attr, asan_thread_start, t);
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
2012-08-01 19:17:00 +08:00
|
|
|
#endif // ASAN_INTERCEPT_PTHREAD_CREATE
|
2012-01-10 02:53:15 +08:00
|
|
|
|
2012-03-26 17:07:29 +08:00
|
|
|
#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, signal, int signum, void *handler) {
|
2012-04-16 16:33:01 +08:00
|
|
|
if (!AsanInterceptsSignal(signum)) {
|
|
|
|
return REAL(signal)(signum, handler);
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
2012-05-31 22:35:53 +08:00
|
|
|
return 0;
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
|
|
|
|
2012-02-17 01:00:45 +08:00
|
|
|
INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
|
|
|
|
struct sigaction *oldact) {
|
2012-04-16 16:33:01 +08:00
|
|
|
if (!AsanInterceptsSignal(signum)) {
|
|
|
|
return REAL(sigaction)(signum, act, oldact);
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
2012-04-16 16:33:01 +08:00
|
|
|
return 0;
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
2012-03-26 17:07:29 +08:00
|
|
|
#elif ASAN_POSIX
|
|
|
|
// We need to have defined REAL(sigaction) on posix systems.
|
|
|
|
DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
|
|
|
|
struct sigaction *oldact);
|
|
|
|
#endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
|
2012-01-10 02:53:15 +08:00
|
|
|
|
2012-11-23 17:46:34 +08:00
|
|
|
#if ASAN_INTERCEPT_SWAPCONTEXT
|
|
|
|
INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
|
|
|
|
struct ucontext_t *ucp) {
|
|
|
|
static bool reported_warning = false;
|
|
|
|
if (!reported_warning) {
|
|
|
|
Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
|
|
|
|
"functions and may produce false positives in some cases!\n");
|
|
|
|
reported_warning = true;
|
|
|
|
}
|
|
|
|
// Clear shadow memory for new context (it may share stack
|
|
|
|
// with current context).
|
|
|
|
ClearShadowMemoryForContext(ucp);
|
|
|
|
int res = REAL(swapcontext)(oucp, ucp);
|
|
|
|
// swapcontext technically does not return, but program may swap context to
|
|
|
|
// "oucp" later, that would look as if swapcontext() returned 0.
|
|
|
|
// We need to clear shadow for ucp once again, as it may be in arbitrary
|
|
|
|
// state.
|
|
|
|
ClearShadowMemoryForContext(ucp);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void, longjmp, void *env, int val) {
|
2012-02-09 05:33:27 +08:00
|
|
|
__asan_handle_no_return();
|
2012-02-08 21:45:31 +08:00
|
|
|
REAL(longjmp)(env, val);
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT__LONGJMP
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void, _longjmp, void *env, int val) {
|
2012-02-09 05:33:27 +08:00
|
|
|
__asan_handle_no_return();
|
2012-02-08 21:45:31 +08:00
|
|
|
REAL(_longjmp)(env, val);
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
2012-08-01 19:17:00 +08:00
|
|
|
#endif
|
2012-01-10 02:53:15 +08:00
|
|
|
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT_SIGLONGJMP
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void, siglongjmp, void *env, int val) {
|
2012-02-09 05:33:27 +08:00
|
|
|
__asan_handle_no_return();
|
2012-02-08 21:45:31 +08:00
|
|
|
REAL(siglongjmp)(env, val);
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
2012-02-22 21:59:49 +08:00
|
|
|
#endif
|
2012-01-10 02:53:15 +08:00
|
|
|
|
2012-12-08 01:54:38 +08:00
|
|
|
#if ASAN_INTERCEPT_PRCTL
|
2012-12-07 23:15:01 +08:00
|
|
|
#define PR_SET_NAME 15
|
2012-12-08 01:54:38 +08:00
|
|
|
INTERCEPTOR(int, prctl, int option,
|
|
|
|
unsigned long arg2, unsigned long arg3, // NOLINT
|
|
|
|
unsigned long arg4, unsigned long arg5) { // NOLINT
|
2012-12-07 23:15:01 +08:00
|
|
|
int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
|
|
|
|
if (option == PR_SET_NAME) {
|
|
|
|
AsanThread *t = asanThreadRegistry().GetCurrent();
|
|
|
|
if (t) {
|
|
|
|
char buff[17];
|
|
|
|
internal_strncpy(buff, (char*)arg2, 16);
|
|
|
|
buff[16] = 0;
|
|
|
|
t->summary()->set_name(buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2012-12-08 01:54:38 +08:00
|
|
|
#endif
|
2012-12-07 23:15:01 +08:00
|
|
|
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT___CXA_THROW
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
|
2012-02-08 21:45:31 +08:00
|
|
|
CHECK(REAL(__cxa_throw));
|
2012-02-09 05:33:27 +08:00
|
|
|
__asan_handle_no_return();
|
2012-02-08 21:45:31 +08:00
|
|
|
REAL(__cxa_throw)(a, b, c);
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// intercept mlock and friends.
|
|
|
|
// Since asan maps 16T of RAM, mlock is completely unfriendly to asan.
|
|
|
|
// All functions return 0 (success).
|
|
|
|
static void MlockIsUnsupported() {
|
|
|
|
static bool printed = 0;
|
|
|
|
if (printed) return;
|
|
|
|
printed = true;
|
|
|
|
Printf("INFO: AddressSanitizer ignores mlock/mlockall/munlock/munlockall\n");
|
|
|
|
}
|
2012-01-30 21:42:44 +08:00
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
extern "C" {
|
2012-08-15 17:22:57 +08:00
|
|
|
INTERCEPTOR(int, mlock, const void *addr, uptr len) {
|
2012-01-10 02:53:15 +08:00
|
|
|
MlockIsUnsupported();
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-30 21:42:44 +08:00
|
|
|
|
2012-08-15 17:22:57 +08:00
|
|
|
INTERCEPTOR(int, munlock, const void *addr, uptr len) {
|
2012-01-10 02:53:15 +08:00
|
|
|
MlockIsUnsupported();
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-30 21:42:44 +08:00
|
|
|
|
2012-08-15 17:22:57 +08:00
|
|
|
INTERCEPTOR(int, mlockall, int flags) {
|
2012-01-10 02:53:15 +08:00
|
|
|
MlockIsUnsupported();
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-30 21:42:44 +08:00
|
|
|
|
2012-08-15 17:22:57 +08:00
|
|
|
INTERCEPTOR(int, munlockall, void) {
|
2012-01-10 02:53:15 +08:00
|
|
|
MlockIsUnsupported();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // extern "C"
|
|
|
|
|
2011-12-29 02:56:42 +08:00
|
|
|
static inline int CharCmp(unsigned char c1, unsigned char c2) {
|
|
|
|
return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
|
2012-02-18 00:15:09 +08:00
|
|
|
int c1_low = ToLower(c1);
|
|
|
|
int c2_low = ToLower(c2);
|
2011-12-29 02:56:42 +08:00
|
|
|
return c1_low - c2_low;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
|
2012-10-05 20:11:06 +08:00
|
|
|
if (!asan_inited) return internal_memcmp(a1, a2, size);
|
2011-12-29 02:56:42 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
|
|
|
unsigned char c1 = 0, c2 = 0;
|
|
|
|
const unsigned char *s1 = (const unsigned char*)a1;
|
|
|
|
const unsigned char *s2 = (const unsigned char*)a2;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr i;
|
2011-12-29 02:56:42 +08:00
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
c1 = s1[i];
|
|
|
|
c2 = s2[i];
|
|
|
|
if (c1 != c2) break;
|
|
|
|
}
|
|
|
|
ASAN_READ_RANGE(s1, Min(i + 1, size));
|
|
|
|
ASAN_READ_RANGE(s2, Min(i + 1, size));
|
|
|
|
return CharCmp(c1, c2);
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
|
2012-10-05 20:11:06 +08:00
|
|
|
if (!asan_inited) return internal_memcpy(to, from, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
// memcpy is called during __asan_init() from the internals
|
|
|
|
// of printf(...).
|
|
|
|
if (asan_init_is_running) {
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(memcpy)(to, from, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2011-12-06 02:56:29 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_intrin) {
|
2012-01-18 02:43:52 +08:00
|
|
|
if (to != from) {
|
|
|
|
// We do not treat memcpy with to==from as a bug.
|
|
|
|
// See http://llvm.org/bugs/show_bug.cgi?id=11763.
|
|
|
|
CHECK_RANGES_OVERLAP("memcpy", to, size, from, size);
|
|
|
|
}
|
2012-12-11 00:02:13 +08:00
|
|
|
ASAN_READ_RANGE(from, size);
|
|
|
|
ASAN_WRITE_RANGE(to, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-10-15 23:34:41 +08:00
|
|
|
#if MAC_INTERPOSE_FUNCTIONS
|
|
|
|
// Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
|
|
|
|
// See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
|
|
|
|
return internal_memcpy(to, from, size);
|
|
|
|
#else
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(memcpy)(to, from, size);
|
2012-10-15 23:34:41 +08:00
|
|
|
#endif
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
|
2012-10-24 17:19:16 +08:00
|
|
|
if (!asan_inited) return internal_memmove(to, from, size);
|
2012-06-08 22:04:04 +08:00
|
|
|
if (asan_init_is_running) {
|
|
|
|
return REAL(memmove)(to, from, size);
|
|
|
|
}
|
2011-12-06 02:56:29 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_intrin) {
|
2012-12-11 00:02:13 +08:00
|
|
|
ASAN_READ_RANGE(from, size);
|
|
|
|
ASAN_WRITE_RANGE(to, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-10-15 23:34:41 +08:00
|
|
|
#if MAC_INTERPOSE_FUNCTIONS
|
|
|
|
// Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
|
|
|
|
// See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
|
|
|
|
return internal_memmove(to, from, size);
|
|
|
|
#else
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(memmove)(to, from, size);
|
2012-10-15 23:34:41 +08:00
|
|
|
#endif
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
|
2012-10-05 20:11:06 +08:00
|
|
|
if (!asan_inited) return internal_memset(block, c, size);
|
2012-03-29 20:20:47 +08:00
|
|
|
// memset is called inside Printf.
|
2011-12-06 02:56:29 +08:00
|
|
|
if (asan_init_is_running) {
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(memset)(block, c, size);
|
2011-12-06 02:56:29 +08:00
|
|
|
}
|
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_intrin) {
|
2011-11-30 09:07:02 +08:00
|
|
|
ASAN_WRITE_RANGE(block, size);
|
|
|
|
}
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(memset)(block, c, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(char*, strchr, const char *str, int c) {
|
2012-10-05 20:11:06 +08:00
|
|
|
if (!asan_inited) return internal_strchr(str, c);
|
2012-09-10 16:35:12 +08:00
|
|
|
// strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is
|
|
|
|
// used.
|
|
|
|
if (asan_init_is_running) {
|
|
|
|
return REAL(strchr)(str, c);
|
|
|
|
}
|
2011-12-06 02:56:29 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-02-08 21:45:31 +08:00
|
|
|
char *result = REAL(strchr)(str, c);
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str) {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1;
|
2011-11-30 09:07:02 +08:00
|
|
|
ASAN_READ_RANGE(str, bytes_read);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT_INDEX
|
|
|
|
# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
|
2012-02-13 20:12:32 +08:00
|
|
|
INTERCEPTOR(char*, index, const char *string, int c)
|
2012-02-03 16:37:19 +08:00
|
|
|
ALIAS(WRAPPER_NAME(strchr));
|
2012-08-01 19:17:00 +08:00
|
|
|
# else
|
2012-06-28 16:27:24 +08:00
|
|
|
DEFINE_REAL(char*, index, const char *string, int c)
|
2012-08-01 19:17:00 +08:00
|
|
|
# endif
|
|
|
|
#endif // ASAN_INTERCEPT_INDEX
|
2011-12-28 10:24:50 +08:00
|
|
|
|
2012-08-02 18:25:46 +08:00
|
|
|
// For both strcat() and strncat() we need to check the validity of |to|
|
|
|
|
// argument irrespective of the |from| length.
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT
|
2011-12-29 03:08:49 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str) {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr from_length = REAL(strlen)(from);
|
2011-12-29 03:08:49 +08:00
|
|
|
ASAN_READ_RANGE(from, from_length + 1);
|
2012-08-02 18:25:46 +08:00
|
|
|
uptr to_length = REAL(strlen)(to);
|
|
|
|
ASAN_READ_RANGE(to, to_length);
|
|
|
|
ASAN_WRITE_RANGE(to + to_length, from_length + 1);
|
|
|
|
// If the copying actually happens, the |from| string should not overlap
|
|
|
|
// with the resulting string starting at |to|, which has a length of
|
|
|
|
// to_length + from_length + 1.
|
2011-12-29 03:08:49 +08:00
|
|
|
if (from_length > 0) {
|
2012-08-02 18:25:46 +08:00
|
|
|
CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1,
|
|
|
|
from, from_length + 1);
|
2011-12-29 03:08:49 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(strcat)(to, from); // NOLINT
|
2011-12-29 03:08:49 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:27:46 +08:00
|
|
|
INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
|
|
|
|
ENSURE_ASAN_INITED();
|
2012-08-02 18:25:46 +08:00
|
|
|
if (flags()->replace_str) {
|
2012-06-15 21:09:52 +08:00
|
|
|
uptr from_length = MaybeRealStrnlen(from, size);
|
2012-08-02 18:25:46 +08:00
|
|
|
uptr copy_length = Min(size, from_length + 1);
|
|
|
|
ASAN_READ_RANGE(from, copy_length);
|
2012-06-08 21:27:46 +08:00
|
|
|
uptr to_length = REAL(strlen)(to);
|
|
|
|
ASAN_READ_RANGE(to, to_length);
|
|
|
|
ASAN_WRITE_RANGE(to + to_length, from_length + 1);
|
|
|
|
if (from_length > 0) {
|
2012-08-02 18:25:46 +08:00
|
|
|
CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
|
|
|
|
from, copy_length);
|
2012-06-08 21:27:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return REAL(strncat)(to, from, size);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
|
2012-10-05 20:11:06 +08:00
|
|
|
if (!asan_inited) return internal_strcmp(s1, s2);
|
2012-08-01 19:17:00 +08:00
|
|
|
if (asan_init_is_running) {
|
|
|
|
return REAL(strcmp)(s1, s2);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-07-27 15:09:49 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2011-11-30 09:07:02 +08:00
|
|
|
unsigned char c1, c2;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr i;
|
2011-11-30 09:07:02 +08:00
|
|
|
for (i = 0; ; i++) {
|
|
|
|
c1 = (unsigned char)s1[i];
|
|
|
|
c2 = (unsigned char)s2[i];
|
|
|
|
if (c1 != c2 || c1 == '\0') break;
|
|
|
|
}
|
|
|
|
ASAN_READ_RANGE(s1, i + 1);
|
|
|
|
ASAN_READ_RANGE(s2, i + 1);
|
|
|
|
return CharCmp(c1, c2);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
|
2012-08-17 17:00:08 +08:00
|
|
|
#if MAC_INTERPOSE_FUNCTIONS
|
2012-08-17 18:08:51 +08:00
|
|
|
if (!asan_inited) return REAL(strcpy)(to, from); // NOLINT
|
2012-08-17 17:00:08 +08:00
|
|
|
#endif
|
2011-11-30 09:07:02 +08:00
|
|
|
// strcpy is called from malloc_default_purgeable_zone()
|
|
|
|
// in __asan::ReplaceSystemAlloc() on Mac.
|
|
|
|
if (asan_init_is_running) {
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(strcpy)(to, from); // NOLINT
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2011-12-06 02:56:29 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str) {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr from_size = REAL(strlen)(from) + 1;
|
2011-12-29 03:24:31 +08:00
|
|
|
CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
|
2011-11-30 09:07:02 +08:00
|
|
|
ASAN_READ_RANGE(from, from_size);
|
|
|
|
ASAN_WRITE_RANGE(to, from_size);
|
|
|
|
}
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(strcpy)(to, from); // NOLINT
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT_STRDUP
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(char*, strdup, const char *s) {
|
2012-10-26 19:31:14 +08:00
|
|
|
#if MAC_INTERPOSE_FUNCTIONS
|
|
|
|
// FIXME: because internal_strdup() uses InternalAlloc(), which currently
|
|
|
|
// just calls malloc() on Mac, we can't use internal_strdup() with the
|
|
|
|
// dynamic runtime. We can remove the call to REAL(strdup) once InternalAlloc
|
|
|
|
// starts using mmap() instead.
|
|
|
|
// See also http://code.google.com/p/address-sanitizer/issues/detail?id=123.
|
|
|
|
if (!asan_inited) return REAL(strdup)(s);
|
|
|
|
#endif
|
2012-10-05 20:11:06 +08:00
|
|
|
if (!asan_inited) return internal_strdup(s);
|
2011-12-06 02:56:29 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str) {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr length = REAL(strlen)(s);
|
2011-11-30 09:07:02 +08:00
|
|
|
ASAN_READ_RANGE(s, length + 1);
|
|
|
|
}
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(strdup)(s);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-08-01 19:17:00 +08:00
|
|
|
#endif
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(uptr, strlen, const char *s) {
|
2012-10-05 20:11:06 +08:00
|
|
|
if (!asan_inited) return internal_strlen(s);
|
2011-11-30 09:07:02 +08:00
|
|
|
// strlen is called from malloc_default_purgeable_zone()
|
|
|
|
// in __asan::ReplaceSystemAlloc() on Mac.
|
|
|
|
if (asan_init_is_running) {
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(strlen)(s);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2011-12-06 02:56:29 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr length = REAL(strlen)(s);
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str) {
|
2011-11-30 09:07:02 +08:00
|
|
|
ASAN_READ_RANGE(s, length + 1);
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
|
|
|
|
INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
|
|
|
|
ENSURE_ASAN_INITED();
|
|
|
|
unsigned char c1, c2;
|
|
|
|
uptr i;
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
c1 = (unsigned char)s1[i];
|
|
|
|
c2 = (unsigned char)s2[i];
|
|
|
|
if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
|
|
|
|
}
|
|
|
|
ASAN_READ_RANGE(s1, i + 1);
|
|
|
|
ASAN_READ_RANGE(s2, i + 1);
|
|
|
|
return CharCaseCmp(c1, c2);
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, uptr n) {
|
2011-12-28 10:24:50 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
|
|
|
unsigned char c1 = 0, c2 = 0;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr i;
|
2012-02-02 18:39:40 +08:00
|
|
|
for (i = 0; i < n; i++) {
|
2011-12-28 10:24:50 +08:00
|
|
|
c1 = (unsigned char)s1[i];
|
|
|
|
c2 = (unsigned char)s2[i];
|
|
|
|
if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
|
|
|
|
}
|
2012-02-02 18:39:40 +08:00
|
|
|
ASAN_READ_RANGE(s1, Min(i + 1, n));
|
|
|
|
ASAN_READ_RANGE(s2, Min(i + 1, n));
|
2011-12-28 10:24:50 +08:00
|
|
|
return CharCaseCmp(c1, c2);
|
|
|
|
}
|
2012-08-01 19:17:00 +08:00
|
|
|
#endif // ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
|
2011-12-28 10:24:50 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
|
2012-10-05 20:11:06 +08:00
|
|
|
if (!asan_inited) return internal_strncmp(s1, s2, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
// strncmp is called from malloc_default_purgeable_zone()
|
|
|
|
// in __asan::ReplaceSystemAlloc() on Mac.
|
|
|
|
if (asan_init_is_running) {
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(strncmp)(s1, s2, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-07-27 15:09:49 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2011-11-30 09:07:02 +08:00
|
|
|
unsigned char c1 = 0, c2 = 0;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr i;
|
2011-11-30 09:07:02 +08:00
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
c1 = (unsigned char)s1[i];
|
|
|
|
c2 = (unsigned char)s2[i];
|
|
|
|
if (c1 != c2 || c1 == '\0') break;
|
|
|
|
}
|
2011-12-03 02:42:04 +08:00
|
|
|
ASAN_READ_RANGE(s1, Min(i + 1, size));
|
|
|
|
ASAN_READ_RANGE(s2, Min(i + 1, size));
|
2011-11-30 09:07:02 +08:00
|
|
|
return CharCmp(c1, c2);
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
|
2011-12-06 02:56:29 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str) {
|
2012-06-15 21:09:52 +08:00
|
|
|
uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
|
2011-12-29 03:24:31 +08:00
|
|
|
CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
|
2011-11-30 09:07:02 +08:00
|
|
|
ASAN_READ_RANGE(from, from_size);
|
|
|
|
ASAN_WRITE_RANGE(to, size);
|
|
|
|
}
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(strncpy)(to, from, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 17:10:50 +08:00
|
|
|
#if ASAN_INTERCEPT_STRNLEN
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) {
|
2011-12-06 02:56:29 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr length = REAL(strnlen)(s, maxlen);
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str) {
|
2011-12-03 02:42:04 +08:00
|
|
|
ASAN_READ_RANGE(s, Min(length + 1, maxlen));
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
2012-03-24 17:10:50 +08:00
|
|
|
#endif // ASAN_INTERCEPT_STRNLEN
|
2012-01-10 03:35:11 +08:00
|
|
|
|
2012-03-27 00:42:22 +08:00
|
|
|
static inline bool IsValidStrtolBase(int base) {
|
|
|
|
return (base == 0) || (2 <= base && base <= 36);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
|
2012-05-31 22:35:53 +08:00
|
|
|
CHECK(endptr != 0);
|
2012-03-27 00:42:22 +08:00
|
|
|
if (nptr == *endptr) {
|
|
|
|
// No digits were found at strtol call, we need to find out the last
|
|
|
|
// symbol accessed by strtoll on our own.
|
|
|
|
// We get this symbol by skipping leading blanks and optional +/- sign.
|
|
|
|
while (IsSpace(*nptr)) nptr++;
|
|
|
|
if (*nptr == '+' || *nptr == '-') nptr++;
|
|
|
|
*endptr = (char*)nptr;
|
|
|
|
}
|
|
|
|
CHECK(*endptr >= nptr);
|
2012-03-24 16:39:14 +08:00
|
|
|
}
|
|
|
|
|
2012-03-29 16:04:35 +08:00
|
|
|
INTERCEPTOR(long, strtol, const char *nptr, // NOLINT
|
2012-03-24 16:39:14 +08:00
|
|
|
char **endptr, int base) {
|
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->replace_str) {
|
2012-03-29 16:04:35 +08:00
|
|
|
return REAL(strtol)(nptr, endptr, base);
|
2012-03-24 16:39:14 +08:00
|
|
|
}
|
|
|
|
char *real_endptr;
|
2012-03-29 16:04:35 +08:00
|
|
|
long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT
|
2012-05-31 22:35:53 +08:00
|
|
|
if (endptr != 0) {
|
2012-03-24 16:39:14 +08:00
|
|
|
*endptr = real_endptr;
|
|
|
|
}
|
2012-03-27 00:42:22 +08:00
|
|
|
if (IsValidStrtolBase(base)) {
|
|
|
|
FixRealStrtolEndptr(nptr, &real_endptr);
|
2012-03-24 16:39:14 +08:00
|
|
|
ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-29 16:04:35 +08:00
|
|
|
INTERCEPTOR(int, atoi, const char *nptr) {
|
2012-08-17 17:00:08 +08:00
|
|
|
#if MAC_INTERPOSE_FUNCTIONS
|
|
|
|
if (!asan_inited) return REAL(atoi)(nptr);
|
|
|
|
#endif
|
2012-03-29 16:04:35 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->replace_str) {
|
2012-03-29 16:04:35 +08:00
|
|
|
return REAL(atoi)(nptr);
|
|
|
|
}
|
|
|
|
char *real_endptr;
|
|
|
|
// "man atoi" tells that behavior of atoi(nptr) is the same as
|
2012-05-31 22:35:53 +08:00
|
|
|
// strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
|
2012-03-29 16:04:35 +08:00
|
|
|
// parsed integer can't be stored in *long* type (even if it's
|
|
|
|
// different from int). So, we just imitate this behavior.
|
|
|
|
int result = REAL(strtol)(nptr, &real_endptr, 10);
|
|
|
|
FixRealStrtolEndptr(nptr, &real_endptr);
|
|
|
|
ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
|
2012-08-17 17:00:08 +08:00
|
|
|
#if MAC_INTERPOSE_FUNCTIONS
|
|
|
|
if (!asan_inited) return REAL(atol)(nptr);
|
|
|
|
#endif
|
2012-03-29 16:04:35 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->replace_str) {
|
2012-03-29 16:04:35 +08:00
|
|
|
return REAL(atol)(nptr);
|
|
|
|
}
|
|
|
|
char *real_endptr;
|
|
|
|
long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT
|
|
|
|
FixRealStrtolEndptr(nptr, &real_endptr);
|
|
|
|
ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
|
|
|
|
INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT
|
2012-03-27 00:42:22 +08:00
|
|
|
char **endptr, int base) {
|
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->replace_str) {
|
2012-03-29 16:04:35 +08:00
|
|
|
return REAL(strtoll)(nptr, endptr, base);
|
2012-03-27 00:42:22 +08:00
|
|
|
}
|
|
|
|
char *real_endptr;
|
2012-03-29 16:04:35 +08:00
|
|
|
long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT
|
2012-05-31 22:35:53 +08:00
|
|
|
if (endptr != 0) {
|
2012-03-27 00:42:22 +08:00
|
|
|
*endptr = real_endptr;
|
|
|
|
}
|
2012-03-29 16:04:35 +08:00
|
|
|
// If base has unsupported value, strtoll can exit with EINVAL
|
|
|
|
// without reading any characters. So do additional checks only
|
|
|
|
// if base is valid.
|
2012-03-27 00:42:22 +08:00
|
|
|
if (IsValidStrtolBase(base)) {
|
|
|
|
FixRealStrtolEndptr(nptr, &real_endptr);
|
|
|
|
ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-29 16:04:35 +08:00
|
|
|
INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
|
|
|
|
ENSURE_ASAN_INITED();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!flags()->replace_str) {
|
2012-03-29 16:04:35 +08:00
|
|
|
return REAL(atoll)(nptr);
|
|
|
|
}
|
|
|
|
char *real_endptr;
|
|
|
|
long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
|
|
|
|
FixRealStrtolEndptr(nptr, &real_endptr);
|
|
|
|
ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
|
|
|
|
|
2012-05-24 21:54:31 +08:00
|
|
|
#define ASAN_INTERCEPT_FUNC(name) do { \
|
2012-07-09 22:36:04 +08:00
|
|
|
if (!INTERCEPT_FUNCTION(name) && flags()->verbosity > 0) \
|
2012-05-24 21:54:31 +08:00
|
|
|
Report("AddressSanitizer: failed to intercept '" #name "'\n"); \
|
|
|
|
} while (0)
|
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
INTERCEPTOR_WINAPI(DWORD, CreateThread,
|
2012-05-31 22:35:53 +08:00
|
|
|
void* security, uptr stack_size,
|
2012-02-24 23:28:43 +08:00
|
|
|
DWORD (__stdcall *start_routine)(void*), void* arg,
|
|
|
|
DWORD flags, void* tid) {
|
|
|
|
GET_STACK_TRACE_HERE(kStackTraceMax);
|
2012-06-06 23:06:58 +08:00
|
|
|
u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
|
2012-02-24 23:28:43 +08:00
|
|
|
AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
|
|
|
|
asanThreadRegistry().RegisterThread(t);
|
|
|
|
return REAL(CreateThread)(security, stack_size,
|
|
|
|
asan_thread_start, t, flags, tid);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
void InitializeWindowsInterceptors() {
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(CreateThread);
|
2012-02-24 23:28:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __asan
|
|
|
|
#endif
|
|
|
|
|
2012-01-10 03:35:11 +08:00
|
|
|
// ---------------------- InitializeAsanInterceptors ---------------- {{{1
|
|
|
|
namespace __asan {
|
|
|
|
void InitializeAsanInterceptors() {
|
2012-03-17 05:02:13 +08:00
|
|
|
static bool was_called_once;
|
|
|
|
CHECK(was_called_once == false);
|
|
|
|
was_called_once = true;
|
2012-08-17 17:00:08 +08:00
|
|
|
#if MAC_INTERPOSE_FUNCTIONS
|
|
|
|
return;
|
|
|
|
#endif
|
2012-12-12 17:54:35 +08:00
|
|
|
|
|
|
|
SANITIZER_COMMON_INTERCEPTORS_INIT;
|
|
|
|
|
2012-02-22 21:59:49 +08:00
|
|
|
// Intercept mem* functions.
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(memcmp);
|
|
|
|
ASAN_INTERCEPT_FUNC(memmove);
|
|
|
|
ASAN_INTERCEPT_FUNC(memset);
|
2012-03-20 18:54:40 +08:00
|
|
|
if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(memcpy);
|
2012-02-01 18:07:52 +08:00
|
|
|
} else {
|
2012-08-17 17:00:08 +08:00
|
|
|
#if !MAC_INTERPOSE_FUNCTIONS
|
2012-08-17 18:08:51 +08:00
|
|
|
// If we're using dynamic interceptors on Mac, these two are just plain
|
2012-08-17 17:00:08 +08:00
|
|
|
// functions.
|
2012-09-12 15:38:47 +08:00
|
|
|
internal_memcpy(&REAL(memcpy), &REAL(memmove), sizeof(REAL(memmove)));
|
2012-08-17 17:00:08 +08:00
|
|
|
#endif
|
2012-02-01 18:07:52 +08:00
|
|
|
}
|
2012-02-22 21:59:49 +08:00
|
|
|
|
|
|
|
// Intercept str* functions.
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(strcat); // NOLINT
|
|
|
|
ASAN_INTERCEPT_FUNC(strchr);
|
|
|
|
ASAN_INTERCEPT_FUNC(strcmp);
|
|
|
|
ASAN_INTERCEPT_FUNC(strcpy); // NOLINT
|
|
|
|
ASAN_INTERCEPT_FUNC(strlen);
|
2012-06-08 21:27:46 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(strncat);
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(strncmp);
|
|
|
|
ASAN_INTERCEPT_FUNC(strncpy);
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(strcasecmp);
|
|
|
|
ASAN_INTERCEPT_FUNC(strncasecmp);
|
2012-08-01 19:17:00 +08:00
|
|
|
#endif
|
|
|
|
#if ASAN_INTERCEPT_STRDUP
|
|
|
|
ASAN_INTERCEPT_FUNC(strdup);
|
|
|
|
#endif
|
|
|
|
#if ASAN_INTERCEPT_STRNLEN
|
|
|
|
ASAN_INTERCEPT_FUNC(strnlen);
|
|
|
|
#endif
|
|
|
|
#if ASAN_INTERCEPT_INDEX
|
|
|
|
# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(index);
|
2012-02-22 21:59:49 +08:00
|
|
|
# else
|
2012-05-25 02:16:39 +08:00
|
|
|
CHECK(OVERRIDE_FUNCTION(index, WRAP(strchr)));
|
2012-02-22 21:59:49 +08:00
|
|
|
# endif
|
|
|
|
#endif
|
2012-02-09 03:52:01 +08:00
|
|
|
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(atoi);
|
|
|
|
ASAN_INTERCEPT_FUNC(atol);
|
|
|
|
ASAN_INTERCEPT_FUNC(strtol);
|
2012-03-29 16:04:35 +08:00
|
|
|
#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(atoll);
|
|
|
|
ASAN_INTERCEPT_FUNC(strtoll);
|
2012-03-24 16:39:14 +08:00
|
|
|
#endif
|
|
|
|
|
2012-08-15 17:46:45 +08:00
|
|
|
#if ASAN_INTERCEPT_MLOCKX
|
2012-08-15 17:22:57 +08:00
|
|
|
// Intercept mlock/munlock.
|
|
|
|
ASAN_INTERCEPT_FUNC(mlock);
|
|
|
|
ASAN_INTERCEPT_FUNC(munlock);
|
|
|
|
ASAN_INTERCEPT_FUNC(mlockall);
|
|
|
|
ASAN_INTERCEPT_FUNC(munlockall);
|
2012-08-15 17:46:45 +08:00
|
|
|
#endif
|
2012-08-15 17:22:57 +08:00
|
|
|
|
2012-02-22 21:59:49 +08:00
|
|
|
// Intecept signal- and jump-related functions.
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(longjmp);
|
2012-03-26 17:07:29 +08:00
|
|
|
#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(sigaction);
|
|
|
|
ASAN_INTERCEPT_FUNC(signal);
|
2012-02-13 20:04:36 +08:00
|
|
|
#endif
|
2012-11-23 17:46:34 +08:00
|
|
|
#if ASAN_INTERCEPT_SWAPCONTEXT
|
|
|
|
ASAN_INTERCEPT_FUNC(swapcontext);
|
|
|
|
#endif
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT__LONGJMP
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(_longjmp);
|
2012-08-01 19:17:00 +08:00
|
|
|
#endif
|
|
|
|
#if ASAN_INTERCEPT_SIGLONGJMP
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(siglongjmp);
|
2012-08-01 19:17:00 +08:00
|
|
|
#endif
|
2012-12-08 01:54:38 +08:00
|
|
|
#if ASAN_INTERCEPT_PRCTL
|
2012-12-07 23:15:01 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(prctl);
|
2012-12-08 01:54:38 +08:00
|
|
|
#endif
|
2012-08-01 19:17:00 +08:00
|
|
|
|
|
|
|
// Intercept exception handling functions.
|
|
|
|
#if ASAN_INTERCEPT___CXA_THROW
|
|
|
|
INTERCEPT_FUNCTION(__cxa_throw);
|
2012-02-10 01:20:14 +08:00
|
|
|
#endif
|
|
|
|
|
2012-02-22 21:59:49 +08:00
|
|
|
// Intercept threading-related functions
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT_PTHREAD_CREATE
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(pthread_create);
|
2012-02-22 21:59:49 +08:00
|
|
|
#endif
|
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
// Some Windows-specific interceptors.
|
|
|
|
#if defined(_WIN32)
|
|
|
|
InitializeWindowsInterceptors();
|
|
|
|
#endif
|
|
|
|
|
2012-02-22 21:59:49 +08:00
|
|
|
// Some Mac-specific interceptors.
|
|
|
|
#if defined(__APPLE__)
|
2012-03-21 20:42:00 +08:00
|
|
|
InitializeMacInterceptors();
|
2012-01-10 03:35:11 +08:00
|
|
|
#endif
|
|
|
|
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->verbosity > 0) {
|
2012-03-21 17:33:05 +08:00
|
|
|
Report("AddressSanitizer: libc interceptors initialized\n");
|
2012-01-10 03:35:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __asan
|