Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/wasmfs/fuzzer/workload.h
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 fuzzer workloads which primarily fuzz multi-threaded
7
// operations.
8
9
#pragma once
10
11
#include "parameters.h"
12
#include "random.h"
13
#include <assert.h>
14
#include <chrono>
15
#include <cstdlib>
16
#include <iostream>
17
#include <optional>
18
#include <stdio.h>
19
#include <string>
20
#include <thread>
21
#include <unistd.h>
22
#include <unordered_map>
23
24
namespace wasmfs {
25
26
// This abstract class defines a fuzzer workload. It sets up a series of file
27
// system operations and then validates that the expected outcome is observed.
28
class Workload {
29
public:
30
Workload(Random& rand) : rand(rand) {}
31
virtual ~Workload() = default;
32
33
// This function generates a workload based on a Random seed. It will test a
34
// file system property, using any required syscalls.
35
virtual void execute() = 0;
36
37
protected:
38
Random& rand;
39
};
40
41
// This workload class attempts to fuzz for tearing in reads and writes.
42
// This should validate that writes are atomic (i.e. that a read interleaved
43
// between two writes should not read a portion of the first write and a portion
44
// of the second write). Writer threads should write a uniform string of the
45
// same character. Reader threads should validate that file content is not
46
// intermixed (it should check that all characters in the file content are the
47
// same).
48
class ReadWrite : public Workload {
49
50
public:
51
ReadWrite(Random& rand) : Workload(rand) {}
52
53
void execute() override;
54
55
private:
56
int fd;
57
// Work describes the list of strings being written to a test file.
58
std::vector<std::string> work;
59
std::atomic<bool> go{false};
60
std::atomic<bool> stop{false};
61
62
bool isSame(std::vector<char>& target);
63
void reader();
64
void writer();
65
};
66
} // namespace wasmfs
67
68