[asan] Fix deadlock in stack unwinder on android/x86.

Fixes PR17116.
Patch by 林作健 (manjian2006 at gmail.com).

llvm-svn: 190590
This commit is contained in:
Evgeniy Stepanov 2013-09-12 08:16:28 +00:00
parent e8f961e847
commit 58dbe06230
3 changed files with 36 additions and 16 deletions

View File

@ -29,21 +29,21 @@ void PrintStack(StackTrace *stack);
// The pc will be in the position 0 of the resulting stack trace.
// The bp may refer to the current frame or to the caller's frame.
#if SANITIZER_WINDOWS
#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast) \
StackTrace stack; \
#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast) \
StackTrace stack; \
GetStackTrace(&stack, max_s, pc, bp, 0, 0, fast)
#else
#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast) \
StackTrace stack; \
{ \
uptr stack_top = 0, stack_bottom = 0; \
AsanThread *t; \
if (asan_inited && (t = GetCurrentThread())) { \
stack_top = t->stack_top(); \
stack_bottom = t->stack_bottom(); \
} \
GetStackTrace(&stack, max_s, pc, bp, \
stack_top, stack_bottom, fast); \
#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast) \
StackTrace stack; \
{ \
AsanThread *t; \
stack.size = 0; \
if (asan_inited && (t = GetCurrentThread()) && !t->isUnwinding()) { \
uptr stack_top = t->stack_top(); \
uptr stack_bottom = t->stack_bottom(); \
ScopedUnwinding unwind_scope(t); \
GetStackTrace(&stack, max_s, pc, bp, stack_top, stack_bottom, fast); \
} \
}
#endif // SANITIZER_WINDOWS

View File

@ -87,11 +87,17 @@ class AsanThread {
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.
bool isUnwinding() const { return unwinding; }
void setUnwinding(bool b) { unwinding = b; }
AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
AsanStats &stats() { return stats_; }
private:
AsanThread() {}
AsanThread() : unwinding(false) {}
void SetThreadStackAndTls();
void ClearShadowForThreadStackAndTLS();
AsanThreadContext *context_;
@ -105,6 +111,19 @@ class AsanThread {
FakeStack *fake_stack_;
AsanThreadLocalMallocStorage malloc_storage_;
AsanStats stats_;
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;
};
struct CreateThreadContextArgs {

View File

@ -114,8 +114,9 @@ static void ProcessPlatformSpecificAllocationsCb(uptr chunk, void *arg) {
chunk = GetUserBegin(chunk);
LsanMetadata m(chunk);
if (m.allocated() && m.tag() != kReachable) {
if (linker->containsAddress(
GetCallerPC(m.stack_trace_id(), param->stack_depot_reverse_map))) {
u32 stack_id = m.stack_trace_id();
if (!stack_id || linker->containsAddress(GetCallerPC(
stack_id, param->stack_depot_reverse_map))) {
m.set_tag(kReachable);
param->frontier->push_back(chunk);
}