github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/tools/syz-minconfig/minconfig.go (about)

     1  // Copyright 2020 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  // syz-minconfig is a tool for manual checking of config minimization functionality in pkg/kconfig/minimize.go.
     5  // Example use:
     6  //
     7  //	$ go run tools/syz-minconfig/minconfig.go -sourcedir /src/linux -configs CAIF_NETDEV,CAIF_USB \
     8  //		-base dashboard/config/linux/upstream-kasan-base.config \
     9  //		-full dashboard/config/linux/upstream-kasan.config \
    10  package main
    11  
    12  import (
    13  	"flag"
    14  	"os"
    15  	"path/filepath"
    16  	"runtime"
    17  	"strings"
    18  
    19  	"github.com/google/syzkaller/pkg/debugtracer"
    20  	"github.com/google/syzkaller/pkg/kconfig"
    21  	"github.com/google/syzkaller/pkg/tool"
    22  	"github.com/google/syzkaller/sys/targets"
    23  )
    24  
    25  func main() {
    26  	var (
    27  		flagSourceDir = flag.String("sourcedir", "", "kernel sources dir")
    28  		flagBase      = flag.String("base", "", "baseline config")
    29  		flagFull      = flag.String("full", "", "full config")
    30  		flagConfigs   = flag.String("configs", "", "comma-separated list of configs for the crash predicate")
    31  		flagArch      = flag.String("arch", runtime.GOARCH, "kernel arch")
    32  	)
    33  	flag.Parse()
    34  	kconf, err := kconfig.Parse(targets.Get("linux", *flagArch), filepath.Join(*flagSourceDir, "Kconfig"))
    35  	if err != nil {
    36  		tool.Fail(err)
    37  	}
    38  	base, err := kconfig.ParseConfig(*flagBase)
    39  	if err != nil {
    40  		tool.Fail(err)
    41  	}
    42  	full, err := kconfig.ParseConfig(*flagFull)
    43  	if err != nil {
    44  		tool.Fail(err)
    45  	}
    46  	pred := func(candidate *kconfig.ConfigFile) (bool, error) {
    47  		for _, cfg := range strings.Split(*flagConfigs, ",") {
    48  			if candidate.Value(cfg) == kconfig.No {
    49  				return false, nil
    50  			}
    51  		}
    52  		return true, nil
    53  	}
    54  	gt := &debugtracer.GenericTracer{
    55  		TraceWriter: os.Stdout,
    56  	}
    57  	res, err := kconf.Minimize(base, full, pred, 0, gt)
    58  	if err != nil {
    59  		tool.Fail(err)
    60  	}
    61  	os.Stdout.Write(res.Serialize())
    62  }