Best Syzkaller code snippet using main.createBugForCrash
api.go
Source:api.go
...675 }676 if active, err := isActiveBug(c, bug); err != nil {677 return nil, err678 } else if !active {679 bug, bugKey, err = createBugForCrash(c, ns, req)680 if err != nil {681 return nil, err682 }683 }684 now := timeNow(c)685 reproLevel := ReproLevelNone686 if len(req.ReproC) != 0 {687 reproLevel = ReproLevelC688 } else if len(req.ReproSyz) != 0 {689 reproLevel = ReproLevelSyz690 }691 save := reproLevel != ReproLevelNone ||692 bug.NumCrashes < maxCrashes ||693 now.Sub(bug.LastSavedCrash) > time.Hour ||694 bug.NumCrashes%20 == 0695 if save {696 if err := saveCrash(c, ns, req, bugKey, build); err != nil {697 return nil, err698 }699 } else {700 log.Infof(c, "not saving crash for %q", bug.Title)701 }702 tx := func(c context.Context) error {703 bug = new(Bug)704 if err := db.Get(c, bugKey, bug); err != nil {705 return fmt.Errorf("failed to get bug: %v", err)706 }707 bug.NumCrashes++708 bug.LastTime = now709 if save {710 bug.LastSavedCrash = now711 }712 if reproLevel != ReproLevelNone {713 bug.NumRepro++714 bug.LastReproTime = now715 }716 if bug.ReproLevel < reproLevel {717 bug.ReproLevel = reproLevel718 }719 if len(req.Report) != 0 {720 bug.HasReport = true721 }722 if !stringInList(bug.HappenedOn, build.Manager) {723 bug.HappenedOn = append(bug.HappenedOn, build.Manager)724 }725 if _, err = db.Put(c, bugKey, bug); err != nil {726 return fmt.Errorf("failed to put bug: %v", err)727 }728 return nil729 }730 if err := db.RunInTransaction(c, tx, &db.TransactionOptions{XG: true}); err != nil {731 return nil, err732 }733 if save {734 purgeOldCrashes(c, bug, bugKey)735 }736 return bug, nil737}738func saveCrash(c context.Context, ns string, req *dashapi.Crash, bugKey *db.Key, build *Build) error {739 // Reporting priority of this crash.740 prio := int64(kernelRepoInfo(build).ReportingPriority) * 1e6741 if len(req.ReproC) != 0 {742 prio += 4e12743 } else if len(req.ReproSyz) != 0 {744 prio += 2e12745 }746 if build.Arch == "amd64" {747 prio += 1e3748 }749 crash := &Crash{750 Manager: build.Manager,751 BuildID: req.BuildID,752 Time: timeNow(c),753 Maintainers: req.Maintainers,754 ReproOpts: req.ReproOpts,755 ReportLen: prio,756 }757 var err error758 if crash.Log, err = putText(c, ns, textCrashLog, req.Log, false); err != nil {759 return err760 }761 if crash.Report, err = putText(c, ns, textCrashReport, req.Report, false); err != nil {762 return err763 }764 if crash.ReproSyz, err = putText(c, ns, textReproSyz, req.ReproSyz, false); err != nil {765 return err766 }767 if crash.ReproC, err = putText(c, ns, textReproC, req.ReproC, false); err != nil {768 return err769 }770 crashKey := db.NewIncompleteKey(c, "Crash", bugKey)771 if _, err = db.Put(c, crashKey, crash); err != nil {772 return fmt.Errorf("failed to put crash: %v", err)773 }774 return nil775}776func purgeOldCrashes(c context.Context, bug *Bug, bugKey *db.Key) {777 const purgeEvery = 10778 if bug.NumCrashes <= 2*maxCrashes || (bug.NumCrashes-1)%purgeEvery != 0 {779 return780 }781 var crashes []*Crash782 keys, err := db.NewQuery("Crash").783 Ancestor(bugKey).784 Filter("Reported=", time.Time{}).785 GetAll(c, &crashes)786 if err != nil {787 log.Errorf(c, "failed to fetch purge crashes: %v", err)788 return789 }790 keyMap := make(map[*Crash]*db.Key)791 for i, crash := range crashes {792 keyMap[crash] = keys[i]793 }794 // Newest first.795 sort.Slice(crashes, func(i, j int) bool {796 return crashes[i].Time.After(crashes[j].Time)797 })798 var toDelete []*db.Key799 latestOnManager := make(map[string]bool)800 deleted, reproCount, noreproCount := 0, 0, 0801 for _, crash := range crashes {802 if !crash.Reported.IsZero() {803 log.Errorf(c, "purging reported crash?")804 continue805 }806 // Preserve latest crash on each manager.807 if !latestOnManager[crash.Manager] {808 latestOnManager[crash.Manager] = true809 continue810 }811 // Preserve maxCrashes latest crashes with repro and without repro.812 count := &noreproCount813 if crash.ReproSyz != 0 || crash.ReproC != 0 {814 count = &reproCount815 }816 if *count < maxCrashes {817 *count++818 continue819 }820 toDelete = append(toDelete, keyMap[crash])821 if crash.Log != 0 {822 toDelete = append(toDelete, db.NewKey(c, textCrashLog, "", crash.Log, nil))823 }824 if crash.Report != 0 {825 toDelete = append(toDelete, db.NewKey(c, textCrashReport, "", crash.Report, nil))826 }827 if crash.ReproSyz != 0 {828 toDelete = append(toDelete, db.NewKey(c, textReproSyz, "", crash.ReproSyz, nil))829 }830 if crash.ReproC != 0 {831 toDelete = append(toDelete, db.NewKey(c, textReproC, "", crash.ReproC, nil))832 }833 deleted++834 if deleted == 2*purgeEvery {835 break836 }837 }838 if len(toDelete) == 0 {839 return840 }841 if err := db.DeleteMulti(c, toDelete); err != nil {842 log.Errorf(c, "failed to delete old crashes: %v", err)843 return844 }845 log.Infof(c, "deleted %v crashes for bug %q", deleted, bug.Title)846}847func apiReportFailedRepro(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {848 req := new(dashapi.CrashID)849 if err := json.Unmarshal(payload, req); err != nil {850 return nil, fmt.Errorf("failed to unmarshal request: %v", err)851 }852 req.Title = limitLength(req.Title, maxTextLen)853 bug, bugKey, err := findBugForCrash(c, ns, req.Title)854 if err != nil {855 return nil, err856 }857 if bug == nil {858 return nil, fmt.Errorf("%v: can't find bug for crash %q", ns, req.Title)859 }860 now := timeNow(c)861 tx := func(c context.Context) error {862 bug := new(Bug)863 if err := db.Get(c, bugKey, bug); err != nil {864 return fmt.Errorf("failed to get bug: %v", err)865 }866 bug.NumRepro++867 bug.LastReproTime = now868 if _, err := db.Put(c, bugKey, bug); err != nil {869 return fmt.Errorf("failed to put bug: %v", err)870 }871 return nil872 }873 err = db.RunInTransaction(c, tx, &db.TransactionOptions{874 XG: true,875 Attempts: 30,876 })877 return nil, err878}879func apiNeedRepro(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {880 req := new(dashapi.CrashID)881 if err := json.Unmarshal(payload, req); err != nil {882 return nil, fmt.Errorf("failed to unmarshal request: %v", err)883 }884 if req.Corrupted {885 resp := &dashapi.NeedReproResp{886 NeedRepro: false,887 }888 return resp, nil889 }890 req.Title = limitLength(req.Title, maxTextLen)891 bug, _, err := findBugForCrash(c, ns, req.Title)892 if err != nil {893 return nil, err894 }895 if bug == nil {896 return nil, fmt.Errorf("%v: can't find bug for crash %q", ns, req.Title)897 }898 resp := &dashapi.NeedReproResp{899 NeedRepro: needRepro(c, bug),900 }901 return resp, nil902}903func apiManagerStats(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {904 req := new(dashapi.ManagerStatsReq)905 if err := json.Unmarshal(payload, req); err != nil {906 return nil, fmt.Errorf("failed to unmarshal request: %v", err)907 }908 now := timeNow(c)909 err := updateManager(c, ns, req.Name, func(mgr *Manager, stats *ManagerStats) error {910 mgr.Link = req.Addr911 mgr.LastAlive = now912 mgr.CurrentUpTime = req.UpTime913 if cur := int64(req.Corpus); cur > stats.MaxCorpus {914 stats.MaxCorpus = cur915 }916 if cur := int64(req.PCs); cur > stats.MaxPCs {917 stats.MaxPCs = cur918 }919 if cur := int64(req.Cover); cur > stats.MaxCover {920 stats.MaxCover = cur921 }922 if cur := int64(req.CrashTypes); cur > stats.CrashTypes {923 stats.CrashTypes = cur924 }925 stats.TotalFuzzingTime += req.FuzzingTime926 stats.TotalCrashes += int64(req.Crashes)927 stats.SuppressedCrashes += int64(req.SuppressedCrashes)928 stats.TotalExecs += int64(req.Execs)929 return nil930 })931 return nil, err932}933func apiBugList(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {934 keys, err := db.NewQuery("Bug").935 Filter("Namespace=", ns).936 KeysOnly().937 GetAll(c, nil)938 if err != nil {939 return nil, fmt.Errorf("failed to query bugs: %v", err)940 }941 resp := &dashapi.BugListResp{}942 for _, key := range keys {943 resp.List = append(resp.List, key.StringID())944 }945 return resp, nil946}947func apiLoadBug(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {948 req := new(dashapi.LoadBugReq)949 if err := json.Unmarshal(payload, req); err != nil {950 return nil, fmt.Errorf("failed to unmarshal request: %v", err)951 }952 bug := new(Bug)953 bugKey := db.NewKey(c, "Bug", req.ID, 0, nil)954 if err := db.Get(c, bugKey, bug); err != nil {955 return nil, fmt.Errorf("failed to get bug: %v", err)956 }957 if bug.Namespace != ns {958 return nil, fmt.Errorf("no such bug")959 }960 if bug.sanitizeAccess(AccessPublic) > AccessPublic {961 return nil, nil962 }963 crash, _, err := findCrashForBug(c, bug)964 if err != nil {965 return nil, err966 }967 build, err := loadBuild(c, ns, crash.BuildID)968 if err != nil {969 return nil, err970 }971 reproSyz, _, err := getText(c, textReproSyz, crash.ReproSyz)972 if err != nil {973 return nil, err974 }975 reproC, _, err := getText(c, textReproC, crash.ReproC)976 if err != nil {977 return nil, err978 }979 statuses := map[int]string{980 BugStatusOpen: "open",981 BugStatusFixed: "fixed",982 BugStatusInvalid: "invalid",983 BugStatusDup: "dup",984 }985 resp := &dashapi.LoadBugResp{986 ID: req.ID,987 Title: bug.displayTitle(),988 Status: statuses[bug.Status],989 SyzkallerCommit: build.SyzkallerCommit,990 ReproOpts: crash.ReproOpts,991 ReproSyz: reproSyz,992 ReproC: reproC,993 }994 return resp, nil995}996func findBugForCrash(c context.Context, ns, title string) (*Bug, *db.Key, error) {997 var bugs []*Bug998 keys, err := db.NewQuery("Bug").999 Filter("Namespace=", ns).1000 Filter("Title=", title).1001 Order("-Seq").1002 Limit(1).1003 GetAll(c, &bugs)1004 if err != nil {1005 return nil, nil, fmt.Errorf("failed to query bugs: %v", err)1006 }1007 if len(bugs) == 0 {1008 return nil, nil, nil1009 }1010 return bugs[0], keys[0], nil1011}1012func createBugForCrash(c context.Context, ns string, req *dashapi.Crash) (*Bug, *db.Key, error) {1013 var bug *Bug1014 var bugKey *db.Key1015 now := timeNow(c)1016 tx := func(c context.Context) error {1017 for seq := int64(0); ; seq++ {1018 bug = new(Bug)1019 bugHash := bugKeyHash(ns, req.Title, seq)1020 bugKey = db.NewKey(c, "Bug", bugHash, 0, nil)1021 if err := db.Get(c, bugKey, bug); err != nil {1022 if err != db.ErrNoSuchEntity {1023 return fmt.Errorf("failed to get bug: %v", err)1024 }1025 bug = &Bug{1026 Namespace: ns,...
createBugForCrash
Using AI Code Generation
1func main() {2 mainObj := mainClass{}3 mainObj.createBugForCrash()4}5func main() {6 mainObj := mainClass{}7 mainObj.createBugForCrash()8}9func main() {10 mainObj := mainClass{}11 mainObj.createBugForCrash()12}13func main() {14 mainObj := mainClass{}15 mainObj.createBugForCrash()16}17func main() {18 mainObj := mainClass{}19 mainObj.createBugForCrash()20}21func main() {22 mainObj := mainClass{}23 mainObj.createBugForCrash()24}25func main() {26 mainObj := mainClass{}27 mainObj.createBugForCrash()28}29func main() {30 mainObj := mainClass{}31 mainObj.createBugForCrash()32}33func main() {34 mainObj := mainClass{}35 mainObj.createBugForCrash()36}37func main() {38 mainObj := mainClass{}39 mainObj.createBugForCrash()40}41func main() {42 mainObj := mainClass{}43 mainObj.createBugForCrash()44}45func main() {46 mainObj := mainClass{}47 mainObj.createBugForCrash()48}
createBugForCrash
Using AI Code Generation
1import (2func main() {3 client := &http.Client{}4 srv, err := sheets.NewService(client, option.WithCredentialsFile("client_secret.json"))5 if err != nil {6 log.Fatalf("Unable to retrieve Sheets Client %v", err)7 }8 spreadsheet, err := srv.Spreadsheets.Get("1Oq3K9m5lPcJ0m4C4Khi4mZg5x5LQJ1KdH5m5XyYjZuY").Do()9 if err != nil {10 log.Fatalf("Unable to retrieve data from sheet. %v", err)11 }12 resp, err := srv.Spreadsheets.Values.Get(spreadsheet.SpreadsheetId, "Sheet1!A1:B").Do()13 if err != nil {14 log.Fatalf("Unable to retrieve data from sheet. %v", err)15 }16 if len(resp.Values) == 0 {17 fmt.Println("No data found.")18 } else {19 fmt.Println("Name, Major:")20 for _, row := range resp.Values {21 fmt.Printf("%s, %s22 }23 }24 f, err := os.Create("bug.txt")25 if err != nil {26 log.Fatal(err)27 }28 _, err = f.WriteString("Bug Title: " + resp.Values[0][0].(string) + "29" + "Bug Description: " + resp.Values[0][1].(string) + "30 if err != nil {31 log.Fatal(err)32 }33 err = f.Close()34 if err != nil {35 log.Fatal(err)36 }37 data, err := ioutil.ReadFile("bug.txt")38 if err != nil {39 log.Fatal(err)40 }
createBugForCrash
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 helloworld.CreateBugForCrash()5}6import (7func main() {8 fmt.Println("Hello, playground")9 helloworld.CreateBugForCrash()10}11import (12func main() {13 fmt.Println("Hello, playground")14 helloworld.CreateBugForCrash()15}16import (17func main() {18 fmt.Println("Hello, playground")19 helloworld.CreateBugForCrash()20}21import (22func main() {23 fmt.Println("Hello, playground")24 helloworld.CreateBugForCrash()25}26import (27func main() {28 fmt.Println("Hello, playground")29 helloworld.CreateBugForCrash()30}31import (32func main() {33 fmt.Println("Hello, playground")34 helloworld.CreateBugForCrash()35}36import (
createBugForCrash
Using AI Code Generation
1import "fmt"2func main() {3 var bug = createBugForCrash("crash")4 fmt.Println(bug)5}6import "fmt"7func main() {8 var bug = createBugForCrash("crash")9 fmt.Println(bug)10}11import "fmt"12func main() {13 var bug = createBugForCrash("crash")14 fmt.Println(bug)15}16import "fmt"17func main() {18 var bug = createBugForCrash("crash")19 fmt.Println(bug)20}21import "fmt"22func main() {23 var bug = createBugForCrash("crash")24 fmt.Println(bug)25}26import "fmt"27func main() {28 var bug = createBugForCrash("crash")29 fmt.Println(bug)30}31import "fmt"32func main() {33 var bug = createBugForCrash("crash")34 fmt.Println(bug)35}36import "fmt"37func main() {38 var bug = createBugForCrash("crash")39 fmt.Println(bug)40}41import "fmt"42func main() {43 var bug = createBugForCrash("crash")44 fmt.Println(bug)45}46import "fmt"47func main() {48 var bug = createBugForCrash("crash")49 fmt.Println(bug)50}
createBugForCrash
Using AI Code Generation
1import (2func main() {3 crashPath := flag.String("crashPath", "", "path of crash file")4 flag.Parse()5 if *crashPath == "" {6 fmt.Println("Please give crash file path")7 os.Exit(1)8 }9 mainObj := mainObj{}10 mainObj.createBugForCrash(*crashPath)11}12import (13func main() {14 crashPath := flag.String("crashPath", "", "path of crash file")15 flag.Parse()16 if *crashPath == "" {17 fmt.Println("Please give crash file path")18 os.Exit(1)19 }20 mainObj := mainObj{}21 mainObj.createBugForCrash(*crashPath)22}23import (24func main() {25 crashPath := flag.String("crashPath", "", "path of crash file")26 flag.Parse()27 if *crashPath == "" {28 fmt.Println("Please give crash file path")29 os.Exit(1)30 }31 mainObj := mainObj{}32 mainObj.createBugForCrash(*crashPath)33}34import (35func main() {36 crashPath := flag.String("crashPath", "", "path of crash file")37 flag.Parse()38 if *crashPath == "" {39 fmt.Println("Please give crash file path")40 os.Exit(1)41 }
createBugForCrash
Using AI Code Generation
1import (2func main() {3 flag.Parse()4 crash.CreateBugForCrash(flag.Arg(0))5 fmt.Println("Bug created")6}7import (8func main() {9 flag.Parse()10 crash.CreateBugForCrash(flag.Arg(0))11 fmt.Println("Bug created")12}13In the above code, we have imported the crash package in all the three files. If we try to run the files individually, it will give an error as shown below:14import (
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!!