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