aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/bisect/bisect_test.go
blob: 2e3184cb8568991cb3ccc45079b024eea31892dc (plain)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright 2019 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 bisect

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"testing"

	"github.com/google/syzkaller/pkg/instance"
	"github.com/google/syzkaller/pkg/mgrconfig"
	"github.com/google/syzkaller/pkg/report"
	"github.com/google/syzkaller/pkg/vcs"
)

// testEnv will implement instance.BuilderTester. This allows us to
// set bisect.env.inst to a testEnv object.
type testEnv struct {
	t    *testing.T
	r    vcs.Repo
	test BisectionTest
}

func (env *testEnv) BuildSyzkaller(repo, commit string) error {
	return nil
}

func (env *testEnv) BuildKernel(compilerBin, userspaceDir, cmdlineFile, sysctlFile string,
	kernelConfig []byte) (string, string, error) {
	commit := env.headCommit()
	kernelSign := fmt.Sprintf("sign-%v", commit)
	if commit >= env.test.sameBinaryStart && commit <= env.test.sameBinaryEnd {
		kernelSign = "same-sign"
	}
	return "", kernelSign, nil
}

func (env *testEnv) Test(numVMs int, reproSyz, reproOpts, reproC []byte) ([]error, error) {
	commit := env.headCommit()
	if commit >= env.test.brokenStart && commit <= env.test.brokenEnd {
		return nil, fmt.Errorf("broken build")
	}
	if !env.test.fix && commit >= env.test.culprit || env.test.fix && commit < env.test.culprit {
		return crashErrors(numVMs, "crash occurs"), nil
	}
	return make([]error, numVMs), nil
}

func (env *testEnv) headCommit() int {
	com, err := env.r.HeadCommit()
	if err != nil {
		env.t.Fatal(err)
	}
	commit, err := strconv.ParseUint(com.Title, 10, 64)
	if err != nil {
		env.t.Fatalf("invalid commit title: %v", com.Title)
	}
	return int(commit)
}

func createTestRepo(t *testing.T) string {
	baseDir, err := ioutil.TempDir("", "syz-bisect-test")
	if err != nil {
		t.Fatal(err)
	}
	repo := vcs.CreateTestRepo(t, baseDir, "")
	if !repo.SupportsBisection() {
		t.Skip("bisection is unsupported by git (probably too old version)")
	}
	for rv := 4; rv < 10; rv++ {
		for i := 0; i < 6; i++ {
			if rv == 7 && i == 0 {
				// Create a slightly special commit graph here (for #1527):
				// Commit 650 is part of 700 release, but it does not have
				// 600 (the previous release) in parents, instead it's based
				// on the previous-previous release 500.
				repo.Git("checkout", "v5.0")
				com := repo.CommitChange("650")
				repo.Git("checkout", "master")
				repo.Git("merge", "-m", "700", com.Hash)
			} else {
				repo.CommitChange(fmt.Sprintf("%v", rv*100+i))
			}
			if i == 0 {
				repo.SetTag(fmt.Sprintf("v%v.0", rv))
			}
		}
	}
	return baseDir
}

func runBisection(t *testing.T, baseDir string, test BisectionTest) (*Result, error) {
	r, err := vcs.NewRepo("test", "64", baseDir)
	if err != nil {
		t.Fatal(err)
	}
	r.SwitchCommit("master")
	sc, err := r.GetCommitByTitle(fmt.Sprint(test.startCommit))
	if err != nil {
		t.Fatal(err)
	}
	trace := new(bytes.Buffer)
	cfg := &Config{
		Fix:   test.fix,
		Trace: trace,
		Manager: mgrconfig.Config{
			TargetOS:     "test",
			TargetVMArch: "64",
			Type:         "qemu",
			KernelSrc:    baseDir,
		},
		Kernel: KernelConfig{
			Repo:   baseDir,
			Commit: sc.Hash,
		},
	}
	inst := &testEnv{
		t:    t,
		r:    r,
		test: test,
	}
	res, err := runImpl(cfg, r, r.(vcs.Bisecter), inst)
	t.Log(trace.String())
	return res, err
}

type BisectionTest struct {
	// input environment
	name        string
	fix         bool
	startCommit int
	brokenStart int
	brokenEnd   int
	// Range of commits that result in the same kernel binary signature.
	sameBinaryStart int
	sameBinaryEnd   int
	// expected output
	expectErr    bool
	expectRep    bool
	noopChange   bool
	isRelease    bool
	commitLen    int
	oldestLatest int
	// input and output
	culprit int
}

func TestBisectionResults(t *testing.T) {
	t.Parallel()
	tests := []BisectionTest{
		// Tests that bisection returns the correct cause commit.
		{
			name:        "cause-finds-cause",
			startCommit: 905,
			commitLen:   1,
			expectRep:   true,
			culprit:     602,
		},
		// Tests that cause bisection returns error when crash does not reproduce
		// on the original commit.
		{
			name:        "cause-does-not-repro",
			startCommit: 400,
			expectErr:   true,
		},
		// Tests that no commits are returned when crash occurs on oldest commit
		// for cause bisection.
		{
			name:         "cause-crashes-oldest",
			startCommit:  905,
			commitLen:    0,
			expectRep:    true,
			culprit:      0,
			oldestLatest: 400,
		},
		// Tests that more than 1 commit is returned when cause bisection is inconclusive.
		{
			name:        "cause-inconclusive",
			startCommit: 802,
			brokenStart: 500,
			brokenEnd:   700,
			commitLen:   15,
			culprit:     605,
		},
		// Tests that bisection returns the correct fix commit.
		{
			name:        "fix-finds-fix",
			fix:         true,
			startCommit: 400,
			commitLen:   1,
			culprit:     500,
			isRelease:   true,
		},
		// Tests that fix bisection returns error when crash does not reproduce
		// on the original commit.
		{
			name:        "fix-does-not-repro",
			fix:         true,
			startCommit: 905,
			expectErr:   true,
		},
		// Tests that no commits are returned when crash occurs on HEAD
		// for fix bisection.
		{
			name:         "fix-crashes-HEAD",
			fix:          true,
			startCommit:  400,
			commitLen:    0,
			expectRep:    true,
			culprit:      1000,
			oldestLatest: 905,
		},
		// Tests that more than 1 commit is returned when fix bisection is inconclusive.
		{
			name:        "fix-inconclusive",
			fix:         true,
			startCommit: 400,
			brokenStart: 500,
			brokenEnd:   600,
			commitLen:   8,
			culprit:     501,
		},
		{
			name:            "cause-same-binary",
			startCommit:     905,
			commitLen:       1,
			expectRep:       true,
			culprit:         503,
			sameBinaryStart: 502,
			sameBinaryEnd:   503,
			noopChange:      true,
		},
		{
			name:            "cause-same-binary-off-by-one",
			startCommit:     905,
			commitLen:       1,
			expectRep:       true,
			culprit:         503,
			sameBinaryStart: 400,
			sameBinaryEnd:   502,
		},
		{
			name:            "cause-same-binary-off-by-one-2",
			startCommit:     905,
			commitLen:       1,
			expectRep:       true,
			culprit:         503,
			sameBinaryStart: 503,
			sameBinaryEnd:   905,
		},
		{
			name:            "fix-same-binary",
			fix:             true,
			startCommit:     400,
			commitLen:       1,
			culprit:         503,
			sameBinaryStart: 502,
			sameBinaryEnd:   504,
			noopChange:      true,
		},
		{
			name:            "cause-same-binary-release1",
			startCommit:     905,
			commitLen:       1,
			expectRep:       true,
			culprit:         500,
			sameBinaryStart: 405,
			sameBinaryEnd:   500,
			noopChange:      true,
			isRelease:       true,
		},
		{
			name:            "cause-same-binary-release2",
			startCommit:     905,
			commitLen:       1,
			expectRep:       true,
			culprit:         501,
			sameBinaryStart: 500,
			sameBinaryEnd:   501,
			noopChange:      true,
		},
		{
			name:            "cause-same-binary-release3",
			startCommit:     905,
			commitLen:       1,
			expectRep:       true,
			culprit:         405,
			sameBinaryStart: 404,
			sameBinaryEnd:   405,
			noopChange:      true,
		},
		{
			name:            "fix-same-binary-last",
			fix:             true,
			startCommit:     400,
			commitLen:       1,
			culprit:         905,
			sameBinaryStart: 904,
			sameBinaryEnd:   905,
			noopChange:      true,
		},
		{
			name:        "fix-release",
			fix:         true,
			startCommit: 400,
			commitLen:   1,
			culprit:     900,
			isRelease:   true,
		},
		{
			name:            "cause-not-in-previous-release-issue-1527",
			startCommit:     905,
			culprit:         650,
			commitLen:       1,
			expectRep:       true,
			sameBinaryStart: 500,
			sameBinaryEnd:   650,
			noopChange:      true,
		},
	}
	// Creating new repos takes majority of the test time,
	// so we reuse them across tests.
	repoCache := make(chan string, len(tests))
	t.Run("group", func(t *testing.T) {
		for _, test := range tests {
			test := test
			t.Run(test.name, func(t *testing.T) {
				t.Parallel()
				checkTest(t, test)
				repoDir := ""
				select {
				case repoDir = <-repoCache:
				default:
					repoDir = createTestRepo(t)
				}
				defer func() {
					repoCache <- repoDir
				}()
				res, err := runBisection(t, repoDir, test)
				if test.expectErr != (err != nil) {
					t.Fatalf("returned error: %v", err)
				}
				if err != nil {
					if res != nil {
						t.Fatalf("got both result and error: '%v' %+v", err, *res)
					}
					return
				}
				if len(res.Commits) != test.commitLen {
					t.Fatalf("expected %d commits got %d commits", test.commitLen, len(res.Commits))
				}
				expectedTitle := fmt.Sprint(test.culprit)
				if len(res.Commits) == 1 && expectedTitle != res.Commits[0].Title {
					t.Fatalf("expected commit '%v' got '%v'", expectedTitle, res.Commits[0].Title)
				}
				if test.expectRep != (res.Report != nil) {
					t.Fatalf("got rep: %v, want: %v", res.Report, test.expectRep)
				}
				if res.NoopChange != test.noopChange {
					t.Fatalf("got noop change: %v, want: %v", res.NoopChange, test.noopChange)
				}
				if res.IsRelease != test.isRelease {
					t.Fatalf("got release change: %v, want: %v", res.IsRelease, test.isRelease)
				}
				if test.oldestLatest != 0 && fmt.Sprint(test.oldestLatest) != res.Commit.Title ||
					test.oldestLatest == 0 && res.Commit != nil {
					t.Fatalf("expected latest/oldest: %v got '%v'",
						test.oldestLatest, res.Commit.Title)
				}
			})
		}
	})
	for {
		select {
		case dir := <-repoCache:
			os.RemoveAll(dir)
		default:
			return
		}
	}
}

func checkTest(t *testing.T, test BisectionTest) {
	if test.expectErr &&
		(test.commitLen != 0 ||
			test.expectRep ||
			test.oldestLatest != 0 ||
			test.culprit != 0) {
		t.Fatalf("expecting non-default values on error")
	}
	if test.brokenStart > test.brokenEnd {
		t.Fatalf("bad broken start/end: %v/%v",
			test.brokenStart, test.brokenEnd)
	}
	if test.sameBinaryStart > test.sameBinaryEnd {
		t.Fatalf("bad same binary start/end: %v/%v",
			test.sameBinaryStart, test.sameBinaryEnd)
	}
}

func crashErrors(num int, title string) []error {
	var errors []error
	for i := 0; i < num; i++ {
		errors = append(errors, &instance.CrashError{
			Report: &report.Report{
				Title: fmt.Sprintf("crashes at %v", title),
			},
		})
	}
	return errors
}