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"
|
|
|
|
#include "asan_interface.h"
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_mapping.h"
|
|
|
|
#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-06-04 21:27:49 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
|
2012-03-24 16:39:14 +08:00
|
|
|
// Use macro to describe if specific function should be
|
|
|
|
// intercepted on a given platform.
|
2012-03-24 18:12:58 +08:00
|
|
|
#if !defined(_WIN32)
|
2012-03-29 16:04:35 +08:00
|
|
|
# define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 1
|
2012-08-01 19:17:00 +08:00
|
|
|
# define ASAN_INTERCEPT__LONGJMP 1
|
|
|
|
# define ASAN_INTERCEPT_STRDUP 1
|
|
|
|
# define ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP 1
|
|
|
|
# define ASAN_INTERCEPT_INDEX 1
|
|
|
|
# define ASAN_INTERCEPT_PTHREAD_CREATE 1
|
2012-03-24 18:12:58 +08:00
|
|
|
#else
|
2012-03-29 16:04:35 +08:00
|
|
|
# define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 0
|
2012-08-01 19:17:00 +08:00
|
|
|
# define ASAN_INTERCEPT__LONGJMP 0
|
|
|
|
# define ASAN_INTERCEPT_STRDUP 0
|
|
|
|
# define ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP 0
|
|
|
|
# define ASAN_INTERCEPT_INDEX 0
|
|
|
|
# define ASAN_INTERCEPT_PTHREAD_CREATE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
# define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1
|
|
|
|
#else
|
|
|
|
# define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0
|
2012-03-24 18:12:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(__APPLE__)
|
|
|
|
# define ASAN_INTERCEPT_STRNLEN 1
|
|
|
|
#else
|
|
|
|
# define ASAN_INTERCEPT_STRNLEN 0
|
|
|
|
#endif
|
2012-03-24 16:39:14 +08:00
|
|
|
|
2012-08-01 19:17:00 +08:00
|
|
|
#if !defined(ANDROID) && !defined(_WIN32)
|
|
|
|
# define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 1
|
|
|
|
#else
|
2012-03-26 17:07:29 +08:00
|
|
|
# define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 0
|
2012-08-01 19:17:00 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// On Darwin siglongjmp tailcalls longjmp, so we don't want to intercept it
|
|
|
|
// there.
|
|
|
|
#if !defined(_WIN32) && !defined(__APPLE__)
|
|
|
|
# define ASAN_INTERCEPT_SIGLONGJMP 1
|
2012-03-26 17:07:29 +08:00
|
|
|
#else
|
2012-08-01 19:17:00 +08:00
|
|
|
# define ASAN_INTERCEPT_SIGLONGJMP 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ASAN_HAS_EXCEPTIONS && !defined(_WIN32)
|
|
|
|
# define ASAN_INTERCEPT___CXA_THROW 1
|
|
|
|
#else
|
|
|
|
# define ASAN_INTERCEPT___CXA_THROW 0
|
2012-03-26 17:07:29 +08:00
|
|
|
#endif
|
|
|
|
|
2012-03-21 22:22:28 +08:00
|
|
|
// Use extern declarations of intercepted functions on Mac and Windows
|
|
|
|
// to avoid including system headers.
|
|
|
|
#if defined(__APPLE__) || (defined(_WIN32) && !defined(_DLL))
|
2012-03-20 19:40:09 +08:00
|
|
|
extern "C" {
|
2012-03-21 21:44:39 +08:00
|
|
|
// signal.h
|
2012-03-26 17:07:29 +08:00
|
|
|
# if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
|
2012-03-21 21:44:39 +08:00
|
|
|
struct sigaction;
|
|
|
|
int sigaction(int sig, const struct sigaction *act,
|
|
|
|
struct sigaction *oldact);
|
|
|
|
void *signal(int signum, void *handler);
|
2012-03-21 22:22:28 +08:00
|
|
|
# endif
|
2012-03-20 19:40:09 +08:00
|
|
|
|
2012-03-21 22:22:28 +08:00
|
|
|
// setjmp.h
|
|
|
|
void longjmp(void* env, int value);
|
2012-08-01 19:17:00 +08:00
|
|
|
# if ASAN_INTERCEPT__LONGJMP
|
2012-03-21 22:22:28 +08:00
|
|
|
void _longjmp(void *env, int value);
|
|
|
|
# endif
|
2012-08-01 19:17:00 +08:00
|
|
|
# if ASAN_INTERCEPT___CXA_THROW
|
|
|
|
void __cxa_throw(void *a, void *b, void *c);
|
|
|
|
#endif
|
2012-03-12 19:45:09 +08:00
|
|
|
|
2012-03-21 22:22:28 +08:00
|
|
|
// string.h / strings.h
|
2012-05-31 22:35:53 +08:00
|
|
|
int memcmp(const void *a1, const void *a2, uptr size);
|
|
|
|
void* memmove(void *to, const void *from, uptr size);
|
|
|
|
void* memcpy(void *to, const void *from, uptr size);
|
|
|
|
void* memset(void *block, int c, uptr size);
|
2012-03-21 22:22:28 +08:00
|
|
|
char* strchr(const char *str, int c);
|
|
|
|
char* strcat(char *to, const char* from); // NOLINT
|
2012-06-08 21:27:46 +08:00
|
|
|
char *strncat(char *to, const char* from, uptr size);
|
2012-03-21 22:22:28 +08:00
|
|
|
char* strcpy(char *to, const char* from); // NOLINT
|
2012-05-31 22:35:53 +08:00
|
|
|
char* strncpy(char *to, const char* from, uptr size);
|
2012-03-12 19:45:09 +08:00
|
|
|
int strcmp(const char *s1, const char* s2);
|
2012-05-31 22:35:53 +08:00
|
|
|
int strncmp(const char *s1, const char* s2, uptr size);
|
2012-08-01 19:17:00 +08:00
|
|
|
uptr strlen(const char *s);
|
|
|
|
# if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
|
2012-03-21 22:33:15 +08:00
|
|
|
int strcasecmp(const char *s1, const char *s2);
|
2012-05-31 22:35:53 +08:00
|
|
|
int strncasecmp(const char *s1, const char *s2, uptr n);
|
2012-08-01 19:17:00 +08:00
|
|
|
# endif
|
|
|
|
# if ASAN_INTERCEPT_STRDUP
|
2012-03-21 22:33:15 +08:00
|
|
|
char* strdup(const char *s);
|
|
|
|
# endif
|
2012-03-24 17:10:50 +08:00
|
|
|
# if ASAN_INTERCEPT_STRNLEN
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr strnlen(const char *s, uptr maxlen);
|
2012-03-21 22:22:28 +08:00
|
|
|
# endif
|
2012-08-01 19:17:00 +08:00
|
|
|
# if ASAN_INTERCEPT_INDEX
|
|
|
|
char* index(const char *string, int c);
|
|
|
|
# endif
|
2012-03-12 19:45:09 +08:00
|
|
|
|
2012-03-24 16:39:14 +08:00
|
|
|
// stdlib.h
|
2012-03-29 16:04:35 +08:00
|
|
|
int atoi(const char *nptr);
|
|
|
|
long atol(const char *nptr); // NOLINT
|
2012-03-27 00:42:22 +08:00
|
|
|
long strtol(const char *nptr, char **endptr, int base); // NOLINT
|
2012-03-29 16:04:35 +08:00
|
|
|
# if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
|
|
|
|
long long atoll(const char *nptr); // NOLINT
|
2012-03-24 16:39:14 +08:00
|
|
|
long long strtoll(const char *nptr, char **endptr, int base); // NOLINT
|
|
|
|
# endif
|
|
|
|
|
2012-03-21 22:22:28 +08:00
|
|
|
// Windows threads.
|
|
|
|
# if defined(_WIN32)
|
2012-03-12 19:45:09 +08:00
|
|
|
__declspec(dllimport)
|
2012-05-31 22:35:53 +08:00
|
|
|
void* __stdcall CreateThread(void *sec, uptr st, void* start,
|
2012-03-12 19:45:09 +08:00
|
|
|
void *arg, DWORD fl, DWORD *id);
|
2012-03-21 22:22:28 +08:00
|
|
|
# endif
|
2012-03-21 23:02:39 +08:00
|
|
|
// Posix threads.
|
2012-08-01 19:17:00 +08:00
|
|
|
# if ASAN_INTERCEPT_PTHREAD_CREATE
|
2012-03-21 23:02:39 +08:00
|
|
|
int pthread_create(void *thread, void *attr, void *(*start_routine)(void*),
|
|
|
|
void *arg);
|
|
|
|
# endif
|
2012-03-12 19:45:09 +08:00
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
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-05-31 22:35:53 +08:00
|
|
|
uptr ptr = (uptr)(offset); \
|
2012-03-15 09:36:00 +08:00
|
|
|
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-06-06 21:11:29 +08:00
|
|
|
AsanReport("ERROR: AddressSanitizer %s-param-overlap: " \
|
|
|
|
"memory ranges [%p,%p) and [%p, %p) overlap\n", \
|
|
|
|
name, offset1, offset1 + length1, offset2, offset2 + length2); \
|
2011-11-30 09:07:02 +08:00
|
|
|
PRINT_CURRENT_STACK(); \
|
|
|
|
ShowStatsAndAbort(); \
|
|
|
|
} \
|
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-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-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-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-01-30 21:42:44 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2012-05-31 22:35:53 +08:00
|
|
|
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
|
|
|
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2012-05-31 22:35:53 +08:00
|
|
|
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
|
|
|
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2012-01-10 02:53:15 +08:00
|
|
|
int mlockall(int flags) {
|
|
|
|
MlockIsUnsupported();
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-30 21:42:44 +08:00
|
|
|
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2012-01-10 02:53:15 +08:00
|
|
|
int munlockall(void) {
|
|
|
|
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) {
|
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) {
|
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);
|
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
ASAN_WRITE_RANGE(from, size);
|
|
|
|
ASAN_READ_RANGE(to, size);
|
|
|
|
}
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(memcpy)(to, from, size);
|
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-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) {
|
2011-11-30 09:07:02 +08:00
|
|
|
ASAN_WRITE_RANGE(from, size);
|
|
|
|
ASAN_READ_RANGE(to, size);
|
|
|
|
}
|
2012-02-08 21:45:31 +08:00
|
|
|
return REAL(memmove)(to, from, size);
|
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-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) {
|
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-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);
|
|
|
|
if (from_length > 0) {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr to_length = REAL(strlen)(to);
|
2011-12-29 03:08:49 +08:00
|
|
|
ASAN_READ_RANGE(to, to_length);
|
|
|
|
ASAN_WRITE_RANGE(to + to_length, from_length + 1);
|
2011-12-29 03:24:31 +08:00
|
|
|
CHECK_RANGES_OVERLAP("strcat", to, 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-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str && size > 0) {
|
2012-06-15 21:09:52 +08:00
|
|
|
uptr from_length = MaybeRealStrnlen(from, size);
|
2012-06-08 21:27:46 +08:00
|
|
|
ASAN_READ_RANGE(from, Min(size, from_length + 1));
|
|
|
|
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) {
|
|
|
|
CHECK_RANGES_OVERLAP("strncat", to, to_length + 1,
|
|
|
|
from, Min(size, from_length + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return REAL(strncat)(to, from, size);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(int, strcmp, const char *s1, const char *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
|
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) {
|
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) {
|
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) {
|
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) {
|
|
|
|
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
|
|
|
|
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-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-02-08 21:45:31 +08:00
|
|
|
REAL(memcpy) = REAL(memmove);
|
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-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-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
|
|
|
|
|
|
|
|
// 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
|