| 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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 95 |
| 96 // Allocate with new[]. | 96 // Allocate with new[]. |
| 97 { | 97 { |
| 98 int* x = new int[1]; | 98 int* x = new int[1]; |
| 99 IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free"); | 99 IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free"); |
| 100 IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete"); | 100 IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete"); |
| 101 delete [] x; | 101 delete [] x; |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 TEST(DebugAllocationTest, DoubleFree) { |
| 106 int* pint = new int; |
| 107 delete pint; |
| 108 IF_DEBUG_EXPECT_DEATH(delete pint, "has been already deallocated"); |
| 109 } |
| 110 |
| 111 TEST(DebugAllocationTest, StompBefore) { |
| 112 int* pint = new int; |
| 113 #ifndef NDEBUG // don't stomp memory if we're not in a position to detect it |
| 114 pint[-1] = 5; |
| 115 IF_DEBUG_EXPECT_DEATH(delete pint, "a word before object"); |
| 116 #endif |
| 117 } |
| 118 |
| 119 TEST(DebugAllocationTest, StompAfter) { |
| 120 int* pint = new int; |
| 121 #ifndef NDEBUG // don't stomp memory if we're not in a position to detect it |
| 122 pint[1] = 5; |
| 123 IF_DEBUG_EXPECT_DEATH(delete pint, "a word after object"); |
| 124 #endif |
| 125 } |
| 126 |
| 105 TEST(DebugAllocationTest, FreeQueueTest) { | 127 TEST(DebugAllocationTest, FreeQueueTest) { |
| 106 // Verify that the allocator doesn't return blocks that were recently freed. | 128 // Verify that the allocator doesn't return blocks that were recently freed. |
| 107 int* x = new int; | 129 int* x = new int; |
| 108 int* old_x = x; | 130 int* old_x = x; |
| 109 delete x; | 131 delete x; |
| 110 x = new int; | 132 x = new int; |
| 111 #if 1 | 133 #if 1 |
| 112 // This check should not be read as a universal guarantee of behavior. If | 134 // This check should not be read as a universal guarantee of behavior. If |
| 113 // other threads are executing, it would be theoretically possible for this | 135 // other threads are executing, it would be theoretically possible for this |
| 114 // check to fail despite the efforts of debugallocation.cc to the contrary. | 136 // check to fail despite the efforts of debugallocation.cc to the contrary. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 } | 220 } |
| 199 #endif | 221 #endif |
| 200 void* a = malloc(1000); | 222 void* a = malloc(1000); |
| 201 EXPECT_GE(MallocExtension::instance()->GetAllocatedSize(a), 1000); | 223 EXPECT_GE(MallocExtension::instance()->GetAllocatedSize(a), 1000); |
| 202 // This is just a sanity check. If we allocated too much, alloc is broken | 224 // This is just a sanity check. If we allocated too much, alloc is broken |
| 203 EXPECT_LE(MallocExtension::instance()->GetAllocatedSize(a), 5000); | 225 EXPECT_LE(MallocExtension::instance()->GetAllocatedSize(a), 5000); |
| 204 EXPECT_GE(MallocExtension::instance()->GetEstimatedAllocatedSize(1000), 1000); | 226 EXPECT_GE(MallocExtension::instance()->GetEstimatedAllocatedSize(1000), 1000); |
| 205 free(a); | 227 free(a); |
| 206 } | 228 } |
| 207 | 229 |
| 230 TEST(DebugAllocationTest, HugeAlloc) { |
| 231 const size_t kTooBig = ~static_cast<size_t>(0); |
| 232 void* a = NULL; |
| 233 char* b = NULL; |
| 234 |
| 235 #ifndef NDEBUG |
| 236 |
| 237 a = malloc(kTooBig); |
| 238 EXPECT_EQ(NULL, a); |
| 239 b = NULL; |
| 240 IF_DEBUG_EXPECT_DEATH(b = new char[kTooBig], |
| 241 "Unable to allocate.*new\\[\\] failed\\."); |
| 242 EXPECT_EQ(NULL, b); |
| 243 |
| 244 // kAlsoTooBig is small enough not to get caught by debugallocation's check, |
| 245 // but will still fall through to tcmalloc's check. |
| 246 const size_t kAlsoTooBig = kTooBig - 1024; |
| 247 |
| 248 a = malloc(kAlsoTooBig); |
| 249 EXPECT_EQ(NULL, a); |
| 250 IF_DEBUG_EXPECT_DEATH(b = new char[kAlsoTooBig], "Unable to allocate.*new fail
ed"); |
| 251 EXPECT_EQ(NULL, b); |
| 252 #endif |
| 253 } |
| 254 |
| 208 int main(int argc, char** argv) { | 255 int main(int argc, char** argv) { |
| 209 // If you run without args, we run the non-death parts of the test. | 256 // If you run without args, we run the non-death parts of the test. |
| 210 // Otherwise, argv[1] should be a number saying which death-test | 257 // Otherwise, argv[1] should be a number saying which death-test |
| 211 // to run. We will output a regexp we expect the death-message | 258 // to run. We will output a regexp we expect the death-message |
| 212 // to include, and then run the given death test (which hopefully | 259 // to include, and then run the given death test (which hopefully |
| 213 // will produce that error message). If argv[1] > the number of | 260 // will produce that error message). If argv[1] > the number of |
| 214 // death tests, we will run only the non-death parts. One way to | 261 // death tests, we will run only the non-death parts. One way to |
| 215 // tell when you are done with all tests is when no 'expected | 262 // tell when you are done with all tests is when no 'expected |
| 216 // regexp' message is printed for a given argv[1]. | 263 // regexp' message is printed for a given argv[1]. |
| 217 if (argc < 2) { | 264 if (argc < 2) { |
| 218 test_to_run = -1; // will never match | 265 test_to_run = -1; // will never match |
| 219 } else { | 266 } else { |
| 220 test_to_run = atoi(argv[1]); | 267 test_to_run = atoi(argv[1]); |
| 221 } | 268 } |
| 222 return RUN_ALL_TESTS(); | 269 return RUN_ALL_TESTS(); |
| 223 } | 270 } |
| OLD | NEW |