2012-11-12 16:53:47 +01:00
|
|
|
//===-- asan_thread.h -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-08-14 10:47:11 +02: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
|
2012-11-12 16:53:47 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
2019-08-14 10:47:11 +02:00
|
|
|
// ASan-private header for asan_thread.cpp.
|
2012-11-12 16:53:47 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-21 09:32:45 +02:00
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
#ifndef ASAN_THREAD_H
|
|
|
|
#define ASAN_THREAD_H
|
|
|
|
|
|
|
|
#include "asan_allocator.h"
|
|
|
|
#include "asan_internal.h"
|
2013-11-04 22:33:31 +01:00
|
|
|
#include "asan_fake_stack.h"
|
2012-11-12 16:53:47 +01:00
|
|
|
#include "asan_stats.h"
|
2013-12-05 10:18:38 +01:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2012-11-12 16:53:47 +01:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2013-11-04 22:33:31 +01:00
|
|
|
#include "sanitizer_common/sanitizer_thread_registry.h"
|
2012-11-12 16:53:47 +01:00
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
namespace __sanitizer {
|
|
|
|
struct DTLS;
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
|
2013-11-04 22:33:31 +01:00
|
|
|
const u32 kMaxNumberOfThreads = (1 << 22); // 4M
|
2012-11-12 16:53:47 +01:00
|
|
|
|
|
|
|
class AsanThread;
|
|
|
|
|
|
|
|
// These objects are created for every thread and are never deleted,
|
|
|
|
// so we can find them by tid even if the thread is long dead.
|
2020-11-13 17:03:23 +01:00
|
|
|
class AsanThreadContext final : public ThreadContextBase {
|
2012-11-12 16:53:47 +01:00
|
|
|
public:
|
2013-11-04 22:33:31 +01:00
|
|
|
explicit AsanThreadContext(int tid)
|
2015-10-21 09:32:45 +02:00
|
|
|
: ThreadContextBase(tid), announced(false),
|
|
|
|
destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
|
|
|
|
thread(nullptr) {}
|
2013-11-04 22:33:31 +01:00
|
|
|
bool announced;
|
2013-12-05 10:18:38 +01:00
|
|
|
u8 destructor_iterations;
|
|
|
|
u32 stack_id;
|
2013-11-04 22:33:31 +01:00
|
|
|
AsanThread *thread;
|
2012-11-12 16:53:47 +01:00
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
void OnCreated(void *arg) override;
|
|
|
|
void OnFinished() override;
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
builtins, store max (log2 (align), 0) into uchar field instead of
align into uptr field.
(ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins,
store uchar 0 field instead of uptr 0 field.
(instrument_nonnull_return): Use _v1 suffixed nonnull return builtin,
instead of passing one address of struct with 2 locations pass
two addresses of structs with 1 location each.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed.
(BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins.
* c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword
from expected output regexps.
* c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-3.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-5.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-6.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-8.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-9.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-10.c: Likewise.
* g++.dg/ubsan/float-cast-overflow-bf.C: Likewise.
* gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise.
* g++.dg/asan/default-options-1.C (__asan_default_options): Add
used attribute.
* g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2
in the environment.
* All source files: Merge from upstream 315899.
* asan/Makefile.am (nodist_saninclude_HEADERS): Add
include/sanitizer/tsan_interface.h.
* asan/libtool-version: Bump the libasan SONAME.
* lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc.
(lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc.
* sanitizer_common/Makefile.am (sanitizer_common_files): Add
sancov_flags.cc, sanitizer_allocator_checks.cc,
sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc,
sanitizer_file.cc, sanitizer_mac_libcdep.cc and
sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc
and sanitizer_coverage_mapping_libcdep.cc.
* tsan/Makefile.am (tsan_files): Add tsan_external.cc.
* ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1.
(ubsan_files): Add ubsan_init_standalone.cc and
ubsan_signals_standalone.cc.
* ubsan/libtool-version: Bump the libubsan SONAME.
* asan/Makefile.in: Regenerate.
* lsan/Makefile.in: Regenerate.
* sanitizer_common/Makefile.in: Regenerate.
* tsan/Makefile.in: Regenerate.
* ubsan/Makefile.in: Regenerate.
From-SVN: r253887
2017-10-19 13:23:59 +02:00
|
|
|
|
|
|
|
struct CreateThreadContextArgs {
|
|
|
|
AsanThread *thread;
|
|
|
|
StackTrace *stack;
|
|
|
|
};
|
2012-11-12 16:53:47 +01:00
|
|
|
};
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
// AsanThreadContext objects are never freed, so we need many of them.
|
2013-12-05 10:18:38 +01:00
|
|
|
COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
|
2013-01-10 13:44:08 +01:00
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
// AsanThread are stored in TSD and destroyed when the thread dies.
|
|
|
|
class AsanThread {
|
|
|
|
public:
|
2015-10-21 09:32:45 +02:00
|
|
|
static AsanThread *Create(thread_callback_t start_routine, void *arg,
|
|
|
|
u32 parent_tid, StackTrace *stack, bool detached);
|
2013-11-04 22:33:31 +01:00
|
|
|
static void TSDDtor(void *tsd);
|
2012-11-12 16:53:47 +01:00
|
|
|
void Destroy();
|
|
|
|
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
builtins, store max (log2 (align), 0) into uchar field instead of
align into uptr field.
(ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins,
store uchar 0 field instead of uptr 0 field.
(instrument_nonnull_return): Use _v1 suffixed nonnull return builtin,
instead of passing one address of struct with 2 locations pass
two addresses of structs with 1 location each.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed.
(BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins.
* c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword
from expected output regexps.
* c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-3.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-5.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-6.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-8.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-9.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-10.c: Likewise.
* g++.dg/ubsan/float-cast-overflow-bf.C: Likewise.
* gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise.
* g++.dg/asan/default-options-1.C (__asan_default_options): Add
used attribute.
* g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2
in the environment.
* All source files: Merge from upstream 315899.
* asan/Makefile.am (nodist_saninclude_HEADERS): Add
include/sanitizer/tsan_interface.h.
* asan/libtool-version: Bump the libasan SONAME.
* lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc.
(lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc.
* sanitizer_common/Makefile.am (sanitizer_common_files): Add
sancov_flags.cc, sanitizer_allocator_checks.cc,
sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc,
sanitizer_file.cc, sanitizer_mac_libcdep.cc and
sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc
and sanitizer_coverage_mapping_libcdep.cc.
* tsan/Makefile.am (tsan_files): Add tsan_external.cc.
* ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1.
(ubsan_files): Add ubsan_init_standalone.cc and
ubsan_signals_standalone.cc.
* ubsan/libtool-version: Bump the libubsan SONAME.
* asan/Makefile.in: Regenerate.
* lsan/Makefile.in: Regenerate.
* sanitizer_common/Makefile.in: Regenerate.
* tsan/Makefile.in: Regenerate.
* ubsan/Makefile.in: Regenerate.
From-SVN: r253887
2017-10-19 13:23:59 +02:00
|
|
|
struct InitOptions;
|
|
|
|
void Init(const InitOptions *options = nullptr);
|
|
|
|
|
|
|
|
thread_return_t ThreadStart(tid_t os_id,
|
2015-10-21 09:32:45 +02:00
|
|
|
atomic_uintptr_t *signal_thread_is_registered);
|
2012-11-12 16:53:47 +01:00
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
uptr stack_top();
|
|
|
|
uptr stack_bottom();
|
|
|
|
uptr stack_size();
|
2013-11-04 22:33:31 +01:00
|
|
|
uptr tls_begin() { return tls_begin_; }
|
|
|
|
uptr tls_end() { return tls_end_; }
|
2016-11-08 23:04:09 +01:00
|
|
|
DTLS *dtls() { return dtls_; }
|
2013-11-04 22:33:31 +01:00
|
|
|
u32 tid() { return context_->tid; }
|
|
|
|
AsanThreadContext *context() { return context_; }
|
|
|
|
void set_context(AsanThreadContext *context) { context_ = context; }
|
2012-11-12 16:53:47 +01:00
|
|
|
|
2014-11-13 21:41:38 +01:00
|
|
|
struct StackFrameAccess {
|
|
|
|
uptr offset;
|
|
|
|
uptr frame_pc;
|
|
|
|
const char *frame_descr;
|
|
|
|
};
|
|
|
|
bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
|
2012-11-12 16:53:47 +01:00
|
|
|
|
2017-12-05 10:23:25 +01:00
|
|
|
// Returns a pointer to the start of the stack variable's shadow memory.
|
|
|
|
uptr GetStackVariableShadowStart(uptr addr);
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
bool AddrIsInStack(uptr addr);
|
2012-11-12 16:53:47 +01:00
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
void DeleteFakeStack(int tid) {
|
2013-11-04 22:33:31 +01:00
|
|
|
if (!fake_stack_) return;
|
|
|
|
FakeStack *t = fake_stack_;
|
2015-10-21 09:32:45 +02:00
|
|
|
fake_stack_ = nullptr;
|
|
|
|
SetTLSFakeStack(nullptr);
|
2014-05-22 09:09:21 +02:00
|
|
|
t->Destroy(tid);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
void StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size);
|
|
|
|
void FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old,
|
|
|
|
uptr *size_old);
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
bool has_fake_stack() {
|
2016-11-08 23:04:09 +01:00
|
|
|
return !atomic_load(&stack_switching_, memory_order_relaxed) &&
|
|
|
|
(reinterpret_cast<uptr>(fake_stack_) > 1);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FakeStack *fake_stack() {
|
|
|
|
if (!__asan_option_detect_stack_use_after_return)
|
2015-10-21 09:32:45 +02:00
|
|
|
return nullptr;
|
2016-11-08 23:04:09 +01:00
|
|
|
if (atomic_load(&stack_switching_, memory_order_relaxed))
|
|
|
|
return nullptr;
|
2013-11-04 22:33:31 +01:00
|
|
|
if (!has_fake_stack())
|
|
|
|
return AsyncSignalSafeLazyInitFakeStack();
|
|
|
|
return fake_stack_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// True is this thread is currently unwinding stack (i.e. collecting a stack
|
|
|
|
// trace). Used to prevent deadlocks on platforms where libc unwinder calls
|
|
|
|
// malloc internally. See PR17116 for more details.
|
2013-12-05 10:18:38 +01:00
|
|
|
bool isUnwinding() const { return unwinding_; }
|
|
|
|
void setUnwinding(bool b) { unwinding_ = b; }
|
2013-11-04 22:33:31 +01:00
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
|
|
|
|
AsanStats &stats() { return stats_; }
|
|
|
|
|
2019-08-14 10:47:11 +02:00
|
|
|
void *extra_spill_area() { return &extra_spill_area_; }
|
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
private:
|
2013-12-05 10:18:38 +01:00
|
|
|
// NOTE: There is no AsanThread constructor. It is allocated
|
|
|
|
// via mmap() and *must* be valid in zero-initialized state.
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
builtins, store max (log2 (align), 0) into uchar field instead of
align into uptr field.
(ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins,
store uchar 0 field instead of uptr 0 field.
(instrument_nonnull_return): Use _v1 suffixed nonnull return builtin,
instead of passing one address of struct with 2 locations pass
two addresses of structs with 1 location each.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed.
(BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins.
* c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword
from expected output regexps.
* c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-3.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-5.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-6.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-8.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-9.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-10.c: Likewise.
* g++.dg/ubsan/float-cast-overflow-bf.C: Likewise.
* gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise.
* g++.dg/asan/default-options-1.C (__asan_default_options): Add
used attribute.
* g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2
in the environment.
* All source files: Merge from upstream 315899.
* asan/Makefile.am (nodist_saninclude_HEADERS): Add
include/sanitizer/tsan_interface.h.
* asan/libtool-version: Bump the libasan SONAME.
* lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc.
(lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc.
* sanitizer_common/Makefile.am (sanitizer_common_files): Add
sancov_flags.cc, sanitizer_allocator_checks.cc,
sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc,
sanitizer_file.cc, sanitizer_mac_libcdep.cc and
sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc
and sanitizer_coverage_mapping_libcdep.cc.
* tsan/Makefile.am (tsan_files): Add tsan_external.cc.
* ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1.
(ubsan_files): Add ubsan_init_standalone.cc and
ubsan_signals_standalone.cc.
* ubsan/libtool-version: Bump the libubsan SONAME.
* asan/Makefile.in: Regenerate.
* lsan/Makefile.in: Regenerate.
* sanitizer_common/Makefile.in: Regenerate.
* tsan/Makefile.in: Regenerate.
* ubsan/Makefile.in: Regenerate.
From-SVN: r253887
2017-10-19 13:23:59 +02:00
|
|
|
|
|
|
|
void SetThreadStackAndTls(const InitOptions *options);
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
void ClearShadowForThreadStackAndTLS();
|
|
|
|
FakeStack *AsyncSignalSafeLazyInitFakeStack();
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
struct StackBounds {
|
|
|
|
uptr bottom;
|
|
|
|
uptr top;
|
|
|
|
};
|
|
|
|
StackBounds GetStackBounds() const;
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
AsanThreadContext *context_;
|
2012-11-12 16:53:47 +01:00
|
|
|
thread_callback_t start_routine_;
|
|
|
|
void *arg_;
|
2016-11-08 23:04:09 +01:00
|
|
|
|
2013-12-05 10:18:38 +01:00
|
|
|
uptr stack_top_;
|
|
|
|
uptr stack_bottom_;
|
2016-11-08 23:04:09 +01:00
|
|
|
// these variables are used when the thread is about to switch stack
|
|
|
|
uptr next_stack_top_;
|
|
|
|
uptr next_stack_bottom_;
|
|
|
|
// true if switching is in progress
|
|
|
|
atomic_uint8_t stack_switching_;
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
uptr tls_begin_;
|
|
|
|
uptr tls_end_;
|
2016-11-08 23:04:09 +01:00
|
|
|
DTLS *dtls_;
|
2012-11-12 16:53:47 +01:00
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
FakeStack *fake_stack_;
|
2012-11-12 16:53:47 +01:00
|
|
|
AsanThreadLocalMallocStorage malloc_storage_;
|
|
|
|
AsanStats stats_;
|
2013-12-05 10:18:38 +01:00
|
|
|
bool unwinding_;
|
2019-08-14 10:47:11 +02:00
|
|
|
uptr extra_spill_area_;
|
2013-11-04 22:33:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Returns a single instance of registry.
|
|
|
|
ThreadRegistry &asanThreadRegistry();
|
|
|
|
|
|
|
|
// Must be called under ThreadRegistryLock.
|
|
|
|
AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
|
|
|
|
|
|
|
|
// Get the current thread. May return 0.
|
|
|
|
AsanThread *GetCurrentThread();
|
|
|
|
void SetCurrentThread(AsanThread *t);
|
|
|
|
u32 GetCurrentTidOrInvalid();
|
|
|
|
AsanThread *FindThreadByStackAddress(uptr addr);
|
|
|
|
|
|
|
|
// Used to handle fork().
|
|
|
|
void EnsureMainThreadIDIsCorrect();
|
2015-10-21 09:32:45 +02:00
|
|
|
} // namespace __asan
|
2012-11-12 16:53:47 +01:00
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
#endif // ASAN_THREAD_H
|