tsan: fix a bug in metamap
authorDmitry Vyukov <dvyukov@google.com>
Tue, 8 Jul 2014 13:28:01 +0000 (13:28 +0000)
committerDmitry Vyukov <dvyukov@google.com>
Tue, 8 Jul 2014 13:28:01 +0000 (13:28 +0000)
The bug happens in the following case:
Mutex is located at heap block beginning,
when we call MutexDestroy, s->next is set to 0,
so free can't find the MBlock related to the block.

llvm-svn: 212531

compiler-rt/lib/tsan/rtl/tsan_sync.cc
compiler-rt/lib/tsan/tests/unit/tsan_sync_test.cc

index 921b1fc..3952b2f 100644 (file)
@@ -46,7 +46,6 @@ void SyncVar::Reset() {
   is_recursive = 0;
   is_broken = 0;
   is_linker_init = 0;
-  next = 0;
 
   clock.Zero();
   read_clock.Reset();
index 664ce7f..6f36c64 100644 (file)
@@ -108,4 +108,16 @@ TEST(MetaMap, MoveMemory) {
   m->FreeRange(thr, 0, (uptr)&block2[0], 4 * sizeof(u64));
 }
 
+TEST(MetaMap, ResetSync) {
+  ThreadState *thr = cur_thread();
+  MetaMap *m = &ctx->metamap;
+  u64 block[1] = {};  // fake malloc block
+  m->AllocBlock(thr, 0, (uptr)&block[0], 1 * sizeof(u64));
+  SyncVar *s = m->GetOrCreateAndLock(thr, 0, (uptr)&block[0], true);
+  s->Reset();
+  s->mtx.Unlock();
+  uptr sz = m->FreeBlock(thr, 0, (uptr)&block[0]);
+  EXPECT_EQ(sz, 1 * sizeof(u64));
+}
+
 }  // namespace __tsan