[llvm-commits] [compiler-rt] r159142 - in /compiler-rt/trunk/lib/sanitizer_common: sanitizer_allocator.cc sanitizer_common.h tests/sanitizer_allocator_test.cc
Dmitry Vyukov
dvyukov at google.com
Mon Jun 25 08:09:24 PDT 2012
Author: dvyukov
Date: Mon Jun 25 10:09:24 2012
New Revision: 159142
URL: http://llvm.org/viewvc/llvm-project?rev=159142&view=rev
Log:
tsan: remove internal allocator, switch to sanitizer_common one.
Added:
compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
- copied, changed from r159134, compiler-rt/trunk/lib/tsan/unit_tests/tsan_allocator_test.cc
Modified:
compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc?rev=159142&r1=159141&r2=159142&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc Mon Jun 25 10:09:24 2012
@@ -28,20 +28,27 @@
namespace __sanitizer {
-static const u64 kInternalAllocBlockMagic = 0x7A6CB03ABCEBC042ull;
+const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
void *InternalAlloc(uptr size) {
void *p = LIBC_MALLOC(size + sizeof(u64));
- ((u64*)p)[0] = kInternalAllocBlockMagic;
+ ((u64*)p)[0] = kBlockMagic;
return (char*)p + sizeof(u64);
}
void InternalFree(void *addr) {
if (!addr) return;
addr = (char*)addr - sizeof(u64);
- CHECK_EQ(((u64*)addr)[0], kInternalAllocBlockMagic);
+ CHECK_EQ(((u64*)addr)[0], kBlockMagic);
((u64*)addr)[0] = 0;
LIBC_FREE(addr);
}
+void *InternalAllocBlock(void *p) {
+ CHECK_NE(p, (void*)0);
+ u64 *pp = (u64*)((uptr)p & ~0x7);
+ for (; pp[0] != kBlockMagic; pp--) {}
+ return pp + 1;
+}
+
} // namespace __sanitizer
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=159142&r1=159141&r2=159142&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Mon Jun 25 10:09:24 2012
@@ -48,7 +48,10 @@
// Internal allocator
void *InternalAlloc(uptr size);
-void InternalFree(void *addr);
+void InternalFree(void *p);
+// Given the pointer p into a valid allocated block,
+// returns a pointer to the beginning of the block.
+void *InternalAllocBlock(void *p);
// IO
void RawWrite(const char *buffer);
Copied: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc (from r159134, compiler-rt/trunk/lib/tsan/unit_tests/tsan_allocator_test.cc)
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc?p2=compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc&p1=compiler-rt/trunk/lib/tsan/unit_tests/tsan_allocator_test.cc&r1=159134&r2=159142&rev=159142&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/unit_tests/tsan_allocator_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc Mon Jun 25 10:09:24 2012
@@ -1,4 +1,4 @@
-//===-- tsan_allocator_test.c----------------------------------------------===//
+//===-- sanitizer_allocator_test.cc ---------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
@@ -10,28 +10,28 @@
// This file is a part of ThreadSanitizer (TSan), a race detector.
//
//===----------------------------------------------------------------------===//
-#include "tsan_allocator.h"
+#include "sanitizer_common/sanitizer_common.h"
#include "gtest/gtest.h"
#include <stdlib.h>
namespace __tsan {
TEST(Allocator, Basic) {
- char *p = (char*)Alloc(10);
+ char *p = (char*)InternalAlloc(10);
EXPECT_NE(p, (char*)0);
- char *p2 = (char*)Alloc(20);
+ char *p2 = (char*)InternalAlloc(20);
EXPECT_NE(p2, (char*)0);
EXPECT_NE(p2, p);
for (int i = 0; i < 10; i++) {
p[i] = 42;
- EXPECT_EQ(p, AllocBlock(p + i));
+ EXPECT_EQ(p, InternalAllocBlock(p + i));
}
for (int i = 0; i < 20; i++) {
((char*)p2)[i] = 42;
- EXPECT_EQ(p2, AllocBlock(p2 + i));
+ EXPECT_EQ(p2, InternalAllocBlock(p2 + i));
}
- Free(p);
- Free(p2);
+ InternalFree(p);
+ InternalFree(p2);
}
TEST(Allocator, Stress) {
@@ -40,16 +40,16 @@
unsigned rnd = 42;
for (int i = 0; i < kCount; i++) {
uptr sz = rand_r(&rnd) % 1000;
- char *p = (char*)Alloc(sz);
+ char *p = (char*)InternalAlloc(sz);
EXPECT_NE(p, (char*)0);
for (uptr j = 0; j < sz; j++) {
p[j] = 42;
- EXPECT_EQ(p, AllocBlock(p + j));
+ EXPECT_EQ(p, InternalAllocBlock(p + j));
}
ptrs[i] = p;
}
for (int i = 0; i < kCount; i++) {
- Free(ptrs[i]);
+ InternalFree(ptrs[i]);
}
}
More information about the llvm-commits
mailing list