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
|
// Copyright 2024 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 main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/syzkaller/pkg/ast"
"github.com/google/syzkaller/pkg/clangtool"
"github.com/google/syzkaller/pkg/compiler"
"github.com/google/syzkaller/pkg/declextract"
"github.com/google/syzkaller/pkg/ifaceprobe"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/pkg/testutil"
)
var (
flagBin = flag.String("bin", "", "path to syz-declextract binary to use")
flagUpdate = flag.Bool("update", false, "update golden files")
)
func TestClangTool(t *testing.T) {
if *flagBin == "" {
t.Skipf("syz-declextract path is not specified, run with -bin=syz-declextract flag")
}
testEachFile(t, func(t *testing.T, cfg *clangtool.Config, file string) {
out, err := clangtool.Run[declextract.Output](cfg)
if err != nil {
t.Fatal(err)
}
got, err := json.MarshalIndent(out, "", "\t")
if err != nil {
t.Fatal(err)
}
compareGoldenData(t, file+".json", got)
})
}
func TestDeclextract(t *testing.T) {
testEachFile(t, func(t *testing.T, cfg *clangtool.Config, file string) {
// Created cache file to avoid running the clang tool.
goldenFile := file + ".json"
cacheFile := filepath.Join(cfg.KernelObj, filepath.Base(goldenFile))
if err := os.Symlink(goldenFile, cacheFile); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(cfg.KernelSrc, "manual.txt"),
filepath.Join(cfg.KernelObj, "manual.txt")); err != nil {
t.Fatal(err)
}
cfg.ToolBin = "this-is-not-supposed-to-run"
probeInfo := new(ifaceprobe.Info)
probeFile := filepath.Join(cfg.KernelSrc, filepath.Base(file)+".probe")
if osutil.IsExist(probeFile) {
var err error
probeInfo, err = readProbeResult(probeFile)
if err != nil {
t.Fatal(err)
}
}
coverFile := filepath.Join(cfg.KernelSrc, filepath.Base(file)+".cover")
if !osutil.IsExist(coverFile) {
coverFile = ""
}
autoFile := filepath.Join(cfg.KernelObj, filepath.Base(file)+".txt")
runcfg := &config{
autoFile: autoFile,
coverFile: coverFile,
loadProbeInfo: func() (*ifaceprobe.Info, error) {
return probeInfo, nil
},
Config: cfg,
}
res, err := run(runcfg)
if err != nil {
if *flagUpdate {
osutil.CopyFile(autoFile, file+".txt")
osutil.CopyFile(autoFile+".info", file+".info")
}
t.Fatal(err)
}
// Check that descriptions compile.
eh, errors := errorHandler()
full := ast.ParseGlob(filepath.Join(cfg.KernelObj, "*.txt"), eh)
if full == nil {
t.Fatalf("failed to parse full descriptions:\n%s", errors)
}
constInfo := compiler.ExtractConsts(full, target, eh)
if constInfo == nil {
t.Fatalf("failed to compile full descriptions:\n%s", errors)
}
// Fabricate consts.
consts := make(map[string]uint64)
for _, info := range constInfo {
for i, c := range info.Consts {
consts[c.Name] = uint64(i + 1)
}
}
desc := compiler.Compile(full, consts, target, eh)
if desc == nil {
t.Fatalf("failed to compile full descriptions:\n%s", errors)
}
// Check that generated structs have the same size/align as they had in C.
// We assume size/align do not depend on const values (which we fabricated).
for _, typ := range desc.Types {
info := res.StructInfo[typ.Name()]
if info == nil {
continue
}
if typ.Size() != uint64(info.Size) || typ.Alignment() != uint64(info.Align) {
t.Errorf("incorrect generated type %v: size %v/%v align %v/%v",
typ.Name(), typ.Size(), info.Size, typ.Alignment(), info.Align)
}
}
// TODO: Ensure that none of the syscalls will be disabled by TransitivelyEnabledCalls.
compareGoldenFile(t, file+".txt", autoFile)
compareGoldenFile(t, file+".info", autoFile+".info")
})
}
func testEachFile(t *testing.T, fn func(t *testing.T, cfg *clangtool.Config, file string)) {
testdata, err := filepath.Abs("testdata")
if err != nil {
t.Fatal(err)
}
files, err := filepath.Glob(filepath.Join(testdata, "*.c"))
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatal("found no source files")
}
for _, file := range files {
t.Run(filepath.Base(file), func(t *testing.T) {
t.Parallel()
buildDir := t.TempDir()
commands := fmt.Sprintf(`[{
"file": "%s",
"directory": "%s",
"command": "clang -c %s -DKBUILD_BASENAME=foo"
}]`,
file, buildDir, file)
dbFile := filepath.Join(buildDir, "compile_commands.json")
if err := os.WriteFile(dbFile, []byte(commands), 0600); err != nil {
t.Fatal(err)
}
cfg := &clangtool.Config{
ToolBin: *flagBin,
KernelSrc: testdata,
KernelObj: buildDir,
CacheFile: filepath.Join(buildDir, filepath.Base(file)+".json"),
DebugTrace: &testutil.Writer{TB: t},
}
fn(t, cfg, file)
})
}
}
func compareGoldenFile(t *testing.T, goldenFile, gotFile string) {
got, err := os.ReadFile(gotFile)
if err != nil {
t.Fatal(err)
}
compareGoldenData(t, goldenFile, got)
}
func compareGoldenData(t *testing.T, goldenFile string, got []byte) {
if *flagUpdate {
if err := os.WriteFile(goldenFile, got, 0644); err != nil {
t.Fatal(err)
}
}
want, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(got, want); diff != "" {
t.Fatal(diff)
}
}
|