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 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 int *x = new int; | 206 int *x = new int; |
207 delete x; | 207 delete x; |
208 int old_x_value = *x; | 208 int old_x_value = *x; |
209 *x = 1; | 209 *x = 1; |
210 // verify that dangling writes are caught at program termination if the | 210 // verify that dangling writes are caught at program termination if the |
211 // corrupted block never got pushed off of the end of the free queue. | 211 // corrupted block never got pushed off of the end of the free queue. |
212 IF_DEBUG_EXPECT_DEATH(exit(0), "Memory was written to after being freed."); | 212 IF_DEBUG_EXPECT_DEATH(exit(0), "Memory was written to after being freed."); |
213 *x = old_x_value; // restore x so that the test can exit successfully. | 213 *x = old_x_value; // restore x so that the test can exit successfully. |
214 } | 214 } |
215 | 215 |
| 216 TEST(DebugAllocationTest, StackTraceWithDanglingWriteAtExitTest) { |
| 217 int *x = new int; |
| 218 delete x; |
| 219 int old_x_value = *x; |
| 220 *x = 1; |
| 221 // verify that we also get a stack trace when we have a dangling write. |
| 222 // The " @ " is part of the stack trace output. |
| 223 IF_DEBUG_EXPECT_DEATH(exit(0), " @ .*main"); |
| 224 *x = old_x_value; // restore x so that the test can exit successfully. |
| 225 } |
| 226 |
216 static size_t CurrentlyAllocatedBytes() { | 227 static size_t CurrentlyAllocatedBytes() { |
217 size_t value; | 228 size_t value; |
218 CHECK(MallocExtension::instance()->GetNumericProperty( | 229 CHECK(MallocExtension::instance()->GetNumericProperty( |
219 "generic.current_allocated_bytes", &value)); | 230 "generic.current_allocated_bytes", &value)); |
220 return value; | 231 return value; |
221 } | 232 } |
222 | 233 |
223 TEST(DebugAllocationTest, CurrentlyAllocated) { | 234 TEST(DebugAllocationTest, CurrentlyAllocated) { |
224 // Clear the free queue | 235 // Clear the free queue |
225 #if 1 | 236 #if 1 |
(...skipping 26 matching lines...) Expand all Loading... |
252 #endif | 263 #endif |
253 void* a = malloc(1000); | 264 void* a = malloc(1000); |
254 EXPECT_GE(MallocExtension::instance()->GetAllocatedSize(a), 1000); | 265 EXPECT_GE(MallocExtension::instance()->GetAllocatedSize(a), 1000); |
255 // This is just a sanity check. If we allocated too much, alloc is broken | 266 // This is just a sanity check. If we allocated too much, alloc is broken |
256 EXPECT_LE(MallocExtension::instance()->GetAllocatedSize(a), 5000); | 267 EXPECT_LE(MallocExtension::instance()->GetAllocatedSize(a), 5000); |
257 EXPECT_GE(MallocExtension::instance()->GetEstimatedAllocatedSize(1000), 1000); | 268 EXPECT_GE(MallocExtension::instance()->GetEstimatedAllocatedSize(1000), 1000); |
258 free(a); | 269 free(a); |
259 } | 270 } |
260 | 271 |
261 TEST(DebugAllocationTest, HugeAlloc) { | 272 TEST(DebugAllocationTest, HugeAlloc) { |
262 const size_t kTooBig = ~static_cast<size_t>(0); | 273 // This must not be a const variable so it doesn't form an |
| 274 // integral-constant-expression which can be *statically* rejected by the |
| 275 // compiler as too large for the allocation. |
| 276 size_t kTooBig = ~static_cast<size_t>(0); |
263 void* a = NULL; | 277 void* a = NULL; |
264 char* b = NULL; | |
265 | 278 |
266 #ifndef NDEBUG | 279 #ifndef NDEBUG |
267 | 280 |
268 a = malloc(kTooBig); | 281 a = malloc(kTooBig); |
269 EXPECT_EQ(NULL, a); | 282 EXPECT_EQ(NULL, a); |
270 b = NULL; | |
271 IF_DEBUG_EXPECT_DEATH(b = new char[kTooBig], | |
272 "Unable to allocate.*new\\[\\] failed\\."); | |
273 EXPECT_EQ(NULL, b); | |
274 | 283 |
275 // kAlsoTooBig is small enough not to get caught by debugallocation's check, | 284 // kAlsoTooBig is small enough not to get caught by debugallocation's check, |
276 // but will still fall through to tcmalloc's check. | 285 // but will still fall through to tcmalloc's check. This must also be |
277 const size_t kAlsoTooBig = kTooBig - 1024; | 286 // a non-const variable. See kTooBig for more details. |
| 287 size_t kAlsoTooBig = kTooBig - 1024; |
278 | 288 |
279 a = malloc(kAlsoTooBig); | 289 a = malloc(kAlsoTooBig); |
280 EXPECT_EQ(NULL, a); | 290 EXPECT_EQ(NULL, a); |
281 IF_DEBUG_EXPECT_DEATH(b = new char[kAlsoTooBig], "Unable to allocate.*new fail
ed"); | |
282 EXPECT_EQ(NULL, b); | |
283 #endif | 291 #endif |
284 } | 292 } |
285 | 293 |
286 int main(int argc, char** argv) { | 294 int main(int argc, char** argv) { |
287 // If you run without args, we run the non-death parts of the test. | 295 // If you run without args, we run the non-death parts of the test. |
288 // Otherwise, argv[1] should be a number saying which death-test | 296 // Otherwise, argv[1] should be a number saying which death-test |
289 // to run. We will output a regexp we expect the death-message | 297 // to run. We will output a regexp we expect the death-message |
290 // to include, and then run the given death test (which hopefully | 298 // to include, and then run the given death test (which hopefully |
291 // will produce that error message). If argv[1] > the number of | 299 // will produce that error message). If argv[1] > the number of |
292 // 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 |
293 // 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 |
294 // regexp' message is printed for a given argv[1]. | 302 // regexp' message is printed for a given argv[1]. |
295 if (argc < 2) { | 303 if (argc < 2) { |
296 test_to_run = -1; // will never match | 304 test_to_run = -1; // will never match |
297 } else { | 305 } else { |
298 test_to_run = atoi(argv[1]); | 306 test_to_run = atoi(argv[1]); |
299 } | 307 } |
300 return RUN_ALL_TESTS(); | 308 return RUN_ALL_TESTS(); |
301 } | 309 } |
OLD | NEW |