|
19 | 19 | //
|
20 | 20 | // Each file should contain a test case.
|
21 | 21 |
|
| 22 | +// TODO: should add some memory tracking, too. |
| 23 | + |
22 | 24 |
|
23 | 25 | #include <iostream>
|
24 | 26 | #include <fstream>
|
|
29 | 31 |
|
30 | 32 | #include "fuzzing.h"
|
31 | 33 |
|
| 34 | +// ==== Count memory allocations ==== |
| 35 | + |
| 36 | +struct MemoryCounters { |
| 37 | + size_t totalAllocationCount; |
| 38 | + size_t netAllocationCount; |
| 39 | + size_t totalBytesAllocated; |
| 40 | + }; |
| 41 | + |
| 42 | +MemoryCounters gMemoryCounters; |
| 43 | + |
| 44 | +void ZeroMemoryCounters() { |
| 45 | + gMemoryCounters.totalAllocationCount = 0; |
| 46 | + gMemoryCounters.netAllocationCount = 0; |
| 47 | + gMemoryCounters.totalBytesAllocated = 0; |
| 48 | +} |
| 49 | + |
| 50 | +void* operator new(std::size_t size) |
| 51 | +{ |
| 52 | + if (size == 0) size = 1; |
| 53 | + void *p = ::malloc(size); |
| 54 | + if (p == NULL) |
| 55 | + throw std::bad_alloc(); |
| 56 | + gMemoryCounters.totalAllocationCount += 1; |
| 57 | + gMemoryCounters.netAllocationCount += 1; |
| 58 | + gMemoryCounters.totalBytesAllocated += size; |
| 59 | + return p; |
| 60 | +} |
| 61 | + |
| 62 | +void* operator new(std::size_t size, const std::nothrow_t&) noexcept |
| 63 | +{ |
| 64 | + try { return operator new(size); } |
| 65 | + catch (const std::bad_alloc &) {} |
| 66 | + return nullptr; |
| 67 | +} |
| 68 | + |
| 69 | +void* operator new[](std::size_t size) |
| 70 | +{ |
| 71 | + return ::operator new(size); |
| 72 | +} |
| 73 | + |
| 74 | +void* operator new[](std::size_t size, const std::nothrow_t&) noexcept |
| 75 | +{ |
| 76 | + try { return operator new(size); } |
| 77 | + catch (const std::bad_alloc &) {} |
| 78 | + return nullptr; |
| 79 | +} |
| 80 | + |
| 81 | +void operator delete(void* ptr) noexcept |
| 82 | +{ |
| 83 | + if (ptr) |
| 84 | + ::free(ptr); |
| 85 | + gMemoryCounters.netAllocationCount -= 1; |
| 86 | +} |
| 87 | + |
| 88 | +void operator delete(void* ptr, const std::nothrow_t&) noexcept |
| 89 | +{ |
| 90 | + ::operator delete(ptr); |
| 91 | +} |
| 92 | + |
| 93 | +void operator delete[](void* ptr) noexcept |
| 94 | +{ |
| 95 | + ::operator delete(ptr); |
| 96 | +} |
| 97 | + |
| 98 | +void operator delete[](void* ptr, const std::nothrow_t&) noexcept |
| 99 | +{ |
| 100 | + ::operator delete(ptr); |
| 101 | +} |
| 102 | + |
| 103 | +// ==== End count memory allocations ==== |
| 104 | + |
| 105 | + |
32 | 106 | typedef int (*FuzzProc) (const uint8_t *data, size_t size);
|
33 | 107 |
|
34 | 108 | const std::map<std::string, FuzzProc> procs = {
|
@@ -64,21 +138,30 @@ void test_one(const char *filename, FuzzProc fp)
|
64 | 138 | std::ifstream f (filename, std::ios::binary);
|
65 | 139 | if (!f.is_open())
|
66 | 140 | std::cerr << "## Can't open '" << filename << "'" << std::endl;
|
67 |
| - else { |
| 141 | + else |
| 142 | + { |
68 | 143 | typedef std::istream_iterator<uint8_t> Iter;
|
69 | 144 | std::copy(Iter(f), Iter(), std::back_inserter(v));
|
70 | 145 | if (verbose)
|
71 | 146 | std::cout << "File '" << filename << "' contains " << v.size() << " entries" << std::endl;
|
| 147 | + ZeroMemoryCounters(); |
72 | 148 | const auto start_time = std::chrono::high_resolution_clock::now();
|
73 | 149 | int ret = fp (v.data(), v.size());
|
74 | 150 | const auto finish_time = std::chrono::high_resolution_clock::now();
|
| 151 | + MemoryCounters mc = gMemoryCounters; |
75 | 152 | if (ret != 0)
|
76 | 153 | std::cerr << "## Failure code: " << ret << std::endl;
|
77 | 154 | if (verbose)
|
| 155 | + { |
78 | 156 | std::cout << "Execution time: "
|
79 | 157 | << std::chrono::duration_cast<std::chrono::milliseconds>(finish_time - start_time).count()
|
80 | 158 | << " milliseconds" << std::endl;
|
| 159 | + std::cout << "Memory: " |
| 160 | + << mc.totalBytesAllocated << " bytes allocated (" |
| 161 | + << mc.totalAllocationCount << " allocations); " |
| 162 | + << mc.netAllocationCount << " allocations remain" << std::endl; |
81 | 163 | }
|
| 164 | + } |
82 | 165 | }
|
83 | 166 |
|
84 | 167 | void usage (const char *name)
|
|
0 commit comments