OLD | NEW |
1 // Copyright (c) 2007, Google Inc. | 1 // Copyright (c) 2007, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 15 matching lines...) Expand all Loading... |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 // --- | 30 // --- |
31 // Author: Fred Akalin | 31 // Author: Fred Akalin |
32 | 32 |
33 #include <stdio.h> | 33 #include <stdio.h> |
34 #include <stdlib.h> | 34 #include <stdlib.h> |
35 #include <vector> | 35 #include <vector> |
36 #include "gperftools/malloc_extension.h" | 36 #include "google/malloc_extension.h" |
37 #include "base/logging.h" | 37 #include "base/logging.h" |
38 | 38 |
39 using std::vector; | 39 using std::vector; |
40 | 40 |
41 vector<void (*)()> g_testlist; // the tests to run | 41 vector<void (*)()> g_testlist; // the tests to run |
42 | 42 |
43 #define TEST(a, b) \ | 43 #define TEST(a, b) \ |
44 struct Test_##a##_##b { \ | 44 struct Test_##a##_##b { \ |
45 Test_##a##_##b() { g_testlist.push_back(&Run); } \ | 45 Test_##a##_##b() { g_testlist.push_back(&Run); } \ |
46 static void Run(); \ | 46 static void Run(); \ |
(...skipping 21 matching lines...) Expand all Loading... |
68 #define IF_DEBUG_EXPECT_DEATH(statement, regex) do { \ | 68 #define IF_DEBUG_EXPECT_DEATH(statement, regex) do { \ |
69 if (test_counter++ == test_to_run) { \ | 69 if (test_counter++ == test_to_run) { \ |
70 fprintf(stderr, "Expected regex:%s\n", regex); \ | 70 fprintf(stderr, "Expected regex:%s\n", regex); \ |
71 statement; \ | 71 statement; \ |
72 } \ | 72 } \ |
73 } while (false) | 73 } while (false) |
74 | 74 |
75 // This flag won't be compiled in in opt mode. | 75 // This flag won't be compiled in in opt mode. |
76 DECLARE_int32(max_free_queue_size); | 76 DECLARE_int32(max_free_queue_size); |
77 | 77 |
78 // Test match as well as mismatch rules. But do not test on OS X; on | 78 // Test match as well as mismatch rules: |
79 // OS X the OS converts new/new[] to malloc before it gets to us, so | |
80 // we are unable to catch these mismatch errors. | |
81 #ifndef __APPLE__ | |
82 TEST(DebugAllocationTest, DeallocMismatch) { | 79 TEST(DebugAllocationTest, DeallocMismatch) { |
83 // malloc can be matched only by free | 80 // malloc can be matched only by free |
84 // new can be matched only by delete and delete(nothrow) | 81 // new can be matched only by delete and delete(nothrow) |
85 // new[] can be matched only by delete[] and delete[](nothrow) | 82 // new[] can be matched only by delete[] and delete[](nothrow) |
86 // new(nothrow) can be matched only by delete and delete(nothrow) | 83 // new(nothrow) can be matched only by delete and delete(nothrow) |
87 // new(nothrow)[] can be matched only by delete[] and delete[](nothrow) | 84 // new(nothrow)[] can be matched only by delete[] and delete[](nothrow) |
88 | 85 |
89 // Allocate with malloc. | 86 // Allocate with malloc. |
90 { | 87 { |
91 int* x = static_cast<int*>(malloc(sizeof(*x))); | 88 int* x = static_cast<int*>(malloc(sizeof(*x))); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // Allocate with new(nothrow)[]. | 125 // Allocate with new(nothrow)[]. |
129 { | 126 { |
130 int* x = new(std::nothrow) int[1]; | 127 int* x = new(std::nothrow) int[1]; |
131 int* y = new(std::nothrow) int[1]; | 128 int* y = new(std::nothrow) int[1]; |
132 IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free"); | 129 IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free"); |
133 IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete"); | 130 IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete"); |
134 delete [] x; | 131 delete [] x; |
135 ::operator delete[](y, std::nothrow); | 132 ::operator delete[](y, std::nothrow); |
136 } | 133 } |
137 } | 134 } |
138 #endif // #ifdef OS_MACOSX | |
139 | 135 |
140 TEST(DebugAllocationTest, DoubleFree) { | 136 TEST(DebugAllocationTest, DoubleFree) { |
141 int* pint = new int; | 137 int* pint = new int; |
142 delete pint; | 138 delete pint; |
143 IF_DEBUG_EXPECT_DEATH(delete pint, "has been already deallocated"); | 139 IF_DEBUG_EXPECT_DEATH(delete pint, "has been already deallocated"); |
144 } | 140 } |
145 | 141 |
146 TEST(DebugAllocationTest, StompBefore) { | 142 TEST(DebugAllocationTest, StompBefore) { |
147 int* pint = new int; | 143 int* pint = new int; |
148 #ifndef NDEBUG // don't stomp memory if we're not in a position to detect it | 144 #ifndef NDEBUG // don't stomp memory if we're not in a position to detect it |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 // death tests, we will run only the non-death parts. One way to | 300 // death tests, we will run only the non-death parts. One way to |
305 // tell when you are done with all tests is when no 'expected | 301 // tell when you are done with all tests is when no 'expected |
306 // regexp' message is printed for a given argv[1]. | 302 // regexp' message is printed for a given argv[1]. |
307 if (argc < 2) { | 303 if (argc < 2) { |
308 test_to_run = -1; // will never match | 304 test_to_run = -1; // will never match |
309 } else { | 305 } else { |
310 test_to_run = atoi(argv[1]); | 306 test_to_run = atoi(argv[1]); |
311 } | 307 } |
312 return RUN_ALL_TESTS(); | 308 return RUN_ALL_TESTS(); |
313 } | 309 } |
OLD | NEW |