Best Syzkaller code snippet using db.ReadCorpus
stress.go
Source:stress.go
1// Copyright 2015 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 "flag"6 "fmt"7 "math/rand"8 "os"9 "runtime"10 "sync"11 "sync/atomic"12 "time"13 "github.com/google/syzkaller/pkg/db"14 "github.com/google/syzkaller/pkg/host"15 "github.com/google/syzkaller/pkg/ipc"16 . "github.com/google/syzkaller/pkg/log"17 "github.com/google/syzkaller/prog"18 _ "github.com/google/syzkaller/sys"19)20var (21 flagOS = flag.String("os", runtime.GOOS, "target os")22 flagArch = flag.String("arch", runtime.GOARCH, "target arch")23 flagCorpus = flag.String("corpus", "", "corpus database")24 flagExecutor = flag.String("executor", "./syz-executor", "path to executor binary")25 flagOutput = flag.Bool("output", false, "print executor output to console")26 flagProcs = flag.Int("procs", 2*runtime.NumCPU(), "number of parallel processes")27 flagLogProg = flag.Bool("logprog", false, "print programs before execution")28 flagGenerate = flag.Bool("generate", true, "generate new programs, otherwise only mutate corpus")29 statExec uint6430 gate *ipc.Gate31)32const programLength = 3033func main() {34 flag.Parse()35 target, err := prog.GetTarget(*flagOS, *flagArch)36 if err != nil {37 Fatalf("%v", err)38 }39 corpus := readCorpus(target)40 Logf(0, "parsed %v programs", len(corpus))41 if !*flagGenerate && len(corpus) == 0 {42 Fatalf("nothing to mutate (-generate=false and no corpus)")43 }44 calls := buildCallList(target)45 prios := target.CalculatePriorities(corpus)46 ct := target.BuildChoiceTable(prios, calls)47 config, err := ipc.DefaultConfig()48 if err != nil {49 Fatalf("%v", err)50 }51 gate = ipc.NewGate(2**flagProcs, nil)52 for pid := 0; pid < *flagProcs; pid++ {53 pid := pid54 go func() {55 env, err := ipc.MakeEnv(*flagExecutor, pid, config)56 if err != nil {57 Fatalf("failed to create execution environment: %v", err)58 }59 rs := rand.NewSource(time.Now().UnixNano() + int64(pid)*1e12)60 rnd := rand.New(rs)61 for i := 0; ; i++ {62 var p *prog.Prog63 if *flagGenerate && len(corpus) == 0 || i%4 != 0 {64 p = target.Generate(rs, programLength, ct)65 execute(pid, env, p)66 p.Mutate(rs, programLength, ct, corpus)67 execute(pid, env, p)68 } else {69 p = corpus[rnd.Intn(len(corpus))].Clone()70 p.Mutate(rs, programLength, ct, corpus)71 execute(pid, env, p)72 p.Mutate(rs, programLength, ct, corpus)73 execute(pid, env, p)74 }75 }76 }()77 }78 for range time.NewTicker(5 * time.Second).C {79 Logf(0, "executed %v programs", atomic.LoadUint64(&statExec))80 }81}82var outMu sync.Mutex83func execute(pid int, env *ipc.Env, p *prog.Prog) {84 if *flagExecutor == "" {85 return86 }87 atomic.AddUint64(&statExec, 1)88 if *flagLogProg {89 ticket := gate.Enter()90 defer gate.Leave(ticket)91 outMu.Lock()92 fmt.Printf("executing program %v\n%s\n", pid, p.Serialize())93 outMu.Unlock()94 }95 opts := &ipc.ExecOpts{}96 output, _, failed, hanged, err := env.Exec(opts, p)97 if err != nil {98 fmt.Printf("failed to execute executor: %v\n", err)99 }100 if failed || hanged || err != nil || *flagOutput {101 fmt.Printf("PROGRAM:\n%s\n", p.Serialize())102 }103 if failed || hanged || err != nil || *flagOutput {104 os.Stdout.Write(output)105 }106}107func readCorpus(target *prog.Target) []*prog.Prog {108 if *flagCorpus == "" {109 return nil110 }111 db, err := db.Open(*flagCorpus)112 if err != nil {113 Fatalf("failed to open corpus database: %v", err)114 }115 var progs []*prog.Prog116 for _, rec := range db.Records {117 p, err := target.Deserialize(rec.Val)118 if err != nil {119 Fatalf("failed to deserialize corpus program: %v", err)120 }121 progs = append(progs, p)122 }123 return progs124}125func buildCallList(target *prog.Target) map[*prog.Syscall]bool {126 if *flagOS != runtime.GOOS {127 // This is currently used on akaros, where syz-stress runs on host.128 calls := make(map[*prog.Syscall]bool)129 for _, c := range target.Syscalls {130 calls[c] = true131 }132 return calls133 }134 calls, err := host.DetectSupportedSyscalls(target)135 if err != nil {136 Logf(0, "failed to detect host supported syscalls: %v", err)137 calls = make(map[*prog.Syscall]bool)138 for _, c := range target.Syscalls {139 calls[c] = true140 }141 }142 for _, c := range target.Syscalls {143 if !calls[c] {144 Logf(0, "disabling unsupported syscall: %v", c.Name)145 }146 }147 trans := target.TransitivelyEnabledCalls(calls)148 for c := range calls {149 if !trans[c] {150 Logf(0, "disabling transitively unsupported syscall: %v", c.Name)151 delete(calls, c)152 }153 }154 return calls155}...
analyze.go
Source:analyze.go
1package main2import (3 "bufio"4 "flag"5 "fmt"6 "io"7 "log"8 "os"9 "strings"10)11var flags struct {12 corpus string13}14func main() {15 log.SetFlags(0)16 log.SetPrefix("sentiment-analyze: ")17 parseflags()18 db, err := readcorpus(flags.corpus)19 ck(err)20 analyze(os.Stdin, db)21}22func ck(err error) {23 if err != nil {24 log.Fatal(err)25 }26}27func parseflags() {28 flag.Usage = usage29 flag.StringVar(&flags.corpus, "corpus", "AFINN-111.txt", "corpus file")30 flag.Parse()31}32func usage() {33 fmt.Fprintln(os.Stderr, "usage: [options] <stdin>")34 flag.PrintDefaults()35 os.Exit(2)36}37func readcorpus(name string) (map[string]int, error) {38 f, err := os.Open(name)39 if err != nil {40 return nil, err41 }42 defer f.Close()43 db := make(map[string]int)44 sc := bufio.NewScanner(f)45 for sc.Scan() {46 var (47 word string48 score int49 )50 line := sc.Text()51 _, err = fmt.Sscanf(line, "%s %d", &word, &score)52 if err != nil {53 continue54 }55 db[word] = score56 }57 return db, nil58}59func analyze(r io.Reader, db map[string]int) {60 var (61 positive int62 neutral int63 negative int64 words int65 sentiment int66 )67 b := bufio.NewReader(r)68 for {69 var word string70 _, err := fmt.Fscan(b, &word)71 if err != nil {72 break73 }74 word = strings.ToLower(word)75 score := db[word]76 if score > 0 {77 positive++78 } else if score == 0 {79 neutral++80 } else {81 negative++82 }83 sentiment += score84 words++85 }86 fmt.Printf("Number of Words %d\n", words)87 fmt.Printf("Sentiment Score %d\n", sentiment)88 if words == 0 {89 words = 190 }91 fmt.Printf("Positive Ratio %d (%f)\n", positive, float64(positive)/float64(words))92 fmt.Printf("Neutral Ratio %d (%f)\n", neutral, float64(neutral)/float64(words))93 fmt.Printf("Negative Ratio %d (%f)\n", negative, float64(negative)/float64(words))94}...
ReadCorpus
Using AI Code Generation
1import (2func main() {3 db, err := sql.Open("sqlite3", "./test.db")4 if err != nil {5 panic(err)6 }7 defer db.Close()8 rows, err := db.Query("SELECT * FROM corpus")9 if err != nil {10 panic(err)11 }12 defer rows.Close()13 for rows.Next() {14 err = rows.Scan(&id, &name)15 if err != nil {16 panic(err)17 }18 fmt.Println(id, name)19 }20 err = rows.Err()21 if err != nil {22 panic(err)23 }24}25import (26func main() {27 db, err := sql.Open("sqlite3", "./test.db")28 if err != nil {29 panic(err)30 }31 defer db.Close()32 stmt, err := db.Prepare("INSERT INTO corpus(name) values(?)")33 if err != nil {34 panic(err)35 }36 defer stmt.Close()37 _, err = stmt.Exec("test")38 if err != nil {39 panic(err)40 }41}42import (43func main() {44 db, err := sql.Open("sqlite3", "./test.db")45 if err != nil {46 panic(err)47 }48 defer db.Close()49 stmt, err := db.Prepare("DELETE FROM corpus WHERE id=?")50 if err != nil {51 panic(err)52 }53 defer stmt.Close()54 _, err = stmt.Exec(1)55 if err != nil {56 panic(err)57 }58}59import (60func main() {61 db, err := sql.Open("sqlite3", "./test.db")62 if err != nil {63 panic(err)64 }65 defer db.Close()66 stmt, err := db.Prepare("UPDATE corpus SET name=? WHERE id=?
ReadCorpus
Using AI Code Generation
1import (2func main() {3 registry.RegisterIndexType( scorch.Name, scorch.New )4 index, err := bleve.New("index", bleve.NewIndexMapping())5 if err != nil {6 panic(err)7 }8 corpus, err := index.ReadCorpus("corpus")9 if err != nil {10 panic(err)11 }12 query := bleve.NewQueryStringQuery("hello")13 search := bleve.NewSearchRequest(query)14 searchResults, err := index.SearchInContext(corpus, search)15 if err != nil {16 panic(err)17 }18 fmt.Println(searchResults)19}20{21 "Request": {22 "Query": {23 },24 },
ReadCorpus
Using AI Code Generation
1import (2func main() {3 corpus := db.ReadCorpus()4 fmt.Println(corpus)5}6import (7func ReadCorpus() corpus.Corpus {8 return corpus.Corpus{}9}
ReadCorpus
Using AI Code Generation
1import (2type Document struct {3}4type Corpus struct {5}6func (c *Corpus) ReadCorpus(filename string) {7 file, err := os.Open(filename)8 if err != nil {9 fmt.Println("Error opening file")10 }11 defer file.Close()12 scanner := bufio.NewScanner(file)13 for scanner.Scan() {14 line := scanner.Text()15 words := strings.Split(line, " ")16 doc := Document{Id: len(c.Docs)}17 doc.Words = make(map[string]int)18 for _, word := range words {19 }20 c.Docs = append(c.Docs, &doc)21 }22}23func (c *Corpus) TFIDF() map[int]map[string]float64 {24 tfidf := make(map[int]map[string]float64)25 for _, doc := range c.Docs {26 tfidf[doc.Id] = make(map[string]float64)27 for word, count := range doc.Words {28 tf := float64(count) / float64(len(doc.Words))29 idf := math.Log(float64(len(c.Docs)) / float64(c.Words[word]))30 }31 }32}33func (c *Corpus) CosineSimilarity(tfidf map[int]map[string]float64, doc1 int, doc2 int) float64 {34 for word, tfidf1 := range tfidf[doc1] {35 if ok {36 }37 }38 for _, tfidf2 := range tfidf[doc2] {39 }40 return dotProduct / (math.Sqrt(magnitude1) * math.Sqrt(m
ReadCorpus
Using AI Code Generation
1import (2type Doc struct {3}4type Term struct {5}6type TermDoc struct {7}8func (t TermDocList) Len() int {9 return len(t)10}11func (t TermDocList) Less(i, j int) bool {12}13func (t TermDocList) Swap(i, j int) {14}15type Index struct {16}17func (i IndexList) Len() int {18 return len(i)19}20func (i IndexList) Less(i, j int) bool {21}22func (i IndexList) Swap(i, j int) {23}24type SearchQuery struct {25}26func (s SearchQueryList) Len() int {27 return len(s)28}29func (s SearchQueryList) Less(i, j int) bool {30}31func (s SearchQueryList) Swap(i, j int) {32}33type SearchResults struct {34}35func (s SearchResultsList) Len() int {36 return len(s)37}38func (s SearchResultsList) Less(i, j int) bool {39}40func (s SearchResultsList) Swap(i, j int) {
ReadCorpus
Using AI Code Generation
1import (2func main() {3 db := indexer.NewDB()4 db.ReadCorpus("corpus1")5 fmt.Println(db.Corpus)6}7### 3.1 BuildIndex()8### 3.2 WriteIndexToFile()
ReadCorpus
Using AI Code Generation
1import (2func main() {3 db.Open("test.gdb")4 db.ReadCorpus("test.txt")5 db.Close()6 fmt.Println("Done")7}8import (9func main() {10 db.Open("test.gdb")11 db.ReadCorpus("test.txt")12 db.Close()13 fmt.Println("Done")14}15import (16func main() {17 db.Open("test.gdb")18 db.ReadCorpus("test.txt")19 db.Close()20 fmt.Println("Done")21}22import (23func main() {24 db.Open("test.gdb")25 db.ReadCorpus("test.txt")26 db.Close()27 fmt.Println("Done")28}29import (30func main() {31 db.Open("test.gdb")32 db.ReadCorpus("test.txt")33 db.Close()34 fmt.Println("Done")35}36import (37func main() {38 db.Open("test.gdb")39 db.ReadCorpus("test.txt")40 db.Close()41 fmt.Println("Done")42}43import (
ReadCorpus
Using AI Code Generation
1import (2func ReadCorpus() {3 file, err := os.Open("corpus.txt")4 if err != nil {5 fmt.Println("Error in opening the file")6 }7 defer file.Close()8 scanner := bufio.NewScanner(file)9 for scanner.Scan() {10 line := scanner.Text()11 words := strings.Fields(line)12 for i := 0; i < len(words); i++ {13 AddToInvertedIndex(words[i], i)14 }15 }16}17func AddToInvertedIndex(word string, index int) {18 db.AddToInvertedIndex(word, index)19}20func PrintInvertedIndex() {21 db.PrintInvertedIndex()22}23func PrintPostingsList(word string) {24 db.PrintPostingsList(word)25}26func Search(word string) {27 db.Search(word)28}29func Delete(word string) {30 db.Delete(word)31}32func DeletePostingsList(word string) {33 db.DeletePostingsList(word)34}35func DeleteInvertedIndex() {36 db.DeleteInvertedIndex()37}38func DeleteCorpus() {39 db.DeleteCorpus()40}41func PrintCorpus() {
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!!