Skip to content

Commit 25298be

Browse files
authored
Fuzzer: Randomly pick which functions to use in RefFunc (#6503)
Previously we chose the first with a proper type, and now we start to scan from a random index, giving later functions a chance too, so we should be emitting a greater variety of ref.func targets. Also remove some obsolete fuzzer TODOs.
1 parent fb5608a commit 25298be

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

src/tools/fuzzing/fuzzing.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,6 @@ void TranslateToFuzzReader::modifyInitialFunctions() {
11951195
if (upTo(RESOLUTION) >= chance) {
11961196
dropToLog(func);
11971197
// TODO add some locals? and the rest of addFunction's operations?
1198-
// TODO: interposition, replace initial a(b) with a(RANDOM_THING(b))
11991198
// TODO: if we add OOB checks after creation, then we can do it on
12001199
// initial contents too, and it may be nice to *not* run these
12011200
// passes, like we don't run them on new functions. But, we may
@@ -1383,7 +1382,6 @@ Expression* TranslateToFuzzReader::_makeConcrete(Type type) {
13831382
&Self::makeArrayGet);
13841383
}
13851384
}
1386-
// TODO: struct.get and other GC things
13871385
return (this->*pick(options))(type);
13881386
}
13891387

@@ -2450,11 +2448,18 @@ Expression* TranslateToFuzzReader::makeRefFuncConst(Type type) {
24502448
// had generic 'func' here.
24512449
heapType = Signature(Type::none, Type::none);
24522450
}
2453-
// TODO: randomize the order
2454-
for (auto& func : wasm.functions) {
2455-
if (Type::isSubType(Type(func->type, NonNullable), type)) {
2456-
return builder.makeRefFunc(func->name, func->type);
2457-
}
2451+
// Look for a proper function starting from a random location, and loop from
2452+
// there, wrapping around to 0.
2453+
if (!wasm.functions.empty()) {
2454+
Index start = upTo(wasm.functions.size());
2455+
Index i = start;
2456+
do {
2457+
auto& func = wasm.functions[i];
2458+
if (Type::isSubType(Type(func->type, NonNullable), type)) {
2459+
return builder.makeRefFunc(func->name, func->type);
2460+
}
2461+
i = (i + 1) % wasm.functions.size();
2462+
} while (i != start);
24582463
}
24592464
// We don't have a matching function. Create a null some of the time here,
24602465
// but only rarely if the type is non-nullable (because in that case we'd need
@@ -3788,7 +3793,6 @@ Expression* TranslateToFuzzReader::makeArraySet(Type type) {
37883793
// Only rarely emit a plain get which might trap. See related logic in
37893794
// ::makePointer().
37903795
if (allowOOB && oneIn(10)) {
3791-
// TODO: fuzz signed and unsigned, and also below
37923796
return builder.makeArraySet(ref, index, value);
37933797
}
37943798
// To avoid a trap, check the length dynamically using this pattern:
@@ -3816,7 +3820,6 @@ Expression* TranslateToFuzzReader::makeArrayBulkMemoryOp(Type type) {
38163820
// Only rarely emit a plain get which might trap. See related logic in
38173821
// ::makePointer().
38183822
if (allowOOB && oneIn(10)) {
3819-
// TODO: fuzz signed and unsigned, and also below
38203823
return builder.makeArrayFill(ref, index, value, length);
38213824
}
38223825
auto check =
@@ -3841,7 +3844,6 @@ Expression* TranslateToFuzzReader::makeArrayBulkMemoryOp(Type type) {
38413844
auto* srcRef = makeTrappingRefUse(srcArrayType);
38423845
auto* length = make(Type::i32);
38433846
if (allowOOB && oneIn(10)) {
3844-
// TODO: fuzz signed and unsigned, and also below
38453847
return builder.makeArrayCopy(ref, index, srcRef, srcIndex, length);
38463848
}
38473849
auto check =
Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
total
2-
[exports] : 4
3-
[funcs] : 7
2+
[exports] : 5
3+
[funcs] : 8
44
[globals] : 1
55
[imports] : 5
66
[memories] : 1
@@ -9,49 +9,46 @@ total
99
[tables] : 1
1010
[tags] : 2
1111
[total] : 674
12-
[vars] : 37
12+
[vars] : 41
1313
ArrayCopy : 1
1414
ArrayGet : 3
15-
ArrayLen : 3
16-
ArrayNew : 4
15+
ArrayLen : 4
16+
ArrayNew : 5
17+
ArrayNewFixed : 1
1718
ArraySet : 1
18-
AtomicCmpxchg : 1
19+
AtomicFence : 1
1920
AtomicNotify : 3
2021
AtomicRMW : 1
21-
Binary : 81
22+
Binary : 84
2223
Block : 75
2324
Break : 12
24-
Call : 25
25-
CallRef : 1
26-
Const : 121
27-
Drop : 5
25+
Call : 21
26+
Const : 133
27+
Drop : 6
2828
GlobalGet : 24
2929
GlobalSet : 24
30-
I31Get : 2
31-
If : 23
32-
Load : 19
33-
LocalGet : 75
30+
I31Get : 3
31+
If : 21
32+
Load : 22
33+
LocalGet : 65
3434
LocalSet : 50
35-
Loop : 7
36-
MemoryFill : 1
35+
Loop : 6
3736
Nop : 4
38-
Pop : 6
39-
RefAs : 9
40-
RefCast : 5
41-
RefEq : 2
42-
RefFunc : 3
43-
RefI31 : 6
37+
Pop : 7
38+
RefAs : 7
39+
RefCast : 3
40+
RefFunc : 2
41+
RefI31 : 7
4442
RefIsNull : 2
45-
RefNull : 12
46-
RefTest : 3
47-
Return : 6
48-
SIMDExtract : 2
49-
Select : 4
43+
RefNull : 11
44+
RefTest : 2
45+
Return : 8
46+
Select : 3
5047
StructGet : 1
51-
StructNew : 1
52-
StructSet : 1
48+
StructNew : 3
49+
StructSet : 2
5350
Try : 5
5451
TupleExtract : 3
5552
TupleMake : 4
56-
Unary : 20
53+
Unary : 21
5754
Unreachable : 13

0 commit comments

Comments
 (0)