Best Syzkaller code snippet using main.InstanceStatsTable
testbed.go
Source:testbed.go
1// Copyright 2021 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// syz-testbed automatically checks out, builds and sets up a number of syzkaller instances.4// This might be very helpful e.g. when gauging the effect of new changes on the total syzkaller5// performance.6// For details see docs/syz_testbed.md.7package main8import (9 "encoding/json"10 "flag"11 "fmt"12 "log"13 "net"14 "os"15 "path/filepath"16 "regexp"17 "strconv"18 "time"19 "github.com/google/syzkaller/pkg/config"20 syz_instance "github.com/google/syzkaller/pkg/instance"21 "github.com/google/syzkaller/pkg/mgrconfig"22 "github.com/google/syzkaller/pkg/osutil"23 "github.com/google/syzkaller/pkg/tool"24 "github.com/google/syzkaller/pkg/vcs"25)26var (27 flagConfig = flag.String("config", "", "config file")28 flagCleanup = flag.Bool("cleanup", false, "remove existing work directories")29)30type TestbedConfig struct {31 Corpus string `json:"corpus"` // path to the corpus file32 Workdir string `json:"workdir"` // instances will be checked out there33 ManagerConfig json.RawMessage `json:"manager_config"` // base manager config34 Checkouts []TestbedCheckout `json:"checkouts"`35}36type TestbedCheckout struct {37 Name string `json:"name"`38 Repo string `json:"repo"`39 Branch string `json:"branch"`40 Count int `json:"count"`41}42type CheckoutInfo struct {43 Path string44 Name string45 Instances []InstanceInfo46}47// The essential information about an already prepared instance.48type InstanceInfo struct {49 Name string50 Workdir string51 BenchFile string52 LogFile string53 HTTP string54 ExecCommand string55 ExecCommandArgs []string56}57func main() {58 flag.Parse()59 cfg := &TestbedConfig{}60 err := config.LoadFile(*flagConfig, &cfg)61 if err != nil {62 tool.Failf("failed to read config: %s", err)63 }64 err = checkConfig(cfg)65 if err != nil {66 tool.Failf("invalid config: %s", err)67 }68 managerCfg, err := mgrconfig.LoadPartialData(cfg.ManagerConfig)69 if err != nil {70 tool.Failf("failed to parse manager config: %s", err)71 }72 if managerCfg.HTTP == "" {73 managerCfg.HTTP = ":50000"74 }75 checkouts := []*CheckoutInfo{}76 for _, co := range cfg.Checkouts {77 checkouts = append(checkouts, newCheckout(co, cfg, managerCfg))78 }79 log.Printf("------------------")80 for _, co := range checkouts {81 for _, instance := range co.Instances {82 go runInstance(instance)83 }84 }85 go collectStats(cfg, checkouts)86 // Block the execution indefinitely.87 // Either the process will be killed or it will exit itself if one of the instances fails.88 select {}89}90func collectStats(cfg *TestbedConfig, checkouts []*CheckoutInfo) {91 const period = 90 * time.Second92 benchFolder := filepath.Join(cfg.Workdir, "benches")93 err := osutil.MkdirAll(benchFolder)94 if err != nil {95 tool.Failf("failed to create bench folder: %s", err)96 }97 tableStats := map[string]func(checkouts []*CheckoutInfo) ([][]string, error){98 "bugs.csv": generateBugTable,99 "checkout_stats.csv": checkoutStatsTable,100 "instance_stats.csv": instanceStatsTable,101 }102 for {103 time.Sleep(period)104 for fileName, genFunc := range tableStats {105 table, err := genFunc(checkouts)106 if err == nil {107 saveTableAsCsv(table, filepath.Join(cfg.Workdir, fileName))108 }109 }110 for _, checkout := range checkouts {111 fileName := fmt.Sprintf("avg_%v.txt", checkout.Name)112 saveAvgBenchFile(checkout, filepath.Join(benchFolder, fileName))113 }114 }115}116func runInstance(info InstanceInfo) {117 logfile, err := os.Create(info.LogFile)118 if err != nil {119 tool.Failf("[%s] failed to create logfile: %s", info.Name, err)120 }121 cmd := osutil.GraciousCommand(info.ExecCommand, info.ExecCommandArgs...)122 cmd.Stdout = logfile123 cmd.Stderr = logfile124 err = cmd.Start()125 if err != nil {126 tool.Failf("[%s] failed to start instance: %s", info.Name, err)127 }128 log.Printf("[%s] Instance started. Listening on %s", info.Name, info.HTTP)129 logfile.Close()130 err = cmd.Wait()131 tool.Failf("[%s] Instance exited: %s", info.Name, err)132}133func newCheckout(co TestbedCheckout, cfg *TestbedConfig, managerCfg *mgrconfig.Config) *CheckoutInfo {134 log.Printf("[%s] Checking out", co.Name)135 path := filepath.Join(cfg.Workdir, "checkouts", co.Name)136 if osutil.IsExist(path) {137 if !*flagCleanup {138 tool.Failf("path %s already exists", path)139 }140 osutil.RemoveAll(path)141 }142 repo := vcs.NewSyzkallerRepo(path)143 commit, err := repo.Poll(co.Repo, co.Branch)144 if err != nil {145 tool.Failf("failed to checkout %s (%s): %s", co.Repo, co.Branch, err)146 }147 log.Printf("[%s] Done. Latest commit: %s", co.Name, commit)148 log.Printf("[%s] Building", co.Name)149 if _, err := osutil.RunCmd(time.Hour, path, syz_instance.MakeBin); err != nil {150 tool.Failf("[%s] Make failed: %s", co.Name, err)151 }152 checkoutInfo := CheckoutInfo{153 Name: co.Name,154 Path: path,155 }156 for i := 1; i <= co.Count; i++ {157 name := fmt.Sprintf("%v-%d", co.Name, i)158 log.Printf("[%s] Generating workdir", name)159 workdir := filepath.Join(path, fmt.Sprintf("workdir_%d", i))160 err = osutil.MkdirAll(workdir)161 if err != nil {162 tool.Failf("failed to create dir %s", workdir)163 }164 if cfg.Corpus != "" {165 corpusPath := filepath.Join(workdir, "corpus.db")166 err = osutil.CopyFile(cfg.Corpus, corpusPath)167 if err != nil {168 tool.Failf("failed to copy corpus from %s: %s", cfg.Corpus, err)169 }170 }171 log.Printf("[%s] Generating syz-manager config", name)172 managerCfg.Name = name173 managerCfg.Workdir = workdir174 managerCfg.Syzkaller = path175 managerCfgPath := filepath.Join(path, fmt.Sprintf("syz_%d.cnf", i))176 err = config.SaveFile(managerCfgPath, managerCfg)177 if err != nil {178 tool.Failf("failed to save manager config to %s: %s", managerCfgPath, err)179 }180 bench := filepath.Join(path, fmt.Sprintf("bench_%d.txt", i))181 log := filepath.Join(path, fmt.Sprintf("log_%d.txt", i))182 checkoutInfo.Instances = append(checkoutInfo.Instances, InstanceInfo{183 Name: managerCfg.Name,184 Workdir: workdir,185 BenchFile: bench,186 LogFile: log,187 HTTP: managerCfg.HTTP,188 ExecCommand: filepath.Join(path, "bin", "syz-manager"),189 ExecCommandArgs: []string{"-config", managerCfgPath, "-bench", bench},190 })191 managerCfg.HTTP, err = increasePort(managerCfg.HTTP)192 if err != nil {193 tool.Failf("failed to inrease port number: %s", err)194 }195 }196 return &checkoutInfo197}198func increasePort(http string) (string, error) {199 host, portStr, err := net.SplitHostPort(http)200 if err != nil {201 return "", fmt.Errorf("invalid http value: %s", http)202 }203 port, err := strconv.Atoi(portStr)204 if err != nil {205 return "", err206 }207 return net.JoinHostPort(host, fmt.Sprintf("%d", port+1)), nil208}209func checkConfig(cfg *TestbedConfig) error {210 if cfg.Workdir == "" {211 return fmt.Errorf("workdir is empty")212 }213 cfg.Workdir = osutil.Abs(cfg.Workdir)214 err := osutil.MkdirAll(cfg.Workdir)215 if err != nil {216 return err217 }218 if cfg.Corpus != "" && !osutil.IsExist(cfg.Corpus) {219 return fmt.Errorf("corpus %v does not exist", cfg.Corpus)220 }221 cfg.Corpus = osutil.Abs(cfg.Corpus)222 instanceNameRe := regexp.MustCompile(`^[0-9a-z\-]{1,20}$`)223 names := make(map[string]bool)224 for idx := range cfg.Checkouts {225 co := &cfg.Checkouts[idx]226 if !vcs.CheckRepoAddress(co.Repo) {227 return fmt.Errorf("invalid repo: %s", co.Repo)228 }229 if co.Branch == "" {230 co.Branch = "master"231 } else if !vcs.CheckBranch(co.Branch) {232 return fmt.Errorf("invalid branch: %s", co.Branch)233 }234 if co.Count < 0 {235 return fmt.Errorf("count cannot be negative")236 } else if co.Count == 0 {237 // The default value.238 co.Count = 1239 }240 if !instanceNameRe.MatchString(co.Name) {241 return fmt.Errorf("invalid instance name: %v", co.Name)242 }243 if names[co.Name] {244 return fmt.Errorf("duplicate instance name: %v", co.Name)245 }246 names[co.Name] = true247 }248 return nil249}...
targets.go
Source:targets.go
...129 }130 tableStats := map[string]func(view StatView) (*Table, error){131 "bugs.csv": (StatView).GenerateBugTable,132 "checkout_stats.csv": (StatView).StatsTable,133 "instance_stats.csv": (StatView).InstanceStatsTable,134 }135 for fileName, genFunc := range tableStats {136 table, err := genFunc(view)137 if err == nil {138 table.SaveAsCsv(filepath.Join(dir, fileName))139 } else {140 log.Printf("stat generation error: %s", err)141 }142 }143 _, err = view.SaveAvgBenches(benchDir)144 return err145}146// TODO: consider other repro testing modes.147// E.g. group different logs by title. Then we could also set different sets of inputs...
stats.go
Source:stats.go
1// Copyright 2021 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.3package main4import (5 "encoding/csv"6 "encoding/json"7 "fmt"8 "io/ioutil"9 "os"10 "path/filepath"11 "strings"12 "github.com/google/syzkaller/pkg/osutil"13)14type BugInfo struct {15 Title string16}17// TODO: we're implementing this functionaity at least the 3rd time (see syz-manager/html18// and tools/reporter). Create a more generic implementation and put it into a globally19// visible package.20func collectBugs(workdir string) ([]BugInfo, error) {21 crashdir := filepath.Join(workdir, "crashes")22 dirs, err := osutil.ListDir(crashdir)23 if err != nil {24 return nil, err25 }26 bugs := []BugInfo{}27 for _, dir := range dirs {28 titleBytes, err := ioutil.ReadFile(filepath.Join(crashdir, dir, "description"))29 if err != nil {30 return nil, err31 }32 title := strings.TrimSpace(string(titleBytes))33 bugs = append(bugs, BugInfo{title})34 }35 return bugs, nil36}37func readBenches(benchFile string) ([]map[string]uint64, error) {38 f, err := os.Open(benchFile)39 if err != nil {40 return nil, err41 }42 defer f.Close()43 dec := json.NewDecoder(f)44 ret := []map[string]uint64{}45 for dec.More() {46 curr := make(map[string]uint64)47 if err := dec.Decode(&curr); err == nil {48 ret = append(ret, curr)49 }50 }51 return ret, nil52}53func avgStats(infos []map[string]uint64) map[string]uint64 {54 ret := make(map[string]uint64)55 if len(infos) == 0 {56 return ret57 }58 for _, stat := range infos {59 for key, value := range stat {60 ret[key] += value61 }62 }63 for key, value := range ret {64 ret[key] = value / uint64(len(infos))65 }66 return ret67}68type BugSummary struct {69 title string70 found map[*CheckoutInfo]bool71}72// If there are several instances belonging to a single checkout, we're interested in the73// set of bugs found by at least one of those instances.74func summarizeBugs(checkouts []*CheckoutInfo) ([]*BugSummary, error) {75 bugsMap := make(map[string]*BugSummary)76 for _, checkout := range checkouts {77 for _, instance := range checkout.Instances {78 bugs, err := collectBugs(instance.Workdir)79 if err != nil {80 return nil, err81 }82 for _, bug := range bugs {83 summary := bugsMap[bug.Title]84 if summary == nil {85 summary = &BugSummary{86 title: bug.Title,87 found: make(map[*CheckoutInfo]bool),88 }89 bugsMap[bug.Title] = summary90 }91 summary.found[checkout] = true92 }93 }94 }95 summaries := []*BugSummary{}96 for _, value := range bugsMap {97 summaries = append(summaries, value)98 }99 return summaries, nil100}101// For each checkout, take the union of sets of bugs found by each instance.102// Then output these unions as a single table.103func generateBugTable(checkouts []*CheckoutInfo) ([][]string, error) {104 table := [][]string{}105 titles := []string{""}106 for _, checkout := range checkouts {107 titles = append(titles, checkout.Name)108 }109 summaries, err := summarizeBugs(checkouts)110 if err != nil {111 return nil, err112 }113 table = append(table, titles)114 for _, bug := range summaries {115 row := []string{bug.title}116 for _, checkout := range checkouts {117 val := ""118 if bug.found[checkout] {119 val = "YES"120 }121 row = append(row, val)122 }123 table = append(table, row)124 }125 return table, nil126}127type StatGroup struct {128 Name string129 Instances []InstanceInfo130}131func genericStatsTable(groups []StatGroup) ([][]string, error) {132 // Map: stats key x group name -> value.133 cells := make(map[string]map[string]string)134 for _, group := range groups {135 infos := []map[string]uint64{}136 for _, instance := range group.Instances {137 records, err := readBenches(instance.BenchFile)138 if err != nil {139 return nil, err140 }141 if len(records) > 0 {142 infos = append(infos, records[len(records)-1])143 }144 }145 for key, value := range avgStats(infos) {146 if _, ok := cells[key]; !ok {147 cells[key] = make(map[string]string)148 }149 cells[key][group.Name] = fmt.Sprintf("%d", value)150 }151 }152 title := []string{""}153 for _, group := range groups {154 title = append(title, group.Name)155 }156 table := [][]string{title}157 for key, valuesMap := range cells {158 row := []string{key}159 for _, group := range groups {160 row = append(row, valuesMap[group.Name])161 }162 table = append(table, row)163 }164 return table, nil165}166func checkoutStatsTable(checkouts []*CheckoutInfo) ([][]string, error) {167 groups := []StatGroup{}168 for _, checkout := range checkouts {169 groups = append(groups, StatGroup{170 Name: checkout.Name,171 Instances: checkout.Instances,172 })173 }174 return genericStatsTable(groups)175}176func instanceStatsTable(checkouts []*CheckoutInfo) ([][]string, error) {177 groups := []StatGroup{}178 for _, checkout := range checkouts {179 for _, instance := range checkout.Instances {180 groups = append(groups, StatGroup{181 Name: instance.Name,182 Instances: []InstanceInfo{instance},183 })184 }185 }186 return genericStatsTable(groups)187}188// Average bench files of several instances into a single bench file.189func saveAvgBenchFile(checkout *CheckoutInfo, fileName string) error {190 allRecords := [][]map[string]uint64{}191 for _, instance := range checkout.Instances {192 records, err := readBenches(instance.BenchFile)193 if err != nil {194 return err195 }196 allRecords = append(allRecords, records)197 }198 f, err := os.Create(fileName)199 if err != nil {200 return err201 }202 defer f.Close()203 for i := 0; ; i++ {204 toAvg := []map[string]uint64{}205 for _, records := range allRecords {206 if i < len(records) {207 toAvg = append(toAvg, records[i])208 }209 }210 if len(toAvg) != len(allRecords) {211 break212 }213 averaged := avgStats(toAvg)214 data, err := json.MarshalIndent(averaged, "", " ")215 if err != nil {216 return err217 }218 if _, err := f.Write(append(data, '\n')); err != nil {219 return err220 }221 }222 return nil223}224func saveTableAsCsv(table [][]string, fileName string) error {225 f, err := os.Create(fileName)226 if err != nil {227 return err228 }229 defer f.Close()230 return csv.NewWriter(f).WriteAll(table)231}...
InstanceStatsTable
Using AI Code Generation
1import (2func main() {3 fmt.Println("This is the main package")4}5import (6func main() {7 fmt.Println("This is the main package")8}9import (10func main() {11 fmt.Println("This is the main package")12}13import (14func main() {15 fmt.Println("This is the main package")16}17import (18func main() {19 fmt.Println("This is the main package")20}21import (22func main() {23 fmt.Println("This is the main package")24}25import (26func main() {27 fmt.Println("This is the main package")28}29import (30func main() {31 fmt.Println("This is the main package")32}33import (34func main() {35 fmt.Println("This is the main package")36}37import (38func main() {39 fmt.Println("This is the main
InstanceStatsTable
Using AI Code Generation
1import (2func main() {3 stats, err := logger.InstanceStatsTable()4 if err != nil {5 glog.Errorf("Unable to get the stats of the instance: %v", err)6 }7 fmt.Println(stats)8}9import (10func main() {11 logs, err := logger.ListLogFiles()12 if err != nil {13 glog.Errorf("Unable to list the logs of the instance: %v", err)14 }15 fmt.Println(logs)16}17import (18func main() {19 logs, err := logger.ListLogFiles()20 if err != nil {21 glog.Errorf("Unable to list the logs of the instance: %v", err)22 }23 fmt.Println(logs)24}25import (26func main() {27 logs, err := logger.ListLogFiles()28 if err != nil {29 glog.Errorf("Unable to list the logs of the instance: %v", err)30 }31 fmt.Println(logs)32}33import (34func main() {35 logs, err := logger.ListLogFiles()36 if err != nil {
InstanceStatsTable
Using AI Code Generation
1import (2func main() {3 o := new(Other)4 o.InstanceStatsTable()5}6import (7type Other struct {8}9func (o *Other) InstanceStatsTable() {10 fmt.Println("InstanceStatsTable")11}12import (13type Other struct {14}15func (o *Other) InstanceStatsTable() {16 fmt.Println("InstanceStatsTable")17}18func (o *Other) InstanceStatsTable2() {19 fmt.Println("InstanceStatsTable2")20}21import (22type Other struct {23}24func (o *Other) InstanceStatsTable() {25 fmt.Println("InstanceStatsTable")26}27func (o *Other) InstanceStatsTable2() {28 fmt.Println("InstanceStatsTable2")29}30func (o *Other) InstanceStatsTable3() {31 fmt.Println("InstanceStatsTable3")32}33import (34type Other struct {35}36func (o *Other) InstanceStatsTable() {37 fmt.Println("InstanceStatsTable")38}39func (o *Other) InstanceStatsTable2() {40 fmt.Println("InstanceStatsTable2")41}42func (o *Other) InstanceStatsTable3() {43 fmt.Println("InstanceStatsTable3")44}45func (o *Other) InstanceStatsTable4() {46 fmt.Println("InstanceStatsTable4")47}48import (49type Other struct {
InstanceStatsTable
Using AI Code Generation
1import (2func main() {3 stats := Singleton.InstanceStatsTable()4 stats.Add("Erik", 10)5 stats.Add("Erik", 20)6 stats.Add("Erik", 30)7 stats.Add("Erik", 40)8 fmt.Println(stats.GetStats("Erik"))9}
InstanceStatsTable
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 instanceStats.InstanceStatsTable()5}6import (7func main() {8 fmt.Println("Hello, playground")9 instanceStats.InstanceStatsTable()10}11import (12func main() {13 fmt.Println("Hello, playground")14 instanceStats.InstanceStatsTable()15}16import (17func main() {18 fmt.Println("Hello, playground")19 instanceStats.InstanceStatsTable()20}21import (22func main() {23 fmt.Println("Hello, playground")24 instanceStats.InstanceStatsTable()25}26import (27func main() {28 fmt.Println("Hello, playground")29 instanceStats.InstanceStatsTable()30}31import (32func main() {33 fmt.Println("Hello, playground")34 instanceStats.InstanceStatsTable()35}36import (37func main() {38 fmt.Println("Hello, playground")
InstanceStatsTable
Using AI Code Generation
1import (2var (3n = flag.Int("n", 100, "Number of goroutines")4func main() {5flag.Parse()6for i := 0; i < *n; i++ {7goroutines = append(goroutines, &runtime.StackRecord{})8}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!