Best Syzkaller code snippet using main.uploadCorpus
manager.go
Source:manager.go
...158 mgr.Errorf("failed to upload cover report: %v", err)159 }160 }161 if mgr.cfg.CorpusUploadPath != "" {162 if err := mgr.uploadCorpus(); err != nil {163 mgr.Errorf("failed to upload corpus: %v", err)164 }165 }166 }167 select {168 case <-mgr.stop:169 break loop170 default:171 }172 if latestInfo != nil && (latestInfo.Time != managerRestartTime || mgr.cmd == nil) {173 managerRestartTime = latestInfo.Time174 mgr.restartManager()175 if mgr.cmd != nil {176 artifactUploadTime = time.Now().Add(6 * time.Hour)177 }178 }179 select {180 case <-ticker.C:181 case <-mgr.stop:182 break loop183 }184 }185 if mgr.cmd != nil {186 mgr.cmd.Close()187 mgr.cmd = nil188 }189 log.Logf(0, "%v: stopped", mgr.name)190}191func (mgr *Manager) pollAndBuild(lastCommit string, latestInfo *BuildInfo) (192 string, *BuildInfo, time.Duration) {193 rebuildAfter := buildRetryPeriod194 commit, err := mgr.repo.Poll(mgr.mgrcfg.Repo, mgr.mgrcfg.Branch)195 if err != nil {196 mgr.Errorf("failed to poll: %v", err)197 } else {198 log.Logf(0, "%v: poll: %v", mgr.name, commit.Hash)199 if commit.Hash != lastCommit &&200 (latestInfo == nil ||201 commit.Hash != latestInfo.KernelCommit ||202 mgr.configTag != latestInfo.KernelConfigTag) {203 lastCommit = commit.Hash204 select {205 case kernelBuildSem <- struct{}{}:206 log.Logf(0, "%v: building kernel...", mgr.name)207 if err := mgr.build(commit); err != nil {208 log.Logf(0, "%v: %v", mgr.name, err)209 } else {210 log.Logf(0, "%v: build successful, [re]starting manager", mgr.name)211 rebuildAfter = kernelRebuildPeriod212 latestInfo = mgr.checkLatest()213 if latestInfo == nil {214 mgr.Errorf("failed to read build info after build")215 }216 }217 <-kernelBuildSem218 case <-mgr.stop:219 }220 }221 }222 return lastCommit, latestInfo, rebuildAfter223}224// BuildInfo characterizes a kernel build.225type BuildInfo struct {226 Time time.Time // when the build was done227 Tag string // unique tag combined from compiler id, kernel commit and config tag228 CompilerID string // compiler identity string (e.g. "gcc 7.1.1")229 KernelRepo string230 KernelBranch string231 KernelCommit string // git hash of kernel checkout232 KernelCommitTitle string233 KernelCommitDate time.Time234 KernelConfigTag string // SHA1 hash of .config contents235}236func loadBuildInfo(dir string) (*BuildInfo, error) {237 info := new(BuildInfo)238 if err := config.LoadFile(filepath.Join(dir, "tag"), info); err != nil {239 return nil, err240 }241 return info, nil242}243// checkLatest checks if we have a good working latest build and returns its build info.244// If the build is missing/broken, nil is returned.245func (mgr *Manager) checkLatest() *BuildInfo {246 if !osutil.FilesExist(mgr.latestDir, imageFiles) {247 return nil248 }249 info, _ := loadBuildInfo(mgr.latestDir)250 return info251}252func (mgr *Manager) createBuildInfo(kernelCommit *vcs.Commit, compilerID string) *BuildInfo {253 var tagData []byte254 tagData = append(tagData, mgr.name...)255 tagData = append(tagData, kernelCommit.Hash...)256 tagData = append(tagData, compilerID...)257 tagData = append(tagData, mgr.configTag...)258 return &BuildInfo{259 Time: time.Now(),260 Tag: hash.String(tagData),261 CompilerID: compilerID,262 KernelRepo: mgr.mgrcfg.Repo,263 KernelBranch: mgr.mgrcfg.Branch,264 KernelCommit: kernelCommit.Hash,265 KernelCommitTitle: kernelCommit.Title,266 KernelCommitDate: kernelCommit.CommitDate,267 KernelConfigTag: mgr.configTag,268 }269}270func (mgr *Manager) build(kernelCommit *vcs.Commit) error {271 // We first form the whole image in tmp dir and then rename it to latest.272 tmpDir := mgr.latestDir + ".tmp"273 if err := os.RemoveAll(tmpDir); err != nil {274 return fmt.Errorf("failed to remove tmp dir: %v", err)275 }276 if err := osutil.MkdirAll(tmpDir); err != nil {277 return fmt.Errorf("failed to create tmp dir: %v", err)278 }279 params := build.Params{280 TargetOS: mgr.managercfg.TargetOS,281 TargetArch: mgr.managercfg.TargetVMArch,282 VMType: mgr.managercfg.Type,283 KernelDir: mgr.kernelDir,284 OutputDir: tmpDir,285 Compiler: mgr.mgrcfg.Compiler,286 Ccache: mgr.mgrcfg.Ccache,287 UserspaceDir: mgr.mgrcfg.Userspace,288 CmdlineFile: mgr.mgrcfg.KernelCmdline,289 SysctlFile: mgr.mgrcfg.KernelSysctl,290 Config: mgr.configData,291 }292 details, err := build.Image(params)293 info := mgr.createBuildInfo(kernelCommit, details.CompilerID)294 if err != nil {295 rep := &report.Report{296 Title: fmt.Sprintf("%v build error", mgr.mgrcfg.RepoAlias),297 }298 switch err1 := err.(type) {299 case *build.KernelError:300 rep.Report = err1.Report301 rep.Output = err1.Output302 rep.Recipients = err1.Recipients303 case *osutil.VerboseError:304 rep.Report = []byte(err1.Title)305 rep.Output = err1.Output306 default:307 rep.Report = []byte(err.Error())308 }309 if err := mgr.reportBuildError(rep, info, tmpDir); err != nil {310 mgr.Errorf("failed to report image error: %v", err)311 }312 return fmt.Errorf("kernel build failed: %v", err)313 }314 if err := config.SaveFile(filepath.Join(tmpDir, "tag"), info); err != nil {315 return fmt.Errorf("failed to write tag file: %v", err)316 }317 if err := mgr.testImage(tmpDir, info); err != nil {318 return err319 }320 // Now try to replace latest with our tmp dir as atomically as we can get on Linux.321 if err := os.RemoveAll(mgr.latestDir); err != nil {322 return fmt.Errorf("failed to remove latest dir: %v", err)323 }324 return osutil.Rename(tmpDir, mgr.latestDir)325}326func (mgr *Manager) restartManager() {327 if !osutil.FilesExist(mgr.latestDir, imageFiles) {328 mgr.Errorf("can't start manager, image files missing")329 return330 }331 if mgr.cmd != nil {332 mgr.cmd.Close()333 mgr.cmd = nil334 }335 if err := osutil.LinkFiles(mgr.latestDir, mgr.currentDir, imageFiles); err != nil {336 mgr.Errorf("failed to create current image dir: %v", err)337 return338 }339 info, err := loadBuildInfo(mgr.currentDir)340 if err != nil {341 mgr.Errorf("failed to load build info: %v", err)342 return343 }344 buildTag, err := mgr.uploadBuild(info, mgr.currentDir)345 if err != nil {346 mgr.Errorf("failed to upload build: %v", err)347 return348 }349 cfgFile, err := mgr.writeConfig(buildTag)350 if err != nil {351 mgr.Errorf("failed to create manager config: %v", err)352 return353 }354 bin := filepath.FromSlash("syzkaller/current/bin/syz-manager")355 logFile := filepath.Join(mgr.currentDir, "manager.log")356 args := []string{"-config", cfgFile}357 if mgr.debug {358 args = append(args, "-debug")359 }360 mgr.cmd = NewManagerCmd(mgr.name, logFile, mgr.Errorf, bin, args...)361}362func (mgr *Manager) testImage(imageDir string, info *BuildInfo) error {363 log.Logf(0, "%v: testing image...", mgr.name)364 mgrcfg, err := mgr.createTestConfig(imageDir, info)365 if err != nil {366 return fmt.Errorf("failed to create manager config: %v", err)367 }368 defer os.RemoveAll(mgrcfg.Workdir)369 if !vm.AllowsOvercommit(mgrcfg.Type) {370 return nil // No support for creating machines out of thin air.371 }372 env, err := instance.NewEnv(mgrcfg)373 if err != nil {374 return err375 }376 const (377 testVMs = 3378 maxFailures = 1379 )380 results, err := env.Test(testVMs, nil, nil, nil)381 if err != nil {382 return err383 }384 failures := 0385 var failureErr error386 for _, res := range results {387 if res.Error == nil {388 continue389 }390 failures++391 switch err := res.Error.(type) {392 case *instance.TestError:393 if rep := err.Report; rep != nil {394 what := "test"395 if err.Boot {396 what = "boot"397 }398 rep.Title = fmt.Sprintf("%v %v error: %v",399 mgr.mgrcfg.RepoAlias, what, rep.Title)400 // There are usually no duplicates for boot errors, so we reset AltTitles.401 // But if we pass them, we would need to add the same prefix as for Title402 // in order to avoid duping boot bugs with non-boot bugs.403 rep.AltTitles = nil404 if err := mgr.reportBuildError(rep, info, imageDir); err != nil {405 mgr.Errorf("failed to report image error: %v", err)406 }407 }408 if err.Boot {409 failureErr = fmt.Errorf("VM boot failed with: %v", err)410 } else {411 failureErr = fmt.Errorf("VM testing failed with: %v", err)412 }413 default:414 failureErr = res.Error415 }416 }417 if failures > maxFailures {418 return failureErr419 }420 return nil421}422func (mgr *Manager) reportBuildError(rep *report.Report, info *BuildInfo, imageDir string) error {423 if mgr.dash == nil {424 log.Logf(0, "%v: image testing failed: %v\n\n%s\n\n%s",425 mgr.name, rep.Title, rep.Report, rep.Output)426 return nil427 }428 build, err := mgr.createDashboardBuild(info, imageDir, "error")429 if err != nil {430 return err431 }432 req := &dashapi.BuildErrorReq{433 Build: *build,434 Crash: dashapi.Crash{435 Title: rep.Title,436 AltTitles: rep.AltTitles,437 Corrupted: false, // Otherwise they get merged with other corrupted reports.438 Recipients: rep.Recipients.ToDash(),439 Log: rep.Output,440 Report: rep.Report,441 },442 }443 return mgr.dash.ReportBuildError(req)444}445func (mgr *Manager) createTestConfig(imageDir string, info *BuildInfo) (*mgrconfig.Config, error) {446 mgrcfg := new(mgrconfig.Config)447 *mgrcfg = *mgr.managercfg448 mgrcfg.Name += "-test"449 mgrcfg.Tag = info.KernelCommit450 mgrcfg.Workdir = filepath.Join(imageDir, "workdir")451 if err := instance.SetConfigImage(mgrcfg, imageDir, true); err != nil {452 return nil, err453 }454 mgrcfg.KernelSrc = mgr.kernelDir455 if err := mgrconfig.Complete(mgrcfg); err != nil {456 return nil, fmt.Errorf("bad manager config: %v", err)457 }458 return mgrcfg, nil459}460func (mgr *Manager) writeConfig(buildTag string) (string, error) {461 mgrcfg := new(mgrconfig.Config)462 *mgrcfg = *mgr.managercfg463 if mgr.dash != nil {464 mgrcfg.DashboardClient = mgr.dash.Client465 mgrcfg.DashboardAddr = mgr.dash.Addr466 mgrcfg.DashboardKey = mgr.dash.Key467 }468 if mgr.cfg.HubAddr != "" {469 mgrcfg.HubClient = mgr.cfg.Name470 mgrcfg.HubAddr = mgr.cfg.HubAddr471 mgrcfg.HubKey = mgr.cfg.HubKey472 }473 mgrcfg.Tag = buildTag474 mgrcfg.Workdir = mgr.workDir475 // There's not much point in keeping disabled progs in the syz-ci corpuses.476 // If the syscalls on some instance are enabled again, syz-hub will provide477 // it with the missing progs over time.478 // And, on the other hand, PreserveCorpus=false lets us disable syscalls in479 // the least destructive way for the rest of the corpus - calls will be cut480 // out the of programs and the leftovers will be retriaged.481 mgrcfg.PreserveCorpus = false482 if err := instance.SetConfigImage(mgrcfg, mgr.currentDir, false); err != nil {483 return "", err484 }485 // Strictly saying this is somewhat racy as builder can concurrently486 // update the source, or even delete and re-clone. If this causes487 // problems, we need to make a copy of sources after build.488 mgrcfg.KernelSrc = mgr.kernelDir489 if err := mgrconfig.Complete(mgrcfg); err != nil {490 return "", fmt.Errorf("bad manager config: %v", err)491 }492 configFile := filepath.Join(mgr.currentDir, "manager.cfg")493 if err := config.SaveFile(configFile, mgrcfg); err != nil {494 return "", err495 }496 return configFile, nil497}498func (mgr *Manager) uploadBuild(info *BuildInfo, imageDir string) (string, error) {499 if mgr.dash == nil {500 // Dashboard identifies builds by unique tags that are combined501 // from kernel tag, compiler tag and config tag.502 // This combined tag is meaningless without dashboard,503 // so we use kenrel tag (commit tag) because it communicates504 // at least some useful information.505 return info.KernelCommit, nil506 }507 build, err := mgr.createDashboardBuild(info, imageDir, "normal")508 if err != nil {509 return "", err510 }511 commitTitles, fixCommits, err := mgr.pollCommits(info.KernelCommit)512 if err != nil {513 // This is not critical for operation.514 mgr.Errorf("failed to poll commits: %v", err)515 }516 build.Commits = commitTitles517 build.FixCommits = fixCommits518 if err := mgr.dash.UploadBuild(build); err != nil {519 return "", err520 }521 return build.ID, nil522}523func (mgr *Manager) createDashboardBuild(info *BuildInfo, imageDir, typ string) (*dashapi.Build, error) {524 var kernelConfig []byte525 if kernelConfigFile := filepath.Join(imageDir, "kernel.config"); osutil.IsExist(kernelConfigFile) {526 var err error527 if kernelConfig, err = ioutil.ReadFile(kernelConfigFile); err != nil {528 return nil, fmt.Errorf("failed to read kernel.config: %v", err)529 }530 }531 // Resulting build depends on both kernel build tag and syzkaller commmit.532 // Also mix in build type, so that image error builds are not merged into normal builds.533 var tagData []byte534 tagData = append(tagData, info.Tag...)535 tagData = append(tagData, prog.GitRevisionBase...)536 tagData = append(tagData, typ...)537 build := &dashapi.Build{538 Manager: mgr.name,539 ID: hash.String(tagData),540 OS: mgr.managercfg.TargetOS,541 Arch: mgr.managercfg.TargetArch,542 VMArch: mgr.managercfg.TargetVMArch,543 SyzkallerCommit: prog.GitRevisionBase,544 SyzkallerCommitDate: prog.GitRevisionDate,545 CompilerID: info.CompilerID,546 KernelRepo: info.KernelRepo,547 KernelBranch: info.KernelBranch,548 KernelCommit: info.KernelCommit,549 KernelCommitTitle: info.KernelCommitTitle,550 KernelCommitDate: info.KernelCommitDate,551 KernelConfig: kernelConfig,552 }553 return build, nil554}555// pollCommits asks dashboard what commits it is interested in (i.e. fixes for556// open bugs) and returns subset of these commits that are present in a build557// on commit buildCommit.558func (mgr *Manager) pollCommits(buildCommit string) ([]string, []dashapi.Commit, error) {559 resp, err := mgr.dash.BuilderPoll(mgr.name)560 if err != nil || len(resp.PendingCommits) == 0 && resp.ReportEmail == "" {561 return nil, nil, err562 }563 var present []string564 if len(resp.PendingCommits) != 0 {565 commits, err := mgr.repo.ListRecentCommits(buildCommit)566 if err != nil {567 return nil, nil, err568 }569 m := make(map[string]bool, len(commits))570 for _, com := range commits {571 m[vcs.CanonicalizeCommit(com)] = true572 }573 for _, com := range resp.PendingCommits {574 if m[vcs.CanonicalizeCommit(com)] {575 present = append(present, com)576 }577 }578 }579 var fixCommits []dashapi.Commit580 if resp.ReportEmail != "" {581 if !brokenRepo(mgr.mgrcfg.Repo) {582 commits, err := mgr.repo.ExtractFixTagsFromCommits(buildCommit, resp.ReportEmail)583 if err != nil {584 return nil, nil, err585 }586 for _, com := range commits {587 fixCommits = append(fixCommits, dashapi.Commit{588 Title: com.Title,589 BugIDs: com.Tags,590 Date: com.Date,591 })592 }593 }594 }595 return present, fixCommits, nil596}597func (mgr *Manager) uploadCoverReport() error {598 // Report generation can consume lots of memory. Generate one at a time.599 select {600 case kernelBuildSem <- struct{}{}:601 case <-mgr.stop:602 return nil603 }604 defer func() { <-kernelBuildSem }()605 // Get coverage report from manager.606 addr := mgr.managercfg.HTTP607 if addr != "" && addr[0] == ':' {608 addr = "127.0.0.1" + addr // in case addr is ":port"609 }610 resp, err := http.Get(fmt.Sprintf("http://%v/cover", addr))611 if err != nil {612 return fmt.Errorf("failed to get report: %v", err)613 }614 defer resp.Body.Close()615 // Upload coverage report.616 return uploadFile(mgr.cfg.CoverUploadPath, mgr.name+".html", resp.Body)617}618func (mgr *Manager) uploadCorpus() error {619 f, err := os.Open(filepath.Join(mgr.workDir, "corpus.db"))620 if err != nil {621 return err622 }623 defer f.Close()624 return uploadFile(mgr.cfg.CorpusUploadPath, mgr.name+"-corpus.db", f)625}626func uploadFile(dstPath, name string, file io.Reader) error {627 URL, err := url.Parse(dstPath)628 if err != nil {629 return fmt.Errorf("failed to parse upload path: %v", err)630 }631 URL.Path = path.Join(URL.Path, name)632 URLStr := URL.String()...
uploadCorpus
Using AI Code Generation
1import (2func main() {3 client, err := rpc.DialHTTP("tcp", "localhost:9000")4 if err != nil {5 fmt.Println(err)6 }7 err = client.Call("main.uploadCorpus", os.Args[1], &reply)8 if err != nil {9 fmt.Println(err)10 }11 fmt.Println(reply)12}13import (14func (t *Main) uploadCorpus(path string, reply *bool) error {15 fmt.Println("uploadCorpus called")16 fmt.Println("Path: ", path)17}18func main() {19 main := new(Main)20 rpc.Register(main)21 rpc.HandleHTTP()22 listener, e := net.Listen("tcp", ":9000")23 if e != nil {24 fmt.Println("listen error:", e)25 }26 http.Serve(listener, nil)27}
uploadCorpus
Using AI Code Generation
1import (2func main() {3 fmt.Println("Starting the application...")4 http.HandleFunc("/", uploadCorpus)5 http.ListenAndServe(":3000", nil)6}7func uploadCorpus(w http.ResponseWriter, r *http.Request) {8 fmt.Fprintf(w, "Upload Corpus Request")9}10import (11func main() {12 fmt.Println("Starting the application...")13 http.HandleFunc("/", uploadCorpus)14 http.ListenAndServe(":3000", nil)15}16func uploadCorpus(w http.ResponseWriter, r *http.Request) {17 fmt.Fprintf(w, "Upload Corpus Request")18}19import (20func main() {21 fmt.Println("Starting the application...")22 http.HandleFunc("/", uploadCorpus)23 http.ListenAndServe(":3000", nil)24}25func uploadCorpus(w http.ResponseWriter, r *http.Request) {26 fmt.Fprintf(w, "Upload Corpus Request")27}28import (29func main() {30 fmt.Println("Starting the application...")31 http.HandleFunc("/", uploadCorpus)32 http.ListenAndServe(":3000", nil)33}34func uploadCorpus(w http.ResponseWriter, r *http.Request) {35 fmt.Fprintf(w, "Upload Corpus Request")36}37import (38func main() {39 fmt.Println("Starting the application...")40 http.HandleFunc("/", uploadCorpus)41 http.ListenAndServe(":3000", nil)42}43func uploadCorpus(w http.ResponseWriter, r *http.Request) {44 fmt.Fprintf(w, "Upload Corpus Request")45}46import (47func main() {48 fmt.Println("Starting the application...")49 http.HandleFunc("/", uploadCorpus)50 http.ListenAndServe(":3000", nil)51}
uploadCorpus
Using AI Code Generation
1type UploadCorpusRequest struct {2}3func (x *UploadCorpusRequest) Reset() {4 *x = UploadCorpusRequest{}5 if protoimpl.UnsafeEnabled {6 ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))7 ms.StoreMessageInfo(mi)8 }9}10func (x *UploadCorpusRequest) String() string {11 return protoimpl.X.MessageStringOf(x)12}13func (*UploadCorpusRequest) ProtoMessage() {}14func (x *UploadCorpusRequest) ProtoReflect() protoreflect.Message {15 if protoimpl.UnsafeEnabled && x != nil {16 ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))17 if ms.LoadMessageInfo() == nil {18 ms.StoreMessageInfo(mi)19 }20 }21 return mi.MessageOf(x)22}23func (*UploadCorpusRequest) Descriptor() ([]byte, []int) {24 return file_proto_goog_fhir_proto_r4_core_resources_corpus_proto_rawDescGZIP(), []int{3}25}26func (x *UploadCorpusRequest) GetName() string {27 if x != nil {28 }29}30func (x *UploadCorpusRequest) GetCorpus() *Corpus {31 if x != nil {32 }33}34type UploadCorpusResponse struct {
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!!