From e6e35dba937599d098fc034eff2686e5ddc409e9 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 26 Oct 2020 10:51:06 +0100 Subject: sys/targets: add OS/Arch name consts We use strings to identify OS/Arch. These strings are duplicated throughout the code base massively. golangci-lint points to possiblity of typos and duplication. We already had to define these names in pkg/csource and disable checking for prog package. A future change triggers such warnings in another package. Add OS/Arch name consts to sys/targets so that they can be used to refer to OS/Arch. Use the consts everywhere. --- tools/syz-check/check.go | 6 +++--- tools/syz-imagegen/imagegen.go | 8 +++++--- tools/syz-kconf/kconf.go | 6 +++--- tools/syz-reprolist/reprolist.go | 5 +++-- tools/syz-runtest/runtest.go | 2 +- tools/syz-trace2syz/proggen/fuzz.go | 3 ++- tools/syz-trace2syz/proggen/proggen_test.go | 3 ++- tools/syz-trace2syz/trace2syz.go | 5 +++-- 8 files changed, 22 insertions(+), 16 deletions(-) (limited to 'tools') diff --git a/tools/syz-check/check.go b/tools/syz-check/check.go index 86048f9a4..de7145dfb 100644 --- a/tools/syz-check/check.go +++ b/tools/syz-check/check.go @@ -52,7 +52,7 @@ func main() { flagNetlink = flag.Bool("netlink", true, "do checking of netlink policies") ) arches := make(map[string]*string) - for arch := range targets.List["linux"] { + for arch := range targets.List[targets.Linux] { arches[arch] = flag.String("obj-"+arch, "", arch+" kernel object file") } failf := func(msg string, args ...interface{}) { @@ -152,7 +152,7 @@ func writeWarnings(OS string, narches int, warnings []Warn) error { byFile := make(map[string][]Warn) for _, warn := range warnings { // KVM is not supported on ARM completely. - if OS == "linux" && warn.arch == "arm" && strings.HasSuffix(warn.pos.File, "_kvm.txt") { + if OS == targets.Linux && warn.arch == targets.ARM && strings.HasSuffix(warn.pos.File, "_kvm.txt") { continue } byFile[warn.pos.File] = append(byFile[warn.pos.File], warn) @@ -358,7 +358,7 @@ func parseDescriptions(OS, arch string) ([]prog.Type, map[string]*ast.Struct, [] // Finally we compare our descriptions with the kernel policy description. func checkNetlink(OS, arch, obj string, structTypes []prog.Type, locs map[string]*ast.Struct) ([]Warn, error) { - if arch != "amd64" { + if arch != targets.AMD64 { // Netlink policies are arch-independent (?), // so no need to check all arches. // Also our definition of nlaPolicy below is 64-bit specific. diff --git a/tools/syz-imagegen/imagegen.go b/tools/syz-imagegen/imagegen.go index 033a1dd58..4ca608254 100644 --- a/tools/syz-imagegen/imagegen.go +++ b/tools/syz-imagegen/imagegen.go @@ -29,6 +29,7 @@ import ( "github.com/google/syzkaller/pkg/osutil" "github.com/google/syzkaller/prog" _ "github.com/google/syzkaller/sys" + "github.com/google/syzkaller/sys/targets" ) // FileSystem represents one file system. @@ -478,7 +479,7 @@ func main() { } return } - target, err := prog.GetTarget("linux", "amd64") + target, err := prog.GetTarget(targets.Linux, targets.AMD64) if err != nil { failf("%v", err) } @@ -587,7 +588,7 @@ func generateImages(target *prog.Target, flagFS string, list bool) ([]*Image, er fmt.Printf("%v [%v images]\n", fs.Name, index) continue } - files, err := filepath.Glob(filepath.Join("sys", "linux", "test", "syz_mount_image_"+fs.Name+"_*")) + files, err := filepath.Glob(filepath.Join("sys", targets.Linux, "test", "syz_mount_image_"+fs.Name+"_*")) if err != nil { return nil, fmt.Errorf("error reading output dir: %v", err) } @@ -634,7 +635,8 @@ func (image *Image) generate() error { } func (image *Image) generateSize() error { - outFile := filepath.Join("sys", "linux", "test", fmt.Sprintf("syz_mount_image_%v_%v", image.fs.Name, image.index)) + outFile := filepath.Join("sys", targets.Linux, "test", + fmt.Sprintf("syz_mount_image_%v_%v", image.fs.Name, image.index)) image.disk = outFile + ".img" f, err := os.Create(image.disk) if err != nil { diff --git a/tools/syz-kconf/kconf.go b/tools/syz-kconf/kconf.go index 02ce0e929..841366146 100644 --- a/tools/syz-kconf/kconf.go +++ b/tools/syz-kconf/kconf.go @@ -49,7 +49,7 @@ func main() { if *flagSourceDir == "" { failf("missing mandatory flag -sourcedir") } - repo, err := vcs.NewRepo("linux", "", *flagSourceDir, vcs.OptPrecious) + repo, err := vcs.NewRepo(targets.Linux, "", *flagSourceDir, vcs.OptPrecious) if err != nil { failf("failed to create repo: %v", err) } @@ -307,12 +307,12 @@ func (ctx *Context) addDependentConfigs(dst *kconfig.ConfigFile, include []strin } func (ctx *Context) setTarget() error { - for _, target := range targets.List["linux"] { + for _, target := range targets.List[targets.Linux] { if ctx.Inst.Features[target.KernelArch] { if ctx.Target != nil { return fmt.Errorf("arch is set twice") } - ctx.Target = targets.GetEx("linux", target.Arch, ctx.Inst.Features[featClang]) + ctx.Target = targets.GetEx(targets.Linux, target.Arch, ctx.Inst.Features[featClang]) } } if ctx.Target == nil { diff --git a/tools/syz-reprolist/reprolist.go b/tools/syz-reprolist/reprolist.go index 5c096bfdf..366f82116 100644 --- a/tools/syz-reprolist/reprolist.go +++ b/tools/syz-reprolist/reprolist.go @@ -19,6 +19,7 @@ import ( "github.com/google/syzkaller/pkg/csource" "github.com/google/syzkaller/pkg/osutil" "github.com/google/syzkaller/pkg/vcs" + "github.com/google/syzkaller/sys/targets" ) var ( @@ -114,7 +115,7 @@ func writeRepros(bugchan chan *dashapi.BugReport) { } log.Printf("%v: %v: C repro", bug.ID, bug.BugStatus) arch := "" - if bug.Arch != "" && bug.Arch != "amd64" { + if bug.Arch != "" && bug.Arch != targets.AMD64 { arch = fmt.Sprintf(" arch:%v", bug.Arch) } repro := []byte(fmt.Sprintf("// %v\n// %v/bug?id=%v\n// status:%v%v\n", @@ -173,7 +174,7 @@ func createProg2CArgs(bug *dashapi.BugReport, opts csource.Options, file string) if haveOSFlag { args = append(args, "-os", *flagOS) } - if bug.Arch != "" && bug.Arch != "amd64" { + if bug.Arch != "" && bug.Arch != targets.AMD64 { args = append(args, "-arch", bug.Arch) } if opts.Fault { diff --git a/tools/syz-runtest/runtest.go b/tools/syz-runtest/runtest.go index d961dc867..b6f25d784 100644 --- a/tools/syz-runtest/runtest.go +++ b/tools/syz-runtest/runtest.go @@ -116,7 +116,7 @@ func main() { fmt.Printf("%-24v: %v calls enabled\n", sandbox+" sandbox", len(calls)) } ctx := &runtest.Context{ - Dir: filepath.Join(cfg.Syzkaller, "sys", target.OS, "test"), + Dir: filepath.Join(cfg.Syzkaller, "sys", target.OS, targets.TestOS), Target: target, Features: mgr.checkResult.Features, EnabledCalls: enabledCalls, diff --git a/tools/syz-trace2syz/proggen/fuzz.go b/tools/syz-trace2syz/proggen/fuzz.go index 7f74534b9..da4fae7bb 100644 --- a/tools/syz-trace2syz/proggen/fuzz.go +++ b/tools/syz-trace2syz/proggen/fuzz.go @@ -8,10 +8,11 @@ package proggen import ( "github.com/google/syzkaller/prog" _ "github.com/google/syzkaller/sys/linux/gen" // pull in the target + "github.com/google/syzkaller/sys/targets" ) var linuxTarget = func() *prog.Target { - target, err := prog.GetTarget("linux", "amd64") + target, err := prog.GetTarget(targets.Linux, targets.AMD64) if err != nil { panic(err) } diff --git a/tools/syz-trace2syz/proggen/proggen_test.go b/tools/syz-trace2syz/proggen/proggen_test.go index 0eaec0d8d..451147bed 100644 --- a/tools/syz-trace2syz/proggen/proggen_test.go +++ b/tools/syz-trace2syz/proggen/proggen_test.go @@ -12,6 +12,7 @@ import ( "github.com/google/syzkaller/prog" _ "github.com/google/syzkaller/sys" + "github.com/google/syzkaller/sys/targets" "github.com/google/syzkaller/tools/syz-trace2syz/parser" ) @@ -265,7 +266,7 @@ r0 = openat$rtc(0xffffffffffffff9c, &(0x7f0000000000)='/dev/rtc0\x00', 0x0, 0x0) ioctl$RTC_WKALM_SET(r0, 0x4028700f, &(0x7f0000000040)={0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x10000, 0x5181}})`, }, } - target, err := prog.GetTarget("linux", "amd64") + target, err := prog.GetTarget(targets.Linux, targets.AMD64) if err != nil { t.Fatal(err) } diff --git a/tools/syz-trace2syz/trace2syz.go b/tools/syz-trace2syz/trace2syz.go index d634f137f..89081d57c 100644 --- a/tools/syz-trace2syz/trace2syz.go +++ b/tools/syz-trace2syz/trace2syz.go @@ -22,6 +22,7 @@ import ( "github.com/google/syzkaller/pkg/osutil" "github.com/google/syzkaller/prog" _ "github.com/google/syzkaller/sys" + "github.com/google/syzkaller/sys/targets" "github.com/google/syzkaller/tools/syz-trace2syz/proggen" ) @@ -32,8 +33,8 @@ var ( ) const ( - goos = "linux" // Target OS - arch = "amd64" // Target architecture + goos = targets.Linux // Target OS + arch = targets.AMD64 // Target architecture ) func main() { -- cgit mrf-deployment