Skip to content

Commit 1885052

Browse files
committed
Add memory tracking
llvm-svn: 322872
1 parent 1bc2ce6 commit 1885052

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

libcxx/fuzzing/fuzz_test.cpp

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
//
2020
// Each file should contain a test case.
2121

22+
// TODO: should add some memory tracking, too.
23+
2224

2325
#include <iostream>
2426
#include <fstream>
@@ -29,6 +31,78 @@
2931

3032
#include "fuzzing.h"
3133

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+
32106
typedef int (*FuzzProc) (const uint8_t *data, size_t size);
33107

34108
const std::map<std::string, FuzzProc> procs = {
@@ -64,21 +138,30 @@ void test_one(const char *filename, FuzzProc fp)
64138
std::ifstream f (filename, std::ios::binary);
65139
if (!f.is_open())
66140
std::cerr << "## Can't open '" << filename << "'" << std::endl;
67-
else {
141+
else
142+
{
68143
typedef std::istream_iterator<uint8_t> Iter;
69144
std::copy(Iter(f), Iter(), std::back_inserter(v));
70145
if (verbose)
71146
std::cout << "File '" << filename << "' contains " << v.size() << " entries" << std::endl;
147+
ZeroMemoryCounters();
72148
const auto start_time = std::chrono::high_resolution_clock::now();
73149
int ret = fp (v.data(), v.size());
74150
const auto finish_time = std::chrono::high_resolution_clock::now();
151+
MemoryCounters mc = gMemoryCounters;
75152
if (ret != 0)
76153
std::cerr << "## Failure code: " << ret << std::endl;
77154
if (verbose)
155+
{
78156
std::cout << "Execution time: "
79157
<< std::chrono::duration_cast<std::chrono::milliseconds>(finish_time - start_time).count()
80158
<< " milliseconds" << std::endl;
159+
std::cout << "Memory: "
160+
<< mc.totalBytesAllocated << " bytes allocated ("
161+
<< mc.totalAllocationCount << " allocations); "
162+
<< mc.netAllocationCount << " allocations remain" << std::endl;
81163
}
164+
}
82165
}
83166

84167
void usage (const char *name)

0 commit comments

Comments
 (0)