• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 syzkaller project authors. All rights reserved.
2// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3
4package repro
5
6import (
7	"math/rand"
8	"testing"
9	"time"
10
11	"github.com/google/syzkaller/pkg/csource"
12	"github.com/google/syzkaller/prog"
13)
14
15func initTest(t *testing.T) (*rand.Rand, int) {
16	iters := 1000
17	if testing.Short() {
18		iters = 100
19	}
20	seed := int64(time.Now().UnixNano())
21	rs := rand.NewSource(seed)
22	t.Logf("seed=%v", seed)
23	return rand.New(rs), iters
24}
25
26func TestBisect(t *testing.T) {
27	ctx := &context{
28		stats: new(Stats),
29	}
30
31	rd, iters := initTest(t)
32	for n := 0; n < iters; n++ {
33		var progs []*prog.LogEntry
34		numTotal := rd.Intn(300)
35		numGuilty := 0
36		for i := 0; i < numTotal; i++ {
37			var prog prog.LogEntry
38			if rd.Intn(30) == 0 {
39				prog.Proc = 42
40				numGuilty++
41			}
42			progs = append(progs, &prog)
43		}
44		if numGuilty == 0 {
45			var prog prog.LogEntry
46			prog.Proc = 42
47			progs = append(progs, &prog)
48			numGuilty++
49		}
50		progs, _ = ctx.bisectProgs(progs, func(p []*prog.LogEntry) (bool, error) {
51			guilty := 0
52			for _, prog := range p {
53				if prog.Proc == 42 {
54					guilty++
55				}
56			}
57			return guilty == numGuilty, nil
58		})
59		if len(progs) != numGuilty {
60			t.Fatalf("bisect test failed: wrong number of guilty progs: got: %v, want: %v", len(progs), numGuilty)
61		}
62		for _, prog := range progs {
63			if prog.Proc != 42 {
64				t.Fatalf("bisect test failed: wrong program is guilty: progs: %v", progs)
65			}
66		}
67	}
68}
69
70func TestSimplifies(t *testing.T) {
71	opts := csource.Options{
72		Threaded:      true,
73		Collide:       true,
74		Repeat:        true,
75		Procs:         10,
76		Sandbox:       "namespace",
77		EnableTun:     true,
78		EnableCgroups: true,
79		EnableNetdev:  true,
80		ResetNet:      true,
81		UseTmpDir:     true,
82		HandleSegv:    true,
83		Repro:         true,
84	}
85	var check func(opts csource.Options, i int)
86	check = func(opts csource.Options, i int) {
87		if err := opts.Check("linux"); err != nil {
88			t.Fatalf("opts are invalid: %v", err)
89		}
90		if i == len(cSimplifies) {
91			return
92		}
93		check(opts, i+1)
94		if cSimplifies[i](&opts) {
95			check(opts, i+1)
96		}
97	}
98	check(opts, 0)
99}
100