OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 Google Inc. All Rights Reserved. | |
rvargas (doing something else)
2012/05/19 01:08:04
Use chromium header
kaiwang
2012/05/21 18:11:42
oops.. done
| |
2 // Author: kaiwang@google.com (Kai Wang) | |
3 #include <stdio.h> | |
4 #include "base/allocator/allocator_shim.h" | |
rvargas (doing something else)
2012/05/19 01:08:04
add a line
kaiwang
2012/05/21 18:11:42
Done.
| |
5 #include "testing/gtest/include/gtest/gtest.h" | |
6 | |
7 // TCMalloc header files | |
8 #include "common.h" // For TCMalloc constants like page size, etc. | |
9 | |
10 using base::allocator::TCMallocDoMallocForTest; | |
11 using base::allocator::TCMallocDoFreeForTest; | |
12 using base::allocator::ExcludeSpaceForMarkForTest; | |
13 | |
14 TEST(TCMallocFreeCheck, BadPointerInFirstPageOfTheLargeObject) { | |
15 char* p = reinterpret_cast<char*>( | |
16 TCMallocDoMallocForTest(ExcludeSpaceForMarkForTest(kMaxSize + 1))); | |
17 for (int offset = 1; offset < kPageSize ; offset <<= 1) { | |
18 ASSERT_DEATH(TCMallocDoFreeForTest(p + offset), | |
19 "Pointer is not pointing to the start of a span"); | |
20 } | |
21 } | |
22 | |
23 TEST(TCMallocFreeCheck, BadPageAlignedPointerInsideLargeObject) { | |
24 char* p = reinterpret_cast<char*>( | |
25 TCMallocDoMallocForTest(ExcludeSpaceForMarkForTest(kMaxSize + 1))); | |
26 | |
27 for (int offset = kPageSize; offset < kMaxSize; offset += kPageSize) { | |
28 // Only the first and last page of a span are in heap map. So for others | |
29 // tcmalloc will give a generaal error of invalid pointer. | |
rvargas (doing something else)
2012/05/19 01:08:04
typo generaal
kaiwang
2012/05/21 18:11:42
Done.
| |
30 ASSERT_DEATH(TCMallocDoFreeForTest(p + offset), | |
31 "Attempt to free invalid pointer"); | |
32 } | |
33 ASSERT_DEATH(TCMallocDoFreeForTest(p + kMaxSize), | |
34 "Pointer is not inside the first page of a span"); | |
35 } | |
36 | |
37 TEST(TCMallocFreeCheck, DoubleFreeLargeObject) { | |
38 char* p = reinterpret_cast<char*>( | |
39 TCMallocDoMallocForTest(ExcludeSpaceForMarkForTest(kMaxSize + 1))); | |
40 ASSERT_DEATH(TCMallocDoFreeForTest(p); TCMallocDoFreeForTest(p), | |
41 "Freeing a span not in use"); | |
42 } | |
43 | |
44 | |
45 #ifdef NDEBUG | |
46 TEST(TCMallocFreeCheck, DoubleFreeSmallObject) { | |
47 for (size_t size = 1; | |
48 size <= ExcludeSpaceForMarkForTest(kMaxSize); | |
49 size <<= 1) { | |
50 char* p = reinterpret_cast<char*>(TCMallocDoMallocForTest(size)); | |
51 ASSERT_DEATH(TCMallocDoFreeForTest(p); TCMallocDoFreeForTest(p), | |
52 "Circular loop in list detected"); | |
53 } | |
54 } | |
55 #else | |
56 TEST(TCMallocFreeCheck, DoubleFreeSmallObject) { | |
57 size_t size = 1; | |
58 | |
59 // When the object is small, tcmalloc validation can not distinguish normal | |
60 // memory corruption or double free, because there's not enough space in | |
61 // freed objects to keep the mark. | |
62 for (; size <= ExcludeSpaceForMarkForTest(kMinClassSize); size <<= 1) { | |
63 char* p = reinterpret_cast<char*>(TCMallocDoMallocForTest(size)); | |
64 ASSERT_DEATH(TCMallocDoFreeForTest(p); TCMallocDoFreeForTest(p), | |
65 "Memory corrupted"); | |
66 } | |
67 | |
68 for (; size <= ExcludeSpaceForMarkForTest(kMaxSize); size <<= 1) { | |
69 char* p = reinterpret_cast<char*>(TCMallocDoMallocForTest(size)); | |
70 ASSERT_DEATH(TCMallocDoFreeForTest(p); TCMallocDoFreeForTest(p), | |
71 "Attempt to double free"); | |
72 } | |
73 } | |
74 #endif | |
75 | |
76 int main(int argc, char **argv) { | |
77 testing::InitGoogleTest(&argc, argv); | |
78 return RUN_ALL_TESTS(); | |
79 } | |
OLD | NEW |