aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/compiler/compiler_test.go
blob: f26c272e6f4daa60bb54b7708994a8007e2e9b32 (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
// Copyright 2017 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 compiler

import (
	"path/filepath"
	"testing"

	"github.com/google/syzkaller/pkg/ast"
	"github.com/google/syzkaller/sys/targets"
)

func TestCompileAll(t *testing.T) {
	for os, arches := range targets.List {
		os, arches := os, arches
		t.Run(os, func(t *testing.T) {
			t.Parallel()
			eh := func(pos ast.Pos, msg string) {
				t.Logf("%v: %v", pos, msg)
			}
			path := filepath.Join("..", "..", "sys", os)
			desc := ast.ParseGlob(filepath.Join(path, "*.txt"), eh)
			if desc == nil {
				t.Fatalf("parsing failed")
			}
			for arch, target := range arches {
				arch, target := arch, target
				t.Run(arch, func(t *testing.T) {
					t.Parallel()
					consts := DeserializeConstsGlob(filepath.Join(path, "*_"+arch+".const"), eh)
					if consts == nil {
						t.Fatalf("reading consts failed")
					}
					prog := Compile(desc, consts, target, eh)
					if prog == nil {
						t.Fatalf("compilation failed")
					}
				})
			}
		})
	}
}

func TestErrors(t *testing.T) {
	consts := map[string]uint64{
		"__NR_foo": 1,
		"C0":       0,
		"C1":       1,
		"C2":       2,
	}
	target := targets.List["test"]["64"]
	for _, name := range []string{"errors.txt", "errors2.txt"} {
		name := name
		t.Run(name, func(t *testing.T) {
			em := ast.NewErrorMatcher(t, filepath.Join("testdata", name))
			desc := ast.Parse(em.Data, name, em.ErrorHandler)
			if desc == nil {
				em.DumpErrors(t)
				t.Fatalf("parsing failed")
			}
			ExtractConsts(desc, target, em.ErrorHandler)
			Compile(desc, consts, target, em.ErrorHandler)
			em.Check(t)
		})
	}
}

func TestFuzz(t *testing.T) {
	inputs := []string{
		"d~^gB̉`i\u007f?\xb0.",
		"da[",
		"define\x98define(define\x98define\x98define\x98define\x98define)define\tdefin",
		"resource g[g]",
	}
	consts := map[string]uint64{"A": 1, "B": 2, "C": 3, "__NR_C": 4}
	eh := func(pos ast.Pos, msg string) {
		t.Logf("%v: %v", pos, msg)
	}
	for _, data := range inputs {
		desc := ast.Parse([]byte(data), "", eh)
		if desc != nil {
			Compile(desc, consts, targets.List["test"]["64"], eh)
		}
	}
}

func TestAlign(t *testing.T) {
	const input = `
foo$0(a ptr[in, s0])
s0 {
	f0	int8
	f1	int16
}

foo$1(a ptr[in, s1])
s1 {
	f0	ptr[in, s2, opt]
}
s2 {
	f1	s1
	f2	array[s1, 2]
	f3	array[array[s1, 2], 2]
}
	`
	desc := ast.Parse([]byte(input), "input", nil)
	if desc == nil {
		t.Fatal("failed to parse")
	}
	p := Compile(desc, map[string]uint64{"__NR_foo": 1}, targets.List["test"]["64"], nil)
	if p == nil {
		t.Fatal("failed to compile")
	}
	got := p.StructDescs[0].Desc
	t.Logf("got: %#v", got)
}