Skip to content

Commit ca32e92

Browse files
Declare some fuzzing members as const (#8255)
Followup to #8253 (comment). Allow us to declare `loggableTypes` and `atomicMemoryOrders` as const to prevent them from being mutated by mistake.
1 parent 19cecf5 commit ca32e92

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

src/tools/fuzzing.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class TranslateToFuzzReader {
194194
std::unordered_map<Type, std::vector<Name>> immutableGlobalsByType;
195195
std::unordered_map<Type, std::vector<Name>> importedImmutableGlobalsByType;
196196

197-
std::vector<Type> loggableTypes;
197+
const std::vector<Type> loggableTypes;
198198

199199
// The heap types we can pick from to generate instructions.
200200
std::vector<HeapType> interestingHeapTypes;
@@ -276,7 +276,7 @@ class TranslateToFuzzReader {
276276
// overridden using another context in an RAII manner).
277277
std::unique_ptr<FuzzParamsContext> globalParams;
278278

279-
std::vector<MemoryOrder> atomicMemoryOrders;
279+
const std::vector<MemoryOrder> atomicMemoryOrders;
280280

281281
public:
282282
int nesting = 0;

src/tools/fuzzing/fuzzing.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,49 @@
2929

3030
namespace wasm {
3131

32-
TranslateToFuzzReader::TranslateToFuzzReader(Module& wasm,
33-
std::vector<char>&& input,
34-
bool closedWorld)
35-
: wasm(wasm), closedWorld(closedWorld), builder(wasm),
36-
random(std::move(input), wasm.features),
37-
publicTypeValidator(wasm.features) {
38-
39-
atomicMemoryOrders = wasm.features.hasRelaxedAtomics()
40-
? std::vector{MemoryOrder::AcqRel, MemoryOrder::SeqCst}
41-
: std::vector{MemoryOrder::SeqCst};
32+
namespace {
4233

43-
haveInitialFunctions = !wasm.functions.empty();
44-
45-
loggableTypes = {Type::i32, Type::i64, Type::f32, Type::f64};
46-
if (wasm.features.hasSIMD()) {
34+
std::vector<Type> getLoggableTypes(const FeatureSet& features) {
35+
std::vector<Type> loggableTypes = {
36+
Type::i32, Type::i64, Type::f32, Type::f64};
37+
if (features.hasSIMD()) {
4738
loggableTypes.push_back(Type::v128);
4839
}
49-
if (wasm.features.hasReferenceTypes()) {
50-
if (wasm.features.hasGC()) {
40+
if (features.hasReferenceTypes()) {
41+
if (features.hasGC()) {
5142
loggableTypes.push_back(Type(HeapType::any, Nullable));
5243
loggableTypes.push_back(Type(HeapType::func, Nullable));
5344
loggableTypes.push_back(Type(HeapType::ext, Nullable));
5445
}
55-
if (wasm.features.hasStackSwitching()) {
46+
if (features.hasStackSwitching()) {
5647
loggableTypes.push_back(Type(HeapType::cont, Nullable));
5748
}
5849
// Note: exnref traps on the JS boundary, so we cannot try to log it.
5950
}
6051

52+
return loggableTypes;
53+
}
54+
55+
std::vector<MemoryOrder> getMemoryOrders(const FeatureSet& features) {
56+
return features.hasRelaxedAtomics()
57+
? std::vector{MemoryOrder::AcqRel, MemoryOrder::SeqCst}
58+
: std::vector{MemoryOrder::SeqCst};
59+
}
60+
61+
} // namespace
62+
63+
TranslateToFuzzReader::TranslateToFuzzReader(Module& wasm,
64+
std::vector<char>&& input,
65+
bool closedWorld)
66+
: wasm(wasm), closedWorld(closedWorld), builder(wasm),
67+
random(std::move(input), wasm.features),
68+
loggableTypes(getLoggableTypes(wasm.features)),
69+
atomicMemoryOrders(getMemoryOrders(wasm.features)),
70+
71+
publicTypeValidator(wasm.features) {
72+
73+
haveInitialFunctions = !wasm.functions.empty();
74+
6175
// Setup params. Start with the defaults.
6276
globalParams = std::make_unique<FuzzParamsContext>(*this);
6377

0 commit comments

Comments
 (0)