Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/wasmfs/fuzzer/fuzzer.cpp
6175 views
1
// Copyright 2021 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
// This file defines a fuzzer that validates operations in a multi-threaded
7
// setting.
8
9
#include "parameters.h"
10
#include "random.h"
11
#include "support/command-line.h"
12
#include "workload.h"
13
#include <cstdlib>
14
#include <iostream>
15
#include <optional>
16
#include <pthread.h>
17
#include <random>
18
#include <stdlib.h>
19
#include <string>
20
#include <unistd.h>
21
22
// Used to define the size of the bytes to seed the Random object.
23
#define NUM_RAND_BYTES 4096
24
25
namespace wasmfs {
26
using RandEngine = std::mt19937_64;
27
bool VERBOSE = false;
28
29
uint64_t getSeed() {
30
// Return a (truly) random 64-bit value.
31
std::random_device rand;
32
return std::uniform_int_distribution<uint64_t>{}(rand);
33
}
34
35
struct Fuzzer {
36
void run(uint64_t seed) {
37
RandEngine getRand(seed);
38
printf("Running with seed: %llu\n", seed);
39
40
std::vector<char> bytes(NUM_RAND_BYTES);
41
for (size_t i = 0; i < bytes.size(); i += sizeof(uint64_t)) {
42
*(uint64_t*)(bytes.data() + i) = getRand();
43
}
44
45
Random rand(std::move(bytes));
46
47
// Create a workload with the Random seed.
48
auto workload = ReadWrite(rand);
49
50
workload.execute();
51
}
52
};
53
} // namespace wasmfs
54
55
int main(int argc, const char* argv[]) {
56
using namespace wasmfs;
57
58
Options options("wasmfs-fuzzer", "Fuzz multi-threaded WasmFS operations");
59
std::optional<uint64_t> seed;
60
61
options.add("--seed",
62
"",
63
"Run a single workload generated by the given seed",
64
Options::Arguments::One,
65
[&](Options*, const std::string& arg) {
66
seed = uint64_t(std::stoull(arg));
67
});
68
69
options.add("--verbose",
70
"-v",
71
"Run with verbose logging",
72
Options::Arguments::Zero,
73
[&](Options*, const std::string& arg) { VERBOSE = true; });
74
75
options.parse(argc, argv);
76
77
Fuzzer fuzzer;
78
79
if (seed) {
80
fuzzer.run(*seed);
81
} else {
82
size_t i = 0;
83
RandEngine nextSeed(getSeed());
84
while (true) {
85
printf("Iteration %zu \n", ++i);
86
printf("Seed %llu \n", getSeed());
87
fuzzer.run(nextSeed());
88
}
89
}
90
return 0;
91
}
92
93