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. --- pkg/csource/common.go | 5 ----- pkg/csource/csource.go | 6 +++--- pkg/csource/csource_test.go | 4 ++-- pkg/csource/options.go | 9 +++++---- pkg/csource/options_test.go | 4 +++- 5 files changed, 13 insertions(+), 15 deletions(-) (limited to 'pkg/csource') diff --git a/pkg/csource/common.go b/pkg/csource/common.go index 242190180..0dd76cfbb 100644 --- a/pkg/csource/common.go +++ b/pkg/csource/common.go @@ -19,11 +19,6 @@ import ( ) const ( - linux = "linux" - freebsd = "freebsd" - openbsd = "openbsd" - netbsd = "netbsd" - sandboxNone = "none" sandboxSetuid = "setuid" sandboxNamespace = "namespace" diff --git a/pkg/csource/csource.go b/pkg/csource/csource.go index 488d277e1..49babbf41 100644 --- a/pkg/csource/csource.go +++ b/pkg/csource/csource.go @@ -166,7 +166,7 @@ func (ctx *context) generateSyscallDefines() string { fmt.Fprintf(buf, "#define %v%v %v\n", prefix, name, ctx.calls[name]) fmt.Fprintf(buf, "#endif\n") } - if ctx.target.OS == "linux" && ctx.target.PtrSize == 4 { + if ctx.target.OS == targets.Linux && ctx.target.PtrSize == 4 { // This is a dirty hack. // On 32-bit linux mmap translated to old_mmap syscall which has a different signature. // mmap2 has the right signature. syz-extract translates mmap to mmap2, do the same here. @@ -403,7 +403,7 @@ func (ctx *context) copyinVal(w *bytes.Buffer, addr, size uint64, val string, bf } func (ctx *context) copyout(w *bytes.Buffer, call prog.ExecCall, resCopyout bool) { - if ctx.sysTarget.OS == "fuchsia" { + if ctx.sysTarget.OS == targets.Fuchsia { // On fuchsia we have real system calls that return ZX_OK on success, // and libc calls that are casted to function returning intptr_t, // as the result int -1 is returned as 0x00000000ffffffff rather than full -1. @@ -523,7 +523,7 @@ func (ctx *context) hoistIncludes(result []byte) []byte { sortedBottom = append(sortedBottom, include) } else if strings.Contains(include, "") { sortedBottom = append(sortedBottom, include) - } else if ctx.target.OS == freebsd && strings.Contains(include, "") { + } else if ctx.target.OS == targets.FreeBSD && strings.Contains(include, "") { sortedTop = append(sortedTop, include) } else { sorted = append(sorted, include) diff --git a/pkg/csource/csource_test.go b/pkg/csource/csource_test.go index 3eec99792..f6d04b35c 100644 --- a/pkg/csource/csource_test.go +++ b/pkg/csource/csource_test.go @@ -136,7 +136,7 @@ func TestSysTests(t *testing.T) { } t.Run(target.OS+"/"+target.Arch, func(t *testing.T) { t.Parallel() - dir := filepath.Join("..", "..", "sys", target.OS, "test") + dir := filepath.Join("..", "..", "sys", target.OS, targets.TestOS) if !osutil.IsExist(dir) { return } @@ -173,7 +173,7 @@ func TestSysTests(t *testing.T) { func TestExecutorMacros(t *testing.T) { // Ensure that executor does not mis-spell any of the SYZ_* macros. - target, _ := prog.GetTarget("test", "64") + target, _ := prog.GetTarget(targets.TestOS, targets.TestArch64) p := target.Generate(rand.NewSource(0), 1, target.DefaultChoiceTable()) expected := commonDefines(p, Options{}) expected["SYZ_EXECUTOR"] = true diff --git a/pkg/csource/options.go b/pkg/csource/options.go index ba74eb37c..7f2bc5109 100644 --- a/pkg/csource/options.go +++ b/pkg/csource/options.go @@ -12,6 +12,7 @@ import ( "strings" "github.com/google/syzkaller/pkg/mgrconfig" + "github.com/google/syzkaller/sys/targets" ) // Options control various aspects of source generation. @@ -113,10 +114,10 @@ func (opts Options) Check(OS string) error { } func (opts Options) checkLinuxOnly(OS string) error { - if OS == linux { + if OS == targets.Linux { return nil } - if opts.NetInjection && !(OS == openbsd || OS == freebsd || OS == netbsd) { + if opts.NetInjection && !(OS == targets.OpenBSD || OS == targets.FreeBSD || OS == targets.NetBSD) { return fmt.Errorf("option NetInjection is not supported on %v", OS) } if opts.NetDevices { @@ -150,7 +151,7 @@ func (opts Options) checkLinuxOnly(OS string) error { return fmt.Errorf("option Wifi is not supported on %v", OS) } if opts.Sandbox == sandboxNamespace || - (opts.Sandbox == sandboxSetuid && !(OS == openbsd || OS == freebsd || OS == netbsd)) || + (opts.Sandbox == sandboxSetuid && !(OS == targets.OpenBSD || OS == targets.FreeBSD || OS == targets.NetBSD)) || opts.Sandbox == sandboxAndroid { return fmt.Errorf("option Sandbox=%v is not supported on %v", opts.Sandbox, OS) } @@ -183,7 +184,7 @@ func DefaultOpts(cfg *mgrconfig.Config) Options { HandleSegv: true, Repro: true, } - if cfg.TargetOS != linux { + if cfg.TargetOS != targets.Linux { opts.NetInjection = false opts.NetDevices = false opts.NetReset = false diff --git a/pkg/csource/options_test.go b/pkg/csource/options_test.go index 1c403ae06..43cffd1d5 100644 --- a/pkg/csource/options_test.go +++ b/pkg/csource/options_test.go @@ -7,10 +7,12 @@ import ( "fmt" "reflect" "testing" + + "github.com/google/syzkaller/sys/targets" ) func TestParseOptions(t *testing.T) { - for _, opts := range allOptionsSingle("linux") { + for _, opts := range allOptionsSingle(targets.Linux) { data := opts.Serialize() got, err := DeserializeOptions(data) if err != nil { -- cgit mrf-deployment