Skip to content

[TSan] Add support for Android #147580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,14 @@ INTERCEPTOR(int, puts, char *s) {
#if SANITIZER_INTERCEPT_PRCTL
INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
unsigned long arg4, unsigned long arg5) {
# if SANITIZER_ANDROID
static const int PR_PAC_RESET_KEYS = 54;
// workaround to avoid crash
if (option == PR_PAC_RESET_KEYS) {
return REAL(prctl)(option, arg2, arg3, arg4, arg5);
}
# endif

void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
static const int PR_SET_NAME = 15;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static inline bool ReportSupportsColors() { return true; }
bool ColorizeReports() {
// FIXME: Add proper Windows support to AnsiColorDecorator and re-enable color
// printing on Windows.
if (SANITIZER_WINDOWS)
if (SANITIZER_WINDOWS || SANITIZER_ANDROID)
return false;

const char *flag = common_flags()->color;
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2411,7 +2411,7 @@ TSAN_INTERCEPTOR(int, vfork, int fake) {
}
#endif

#if SANITIZER_LINUX
#if SANITIZER_LINUX && !SANITIZER_ANDROID
TSAN_INTERCEPTOR(int, clone, int (*fn)(void *), void *stack, int flags,
void *arg, int *parent_tid, void *tls, pid_t *child_tid) {
SCOPED_INTERCEPTOR_RAW(clone, fn, stack, flags, arg, parent_tid, tls,
Expand Down Expand Up @@ -3120,7 +3120,7 @@ void InitializeInterceptors() {

TSAN_INTERCEPT(fork);
TSAN_INTERCEPT(vfork);
#if SANITIZER_LINUX
#if SANITIZER_LINUX && !SANITIZER_ANDROID
TSAN_INTERCEPT(clone);
#endif
#if !SANITIZER_ANDROID
Expand Down
86 changes: 55 additions & 31 deletions compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ extern "C" void *__libc_stack_end;
void *__libc_stack_end = 0;
#endif

#if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64)) && \
!SANITIZER_GO
# define INIT_LONGJMP_XOR_KEY 1
#else
# define INIT_LONGJMP_XOR_KEY 0
#endif
# if SANITIZER_LINUX && \
(defined(__aarch64__) || defined(__loongarch_lp64)) && !SANITIZER_GO && \
!SANITIZER_ANDROID
# define INIT_LONGJMP_XOR_KEY 1
# else
# define INIT_LONGJMP_XOR_KEY 0
# endif

#if INIT_LONGJMP_XOR_KEY
#include "interception/interception.h"
# if INIT_LONGJMP_XOR_KEY
# include "interception/interception.h"
// Must be declared outside of other namespaces.
DECLARE_REAL(int, _setjmp, void *env)
#endif
Expand Down Expand Up @@ -415,7 +416,7 @@ void InitializePlatform() {
// is not compiled with -pie.
#if !SANITIZER_GO
{
# if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64))
# if INIT_LONGJMP_XOR_KEY
// Initialize the xor key used in {sig}{set,long}jump.
InitializeLongjmpXorKey();
# endif
Expand Down Expand Up @@ -484,6 +485,7 @@ int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) {
return res;
}

# if !SANITIZER_ANDROID
// Reverse operation of libc stack pointer mangling
static uptr UnmangleLongJmpSp(uptr mangled_sp) {
#if defined(__x86_64__)
Expand Down Expand Up @@ -527,40 +529,54 @@ static uptr UnmangleLongJmpSp(uptr mangled_sp) {
# error "Unknown platform"
# endif
}
# endif // !SANITIZER_ANDROID

#if SANITIZER_NETBSD
# ifdef __x86_64__
# define LONG_JMP_SP_ENV_SLOT 6
# else
# error unsupported
# endif
#elif defined(__powerpc__)
# define LONG_JMP_SP_ENV_SLOT 0
#elif SANITIZER_FREEBSD
# ifdef __aarch64__
# define LONG_JMP_SP_ENV_SLOT 1
# else
# define LONG_JMP_SP_ENV_SLOT 2
# endif
#elif SANITIZER_LINUX
# ifdef __aarch64__
# define LONG_JMP_SP_ENV_SLOT 13
# elif defined(__loongarch__)
# define LONG_JMP_SP_ENV_SLOT 1
# elif defined(__mips64)
# define LONG_JMP_SP_ENV_SLOT 1
# if SANITIZER_NETBSD
# ifdef __x86_64__
# define LONG_JMP_SP_ENV_SLOT 6
# else
# error unsupported
# endif
# elif defined(__powerpc__)
# define LONG_JMP_SP_ENV_SLOT 0
# elif SANITIZER_FREEBSD
# ifdef __aarch64__
# define LONG_JMP_SP_ENV_SLOT 1
# else
# define LONG_JMP_SP_ENV_SLOT 2
# endif
# elif SANITIZER_LINUX && !SANITIZER_ANDROID
# ifdef __aarch64__
# define LONG_JMP_SP_ENV_SLOT 13
# elif defined(__loongarch__)
# define LONG_JMP_SP_ENV_SLOT 1
# elif defined(__mips64)
# define LONG_JMP_SP_ENV_SLOT 1
# elif SANITIZER_RISCV64
# define LONG_JMP_SP_ENV_SLOT 13
# elif defined(__s390x__)
# define LONG_JMP_SP_ENV_SLOT 9
# else
# define LONG_JMP_SP_ENV_SLOT 6
# endif
#endif
# elif SANITIZER_ANDROID
# ifdef __aarch64__
# define LONG_JMP_SP_ENV_SLOT 3
# define LONG_JMP_COOKIE_ENV_SLOT 0
# else
# error unsupported
# endif
# endif

uptr ExtractLongJmpSp(uptr *env) {
uptr mangled_sp = env[LONG_JMP_SP_ENV_SLOT];
# if SANITIZER_ANDROID
// This only works for Android arm64
// https://android.googlesource.com/platform/bionic/+/refs/heads/android16-release/libc/arch-arm64/bionic/setjmp.S#46
return mangled_sp ^ (env[LONG_JMP_COOKIE_ENV_SLOT] & ~1ULL);
# else
return UnmangleLongJmpSp(mangled_sp);
# endif
}

#if INIT_LONGJMP_XOR_KEY
Expand Down Expand Up @@ -653,6 +669,14 @@ ThreadState *cur_thread() {
}
CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &oldset, nullptr));
}

// This is a temporary workaround.
// Somewhere wrote get_android_tls_ptr unexpected.
uptr addr = reinterpret_cast<uptr>(thr);
if (addr % 2 != 0) {
return reinterpret_cast<ThreadState *>(addr & ~1ULL);
}

return thr;
}

Expand Down
5 changes: 4 additions & 1 deletion compiler-rt/lib/tsan/rtl/tsan_rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ void MapShadow(uptr addr, uptr size) {
addr + size, meta_begin, meta_end);
}

#if !SANITIZER_GO
#if !SANITIZER_GO && !SANITIZER_ANDROID
static void OnStackUnwind(const SignalContext &sig, const void *,
BufferedStackTrace *stack) {
stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,
Expand Down Expand Up @@ -733,6 +733,9 @@ void Initialize(ThreadState *thr) {
#if !SANITIZER_GO
InitializeShadowMemory();
InitializeAllocatorLate();
#endif

#if !SANITIZER_GO && !SANITIZER_ANDROID
InstallDeadlySignalHandlers(TsanOnDeadlySignal);
#endif
// Setup correct file descriptor for error reports.
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void ThreadStart(ThreadState *thr, Tid tid, tid_t os_id,
}
#endif

#if !SANITIZER_GO
#if !SANITIZER_GO && !SANITIZER_ANDROID
// Don't imitate stack/TLS writes for the main thread,
// because its initialization is synchronized with all
// subsequent threads anyway.
Expand Down