-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
119 lines (98 loc) · 2.55 KB
/
main.go
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
// Copyright 2018 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 (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/google/syzkaller/pkg/ast"
"github.com/google/syzkaller/pkg/compiler"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/sys/fuchsia/layout"
"github.com/google/syzkaller/sys/targets"
)
func main() {
targetArch := os.Getenv("TARGETARCH")
target := targets.Get("fuchsia", targetArch)
if target == nil {
failf("unknown TARGETARCH %s", targetArch)
}
arch := target.KernelHeaderArch
sourceDir := os.Getenv("SOURCEDIR")
if !osutil.IsExist(sourceDir) {
failf("cannot find SOURCEDIR %s", sourceDir)
}
fidlgenPath := filepath.Join(
sourceDir,
"out",
arch,
"host_x64",
"fidlgen",
)
if !osutil.IsExist(fidlgenPath) {
failf("cannot find fidlgen %s", fidlgenPath)
}
var newFiles []string
for _, fidlLib := range layout.AllFidlLibraries {
jsonPath := filepath.Join(sourceDir, "out", arch, fidlLib.PathToJSONIr())
txtPathBase := strings.Replace(strings.Join(fidlLib.FqName, "_"), "^fuchsia", "fidl", 1)
txtPath := fidlgen(
fidlgenPath,
jsonPath,
txtPathBase,
)
newFiles = append(newFiles, txtPath)
}
var errorPos ast.Pos
var errorMsg string
desc := ast.ParseGlob("*.txt", func(pos ast.Pos, msg string) {
errorPos = pos
errorMsg = msg
})
if desc == nil {
failf("parsing failed at %v: %v", errorPos, errorMsg)
}
unused := make(map[ast.Node]bool)
nodes, err := compiler.CollectUnused(desc, target, nil)
if err != nil {
failf("collect unused nodes failed: %v", err)
}
for _, n := range nodes {
unused[n] = true
}
pruned := desc.Filter(func(n ast.Node) bool {
_, ok := unused[n]
return !ok
})
for _, file := range newFiles {
desc := ast.Format(pruned.Filter(func(n ast.Node) bool {
pos, _, _ := n.Info()
return pos.File == file
}))
if err := osutil.WriteFile(file, desc); err != nil {
failf("%v", err)
}
}
}
func fidlgen(fidlgenPath string, jsonPath string, txtPathBase string) string {
if !osutil.IsExist(jsonPath) {
failf("cannot find %s", jsonPath)
}
_, err := osutil.RunCmd(time.Minute, "",
fidlgenPath,
"-generators", "syzkaller",
"-json", jsonPath,
"-output-base", txtPathBase,
"-include-base", txtPathBase,
)
if err != nil {
failf("fidlgen failed: %v", err)
}
return fmt.Sprintf("%s.syz.txt", txtPathBase)
}
func failf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}