2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_thread.h -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// ASan-private header for asan_thread.cc.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#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_stack.h"
|
|
|
|
#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
|
|
|
|
|
|
|
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)
|
|
|
|
: ThreadContextBase(tid),
|
|
|
|
announced(false),
|
2013-10-14 20:01:05 +08:00
|
|
|
destructor_iterations(kPthreadDestructorIterations),
|
2013-03-21 19:23:41 +08:00
|
|
|
thread(0) {
|
|
|
|
internal_memset(&stack, 0, sizeof(stack));
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2013-03-21 19:23:41 +08:00
|
|
|
bool announced;
|
2013-10-14 20:01:05 +08:00
|
|
|
int destructor_iterations;
|
2013-03-21 19:23:41 +08:00
|
|
|
StackTrace stack;
|
|
|
|
AsanThread *thread;
|
2012-02-07 08:27:15 +08:00
|
|
|
|
2013-03-21 19:23:41 +08:00
|
|
|
void OnCreated(void *arg);
|
|
|
|
void OnFinished();
|
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.
|
|
|
|
COMPILER_CHECK(sizeof(AsanThreadContext) <= 4096);
|
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:
|
2013-03-21 19:23:41 +08:00
|
|
|
static AsanThread *Create(thread_callback_t start_routine, void *arg);
|
|
|
|
static void TSDDtor(void *tsd);
|
2012-01-07 03:44:11 +08:00
|
|
|
void Destroy();
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2011-12-17 03:13:35 +08:00
|
|
|
void Init(); // Should be called from the thread itself.
|
2013-03-21 19:23:41 +08:00
|
|
|
thread_return_t ThreadStart(uptr os_id);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr stack_top() { return stack_top_; }
|
|
|
|
uptr stack_bottom() { return stack_bottom_; }
|
2013-09-19 22:59:52 +08:00
|
|
|
uptr stack_size() { return stack_size_; }
|
2013-05-29 21:09:44 +08:00
|
|
|
uptr tls_begin() { return tls_begin_; }
|
|
|
|
uptr tls_end() { return tls_end_; }
|
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
|
|
|
|
[asan] Change the way we report the alloca frame on stack-buff-overflow.
Before: the function name was stored by the compiler as a constant string
and the run-time was printing it.
Now: the PC is stored instead and the run-time prints the full symbolized frame.
This adds a couple of instructions into every function with non-empty stack frame,
but also reduces the binary size because we store less strings (I saw 2% size reduction).
This change bumps the asan ABI version to v3.
compiler-rt part, llvm part will follow.
Example of report (now):
==31711==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffa77cf1c5 at pc 0x41feb0 bp 0x7fffa77cefb0 sp 0x7fffa77cefa8
READ of size 1 at 0x7fffa77cf1c5 thread T0
#0 0x41feaf in Frame0(int, char*, char*, char*) stack-oob-frames.cc:20
#1 0x41f7ff in Frame1(int, char*, char*) stack-oob-frames.cc:24
#2 0x41f477 in Frame2(int, char*) stack-oob-frames.cc:28
#3 0x41f194 in Frame3(int) stack-oob-frames.cc:32
#4 0x41eee0 in main stack-oob-frames.cc:38
#5 0x7f0c5566f76c (/lib/x86_64-linux-gnu/libc.so.6+0x2176c)
#6 0x41eb1c (/usr/local/google/kcc/llvm_cmake/a.out+0x41eb1c)
Address 0x7fffa77cf1c5 is located in stack of thread T0 at offset 293 in frame
#0 0x41f87f in Frame0(int, char*, char*, char*) stack-oob-frames.cc:12 <<<<<<<<<<<<<< this is new
This frame has 6 object(s):
[32, 36) 'frame.addr'
[96, 104) 'a.addr'
[160, 168) 'b.addr'
[224, 232) 'c.addr'
[288, 292) 's'
[352, 360) 'd'
llvm-svn: 177723
2013-03-22 18:36:24 +08:00
|
|
|
const char *GetFrameNameByAddr(uptr addr, uptr *offset, uptr *frame_pc);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
bool AddrIsInStack(uptr addr) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return addr >= stack_bottom_ && addr < stack_top_;
|
|
|
|
}
|
|
|
|
|
2013-06-26 20:16:05 +08:00
|
|
|
void DeleteFakeStack() {
|
|
|
|
if (!fake_stack_) return;
|
2013-09-13 14:32:26 +08:00
|
|
|
FakeStack *t = fake_stack_;
|
|
|
|
fake_stack_ = 0;
|
|
|
|
SetTLSFakeStack(0);
|
|
|
|
t->Destroy();
|
2013-09-12 15:11:58 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 16:43:44 +08:00
|
|
|
bool has_fake_stack() {
|
|
|
|
return (reinterpret_cast<uptr>(fake_stack_) > 1);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return 0;
|
2013-09-12 16:43:44 +08:00
|
|
|
if (!has_fake_stack())
|
2013-09-12 16:34:50 +08:00
|
|
|
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.
|
|
|
|
bool isUnwinding() const { return unwinding; }
|
|
|
|
void setUnwinding(bool b) { unwinding = b; }
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
|
|
|
|
AsanStats &stats() { return stats_; }
|
|
|
|
|
|
|
|
private:
|
2013-09-12 16:16:28 +08:00
|
|
|
AsanThread() : unwinding(false) {}
|
2013-05-29 21:09:44 +08:00
|
|
|
void SetThreadStackAndTls();
|
|
|
|
void ClearShadowForThreadStackAndTLS();
|
2013-09-12 16:34:50 +08:00
|
|
|
FakeStack *AsyncSignalSafeLazyInitFakeStack();
|
|
|
|
|
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_;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr stack_top_;
|
|
|
|
uptr stack_bottom_;
|
2013-09-19 22:59:52 +08:00
|
|
|
// stack_size_ == stack_top_ - stack_bottom_;
|
|
|
|
// It needs to be set in a async-signal-safe manner.
|
|
|
|
uptr stack_size_;
|
2013-05-29 21:09:44 +08:00
|
|
|
uptr tls_begin_;
|
|
|
|
uptr tls_end_;
|
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-09-12 16:16:28 +08:00
|
|
|
bool unwinding;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
struct CreateThreadContextArgs {
|
|
|
|
AsanThread *thread;
|
|
|
|
StackTrace *stack;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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();
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#endif // ASAN_THREAD_H
|