Best Syzkaller code snippet using main.findBugForCrash
api.go
Source:api.go  
...668		// Collect them into a single bin.669		req.Title = corruptedReportTitle670	}671	ns := build.Namespace672	bug, bugKey, err := findBugForCrash(c, ns, req.Title)673	if err != nil {674		return nil, err675	}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], nil...findBugForCrash
Using AI Code Generation
1import (2func main() {3    fmt.Println("Hello, playground")4    findBugForCrash()5}6import (7func main() {8    fmt.Println("Hello, playground")9    findBugForCrash()10}11import (12func main() {13    fmt.Println("Hello, playground")14    findBugForCrash()15}16import (17func main() {18    fmt.Println("Hello, playground")19    findBugForCrash()20}21import (22func main() {23    fmt.Println("Hello, playground")24    findBugForCrash()25}26import (27func main() {28    fmt.Println("Hello, playground")29    findBugForCrash()30}31import (32func main() {33    fmt.Println("Hello, playground")34    findBugForCrash()35}36import (37func main() {38    fmt.Println("Hello, playground")39    findBugForCrash()40}41import (42func main() {43    fmt.Println("Hello, playground")44    findBugForCrash()45}46import (47func main() {48    fmt.Println("Hello, playground")49    findBugForCrash()50}findBugForCrash
Using AI Code Generation
1import (2func main() {3    file, err := os.Open("input.txt")4    if err != nil {5        log.Fatal(err)6    }7    defer file.Close()8    scanner := bufio.NewScanner(file)9    scanner.Split(bufio.ScanLines)10    for scanner.Scan() {11        txtlines = append(txtlines, scanner.Text())12    }13    file.Close()14    for i := 0; i < len(txtlines); i++ {15        if strings.Contains(txtlines[i], "Crash") {16            crash, _ = strconv.Atoi(txtlines[i+1])17        }18    }19    var bug = findBugForCrash(crash)20    fmt.Println("The bug is : ", bug)21}22import (23func findBugForCrash(crash int) string {24    file, err := os.Open("input.txt")25    if err != nil {26        log.Fatal(err)27    }28    defer file.Close()29    scanner := bufio.NewScanner(file)30    scanner.Split(bufio.ScanLines)31    for scanner.Scan() {32        txtlines = append(txtlines, scanner.Text())33    }34    file.Close()35    for i := 0; i < len(txtlines); i++ {36        if strings.Contains(txtlines[i], "Bug") {37        }38    }39}40import (findBugForCrash
Using AI Code Generation
1func main() {2    bug = findBugForCrash(crash)3    fmt.Println(bug)4}5func main() {6    bug = findBugForCrash(crash)7    fmt.Println(bug)8}9func main() {10    bug = findBugForCrash(crash)11    fmt.Println(bug)12}13func main() {14    bug = findBugForCrash(crash)15    fmt.Println(bug)16}17func main() {18    bug = findBugForCrash(crash)19    fmt.Println(bug)20}21func main() {22    bug = findBugForCrash(crash)23    fmt.Println(bug)24}25func main() {26    bug = findBugForCrash(crash)27    fmt.Println(bug)28}29func main() {30    bug = findBugForCrash(crash)31    fmt.Println(bug)32}33func main() {findBugForCrash
Using AI Code Generation
1import "fmt"2func main() {3    fmt.Println("Hello, playground")4    main.findBugForCrash()5}6import "fmt"7func main() {8    fmt.Println("Hello, playground")9    main.findBugForCrash()10}11import "fmt"12func main() {13    fmt.Println("Hello, playground")14    main.findBugForCrash()15}16import "fmt"17func main() {18    fmt.Println("Hello, playground")19    main.findBugForCrash()20}21import "fmt"22func main() {23    fmt.Println("Hello, playground")24    main.findBugForCrash()25}26import "fmt"27func main() {28    fmt.Println("Hello, playground")29    main.findBugForCrash()30}31import "fmt"32func main() {33    fmt.Println("Hello, playground")34    main.findBugForCrash()35}36import "fmt"37func main() {38    fmt.Println("Hello, playground")39    main.findBugForCrash()40}41import "fmt"findBugForCrash
Using AI Code Generation
1import (2type Crash struct {3}4func main() {5	crash := Crash{"test", "test", "test"}6	crash.findBugForCrash()7	fmt.Println(crash)8}9func (crash *Crash) findBugForCrash() {10}11import (12type Crash struct {13}14func main() {15	crash := Crash{"test", "test", "test"}16	crash.findBugForCrash()17	fmt.Println(crash)18}19func (crash *Crash) findBugForCrash() {20}21import (22type Crash struct {23}24func main() {25	crash := Crash{"test", "test", "test"}26	crash.findBugForCrash()27	fmt.Println(crash)28}29func (crash *Crash) findBugForCrash() {30}31import (32type Crash struct {33}34func main() {35	crash := Crash{"test", "test", "test"}36	crash.findBugForCrash()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!!
