<br><br><div class="gmail_quote">On Thu, Aug 30, 2012 at 5:02 PM, Dmitry Vyukov <span dir="ltr"><<a href="mailto:dvyukov@google.com" target="_blank">dvyukov@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: dvyukov<br>
Date: Thu Aug 30 08:02:30 2012<br>
New Revision: 162902<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=162902&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=162902&view=rev</a><br>
Log:<br>
tsan: use stack depot to describe heap blocks<br>
<br>
Added:<br>
compiler-rt/trunk/lib/tsan/output_tests/race_on_heap.cc<br>
Modified:<br>
compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h<br>
compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc<br>
compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.h<br>
compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cc<br>
compiler-rt/trunk/lib/tsan/output_tests/race_on_mutex.c<br>
compiler-rt/trunk/lib/tsan/output_tests/simple_stack.c<br>
compiler-rt/trunk/lib/tsan/output_tests/simple_stack2.cc<br>
compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc<br>
compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.cc<br>
compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.h<br>
compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc<br>
compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc<br>
compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h<br>
compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc<br>
compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h Thu Aug 30 08:02:30 2012<br>
@@ -176,7 +176,16 @@<br>
return (reinterpret_cast<uptr>(p) / kRegionSize) % kNumClasses;<br>
}<br>
<br>
- uptr GetActuallyAllocatedSize(void *p) {<br>
+ static void *GetBlockBegin(void *p) {<br>
+ uptr u = (uptr)p;<br>
+ uptr s = GetActuallyAllocatedSize(p);<br>
+ uptr regBeg = u & ~(kRegionSize - 1);<br>
+ uptr regOff = u - regBeg;<br>
+ uptr begin = regBeg + regOff / s * s;<br></blockquote><div><br></div><div><br></div><div>Why you don't use GetChunkIdx()? </div><div>In your implementation you have 64-bit division, while GetChunkIdx uses faster 32-bit division</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ return (void*)begin;<br>
+ }<br>
+<br>
+ static uptr GetActuallyAllocatedSize(void *p) {<br>
CHECK(PointerIsMine(p));<br>
return SizeClassMap::Size(GetSizeClass(p));<br>
}<br>
@@ -415,6 +424,16 @@<br>
return GetHeader(p) + 1;<br>
}<br>
<br>
+ void *GetBlockBegin(void *p) {<br>
+ SpinMutexLock l(&mutex_);<br>
+ for (Header *l = list_; l; l = l->next) {<br>
+ void *b = GetUser(l);<br>
+ if (p >= b && p < (u8*)b + l->size)<br>
+ return b;<br>
+ }<br>
+ return 0;<br>
+ }<br>
+<br>
private:<br>
struct Header {<br>
uptr size;<br>
@@ -512,6 +531,12 @@<br>
return secondary_.GetMetaData(p);<br>
}<br>
<br>
+ void *GetBlockBegin(void *p) {<br>
+ if (primary_.PointerIsMine(p))<br>
+ return primary_.GetBlockBegin(p);<br>
+ return secondary_.GetBlockBegin(p);<br>
+ }<br>
+<br>
uptr GetActuallyAllocatedSize(void *p) {<br>
if (primary_.PointerIsMine(p))<br>
return primary_.GetActuallyAllocatedSize(p);<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.cc Thu Aug 30 08:02:30 2012<br>
@@ -33,7 +33,7 @@<br>
u32 seq;<br>
} depot;<br>
<br>
-static uptr hash(uptr *stack, uptr size) {<br>
+static uptr hash(const uptr *stack, uptr size) {<br>
return 0;<br>
}<br>
<br>
@@ -51,7 +51,7 @@<br>
return s;<br>
}<br>
<br>
-u32 StackDepotPut(uptr *stack, uptr size) {<br>
+u32 StackDepotPut(const uptr *stack, uptr size) {<br>
if (stack == 0 || size == 0)<br>
return 0;<br>
uptr h = hash(stack, size);<br>
@@ -71,7 +71,7 @@<br>
return s->id;<br>
}<br>
<br>
-uptr *StackDepotGet(u32 id, uptr *size) {<br>
+const uptr *StackDepotGet(u32 id, uptr *size) {<br>
if (id == 0)<br>
return 0;<br>
SpinMutexLock l(&depot.mtx);<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.h?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.h?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.h (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_stackdepot.h Thu Aug 30 08:02:30 2012<br>
@@ -20,9 +20,9 @@<br>
// StackDepot efficiently stores huge amounts of stack traces.<br>
<br>
// Maps stack trace to an unique id.<br>
-u32 StackDepotPut(uptr *stack, uptr size);<br>
+u32 StackDepotPut(const uptr *stack, uptr size);<br>
// Retrieves a stored stack trace by the id.<br>
-uptr *StackDepotGet(u32 id, uptr *size);<br>
+const uptr *StackDepotGet(u32 id, uptr *size);<br>
<br>
} // namespace __sanitizer<br>
<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cc?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cc?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cc Thu Aug 30 08:02:30 2012<br>
@@ -21,7 +21,7 @@<br>
uptr s1[] = {1, 2, 3, 4, 5};<br>
u32 i1 = StackDepotPut(s1, ARRAY_SIZE(s1));<br>
uptr sz1 = 0;<br>
- uptr *sp1 = StackDepotGet(i1, &sz1);<br>
+ const uptr *sp1 = StackDepotGet(i1, &sz1);<br>
EXPECT_NE(sp1, (uptr*)0);<br>
EXPECT_EQ(sz1, ARRAY_SIZE(s1));<br>
EXPECT_EQ(internal_memcmp(sp1?:s1, s1, sizeof(s1)), 0);<br>
@@ -29,14 +29,20 @@<br>
<br>
TEST(SanitizerCommon, StackDepotAbsent) {<br>
uptr sz1 = 0;<br>
- uptr *sp1 = StackDepotGet(-10, &sz1);<br>
+ const uptr *sp1 = StackDepotGet(-10, &sz1);<br>
EXPECT_EQ(sp1, (uptr*)0);<br>
}<br>
<br>
-TEST(SanitizerCommon, StackDepotZero) {<br>
+TEST(SanitizerCommon, StackDepotEmptyStack) {<br>
u32 i1 = StackDepotPut(0, 0);<br>
uptr sz1 = 0;<br>
- uptr *sp1 = StackDepotGet(i1, &sz1);<br>
+ const uptr *sp1 = StackDepotGet(i1, &sz1);<br>
+ EXPECT_EQ(sp1, (uptr*)0);<br>
+}<br>
+<br>
+TEST(SanitizerCommon, StackDepotZeroId) {<br>
+ uptr sz1 = 0;<br>
+ const uptr *sp1 = StackDepotGet(0, &sz1);<br>
EXPECT_EQ(sp1, (uptr*)0);<br>
}<br>
<br>
@@ -46,7 +52,7 @@<br>
u32 i2 = StackDepotPut(s1, ARRAY_SIZE(s1));<br>
EXPECT_EQ(i1, i2);<br>
uptr sz1 = 0;<br>
- uptr *sp1 = StackDepotGet(i1, &sz1);<br>
+ const uptr *sp1 = StackDepotGet(i1, &sz1);<br>
EXPECT_NE(sp1, (uptr*)0);<br>
EXPECT_EQ(sz1, ARRAY_SIZE(s1));<br>
EXPECT_EQ(internal_memcmp(sp1?:s1, s1, sizeof(s1)), 0);<br>
<br>
Added: compiler-rt/trunk/lib/tsan/output_tests/race_on_heap.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/output_tests/race_on_heap.cc?rev=162902&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/output_tests/race_on_heap.cc?rev=162902&view=auto</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/output_tests/race_on_heap.cc (added)<br>
+++ compiler-rt/trunk/lib/tsan/output_tests/race_on_heap.cc Thu Aug 30 08:02:30 2012<br>
@@ -0,0 +1,46 @@<br>
+#include <pthread.h><br>
+#include <stdlib.h><br>
+#include <stdio.h><br>
+<br>
+void *Thread1(void *p) {<br>
+ *(int*)p = 42;<br>
+ return 0;<br>
+}<br>
+<br>
+void *Thread2(void *p) {<br>
+ *(int*)p = 44;;<br>
+ return 0;<br>
+}<br>
+<br>
+void *alloc() {<br>
+ return malloc(99);<br>
+}<br>
+<br>
+void *AllocThread(void*) {<br>
+ return alloc();<br>
+}<br>
+<br>
+int main() {<br>
+ void *p = 0;<br>
+ pthread_t t[2];<br>
+ pthread_create(&t[0], 0, AllocThread, 0);<br>
+ pthread_join(t[0], &p);<br>
+ fprintf(stderr, "addr=%p\n", p);<br>
+ pthread_create(&t[0], 0, Thread1, (char*)p + 16);<br>
+ pthread_create(&t[1], 0, Thread2, (char*)p + 16);<br>
+ pthread_join(t[0], 0);<br>
+ pthread_join(t[1], 0);<br>
+ return 0;<br>
+}<br>
+<br>
+// CHECK: addr=[[ADDR:0x[0-9,a-f]+]]<br>
+// CHECK: WARNING: ThreadSanitizer: data race<br>
+//...<br>
+// CHECK: Location is heap block of size 99 at [[ADDR]] allocated by thread 1:<br>
+// CHECK: #0 alloc<br>
+// CHECK: #1 AllocThread<br>
+//...<br>
+// CHECK: Thread 1 (finished) created at:<br>
+// CHECK: #0 pthread_create<br>
+// CHECK: #1 main<br>
+<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/output_tests/race_on_mutex.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/output_tests/race_on_mutex.c?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/output_tests/race_on_mutex.c?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/output_tests/race_on_mutex.c (original)<br>
+++ compiler-rt/trunk/lib/tsan/output_tests/race_on_mutex.c Thu Aug 30 08:02:30 2012<br>
@@ -34,8 +34,8 @@<br>
<br>
// CHECK: WARNING: ThreadSanitizer: data race<br>
// CHECK-NEXT: Read of size 1 at {{.*}} by thread 2:<br>
-// CHECK-NEXT: #0 pthread_mutex_lock {{.*}} ({{.*}})<br>
-// CHECK-NEXT: #1 Thread2 {{.*}}race_on_mutex.c:19{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: Previous write of size 1 at {{.*}} by thread 1:<br>
+// CHECK-NEXT: #0 pthread_mutex_lock<br>
+// CHECK-NEXT: #1 Thread2{{.*}} {{.*}}race_on_mutex.c:19{{(:3)?}} ({{.*}})<br>
+// CHECK: Previous write of size 1 at {{.*}} by thread 1:<br>
// CHECK-NEXT: #0 pthread_mutex_init {{.*}} ({{.*}})<br>
-// CHECK-NEXT: #1 Thread1 {{.*}}race_on_mutex.c:10{{(:3)?}} ({{.*}})<br>
+// CHECK-NEXT: #1 Thread1{{.*}} {{.*}}race_on_mutex.c:10{{(:3)?}} ({{.*}})<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/output_tests/simple_stack.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/output_tests/simple_stack.c?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/output_tests/simple_stack.c?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/output_tests/simple_stack.c (original)<br>
+++ compiler-rt/trunk/lib/tsan/output_tests/simple_stack.c Thu Aug 30 08:02:30 2012<br>
@@ -48,18 +48,18 @@<br>
<br>
// CHECK: WARNING: ThreadSanitizer: data race<br>
// CHECK-NEXT: Write of size 4 at {{.*}} by thread 1:<br>
-// CHECK-NEXT: #0 foo1 {{.*}}simple_stack.c:8{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: #1 bar1 {{.*}}simple_stack.c:13{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: #2 Thread1 {{.*}}simple_stack.c:27{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: Previous read of size 4 at {{.*}} by thread 2:<br>
-// CHECK-NEXT: #0 foo2 {{.*}}simple_stack.c:17{{(:26)?}} ({{.*}})<br>
-// CHECK-NEXT: #1 bar2 {{.*}}simple_stack.c:22{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: #2 Thread2 {{.*}}simple_stack.c:32{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: Thread 1 (running) created at:<br>
+// CHECK-NEXT: #0 foo1{{.*}} {{.*}}simple_stack.c:8{{(:3)?}} ({{.*}})<br>
+// CHECK-NEXT: #1 bar1{{.*}} {{.*}}simple_stack.c:13{{(:3)?}} ({{.*}})<br>
+// CHECK-NEXT: #2 Thread1{{.*}} {{.*}}simple_stack.c:27{{(:3)?}} ({{.*}})<br>
+// CHECK: Previous read of size 4 at {{.*}} by thread 2:<br>
+// CHECK-NEXT: #0 foo2{{.*}} {{.*}}simple_stack.c:17{{(:26)?}} ({{.*}})<br>
+// CHECK-NEXT: #1 bar2{{.*}} {{.*}}simple_stack.c:22{{(:3)?}} ({{.*}})<br>
+// CHECK-NEXT: #2 Thread2{{.*}} {{.*}}simple_stack.c:32{{(:3)?}} ({{.*}})<br>
+// CHECK: Thread 1 (running) created at:<br>
// CHECK-NEXT: #0 pthread_create {{.*}} ({{.*}})<br>
-// CHECK-NEXT: #1 StartThread {{.*}}simple_stack.c:37{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: #2 main {{.*}}simple_stack.c:42{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: Thread 2 ({{.*}}) created at:<br>
+// CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:37{{(:3)?}} ({{.*}})<br>
+// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack.c:42{{(:3)?}} ({{.*}})<br>
+// CHECK: Thread 2 ({{.*}}) created at:<br>
// CHECK-NEXT: #0 pthread_create {{.*}} ({{.*}})<br>
-// CHECK-NEXT: #1 StartThread {{.*}}simple_stack.c:37{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: #2 main {{.*}}simple_stack.c:43{{(:3)?}} ({{.*}})<br>
+// CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:37{{(:3)?}} ({{.*}})<br>
+// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack.c:43{{(:3)?}} ({{.*}})<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/output_tests/simple_stack2.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/output_tests/simple_stack2.cc?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/output_tests/simple_stack2.cc?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/output_tests/simple_stack2.cc (original)<br>
+++ compiler-rt/trunk/lib/tsan/output_tests/simple_stack2.cc Thu Aug 30 08:02:30 2012<br>
@@ -40,7 +40,7 @@<br>
// CHECK-NEXT: #0 foo1{{.*}} {{.*}}simple_stack2.cc:8{{(:3)?}} ({{.*}})<br>
// CHECK-NEXT: #1 bar1{{.*}} {{.*}}simple_stack2.cc:13{{(:3)?}} ({{.*}})<br>
// CHECK-NEXT: #2 Thread1{{.*}} {{.*}}simple_stack2.cc:27{{(:3)?}} ({{.*}})<br>
-// CHECK-NEXT: Previous read of size 4 at {{.*}} by main thread:<br>
+// CHECK: Previous read of size 4 at {{.*}} by main thread:<br>
// CHECK-NEXT: #0 foo2{{.*}} {{.*}}simple_stack2.cc:17{{(:28)?}} ({{.*}})<br>
// CHECK-NEXT: #1 bar2{{.*}} {{.*}}simple_stack2.cc:22{{(:3)?}} ({{.*}})<br>
// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack2.cc:34{{(:3)?}} ({{.*}})<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc (original)<br>
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc Thu Aug 30 08:02:30 2012<br>
@@ -12,6 +12,7 @@<br>
//===----------------------------------------------------------------------===//<br>
#include "sanitizer_common/sanitizer_common.h"<br>
#include "sanitizer_common/sanitizer_placement_new.h"<br>
+#include "sanitizer_common/sanitizer_stackdepot.h"<br>
#include "tsan_mman.h"<br>
#include "tsan_rtl.h"<br>
#include "tsan_report.h"<br>
@@ -19,8 +20,8 @@<br>
<br>
namespace __tsan {<br>
<br>
-extern char allocator_placeholder[];<br>
-INLINE Allocator *allocator() {<br>
+static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);<br>
+Allocator *allocator() {<br>
return reinterpret_cast<Allocator*>(&allocator_placeholder);<br>
}<br>
<br>
@@ -49,6 +50,11 @@<br>
return 0;<br>
MBlock *b = (MBlock*)allocator()->GetMetaData(p);<br>
b->size = sz;<br>
+ b->alloc_tid = thr->unique_id;<br>
+ b->alloc_stack_id = 0;<br>
+ if (thr->shadow_stack_pos) // May happen during bootstrap.<br>
+ b->alloc_stack_id = StackDepotPut(thr->shadow_stack,<br>
+ thr->shadow_stack_pos - thr->shadow_stack);<br>
if (CTX() && CTX()->initialized) {<br>
MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);<br>
}<br>
@@ -102,7 +108,7 @@<br>
}<br>
<br>
MBlock *user_mblock(ThreadState *thr, void *p) {<br>
- CHECK_GT(thr->in_rtl, 0);<br>
+ // CHECK_GT(thr->in_rtl, 0);<br>
CHECK_NE(p, (void*)0);<br>
return (MBlock*)allocator()->GetMetaData(p);<br>
}<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.cc?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.cc?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.cc (original)<br>
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.cc Thu Aug 30 08:02:30 2012<br>
@@ -141,7 +141,7 @@<br>
TsanPrintf("ThreadSanitizer: internal deadlock detected\n");<br>
TsanPrintf("ThreadSanitizer: can't lock %d while under %zu\n",<br>
t, (uptr)max_idx);<br>
- Die();<br>
+ CHECK(0);<br>
}<br>
}<br>
<br>
@@ -256,4 +256,8 @@<br>
#endif<br>
}<br>
<br>
+void Mutex::CheckLocked() {<br>
+ CHECK_NE(atomic_load(&state_, memory_order_relaxed), 0);<br>
+}<br>
+<br>
} // namespace __tsan<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.h?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.h?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.h (original)<br>
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.h Thu Aug 30 08:02:30 2012<br>
@@ -45,6 +45,8 @@<br>
void ReadLock();<br>
void ReadUnlock();<br>
<br>
+ void CheckLocked();<br>
+<br>
private:<br>
atomic_uintptr_t state_;<br>
#if TSAN_DEBUG<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc (original)<br>
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc Thu Aug 30 08:02:30 2012<br>
@@ -58,6 +58,7 @@<br>
else<br>
TsanPrintf(" (%p)\n", (void*)ent->pc);<br>
}<br>
+ TsanPrintf("\n");<br>
}<br>
<br>
static void PrintMop(const ReportMop *mop, bool first) {<br>
@@ -77,8 +78,12 @@<br>
TsanPrintf(" Location is global '%s' of size %zu at %zx %s:%d\n",<br>
loc->name, loc->size, loc->addr, loc->file, loc->line);<br>
} else if (loc->type == ReportLocationHeap) {<br>
- TsanPrintf(" Location is heap of size %zu at %zx allocated "<br>
- "by thread %d:\n", loc->size, loc->addr, loc->tid);<br>
+ TsanPrintf(" Location is heap block of size %zu at %p allocated",<br>
+ loc->size, loc->addr);<br>
+ if (loc->tid == 0)<br>
+ TsanPrintf(" by main thread:\n");<br>
+ else<br>
+ TsanPrintf(" by thread %d:\n", loc->tid);<br>
PrintStack(loc->stack);<br>
} else if (loc->type == ReportLocationStack) {<br>
TsanPrintf(" Location is stack of thread %d:\n", loc->tid);<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc (original)<br>
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc Thu Aug 30 08:02:30 2012<br>
@@ -32,7 +32,6 @@<br>
<br>
#ifndef TSAN_GO<br>
THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGNED(64);<br>
-char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);<br>
#endif<br>
static char ctx_placeholder[sizeof(Context)] ALIGNED(64);<br>
<br>
@@ -52,7 +51,7 @@<br>
}<br>
<br>
// The objects are allocated in TLS, so one may rely on zero-initialization.<br>
-ThreadState::ThreadState(Context *ctx, int tid, u64 epoch,<br>
+ThreadState::ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,<br>
uptr stk_addr, uptr stk_size,<br>
uptr tls_addr, uptr tls_size)<br>
: fast_state(tid, epoch)<br>
@@ -63,6 +62,7 @@<br>
// , in_rtl()<br>
, shadow_stack_pos(&shadow_stack[0])<br>
, tid(tid)<br>
+ , unique_id(unique_id)<br>
, stk_addr(stk_addr)<br>
, stk_size(stk_size)<br>
, tls_addr(tls_addr)<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h (original)<br>
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h Thu Aug 30 08:02:30 2012<br>
@@ -42,6 +42,8 @@<br>
struct MBlock {<br>
Mutex mtx;<br>
uptr size;<br>
+ u32 alloc_tid;<br>
+ u32 alloc_stack_id;<br>
SyncVar *head;<br>
};<br>
<br>
@@ -60,6 +62,7 @@<br>
typedef LargeMmapAllocator SecondaryAllocator;<br>
typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,<br>
SecondaryAllocator> Allocator;<br>
+Allocator *allocator();<br>
#endif<br>
<br>
void TsanPrintf(const char *format, ...);<br>
@@ -270,6 +273,7 @@<br>
#endif<br>
u64 stat[StatCnt];<br>
const int tid;<br>
+ const int unique_id;<br>
int in_rtl;<br>
bool is_alive;<br>
const uptr stk_addr;<br>
@@ -286,7 +290,7 @@<br>
// If set, malloc must not be called.<br>
int nomalloc;<br>
<br>
- explicit ThreadState(Context *ctx, int tid, u64 epoch,<br>
+ explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,<br>
uptr stk_addr, uptr stk_size,<br>
uptr tls_addr, uptr tls_size);<br>
};<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc (original)<br>
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc Thu Aug 30 08:02:30 2012<br>
@@ -13,6 +13,7 @@<br>
<br>
#include "sanitizer_common/sanitizer_libc.h"<br>
#include "sanitizer_common/sanitizer_placement_new.h"<br>
+#include "sanitizer_common/sanitizer_stackdepot.h"<br>
#include "tsan_platform.h"<br>
#include "tsan_rtl.h"<br>
#include "tsan_suppressions.h"<br>
@@ -150,6 +151,10 @@<br>
}<br>
<br>
void ScopedReport::AddThread(const ThreadContext *tctx) {<br>
+ for (uptr i = 0; i < rep_->threads.Size(); i++) {<br>
+ if (rep_->threads[i]->id == tctx->tid)<br>
+ return;<br>
+ }<br>
void *mem = internal_alloc(MBlockReportThread, sizeof(ReportThread));<br>
ReportThread *rt = new(mem) ReportThread();<br>
rep_->threads.PushBack(rt);<br>
@@ -158,6 +163,17 @@<br>
rt->stack = SymbolizeStack(tctx->creation_stack);<br>
}<br>
<br>
+static ThreadContext *FindThread(int unique_id) {<br>
+ CTX()->thread_mtx.CheckLocked();<br>
+ for (unsigned i = 0; i < kMaxTid; i++) {<br>
+ ThreadContext *tctx = CTX()->threads[i];<br>
+ if (tctx && tctx->unique_id == unique_id) {<br>
+ return tctx;<br>
+ }<br>
+ }<br>
+ return 0;<br>
+}<br>
+<br>
void ScopedReport::AddMutex(const SyncVar *s) {<br>
void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));<br>
ReportMutex *rm = new(mem) ReportMutex();<br>
@@ -167,6 +183,35 @@<br>
}<br>
<br>
void ScopedReport::AddLocation(uptr addr, uptr size) {<br>
+ if (addr == 0)<br>
+ return;<br>
+#ifndef TSAN_GO<br>
+ if (allocator()->PointerIsMine((void*)addr)) {<br>
+ MBlock *b = user_mblock(0, (void*)addr);<br>
+ ThreadContext *tctx = FindThread(b->alloc_tid);<br>
+ void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));<br>
+ ReportLocation *loc = new(mem) ReportLocation();<br>
+ rep_->locs.PushBack(loc);<br>
+ loc->type = ReportLocationHeap;<br>
+ loc->addr = (uptr)allocator()->GetBlockBegin((void*)addr);<br>
+ loc->size = b->size;<br>
+ loc->tid = tctx ? tctx->tid : b->alloc_tid;<br>
+ loc->name = 0;<br>
+ loc->file = 0;<br>
+ loc->line = 0;<br>
+ loc->stack = 0;<br>
+ uptr ssz = 0;<br>
+ const uptr *stack = StackDepotGet(b->alloc_stack_id, &ssz);<br>
+ if (stack) {<br>
+ StackTrace trace;<br>
+ trace.Init(stack, ssz);<br>
+ loc->stack = SymbolizeStack(trace);<br>
+ }<br>
+ if (tctx)<br>
+ AddThread(tctx);<br>
+ return;<br>
+ }<br>
+#endif<br>
ReportStack *symb = SymbolizeData(addr);<br>
if (symb) {<br>
void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));<br>
@@ -181,6 +226,7 @@<br>
loc->line = symb->line;<br>
loc->stack = 0;<br>
internal_free(symb);<br>
+ return;<br>
}<br>
}<br>
<br>
@@ -353,7 +399,7 @@<br>
}<br>
<br>
// Ensure that we have at least something for the current thread.<br>
- CHECK_EQ(traces[0].IsEmpty(), false);<br>
+ DCHECK_EQ(traces[0].IsEmpty(), false);<br>
<br>
for (uptr i = 0; i < kMop; i++) {<br>
FastState s(thr->racy_state[i]);<br>
@@ -363,6 +409,8 @@<br>
rep.AddThread(tctx);<br>
}<br>
<br>
+ rep.AddLocation(addr_min, addr_max - addr_min);<br>
+<br>
if (!OutputReport(rep, rep.GetReport()->mops[0]->stack))<br>
return;<br>
<br>
<br>
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc?rev=162902&r1=162901&r2=162902&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc?rev=162902&r1=162901&r2=162902&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc (original)<br>
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc Thu Aug 30 08:02:30 2012<br>
@@ -171,8 +171,9 @@<br>
tctx->status = ThreadStatusRunning;<br>
tctx->epoch0 = tctx->epoch1 + 1;<br>
tctx->epoch1 = (u64)-1;<br>
- new(thr) ThreadState(CTX(), tid, tctx->epoch0, stk_addr, stk_size,<br>
- tls_addr, tls_size);<br>
+ new(thr) ThreadState(CTX(), tid, tctx->unique_id,<br>
+ tctx->epoch0, stk_addr, stk_size,<br>
+ tls_addr, tls_size);<br>
#ifdef TSAN_GO<br>
// Setup dynamic shadow stack.<br>
const int kInitStackSize = 8;<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br>