summaryrefslogtreecommitdiff
path: root/fuzzing/fuzzing.cpp
blob: cc349cdee69e21e577f85978a4666f9d61408f17 (plain)
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
// -*- C++ -*-
//===------------------------- fuzzing.cpp -------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

//	A set of routines to use when fuzzing the algorithms in libc++
//	Each one tests a single algorithm.
//
//	They all have the form of:
//		int `algorithm`(const uint8_t *data, size_t size);
//
//	They perform the operation, and then check to see if the results are correct.
//	If so, they return zero, and non-zero otherwise.
//
//	For example, sort calls std::sort, then checks two things:
//		(1) The resulting vector is sorted
//		(2) The resulting vector contains the same elements as the original data.



#include "fuzzing.h"
#include <vector>
#include <algorithm>

#include <iostream>

//	If we had C++14, we could use the four iterator version of is_permutation

namespace fuzzing {

//	This is a struct we can use to test the stable_XXX algorithms.
//	perform the operation on the key, then check the order of the payload.

struct stable_test {
	uint8_t key;
	uint8_t payload;
	
	stable_test(uint8_t k) : key(k), payload(0) {}
	stable_test(uint8_t k, uint8_t p) : key(k), payload(p) {}
	};

void swap(stable_test &lhs, stable_test &rhs)
{
	using std::swap;
	swap(lhs.key,     rhs.key);
	swap(lhs.payload, rhs.payload);
}

struct key_less
{
	bool operator () (const stable_test &lhs, const stable_test &rhs) const
	{
		return lhs.key < rhs.key;
	}
};

struct payload_less
{
	bool operator () (const stable_test &lhs, const stable_test &rhs) const
	{
		return lhs.payload < rhs.payload;
	}
};

struct total_less
{
	bool operator () (const stable_test &lhs, const stable_test &rhs) const
	{
		return lhs.key == rhs.key ? lhs.payload < rhs.payload : lhs.key < rhs.key;
	}
};

bool operator==(const stable_test &lhs, const stable_test &rhs)
{ 
	return lhs.key == rhs.key && lhs.payload == rhs.payload;
}


template<typename T>
struct is_even
{
	bool operator () (const T &t) const
	{
		return t % 2 == 0;
	}
};


template<>
struct is_even<stable_test>
{
	bool operator () (const stable_test &t) const
	{
		return t.key % 2 == 0;
	}
};

//	== sort ==

int sort(const uint8_t *data, size_t size)
{
	std::vector<uint8_t> working(data, data + size);
	std::sort(working.begin(), working.end());

	if (!std::is_sorted(working.begin(), working.end())) return 1;
	if (!std::is_permutation(data, data + size, working.begin())) return 99;
	return 0;
}


//	== stable_sort ==

int stable_sort(const uint8_t *data, size_t size)
{
	std::vector<stable_test> input;
	for (size_t i = 0; i < size; ++i)
		input.push_back(stable_test(data[i], i));
	std::vector<stable_test> working = input;
	std::stable_sort(working.begin(), working.end(), key_less());

	if (!std::is_sorted(working.begin(), working.end(), key_less()))   return 1;
	auto iter = working.begin();
	while (iter != working.end())
	{
		auto range = std::equal_range(iter, working.end(), *iter, key_less());
		if (!std::is_sorted(range.first, range.second, total_less())) return 2;			
		iter = range.second;
	}
	if (!std::is_permutation(input.begin(), input.end(), working.begin())) return 99;
	return 0;
}

//	== partition ==

int partition(const uint8_t *data, size_t size)
{
	std::vector<uint8_t> working(data, data + size);
	auto iter = std::partition(working.begin(), working.end(), is_even<uint8_t>());

	if (!std::all_of (working.begin(), iter, is_even<uint8_t>())) return 1;
	if (!std::none_of(iter,   working.end(), is_even<uint8_t>())) return 2;
	if (!std::is_permutation(data, data + size, working.begin())) return 99;
	return 0;
}


//	== stable_partition ==

int stable_partition (const uint8_t *data, size_t size)
{
	std::vector<stable_test> input;
	for (size_t i = 0; i < size; ++i)
		input.push_back(stable_test(data[i], i));
	std::vector<stable_test> working = input;
	auto iter = std::stable_partition(working.begin(), working.end(), is_even<stable_test>());

	if (!std::all_of (working.begin(), iter, is_even<stable_test>())) return 1;
	if (!std::none_of(iter,   working.end(), is_even<stable_test>())) return 2;
	if (!std::is_sorted(working.begin(), iter, payload_less()))   return 3;
	if (!std::is_sorted(iter,   working.end(), payload_less()))   return 4;
	if (!std::is_permutation(input.begin(), input.end(), working.begin())) return 99;
	return 0;
}

//	== nth_element ==
//	use the first element as a position into the data
int nth_element (const uint8_t *data, size_t size)
{
	if (size <= 1) return 0;
	const size_t partition_point = data[0] % size;	
	std::vector<uint8_t> working(data + 1, data + size);
	const auto partition_iter = working.begin() + partition_point;
	std::nth_element(working.begin(), partition_iter, working.end());

//	nth may be the end iterator, in this case nth_element has no effect.
	if (partition_iter == working.end())
	{
		if (!std::equal(data + 1, data + size, working.begin())) return 98;
	}
	else
	{
		const uint8_t nth = *partition_iter;
		if (!std::all_of(working.begin(), partition_iter, [=](uint8_t v) { return v <= nth; }))
			return 1;
		if (!std::all_of(partition_iter, working.end(),   [=](uint8_t v) { return v >= nth; }))
			return 2;
		if (!std::is_permutation(data + 1, data + size, working.begin())) return 99;
		}

	return 0;
}

//	== partial_sort ==
//	use the first element as a position into the data
int partial_sort (const uint8_t *data, size_t size)
{
	if (size <= 1) return 0;
	const size_t sort_point = data[0] % size;
	std::vector<uint8_t> working(data + 1, data + size);
	const auto sort_iter = working.begin() + sort_point;
	std::partial_sort(working.begin(), sort_iter, working.end());

	if (sort_iter != working.end())
	{
		const uint8_t nth = *std::min_element(sort_iter, working.end());
		if (!std::all_of(working.begin(), sort_iter, [=](uint8_t v) { return v <= nth; }))
			return 1;
		if (!std::all_of(sort_iter, working.end(),   [=](uint8_t v) { return v >= nth; }))
			return 2;		
	}
	if (!std::is_sorted(working.begin(), sort_iter)) return 3;
	if (!std::is_permutation(data + 1, data + size, working.begin())) return 99;

	return 0;
}

} // namespace fuzzing