2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_thread.h -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
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
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// ASan-private header for asan_thread.cc.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-01 08:22:21 +08:00
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
#ifndef ASAN_THREAD_H
|
|
|
|
#define ASAN_THREAD_H
|
|
|
|
|
|
|
|
#include "asan_allocator.h"
|
|
|
|
#include "asan_internal.h"
|
2013-04-11 19:39:19 +08:00
|
|
|
#include "asan_fake_stack.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_stats.h"
|
2013-10-14 20:01:05 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2012-07-17 15:20:13 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2013-03-21 19:23:41 +08:00
|
|
|
#include "sanitizer_common/sanitizer_thread_registry.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2016-01-15 02:50:09 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
struct DTLS;
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
namespace __asan {
|
|
|
|
|
2012-06-06 23:06:58 +08:00
|
|
|
const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
|
2013-03-21 19:23:41 +08:00
|
|
|
const u32 kMaxNumberOfThreads = (1 << 22); // 4M
|
2012-06-06 23:06:58 +08:00
|
|
|
|
2011-11-30 09:07:02 +08: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.
|
2013-03-21 19:23:41 +08:00
|
|
|
class AsanThreadContext : public ThreadContextBase {
|
2011-11-30 09:07:02 +08:00
|
|
|
public:
|
2013-03-21 19:23:41 +08:00
|
|
|
explicit AsanThreadContext(int tid)
|
2015-06-30 04:28:55 +08:00
|
|
|
: ThreadContextBase(tid), announced(false),
|
|
|
|
destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
|
2015-10-01 08:22:21 +08:00
|
|
|
thread(nullptr) {}
|
2013-03-21 19:23:41 +08:00
|
|
|
bool announced;
|
2013-10-18 22:50:44 +08:00
|
|
|
u8 destructor_iterations;
|
|
|
|
u32 stack_id;
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThread *thread;
|
2012-02-07 08:27:15 +08:00
|
|
|
|
2015-04-11 10:44:24 +08:00
|
|
|
void OnCreated(void *arg) override;
|
|
|
|
void OnFinished() override;
|
2017-08-09 08:38:57 +08:00
|
|
|
|
|
|
|
struct CreateThreadContextArgs {
|
|
|
|
AsanThread *thread;
|
|
|
|
StackTrace *stack;
|
|
|
|
};
|
2011-11-30 09:07:02 +08:00
|
|
|
};
|
|
|
|
|
2013-03-21 19:23:41 +08:00
|
|
|
// AsanThreadContext objects are never freed, so we need many of them.
|
2013-10-18 22:50:44 +08:00
|
|
|
COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
|
2012-12-07 23:15:01 +08:00
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// AsanThread are stored in TSD and destroyed when the thread dies.
|
|
|
|
class AsanThread {
|
|
|
|
public:
|
2014-12-06 01:31:13 +08:00
|
|
|
static AsanThread *Create(thread_callback_t start_routine, void *arg,
|
|
|
|
u32 parent_tid, StackTrace *stack, bool detached);
|
2013-03-21 19:23:41 +08:00
|
|
|
static void TSDDtor(void *tsd);
|
2012-01-07 03:44:11 +08:00
|
|
|
void Destroy();
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2017-08-09 08:38:57 +08:00
|
|
|
struct InitOptions;
|
|
|
|
void Init(const InitOptions *options = nullptr);
|
|
|
|
|
2017-04-18 02:17:38 +08:00
|
|
|
thread_return_t ThreadStart(tid_t os_id,
|
2014-12-05 08:10:15 +08:00
|
|
|
atomic_uintptr_t *signal_thread_is_registered);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2016-06-21 20:29:18 +08:00
|
|
|
uptr stack_top();
|
|
|
|
uptr stack_bottom();
|
|
|
|
uptr stack_size();
|
2013-05-29 21:09:44 +08:00
|
|
|
uptr tls_begin() { return tls_begin_; }
|
|
|
|
uptr tls_end() { return tls_end_; }
|
2016-01-15 02:50:09 +08:00
|
|
|
DTLS *dtls() { return dtls_; }
|
2013-03-21 19:23:41 +08:00
|
|
|
u32 tid() { return context_->tid; }
|
|
|
|
AsanThreadContext *context() { return context_; }
|
|
|
|
void set_context(AsanThreadContext *context) { context_ = context; }
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2014-10-02 05:13:00 +08:00
|
|
|
struct StackFrameAccess {
|
|
|
|
uptr offset;
|
|
|
|
uptr frame_pc;
|
|
|
|
const char *frame_descr;
|
|
|
|
};
|
|
|
|
bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2017-12-05 02:00:24 +08:00
|
|
|
// Returns a pointer to the start of the stack variable's shadow memory.
|
|
|
|
uptr GetStackVariableShadowStart(uptr addr);
|
|
|
|
|
2016-06-21 20:29:18 +08:00
|
|
|
bool AddrIsInStack(uptr addr);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2013-12-11 21:54:01 +08:00
|
|
|
void DeleteFakeStack(int tid) {
|
2013-06-26 20:16:05 +08:00
|
|
|
if (!fake_stack_) return;
|
2013-09-13 14:32:26 +08:00
|
|
|
FakeStack *t = fake_stack_;
|
2015-10-01 08:22:21 +08:00
|
|
|
fake_stack_ = nullptr;
|
|
|
|
SetTLSFakeStack(nullptr);
|
2013-12-11 21:54:01 +08:00
|
|
|
t->Destroy(tid);
|
2013-09-12 15:11:58 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 20:29:18 +08:00
|
|
|
void StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size);
|
2016-09-28 20:28:16 +08:00
|
|
|
void FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old,
|
|
|
|
uptr *size_old);
|
2016-06-21 20:29:18 +08:00
|
|
|
|
2013-09-12 16:43:44 +08:00
|
|
|
bool has_fake_stack() {
|
2016-06-21 20:29:18 +08:00
|
|
|
return !atomic_load(&stack_switching_, memory_order_relaxed) &&
|
|
|
|
(reinterpret_cast<uptr>(fake_stack_) > 1);
|
2013-09-12 16:43:44 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
FakeStack *fake_stack() {
|
2013-09-18 18:35:12 +08:00
|
|
|
if (!__asan_option_detect_stack_use_after_return)
|
2015-10-01 08:22:21 +08:00
|
|
|
return nullptr;
|
2016-06-21 20:29:18 +08:00
|
|
|
if (atomic_load(&stack_switching_, memory_order_relaxed))
|
|
|
|
return nullptr;
|
2018-05-17 07:23:56 +08:00
|
|
|
if (!has_fake_stack())
|
|
|
|
return AsyncSignalSafeLazyInitFakeStack();
|
2013-09-12 15:11:58 +08:00
|
|
|
return fake_stack_;
|
2013-06-26 20:16:05 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 16:16:28 +08:00
|
|
|
// 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-11-27 21:22:21 +08:00
|
|
|
bool isUnwinding() const { return unwinding_; }
|
|
|
|
void setUnwinding(bool b) { unwinding_ = b; }
|
2013-09-12 16:16:28 +08:00
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
|
|
|
|
AsanStats &stats() { return stats_; }
|
|
|
|
|
|
|
|
private:
|
2013-11-27 21:22:21 +08:00
|
|
|
// NOTE: There is no AsanThread constructor. It is allocated
|
|
|
|
// via mmap() and *must* be valid in zero-initialized state.
|
2017-08-09 08:38:57 +08:00
|
|
|
|
|
|
|
void SetThreadStackAndTls(const InitOptions *options);
|
|
|
|
|
2013-05-29 21:09:44 +08:00
|
|
|
void ClearShadowForThreadStackAndTLS();
|
2013-09-12 16:34:50 +08:00
|
|
|
FakeStack *AsyncSignalSafeLazyInitFakeStack();
|
|
|
|
|
2016-06-21 20:29:18 +08:00
|
|
|
struct StackBounds {
|
|
|
|
uptr bottom;
|
|
|
|
uptr top;
|
|
|
|
};
|
|
|
|
StackBounds GetStackBounds() const;
|
|
|
|
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThreadContext *context_;
|
2012-02-24 23:28:43 +08:00
|
|
|
thread_callback_t start_routine_;
|
2011-11-30 09:07:02 +08:00
|
|
|
void *arg_;
|
2016-06-21 20:29:18 +08:00
|
|
|
|
2013-11-27 21:22:21 +08:00
|
|
|
uptr stack_top_;
|
|
|
|
uptr stack_bottom_;
|
2016-06-21 20:29:18 +08: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-05-29 21:09:44 +08:00
|
|
|
uptr tls_begin_;
|
|
|
|
uptr tls_end_;
|
2016-01-15 02:50:09 +08:00
|
|
|
DTLS *dtls_;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2013-06-26 20:16:05 +08:00
|
|
|
FakeStack *fake_stack_;
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanThreadLocalMallocStorage malloc_storage_;
|
|
|
|
AsanStats stats_;
|
2013-11-27 21:22:21 +08:00
|
|
|
bool unwinding_;
|
2013-09-12 16:16:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// ScopedUnwinding is a scope for stacktracing member of a context
|
|
|
|
class ScopedUnwinding {
|
|
|
|
public:
|
|
|
|
explicit ScopedUnwinding(AsanThread *t) : thread(t) {
|
|
|
|
t->setUnwinding(true);
|
|
|
|
}
|
|
|
|
~ScopedUnwinding() { thread->setUnwinding(false); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
AsanThread *thread;
|
2011-11-30 09:07:02 +08:00
|
|
|
};
|
|
|
|
|
2013-03-21 19:23:41 +08:00
|
|
|
// Returns a single instance of registry.
|
|
|
|
ThreadRegistry &asanThreadRegistry();
|
|
|
|
|
|
|
|
// Must be called under ThreadRegistryLock.
|
|
|
|
AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
|
|
|
|
|
2013-03-20 17:23:28 +08:00
|
|
|
// Get the current thread. May return 0.
|
|
|
|
AsanThread *GetCurrentThread();
|
|
|
|
void SetCurrentThread(AsanThread *t);
|
|
|
|
u32 GetCurrentTidOrInvalid();
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThread *FindThreadByStackAddress(uptr addr);
|
2013-03-20 17:23:28 +08:00
|
|
|
|
2013-07-08 20:57:24 +08:00
|
|
|
// Used to handle fork().
|
|
|
|
void EnsureMainThreadIDIsCorrect();
|
2015-10-01 08:22:21 +08:00
|
|
|
} // namespace __asan
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
#endif // ASAN_THREAD_H
|