Best Syzkaller code snippet using main.apiAddBuildAssets
api.go
Source:api.go
...50 "commit_poll": apiCommitPoll,51 "upload_commits": apiUploadCommits,52 "bug_list": apiBugList,53 "load_bug": apiLoadBug,54 "add_build_assets": apiAddBuildAssets,55}56type JSONHandler func(c context.Context, r *http.Request) (interface{}, error)57type APIHandler func(c context.Context, r *http.Request, payload []byte) (interface{}, error)58type APINamespaceHandler func(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error)59const (60 maxReproPerBug = 1061 reproRetryPeriod = 24 * time.Hour // try 1 repro per day until we have at least syz repro62 // Attempt a new repro every ~ 3 months, even if we have already found it for the bug. This should:63 // 1) Improve old repros over time (as we update descriptions / change syntax / repro algorithms).64 // 2) Constrain the impact of bugs in syzkaller's backward compatibility. Fewer old repros, fewer problems.65 reproStalePeriod = 100 * 24 * time.Hour66)67// Overridable for testing.68var timeNow = func(c context.Context) time.Time {69 return time.Now()70}71func timeSince(c context.Context, t time.Time) time.Duration {72 return timeNow(c).Sub(t)73}74func handleJSON(fn JSONHandler) http.Handler {75 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {76 c := appengine.NewContext(r)77 reply, err := fn(c, r)78 if err != nil {79 // ErrAccess is logged earlier.80 if err != ErrAccess {81 log.Errorf(c, "%v", err)82 }83 http.Error(w, err.Error(), http.StatusInternalServerError)84 return85 }86 w.Header().Set("Content-Type", "application/json")87 if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {88 w.Header().Set("Content-Encoding", "gzip")89 gz := gzip.NewWriter(w)90 if err := json.NewEncoder(gz).Encode(reply); err != nil {91 log.Errorf(c, "failed to encode reply: %v", err)92 }93 gz.Close()94 } else {95 if err := json.NewEncoder(w).Encode(reply); err != nil {96 log.Errorf(c, "failed to encode reply: %v", err)97 }98 }99 })100}101func handleAPI(c context.Context, r *http.Request) (reply interface{}, err error) {102 client := r.PostFormValue("client")103 method := r.PostFormValue("method")104 log.Infof(c, "api %q from %q", method, client)105 auth := auth.MakeEndpoint(auth.GoogleTokenInfoEndpoint)106 subj, err := auth.DetermineAuthSubj(timeNow(c), r.Header["Authorization"])107 if err != nil {108 return nil, err109 }110 // Somewhat confusingly the "key" parameter is the password.111 ns, err := checkClient(config, client, r.PostFormValue("key"), subj)112 if err != nil {113 if client != "" {114 log.Errorf(c, "%v", err)115 } else {116 // Don't log as error if somebody just invokes /api.117 log.Infof(c, "%v", err)118 }119 return nil, err120 }121 var payload []byte122 if str := r.PostFormValue("payload"); str != "" {123 gr, err := gzip.NewReader(strings.NewReader(str))124 if err != nil {125 return nil, fmt.Errorf("failed to ungzip payload: %v", err)126 }127 payload, err = ioutil.ReadAll(gr)128 if err != nil {129 return nil, fmt.Errorf("failed to ungzip payload: %v", err)130 }131 if err := gr.Close(); err != nil {132 return nil, fmt.Errorf("failed to ungzip payload: %v", err)133 }134 }135 handler := apiHandlers[method]136 if handler != nil {137 return handler(c, r, payload)138 }139 nsHandler := apiNamespaceHandlers[method]140 if nsHandler == nil {141 return nil, fmt.Errorf("unknown api method %q", method)142 }143 if ns == "" {144 return nil, fmt.Errorf("method %q must be called within a namespace", method)145 }146 return nsHandler(c, ns, r, payload)147}148func apiLogError(c context.Context, r *http.Request, payload []byte) (interface{}, error) {149 req := new(dashapi.LogEntry)150 if err := json.Unmarshal(payload, req); err != nil {151 return nil, fmt.Errorf("failed to unmarshal request: %v", err)152 }153 log.Errorf(c, "%v: %v", req.Name, req.Text)154 return nil, nil155}156func apiBuilderPoll(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {157 req := new(dashapi.BuilderPollReq)158 if err := json.Unmarshal(payload, req); err != nil {159 return nil, fmt.Errorf("failed to unmarshal request: %v", err)160 }161 bugs, _, err := loadAllBugs(c, func(query *db.Query) *db.Query {162 return query.Filter("Namespace=", ns).163 Filter("Status<", BugStatusFixed)164 })165 if err != nil {166 return nil, err167 }168 m := make(map[string]bool)169loop:170 for _, bug := range bugs {171 // TODO(dvyukov): include this condition into the query if possible.172 if len(bug.Commits) == 0 {173 continue174 }175 for _, mgr := range bug.PatchedOn {176 if mgr == req.Manager {177 continue loop178 }179 }180 for _, com := range bug.Commits {181 m[com] = true182 }183 }184 commits := make([]string, 0, len(m))185 for com := range m {186 commits = append(commits, com)187 }188 sort.Strings(commits)189 resp := &dashapi.BuilderPollResp{190 PendingCommits: commits,191 ReportEmail: reportEmail(c, ns),192 }193 return resp, nil194}195func reportEmail(c context.Context, ns string) string {196 for _, reporting := range config.Namespaces[ns].Reporting {197 if _, ok := reporting.Config.(*EmailConfig); ok {198 return ownEmail(c)199 }200 }201 return ""202}203func apiCommitPoll(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {204 resp := &dashapi.CommitPollResp{205 ReportEmail: reportEmail(c, ns),206 }207 for _, repo := range config.Namespaces[ns].Repos {208 resp.Repos = append(resp.Repos, dashapi.Repo{209 URL: repo.URL,210 Branch: repo.Branch,211 })212 }213 var bugs []*Bug214 _, err := db.NewQuery("Bug").215 Filter("Namespace=", ns).216 Filter("NeedCommitInfo=", true).217 Project("Commits").218 Limit(100).219 GetAll(c, &bugs)220 if err != nil {221 return nil, fmt.Errorf("failed to query bugs: %v", err)222 }223 commits := make(map[string]bool)224 for _, bug := range bugs {225 for _, com := range bug.Commits {226 commits[com] = true227 }228 }229 for com := range commits {230 resp.Commits = append(resp.Commits, com)231 }232 return resp, nil233}234func apiUploadCommits(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {235 req := new(dashapi.CommitPollResultReq)236 if err := json.Unmarshal(payload, req); err != nil {237 return nil, fmt.Errorf("failed to unmarshal request: %v", err)238 }239 // This adds fixing commits to bugs.240 err := addCommitsToBugs(c, ns, "", nil, req.Commits)241 if err != nil {242 return nil, err243 }244 // Now add commit info to commits.245 for _, com := range req.Commits {246 if com.Hash == "" {247 continue248 }249 if err := addCommitInfo(c, ns, com); err != nil {250 return nil, err251 }252 }253 return nil, nil254}255func addCommitInfo(c context.Context, ns string, com dashapi.Commit) error {256 var bugs []*Bug257 keys, err := db.NewQuery("Bug").258 Filter("Namespace=", ns).259 Filter("Commits=", com.Title).260 GetAll(c, &bugs)261 if err != nil {262 return fmt.Errorf("failed to query bugs: %v", err)263 }264 for i, bug := range bugs {265 if err := addCommitInfoToBug(c, bug, keys[i], com); err != nil {266 return err267 }268 }269 return nil270}271func addCommitInfoToBug(c context.Context, bug *Bug, bugKey *db.Key, com dashapi.Commit) error {272 if needUpdate, err := addCommitInfoToBugImpl(c, bug, com); err != nil {273 return err274 } else if !needUpdate {275 return nil276 }277 tx := func(c context.Context) error {278 bug := new(Bug)279 if err := db.Get(c, bugKey, bug); err != nil {280 return fmt.Errorf("failed to get bug %v: %v", bugKey.StringID(), err)281 }282 if needUpdate, err := addCommitInfoToBugImpl(c, bug, com); err != nil {283 return err284 } else if !needUpdate {285 return nil286 }287 if _, err := db.Put(c, bugKey, bug); err != nil {288 return fmt.Errorf("failed to put bug: %v", err)289 }290 return nil291 }292 return db.RunInTransaction(c, tx, nil)293}294func addCommitInfoToBugImpl(c context.Context, bug *Bug, com dashapi.Commit) (bool, error) {295 ci := -1296 for i, title := range bug.Commits {297 if title == com.Title {298 ci = i299 break300 }301 }302 if ci < 0 {303 return false, nil304 }305 for len(bug.CommitInfo) < len(bug.Commits) {306 bug.CommitInfo = append(bug.CommitInfo, Commit{})307 }308 hash0 := bug.CommitInfo[ci].Hash309 date0 := bug.CommitInfo[ci].Date310 author0 := bug.CommitInfo[ci].Author311 needCommitInfo0 := bug.NeedCommitInfo312 bug.CommitInfo[ci].Hash = com.Hash313 bug.CommitInfo[ci].Date = com.Date314 bug.CommitInfo[ci].Author = com.Author315 bug.NeedCommitInfo = false316 for i := range bug.CommitInfo {317 if bug.CommitInfo[i].Hash == "" {318 bug.NeedCommitInfo = true319 break320 }321 }322 changed := hash0 != bug.CommitInfo[ci].Hash ||323 date0 != bug.CommitInfo[ci].Date ||324 author0 != bug.CommitInfo[ci].Author ||325 needCommitInfo0 != bug.NeedCommitInfo326 return changed, nil327}328func apiJobPoll(c context.Context, r *http.Request, payload []byte) (interface{}, error) {329 req := new(dashapi.JobPollReq)330 if err := json.Unmarshal(payload, req); err != nil {331 return nil, fmt.Errorf("failed to unmarshal request: %v", err)332 }333 if len(req.Managers) == 0 {334 return nil, fmt.Errorf("no managers")335 }336 return pollPendingJobs(c, req.Managers)337}338func apiJobDone(c context.Context, r *http.Request, payload []byte) (interface{}, error) {339 req := new(dashapi.JobDoneReq)340 if err := json.Unmarshal(payload, req); err != nil {341 return nil, fmt.Errorf("failed to unmarshal request: %v", err)342 }343 err := doneJob(c, req)344 return nil, err345}346func apiUploadBuild(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {347 req := new(dashapi.Build)348 if err := json.Unmarshal(payload, req); err != nil {349 return nil, fmt.Errorf("failed to unmarshal request: %v", err)350 }351 now := timeNow(c)352 _, isNewBuild, err := uploadBuild(c, now, ns, req, BuildNormal)353 if err != nil {354 return nil, err355 }356 if isNewBuild {357 err := updateManager(c, ns, req.Manager, func(mgr *Manager, stats *ManagerStats) error {358 prevKernel, prevSyzkaller := "", ""359 if mgr.CurrentBuild != "" {360 prevBuild, err := loadBuild(c, ns, mgr.CurrentBuild)361 if err != nil {362 return err363 }364 prevKernel = prevBuild.KernelCommit365 prevSyzkaller = prevBuild.SyzkallerCommit366 }367 log.Infof(c, "new build on %v: kernel %v->%v syzkaller %v->%v",368 req.Manager, prevKernel, req.KernelCommit, prevSyzkaller, req.SyzkallerCommit)369 mgr.CurrentBuild = req.ID370 if req.KernelCommit != prevKernel {371 mgr.FailedBuildBug = ""372 }373 if req.SyzkallerCommit != prevSyzkaller {374 mgr.FailedSyzBuildBug = ""375 }376 return nil377 })378 if err != nil {379 return nil, err380 }381 }382 if len(req.Commits) != 0 || len(req.FixCommits) != 0 {383 for i := range req.FixCommits {384 // Reset hashes just to make sure,385 // the build does not necessary come from the master repo, so we must not remember hashes.386 req.FixCommits[i].Hash = ""387 }388 if err := addCommitsToBugs(c, ns, req.Manager, req.Commits, req.FixCommits); err != nil {389 // We've already uploaded the build successfully and manager can use it.390 // Moreover, addCommitsToBugs scans all bugs and can take long time.391 // So just log the error.392 log.Errorf(c, "failed to add commits to bugs: %v", err)393 }394 }395 return nil, nil396}397func uploadBuild(c context.Context, now time.Time, ns string, req *dashapi.Build, typ BuildType) (398 *Build, bool, error) {399 newAssets := []Asset{}400 for i, toAdd := range req.Assets {401 newAsset, err := parseIncomingAsset(c, toAdd)402 if err != nil {403 return nil, false, fmt.Errorf("failed to parse asset #%d: %w", i, err)404 }405 newAssets = append(newAssets, newAsset)406 }407 if build, err := loadBuild(c, ns, req.ID); err == nil {408 return build, false, nil409 }410 checkStrLen := func(str, name string, maxLen int) error {411 if str == "" {412 return fmt.Errorf("%v is empty", name)413 }414 if len(str) > maxLen {415 return fmt.Errorf("%v is too long (%v)", name, len(str))416 }417 return nil418 }419 if err := checkStrLen(req.Manager, "Build.Manager", MaxStringLen); err != nil {420 return nil, false, err421 }422 if err := checkStrLen(req.ID, "Build.ID", MaxStringLen); err != nil {423 return nil, false, err424 }425 if err := checkStrLen(req.KernelRepo, "Build.KernelRepo", MaxStringLen); err != nil {426 return nil, false, err427 }428 if len(req.KernelBranch) > MaxStringLen {429 return nil, false, fmt.Errorf("Build.KernelBranch is too long (%v)", len(req.KernelBranch))430 }431 if err := checkStrLen(req.SyzkallerCommit, "Build.SyzkallerCommit", MaxStringLen); err != nil {432 return nil, false, err433 }434 if len(req.CompilerID) > MaxStringLen {435 return nil, false, fmt.Errorf("Build.CompilerID is too long (%v)", len(req.CompilerID))436 }437 if len(req.KernelCommit) > MaxStringLen {438 return nil, false, fmt.Errorf("Build.KernelCommit is too long (%v)", len(req.KernelCommit))439 }440 configID, err := putText(c, ns, textKernelConfig, req.KernelConfig, true)441 if err != nil {442 return nil, false, err443 }444 build := &Build{445 Namespace: ns,446 Manager: req.Manager,447 ID: req.ID,448 Type: typ,449 Time: now,450 OS: req.OS,451 Arch: req.Arch,452 VMArch: req.VMArch,453 SyzkallerCommit: req.SyzkallerCommit,454 SyzkallerCommitDate: req.SyzkallerCommitDate,455 CompilerID: req.CompilerID,456 KernelRepo: req.KernelRepo,457 KernelBranch: req.KernelBranch,458 KernelCommit: req.KernelCommit,459 KernelCommitTitle: req.KernelCommitTitle,460 KernelCommitDate: req.KernelCommitDate,461 KernelConfig: configID,462 Assets: newAssets,463 }464 if _, err := db.Put(c, buildKey(c, ns, req.ID), build); err != nil {465 return nil, false, err466 }467 return build, true, nil468}469func addCommitsToBugs(c context.Context, ns, manager string, titles []string, fixCommits []dashapi.Commit) error {470 presentCommits := make(map[string]bool)471 bugFixedBy := make(map[string][]string)472 for _, com := range titles {473 presentCommits[com] = true474 }475 for _, com := range fixCommits {476 presentCommits[com.Title] = true477 for _, bugID := range com.BugIDs {478 bugFixedBy[bugID] = append(bugFixedBy[bugID], com.Title)479 }480 }481 managers, err := managerList(c, ns)482 if err != nil {483 return err484 }485 // Fetching all bugs in a namespace can be slow, and there is no way to filter only Open/Dup statuses.486 // So we run a separate query for each status, this both avoids fetching unnecessary data487 // and splits a long query into two (two smaller queries have lower chances of trigerring488 // timeouts than one huge).489 for _, status := range []int{BugStatusOpen, BugStatusDup} {490 err := addCommitsToBugsInStatus(c, status, ns, manager, managers, presentCommits, bugFixedBy)491 if err != nil {492 return err493 }494 }495 return nil496}497func addCommitsToBugsInStatus(c context.Context, status int, ns, manager string, managers []string,498 presentCommits map[string]bool, bugFixedBy map[string][]string) error {499 bugs, _, err := loadAllBugs(c, func(query *db.Query) *db.Query {500 return query.Filter("Namespace=", ns).501 Filter("Status=", status)502 })503 if err != nil {504 return err505 }506 for _, bug := range bugs {507 var fixCommits []string508 for i := range bug.Reporting {509 fixCommits = append(fixCommits, bugFixedBy[bug.Reporting[i].ID]...)510 }511 sort.Strings(fixCommits)512 if err := addCommitsToBug(c, bug, manager, managers, fixCommits, presentCommits); err != nil {513 return err514 }515 if bug.Status == BugStatusDup {516 canon, err := canonicalBug(c, bug)517 if err != nil {518 return err519 }520 if canon.Status == BugStatusOpen && len(bug.Commits) == 0 {521 if err := addCommitsToBug(c, canon, manager, managers,522 fixCommits, presentCommits); err != nil {523 return err524 }525 }526 }527 }528 return nil529}530func addCommitsToBug(c context.Context, bug *Bug, manager string, managers []string,531 fixCommits []string, presentCommits map[string]bool) error {532 if !bugNeedsCommitUpdate(c, bug, manager, fixCommits, presentCommits, true) {533 return nil534 }535 now := timeNow(c)536 bugKey := bug.key(c)537 tx := func(c context.Context) error {538 bug := new(Bug)539 if err := db.Get(c, bugKey, bug); err != nil {540 return fmt.Errorf("failed to get bug %v: %v", bugKey.StringID(), err)541 }542 if !bugNeedsCommitUpdate(c, bug, manager, fixCommits, presentCommits, false) {543 return nil544 }545 if len(fixCommits) != 0 && !reflect.DeepEqual(bug.Commits, fixCommits) {546 bug.updateCommits(fixCommits, now)547 }548 if manager != "" {549 bug.PatchedOn = append(bug.PatchedOn, manager)550 if bug.Status == BugStatusOpen {551 fixed := true552 for _, mgr := range managers {553 if !stringInList(bug.PatchedOn, mgr) {554 fixed = false555 break556 }557 }558 if fixed {559 bug.Status = BugStatusFixed560 bug.Closed = now561 }562 }563 }564 if _, err := db.Put(c, bugKey, bug); err != nil {565 return fmt.Errorf("failed to put bug: %v", err)566 }567 return nil568 }569 return db.RunInTransaction(c, tx, nil)570}571func bugNeedsCommitUpdate(c context.Context, bug *Bug, manager string, fixCommits []string,572 presentCommits map[string]bool, dolog bool) bool {573 if len(fixCommits) != 0 && !reflect.DeepEqual(bug.Commits, fixCommits) {574 if dolog {575 log.Infof(c, "bug %q is fixed with %q", bug.Title, fixCommits)576 }577 return true578 }579 if len(bug.Commits) == 0 || manager == "" || stringInList(bug.PatchedOn, manager) {580 return false581 }582 for _, com := range bug.Commits {583 if !presentCommits[com] {584 return false585 }586 }587 return true588}589func managerList(c context.Context, ns string) ([]string, error) {590 var builds []*Build591 _, err := db.NewQuery("Build").592 Filter("Namespace=", ns).593 Project("Manager").594 Distinct().595 GetAll(c, &builds)596 if err != nil {597 return nil, fmt.Errorf("failed to query builds: %v", err)598 }599 configManagers := config.Namespaces[ns].Managers600 var managers []string601 for _, build := range builds {602 if configManagers[build.Manager].Decommissioned {603 continue604 }605 managers = append(managers, build.Manager)606 }607 return managers, nil608}609func apiReportBuildError(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {610 req := new(dashapi.BuildErrorReq)611 if err := json.Unmarshal(payload, req); err != nil {612 return nil, fmt.Errorf("failed to unmarshal request: %v", err)613 }614 now := timeNow(c)615 build, _, err := uploadBuild(c, now, ns, &req.Build, BuildFailed)616 if err != nil {617 return nil, err618 }619 req.Crash.BuildID = req.Build.ID620 bug, err := reportCrash(c, build, &req.Crash)621 if err != nil {622 return nil, err623 }624 if err := updateManager(c, ns, req.Build.Manager, func(mgr *Manager, stats *ManagerStats) error {625 log.Infof(c, "failed build on %v: kernel=%v", req.Build.Manager, req.Build.KernelCommit)626 if req.Build.KernelCommit != "" {627 mgr.FailedBuildBug = bug.keyHash()628 } else {629 mgr.FailedSyzBuildBug = bug.keyHash()630 }631 return nil632 }); err != nil {633 return nil, err634 }635 return nil, nil636}637const (638 corruptedReportTitle = "corrupted report"639 suppressedReportTitle = "suppressed report"640)641func apiReportCrash(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {642 req := new(dashapi.Crash)643 if err := json.Unmarshal(payload, req); err != nil {644 return nil, fmt.Errorf("failed to unmarshal request: %v", err)645 }646 build, err := loadBuild(c, ns, req.BuildID)647 if err != nil {648 return nil, err649 }650 if !config.Namespaces[ns].TransformCrash(build, req) {651 return new(dashapi.ReportCrashResp), nil652 }653 bug, err := reportCrash(c, build, req)654 if err != nil {655 return nil, err656 }657 resp := &dashapi.ReportCrashResp{658 NeedRepro: needRepro(c, bug),659 }660 return resp, nil661}662func reportCrash(c context.Context, build *Build, req *dashapi.Crash) (*Bug, error) {663 req.Title = canonicalizeCrashTitle(req.Title, req.Corrupted, req.Suppressed)664 if req.Corrupted || req.Suppressed {665 req.AltTitles = []string{req.Title}666 } else {667 for i, t := range req.AltTitles {668 req.AltTitles[i] = normalizeCrashTitle(t)669 }670 req.AltTitles = mergeStringList([]string{req.Title}, req.AltTitles) // dedup671 }672 req.Maintainers = email.MergeEmailLists(req.Maintainers)673 ns := build.Namespace674 bug, err := findBugForCrash(c, ns, req.AltTitles)675 if err != nil {676 return nil, err677 }678 if bug == nil {679 bug, err = createBugForCrash(c, ns, req)680 if err != nil {681 return nil, err682 }683 }684 bugKey := bug.key(c)685 now := timeNow(c)686 reproLevel := ReproLevelNone687 if len(req.ReproC) != 0 {688 reproLevel = ReproLevelC689 } else if len(req.ReproSyz) != 0 {690 reproLevel = ReproLevelSyz691 }692 save := reproLevel != ReproLevelNone ||693 bug.NumCrashes < maxCrashes ||694 now.Sub(bug.LastSavedCrash) > time.Hour ||695 bug.NumCrashes%20 == 0 ||696 !stringInList(bug.MergedTitles, req.Title)697 if save {698 if err := saveCrash(c, ns, req, bug, bugKey, build); err != nil {699 return nil, err700 }701 } else {702 log.Infof(c, "not saving crash for %q", bug.Title)703 }704 tx := func(c context.Context) error {705 bug = new(Bug)706 if err := db.Get(c, bugKey, bug); err != nil {707 return fmt.Errorf("failed to get bug: %v", err)708 }709 bug.LastTime = now710 if save {711 bug.LastSavedCrash = now712 }713 if reproLevel != ReproLevelNone {714 bug.NumRepro++715 bug.LastReproTime = now716 }717 if bug.ReproLevel < reproLevel {718 bug.ReproLevel = reproLevel719 bug.HeadReproLevel = reproLevel720 }721 if len(req.Report) != 0 {722 bug.HasReport = true723 }724 bug.increaseCrashStats(now)725 bug.HappenedOn = mergeString(bug.HappenedOn, build.Manager)726 // Migration of older entities (for new bugs Title is always in MergedTitles).727 bug.MergedTitles = mergeString(bug.MergedTitles, bug.Title)728 bug.MergedTitles = mergeString(bug.MergedTitles, req.Title)729 bug.AltTitles = mergeStringList(bug.AltTitles, req.AltTitles)730 if _, err = db.Put(c, bugKey, bug); err != nil {731 return fmt.Errorf("failed to put bug: %v", err)732 }733 return nil734 }735 if err := db.RunInTransaction(c, tx, &db.TransactionOptions{XG: true}); err != nil {736 return nil, err737 }738 if save {739 purgeOldCrashes(c, bug, bugKey)740 }741 return bug, nil742}743func (crash *Crash) UpdateReportingPriority(build *Build, bug *Bug) {744 prio := int64(kernelRepoInfo(build).ReportingPriority) * 1e6745 divReproPrio := int64(1)746 if crash.ReproIsRevoked {747 divReproPrio = 10748 }749 if crash.ReproC > 0 {750 prio += 4e12 / divReproPrio751 } else if crash.ReproSyz > 0 {752 prio += 2e12 / divReproPrio753 }754 if crash.Title == bug.Title {755 prio += 1e8 // prefer reporting crash that matches bug title756 }757 if build.Arch == targets.AMD64 {758 prio += 1e3759 }760 crash.ReportLen = prio761}762func saveCrash(c context.Context, ns string, req *dashapi.Crash, bug *Bug, bugKey *db.Key, build *Build) error {763 crash := &Crash{764 Title: req.Title,765 Manager: build.Manager,766 BuildID: req.BuildID,767 Time: timeNow(c),768 Maintainers: email.MergeEmailLists(req.Maintainers,769 GetEmails(req.Recipients, dashapi.To),770 GetEmails(req.Recipients, dashapi.Cc)),771 ReproOpts: req.ReproOpts,772 Flags: int64(req.Flags),773 }774 var err error775 if crash.Log, err = putText(c, ns, textCrashLog, req.Log, false); err != nil {776 return err777 }778 if crash.Report, err = putText(c, ns, textCrashReport, req.Report, false); err != nil {779 return err780 }781 if crash.ReproSyz, err = putText(c, ns, textReproSyz, req.ReproSyz, false); err != nil {782 return err783 }784 if crash.ReproC, err = putText(c, ns, textReproC, req.ReproC, false); err != nil {785 return err786 }787 if crash.MachineInfo, err = putText(c, ns, textMachineInfo, req.MachineInfo, true); err != nil {788 return err789 }790 crash.UpdateReportingPriority(build, bug)791 crashKey := db.NewIncompleteKey(c, "Crash", bugKey)792 if _, err = db.Put(c, crashKey, crash); err != nil {793 return fmt.Errorf("failed to put crash: %v", err)794 }795 return nil796}797func purgeOldCrashes(c context.Context, bug *Bug, bugKey *db.Key) {798 const purgeEvery = 10799 if bug.NumCrashes <= 2*maxCrashes || (bug.NumCrashes-1)%purgeEvery != 0 {800 return801 }802 var crashes []*Crash803 keys, err := db.NewQuery("Crash").804 Ancestor(bugKey).805 Filter("Reported=", time.Time{}).806 GetAll(c, &crashes)807 if err != nil {808 log.Errorf(c, "failed to fetch purge crashes: %v", err)809 return810 }811 keyMap := make(map[*Crash]*db.Key)812 for i, crash := range crashes {813 keyMap[crash] = keys[i]814 }815 // Newest first.816 sort.Slice(crashes, func(i, j int) bool {817 return crashes[i].Time.After(crashes[j].Time)818 })819 var toDelete []*db.Key820 latestOnManager := make(map[string]bool)821 uniqueTitle := make(map[string]bool)822 deleted, reproCount, noreproCount := 0, 0, 0823 for _, crash := range crashes {824 if !crash.Reported.IsZero() {825 log.Errorf(c, "purging reported crash?")826 continue827 }828 // Preserve latest crash on each manager.829 if !latestOnManager[crash.Manager] {830 latestOnManager[crash.Manager] = true831 continue832 }833 // Preserve at least one crash with each title.834 if !uniqueTitle[crash.Title] {835 uniqueTitle[crash.Title] = true836 continue837 }838 // Preserve maxCrashes latest crashes with repro and without repro.839 count := &noreproCount840 if crash.ReproSyz != 0 || crash.ReproC != 0 {841 count = &reproCount842 }843 if *count < maxCrashes {844 *count++845 continue846 }847 toDelete = append(toDelete, keyMap[crash])848 if crash.Log != 0 {849 toDelete = append(toDelete, db.NewKey(c, textCrashLog, "", crash.Log, nil))850 }851 if crash.Report != 0 {852 toDelete = append(toDelete, db.NewKey(c, textCrashReport, "", crash.Report, nil))853 }854 if crash.ReproSyz != 0 {855 toDelete = append(toDelete, db.NewKey(c, textReproSyz, "", crash.ReproSyz, nil))856 }857 if crash.ReproC != 0 {858 toDelete = append(toDelete, db.NewKey(c, textReproC, "", crash.ReproC, nil))859 }860 deleted++861 if deleted == 2*purgeEvery {862 break863 }864 }865 if len(toDelete) == 0 {866 return867 }868 if err := db.DeleteMulti(c, toDelete); err != nil {869 log.Errorf(c, "failed to delete old crashes: %v", err)870 return871 }872 log.Infof(c, "deleted %v crashes for bug %q", deleted, bug.Title)873}874func apiReportFailedRepro(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {875 req := new(dashapi.CrashID)876 if err := json.Unmarshal(payload, req); err != nil {877 return nil, fmt.Errorf("failed to unmarshal request: %v", err)878 }879 req.Title = canonicalizeCrashTitle(req.Title, req.Corrupted, req.Suppressed)880 bug, err := findExistingBugForCrash(c, ns, []string{req.Title})881 if err != nil {882 return nil, err883 }884 if bug == nil {885 return nil, fmt.Errorf("%v: can't find bug for crash %q", ns, req.Title)886 }887 bugKey := bug.key(c)888 now := timeNow(c)889 tx := func(c context.Context) error {890 bug := new(Bug)891 if err := db.Get(c, bugKey, bug); err != nil {892 return fmt.Errorf("failed to get bug: %v", err)893 }894 bug.NumRepro++895 bug.LastReproTime = now896 if _, err := db.Put(c, bugKey, bug); err != nil {897 return fmt.Errorf("failed to put bug: %v", err)898 }899 return nil900 }901 err = db.RunInTransaction(c, tx, &db.TransactionOptions{902 XG: true,903 Attempts: 30,904 })905 return nil, err906}907func apiNeedRepro(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {908 req := new(dashapi.CrashID)909 if err := json.Unmarshal(payload, req); err != nil {910 return nil, fmt.Errorf("failed to unmarshal request: %v", err)911 }912 if req.Corrupted {913 resp := &dashapi.NeedReproResp{914 NeedRepro: false,915 }916 return resp, nil917 }918 req.Title = canonicalizeCrashTitle(req.Title, req.Corrupted, req.Suppressed)919 bug, err := findExistingBugForCrash(c, ns, []string{req.Title})920 if err != nil {921 return nil, err922 }923 if bug == nil {924 if req.MayBeMissing {925 // Manager does not send leak reports w/o repro to dashboard, we want to reproduce them.926 resp := &dashapi.NeedReproResp{927 NeedRepro: true,928 }929 return resp, nil930 }931 return nil, fmt.Errorf("%v: can't find bug for crash %q", ns, req.Title)932 }933 resp := &dashapi.NeedReproResp{934 NeedRepro: needRepro(c, bug),935 }936 return resp, nil937}938func canonicalizeCrashTitle(title string, corrupted, suppressed bool) string {939 if corrupted {940 // The report is corrupted and the title is most likely invalid.941 // Such reports are usually unactionable and are discarded.942 // Collect them into a single bin.943 return corruptedReportTitle944 }945 if suppressed {946 // Collect all of them into a single bucket so that it's possible to control and assess them,947 // e.g. if there are some spikes in suppressed reports.948 return suppressedReportTitle949 }950 return normalizeCrashTitle(title)951}952func normalizeCrashTitle(title string) string {953 return strings.TrimSpace(limitLength(title, maxTextLen))954}955func apiManagerStats(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {956 req := new(dashapi.ManagerStatsReq)957 if err := json.Unmarshal(payload, req); err != nil {958 return nil, fmt.Errorf("failed to unmarshal request: %v", err)959 }960 now := timeNow(c)961 err := updateManager(c, ns, req.Name, func(mgr *Manager, stats *ManagerStats) error {962 mgr.Link = req.Addr963 mgr.LastAlive = now964 mgr.CurrentUpTime = req.UpTime965 if cur := int64(req.Corpus); cur > stats.MaxCorpus {966 stats.MaxCorpus = cur967 }968 if cur := int64(req.PCs); cur > stats.MaxPCs {969 stats.MaxPCs = cur970 }971 if cur := int64(req.Cover); cur > stats.MaxCover {972 stats.MaxCover = cur973 }974 if cur := int64(req.CrashTypes); cur > stats.CrashTypes {975 stats.CrashTypes = cur976 }977 stats.TotalFuzzingTime += req.FuzzingTime978 stats.TotalCrashes += int64(req.Crashes)979 stats.SuppressedCrashes += int64(req.SuppressedCrashes)980 stats.TotalExecs += int64(req.Execs)981 return nil982 })983 return nil, err984}985func apiBugList(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {986 keys, err := db.NewQuery("Bug").987 Filter("Namespace=", ns).988 KeysOnly().989 GetAll(c, nil)990 if err != nil {991 return nil, fmt.Errorf("failed to query bugs: %v", err)992 }993 resp := &dashapi.BugListResp{}994 for _, key := range keys {995 resp.List = append(resp.List, key.StringID())996 }997 return resp, nil998}999func apiLoadBug(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {1000 req := new(dashapi.LoadBugReq)1001 if err := json.Unmarshal(payload, req); err != nil {1002 return nil, fmt.Errorf("failed to unmarshal request: %v", err)1003 }1004 bug := new(Bug)1005 bugKey := db.NewKey(c, "Bug", req.ID, 0, nil)1006 if err := db.Get(c, bugKey, bug); err != nil {1007 return nil, fmt.Errorf("failed to get bug: %v", err)1008 }1009 if bug.Namespace != ns {1010 return nil, fmt.Errorf("no such bug")1011 }1012 if bug.sanitizeAccess(AccessPublic) > AccessPublic {1013 return nil, nil1014 }1015 return loadBugReport(c, bug)1016}1017func loadBugReport(c context.Context, bug *Bug) (*dashapi.BugReport, error) {1018 crash, crashKey, err := findCrashForBug(c, bug)1019 if err != nil {1020 return nil, err1021 }1022 // Create report for the last reporting so that it's stable and ExtID does not change over time.1023 bugReporting := &bug.Reporting[len(bug.Reporting)-1]1024 reporting := config.Namespaces[bug.Namespace].ReportingByName(bugReporting.Name)1025 if reporting == nil {1026 return nil, fmt.Errorf("reporting %v is missing in config", bugReporting.Name)1027 }1028 return createBugReport(c, bug, crash, crashKey, bugReporting, reporting)1029}1030func apiAddBuildAssets(c context.Context, ns string, r *http.Request, payload []byte) (interface{}, error) {1031 req := new(dashapi.AddBuildAssetsReq)1032 if err := json.Unmarshal(payload, req); err != nil {1033 return nil, fmt.Errorf("failed to unmarshal request: %v", err)1034 }1035 assets := []Asset{}1036 for i, toAdd := range req.Assets {1037 asset, err := parseIncomingAsset(c, toAdd)1038 if err != nil {1039 return nil, fmt.Errorf("failed to parse asset #%d: %w", i, err)1040 }1041 assets = append(assets, asset)1042 }1043 _, err := appendBuildAssets(c, ns, req.BuildID, assets)1044 if err != nil {...
apiAddBuildAssets
Using AI Code Generation
1import (2func main() {3 r := mux.NewRouter()4 r.HandleFunc("/api/addBuildAssets", apiAddBuildAssets)5 log.Fatal(http.ListenAndServe(":8080", r))6}7import (8func main() {9 r := mux.NewRouter()10 r.HandleFunc("/api/addBuildAssets", apiAddBuildAssets)11 log.Fatal(http.ListenAndServe(":8080", r))12}13import (14func main() {15 r := mux.NewRouter()16 r.HandleFunc("/api/addBuildAssets", apiAddBuildAssets)17 log.Fatal(http.ListenAndServe(":8080", r))18}19import (20func main() {21 r := mux.NewRouter()22 r.HandleFunc("/api/addBuildAssets", apiAddBuildAssets)23 log.Fatal(http.ListenAndServe(":8080", r))24}25import (26func main() {27 r := mux.NewRouter()28 r.HandleFunc("/api/addBuildAssets", apiAddBuildAssets)29 log.Fatal(http.ListenAndServe(":8080", r))30}31import (32func main() {33 r := mux.NewRouter()34 r.HandleFunc("/api/addBuildAssets", apiAddBuildAssets)35 log.Fatal(http.ListenAndServe(":8080", r))36}
apiAddBuildAssets
Using AI Code Generation
1import (2func main() {3 http.HandleFunc("/apiAddBuildAssets", func(w http.ResponseWriter, r *http.Request) {4 fmt.Fprintf(w, "Hello, %q", r.URL.Path)5 })6 http.ListenAndServe(":8080", nil)7}8import (9func main() {10 http.HandleFunc("/apiAddBuildAssets", func(w http.ResponseWriter, r *http.Request) {11 fmt.Fprintf(w, "Hello, %q", r.URL.Path)12 })13 http.ListenAndServe(":8080", nil)14}15import (16func main() {17 http.HandleFunc("/apiAddBuildAssets", func(w http.ResponseWriter, r *http.Request) {18 fmt.Fprintf(w, "Hello, %q", r.URL.Path)19 })20 http.ListenAndServe(":8080", nil)21}22import (23func main() {24 http.HandleFunc("/apiAddBuildAssets", func(w http.ResponseWriter, r *http.Request) {25 fmt.Fprintf(w, "Hello, %q", r.URL.Path)26 })27 http.ListenAndServe(":8080", nil)28}29import (30func main() {31 http.HandleFunc("/apiAddBuildAssets", func(w http.ResponseWriter, r *http.Request) {32 fmt.Fprintf(w, "Hello, %q", r.URL.Path)33 })34 http.ListenAndServe(":8080", nil)35}36import (37func main() {38 http.HandleFunc("/apiAddBuildAssets", func(w http.ResponseWriter, r *http.Request) {39 fmt.Fprintf(w, "Hello, %q", r.URL.Path)40 })41 http.ListenAndServe(":8080", nil)42}
apiAddBuildAssets
Using AI Code Generation
1import (2func main() {3 fmt.Println("Starting the application...")4 file, err := os.Open("test.txt")5 if err != nil {6 panic(err)7 }8 defer file.Close()9 w := multipart.NewWriter(&b)10 fw, err := w.CreateFormFile("file", filepath.Base("test.txt"))11 if err != nil {12 panic(err)13 }14 if _, err = io.Copy(fw, file); err != nil {15 panic(err)16 }17 if fw, err = w.CreateFormField("buildId"); err != nil {18 panic(err)19 }20 if _, err = fw.Write([]byte("123")); err != nil {21 panic(err)22 }23 w.Close()24 if err != nil {25 panic(err)26 }27 req.Header.Set("Content-Type", w.FormDataContentType())28 client := &http.Client{}29 resp, err := client.Do(req)30 if err != nil {31 panic(err)32 }33 defer resp.Body.Close()34 if resp.StatusCode != http.StatusOK {35 panic(resp.Status)36 }37}38import (
apiAddBuildAssets
Using AI Code Generation
1import (2type BuildAssets struct {3}4func main() {5 buildAssets.Assets = append(buildAssets.Assets, "asset1")6 buildAssets.Assets = append(buildAssets.Assets, "asset2")7 buildAssets.Assets = append(buildAssets.Assets, "asset3")8 jsonData, _ := json.Marshal(buildAssets)9 req.Header("Content-Type", "application/json")10 req.Body(jsonData)11 str, err := req.String()12 if err != nil {13 fmt.Println(err)14 } else {15 fmt.Println(str)16 }17}18{"success":true,"message":"Build Assets added successfully"}19func (this *BuildsController) apiGetBuildAssets() {20 buildID := this.GetString("buildID")21 buildAssets, err := models.GetBuildAssets(buildID)22 if err != nil {23 this.Data["json"] = map[string]interface{}{"success": false, "message": err.Error()}24 } else {25 this.Data["json"] = map[string]interface{}{"success": true, "buildAssets": buildAssets}26 }27 this.ServeJSON()28}29func GetBuildAssets(buildID string) ([]string, error) {30 o := orm.NewOrm()31 _, err := o.Raw("SELECT asset_id FROM BuildAssets WHERE build_id = ?", buildID).Values(&maps)32 if err != nil {33 }34 for _, m := range maps {35 buildAssets = append(buildAssets, m["asset_id"].(string))36 }37}38import (
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!!