aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/manager/diff/manager_test.go
blob: 89a35c79451f80c4737ce6c46deea402a77e441d (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
// Copyright 2026 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 diff

import (
	"context"
	"errors"
	"testing"
	"time"

	"github.com/google/syzkaller/pkg/manager"
	"github.com/google/syzkaller/pkg/report"
	"github.com/google/syzkaller/pkg/repro"
	_ "github.com/google/syzkaller/prog/test"
	"github.com/stretchr/testify/assert"
)

const testTimeout = 15 * time.Second

func TestNeedReproForTitle(t *testing.T) {
	for title, skip := range map[string]bool{
		"no output from test machine":                          false,
		"SYZFAIL: read failed":                                 false,
		"lost connection to test machine":                      false,
		"INFO: rcu detected stall in clone":                    false,
		"WARNING in arch_install_hw_breakpoint":                true,
		"KASAN: slab-out-of-bounds Write in __bpf_get_stackid": true,
	} {
		assert.Equal(t, skip, needReproForTitle(title), "title=%q", title)
	}
}

func TestDiffBaseCrashInterception(t *testing.T) {
	env := newTestEnv(t, nil)
	defer env.close()
	env.start()

	env.base.CrashesCh <- &report.Report{Title: "base_crash"}

	select {
	case title := <-env.diffCtx.cfg.BaseCrashes:
		assert.Equal(t, "base_crash", title)
	case <-time.After(testTimeout):
		t.Error("expected base crash")
	}
}

func TestDiffExternalIgnore(t *testing.T) {
	// Config ignores crash -> Patched crash ignored -> No repro.
	runReproCalled := false
	mockRunRepro := mockReproCallback("important_crash", nil, func() {
		runReproCalled = true
	})

	env := newTestEnv(t, &Config{
		IgnoreCrash: func(ctx context.Context, title string) (bool, error) {
			if title == "ignored_crash" {
				return true, nil
			}
			return false, nil
		},
		runRepro: mockRunRepro,
	})
	defer env.close()

	env.new.FinishCorpusTriage()
	env.start()

	env.new.CrashesCh <- &report.Report{Title: "ignored_crash", Report: []byte("log")}
	env.waitForStatus("ignored_crash", manager.DiffBugStatusIgnored)
	assert.False(t, runReproCalled, "should not repro ignored crash")

	env.new.CrashesCh <- &report.Report{Title: "important_crash", Report: []byte("log")}
	env.waitForStatus("important_crash", manager.DiffBugStatusVerifying)
}

func TestDiffSuccess(t *testing.T) {
	// Patched kernel crashes -> Repro succeeds -> Base kernel does NOT crash -> PatchedOnly reported.

	// Mock Runner (Repro on Base).
	env := newTestEnv(t, &Config{
		runRepro: mockRepro("crash_title", nil),
		runner: newMockRunner(func(ctx context.Context, k Kernel, r *repro.Result, fullRepro bool) *reproRunnerResult {
			// Simulate successful run on base without crash.
			return &reproRunnerResult{
				reproReport: r.Report,
				repro:       r,
				crashReport: nil, // No crash on base.
				fullRepro:   true,
			}
		}),
	})
	defer env.close()

	env.new.FinishCorpusTriage()
	env.start()

	env.new.CrashesCh <- &report.Report{Title: "crash_title", Report: []byte("log")}
	select {
	case bug := <-env.diffCtx.patchedOnly:
		assert.Equal(t, "crash_title", bug.Report.Title)
	case <-time.After(testTimeout):
		t.Fatal("expected patched only report")
	}
}

func TestDiffFailNoRepro(t *testing.T) {
	// Patched kernel crashes -> Repro fails -> No report.
	env := newTestEnv(t, &Config{
		runRepro: mockRepro("", errors.New("repro failed")),
	})
	defer env.close()

	env.new.FinishCorpusTriage()
	env.start()

	env.new.CrashesCh <- &report.Report{Title: "crash_title", Report: []byte("log")}
	env.waitForStatus("crash_title", manager.DiffBugStatusCompleted)
}

func TestDiffFailBaseCrash(t *testing.T) {
	// Patched kernel crashes -> Repro succeeds -> Base also crashes -> No PatchedOnly report.
	env := newTestEnv(t, &Config{
		runRepro: mockRepro("crash_title", nil),
		runner: newMockRunner(func(ctx context.Context, k Kernel, r *repro.Result, fullRepro bool) *reproRunnerResult {
			return &reproRunnerResult{
				reproReport: r.Report,
				repro:       r,
				crashReport: &report.Report{Title: "crash_title"}, // Base crashed.
			}
		}),
	})
	defer env.close()

	env.new.FinishCorpusTriage()
	env.start()

	env.new.CrashesCh <- &report.Report{Title: "crash_title", Report: []byte("log")}

	select {
	case <-env.diffCtx.patchedOnly:
		t.Fatal("unexpected patched only report")
	case <-env.diffCtx.cfg.BaseCrashes: // Should report to BaseCrashes.
		// Expected.
	case <-time.After(testTimeout):
		t.Fatal("expected base crash report")
	}

	env.waitForStatus("crash_title", manager.DiffBugStatusCompleted)
}

func TestDiffFailBaseCrashEarly(t *testing.T) {
	// Base crashes first -> Patched crashes same title -> No reproduction attempt.
	runReproCalled := false
	mockRunRepro := mockReproCallback("crash_title", nil, func() {
		runReproCalled = true
	})

	env := newTestEnv(t, &Config{
		runRepro: mockRunRepro,
	})
	defer env.close()

	env.new.FinishCorpusTriage()
	env.start()

	env.base.CrashesCh <- &report.Report{Title: "crash_title", Report: []byte("log")}
	select {
	case <-env.diffCtx.cfg.BaseCrashes:
	case <-time.After(testTimeout):
		t.Fatal("expected base crash")
	}
	env.new.CrashesCh <- &report.Report{Title: "crash_title", Report: []byte("log")}
	env.waitForStatus("crash_title", manager.DiffBugStatusIgnored)
	assert.False(t, runReproCalled, "WaitRepro should not be called")
}

func TestDiffRetryRepro(t *testing.T) {
	// Patched kernel crashes -> Repro fails -> Retried until max attempts.
	reproCalled := make(chan struct{}, 10)
	mockRunRepro := mockReproCallback("", errors.New("repro failed"), func() {
		reproCalled <- struct{}{}
	})

	env := newTestEnv(t, &Config{
		runRepro: mockRunRepro,
	})
	defer env.close()

	env.new.FinishCorpusTriage()
	env.start()

	for i := 0; i <= maxReproAttempts; i++ {
		env.new.CrashesCh <- &report.Report{Title: "crash_title", Report: []byte("log")}
		select {
		case <-reproCalled:
		case <-time.After(testTimeout):
			t.Fatalf("iteration %d: expected repro", i)
		}
		env.waitForStatus("crash_title", manager.DiffBugStatusCompleted)
	}
	// Inject one more crash, which should be ignored.
	env.new.CrashesCh <- &report.Report{Title: "crash_title", Report: []byte("log")}
	env.waitForStatus("crash_title", manager.DiffBugStatusIgnored)
	select {
	case <-reproCalled:
		t.Fatalf("unexpected repro")
	default:
	}
}