Skip to content

Commit 27af98d

Browse files
committed
fuzzing improvements:
* randomize initial memory * low chance to have tiny blocks * decent chance to have a branch back to the loop top
1 parent c692533 commit 27af98d

3 files changed

Lines changed: 426 additions & 783 deletions

File tree

src/tools/translate-to-fuzz.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
//
2121

2222
/*
23-
memory too
2423
high chance for set at start of loop
2524
high chance of get of a set local in the scope of that scope
2625
high chance of a tee in that case => loop var
@@ -90,6 +89,7 @@ class TranslateToFuzzReader {
9089

9190
// the memory that we use, a small portion so that we have a good chance of
9291
// looking at writes (we also look outside of this region with small probability)
92+
// this should be a power of 2
9393
static const int USABLE_MEMORY = 32;
9494

9595
// the number of runtime iterations (function calls, loop backbranches) we
@@ -158,6 +158,13 @@ class TranslateToFuzzReader {
158158
wasm.memory.exists = true;
159159
// use one page
160160
wasm.memory.initial = wasm.memory.max = 1;
161+
// init some data
162+
wasm.memory.segments.emplace_back(builder.makeConst(Literal(int32_t(0))));
163+
auto num = upTo(USABLE_MEMORY * 2);
164+
for (size_t i = 0; i < num; i++) {
165+
auto value = upTo(512);
166+
wasm.memory.segments[0].data.push_back(value >= 256 ? 0 : (value & 0xff));
167+
}
161168
}
162169

163170
void setupTable() {
@@ -509,6 +516,10 @@ class TranslateToFuzzReader {
509516
num /= 2;
510517
}
511518
}
519+
// not likely to have a block of size 1
520+
if (num == 0 && !oneIn(10)) {
521+
num++;
522+
}
512523
while (num > 0 && !finishedInput) {
513524
ret->list.push_back(make(none));
514525
num--;
@@ -540,7 +551,17 @@ class TranslateToFuzzReader {
540551
ret->name = makeLabel();
541552
breakableStack.push_back(ret);
542553
hangStack.push_back(ret);
543-
ret->body = makeMaybeBlock(type);
554+
// either create random content, or do something more targeted
555+
if (oneIn(2)) {
556+
ret->body = makeMaybeBlock(type);
557+
} else {
558+
// ensure a branch back. also optionally create some loop vars
559+
std::vector<Expression*> list;
560+
list.push_back(makeMaybeBlock(none)); // primary contents
561+
list.push_back(builder.makeBreak(ret->name, nullptr, makeCondition())); // possible branch back
562+
list.push_back(make(type)); // final element, so we have the right type
563+
ret->body = builder.makeBlock(list);
564+
}
544565
breakableStack.pop_back();
545566
hangStack.pop_back();
546567
if (HANG_LIMIT > 0) {
@@ -1147,6 +1168,12 @@ class TranslateToFuzzReader {
11471168
return upTo(x) == 0;
11481169
}
11491170

1171+
bool onceEvery(Index x) {
1172+
static int counter = 0;
1173+
counter++;
1174+
return counter % x == 0;
1175+
}
1176+
11501177
// apply upTo twice, generating a skewed distribution towards
11511178
// low values
11521179
Index upToSquared(Index x) {

src/wasm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,7 @@ class Table {
654654
Expression* offset;
655655
std::vector<Name> data;
656656
Segment() {}
657-
Segment(Expression* offset) : offset(offset) {
658-
}
657+
Segment(Expression* offset) : offset(offset) {}
659658
Segment(Expression* offset, std::vector<Name>& init) : offset(offset) {
660659
data.swap(init);
661660
}
@@ -685,6 +684,7 @@ class Memory {
685684
Expression* offset;
686685
std::vector<char> data; // TODO: optimize
687686
Segment() {}
687+
Segment(Expression* offset) : offset(offset) {}
688688
Segment(Expression* offset, const char* init, Address size) : offset(offset) {
689689
data.resize(size);
690690
std::copy_n(init, size, data.begin());

0 commit comments

Comments
 (0)