forked from google/fuzztest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cc
424 lines (379 loc) · 14.8 KB
/
util.cc
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2022 The Centipede Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO(ussuri): This module has become a catch-all for all sorts of utils.
// Split it by category.
#include "./centipede/util.h"
#include <linux/limits.h> // NOLINT(PATH_MAX)
#include <sys/mman.h>
#include <unistd.h>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio> // NOLINT(popen)
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <filesystem> // NOLINT
#include <fstream>
#include <functional>
#include <ios>
#include <queue>
#include <random>
#include <sstream>
#include <string>
#include <string_view>
#include <thread> // NOLINT(build/c++11)
#include <utility>
#include <vector>
#include "absl/base/attributes.h"
#include "absl/base/const_init.h"
#include "absl/base/thread_annotations.h"
#include "absl/log/check.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/span.h"
#include "./centipede/defs.h"
#include "./centipede/feature.h"
#include "./centipede/logging.h"
#include "./centipede/remote_file.h"
namespace centipede {
static_assert(kPathMax >= PATH_MAX, "kPathMax is too small.");
size_t GetRandomSeed(size_t seed) {
if (seed != 0) return seed;
return time(nullptr) + getpid() +
std::hash<std::thread::id>{}(std::this_thread::get_id());
}
std::string AsString(const ByteArray &data, size_t max_len) {
std::ostringstream out;
size_t len = std::min(max_len, data.size());
for (size_t i = 0; i < len; ++i) {
const auto ch = data[i];
if (std::isprint(ch)) {
out << ch;
} else {
out << "\\x" << std::uppercase << std::hex << static_cast<uint32_t>(ch);
}
}
return out.str();
}
template <typename Container>
void ReadFromLocalFile(std::string_view file_path, Container &data) {
std::ifstream f(std::string{file_path});
if (!f) return;
f.seekg(0, std::ios_base::end);
auto size = f.tellg();
f.seekg(0, std::ios_base::beg);
CHECK_EQ(size % sizeof(data[0]), 0);
data.resize(size / sizeof(data[0]));
f.read(reinterpret_cast<char *>(data.data()), size);
CHECK(f) << "Failed to read from local file: " << VV(file_path) << VV(f.eof())
<< VV(f.bad()) << VV(f.fail()) << VV(size);
f.close();
}
void ReadFromLocalFile(std::string_view file_path, std::string &data) {
return ReadFromLocalFile<std::string>(file_path, data);
}
void ReadFromLocalFile(std::string_view file_path, ByteArray &data) {
return ReadFromLocalFile<ByteArray>(file_path, data);
}
void ReadFromLocalFile(std::string_view file_path, FeatureVec &data) {
return ReadFromLocalFile<FeatureVec>(file_path, data);
}
void ReadFromLocalFile(std::string_view file_path,
std::vector<uint32_t> &data) {
return ReadFromLocalFile<std::vector<uint32_t> &>(file_path, data);
}
void WriteToLocalFile(std::string_view file_path, ByteSpan data) {
std::ofstream f(std::string{file_path.data()});
CHECK(f) << "Failed to open local file: " << file_path;
f.write(reinterpret_cast<const char *>(data.data()),
static_cast<int64_t>(data.size()));
CHECK(f) << "Failed to write to local file: " << file_path;
f.close();
}
void WriteToLocalFile(std::string_view file_path, std::string_view data) {
static_assert(sizeof(decltype(data)::value_type) == sizeof(uint8_t));
WriteToLocalFile(
file_path,
ByteSpan(reinterpret_cast<const uint8_t *>(data.data()), data.size()));
}
void WriteToLocalFile(std::string_view file_path, const FeatureVec &data) {
WriteToLocalFile(file_path,
ByteSpan(reinterpret_cast<const uint8_t *>(data.data()),
sizeof(data[0]) * data.size()));
}
void WriteToLocalHashedFileInDir(std::string_view dir_path, ByteSpan data) {
if (dir_path.empty()) return;
std::string file_path = std::filesystem::path(dir_path).append(Hash(data));
WriteToLocalFile(file_path, data);
}
void WriteToRemoteHashedFileInDir(std::string_view dir_path, ByteSpan data) {
if (dir_path.empty()) return;
std::string file_path = std::filesystem::path(dir_path).append(Hash(data));
RemoteFileSetContents(file_path, std::string(data.begin(), data.end()));
}
std::string HashOfFileContents(std::string_view file_path) {
if (file_path.empty()) return "";
std::string file_contents;
RemoteFileGetContents(std::filesystem::path(file_path), file_contents);
return Hash(file_contents);
}
std::string ProcessAndThreadUniqueID(std::string_view prefix) {
// operator << is the only way to serialize std::this_thread::get_id().
std::ostringstream oss;
oss << prefix << getpid() << "-" << std::this_thread::get_id();
return oss.str();
}
std::string TemporaryLocalDirPath() {
const char *TMPDIR = getenv("TMPDIR");
std::string tmp = TMPDIR ? TMPDIR : "/tmp";
return std::filesystem::path(tmp).append(
ProcessAndThreadUniqueID("centipede-"));
}
// We need to maintain a global set of dirs that CreateLocalDirRemovedAtExit()
// was called with, so that we can remove all these dirs at exit.
ABSL_CONST_INIT static absl::Mutex dirs_to_delete_at_exit_mutex{
absl::kConstInit};
static std::vector<std::string> *dirs_to_delete_at_exit
ABSL_GUARDED_BY(dirs_to_delete_at_exit_mutex);
// Atexit handler added by CreateLocalDirRemovedAtExit().
// Deletes all dirs in dirs_to_delete_at_exit.
static void RemoveDirsAtExit() {
absl::MutexLock lock(&dirs_to_delete_at_exit_mutex);
for (auto &dir : *dirs_to_delete_at_exit) {
std::filesystem::remove_all(dir);
}
}
void CreateLocalDirRemovedAtExit(std::string_view path) {
// Safeguard against removing dirs not created by TemporaryLocalDirPath().
CHECK_NE(path.find("/centipede-"), std::string::npos);
// Create the dir.
std::filesystem::remove_all(path);
std::filesystem::create_directories(path);
// Add to dirs_to_delete_at_exit.
absl::MutexLock lock(&dirs_to_delete_at_exit_mutex);
if (!dirs_to_delete_at_exit) {
dirs_to_delete_at_exit = new std::vector<std::string>();
atexit(&RemoveDirsAtExit);
}
dirs_to_delete_at_exit->emplace_back(path);
}
ScopedFile::ScopedFile(std::string_view dir_path, std::string_view name)
: my_path_(std::filesystem::path(dir_path) / name) {}
ScopedFile::~ScopedFile() { std::filesystem::remove_all(my_path_); }
static const size_t kMagicLen = 11;
static const uint8_t kPackBegMagic[] = "-Centipede-";
static const uint8_t kPackEndMagic[] = "-edepitneC-";
static_assert(sizeof(kPackBegMagic) == kMagicLen + 1);
static_assert(sizeof(kPackEndMagic) == kMagicLen + 1);
// Pack 'data' such that it can be appended to a file and later extracted:
// * kPackBegMagic
// * hash(data)
// * data.size() (8 bytes)
// * data itself
// * kPackEndMagic
// Storing the magics and the hash is a precaution against partial writes.
// UnpackBytesFromAppendFile looks for the kPackBegMagic and so
// it will ignore any partially-written data.
//
// This is simple and efficient, but I wonder if there is a ready-to-use
// standard open-source alternative. Or should we just use tar?
ByteArray PackBytesForAppendFile(const ByteArray &data) {
ByteArray res;
auto hash = Hash(data);
CHECK_EQ(hash.size(), kHashLen);
size_t size = data.size();
uint8_t size_bytes[sizeof(size)];
memcpy(size_bytes, &size, sizeof(size));
res.insert(res.end(), &kPackBegMagic[0], &kPackBegMagic[kMagicLen]);
res.insert(res.end(), hash.begin(), hash.end());
res.insert(res.end(), &size_bytes[0], &size_bytes[sizeof(size_bytes)]);
res.insert(res.end(), data.begin(), data.end());
res.insert(res.end(), &kPackEndMagic[0], &kPackEndMagic[kMagicLen]);
return res;
}
// Reverse to a sequence of PackBytesForAppendFile() appended to each other.
void UnpackBytesFromAppendFile(const ByteArray &packed_data,
std::vector<ByteArray> *unpacked,
std::vector<std::string> *hashes) {
auto pos = packed_data.cbegin();
while (true) {
pos = std::search(pos, packed_data.end(), &kPackBegMagic[0],
&kPackBegMagic[kMagicLen]);
if (pos == packed_data.end()) return;
pos += kMagicLen;
if (packed_data.end() - pos < kHashLen) return;
std::string hash(pos, pos + kHashLen);
pos += kHashLen;
size_t size = 0;
if (packed_data.end() - pos < sizeof(size)) return;
memcpy(&size, &*pos, sizeof(size));
pos += sizeof(size);
if (packed_data.end() - pos < size) return;
ByteArray ba(pos, pos + size);
pos += size;
if (packed_data.end() - pos < kMagicLen) return;
if (memcmp(&*pos, kPackEndMagic, kMagicLen) != 0) continue;
pos += kMagicLen;
if (hash != Hash(ba)) continue;
if (unpacked) unpacked->push_back(std::move(ba));
if (hashes) hashes->push_back(std::move(hash));
}
}
void AppendHashToArray(ByteArray &ba, std::string_view hash) {
CHECK_EQ(hash.size(), kHashLen);
ba.insert(ba.end(), hash.begin(), hash.end());
}
std::string ExtractHashFromArray(ByteArray &ba) {
CHECK_GE(ba.size(), kHashLen);
std::string res;
res.insert(res.end(), ba.end() - kHashLen, ba.end());
ba.resize(ba.size() - kHashLen);
return res;
}
ByteArray PackFeaturesAndHash(const ByteArray &data,
const FeatureVec &features) {
ByteSpan feature_bytes(reinterpret_cast<const uint8_t *>(features.data()),
features.size() * sizeof(feature_t));
return PackFeaturesAndHashAsRawBytes(data, feature_bytes);
}
ByteArray PackFeaturesAndHashAsRawBytes(const ByteArray &data,
ByteSpan features) {
ByteArray feature_bytes_with_hash(features.size() + kHashLen);
auto hash = Hash(data);
CHECK_EQ(hash.size(), kHashLen);
memcpy(feature_bytes_with_hash.data(), features.data(), features.size());
memcpy(feature_bytes_with_hash.data() + features.size(), hash.data(),
kHashLen);
return feature_bytes_with_hash;
}
std::string UnpackFeaturesAndHash(const ByteSpan &blob, FeatureVec *features) {
size_t features_len_in_bytes = blob.size() - kHashLen;
features->resize(features_len_in_bytes / sizeof(feature_t));
memcpy(features->data(), blob.data(), features_len_in_bytes);
std::string hash;
hash.insert(hash.end(), blob.end() - kHashLen, blob.end());
return hash;
}
// Returns a vector of string pairs that are used to replace special characters
// and hex values in ParseAFLDictionary.
static std::vector<std::pair<std::string, std::string>>
AFLDictionaryStringReplacements() {
std::vector<std::pair<std::string, std::string>> replacements;
replacements.emplace_back("\\\\", "\\");
replacements.emplace_back("\\r", "\r");
replacements.emplace_back("\\n", "\n");
replacements.emplace_back("\\t", "\t");
replacements.emplace_back("\\\"", "\"");
// Hex string replacements, lower and upper case.
for (int i = 0; i < 256; i++) {
replacements.emplace_back(absl::StrFormat("\\x%02x", i), std::string(1, i));
replacements.emplace_back(absl::StrFormat("\\x%02X", i), std::string(1, i));
}
return replacements;
}
bool ParseAFLDictionary(std::string_view dictionary_text,
std::vector<ByteArray> &dictionary_entries) {
auto replacements = AFLDictionaryStringReplacements();
dictionary_entries.clear();
// Check if the contents is ASCII.
for (char ch : dictionary_text) {
if (!std::isprint(ch) && !std::isspace(ch)) return false;
}
// Iterate over all lines.
for (auto line : absl::StrSplit(dictionary_text, '\n')) {
// [start, stop) are the offsets of the dictionary entry.
size_t start = 0;
// Skip leading spaces.
while (start < line.size() && isspace(line[start])) ++start;
// Skip empty line.
if (start == line.size()) continue;
// Skip comment line.
if (line[start] == '#') continue;
// Find the first "
while (start < line.size() && line[start] != '"') ++start;
if (start == line.size()) return false; // no opening "
++start; // skip the first "
size_t stop = line.size() - 1;
// Find the last "
while (stop > start && line[stop] != '"') --stop;
if (stop == start) return false; // no closing "
// Replace special characters and hex values.
std::string replaced = absl::StrReplaceAll(
std::string_view(line.begin() + start, stop - start), replacements);
dictionary_entries.emplace_back(replaced.begin(), replaced.end());
}
return true;
}
std::vector<size_t> RandomWeightedSubset(absl::Span<const uint64_t> set,
size_t target_size, Rng &rng) {
std::vector<size_t> res;
// Collect indices of all zeros.
for (size_t i = 0, n = set.size(); i < n; ++i) {
if (set[i] == 0) res.push_back(i);
}
// Check how many more elements need to be removed to reach `target_size`.
if (set.size() - res.size() <= target_size) return res;
size_t to_remove = set.size() - res.size() - target_size;
// Pairs of index and floating point weight, ordered by weight.
struct index_and_weight {
size_t index;
double weight;
bool operator<(const index_and_weight &other) const {
return weight < other.weight;
}
};
// Similar to https://en.wikipedia.org/wiki/Reservoir_sampling#Algorithm_A-Res
// except that we pick elements to remove from the set.
// Invariant: queue contains up to `to_remove` smallest weights observed.
std::priority_queue<index_and_weight> queue;
std::uniform_real_distribution<double> unif(0, 1); // values in [0, 1).
for (size_t i = 0; i < set.size(); ++i) {
auto w = set[i];
if (w == 0) continue;
// The idea of using rand(0,1)^(1./w) is described in the link above.
index_and_weight iw{i, pow(unif(rng), 1. / w)};
if (queue.size() < to_remove) {
// queue is not full, add iw unconditionally.
queue.push(iw);
} else {
// queue is full. Swap the top of queue with iw if iw is smaller.
if (iw < queue.top()) {
queue.pop();
queue.push(iw);
}
}
}
// Move elements from queue to res, and sort res.
while (!queue.empty()) {
res.push_back(queue.top().index);
queue.pop();
}
std::sort(res.begin(), res.end());
return res;
}
uint8_t *MmapNoReserve(size_t size) {
auto result = mmap(0, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0);
CHECK(result != MAP_FAILED);
return reinterpret_cast<uint8_t *>(result);
}
void Munmap(uint8_t *ptr, size_t size) {
auto result = munmap(ptr, size);
CHECK_EQ(result, 0);
}
} // namespace centipede