aboutsummaryrefslogtreecommitdiffstats
path: root/tools/syz-minconfig/minconfig.go
blob: 3a4693d080d057d62999d45f010c48f5421bb2da (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
// Copyright 2020 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.

// syz-minconfig is a tool for manual checking of config minimization functionality in pkg/kconfig/minimize.go.
// Example use:
//
//	$ go run tools/syz-minconfig/minconfig.go -sourcedir /src/linux -configs CAIF_NETDEV,CAIF_USB \
//		-base dashboard/config/linux/upstream-kasan-base.config \
//		-full dashboard/config/linux/upstream-kasan.config \
package main

import (
	"flag"
	"os"
	"path/filepath"
	"runtime"
	"strings"

	"github.com/google/syzkaller/pkg/debugtracer"
	"github.com/google/syzkaller/pkg/kconfig"
	"github.com/google/syzkaller/pkg/tool"
	"github.com/google/syzkaller/sys/targets"
)

func main() {
	var (
		flagSourceDir = flag.String("sourcedir", "", "kernel sources dir")
		flagBase      = flag.String("base", "", "baseline config")
		flagFull      = flag.String("full", "", "full config")
		flagConfigs   = flag.String("configs", "", "comma-separated list of configs for the crash predicate")
		flagArch      = flag.String("arch", runtime.GOARCH, "kernel arch")
	)
	flag.Parse()
	kconf, err := kconfig.Parse(targets.Get("linux", *flagArch), filepath.Join(*flagSourceDir, "Kconfig"))
	if err != nil {
		tool.Fail(err)
	}
	base, err := kconfig.ParseConfig(*flagBase)
	if err != nil {
		tool.Fail(err)
	}
	full, err := kconfig.ParseConfig(*flagFull)
	if err != nil {
		tool.Fail(err)
	}
	pred := func(candidate *kconfig.ConfigFile) (bool, error) {
		for _, cfg := range strings.Split(*flagConfigs, ",") {
			if candidate.Value(cfg) == kconfig.No {
				return false, nil
			}
		}
		return true, nil
	}
	gt := &debugtracer.GenericTracer{
		TraceWriter: os.Stdout,
	}
	res, err := kconf.Minimize(base, full, pred, 0, gt)
	if err != nil {
		tool.Fail(err)
	}
	os.Stdout.Write(res.Serialize())
}