[llvm-commits] [compiler-rt] r170434 - in /compiler-rt/trunk/lib/sanitizer_common: sanitizer_allocator.h tests/sanitizer_allocator_test.cc
Kostya Serebryany
kcc at google.com
Tue Dec 18 06:56:38 PST 2012
Author: kcc
Date: Tue Dec 18 08:56:38 2012
New Revision: 170434
URL: http://llvm.org/viewvc/llvm-project?rev=170434&view=rev
Log:
[sanitizer] fix LargeMmapAllocator::GetBlockBegin
Modified:
compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.h
compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.h?rev=170434&r1=170433&r2=170434&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.h Tue Dec 18 08:56:38 2012
@@ -674,17 +674,11 @@
}
bool PointerIsMine(void *p) {
- // Fast check.
- if ((reinterpret_cast<uptr>(p) & (page_size_ - 1))) return false;
- SpinMutexLock l(&mutex_);
- for (Header *l = list_; l; l = l->next) {
- if (GetUser(l) == p) return true;
- }
- return false;
+ return GetBlockBegin(p) != 0;
}
uptr GetActuallyAllocatedSize(void *p) {
- return RoundUpMapSize(GetHeader(p)->size) - page_size_;
+ return RoundUpTo(GetHeader(p)->size, page_size_);
}
// At least page_size_/2 metadata bytes is available.
@@ -692,12 +686,12 @@
return GetHeader(p) + 1;
}
- void *GetBlockBegin(void *p) {
+ void *GetBlockBegin(void *ptr) {
+ uptr p = reinterpret_cast<uptr>(ptr);
SpinMutexLock l(&mutex_);
for (Header *l = list_; l; l = l->next) {
- void *b = GetUser(l);
- if (p >= b && p < (u8*)b + l->size)
- return b;
+ if (p >= l->map_beg && p < l->map_beg + l->map_size)
+ return GetUser(l);
}
return 0;
}
Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc?rev=170434&r1=170433&r2=170434&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc Tue Dec 18 08:56:38 2012
@@ -106,6 +106,8 @@
CHECK_EQ(x, a->GetBlockBegin(x));
CHECK_EQ(x, a->GetBlockBegin(x + size - 1));
CHECK(a->PointerIsMine(x));
+ CHECK(a->PointerIsMine(x + size - 1));
+ CHECK(a->PointerIsMine(x + size / 2));
CHECK_GE(a->GetActuallyAllocatedSize(x), size);
uptr class_id = a->GetSizeClass(x);
CHECK_EQ(class_id, Allocator::SizeClassMapT::ClassID(size));
@@ -265,16 +267,16 @@
a.Init();
static const int kNumAllocs = 100;
- void *allocated[kNumAllocs];
+ char *allocated[kNumAllocs];
static const uptr size = 1000;
// Allocate some.
for (int i = 0; i < kNumAllocs; i++) {
- allocated[i] = a.Allocate(size, 1);
+ allocated[i] = (char *)a.Allocate(size, 1);
}
// Deallocate all.
CHECK_GT(a.TotalMemoryUsed(), size * kNumAllocs);
for (int i = 0; i < kNumAllocs; i++) {
- void *p = allocated[i];
+ char *p = allocated[i];
CHECK(a.PointerIsMine(p));
a.Deallocate(p);
}
@@ -283,7 +285,7 @@
// Allocate some more, also add metadata.
for (int i = 0; i < kNumAllocs; i++) {
- void *x = a.Allocate(size, 1);
+ char *x = (char *)a.Allocate(size, 1);
CHECK_GE(a.GetActuallyAllocatedSize(x), size);
uptr *meta = reinterpret_cast<uptr*>(a.GetMetaData(x));
*meta = i;
@@ -293,7 +295,7 @@
// Deallocate all in reverse order.
for (int i = 0; i < kNumAllocs; i++) {
int idx = kNumAllocs - i - 1;
- void *p = allocated[idx];
+ char *p = allocated[idx];
uptr *meta = reinterpret_cast<uptr*>(a.GetMetaData(p));
CHECK_EQ(*meta, idx);
CHECK(a.PointerIsMine(p));
@@ -304,9 +306,11 @@
for (uptr alignment = 8; alignment <= max_alignment; alignment *= 2) {
for (int i = 0; i < kNumAllocs; i++) {
uptr size = ((i % 10) + 1) * 4096;
- allocated[i] = a.Allocate(size, alignment);
+ char *p = allocated[i] = (char *)a.Allocate(size, alignment);
+ CHECK_EQ(p, a.GetBlockBegin(p));
+ CHECK_EQ(p, a.GetBlockBegin(p + size - 1));
+ CHECK_EQ(p, a.GetBlockBegin(p + size / 2));
CHECK_EQ(0, (uptr)allocated[i] % alignment);
- char *p = (char*)allocated[i];
p[0] = p[size - 1] = 0;
}
for (int i = 0; i < kNumAllocs; i++) {
More information about the llvm-commits
mailing list