Path: blob/main/system/lib/wasmfs/fuzzer/fuzzer.cpp
6175 views
// Copyright 2021 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45// This file defines a fuzzer that validates operations in a multi-threaded6// setting.78#include "parameters.h"9#include "random.h"10#include "support/command-line.h"11#include "workload.h"12#include <cstdlib>13#include <iostream>14#include <optional>15#include <pthread.h>16#include <random>17#include <stdlib.h>18#include <string>19#include <unistd.h>2021// Used to define the size of the bytes to seed the Random object.22#define NUM_RAND_BYTES 40962324namespace wasmfs {25using RandEngine = std::mt19937_64;26bool VERBOSE = false;2728uint64_t getSeed() {29// Return a (truly) random 64-bit value.30std::random_device rand;31return std::uniform_int_distribution<uint64_t>{}(rand);32}3334struct Fuzzer {35void run(uint64_t seed) {36RandEngine getRand(seed);37printf("Running with seed: %llu\n", seed);3839std::vector<char> bytes(NUM_RAND_BYTES);40for (size_t i = 0; i < bytes.size(); i += sizeof(uint64_t)) {41*(uint64_t*)(bytes.data() + i) = getRand();42}4344Random rand(std::move(bytes));4546// Create a workload with the Random seed.47auto workload = ReadWrite(rand);4849workload.execute();50}51};52} // namespace wasmfs5354int main(int argc, const char* argv[]) {55using namespace wasmfs;5657Options options("wasmfs-fuzzer", "Fuzz multi-threaded WasmFS operations");58std::optional<uint64_t> seed;5960options.add("--seed",61"",62"Run a single workload generated by the given seed",63Options::Arguments::One,64[&](Options*, const std::string& arg) {65seed = uint64_t(std::stoull(arg));66});6768options.add("--verbose",69"-v",70"Run with verbose logging",71Options::Arguments::Zero,72[&](Options*, const std::string& arg) { VERBOSE = true; });7374options.parse(argc, argv);7576Fuzzer fuzzer;7778if (seed) {79fuzzer.run(*seed);80} else {81size_t i = 0;82RandEngine nextSeed(getSeed());83while (true) {84printf("Iteration %zu \n", ++i);85printf("Seed %llu \n", getSeed());86fuzzer.run(nextSeed());87}88}89return 0;90}919293