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"
|
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),
|
|
|
|
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;
|
|
|
|
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_; }
|
|
|
|
uptr stack_size() { return stack_top_ - stack_bottom_; }
|
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_;
|
|
|
|
}
|
|
|
|
|
|
|
|
FakeStack &fake_stack() { return fake_stack_; }
|
|
|
|
AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
|
|
|
|
AsanStats &stats() { return stats_; }
|
|
|
|
|
|
|
|
private:
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThread() {}
|
2011-11-30 09:07:02 +08:00
|
|
|
void SetThreadStackTopAndBottom();
|
|
|
|
void ClearShadowForThreadStack();
|
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_;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
FakeStack fake_stack_;
|
|
|
|
AsanThreadLocalMallocStorage malloc_storage_;
|
|
|
|
AsanStats stats_;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#endif // ASAN_THREAD_H
|