2019-02-27 23:44:03 +08:00
|
|
|
//===-- hwasan_interceptors.cpp -------------------------------------------===//
|
2017-12-09 09:31:51 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-12-09 09:31:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of HWAddressSanitizer.
|
|
|
|
//
|
|
|
|
// Interceptors for standard library functions.
|
|
|
|
//
|
|
|
|
// FIXME: move as many interceptors as possible into
|
|
|
|
// sanitizer_common/sanitizer_common_interceptors.h
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "interception/interception.h"
|
|
|
|
#include "hwasan.h"
|
|
|
|
#include "hwasan_thread.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
|
|
|
|
2021-06-08 06:35:28 +08:00
|
|
|
#if !SANITIZER_FUCHSIA
|
2017-12-09 09:31:51 +08:00
|
|
|
|
|
|
|
using namespace __hwasan;
|
|
|
|
|
2018-09-07 06:53:08 +08:00
|
|
|
#if HWASAN_WITH_INTERCEPTORS
|
|
|
|
|
2019-11-04 18:58:46 +08:00
|
|
|
struct ThreadStartArg {
|
|
|
|
thread_callback_t callback;
|
|
|
|
void *param;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *HwasanThreadStartFunc(void *arg) {
|
|
|
|
__hwasan_thread_enter();
|
|
|
|
ThreadStartArg A = *reinterpret_cast<ThreadStartArg*>(arg);
|
|
|
|
UnmapOrDie(arg, GetPageSizeCached());
|
|
|
|
return A.callback(A.param);
|
|
|
|
}
|
2019-01-05 07:24:02 +08:00
|
|
|
|
2019-11-04 18:58:46 +08:00
|
|
|
INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
|
|
|
|
void * param) {
|
2019-01-05 07:24:02 +08:00
|
|
|
ScopedTaggingDisabler disabler;
|
2019-11-04 18:58:46 +08:00
|
|
|
ThreadStartArg *A = reinterpret_cast<ThreadStartArg *> (MmapOrDie(
|
|
|
|
GetPageSizeCached(), "pthread_create"));
|
|
|
|
*A = {callback, param};
|
2021-03-25 21:34:25 +08:00
|
|
|
int res = REAL(pthread_create)(th, attr, &HwasanThreadStartFunc, A);
|
2019-01-05 07:24:02 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-12-02 15:52:31 +08:00
|
|
|
INTERCEPTOR(int, pthread_join, void *t, void **arg) {
|
|
|
|
return REAL(pthread_join)(t, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_REAL_PTHREAD_FUNCTIONS
|
|
|
|
|
2019-05-10 22:15:13 +08:00
|
|
|
DEFINE_REAL(int, vfork)
|
|
|
|
DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(int, vfork)
|
[hwasan, asan] Intercept vfork.
Summary:
Intercept vfork on arm, aarch64, i386 and x86_64.
Reviewers: pcc, vitalybuka
Subscribers: kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, #sanitizers, llvm-commits
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58533
llvm-svn: 355030
2019-02-28 05:11:50 +08:00
|
|
|
|
2019-10-30 22:04:40 +08:00
|
|
|
// Get and/or change the set of blocked signals.
|
|
|
|
extern "C" int sigprocmask(int __how, const __hw_sigset_t *__restrict __set,
|
|
|
|
__hw_sigset_t *__restrict __oset);
|
|
|
|
#define SIG_BLOCK 0
|
|
|
|
#define SIG_SETMASK 2
|
|
|
|
extern "C" int __sigjmp_save(__hw_sigjmp_buf env, int savemask) {
|
2021-09-15 22:53:25 +08:00
|
|
|
env[0].__magic = kHwJmpBufMagic;
|
2019-10-30 22:04:40 +08:00
|
|
|
env[0].__mask_was_saved =
|
|
|
|
(savemask && sigprocmask(SIG_BLOCK, (__hw_sigset_t *)0,
|
|
|
|
&env[0].__saved_mask) == 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __attribute__((always_inline))
|
|
|
|
InternalLongjmp(__hw_register_buf env, int retval) {
|
2021-09-17 22:08:57 +08:00
|
|
|
# if defined(__aarch64__)
|
|
|
|
constexpr size_t kSpIndex = 13;
|
|
|
|
# elif defined(__x86_64__)
|
|
|
|
constexpr size_t kSpIndex = 6;
|
|
|
|
# endif
|
|
|
|
|
2019-10-30 22:04:40 +08:00
|
|
|
// Clear all memory tags on the stack between here and where we're going.
|
2021-09-17 22:08:57 +08:00
|
|
|
unsigned long long stack_pointer = env[kSpIndex];
|
2019-10-30 22:04:40 +08:00
|
|
|
// The stack pointer should never be tagged, so we don't need to clear the
|
|
|
|
// tag for this function call.
|
|
|
|
__hwasan_handle_longjmp((void *)stack_pointer);
|
|
|
|
|
|
|
|
// Run code for handling a longjmp.
|
|
|
|
// Need to use a register that isn't going to be loaded from the environment
|
|
|
|
// buffer -- hence why we need to specify the register to use.
|
|
|
|
// Must implement this ourselves, since we don't know the order of registers
|
|
|
|
// in different libc implementations and many implementations mangle the
|
|
|
|
// stack pointer so we can't use it without knowing the demangling scheme.
|
2021-09-17 22:08:57 +08:00
|
|
|
# if defined(__aarch64__)
|
2019-10-30 22:04:40 +08:00
|
|
|
register long int retval_tmp asm("x1") = retval;
|
|
|
|
register void *env_address asm("x0") = &env[0];
|
|
|
|
asm volatile("ldp x19, x20, [%0, #0<<3];"
|
|
|
|
"ldp x21, x22, [%0, #2<<3];"
|
|
|
|
"ldp x23, x24, [%0, #4<<3];"
|
|
|
|
"ldp x25, x26, [%0, #6<<3];"
|
|
|
|
"ldp x27, x28, [%0, #8<<3];"
|
|
|
|
"ldp x29, x30, [%0, #10<<3];"
|
|
|
|
"ldp d8, d9, [%0, #14<<3];"
|
|
|
|
"ldp d10, d11, [%0, #16<<3];"
|
|
|
|
"ldp d12, d13, [%0, #18<<3];"
|
|
|
|
"ldp d14, d15, [%0, #20<<3];"
|
|
|
|
"ldr x5, [%0, #13<<3];"
|
|
|
|
"mov sp, x5;"
|
|
|
|
// Return the value requested to return through arguments.
|
|
|
|
// This should be in x1 given what we requested above.
|
|
|
|
"cmp %1, #0;"
|
|
|
|
"mov x0, #1;"
|
|
|
|
"csel x0, %1, x0, ne;"
|
|
|
|
"br x30;"
|
|
|
|
: "+r"(env_address)
|
|
|
|
: "r"(retval_tmp));
|
2021-09-17 22:08:57 +08:00
|
|
|
# elif defined(__x86_64__)
|
|
|
|
register long int retval_tmp asm("%rsi") = retval;
|
|
|
|
register void *env_address asm("%rdi") = &env[0];
|
|
|
|
asm volatile(
|
|
|
|
// Restore registers.
|
|
|
|
"mov (0*8)(%0),%%rbx;"
|
|
|
|
"mov (1*8)(%0),%%rbp;"
|
|
|
|
"mov (2*8)(%0),%%r12;"
|
|
|
|
"mov (3*8)(%0),%%r13;"
|
|
|
|
"mov (4*8)(%0),%%r14;"
|
|
|
|
"mov (5*8)(%0),%%r15;"
|
|
|
|
"mov (6*8)(%0),%%rsp;"
|
|
|
|
"mov (7*8)(%0),%%rdx;"
|
|
|
|
// Return 1 if retval is 0.
|
|
|
|
"mov $1,%%rax;"
|
|
|
|
"test %1,%1;"
|
|
|
|
"cmovnz %1,%%rax;"
|
|
|
|
"jmp *%%rdx;" ::"r"(env_address),
|
|
|
|
"r"(retval_tmp));
|
|
|
|
# endif
|
2019-10-30 22:04:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void, siglongjmp, __hw_sigjmp_buf env, int val) {
|
2021-09-15 22:53:25 +08:00
|
|
|
if (env[0].__magic != kHwJmpBufMagic) {
|
|
|
|
Printf(
|
|
|
|
"WARNING: Unexpected bad jmp_buf. Either setjmp was not called or "
|
2021-09-16 00:06:01 +08:00
|
|
|
"there is a bug in HWASan.\n");
|
2021-09-15 22:53:25 +08:00
|
|
|
return REAL(siglongjmp)(env, val);
|
|
|
|
}
|
|
|
|
|
2019-10-30 22:04:40 +08:00
|
|
|
if (env[0].__mask_was_saved)
|
|
|
|
// Restore the saved signal mask.
|
|
|
|
(void)sigprocmask(SIG_SETMASK, &env[0].__saved_mask,
|
|
|
|
(__hw_sigset_t *)0);
|
|
|
|
InternalLongjmp(env[0].__jmpbuf, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Required since glibc libpthread calls __libc_longjmp on pthread_exit, and
|
|
|
|
// _setjmp on start_thread. Hence we have to intercept the longjmp on
|
|
|
|
// pthread_exit so the __hw_jmp_buf order matches.
|
|
|
|
INTERCEPTOR(void, __libc_longjmp, __hw_jmp_buf env, int val) {
|
2021-09-15 22:53:25 +08:00
|
|
|
if (env[0].__magic != kHwJmpBufMagic)
|
|
|
|
return REAL(__libc_longjmp)(env, val);
|
2019-10-30 22:04:40 +08:00
|
|
|
InternalLongjmp(env[0].__jmpbuf, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void, longjmp, __hw_jmp_buf env, int val) {
|
2021-09-15 22:53:25 +08:00
|
|
|
if (env[0].__magic != kHwJmpBufMagic) {
|
|
|
|
Printf(
|
|
|
|
"WARNING: Unexpected bad jmp_buf. Either setjmp was not called or "
|
2021-09-16 00:06:01 +08:00
|
|
|
"there is a bug in HWASan.\n");
|
2021-09-15 22:53:25 +08:00
|
|
|
return REAL(longjmp)(env, val);
|
|
|
|
}
|
2019-10-30 22:04:40 +08:00
|
|
|
InternalLongjmp(env[0].__jmpbuf, val);
|
|
|
|
}
|
|
|
|
#undef SIG_BLOCK
|
|
|
|
#undef SIG_SETMASK
|
|
|
|
|
2021-09-17 22:08:57 +08:00
|
|
|
# endif // HWASAN_WITH_INTERCEPTORS
|
2019-10-30 22:04:40 +08:00
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
namespace __hwasan {
|
|
|
|
|
|
|
|
int OnExit() {
|
|
|
|
// FIXME: ask frontend whether we need to return failure.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __hwasan
|
|
|
|
|
|
|
|
namespace __hwasan {
|
|
|
|
|
|
|
|
void InitializeInterceptors() {
|
|
|
|
static int inited = 0;
|
|
|
|
CHECK_EQ(inited, 0);
|
|
|
|
|
[hwasan] Add a (almost) no-interceptor mode.
Summary:
The idea behind this change is to allow sanitization of libc. We are prototyping on Bionic,
but the tool interface will be general enough (or at least generalizable) to support any other libc.
When libc depends on libclang_rt.hwasan, the latter can not interpose libc functions.
In fact, majority of interceptors become unnecessary when libc code is instrumented.
This change gets rid of most hwasan interceptors and provides interface for libc to notify
hwasan about thread creation and destruction events. Some interceptors (pthread_create)
are kept under #ifdef to enable testing with uninstrumented libc. They are expressed in
terms of the new libc interface.
The new cmake switch, COMPILER_RT_HWASAN_WITH_INTERCEPTORS, ON by default, builds testing
version of the library with the aforementioned pthread_create interceptor.
With the OFF setting, the library becomes more of a libc plugin.
Reviewers: vitalybuka, kcc, jfb
Subscribers: srhines, kubamracek, mgorny, jfb, llvm-commits
Differential Revision: https://reviews.llvm.org/D50922
llvm-svn: 340216
2018-08-21 05:49:15 +08:00
|
|
|
#if HWASAN_WITH_INTERCEPTORS
|
[hwasan, asan] Intercept vfork.
Summary:
Intercept vfork on arm, aarch64, i386 and x86_64.
Reviewers: pcc, vitalybuka
Subscribers: kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, #sanitizers, llvm-commits
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58533
llvm-svn: 355030
2019-02-28 05:11:50 +08:00
|
|
|
#if defined(__linux__)
|
2021-09-15 22:53:25 +08:00
|
|
|
INTERCEPT_FUNCTION(__libc_longjmp);
|
|
|
|
INTERCEPT_FUNCTION(longjmp);
|
|
|
|
INTERCEPT_FUNCTION(siglongjmp);
|
[hwasan, asan] Intercept vfork.
Summary:
Intercept vfork on arm, aarch64, i386 and x86_64.
Reviewers: pcc, vitalybuka
Subscribers: kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, #sanitizers, llvm-commits
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58533
llvm-svn: 355030
2019-02-28 05:11:50 +08:00
|
|
|
INTERCEPT_FUNCTION(vfork);
|
|
|
|
#endif // __linux__
|
2019-01-05 07:24:02 +08:00
|
|
|
INTERCEPT_FUNCTION(pthread_create);
|
2021-12-02 15:52:31 +08:00
|
|
|
INTERCEPT_FUNCTION(pthread_join);
|
|
|
|
# endif
|
[hwasan] Add a (almost) no-interceptor mode.
Summary:
The idea behind this change is to allow sanitization of libc. We are prototyping on Bionic,
but the tool interface will be general enough (or at least generalizable) to support any other libc.
When libc depends on libclang_rt.hwasan, the latter can not interpose libc functions.
In fact, majority of interceptors become unnecessary when libc code is instrumented.
This change gets rid of most hwasan interceptors and provides interface for libc to notify
hwasan about thread creation and destruction events. Some interceptors (pthread_create)
are kept under #ifdef to enable testing with uninstrumented libc. They are expressed in
terms of the new libc interface.
The new cmake switch, COMPILER_RT_HWASAN_WITH_INTERCEPTORS, ON by default, builds testing
version of the library with the aforementioned pthread_create interceptor.
With the OFF setting, the library becomes more of a libc plugin.
Reviewers: vitalybuka, kcc, jfb
Subscribers: srhines, kubamracek, mgorny, jfb, llvm-commits
Differential Revision: https://reviews.llvm.org/D50922
llvm-svn: 340216
2018-08-21 05:49:15 +08:00
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
inited = 1;
|
|
|
|
}
|
|
|
|
} // namespace __hwasan
|
2021-06-08 06:35:28 +08:00
|
|
|
|
|
|
|
#endif // #if !SANITIZER_FUCHSIA
|