| 1 | #include "CppUTest/TestHarness.h" |
| 2 | #include "CppUTest/TestMemoryAllocator.h" |
| 3 | #include "CppUTest/MemoryLeakDetector.h" |
| 4 | #include "CppUTest/TestOutput.h" |
| 5 | #include "CppUTest/TestRegistry.h" |
| 6 | #include "CppUTest/PlatformSpecificFunctions.h" |
| 7 | #include "CppUTest/TestTestingFixture.h" |
| 8 | #include "AllocationInCppFile.h" |
| 9 | |
| 10 | #include "CppUTest/TestHarness_c.h" |
| 11 | #include "AllocationInCFile.h" |
| 12 | |
| 13 | TEST_GROUP(BasicBehavior) |
| 14 | { |
| 15 | }; |
| 16 | |
| 17 | TEST(BasicBehavior, CanDeleteNullPointers) |
| 18 | { |
| 19 | delete (char*) NULLPTR; |
| 20 | delete [] (char*) NULLPTR; |
| 21 | } |
| 22 | |
| 23 | #if CPPUTEST_USE_MEM_LEAK_DETECTION |
| 24 | |
| 25 | CPPUTEST_DO_NOT_SANITIZE_ADDRESS |
| 26 | static void deleteArrayInvalidatesMemory() |
| 27 | { |
| 28 | unsigned char* memory = new unsigned char[10]; |
| 29 | PlatformSpecificMemset(memory, 0xAB, 10); |
| 30 | delete [] memory; |
| 31 | CHECK(memory[5] != 0xCB); |
| 32 | } |
| 33 | |
| 34 | TEST(BasicBehavior, deleteArrayInvalidatesMemory) |
| 35 | { |
| 36 | deleteArrayInvalidatesMemory(); |
| 37 | } |
| 38 | |
| 39 | CPPUTEST_DO_NOT_SANITIZE_ADDRESS |
| 40 | static void deleteInvalidatesMemory() |
| 41 | { |
| 42 | unsigned char* memory = new unsigned char; |
| 43 | *memory = 0xAD; |
| 44 | delete memory; |
| 45 | CHECK(*memory != 0xAD); |
| 46 | } |
| 47 | |
| 48 | TEST(BasicBehavior, deleteInvalidatesMemory) |
| 49 | { |
| 50 | deleteInvalidatesMemory(); |
| 51 | } |
| 52 | |
| 53 | #if __cplusplus >= 201402L |
| 54 | TEST(BasicBehavior, DeleteWithSizeParameterWorks) |
| 55 | { |
| 56 | char* charMemory = new char; |
| 57 | char* charArrayMemory = new char[10]; |
| 58 | ::operator delete(charMemory, sizeof(char)); |
| 59 | ::operator delete[](charArrayMemory, sizeof(char)* 10); |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | static void deleteUnallocatedMemory() |
| 64 | { |
| 65 | delete (char*) 0x1234678; |
| 66 | FAIL("Should never come here"); // LCOV_EXCL_LINE |
| 67 | } // LCOV_EXCL_LINE |
| 68 | |
| 69 | TEST(BasicBehavior, deleteWillNotThrowAnExceptionWhenDeletingUnallocatedMemoryButCanStillCauseTestFailures) |
| 70 | { |
| 71 | /* |
| 72 | * Test failure might cause an exception. But according to C++ standard, you aren't allowed |
| 73 | * to throw exceptions in the delete function. If you do that, it will call std::terminate. |
| 74 | * Therefore, the delete will need to fail without exceptions. |
| 75 | */ |
| 76 | MemoryLeakFailure* defaultReporter = MemoryLeakWarningPlugin::getGlobalFailureReporter(); |
| 77 | TestTestingFixture fixture; |
| 78 | fixture.setTestFunction(deleteUnallocatedMemory); |
| 79 | fixture.runAllTests(); |
| 80 | LONGS_EQUAL(1, fixture.getFailureCount()); |
| 81 | POINTERS_EQUAL(defaultReporter, MemoryLeakWarningPlugin::getGlobalFailureReporter()); |
| 82 | } |
| 83 | |
| 84 | #endif |
| 85 | |
| 86 | #ifdef CPPUTEST_USE_MALLOC_MACROS |
| 87 | |
| 88 | /* This include is added because *sometimes* the cstdlib does an #undef. This should have been prevented */ |
| 89 | #if CPPUTEST_USE_STD_CPP_LIB |
| 90 | #include <cstdlib> |
| 91 | #endif |
| 92 | |
| 93 | TEST(BasicBehavior, bothMallocAndFreeAreOverloaded) |
| 94 | { |
| 95 | void* memory = cpputest_malloc_location(sizeof(char), "file", 10); |
| 96 | free(memory); |
| 97 | |
| 98 | memory = malloc(sizeof(unsigned char)); |
| 99 | cpputest_free_location(memory, "file", 10); |
| 100 | } |
| 101 | |
| 102 | #endif |
| 103 | |
| 104 | #if CPPUTEST_USE_MEM_LEAK_DETECTION |
| 105 | |
| 106 | CPPUTEST_DO_NOT_SANITIZE_ADDRESS |
| 107 | static void freeInvalidatesMemory() |
| 108 | { |
| 109 | unsigned char* memory = (unsigned char*) cpputest_malloc(sizeof(unsigned char)); |
| 110 | *memory = 0xAD; |
| 111 | cpputest_free(memory); |
| 112 | CHECK(*memory != 0xAD); |
| 113 | } |
| 114 | |
| 115 | TEST(BasicBehavior, freeInvalidatesMemory) |
| 116 | { |
| 117 | freeInvalidatesMemory(); |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | TEST_GROUP(MemoryLeakOverridesToBeUsedInProductionCode) |
| 122 | { |
| 123 | MemoryLeakDetector* memLeakDetector; |
| 124 | void setup() |
| 125 | { |
| 126 | memLeakDetector = MemoryLeakWarningPlugin::getGlobalDetector(); |
| 127 | } |
| 128 | |
| 129 | }; |
| 130 | |
| 131 | #ifdef CPPUTEST_USE_MALLOC_MACROS |
| 132 | |
| 133 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, MallocOverrideIsUsed) |
| 134 | { |
| 135 | size_t memLeaks = memLeakDetector->totalMemoryLeaks(mem_leak_period_checking); |
| 136 | void* memory = malloc(10); |
| 137 | LONGS_EQUAL(memLeaks+1, memLeakDetector->totalMemoryLeaks(mem_leak_period_checking)); |
| 138 | free (memory); |
| 139 | } |
| 140 | |
| 141 | #ifdef CPPUTEST_USE_STRDUP_MACROS |
| 142 | |
| 143 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, StrdupOverrideIsUsed) |
| 144 | { |
| 145 | size_t memLeaks = memLeakDetector->totalMemoryLeaks(mem_leak_period_checking); |
| 146 | char* memory = strdup("0123456789"); |
| 147 | LONGS_EQUAL(memLeaks+1, memLeakDetector->totalMemoryLeaks(mem_leak_period_checking)); |
| 148 | free (memory); |
| 149 | } |
| 150 | |
| 151 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, StrndupOverrideIsUsed) |
| 152 | { |
| 153 | size_t memLeaks = memLeakDetector->totalMemoryLeaks(mem_leak_period_checking); |
| 154 | char* memory = strndup("0123456789", 10); |
| 155 | LONGS_EQUAL(memLeaks+1, memLeakDetector->totalMemoryLeaks(mem_leak_period_checking)); |
| 156 | free (memory); |
| 157 | } |
| 158 | #endif |
| 159 | |
| 160 | #endif |
| 161 | |
| 162 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, UseNativeMallocByTemporarlySwitchingOffMalloc) |
| 163 | { |
| 164 | size_t memLeaks = memLeakDetector->totalMemoryLeaks(mem_leak_period_checking); |
| 165 | #ifdef CPPUTEST_USE_MALLOC_MACROS |
| 166 | #undef malloc |
| 167 | #undef free |
| 168 | #endif |
| 169 | |
| 170 | #if CPPUTEST_USE_STD_C_LIB |
| 171 | void* memory = malloc(10); |
| 172 | LONGS_EQUAL(memLeaks, memLeakDetector->totalMemoryLeaks(mem_leak_period_checking)); |
| 173 | free (memory); |
| 174 | #else |
| 175 | void* memory = PlatformSpecificMalloc(10); |
| 176 | LONGS_EQUAL(memLeaks, memLeakDetector->totalMemoryLeaks(mem_leak_period_checking)); |
| 177 | PlatformSpecificFree (memory); |
| 178 | #endif |
| 179 | |
| 180 | #ifdef CPPUTEST_USE_MALLOC_MACROS |
| 181 | #include "CppUTest/MemoryLeakDetectorMallocMacros.h" |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | /* TEST... allowing for a new overload in a class */ |
| 186 | class NewDummyClass |
| 187 | { |
| 188 | public: |
| 189 | static bool overloaded_new_called; |
| 190 | |
| 191 | #ifdef CPPUTEST_USE_NEW_MACROS |
| 192 | #undef new |
| 193 | #endif |
| 194 | void* operator new (size_t size) |
| 195 | #ifdef CPPUTEST_USE_NEW_MACROS |
| 196 | #include "CppUTest/MemoryLeakDetectorNewMacros.h" |
| 197 | #endif |
| 198 | { |
| 199 | overloaded_new_called = true; |
| 200 | return malloc(size); |
| 201 | } |
| 202 | void dummyFunction() |
| 203 | { |
| 204 | char* memory = new char; |
| 205 | delete memory; |
| 206 | } |
| 207 | }; |
| 208 | bool NewDummyClass::overloaded_new_called = false; |
| 209 | |
| 210 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, NoSideEffectsFromTurningOffNewMacros) |
| 211 | { |
| 212 | /* |
| 213 | * Interesting effect of wrapping the operator new around the macro is |
| 214 | * that the actual new that is called is a different one than expected. |
| 215 | * |
| 216 | * The overloaded operator new doesn't actually ever get called. |
| 217 | * |
| 218 | * This might come as a surprise, so it is important to realize! |
| 219 | */ |
| 220 | NewDummyClass dummy; |
| 221 | dummy.dummyFunction(); |
| 222 | // CHECK(dummy.overloaded_new_called); |
| 223 | } |
| 224 | |
| 225 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, UseNativeNewByTemporarlySwitchingOffNew) |
| 226 | { |
| 227 | #ifdef CPPUTEST_USE_NEW_MACROS |
| 228 | #undef new |
| 229 | #undef delete |
| 230 | #endif |
| 231 | char* memory = new char[10]; |
| 232 | delete [] memory; |
| 233 | #ifdef CPPUTEST_USE_NEW_MACROS |
| 234 | #include "CppUTest/MemoryLeakDetectorNewMacros.h" |
| 235 | #endif |
| 236 | } |
| 237 | |
| 238 | |
| 239 | #if CPPUTEST_USE_MEM_LEAK_DETECTION |
| 240 | |
| 241 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, OperatorNewMacroOverloadViaIncludeFileWorks) |
| 242 | { |
| 243 | char* leak = newAllocation(); |
| 244 | STRCMP_NOCASE_CONTAINS("AllocationInCppFile.cpp", memLeakDetector->report(mem_leak_period_checking)); |
| 245 | delete leak; |
| 246 | } |
| 247 | |
| 248 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, OperatorNewArrayMacroOverloadViaIncludeFileWorks) |
| 249 | { |
| 250 | char* leak = newArrayAllocation(); |
| 251 | STRCMP_NOCASE_CONTAINS("AllocationInCppFile.cpp", memLeakDetector->report(mem_leak_period_checking)); |
| 252 | delete[] leak; |
| 253 | } |
| 254 | |
| 255 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, MallocOverrideWorks) |
| 256 | { |
| 257 | char* leak = mallocAllocation(); |
| 258 | STRCMP_NOCASE_CONTAINS("AllocationInCFile.c", memLeakDetector->report(mem_leak_period_checking)); |
| 259 | freeAllocation(leak); |
| 260 | } |
| 261 | |
| 262 | #ifdef CPPUTEST_USE_STRDUP_MACROS |
| 263 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, StrdupOverrideWorks) |
| 264 | { |
| 265 | char* leak = strdupAllocation(); |
| 266 | STRCMP_NOCASE_CONTAINS("AllocationInCFile.c", memLeakDetector->report(mem_leak_period_checking)); |
| 267 | freeAllocation(leak); |
| 268 | } |
| 269 | |
| 270 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, StrndupOverrideWorks) |
| 271 | { |
| 272 | char* leak = strndupAllocation(); |
| 273 | STRCMP_NOCASE_CONTAINS("AllocationInCFile.c", memLeakDetector->report(mem_leak_period_checking)); |
| 274 | freeAllocation(leak); |
| 275 | } |
| 276 | #endif |
| 277 | |
| 278 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, MallocWithButFreeWithoutLeakDetectionDoesntCrash) |
| 279 | { |
| 280 | char* leak = mallocAllocation(); |
| 281 | freeAllocationWithoutMacro(leak); |
| 282 | LONGS_EQUAL(2, memLeakDetector->totalMemoryLeaks(mem_leak_period_checking)); |
| 283 | memLeakDetector->removeMemoryLeakInformationWithoutCheckingOrDeallocatingTheMemoryButDeallocatingTheAccountInformation(getCurrentMallocAllocator(), leak, true); |
| 284 | } |
| 285 | |
| 286 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, OperatorNewOverloadingWithoutMacroWorks) |
| 287 | { |
| 288 | char* leak = newAllocationWithoutMacro(); |
| 289 | STRCMP_CONTAINS("unknown", memLeakDetector->report(mem_leak_period_checking)); |
| 290 | delete leak; |
| 291 | } |
| 292 | |
| 293 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, OperatorNewArrayOverloadingWithoutMacroWorks) |
| 294 | { |
| 295 | char* leak = newArrayAllocationWithoutMacro(); |
| 296 | STRCMP_CONTAINS("unknown", memLeakDetector->report(mem_leak_period_checking)); |
| 297 | delete[] leak; |
| 298 | } |
| 299 | |
| 300 | #else |
| 301 | |
| 302 | TEST(MemoryLeakOverridesToBeUsedInProductionCode, MemoryOverridesAreDisabled) |
| 303 | { |
| 304 | char* leak = newAllocation(); |
| 305 | STRCMP_EQUAL("No memory leaks were detected.", memLeakDetector->report(mem_leak_period_checking)); |
| 306 | delete leak; |
| 307 | } |
| 308 | |
| 309 | #endif |
| 310 | |
| 311 | TEST_GROUP(OutOfMemoryTestsForOperatorNew) |
| 312 | { |
| 313 | TestMemoryAllocator* no_memory_allocator; |
| 314 | GlobalMemoryAllocatorStash memoryAllocatorStash; |
| 315 | void setup() |
| 316 | { |
| 317 | memoryAllocatorStash.save(); |
| 318 | no_memory_allocator = new NullUnknownAllocator; |
| 319 | setCurrentNewAllocator(no_memory_allocator); |
| 320 | setCurrentNewArrayAllocator(no_memory_allocator); |
| 321 | } |
| 322 | |
| 323 | void teardown() |
| 324 | { |
| 325 | memoryAllocatorStash.restore(); |
| 326 | delete no_memory_allocator; |
| 327 | } |
| 328 | }; |
| 329 | |
| 330 | #if CPPUTEST_USE_MEM_LEAK_DETECTION |
| 331 | |
| 332 | #if CPPUTEST_USE_STD_CPP_LIB |
| 333 | |
| 334 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewOperatorThrowsAnExceptionWhenUsingStdCppNew) |
| 335 | { |
| 336 | CHECK_THROWS(std::bad_alloc, new char); |
| 337 | } |
| 338 | |
| 339 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewArrayOperatorThrowsAnExceptionWhenUsingStdCppNew) |
| 340 | { |
| 341 | CHECK_THROWS(std::bad_alloc, new char[10]); |
| 342 | } |
| 343 | |
| 344 | TEST_GROUP(TestForExceptionsInConstructor) |
| 345 | { |
| 346 | }; |
| 347 | |
| 348 | TEST(TestForExceptionsInConstructor,ConstructorThrowsAnException) |
| 349 | { |
| 350 | CHECK_THROWS(int, new ClassThatThrowsAnExceptionInTheConstructor); |
| 351 | } |
| 352 | |
| 353 | TEST(TestForExceptionsInConstructor,ConstructorThrowsAnExceptionAllocatedAsArray) |
| 354 | { |
| 355 | CHECK_THROWS(int, new ClassThatThrowsAnExceptionInTheConstructor[10]); |
| 356 | } |
| 357 | |
| 358 | #else |
| 359 | |
| 360 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewOperatorReturnsNull) |
| 361 | { |
| 362 | POINTERS_EQUAL(NULLPTR, new char); |
| 363 | } |
| 364 | |
| 365 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewArrayOperatorReturnsNull) |
| 366 | { |
| 367 | POINTERS_EQUAL(NULLPTR, new char[10]); |
| 368 | } |
| 369 | |
| 370 | #endif |
| 371 | |
| 372 | #undef new |
| 373 | |
| 374 | #if CPPUTEST_USE_STD_CPP_LIB |
| 375 | |
| 376 | |
| 377 | /* |
| 378 | * CLang 4.2 and memory allocation. |
| 379 | * |
| 380 | * Clang 4.2 has done some optimizations to their memory management that actually causes slightly different behavior than what the C++ Standard defines. |
| 381 | * Usually this is not a problem... but in this case, it is a problem. |
| 382 | * |
| 383 | * More information about the optimization can be found at: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3433.html |
| 384 | * We've done a bug-report to clang to fix some of this non-standard behavior, which is open at: http://llvm.org/bugs/show_bug.cgi?id=15541 |
| 385 | * |
| 386 | * I very much hope that nobody would actually ever hit this bug/optimization as it is hard to figure out what is going on. |
| 387 | * |
| 388 | * The original test simply did "new char". Because the memory wasn't assigned to anything and is local in context, the optimization *doesn't* call |
| 389 | * the operator new overload. Because it doesn't call the operator new (optimizing away a call to operator new), therefore the method wouldn't throw an exception |
| 390 | * and therefore this test failed. |
| 391 | * |
| 392 | * The first attempt to fix this is to create a local variable and assigned the memory to that. Also this doesn't work as it still detects the allocation is |
| 393 | * local and optimizes away the memory call. |
| 394 | * |
| 395 | * Now, we assign the memory on some static global which fools the optimizer to believe that it isn't local and it stops optimizing the operator new call. |
| 396 | * |
| 397 | * We (Bas Vodde and Terry Yin) suspect that in a real product, you wouldn't be able to detect the optimization and it's breaking of Standard C++. Therefore, |
| 398 | * for now, we keep this hack in the test to fool the optimizer and hope nobody will ever notice this 'optimizer behavior' in a real product. |
| 399 | * |
| 400 | * Update 2020: The gcc compiler implemented the same optimization, but it seems to be slightly smarter and discovered that we assign to a static variable. |
| 401 | * Thus it still optimized away the call to operator new. Did another bug report, but it is unlikely to get fixed. You can find it at: |
| 402 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94671 |
| 403 | * |
| 404 | * Changed the variable to be external so it would definitively be a mistake to optimize the call. |
| 405 | * |
| 406 | */ |
| 407 | |
| 408 | extern char* some_memory; |
| 409 | char* some_memory; |
| 410 | |
| 411 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewOperatorThrowsAnExceptionWhenUsingStdCppNewWithoutOverride) |
| 412 | { |
| 413 | CHECK_THROWS(std::bad_alloc, some_memory = new char); |
| 414 | } |
| 415 | |
| 416 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewArrayOperatorThrowsAnExceptionWhenUsingStdCppNewWithoutOverride) |
| 417 | { |
| 418 | CHECK_THROWS(std::bad_alloc, some_memory = new char[10]); |
| 419 | } |
| 420 | |
| 421 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewOperatorReturnsNullWithoutOverride) |
| 422 | { |
| 423 | POINTERS_EQUAL(NULLPTR, new (std::nothrow) char); |
| 424 | } |
| 425 | |
| 426 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewArrayOperatorReturnsNullWithoutOverride) |
| 427 | { |
| 428 | POINTERS_EQUAL(NULLPTR, new (std::nothrow) char[10]); |
| 429 | } |
| 430 | |
| 431 | #else |
| 432 | |
| 433 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewOperatorReturnsNullWithoutOverride) |
| 434 | { |
| 435 | POINTERS_EQUAL(NULLPTR, new char); |
| 436 | } |
| 437 | |
| 438 | TEST(OutOfMemoryTestsForOperatorNew, FailingNewArrayOperatorReturnsNullWithoutOverride) |
| 439 | { |
| 440 | POINTERS_EQUAL(NULLPTR, new char[10]); |
| 441 | } |
| 442 | |
| 443 | #endif |
| 444 | |
| 445 | #endif |