aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/compiler/compiler_test.go
blob: 5415234c6d8ad2150df82be8610a656b11376b2f (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
// 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"
	"reflect"
	"runtime"
	"testing"

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

func TestCompileAll(t *testing.T) {
	eh := func(pos ast.Pos, msg string) {
		t.Logf("%v:%v:%v: %v", pos.File, pos.Line, pos.Col, msg)
	}
	desc := ast.ParseGlob(filepath.Join("..", "..", "sys", "*.txt"), eh)
	if desc == nil {
		t.Fatalf("parsing failed")
	}
	glob := filepath.Join("..", "..", "sys", "*_"+runtime.GOARCH+".const")
	consts := DeserializeConstsGlob(glob, eh)
	if consts == nil {
		t.Fatalf("reading consts failed")
	}
	prog := Compile(desc, consts, eh)
	if prog == nil {
		t.Fatalf("compilation failed")
	}
}

func TestExtractConsts(t *testing.T) {
	desc := ast.Parse([]byte(extractConstsInput), "test", nil)
	if desc == nil {
		t.Fatalf("failed to parse input")
	}
	consts, includes, incdirs, defines := ExtractConsts(desc)
	wantConsts := []string{"CONST1", "CONST10", "CONST11", "CONST12", "CONST13",
		"CONST14", "CONST15", "CONST16",
		"CONST2", "CONST3", "CONST4", "CONST5",
		"CONST6", "CONST7", "CONST8", "CONST9", "__NR_bar", "__NR_foo"}
	if !reflect.DeepEqual(consts, wantConsts) {
		t.Fatalf("got consts:\n%q\nwant:\n%q", consts, wantConsts)
	}
	wantIncludes := []string{"foo/bar.h", "bar/foo.h"}
	if !reflect.DeepEqual(includes, wantIncludes) {
		t.Fatalf("got includes:\n%q\nwant:\n%q", includes, wantIncludes)
	}
	wantIncdirs := []string{"/foo", "/bar"}
	if !reflect.DeepEqual(incdirs, wantIncdirs) {
		t.Fatalf("got incdirs:\n%q\nwant:\n%q", incdirs, wantIncdirs)
	}
	wantDefines := map[string]string{
		"CONST1": "1",
		"CONST2": "FOOBAR + 1",
	}
	if !reflect.DeepEqual(defines, wantDefines) {
		t.Fatalf("got defines:\n%q\nwant:\n%q", defines, wantDefines)
	}
}

const extractConstsInput = `
include <foo/bar.h>
incdir </foo>
include <bar/foo.h>
incdir </bar>

flags = CONST3, CONST2, CONST1

define CONST1 1
define CONST2 FOOBAR + 1

foo(x const[CONST4]) ptr[out, array[int32, CONST5]]
bar$BAR()

str {
	f1	const[CONST6, int32]
	f2	array[array[int8, CONST7]]
}

bar$BAZ(x vma[opt], y vma[CONST8], z vma[CONST9:CONST10])
bar$QUX(s ptr[in, string["foo", CONST11]], x csum[s, pseudo, CONST12])
bar$FOO(x int8[8:CONST13], y int16be[CONST14:10], z intptr[CONST15:CONST16])
`