aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/csource/csource_test.go
blob: 9743b6356422bc38e33f2e5d08fb256e8e098575 (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
// Copyright 2015 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 csource

import (
	"fmt"
	"math/rand"
	"os"
	"runtime"
	"strings"
	"testing"
	"time"

	"github.com/google/syzkaller/prog"
	_ "github.com/google/syzkaller/sys"
	"github.com/google/syzkaller/sys/targets"
)

func TestGenerate(t *testing.T) {
	switch runtime.GOOS {
	case "openbsd":
		t.Skipf("broken on %v", runtime.GOOS)
	}
	t.Parallel()
	for _, target := range prog.AllTargets() {
		target := target
		if runtime.GOOS != targets.Get(target.OS, target.Arch).BuildOS {
			continue
		}
		t.Run(target.OS+"/"+target.Arch, func(t *testing.T) {
			if target.OS == "linux" && target.Arch == "arm" {
				// This currently fails (at least with my arm-linux-gnueabihf-gcc-4.8) with:
				// Assembler messages:
				// Error: alignment too large: 15 assumed
				t.Skip("broken")
			}
			if target.OS == "linux" && target.Arch == "386" {
				// Currently fails on travis with:
				// fatal error: asm/unistd.h: No such file or directory
				t.Skip("broken")
			}
			if target.OS == "test" && target.PtrSize == 4 {
				// The same reason as linux/32.
				t.Skip("broken")
			}
			t.Parallel()
			testTarget(t, target)
		})
	}
}

func testTarget(t *testing.T, target *prog.Target) {
	seed := int64(time.Now().UnixNano())
	rs := rand.NewSource(seed)
	t.Logf("seed=%v", seed)
	r := rand.New(rs)
	progs := []*prog.Prog{target.GenerateSimpleProg()}
	if p := target.GenerateAllSyzProg(rs); len(p.Calls) != 0 {
		progs = append(progs, p)
	}
	if !testing.Short() {
		progs = append(progs, target.Generate(rs, 10, nil))
	}
	opts := allOptionsSingle(target.OS)
	opts = append(opts, Options{
		Threaded:  true,
		Collide:   true,
		Repeat:    true,
		Procs:     2,
		Sandbox:   "none",
		Repro:     true,
		UseTmpDir: true,
	})
	allPermutations := allOptionsPermutations(target.OS)
	if testing.Short() {
		for i := 0; i < 16; i++ {
			opts = append(opts, allPermutations[r.Intn(len(allPermutations))])
		}
	} else {
		opts = allPermutations
	}
	for pi, p := range progs {
		for opti, opts := range opts {
			p, opts := p, opts
			t.Run(fmt.Sprintf("%v/%v", pi, opti), func(t *testing.T) {
				t.Parallel()
				testOne(t, p, opts)
			})
		}
	}
}

func testOne(t *testing.T, p *prog.Prog, opts Options) {
	src, err := Write(p, opts)
	if err != nil {
		t.Logf("opts: %+v\nprogram:\n%s\n", opts, p.Serialize())
		t.Fatalf("%v", err)
	}
	bin, err := Build(p.Target, src)
	if err != nil {
		if strings.Contains(err.Error(), "no target compiler") {
			t.Skip(err)
		}
		t.Logf("opts: %+v\nprogram:\n%s\n", opts, p.Serialize())
		t.Fatalf("%v", err)
	}
	defer os.Remove(bin)
}