From 6dc47718fdbb634024bcfe47cbc4d7cc781b4cc4 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 3 Apr 2025 10:27:09 +0200 Subject: tools/syz-declextract: allow to run on subset of arches This may be useful for downstream kernels that only build and are supposed to be used with a subset of arches. Some esoteric arches may be broken on such kernels. Allow to ignore them. --- pkg/tool/flags.go | 26 ++++++++++++++++++++++++++ pkg/tool/flags_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) (limited to 'pkg') diff --git a/pkg/tool/flags.go b/pkg/tool/flags.go index ec0d3015f..c74026540 100644 --- a/pkg/tool/flags.go +++ b/pkg/tool/flags.go @@ -9,9 +9,11 @@ import ( "errors" "flag" "fmt" + "sort" "strings" "github.com/google/syzkaller/pkg/log" + "github.com/google/syzkaller/sys/targets" ) type Flag struct { @@ -50,6 +52,30 @@ func ParseFlags(set *flag.FlagSet, args []string) error { return nil } +func ParseArchList(OS, archList string) ([]string, error) { + allArches := targets.List[OS] + if allArches == nil { + return nil, fmt.Errorf("bad OS %q", OS) + } + archMap := make(map[string]bool) + if archList != "" { + for _, arch := range strings.Split(archList, ",") { + if allArches[arch] == nil { + return nil, fmt.Errorf("bad arch %q for OS %q in arches flag", arch, OS) + } + archMap[arch] = true + } + } + var arches []string + for arch := range allArches { + if len(archMap) == 0 || archMap[arch] { + arches = append(arches, arch) + } + } + sort.Strings(arches) + return arches, nil +} + const optionalFlag = "optional" func serializeFlags(flags []Flag) string { diff --git a/pkg/tool/flags_test.go b/pkg/tool/flags_test.go index 0503c48fc..df642bca6 100644 --- a/pkg/tool/flags_test.go +++ b/pkg/tool/flags_test.go @@ -4,6 +4,7 @@ package tool import ( + "errors" "flag" "fmt" "io" @@ -11,6 +12,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" ) func TestParseFlags(t *testing.T) { @@ -84,3 +86,40 @@ func TestCfgsFlagAlreadySet(t *testing.T) { t.Errorf("cfgs.Set got: nil, want: error") } } + +func TestParseArchList(t *testing.T) { + type Test struct { + OS string + In string + Out []string + Err error + } + tests := []Test{ + { + OS: "foo", + Err: errors.New(`bad OS "foo"`), + }, + { + OS: "linux", + In: "amd64,bar", + Err: errors.New(`bad arch "bar" for OS "linux" in arches flag`), + }, + { + OS: "linux", + In: "", + Out: []string{"386", "amd64", "arm", "arm64", "mips64le", "ppc64le", "riscv64", "s390x"}, + }, + { + OS: "linux", + In: "ppc64le,386", + Out: []string{"386", "ppc64le"}, + }, + } + for i, test := range tests { + t.Run(fmt.Sprint(i), func(t *testing.T) { + got, err := ParseArchList(test.OS, test.In) + assert.Equal(t, err, test.Err) + assert.Equal(t, got, test.Out) + }) + } +} -- cgit mrf-deployment