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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
#include "asan_interceptors.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#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"
|
2016-01-16 08:57:25 +08:00
|
|
|
#include "lsan/lsan_common.h"
|
2012-06-04 21:27:49 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
|
2015-04-09 01:08:24 +08:00
|
|
|
#if SANITIZER_POSIX
|
|
|
|
#include "sanitizer_common/sanitizer_posix.h"
|
|
|
|
#endif
|
|
|
|
|
2015-09-23 05:34:44 +08:00
|
|
|
#if defined(__i386) && SANITIZER_LINUX
|
|
|
|
#define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.1"
|
2015-10-05 20:43:47 +08:00
|
|
|
#elif defined(__mips__) && SANITIZER_LINUX
|
|
|
|
#define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.2"
|
2015-09-23 05:34:44 +08:00
|
|
|
#endif
|
|
|
|
|
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; \
|
2015-11-11 19:59:38 +08:00
|
|
|
ReportGenericError(pc, bp, sp, __bad, isWrite, __size, 0, false);\
|
2014-12-06 04:26:09 +08:00
|
|
|
} \
|
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
|
|
|
|
2015-04-07 02:00:26 +08:00
|
|
|
#define ASAN_READ_STRING_OF_LEN(ctx, s, len, n) \
|
|
|
|
ASAN_READ_RANGE((ctx), (s), \
|
|
|
|
common_flags()->strict_string_checks ? (len) + 1 : (n))
|
|
|
|
|
|
|
|
#define ASAN_READ_STRING(ctx, s, n) \
|
|
|
|
ASAN_READ_STRING_OF_LEN((ctx), (s), REAL(strlen)(s), (n))
|
|
|
|
|
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) {
|
2016-03-24 05:24:28 +08:00
|
|
|
#if SANITIZER_INTERCEPT_STRNLEN
|
2015-10-01 08:22:21 +08:00
|
|
|
if (REAL(strnlen)) {
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
} // namespace __asan
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
// ---------------------- 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 *)
|
|
|
|
|
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)
|
2016-04-28 05:24:21 +08:00
|
|
|
#define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
|
|
|
|
ASAN_INTERCEPT_FUNC_VER(name, ver)
|
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, ...) \
|
2015-01-28 06:50:19 +08:00
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, func); \
|
2014-05-14 22:03:31 +08:00
|
|
|
do { \
|
|
|
|
if (asan_init_is_running) \
|
|
|
|
return REAL(func)(__VA_ARGS__); \
|
|
|
|
if (SANITIZER_MAC && UNLIKELY(!asan_inited)) \
|
|
|
|
return REAL(func)(__VA_ARGS__); \
|
|
|
|
ENSURE_ASAN_INITED(); \
|
2013-05-29 17:09:58 +08:00
|
|
|
} while (false)
|
2015-01-21 16:54:01 +08:00
|
|
|
#define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
|
|
|
|
do { \
|
|
|
|
} while (false)
|
2013-05-29 17:09:58 +08:00
|
|
|
#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)
|
2015-02-24 08:37:27 +08:00
|
|
|
// Strict init-order checking is dlopen-hostile:
|
2015-12-05 01:37:40 +08:00
|
|
|
// https://github.com/google/sanitizers/issues/178
|
2015-02-24 08:37:27 +08:00
|
|
|
#define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
|
|
|
|
if (flags()->strict_init_order) { \
|
|
|
|
StopInitOrderChecking(); \
|
|
|
|
}
|
2013-10-03 23:22:29 +08:00
|
|
|
#define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
|
2015-01-30 20:43:52 +08:00
|
|
|
#define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
|
|
|
|
CoverageUpdateMapping()
|
2014-12-26 20:32:32 +08:00
|
|
|
#define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() CoverageUpdateMapping()
|
2014-07-25 20:45:36 +08:00
|
|
|
#define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
|
2015-05-16 08:34:15 +08:00
|
|
|
#define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
|
|
|
|
if (AsanThread *t = GetCurrentThread()) { \
|
|
|
|
*begin = t->tls_begin(); \
|
|
|
|
*end = t->tls_end(); \
|
|
|
|
} else { \
|
|
|
|
*begin = *end = 0; \
|
|
|
|
}
|
[sanitizer] Add memset, memmove, and memcpy to the common interceptors
Summary:
Currently, sanitizer_common_interceptors.inc has an implicit, undocumented
assumption that the sanitizer including it has previously declared
interceptors for memset and memmove. Since the memset, memmove, and memcpy
routines require interception by many sanitizers, we add them to the
set of common interceptions, both to address the undocumented assumption
and to speed future tool development. They are intercepted under a new
flag intercept_intrin.
The tsan interceptors are removed in favor of the new common versions. The
asan and msan interceptors for these are more complex (they incur extra
interception steps and their function bodies are exposed to the compiler)
so they opt out of the common versions and keep their own.
Reviewers: vitalybuka
Subscribers: zhaoqin, llvm-commits, kcc
Differential Revision: http://reviews.llvm.org/D18465
llvm-svn: 264451
2016-03-26 03:33:45 +08:00
|
|
|
// Asan needs custom handling of these:
|
|
|
|
#undef SANITIZER_INTERCEPT_MEMSET
|
|
|
|
#undef SANITIZER_INTERCEPT_MEMMOVE
|
|
|
|
#undef SANITIZER_INTERCEPT_MEMCPY
|
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;
|
|
|
|
};
|
|
|
|
|
2016-04-02 01:09:12 +08:00
|
|
|
#if ASAN_INTERCEPT_PTHREAD_CREATE
|
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 *>(
|
2015-10-01 08:22:21 +08:00
|
|
|
atomic_load(¶m->t, memory_order_acquire))) == nullptr)
|
2014-12-05 08:10:15 +08:00
|
|
|
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-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;
|
2015-10-01 08:22:21 +08:00
|
|
|
if (attr)
|
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);
|
2016-01-16 08:57:25 +08:00
|
|
|
int result;
|
|
|
|
{
|
|
|
|
// Ignore all allocations made by pthread_create: thread stack/TLS may be
|
|
|
|
// stored by pthread for future reuse even after thread destruction, and
|
|
|
|
// the linked list it's stored in doesn't even hold valid pointers to the
|
|
|
|
// objects, the latter are calculated by obscure pointer arithmetic.
|
|
|
|
#if CAN_SANITIZE_LEAKS
|
|
|
|
__lsan::ScopedInterceptorDisabler disabler;
|
|
|
|
#endif
|
|
|
|
result = REAL(pthread_create)(thread, attr, asan_thread_start, ¶m);
|
|
|
|
}
|
2014-12-05 08:10:15 +08:00
|
|
|
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);
|
|
|
|
}
|
2015-01-13 01:03:02 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-09-03 05:32:46 +08:00
|
|
|
#if SANITIZER_ANDROID
|
2014-02-13 22:33:24 +08:00
|
|
|
INTERCEPTOR(void*, bsd_signal, int signum, void *handler) {
|
2016-02-02 10:01:17 +08:00
|
|
|
if (!IsHandledDeadlySignal(signum) ||
|
|
|
|
common_flags()->allow_user_segv_handler) {
|
2014-02-13 22:33:24 +08:00
|
|
|
return REAL(bsd_signal)(signum, handler);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-03 05:32:46 +08:00
|
|
|
#endif
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, signal, int signum, void *handler) {
|
2016-02-02 10:01:17 +08:00
|
|
|
if (!IsHandledDeadlySignal(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
|
|
|
}
|
2015-10-01 08:22:21 +08:00
|
|
|
return nullptr;
|
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) {
|
2016-02-02 10:01:17 +08:00
|
|
|
if (!IsHandledDeadlySignal(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
|
|
|
}
|
2015-10-01 08:22:21 +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
|
2015-03-28 04:40:23 +08:00
|
|
|
if (AddrIsInMem(bottom) && 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-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-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);
|
2015-04-07 02:00:26 +08:00
|
|
|
ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
|
2014-12-06 04:26:09 +08:00
|
|
|
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);
|
2015-04-07 02:00:26 +08:00
|
|
|
ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
|
2014-12-06 04:26:09 +08:00
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2016-04-21 06:45:23 +08:00
|
|
|
#if ASAN_INTERCEPT___STRDUP
|
|
|
|
INTERCEPTOR(char*, __strdup, const char *s) {
|
|
|
|
void *ctx;
|
|
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
|
|
|
|
if (UNLIKELY(!asan_inited)) return internal_strdup(s);
|
|
|
|
ENSURE_ASAN_INITED();
|
|
|
|
uptr length = REAL(strlen)(s);
|
|
|
|
if (flags()->replace_str) {
|
|
|
|
ASAN_READ_RANGE(ctx, s, length + 1);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
#endif // ASAN_INTERCEPT___STRDUP
|
|
|
|
|
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-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
|
2015-04-07 02:00:26 +08:00
|
|
|
StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
|
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);
|
2015-04-07 02:00:26 +08:00
|
|
|
ASAN_READ_STRING(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);
|
2015-04-07 02:00:26 +08:00
|
|
|
ASAN_READ_STRING(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
|
2015-04-07 02:00:26 +08:00
|
|
|
StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
|
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);
|
2015-04-07 02:00:26 +08:00
|
|
|
ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
|
2012-03-29 16:04:35 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
|
|
|
|
|
2016-04-02 01:09:12 +08:00
|
|
|
#if ASAN_INTERCEPT___CXA_ATEXIT
|
2013-05-24 19:46:56 +08:00
|
|
|
static void AtCxaAtexit(void *unused) {
|
|
|
|
(void)unused;
|
|
|
|
StopInitOrderChecking();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-10-01 08:22:21 +08:00
|
|
|
REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
|
2013-05-24 19:46:56 +08:00
|
|
|
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
|
|
|
|
|
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.
|
2016-07-12 04:37:12 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(memcpy);
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(memset);
|
2012-03-20 18:54:40 +08:00
|
|
|
if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
|
2016-07-12 04:37:12 +08:00
|
|
|
// In asan, REAL(memmove) is not used, but it is used in msan.
|
|
|
|
ASAN_INTERCEPT_FUNC(memmove);
|
2012-02-01 18:07:52 +08:00
|
|
|
}
|
2016-07-12 04:37:12 +08:00
|
|
|
CHECK(REAL(memcpy));
|
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(strcpy); // NOLINT
|
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
|
|
|
ASAN_INTERCEPT_FUNC(strdup);
|
2016-04-21 06:45:23 +08:00
|
|
|
#if ASAN_INTERCEPT___STRDUP
|
|
|
|
ASAN_INTERCEPT_FUNC(__strdup);
|
|
|
|
#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);
|
2015-09-03 05:32:46 +08:00
|
|
|
#if SANITIZER_ANDROID
|
2014-02-13 22:33:24 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(bsd_signal);
|
2012-02-13 20:04:36 +08:00
|
|
|
#endif
|
2015-09-03 05:32:46 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(signal);
|
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
|
2015-09-23 05:34:44 +08:00
|
|
|
#if defined(ASAN_PTHREAD_CREATE_VERSION)
|
|
|
|
ASAN_INTERCEPT_FUNC_VER(pthread_create, ASAN_PTHREAD_CREATE_VERSION);
|
|
|
|
#else
|
2012-05-24 21:54:31 +08:00
|
|
|
ASAN_INTERCEPT_FUNC(pthread_create);
|
2015-09-23 05:34:44 +08:00
|
|
|
#endif
|
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
|
|
|
|
|
2015-03-16 22:22:53 +08:00
|
|
|
InitializePlatformInterceptors();
|
2012-02-24 23:28:43 +08:00
|
|
|
|
2013-12-05 20:04:51 +08:00
|
|
|
VReport(1, "AddressSanitizer: libc interceptors initialized\n");
|
2012-01-10 03:35:11 +08:00
|
|
|
}
|
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
} // namespace __asan
|