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

     1  // Copyright 2024 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  package main
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"log"
    10  	"os"
    11  
    12  	"github.com/google/syzkaller/pkg/covermerger"
    13  )
    14  
    15  func main() {
    16  	flagWorkdir := flag.String("workdir", "workdir-cover-aggregation",
    17  		"[optional] used to clone repos")
    18  	flagCleanWorkdir := flag.Bool("clean-workdir", false,
    19  		"[optional] cleans workdir before start")
    20  	flagRepo := flag.String("repo", "", "[required] repo to be used as an aggregation point")
    21  	flagBranch := flag.String("branch", "", "[required] branch to be used as an aggregation point")
    22  	flagCommit := flag.String("commit", "", "[required] commit hash to be used as an aggregation point")
    23  	flag.Parse()
    24  
    25  	config := &covermerger.Config{
    26  		Workdir: *flagWorkdir,
    27  	}
    28  	if *flagRepo == "" || *flagBranch == "" || *flagCommit == "" {
    29  		log.Print("-repo -branch and -commit flags are required")
    30  		return
    31  	}
    32  	if *flagCleanWorkdir {
    33  		if err := os.RemoveAll(config.Workdir); err != nil {
    34  			panic("failed to clean workdir " + config.Workdir)
    35  		}
    36  	}
    37  	mergedCoverage, err := covermerger.AggregateStreamData(
    38  		config,
    39  		os.Stdin,
    40  		covermerger.RepoBranchCommit{
    41  			Repo:   *flagRepo,
    42  			Branch: *flagBranch,
    43  			Commit: *flagCommit,
    44  		})
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  	for fileName, lineStat := range mergedCoverage {
    49  		totalInstrumentedLines := 0
    50  		totalCoveredLines := 0
    51  		if !lineStat.FileExists {
    52  			continue
    53  		}
    54  
    55  		for _, lineHitCount := range lineStat.HitCounts {
    56  			totalInstrumentedLines++
    57  			if lineHitCount > 0 {
    58  				totalCoveredLines++
    59  			}
    60  		}
    61  		coverage := 0.0
    62  		if totalInstrumentedLines != 0 {
    63  			coverage = float64(totalCoveredLines) / float64(totalInstrumentedLines)
    64  		}
    65  		fmt.Printf("%s: %.2f%%", fileName, coverage*100)
    66  	}
    67  }