| 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 "google/malloc_extension.h" | 36 #include "gperftools/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: | 78 // Test match as well as mismatch rules. But do not test on OS X; on |
| 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__ |
| 79 TEST(DebugAllocationTest, DeallocMismatch) { | 82 TEST(DebugAllocationTest, DeallocMismatch) { |
| 80 // malloc can be matched only by free | 83 // malloc can be matched only by free |
| 81 // new can be matched only by delete and delete(nothrow) | 84 // new can be matched only by delete and delete(nothrow) |
| 82 // new[] can be matched only by delete[] and delete[](nothrow) | 85 // new[] can be matched only by delete[] and delete[](nothrow) |
| 83 // new(nothrow) can be matched only by delete and delete(nothrow) | 86 // new(nothrow) can be matched only by delete and delete(nothrow) |
| 84 // new(nothrow)[] can be matched only by delete[] and delete[](nothrow) | 87 // new(nothrow)[] can be matched only by delete[] and delete[](nothrow) |
| 85 | 88 |
| 86 // Allocate with malloc. | 89 // Allocate with malloc. |
| 87 { | 90 { |
| 88 int* x = static_cast<int*>(malloc(sizeof(*x))); | 91 int* x = static_cast<int*>(malloc(sizeof(*x))); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 // Allocate with new(nothrow)[]. | 128 // Allocate with new(nothrow)[]. |
| 126 { | 129 { |
| 127 int* x = new(std::nothrow) int[1]; | 130 int* x = new(std::nothrow) int[1]; |
| 128 int* y = new(std::nothrow) int[1]; | 131 int* y = new(std::nothrow) int[1]; |
| 129 IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free"); | 132 IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free"); |
| 130 IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete"); | 133 IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete"); |
| 131 delete [] x; | 134 delete [] x; |
| 132 ::operator delete[](y, std::nothrow); | 135 ::operator delete[](y, std::nothrow); |
| 133 } | 136 } |
| 134 } | 137 } |
| 138 #endif // #ifdef OS_MACOSX |
| 135 | 139 |
| 136 TEST(DebugAllocationTest, DoubleFree) { | 140 TEST(DebugAllocationTest, DoubleFree) { |
| 137 int* pint = new int; | 141 int* pint = new int; |
| 138 delete pint; | 142 delete pint; |
| 139 IF_DEBUG_EXPECT_DEATH(delete pint, "has been already deallocated"); | 143 IF_DEBUG_EXPECT_DEATH(delete pint, "has been already deallocated"); |
| 140 } | 144 } |
| 141 | 145 |
| 142 TEST(DebugAllocationTest, StompBefore) { | 146 TEST(DebugAllocationTest, StompBefore) { |
| 143 int* pint = new int; | 147 int* pint = new int; |
| 144 #ifndef NDEBUG // don't stomp memory if we're not in a position to detect it | 148 #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... |
| 300 // death tests, we will run only the non-death parts. One way to | 304 // death tests, we will run only the non-death parts. One way to |
| 301 // tell when you are done with all tests is when no 'expected | 305 // tell when you are done with all tests is when no 'expected |
| 302 // regexp' message is printed for a given argv[1]. | 306 // regexp' message is printed for a given argv[1]. |
| 303 if (argc < 2) { | 307 if (argc < 2) { |
| 304 test_to_run = -1; // will never match | 308 test_to_run = -1; // will never match |
| 305 } else { | 309 } else { |
| 306 test_to_run = atoi(argv[1]); | 310 test_to_run = atoi(argv[1]); |
| 307 } | 311 } |
| 308 return RUN_ALL_TESTS(); | 312 return RUN_ALL_TESTS(); |
| 309 } | 313 } |
| OLD | NEW |