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_internal.h"
|
|
|
|
#include "asan_mapping.h"
|
2013-03-28 23:42:43 +08:00
|
|
|
#include "asan_poisoning.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"
|
2014-12-06 04:26:09 +08:00
|
|
|
#include "asan_suppressions.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 {
|
|
|
|
|
2013-02-21 15:07:39 +08:00
|
|
|
// Return true if we can quickly decide that the region is unpoisoned.
|
|
|
|
static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {
|
|
|
|
if (size == 0) return true;
|
|
|
|
if (size <= 32)
|
|
|
|
return !AddressIsPoisoned(beg) &&
|
|
|
|
!AddressIsPoisoned(beg + size - 1) &&
|
|
|
|
!AddressIsPoisoned(beg + size / 2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-06 04:26:09 +08:00
|
|
|
struct AsanInterceptorContext {
|
|
|
|
const char *interceptor_name;
|
|
|
|
};
|
|
|
|
|
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.
|
2012-12-28 23:24:16 +08:00
|
|
|
// We check all shadow bytes.
|
2014-12-06 04:26:09 +08:00
|
|
|
#define ACCESS_MEMORY_RANGE(ctx, offset, size, isWrite) do { \
|
2013-02-05 22:32:03 +08:00
|
|
|
uptr __offset = (uptr)(offset); \
|
|
|
|
uptr __size = (uptr)(size); \
|
2013-02-26 15:25:18 +08:00
|
|
|
uptr __bad = 0; \
|
2014-04-14 17:50:52 +08:00
|
|
|
if (__offset > __offset + __size) { \
|
|
|
|
GET_STACK_TRACE_FATAL_HERE; \
|
|
|
|
ReportStringFunctionSizeOverflow(__offset, __size, &stack); \
|
|
|
|
} \
|
2013-02-21 15:07:39 +08:00
|
|
|
if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \
|
2013-02-26 15:25:18 +08:00
|
|
|
(__bad = __asan_region_is_poisoned(__offset, __size))) { \
|
2014-12-06 04:26:09 +08:00
|
|
|
AsanInterceptorContext *_ctx = (AsanInterceptorContext *)ctx; \
|
|
|
|
bool suppressed = false; \
|
|
|
|
if (_ctx) { \
|
|
|
|
suppressed = IsInterceptorSuppressed(_ctx->interceptor_name); \
|
|
|
|
if (!suppressed && HaveStackTraceBasedSuppressions()) { \
|
|
|
|
GET_STACK_TRACE_FATAL_HERE; \
|
|
|
|
suppressed = IsStackTraceSuppressed(&stack); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (!suppressed) { \
|
|
|
|
GET_CURRENT_PC_BP_SP; \
|
|
|
|
__asan_report_error(pc, bp, sp, __bad, isWrite, __size); \
|
|
|
|
} \
|
2013-02-05 22:32:03 +08:00
|
|
|
} \
|
|
|
|
} while (0)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2014-12-06 04:26:09 +08:00
|
|
|
#define ASAN_READ_RANGE(ctx, offset, size) \
|
|
|
|
ACCESS_MEMORY_RANGE(ctx, offset, size, false)
|
|
|
|
#define ASAN_WRITE_RANGE(ctx, offset, size) \
|
|
|
|
ACCESS_MEMORY_RANGE(ctx, offset, size, true)
|
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-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_FATAL_HERE; \
|
2012-08-09 16:32:33 +08:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2013-01-18 14:43:13 +08:00
|
|
|
void SetThreadName(const char *name) {
|
2013-03-20 17:23:28 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
2013-01-18 14:43:13 +08:00
|
|
|
if (t)
|
2013-03-21 19:23:41 +08:00
|
|
|
asanThreadRegistry().SetThreadName(t->tid(), name);
|
2013-01-18 14:43:13 +08:00
|
|
|
}
|
|
|
|
|
2013-10-03 23:43:59 +08:00
|
|
|
int OnExit() {
|
2013-10-03 23:22:29 +08:00
|
|
|
// FIXME: ask frontend whether we need to return failure.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
// ---------------------- Wrappers ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
2013-07-09 17:53:37 +08:00
|
|
|
DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
|
|
|
|
DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
|
|
|
|
|
2013-11-11 19:28:30 +08:00
|
|
|
#if !SANITIZER_MAC
|
2013-12-05 20:04:51 +08:00
|
|
|
#define ASAN_INTERCEPT_FUNC(name) \
|
|
|
|
do { \
|
|
|
|
if ((!INTERCEPT_FUNCTION(name) || !REAL(name))) \
|
|
|
|
VReport(1, "AddressSanitizer: failed to intercept '" #name "'\n"); \
|
2013-11-11 19:28:30 +08:00
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
// OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
|
|
|
|
#define ASAN_INTERCEPT_FUNC(name)
|
|
|
|
#endif // SANITIZER_MAC
|
|
|
|
|
2014-12-06 04:26:09 +08:00
|
|
|
#define ASAN_INTERCEPTOR_ENTER(ctx, func) \
|
|
|
|
AsanInterceptorContext _ctx = {#func}; \
|
|
|
|
ctx = (void *)&_ctx; \
|
|
|
|
(void) ctx; \
|
|
|
|
|
2013-11-11 19:28:30 +08:00
|
|
|
#define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name)
|
2013-01-18 21:12:56 +08:00
|
|
|
#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_WRITE_RANGE(ctx, ptr, size)
|
|
|
|
#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
|
|
|
|
ASAN_READ_RANGE(ctx, ptr, size)
|
2014-05-14 22:03:31 +08:00
|
|
|
#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
|
|
|
|
do { \
|
|
|
|
if (asan_init_is_running) \
|
|
|
|
return REAL(func)(__VA_ARGS__); \
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, func); \
|
2014-05-14 22:03:31 +08:00
|
|
|
if (SANITIZER_MAC && UNLIKELY(!asan_inited)) \
|
|
|
|
return REAL(func)(__VA_ARGS__); \
|
|
|
|
ENSURE_ASAN_INITED(); \
|
2013-05-29 17:09:58 +08:00
|
|
|
} while (false)
|
|
|
|
#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
|
|
|
|
do { \
|
|
|
|
} while (false)
|
|
|
|
#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
|
|
|
|
do { \
|
|
|
|
} while (false)
|
|
|
|
#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
|
|
|
|
do { \
|
2013-01-18 21:12:56 +08:00
|
|
|
} while (false)
|
2013-01-18 19:17:23 +08:00
|
|
|
#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
|
2013-11-15 00:48:22 +08:00
|
|
|
// Should be asanThreadRegistry().SetThreadNameByUserId(thread, name)
|
|
|
|
// But asan does not remember UserId's for threads (pthread_t);
|
|
|
|
// and remembers all ever existed threads, so the linear search by UserId
|
|
|
|
// can be slow.
|
2013-10-29 18:30:39 +08:00
|
|
|
#define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
|
2013-11-15 00:48:22 +08:00
|
|
|
do { \
|
|
|
|
} while (false)
|
2013-08-12 21:19:53 +08:00
|
|
|
#define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
|
2013-10-03 23:22:29 +08:00
|
|
|
#define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
|
2014-05-27 20:37:52 +08:00
|
|
|
#define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res) CovUpdateMapping()
|
|
|
|
#define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() CovUpdateMapping()
|
2014-07-25 20:45:36 +08:00
|
|
|
#define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
|
2013-01-18 21:01:18 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common_interceptors.inc"
|
2012-12-12 17:54:35 +08:00
|
|
|
|
2014-12-06 04:26:09 +08:00
|
|
|
// Syscall interceptors don't have contexts, we don't support suppressions
|
|
|
|
// for them.
|
|
|
|
#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(nullptr, p, s)
|
|
|
|
#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(nullptr, p, s)
|
2013-07-09 17:29:19 +08:00
|
|
|
#define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
|
|
|
|
do { \
|
2013-10-30 02:29:39 +08:00
|
|
|
(void)(p); \
|
|
|
|
(void)(s); \
|
2013-07-09 17:29:19 +08:00
|
|
|
} while (false)
|
|
|
|
#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
|
|
|
|
do { \
|
2013-10-30 02:29:39 +08:00
|
|
|
(void)(p); \
|
|
|
|
(void)(s); \
|
2013-07-09 17:29:19 +08:00
|
|
|
} while (false)
|
2013-04-12 22:57:03 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common_syscalls.inc"
|
|
|
|
|
2014-12-05 08:10:15 +08:00
|
|
|
struct ThreadStartParam {
|
|
|
|
atomic_uintptr_t t;
|
|
|
|
atomic_uintptr_t is_registered;
|
|
|
|
};
|
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
|
2014-12-05 08:10:15 +08:00
|
|
|
ThreadStartParam *param = reinterpret_cast<ThreadStartParam *>(arg);
|
|
|
|
AsanThread *t = nullptr;
|
|
|
|
while ((t = reinterpret_cast<AsanThread *>(
|
|
|
|
atomic_load(¶m->t, memory_order_acquire))) == 0)
|
|
|
|
internal_sched_yield();
|
2013-03-20 17:23:28 +08:00
|
|
|
SetCurrentThread(t);
|
2014-12-05 08:10:15 +08:00
|
|
|
return t->ThreadStart(GetTid(), ¶m->is_registered);
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2013-07-08 20:57:24 +08:00
|
|
|
EnsureMainThreadIDIsCorrect();
|
2014-12-05 08:10:15 +08:00
|
|
|
// Strict init-order checking is thread-hostile.
|
2013-07-02 00:16:41 +08:00
|
|
|
if (flags()->strict_init_order)
|
|
|
|
StopInitOrderChecking();
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_THREAD;
|
2013-03-21 19:23:41 +08:00
|
|
|
int detached = 0;
|
|
|
|
if (attr != 0)
|
2013-11-11 16:56:49 +08:00
|
|
|
REAL(pthread_attr_getdetachstate)(attr, &detached);
|
2014-12-05 08:10:15 +08:00
|
|
|
ThreadStartParam param;
|
|
|
|
atomic_store(¶m.t, 0, memory_order_relaxed);
|
|
|
|
atomic_store(¶m.is_registered, 0, memory_order_relaxed);
|
|
|
|
int result = REAL(pthread_create)(thread, attr, asan_thread_start, ¶m);
|
|
|
|
if (result == 0) {
|
|
|
|
u32 current_tid = GetCurrentTidOrInvalid();
|
2014-12-06 01:31:13 +08:00
|
|
|
AsanThread *t =
|
|
|
|
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
|
2014-12-05 08:10:15 +08:00
|
|
|
atomic_store(¶m.t, reinterpret_cast<uptr>(t), memory_order_release);
|
|
|
|
// Wait until the AsanThread object is initialized and the ThreadRegistry
|
|
|
|
// entry is in "started" state. One reason for this is that after this
|
|
|
|
// interceptor exits, the child thread's stack may be the only thing holding
|
|
|
|
// the |arg| pointer. This may cause LSan to report a leak if leak checking
|
|
|
|
// happens at a point when the interceptor has already exited, but the stack
|
|
|
|
// range for the child thread is not yet known.
|
|
|
|
while (atomic_load(¶m.is_registered, memory_order_acquire) == 0)
|
|
|
|
internal_sched_yield();
|
|
|
|
}
|
|
|
|
return result;
|
2012-01-10 02:53:15 +08:00
|
|
|
}
|
2014-12-17 03:13:01 +08:00
|
|
|
|
|
|
|
INTERCEPTOR(int, pthread_join, void *t, void **arg) {
|
|
|
|
return real_pthread_join(t, arg);
|
|
|
|
}
|
|
|
|
DEFINE_REAL_PTHREAD_FUNCTIONS;
|
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
|
2014-02-13 22:33:24 +08:00
|
|
|
|
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
INTERCEPTOR(void*, bsd_signal, int signum, void *handler) {
|
|
|
|
if (!AsanInterceptsSignal(signum) ||
|
|
|
|
common_flags()->allow_user_segv_handler) {
|
|
|
|
return REAL(bsd_signal)(signum, handler);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, signal, int signum, void *handler) {
|
2014-01-28 17:28:57 +08:00
|
|
|
if (!AsanInterceptsSignal(signum) ||
|
|
|
|
common_flags()->allow_user_segv_handler) {
|
2012-04-16 16:33:01 +08:00
|
|
|
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
|
|
|
}
|
2014-02-13 22:33:24 +08:00
|
|
|
#endif
|
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) {
|
2014-01-28 17:28:57 +08:00
|
|
|
if (!AsanInterceptsSignal(signum) ||
|
|
|
|
common_flags()->allow_user_segv_handler) {
|
2012-04-16 16:33:01 +08:00
|
|
|
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
|
|
|
}
|
2014-01-31 21:10:07 +08:00
|
|
|
|
2014-02-05 23:06:32 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
int real_sigaction(int signum, const void *act, void *oldact) {
|
2014-11-14 06:40:59 +08:00
|
|
|
return REAL(sigaction)(signum, (const struct sigaction *)act,
|
|
|
|
(struct sigaction *)oldact);
|
2014-01-31 21:10:07 +08:00
|
|
|
}
|
2014-02-05 23:06:32 +08:00
|
|
|
} // namespace __sanitizer
|
2014-01-31 21:10:07 +08:00
|
|
|
|
2013-03-19 23:26:41 +08:00
|
|
|
#elif SANITIZER_POSIX
|
2012-03-26 17:07:29 +08:00
|
|
|
// We need to have defined REAL(sigaction) on posix systems.
|
|
|
|
DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act,
|
2013-06-10 22:17:08 +08:00
|
|
|
struct sigaction *oldact)
|
2012-03-26 17:07:29 +08:00
|
|
|
#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
|
2013-01-17 23:45:28 +08:00
|
|
|
static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
|
|
|
|
// Align to page size.
|
|
|
|
uptr PageSize = GetPageSizeCached();
|
|
|
|
uptr bottom = stack & ~(PageSize - 1);
|
|
|
|
ssize += stack - bottom;
|
|
|
|
ssize = RoundUpTo(ssize, PageSize);
|
|
|
|
static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
|
2013-01-18 19:30:36 +08:00
|
|
|
if (ssize && ssize <= kMaxSaneContextStackSize) {
|
2013-01-17 23:45:28 +08:00
|
|
|
PoisonShadow(bottom, ssize, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-23 17:46:34 +08:00
|
|
|
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).
|
2013-01-17 23:45:28 +08:00
|
|
|
uptr stack, ssize;
|
|
|
|
ReadContextStack(ucp, &stack, &ssize);
|
|
|
|
ClearShadowMemoryForContextStack(stack, ssize);
|
2012-11-23 17:46:34 +08:00
|
|
|
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.
|
2013-01-17 23:45:28 +08:00
|
|
|
ClearShadowMemoryForContextStack(stack, ssize);
|
2012-11-23 17:46:34 +08:00
|
|
|
return res;
|
|
|
|
}
|
2013-01-17 23:45:28 +08:00
|
|
|
#endif // ASAN_INTERCEPT_SWAPCONTEXT
|
2012-11-23 17:46:34 +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
|
|
|
|
|
2014-07-22 20:37:51 +08:00
|
|
|
#if SANITIZER_WINDOWS
|
|
|
|
INTERCEPTOR_WINAPI(void, RaiseException, void *a, void *b, void *c, void *d) {
|
|
|
|
CHECK(REAL(RaiseException));
|
|
|
|
__asan_handle_no_return();
|
|
|
|
REAL(RaiseException)(a, b, c, d);
|
|
|
|
}
|
2014-07-22 21:44:18 +08:00
|
|
|
|
|
|
|
INTERCEPTOR(int, _except_handler3, void *a, void *b, void *c, void *d) {
|
|
|
|
CHECK(REAL(_except_handler3));
|
|
|
|
__asan_handle_no_return();
|
|
|
|
return REAL(_except_handler3)(a, b, c, d);
|
|
|
|
}
|
2014-08-05 21:26:26 +08:00
|
|
|
|
2014-08-22 20:38:07 +08:00
|
|
|
#if ASAN_DYNAMIC
|
|
|
|
// This handler is named differently in -MT and -MD CRTs.
|
|
|
|
#define _except_handler4 _except_handler4_common
|
|
|
|
#endif
|
2014-08-05 21:26:26 +08:00
|
|
|
INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) {
|
|
|
|
CHECK(REAL(_except_handler4));
|
|
|
|
__asan_handle_no_return();
|
|
|
|
return REAL(_except_handler4)(a, b, c, d);
|
|
|
|
}
|
2014-07-22 20:37:51 +08:00
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, memcmp);
|
2014-05-14 22:03:31 +08:00
|
|
|
if (UNLIKELY(!asan_inited)) return internal_memcmp(a1, a2, size);
|
2011-12-29 02:56:42 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2013-02-26 15:25:18 +08:00
|
|
|
if (flags()->replace_intrin) {
|
2013-02-28 22:09:30 +08:00
|
|
|
if (flags()->strict_memcmp) {
|
|
|
|
// Check the entire regions even if the first bytes of the buffers are
|
|
|
|
// different.
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, a1, size);
|
|
|
|
ASAN_READ_RANGE(ctx, a2, size);
|
2013-02-28 22:09:30 +08:00
|
|
|
// Fallthrough to REAL(memcmp) below.
|
|
|
|
} else {
|
|
|
|
unsigned char c1 = 0, c2 = 0;
|
|
|
|
const unsigned char *s1 = (const unsigned char*)a1;
|
|
|
|
const unsigned char *s2 = (const unsigned char*)a2;
|
|
|
|
uptr i;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
c1 = s1[i];
|
|
|
|
c2 = s2[i];
|
|
|
|
if (c1 != c2) break;
|
|
|
|
}
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, s1, Min(i + 1, size));
|
|
|
|
ASAN_READ_RANGE(ctx, s2, Min(i + 1, size));
|
2013-02-28 22:09:30 +08:00
|
|
|
return CharCmp(c1, c2);
|
|
|
|
}
|
2011-12-29 02:56:42 +08:00
|
|
|
}
|
2013-02-26 15:25:18 +08:00
|
|
|
return REAL(memcmp(a1, a2, size));
|
2011-12-29 02:56:42 +08:00
|
|
|
}
|
|
|
|
|
2014-12-06 04:26:09 +08:00
|
|
|
// memcpy is called during __asan_init() from the internals of printf(...).
|
|
|
|
// We do not treat memcpy with to==from as a bug.
|
|
|
|
// See http://llvm.org/bugs/show_bug.cgi?id=11763.
|
|
|
|
#define ASAN_MEMCPY_IMPL(ctx, to, from, size) do { \
|
|
|
|
if (UNLIKELY(!asan_inited)) return internal_memcpy(to, from, size); \
|
|
|
|
if (asan_init_is_running) { \
|
|
|
|
return REAL(memcpy)(to, from, size); \
|
|
|
|
} \
|
|
|
|
ENSURE_ASAN_INITED(); \
|
|
|
|
if (flags()->replace_intrin) { \
|
|
|
|
if (to != from) { \
|
|
|
|
CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); \
|
|
|
|
} \
|
|
|
|
ASAN_READ_RANGE(ctx, from, size); \
|
|
|
|
ASAN_WRITE_RANGE(ctx, to, size); \
|
|
|
|
} \
|
|
|
|
return REAL(memcpy)(to, from, size); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-21 19:58:25 +08:00
|
|
|
void *__asan_memcpy(void *to, const void *from, uptr size) {
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_MEMCPY_IMPL(nullptr, to, from, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2014-12-06 04:26:09 +08:00
|
|
|
// memset is called inside Printf.
|
|
|
|
#define ASAN_MEMSET_IMPL(ctx, block, c, size) do { \
|
|
|
|
if (UNLIKELY(!asan_inited)) return internal_memset(block, c, size); \
|
|
|
|
if (asan_init_is_running) { \
|
|
|
|
return REAL(memset)(block, c, size); \
|
|
|
|
} \
|
|
|
|
ENSURE_ASAN_INITED(); \
|
|
|
|
if (flags()->replace_intrin) { \
|
|
|
|
ASAN_WRITE_RANGE(ctx, block, size); \
|
|
|
|
} \
|
|
|
|
return REAL(memset)(block, c, size); \
|
|
|
|
} while (0)
|
|
|
|
|
2014-04-21 19:58:25 +08:00
|
|
|
void *__asan_memset(void *block, int c, uptr size) {
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_MEMSET_IMPL(nullptr, block, c, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2014-12-06 04:26:09 +08:00
|
|
|
#define ASAN_MEMMOVE_IMPL(ctx, to, from, size) do { \
|
|
|
|
if (UNLIKELY(!asan_inited)) \
|
|
|
|
return internal_memmove(to, from, size); \
|
|
|
|
ENSURE_ASAN_INITED(); \
|
|
|
|
if (flags()->replace_intrin) { \
|
|
|
|
ASAN_READ_RANGE(ctx, from, size); \
|
|
|
|
ASAN_WRITE_RANGE(ctx, to, size); \
|
|
|
|
} \
|
|
|
|
return internal_memmove(to, from, size); \
|
|
|
|
} while (0)
|
|
|
|
|
2014-04-21 19:58:25 +08:00
|
|
|
void *__asan_memmove(void *to, const void *from, uptr size) {
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_MEMMOVE_IMPL(nullptr, to, from, size);
|
2014-04-21 19:58:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, memmove);
|
|
|
|
ASAN_MEMMOVE_IMPL(ctx, to, from, size);
|
2014-04-21 19:58:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, memcpy);
|
2014-04-21 19:58:25 +08:00
|
|
|
#if !SANITIZER_MAC
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_MEMCPY_IMPL(ctx, to, from, size);
|
2014-04-21 19:58:25 +08:00
|
|
|
#else
|
|
|
|
// At least on 10.7 and 10.8 both memcpy() and memmove() are being replaced
|
|
|
|
// with WRAP(memcpy). As a result, false positives are reported for memmove()
|
|
|
|
// calls. If we just disable error reporting with
|
|
|
|
// ASAN_OPTIONS=replace_intrin=0, memmove() is still replaced with
|
|
|
|
// internal_memcpy(), which may lead to crashes, see
|
|
|
|
// http://llvm.org/bugs/show_bug.cgi?id=16362.
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_MEMMOVE_IMPL(ctx, to, from, size);
|
2014-04-21 19:58:25 +08:00
|
|
|
#endif // !SANITIZER_MAC
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, memset);
|
|
|
|
ASAN_MEMSET_IMPL(ctx, block, c, size);
|
2014-04-21 19:58:25 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(char*, strchr, const char *str, int c) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strchr);
|
2014-05-14 22:03:31 +08:00
|
|
|
if (UNLIKELY(!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;
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, str, bytes_read);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
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)
|
2013-02-21 23:15:43 +08:00
|
|
|
ALIAS(WRAPPER_NAME(strchr));
|
2012-08-01 19:17:00 +08:00
|
|
|
# else
|
2013-03-19 22:33:38 +08:00
|
|
|
# if SANITIZER_MAC
|
2013-02-21 22:41:16 +08:00
|
|
|
DECLARE_REAL(char*, index, const char *string, int c)
|
|
|
|
OVERRIDE_FUNCTION(index, strchr);
|
|
|
|
# else
|
2013-06-10 22:17:08 +08:00
|
|
|
DEFINE_REAL(char*, index, const char *string, int c)
|
2013-02-21 22:41:16 +08:00
|
|
|
# endif
|
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
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strcat); // 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);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, from, from_length + 1);
|
2012-08-02 18:25:46 +08:00
|
|
|
uptr to_length = REAL(strlen)(to);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, to, to_length);
|
|
|
|
ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
|
2012-08-02 18:25:46 +08:00
|
|
|
// 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) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strncat);
|
2012-06-08 21:27:46 +08:00
|
|
|
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);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, from, copy_length);
|
2012-06-08 21:27:46 +08:00
|
|
|
uptr to_length = REAL(strlen)(to);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, to, to_length);
|
|
|
|
ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
|
2012-06-08 21:27:46 +08:00
|
|
|
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(char*, strcpy, char *to, const char *from) { // NOLINT
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strcpy); // NOLINT
|
2013-03-19 22:33:38 +08:00
|
|
|
#if SANITIZER_MAC
|
2014-05-14 22:03:31 +08:00
|
|
|
if (UNLIKELY(!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);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, from, from_size);
|
|
|
|
ASAN_WRITE_RANGE(ctx, to, from_size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
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) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
|
2014-05-14 22:03:31 +08:00
|
|
|
if (UNLIKELY(!asan_inited)) return internal_strdup(s);
|
2011-12-06 02:56:29 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
2013-06-21 22:41:59 +08:00
|
|
|
uptr length = REAL(strlen)(s);
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str) {
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, s, length + 1);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2013-06-21 22:41:59 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
|
|
|
void *new_mem = asan_malloc(length + 1, &stack);
|
|
|
|
REAL(memcpy)(new_mem, s, length + 1);
|
|
|
|
return reinterpret_cast<char*>(new_mem);
|
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
|
|
|
|
2014-08-22 00:12:46 +08:00
|
|
|
INTERCEPTOR(SIZE_T, strlen, const char *s) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strlen);
|
2014-05-14 22:03:31 +08:00
|
|
|
if (UNLIKELY(!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();
|
2014-08-22 00:12:46 +08:00
|
|
|
SIZE_T length = REAL(strlen)(s);
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->replace_str) {
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, s, length + 1);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2014-08-22 00:12:46 +08:00
|
|
|
INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, wcslen);
|
2014-08-22 00:12:46 +08:00
|
|
|
SIZE_T length = REAL(wcslen)(s);
|
2013-09-05 09:13:49 +08:00
|
|
|
if (!asan_init_is_running) {
|
|
|
|
ENSURE_ASAN_INITED();
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, s, (length + 1) * sizeof(wchar_t));
|
2013-09-05 09:13:49 +08:00
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strncpy);
|
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);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, from, from_size);
|
|
|
|
ASAN_WRITE_RANGE(ctx, to, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
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) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strnlen);
|
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) {
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, 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) {
|
2013-04-05 22:40:25 +08:00
|
|
|
CHECK(endptr);
|
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++;
|
2014-11-14 06:40:59 +08:00
|
|
|
*endptr = const_cast<char *>(nptr);
|
2012-03-27 00:42:22 +08:00
|
|
|
}
|
|
|
|
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) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strtol);
|
2012-03-24 16:39:14 +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(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);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, nptr, (real_endptr - nptr) + 1);
|
2012-03-24 16:39:14 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-29 16:04:35 +08:00
|
|
|
INTERCEPTOR(int, atoi, const char *nptr) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, atoi);
|
2013-03-19 22:33:38 +08:00
|
|
|
#if SANITIZER_MAC
|
2014-05-14 22:03:31 +08:00
|
|
|
if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
|
2012-08-17 17:00:08 +08:00
|
|
|
#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);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, nptr, (real_endptr - nptr) + 1);
|
2012-03-29 16:04:35 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(long, atol, const char *nptr) { // NOLINT
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, atol);
|
2013-03-19 22:33:38 +08:00
|
|
|
#if SANITIZER_MAC
|
2014-05-14 22:03:31 +08:00
|
|
|
if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
|
2012-08-17 17:00:08 +08:00
|
|
|
#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);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, nptr, (real_endptr - nptr) + 1);
|
2012-03-29 16:04:35 +08:00
|
|
|
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) {
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strtoll);
|
2012-03-27 00:42:22 +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(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);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, nptr, (real_endptr - nptr) + 1);
|
2012-03-27 00:42:22 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-29 16:04:35 +08:00
|
|
|
INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT
|
2014-12-06 04:26:09 +08:00
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, atoll);
|
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(atoll)(nptr);
|
|
|
|
}
|
|
|
|
char *real_endptr;
|
|
|
|
long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT
|
|
|
|
FixRealStrtolEndptr(nptr, &real_endptr);
|
2014-12-06 04:26:09 +08:00
|
|
|
ASAN_READ_RANGE(ctx, nptr, (real_endptr - nptr) + 1);
|
2012-03-29 16:04:35 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
|
|
|
|
|
2013-05-24 19:46:56 +08:00
|
|
|
static void AtCxaAtexit(void *unused) {
|
|
|
|
(void)unused;
|
|
|
|
StopInitOrderChecking();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ASAN_INTERCEPT___CXA_ATEXIT
|
|
|
|
INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
|
|
|
|
void *dso_handle) {
|
2013-11-13 21:34:53 +08:00
|
|
|
#if SANITIZER_MAC
|
2014-05-14 22:03:31 +08:00
|
|
|
if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
|
2013-11-13 21:34:53 +08:00
|
|
|
#endif
|
2013-05-24 19:46:56 +08:00
|
|
|
ENSURE_ASAN_INITED();
|
|
|
|
int res = REAL(__cxa_atexit)(func, arg, dso_handle);
|
|
|
|
REAL(__cxa_atexit)(AtCxaAtexit, 0, 0);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif // ASAN_INTERCEPT___CXA_ATEXIT
|
|
|
|
|
2014-06-04 20:13:54 +08:00
|
|
|
#if ASAN_INTERCEPT_FORK
|
|
|
|
INTERCEPTOR(int, fork, void) {
|
|
|
|
ENSURE_ASAN_INITED();
|
|
|
|
if (common_flags()->coverage) CovBeforeFork();
|
|
|
|
int pid = REAL(fork)();
|
|
|
|
if (common_flags()->coverage) CovAfterFork(pid);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
#endif // ASAN_INTERCEPT_FORK
|
|
|
|
|
2013-03-19 22:33:38 +08:00
|
|
|
#if SANITIZER_WINDOWS
|
2012-02-24 23:28:43 +08:00
|
|
|
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,
|
2013-07-02 00:38:38 +08:00
|
|
|
DWORD thr_flags, void* tid) {
|
2014-12-05 08:10:15 +08:00
|
|
|
// Strict init-order checking is thread-hostile.
|
2013-07-02 00:16:41 +08:00
|
|
|
if (flags()->strict_init_order)
|
|
|
|
StopInitOrderChecking();
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_THREAD;
|
2013-05-30 01:26:25 +08:00
|
|
|
bool detached = false; // FIXME: how can we determine it on Windows?
|
2014-12-06 00:53:58 +08:00
|
|
|
ThreadStartParam param;
|
|
|
|
atomic_store(¶m.t, 0, memory_order_relaxed);
|
|
|
|
atomic_store(¶m.is_registered, 0, memory_order_relaxed);
|
|
|
|
DWORD result = REAL(CreateThread)(security, stack_size, asan_thread_start,
|
|
|
|
¶m, thr_flags, tid);
|
|
|
|
if (result) {
|
|
|
|
u32 current_tid = GetCurrentTidOrInvalid();
|
2014-12-06 01:31:13 +08:00
|
|
|
AsanThread *t =
|
|
|
|
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
|
2014-12-06 00:53:58 +08:00
|
|
|
atomic_store(¶m.t, reinterpret_cast<uptr>(t), memory_order_release);
|
|
|
|
// The pthread_create interceptor waits here, so we do the same for
|
|
|
|
// consistency.
|
|
|
|
while (atomic_load(¶m.is_registered, memory_order_acquire) == 0)
|
|
|
|
internal_sched_yield();
|
|
|
|
}
|
|
|
|
return result;
|
2012-02-24 23:28:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
void InitializeWindowsInterceptors() {
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(CreateThread);
|
2014-07-22 20:37:51 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(RaiseException);
|
2014-07-22 21:44:18 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(_except_handler3);
|
2014-08-05 21:26:26 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(_except_handler4);
|
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;
|
2014-05-07 21:24:28 +08:00
|
|
|
InitializeCommonInterceptors();
|
2012-12-12 17:54:35 +08:00
|
|
|
|
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
|
|
|
}
|
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(strcpy); // NOLINT
|
|
|
|
ASAN_INTERCEPT_FUNC(strlen);
|
2013-09-05 09:13:49 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(wcslen);
|
2012-06-08 21:27:46 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(strncat);
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(strncpy);
|
2012-08-01 19:17:00 +08:00
|
|
|
#if ASAN_INTERCEPT_STRDUP
|
|
|
|
ASAN_INTERCEPT_FUNC(strdup);
|
|
|
|
#endif
|
|
|
|
#if ASAN_INTERCEPT_STRNLEN
|
|
|
|
ASAN_INTERCEPT_FUNC(strnlen);
|
|
|
|
#endif
|
2013-02-05 23:57:12 +08:00
|
|
|
#if ASAN_INTERCEPT_INDEX && 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
|
|
|
#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);
|
2014-02-13 22:33:24 +08:00
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
ASAN_INTERCEPT_FUNC(bsd_signal);
|
|
|
|
#else
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(signal);
|
2012-02-13 20:04:36 +08:00
|
|
|
#endif
|
2014-02-13 22:33:24 +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
|
|
|
|
|
|
|
|
// Intercept exception handling functions.
|
|
|
|
#if ASAN_INTERCEPT___CXA_THROW
|
2014-08-21 21:33:39 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(__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);
|
2014-12-17 03:13:01 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(pthread_join);
|
2012-02-22 21:59:49 +08:00
|
|
|
#endif
|
|
|
|
|
2013-05-24 19:46:56 +08:00
|
|
|
// Intercept atexit function.
|
|
|
|
#if ASAN_INTERCEPT___CXA_ATEXIT
|
|
|
|
ASAN_INTERCEPT_FUNC(__cxa_atexit);
|
|
|
|
#endif
|
|
|
|
|
2014-06-04 20:13:54 +08:00
|
|
|
#if ASAN_INTERCEPT_FORK
|
|
|
|
ASAN_INTERCEPT_FUNC(fork);
|
|
|
|
#endif
|
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
// Some Windows-specific interceptors.
|
2013-03-19 22:33:38 +08:00
|
|
|
#if SANITIZER_WINDOWS
|
2012-02-24 23:28:43 +08:00
|
|
|
InitializeWindowsInterceptors();
|
|
|
|
#endif
|
|
|
|
|
2013-12-05 20:04:51 +08:00
|
|
|
VReport(1, "AddressSanitizer: libc interceptors initialized\n");
|
2012-01-10 03:35:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __asan
|