-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathsnmalloc-fuzzer.cpp
More file actions
228 lines (206 loc) · 5.52 KB
/
Copy pathsnmalloc-fuzzer.cpp
File metadata and controls
228 lines (206 loc) · 5.52 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
#include "fuzztest/fuzztest.h"
#include "snmalloc/snmalloc.h"
#include <cstddef>
#include <cstdlib>
#include <execution>
#include <new>
#include <string_view>
#include <vector>
void simple_memcpy(std::vector<char> data)
{
std::vector<char> dest(data.size());
snmalloc::memcpy<true>(dest.data(), data.data(), data.size());
if (data != dest)
abort();
}
void memcpy_with_align_offset(
size_t source_alignment,
size_t source_offset,
size_t dest_alignment,
size_t dest_offset,
std::string data)
{
source_alignment = 1 << source_alignment;
dest_alignment = 1 << dest_alignment;
source_offset = source_offset % source_alignment;
dest_offset = dest_offset % dest_alignment;
auto src_ = ::operator new(
data.size() + source_offset, std::align_val_t{source_alignment});
auto dst_ =
::operator new(data.size() + dest_offset, std::align_val_t{dest_alignment});
auto src = static_cast<char*>(src_) + source_offset;
auto dst = static_cast<char*>(dst_) + dest_offset;
snmalloc::memcpy<true>(src, data.data(), data.size());
snmalloc::memcpy<true>(dst, src, data.size());
if (std::string_view(dst, data.size()) != data)
abort();
::operator delete(src_, std::align_val_t{source_alignment});
::operator delete(dst_, std::align_val_t{dest_alignment});
}
void simple_memmove(std::vector<char> data)
{
std::vector<char> dest(data.size());
snmalloc::memmove<true>(dest.data(), data.data(), data.size());
if (data != dest)
abort();
}
void forward_memmove(std::string data, size_t offset)
{
std::string to_move = data;
offset = std::min(offset, data.size());
snmalloc::memmove<true>(
to_move.data() + offset, to_move.data(), to_move.size() - offset);
size_t after_move = to_move.size() - offset;
if (
std::string_view(data.data(), after_move) !=
std::string_view(to_move.data() + offset, after_move))
abort();
}
void backward_memmove(std::string data, size_t offset)
{
std::string to_move = data;
offset = std::min(offset, data.size());
snmalloc::memmove<true>(
to_move.data(), to_move.data() + offset, to_move.size() - offset);
size_t after_move = to_move.size() - offset;
if (
std::string_view(data.data() + offset, after_move) !=
std::string_view(to_move.data(), after_move))
abort();
}
constexpr static size_t size_limit = 16384;
enum class EventKind : unsigned
{
AllocZero = 0,
AllocNoZero = 1,
Free = 2,
Check = 3,
ReFill = 4,
};
struct Event
{
EventKind kind;
size_t size_or_index;
char filler;
Event(std::tuple<unsigned, size_t, char> payload)
: kind(static_cast<EventKind>(std::get<0>(payload) % 5)),
size_or_index(std::get<1>(payload)),
filler(std::get<2>(payload))
{
if (kind == EventKind::AllocZero || kind == EventKind::AllocNoZero)
{
size_or_index %= size_limit;
}
}
};
struct Result
{
char filler;
char* ptr;
size_t size;
Result(char filler, char* ptr, size_t size)
: filler(filler), ptr(ptr), size(size)
{}
Result(Result&& other) noexcept
: filler(other.filler), ptr(other.ptr), size(other.size)
{
other.ptr = nullptr;
}
Result& operator=(Result&& other) noexcept
{
if (this != &other)
{
filler = other.filler;
ptr = other.ptr;
size = other.size;
other.ptr = nullptr;
}
return *this;
}
void check()
{
auto res = std::reduce(
std::execution::unseq,
ptr,
ptr + size,
static_cast<unsigned char>(0),
[&](unsigned char acc, char c) -> unsigned char {
return acc | static_cast<unsigned char>(c != filler);
});
if (res)
abort();
}
~Result()
{
auto alloc = snmalloc::get_scoped_allocator();
if (ptr)
alloc->dealloc(ptr);
ptr = nullptr;
}
};
void snmalloc_random_walk(
std::vector<std::tuple<unsigned, size_t, char>> payload)
{
std::vector<Result> results;
for (auto& p : payload)
{
Event e(p);
auto scoped = snmalloc::get_scoped_allocator();
switch (e.kind)
{
case EventKind::AllocZero:
{
auto ptr =
static_cast<char*>(scoped->alloc<snmalloc::Zero>(e.size_or_index));
results.emplace_back(0, ptr, e.size_or_index);
break;
}
case EventKind::AllocNoZero:
{
auto ptr =
static_cast<char*>(scoped->alloc<snmalloc::Uninit>(e.size_or_index));
std::fill(ptr, ptr + e.size_or_index, e.filler);
results.emplace_back(e.filler, ptr, e.size_or_index);
break;
}
case EventKind::Free:
{
if (results.empty())
break;
auto index = e.size_or_index % results.size();
results.erase(results.begin() + static_cast<ptrdiff_t>(index));
break;
}
case EventKind::Check:
{
for (auto& r : results)
r.check();
break;
}
case EventKind::ReFill:
{
if (results.empty())
break;
auto index = e.size_or_index % results.size();
std::fill(
results[index].ptr,
results[index].ptr + results[index].size,
e.filler);
results[index].filler = e.filler;
break;
}
}
}
}
FUZZ_TEST(snmalloc_fuzzing, simple_memcpy);
FUZZ_TEST(snmalloc_fuzzing, simple_memmove);
FUZZ_TEST(snmalloc_fuzzing, forward_memmove);
FUZZ_TEST(snmalloc_fuzzing, backward_memmove);
FUZZ_TEST(snmalloc_fuzzing, memcpy_with_align_offset)
.WithDomains(
fuzztest::InRange(0, 6),
fuzztest::Positive<size_t>(),
fuzztest::InRange(0, 6),
fuzztest::Positive<size_t>(),
fuzztest::String());
FUZZ_TEST(snmalloc_fuzzing, snmalloc_random_walk);