1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/allocator/allocator_shim.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 // TCMalloc header files
10 #include "common.h" // For TCMalloc constants like page size, etc.
12 using base::allocator::TCMallocDoMallocForTest
;
13 using base::allocator::TCMallocDoFreeForTest
;
14 using base::allocator::ExcludeSpaceForMarkForTest
;
16 TEST(TCMallocFreeCheck
, BadPointerInFirstPageOfTheLargeObject
) {
17 char* p
= reinterpret_cast<char*>(
18 TCMallocDoMallocForTest(ExcludeSpaceForMarkForTest(kMaxSize
+ 1)));
19 for (int offset
= 1; offset
< kPageSize
; offset
<<= 1) {
20 ASSERT_DEATH(TCMallocDoFreeForTest(p
+ offset
),
21 "Pointer is not pointing to the start of a span");
25 TEST(TCMallocFreeCheck
, BadPageAlignedPointerInsideLargeObject
) {
26 char* p
= reinterpret_cast<char*>(
27 TCMallocDoMallocForTest(ExcludeSpaceForMarkForTest(kMaxSize
+ 1)));
29 for (int offset
= kPageSize
; offset
< kMaxSize
; offset
+= kPageSize
) {
30 // Only the first and last page of a span are in heap map. So for others
31 // tcmalloc will give a general error of invalid pointer.
32 ASSERT_DEATH(TCMallocDoFreeForTest(p
+ offset
),
33 "Attempt to free invalid pointer");
35 ASSERT_DEATH(TCMallocDoFreeForTest(p
+ kMaxSize
),
36 "Pointer is not pointing to the start of a span");
39 TEST(TCMallocFreeCheck
, DoubleFreeLargeObject
) {
40 char* p
= reinterpret_cast<char*>(
41 TCMallocDoMallocForTest(ExcludeSpaceForMarkForTest(kMaxSize
+ 1)));
42 ASSERT_DEATH(TCMallocDoFreeForTest(p
); TCMallocDoFreeForTest(p
),
43 "Object was not in-use");
48 TEST(TCMallocFreeCheck
, DoubleFreeSmallObject
) {
50 size
<= ExcludeSpaceForMarkForTest(kMaxSize
);
52 char* p
= reinterpret_cast<char*>(TCMallocDoMallocForTest(size
));
53 ASSERT_DEATH(TCMallocDoFreeForTest(p
); TCMallocDoFreeForTest(p
),
54 "Circular loop in list detected");
58 TEST(TCMallocFreeCheck
, DoubleFreeSmallObject
) {
61 // When the object is small, tcmalloc validation can not distinguish normal
62 // memory corruption or double free, because there's not enough space in
63 // freed objects to keep the mark.
64 for (; size
<= ExcludeSpaceForMarkForTest(kMinClassSize
); size
<<= 1) {
65 char* p
= reinterpret_cast<char*>(TCMallocDoMallocForTest(size
));
66 ASSERT_DEATH(TCMallocDoFreeForTest(p
); TCMallocDoFreeForTest(p
),
70 for (; size
<= ExcludeSpaceForMarkForTest(kMaxSize
); size
<<= 1) {
71 char* p
= reinterpret_cast<char*>(TCMallocDoMallocForTest(size
));
72 ASSERT_DEATH(TCMallocDoFreeForTest(p
); TCMallocDoFreeForTest(p
),
73 "Attempt to double free");
78 int main(int argc
, char **argv
) {
79 testing::InitGoogleTest(&argc
, argv
);
80 return RUN_ALL_TESTS();