fuzz_test.cpp
5.58 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
// -*- C++ -*-
//===------------------------- fuzz_test.cpp ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// A simple program for running regressions on the fuzzing routines.
// This code is not part of any shipping product.
//
// To build:
// clang++ -std=c++11 fuzz_test.cpp fuzzing.cpp
//
// To use:
// fuzz_test -r partial_sort [-v] files...
//
// Each file should contain a test case.
// TODO: should add some memory tracking, too.
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <map>
#include <chrono>
#include "fuzzing.h"
// ==== Count memory allocations ====
struct MemoryCounters {
size_t totalAllocationCount;
size_t netAllocationCount;
size_t totalBytesAllocated;
};
MemoryCounters gMemoryCounters;
void ZeroMemoryCounters() {
gMemoryCounters.totalAllocationCount = 0;
gMemoryCounters.netAllocationCount = 0;
gMemoryCounters.totalBytesAllocated = 0;
}
void* operator new(std::size_t size)
{
if (size == 0) size = 1;
void *p = ::malloc(size);
if (p == NULL)
throw std::bad_alloc();
gMemoryCounters.totalAllocationCount += 1;
gMemoryCounters.netAllocationCount += 1;
gMemoryCounters.totalBytesAllocated += size;
return p;
}
void* operator new(std::size_t size, const std::nothrow_t&) noexcept
{
try { return operator new(size); }
catch (const std::bad_alloc &) {}
return nullptr;
}
void* operator new[](std::size_t size)
{
return ::operator new(size);
}
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept
{
try { return operator new(size); }
catch (const std::bad_alloc &) {}
return nullptr;
}
void operator delete(void* ptr) noexcept
{
if (ptr)
::free(ptr);
gMemoryCounters.netAllocationCount -= 1;
}
void operator delete(void* ptr, const std::nothrow_t&) noexcept
{
::operator delete(ptr);
}
void operator delete[](void* ptr) noexcept
{
::operator delete(ptr);
}
void operator delete[](void* ptr, const std::nothrow_t&) noexcept
{
::operator delete(ptr);
}
// ==== End count memory allocations ====
typedef int (*FuzzProc) (const uint8_t *data, size_t size);
const std::map<std::string, FuzzProc> procs = {
{"sort", fuzzing::sort},
{"stable_sort", fuzzing::stable_sort},
{"partition", fuzzing::partition},
{"partition_copy", fuzzing::partition_copy},
{"stable_partition", fuzzing::stable_partition},
{"unique", fuzzing::unique},
{"unique_copy", fuzzing::unique_copy},
{"nth_element", fuzzing::nth_element},
{"partial_sort", fuzzing::partial_sort},
{"partial_sort_copy", fuzzing::partial_sort_copy},
{"make_heap", fuzzing::make_heap},
{"push_heap", fuzzing::push_heap},
{"pop_heap", fuzzing::pop_heap},
{"regex_ECMAScript", fuzzing::regex_ECMAScript},
{"regex_POSIX", fuzzing::regex_POSIX},
{"regex_extended", fuzzing::regex_extended},
{"regex_awk", fuzzing::regex_awk},
{"regex_grep", fuzzing::regex_grep},
{"regex_egrep", fuzzing::regex_egrep},
{"search", fuzzing::search}
};
bool verbose = false;
void test_one(const char *filename, FuzzProc fp)
{
std::vector<uint8_t> v;
std::ifstream f (filename, std::ios::binary);
if (!f.is_open())
std::cerr << "## Can't open '" << filename << "'" << std::endl;
else
{
typedef std::istream_iterator<uint8_t> Iter;
std::copy(Iter(f), Iter(), std::back_inserter(v));
if (verbose)
std::cout << "File '" << filename << "' contains " << v.size() << " entries" << std::endl;
ZeroMemoryCounters();
const auto start_time = std::chrono::high_resolution_clock::now();
int ret = fp (v.data(), v.size());
const auto finish_time = std::chrono::high_resolution_clock::now();
MemoryCounters mc = gMemoryCounters;
if (ret != 0)
std::cerr << "## Failure code: " << ret << std::endl;
if (verbose)
{
std::cout << "Execution time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(finish_time - start_time).count()
<< " milliseconds" << std::endl;
std::cout << "Memory: "
<< mc.totalBytesAllocated << " bytes allocated ("
<< mc.totalAllocationCount << " allocations); "
<< mc.netAllocationCount << " allocations remain" << std::endl;
}
}
}
void usage (const char *name)
{
std::cout << "Usage: " << name << " -r proc [-v] files..." << std::endl;
std::cout << "Supported routines:" << std::endl;
for (const auto &p : procs)
std::cout << " " << p.first << std::endl;
std::cout << std::endl;
}
// Poor man's command-line options
const std::string dashR("-r");
const std::string dashV("-v");
int main(int argc, char *argv[])
{
if (argc < 4 || dashR != argv[1] || procs.find(argv[2]) == procs.end())
usage(argv[0]);
else {
FuzzProc fp = procs.find(argv[2])->second;
int firstFile = 3;
if (dashV == argv[firstFile])
{
verbose = true;
++firstFile;
}
for (int i = firstFile; i < argc; ++i)
test_one(argv[i], fp);
}
}