forked from google/syzkaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimization_test.go
306 lines (301 loc) · 8.78 KB
/
minimization_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2018 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package prog
import (
"math/rand"
"testing"
)
// nolint:gocyclo
func TestMinimize(t *testing.T) {
// nolint: lll
tests := []struct {
os string
arch string
orig string
callIndex int
pred func(*Prog, int) bool
result string
resultCallIndex int
}{
// Predicate always returns false, so must get the same program.
{
"linux", "amd64",
"mmap(&(0x7f0000000000/0x1000)=nil, 0x1000, 0x3, 0x32, 0xffffffffffffffff, 0x0)\n" +
"sched_yield()\n" +
"pipe2(&(0x7f0000000000), 0x0)\n",
2,
func(p *Prog, callIndex int) bool {
if len(p.Calls) == 0 {
t.Fatalf("got an empty program")
}
if p.Calls[len(p.Calls)-1].Meta.Name != "pipe2" {
t.Fatalf("last call is removed")
}
return false
},
"mmap(&(0x7f0000000000/0x1000)=nil, 0x1000, 0x3, 0x32, 0xffffffffffffffff, 0x0)\n" +
"sched_yield()\n" +
"pipe2(&(0x7f0000000000), 0x0)\n",
2,
},
// Remove a call.
{
"linux", "amd64",
"mmap(&(0x7f0000000000/0x1000)=nil, 0x1000, 0x3, 0x32, 0xffffffffffffffff, 0x0)\n" +
"sched_yield()\n" +
"pipe2(&(0x7f0000000000)={0xffffffffffffffff, 0xffffffffffffffff}, 0x0)\n",
2,
func(p *Prog, callIndex int) bool {
// Aim at removal of sched_yield.
return len(p.Calls) == 2 && p.Calls[0].Meta.Name == "mmap" && p.Calls[1].Meta.Name == "pipe2"
},
"mmap(&(0x7f0000000000/0x1000)=nil, 0x1000, 0x0, 0x10, 0xffffffffffffffff, 0x0)\n" +
"pipe2(0x0, 0x0)\n",
1,
},
// Remove two dependent calls.
{
"linux", "amd64",
"mmap(&(0x7f0000000000/0x1000)=nil, 0x1000, 0x3, 0x32, 0xffffffffffffffff, 0x0)\n" +
"pipe2(&(0x7f0000000000)={0x0, 0x0}, 0x0)\n" +
"sched_yield()\n",
2,
func(p *Prog, callIndex int) bool {
// Aim at removal of pipe2 and then mmap.
if len(p.Calls) == 2 && p.Calls[0].Meta.Name == "mmap" && p.Calls[1].Meta.Name == "sched_yield" {
return true
}
if len(p.Calls) == 1 && p.Calls[0].Meta.Name == "sched_yield" {
return true
}
return false
},
"sched_yield()\n",
0,
},
// Remove a call and replace results.
{
"linux", "amd64",
"mmap(&(0x7f0000000000/0x1000)=nil, 0x1000, 0x3, 0x32, 0xffffffffffffffff, 0x0)\n" +
"pipe2(&(0x7f0000000000)={<r0=>0x0, 0x0}, 0x0)\n" +
"write(r0, &(0x7f0000000000)=\"1155\", 0x2)\n" +
"sched_yield()\n",
3,
func(p *Prog, callIndex int) bool {
return p.String() == "mmap-write-sched_yield"
},
"mmap(&(0x7f0000000000/0x1000)=nil, 0x1000, 0x0, 0x10, 0xffffffffffffffff, 0x0)\n" +
"write(0xffffffffffffffff, 0x0, 0x0)\n" +
"sched_yield()\n",
2,
},
// Remove a call and replace results.
{
"linux", "amd64",
"mmap(&(0x7f0000000000/0x1000)=nil, 0x1000, 0x3, 0x32, 0xffffffffffffffff, 0x0)\n" +
"r0=open(&(0x7f0000000000)=\"1155\", 0x0, 0x0)\n" +
"write(r0, &(0x7f0000000000)=\"1155\", 0x2)\n" +
"sched_yield()\n",
-1,
func(p *Prog, callIndex int) bool {
return p.String() == "mmap-write-sched_yield"
},
"mmap(&(0x7f0000000000/0x1000)=nil, 0x1000, 0x0, 0x10, 0xffffffffffffffff, 0x0)\n" +
"write(0xffffffffffffffff, 0x0, 0x0)\n" +
"sched_yield()\n",
-1,
},
// Minimize pointer.
{
"linux", "amd64",
"pipe2(&(0x7f0000001000)={0xffffffffffffffff, 0xffffffffffffffff}, 0x0)\n",
-1,
func(p *Prog, callIndex int) bool {
return len(p.Calls) == 1 && p.Calls[0].Meta.Name == "pipe2"
},
"pipe2(0x0, 0x0)\n",
-1,
},
// Minimize pointee.
{
"linux", "amd64",
"pipe2(&(0x7f0000001000)={0xffffffffffffffff, 0xffffffffffffffff}, 0x0)\n",
-1,
func(p *Prog, callIndex int) bool {
return len(p.Calls) == 1 && p.Calls[0].Meta.Name == "pipe2" && p.Calls[0].Args[0].(*PointerArg).Address != 0
},
"pipe2(&(0x7f0000001000), 0x0)\n",
-1,
},
// Make sure we don't hang when minimizing resources.
{
"test", "64",
"r0 = test$res0()\n" +
"test$res1(r0)\n",
-1,
func(p *Prog, callIndex int) bool {
return false
},
"r0 = test$res0()\n" +
"test$res1(r0)\n",
-1,
},
{
"test", "64",
"minimize$0(0x1, 0x1)\n",
-1,
func(p *Prog, callIndex int) bool { return len(p.Calls) == 1 },
"minimize$0(0x1, 0xffffffffffffffff)\n",
-1,
},
// Clear unneeded fault injection.
{
"linux", "amd64",
"pipe2(0x0, 0x0) (fail_nth: 5)\n",
-1,
func(p *Prog, callIndex int) bool {
return len(p.Calls) == 1 && p.Calls[0].Meta.Name == "pipe2"
},
"pipe2(0x0, 0x0)\n",
-1,
},
// Keep important fault injection.
{
"linux", "amd64",
"pipe2(0x0, 0x0) (fail_nth: 5)\n",
-1,
func(p *Prog, callIndex int) bool {
return len(p.Calls) == 1 && p.Calls[0].Meta.Name == "pipe2" && p.Calls[0].Props.FailNth == 5
},
"pipe2(0x0, 0x0) (fail_nth: 5)\n",
-1,
},
// Clear unneeded async flag.
{
"linux", "amd64",
"pipe2(0x0, 0x0) (async)\n",
-1,
func(p *Prog, callIndex int) bool {
return len(p.Calls) == 1 && p.Calls[0].Meta.Name == "pipe2"
},
"pipe2(0x0, 0x0)\n",
-1,
},
// Keep important async flag.
{
"linux", "amd64",
"pipe2(0x0, 0x0) (async)\n",
-1,
func(p *Prog, callIndex int) bool {
return len(p.Calls) == 1 && p.Calls[0].Meta.Name == "pipe2" && p.Calls[0].Props.Async
},
"pipe2(0x0, 0x0) (async)\n",
-1,
},
// Clear unneeded rerun.
{
"linux", "amd64",
"pipe2(0x0, 0x0) (rerun: 100)\n",
-1,
func(p *Prog, callIndex int) bool {
return len(p.Calls) == 1 && p.Calls[0].Meta.Name == "pipe2"
},
"pipe2(0x0, 0x0)\n",
-1,
},
// Keep important rerun.
{
"linux", "amd64",
"pipe2(0x0, 0x0) (rerun: 100)\n",
-1,
func(p *Prog, callIndex int) bool {
return len(p.Calls) == 1 && p.Calls[0].Meta.Name == "pipe2" && p.Calls[0].Props.Rerun >= 100
},
"pipe2(0x0, 0x0) (rerun: 100)\n",
-1,
},
// Undo target.SpecialFileLenghts mutation (reduce file name length).
{
"test", "64",
"mutate9(&(0x7f0000000000)='./file0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\x00')\n",
0,
func(p *Prog, callIndex int) bool {
return p.Calls[0].Args[0].(*PointerArg).Res != nil
},
"mutate9(&(0x7f0000000000)='./file0\\x00')\n",
0,
},
// Ensure `no_minimize` calls are untouched.
{
"linux", "amd64",
"syz_mount_image$ext4(&(0x7f0000000000)='ext4\\x00', &(0x7f0000000100)='./file0\\x00', 0x0, &(0x7f0000010020), 0x1, 0x15, &(0x7f0000000200)=\"$eJwqrqzKTszJSS0CBAAA//8TyQPi\")\n",
0,
func(p *Prog, callIndex int) bool {
// Anything is allowed except removing a call.
return len(p.Calls) > 0
},
"syz_mount_image$ext4(&(0x7f0000000000)='ext4\\x00', &(0x7f0000000100)='./file0\\x00', 0x0, &(0x7f0000010020), 0x1, 0x15, &(0x7f0000000200)=\"$eJwqrqzKTszJSS0CBAAA//8TyQPi\")\n",
0,
},
}
t.Parallel()
for ti, test := range tests {
target, err := GetTarget(test.os, test.arch)
if err != nil {
t.Fatal(err)
}
p, err := target.Deserialize([]byte(test.orig), Strict)
if err != nil {
t.Fatalf("failed to deserialize original program #%v: %v", ti, err)
}
p1, ci := Minimize(p, test.callIndex, false, test.pred)
res := p1.Serialize()
if string(res) != test.result {
t.Fatalf("minimization produced wrong result #%v\norig:\n%v\nexpect:\n%v\ngot:\n%v\n",
ti, test.orig, test.result, string(res))
}
if ci != test.resultCallIndex {
t.Fatalf("minimization broke call index #%v: got %v, want %v",
ti, ci, test.resultCallIndex)
}
}
}
func TestMinimizeRandom(t *testing.T) {
target, rs, iters := initTest(t)
iters /= 10 // Long test.
ct := target.DefaultChoiceTable()
r := rand.New(rs)
for i := 0; i < iters; i++ {
for _, crash := range []bool{false, true} {
p := target.Generate(rs, 5, ct)
copyP := p.Clone()
minP, _ := Minimize(p, len(p.Calls)-1, crash, func(p1 *Prog, callIndex int) bool {
if r.Intn(2) == 0 {
return false
}
copyP = p1.Clone()
return true
})
got := string(minP.Serialize())
want := string(copyP.Serialize())
if got != want {
t.Fatalf("program:\n%s\ngot:\n%v\nwant:\n%s", string(p.Serialize()), got, want)
}
}
}
}
func TestMinimizeCallIndex(t *testing.T) {
target, rs, iters := initTest(t)
ct := target.DefaultChoiceTable()
r := rand.New(rs)
for i := 0; i < iters; i++ {
p := target.Generate(rs, 5, ct)
ci := r.Intn(len(p.Calls))
p1, ci1 := Minimize(p, ci, r.Intn(2) == 0, func(p1 *Prog, callIndex int) bool {
return r.Intn(2) == 0
})
if ci1 < 0 || ci1 >= len(p1.Calls) || p.Calls[ci].Meta.Name != p1.Calls[ci1].Meta.Name {
t.Fatalf("bad call index after minimization")
}
}
}