aboutsummaryrefslogtreecommitdiffstats
path: root/sys/fuchsia/fidlgen/main.go
blob: 315d6a466b77bab1a5a9fbd849b1f29a53035023 (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
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/pkg/tool"
	"github.com/google/syzkaller/sys/fuchsia/layout"
	"github.com/google/syzkaller/sys/targets"
)

func main() {
	targetOS := os.Getenv("TARGETOS")
	targetArch := os.Getenv("TARGETARCH")
	sourceDir := os.Getenv("SOURCEDIR")
	if targetOS != targets.Fuchsia || sourceDir == "" {
		return
	}
	if !osutil.IsExist(sourceDir) {
		tool.Failf("cannot find SOURCEDIR %s", sourceDir)
	}
	target := targets.Get(targetOS, targetArch)
	if target == nil {
		tool.Failf("unknown TARGETARCH %s", targetArch)
	}
	arch := target.KernelHeaderArch

	fidlgenPath := filepath.Join(
		sourceDir,
		"out",
		arch,
		"host_x64",
		"fidlgen_syzkaller",
	)
	if !osutil.IsExist(fidlgenPath) {
		tool.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, "_"), "^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 {
		tool.Failf("parsing failed at %v: %v", errorPos, errorMsg)
	}

	unused := make(map[ast.Node]bool)

	nodes, err := compiler.CollectUnused(desc, target, nil)
	if err != nil {
		tool.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.WriteFileAtomically(file, desc); err != nil {
			tool.Fail(err)
		}
	}
}

func fidlgen(fidlgenPath, jsonPath, txtPathBase string) string {
	if !osutil.IsExist(jsonPath) {
		tool.Failf("cannot find %s", jsonPath)
	}

	out, err := osutil.RunCmd(time.Minute, "",
		fidlgenPath,
		"-json", jsonPath,
		"-output-syz", txtPathBase+".syz.txt",
	)
	if len(out) != 0 {
		fmt.Println(string(out))
	}

	if err != nil {
		tool.Failf("fidlgen failed: %v", err)
	}

	return fmt.Sprintf("%s.syz.txt", txtPathBase)
}