Skip to content

Commit b3d9ad5

Browse files
authored
Merge pull request #1195 from WebAssembly/fuzz
Fuzzer improvements and a fuzz fix on negative-zero comparison
2 parents c692533 + d4f7519 commit b3d9ad5

File tree

6 files changed

+490
-784
lines changed

6 files changed

+490
-784
lines changed

src/ast/ExpressionAnalyzer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ bool ExpressionAnalyzer::flexibleEqual(Expression* left, Expression* right, Expr
252252
break;
253253
}
254254
case Expression::Id::ConstId: {
255-
CHECK(Const, value);
255+
if (!left->cast<Const>()->value.bitwiseEqual(right->cast<Const>()->value)) {
256+
return false;
257+
}
256258
break;
257259
}
258260
case Expression::Id::UnaryId: {

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());

test/passes/code-folding.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(module
22
(type $13 (func (param f32)))
33
(type $1 (func))
4+
(type $2 (func (result f32)))
45
(table 282 282 anyfunc)
56
(memory $0 1 1)
67
(func $0 (type $1)
@@ -21,4 +22,31 @@
2122
)
2223
)
2324
)
25+
(func $negative-zero (type $2) (result f32)
26+
(if (result f32)
27+
(i32.const 0)
28+
(block $label$0 (result f32)
29+
(f32.const 0)
30+
)
31+
(block $label$1 (result f32)
32+
(f32.const -0)
33+
)
34+
)
35+
)
36+
(func $negative-zero-b (type $2) (result f32)
37+
(drop
38+
(i32.const 0)
39+
)
40+
(block $label$0 (result f32)
41+
(f32.const -0)
42+
)
43+
)
44+
(func $negative-zero-c (type $2) (result f32)
45+
(drop
46+
(i32.const 0)
47+
)
48+
(block $label$0 (result f32)
49+
(f32.const 0)
50+
)
51+
)
2452
)

test/passes/code-folding.wast

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,38 @@
1919
)
2020
)
2121
)
22+
(func $negative-zero (result f32)
23+
(if (result f32)
24+
(i32.const 0)
25+
(block $label$0 (result f32)
26+
(f32.const 0)
27+
)
28+
(block $label$1 (result f32)
29+
(f32.const -0)
30+
)
31+
)
32+
)
33+
(func $negative-zero-b (result f32)
34+
(if (result f32)
35+
(i32.const 0)
36+
(block $label$0 (result f32)
37+
(f32.const -0)
38+
)
39+
(block $label$1 (result f32)
40+
(f32.const -0)
41+
)
42+
)
43+
)
44+
(func $negative-zero-c (result f32)
45+
(if (result f32)
46+
(i32.const 0)
47+
(block $label$0 (result f32)
48+
(f32.const 0)
49+
)
50+
(block $label$1 (result f32)
51+
(f32.const 0)
52+
)
53+
)
54+
)
2255
)
2356

0 commit comments

Comments
 (0)