How to use testCProg method of repro Package

Best Syzkaller code snippet using repro.testCProg

repro.go

Source:repro.go Github

copy

Full Screen

...141 // Try to rerun the repro if the report is corrupted.142 for attempts := 0; ctx.report.Corrupted && attempts < 3; attempts++ {143 ctx.reproLog(3, "report is corrupted, running repro again")144 if res.CRepro {145 _, err = ctx.testCProg(res.Prog, res.Duration, res.Opts)146 } else {147 _, err = ctx.testProg(res.Prog, res.Duration, res.Opts)148 }149 if err != nil {150 return nil, err151 }152 }153 ctx.reproLog(3, "final repro crashed as (corrupted=%v):\n%s",154 ctx.report.Corrupted, ctx.report.Report)155 res.Report = ctx.report156 res.Stats = ctx.stats157 }158 close(ctx.bootRequests)159 for inst := range ctx.instances {160 inst.Close()161 }162 return res, err163}164func (ctx *context) repro(entries []*prog.LogEntry, crashStart int) (*Result, error) {165 // Cut programs that were executed after crash.166 for i, ent := range entries {167 if ent.Start > crashStart {168 entries = entries[:i]169 break170 }171 }172 reproStart := time.Now()173 defer func() {174 ctx.reproLog(3, "reproducing took %s", time.Since(reproStart))175 }()176 res, err := ctx.extractProg(entries)177 if err != nil {178 return nil, err179 }180 if res == nil {181 return nil, nil182 }183 defer func() {184 if res != nil {185 res.Opts.Repro = false186 }187 }()188 res, err = ctx.minimizeProg(res)189 if err != nil {190 return nil, err191 }192 // Try extracting C repro without simplifying options first.193 res, err = ctx.extractC(res)194 if err != nil {195 return nil, err196 }197 // Simplify options and try extracting C repro.198 if !res.CRepro {199 res, err = ctx.simplifyProg(res)200 if err != nil {201 return nil, err202 }203 }204 // Simplify C related options.205 if res.CRepro {206 res, err = ctx.simplifyC(res)207 if err != nil {208 return nil, err209 }210 }211 return res, nil212}213func (ctx *context) extractProg(entries []*prog.LogEntry) (*Result, error) {214 ctx.reproLog(2, "extracting reproducer from %v programs", len(entries))215 start := time.Now()216 defer func() {217 ctx.stats.ExtractProgTime = time.Since(start)218 }()219 // Extract last program on every proc.220 procs := make(map[int]int)221 for i, ent := range entries {222 procs[ent.Proc] = i223 }224 var indices []int225 for _, idx := range procs {226 indices = append(indices, idx)227 }228 sort.Ints(indices)229 var lastEntries []*prog.LogEntry230 for i := len(indices) - 1; i >= 0; i-- {231 lastEntries = append(lastEntries, entries[indices[i]])232 }233 // The shortest duration is 10 seconds to detect simple crashes (i.e. no races and no hangs).234 // The longest duration is 5 minutes to catch races and hangs. Note that this value must be larger235 // than hang/no output detection duration in vm.MonitorExecution, which is currently set to 3 mins.236 timeouts := []time.Duration{10 * time.Second, 1 * time.Minute, 5 * time.Minute}237 for _, timeout := range timeouts {238 // Execute each program separately to detect simple crashes caused by a single program.239 // Programs are executed in reverse order, usually the last program is the guilty one.240 res, err := ctx.extractProgSingle(reverseEntries(lastEntries), timeout)241 if err != nil {242 return nil, err243 }244 if res != nil {245 ctx.reproLog(3, "found reproducer with %d syscalls", len(res.Prog.Calls))246 return res, nil247 }248 // Don't try bisecting if there's only one entry.249 if len(entries) == 1 {250 continue251 }252 // Execute all programs and bisect the log to find multiple guilty programs.253 res, err = ctx.extractProgBisect(reverseEntries(entries), timeout)254 if err != nil {255 return nil, err256 }257 if res != nil {258 ctx.reproLog(3, "found reproducer with %d syscalls", len(res.Prog.Calls))259 return res, nil260 }261 }262 ctx.reproLog(0, "failed to extract reproducer")263 return nil, nil264}265func (ctx *context) createDefaultOps() csource.Options {266 opts := csource.Options{267 Threaded: true,268 Collide: true,269 Repeat: true,270 Procs: ctx.cfg.Procs,271 Sandbox: ctx.cfg.Sandbox,272 EnableTun: true,273 EnableCgroups: true,274 EnableNetdev: true,275 ResetNet: true,276 UseTmpDir: true,277 HandleSegv: true,278 WaitRepeat: true,279 Repro: true,280 }281 return opts282}283func (ctx *context) extractProgSingle(entries []*prog.LogEntry, duration time.Duration) (*Result, error) {284 ctx.reproLog(3, "single: executing %d programs separately with timeout %s", len(entries), duration)285 opts := ctx.createDefaultOps()286 for _, ent := range entries {287 opts.Fault = ent.Fault288 opts.FaultCall = ent.FaultCall289 opts.FaultNth = ent.FaultNth290 if opts.FaultCall < 0 || opts.FaultCall >= len(ent.P.Calls) {291 opts.FaultCall = len(ent.P.Calls) - 1292 }293 crashed, err := ctx.testProg(ent.P, duration, opts)294 if err != nil {295 return nil, err296 }297 if crashed {298 res := &Result{299 Prog: ent.P,300 Duration: duration * 3 / 2,301 Opts: opts,302 }303 ctx.reproLog(3, "single: successfully extracted reproducer")304 return res, nil305 }306 }307 ctx.reproLog(3, "single: failed to extract reproducer")308 return nil, nil309}310func (ctx *context) extractProgBisect(entries []*prog.LogEntry, baseDuration time.Duration) (*Result, error) {311 ctx.reproLog(3, "bisect: bisecting %d programs with base timeout %s", len(entries), baseDuration)312 opts := ctx.createDefaultOps()313 duration := func(entries int) time.Duration {314 return baseDuration + time.Duration((entries/4))*time.Second315 }316 // Bisect the log to find multiple guilty programs.317 entries, err := ctx.bisectProgs(entries, func(progs []*prog.LogEntry) (bool, error) {318 return ctx.testProgs(progs, duration(len(progs)), opts)319 })320 if err != nil {321 return nil, err322 }323 if len(entries) == 0 {324 return nil, nil325 }326 // TODO: Minimize each program before concatenation.327 // TODO: Return multiple programs if concatenation fails.328 ctx.reproLog(3, "bisect: %d programs left: \n\n%s\n", len(entries), encodeEntries(entries))329 ctx.reproLog(3, "bisect: trying to concatenate")330 // Concatenate all programs into one.331 prog := &prog.Prog{332 Target: entries[0].P.Target,333 }334 for _, entry := range entries {335 prog.Calls = append(prog.Calls, entry.P.Calls...)336 }337 dur := duration(len(entries)) * 3 / 2338 // Execute the program without fault injection.339 crashed, err := ctx.testProg(prog, dur, opts)340 if err != nil {341 return nil, err342 }343 if crashed {344 res := &Result{345 Prog: prog,346 Duration: dur,347 Opts: opts,348 }349 ctx.reproLog(3, "bisect: concatenation succeeded")350 return res, nil351 }352 // Try with fault injection.353 calls := 0354 for _, entry := range entries {355 if entry.Fault {356 opts.FaultCall = calls + entry.FaultCall357 opts.FaultNth = entry.FaultNth358 if entry.FaultCall < 0 || entry.FaultCall >= len(entry.P.Calls) {359 opts.FaultCall = calls + len(entry.P.Calls) - 1360 }361 crashed, err := ctx.testProg(prog, dur, opts)362 if err != nil {363 return nil, err364 }365 if crashed {366 res := &Result{367 Prog: prog,368 Duration: dur,369 Opts: opts,370 }371 ctx.reproLog(3, "bisect: concatenation succeeded with fault injection")372 return res, nil373 }374 }375 calls += len(entry.P.Calls)376 }377 ctx.reproLog(3, "bisect: concatenation failed")378 return nil, nil379}380// Minimize calls and arguments.381func (ctx *context) minimizeProg(res *Result) (*Result, error) {382 ctx.reproLog(2, "minimizing guilty program")383 start := time.Now()384 defer func() {385 ctx.stats.MinimizeProgTime = time.Since(start)386 }()387 call := -1388 if res.Opts.Fault {389 call = res.Opts.FaultCall390 }391 res.Prog, res.Opts.FaultCall = prog.Minimize(res.Prog, call, true,392 func(p1 *prog.Prog, callIndex int) bool {393 crashed, err := ctx.testProg(p1, res.Duration, res.Opts)394 if err != nil {395 ctx.reproLog(0, "minimization failed with %v", err)396 return false397 }398 return crashed399 })400 return res, nil401}402// Simplify repro options (threaded, collide, sandbox, etc).403func (ctx *context) simplifyProg(res *Result) (*Result, error) {404 ctx.reproLog(2, "simplifying guilty program")405 start := time.Now()406 defer func() {407 ctx.stats.SimplifyProgTime = time.Since(start)408 }()409 for _, simplify := range progSimplifies {410 opts := res.Opts411 if simplify(&opts) {412 crashed, err := ctx.testProg(res.Prog, res.Duration, opts)413 if err != nil {414 return nil, err415 }416 if crashed {417 res.Opts = opts418 // Simplification successful, try extracting C repro.419 res, err := ctx.extractC(res)420 if err != nil {421 return nil, err422 }423 if res.CRepro {424 return res, nil425 }426 }427 }428 }429 return res, nil430}431// Try triggering crash with a C reproducer.432func (ctx *context) extractC(res *Result) (*Result, error) {433 ctx.reproLog(2, "extracting C reproducer")434 start := time.Now()435 defer func() {436 ctx.stats.ExtractCTime = time.Since(start)437 }()438 crashed, err := ctx.testCProg(res.Prog, res.Duration, res.Opts)439 if err != nil {440 return nil, err441 }442 res.CRepro = crashed443 return res, nil444}445// Try to simplify the C reproducer.446func (ctx *context) simplifyC(res *Result) (*Result, error) {447 ctx.reproLog(2, "simplifying C reproducer")448 start := time.Now()449 defer func() {450 ctx.stats.SimplifyCTime = time.Since(start)451 }()452 for _, simplify := range cSimplifies {453 opts := res.Opts454 if simplify(&opts) {455 crashed, err := ctx.testCProg(res.Prog, res.Duration, opts)456 if err != nil {457 return nil, err458 }459 if crashed {460 res.Opts = opts461 }462 }463 }464 return res, nil465}466func (ctx *context) testProg(p *prog.Prog, duration time.Duration, opts csource.Options) (crashed bool, err error) {467 entry := prog.LogEntry{P: p}468 if opts.Fault {469 entry.Fault = true470 entry.FaultCall = opts.FaultCall471 entry.FaultNth = opts.FaultNth472 }473 return ctx.testProgs([]*prog.LogEntry{&entry}, duration, opts)474}475func (ctx *context) testProgs(entries []*prog.LogEntry, duration time.Duration, opts csource.Options) (476 crashed bool, err error) {477 inst := <-ctx.instances478 if inst == nil {479 return false, fmt.Errorf("all VMs failed to boot")480 }481 defer ctx.returnInstance(inst)482 if len(entries) == 0 {483 return false, fmt.Errorf("no programs to execute")484 }485 pstr := encodeEntries(entries)486 progFile, err := osutil.WriteTempFile(pstr)487 if err != nil {488 return false, err489 }490 defer os.Remove(progFile)491 vmProgFile, err := inst.Copy(progFile)492 if err != nil {493 return false, fmt.Errorf("failed to copy to VM: %v", err)494 }495 repeat := 1496 if opts.Repeat {497 repeat = 0498 }499 if !opts.Fault {500 opts.FaultCall = -1501 }502 program := entries[0].P.String()503 if len(entries) > 1 {504 program = "["505 for i, entry := range entries {506 program += fmt.Sprintf("%v", len(entry.P.Calls))507 if i != len(entries)-1 {508 program += ", "509 }510 }511 program += "]"512 }513 command := fmt.Sprintf("%v -executor %v -arch=%v -cover=0 -procs=%v -repeat=%v"+514 " -sandbox %v -threaded=%v -collide=%v %v",515 inst.execprogBin, inst.executorBin, ctx.cfg.TargetArch, opts.Procs, repeat,516 opts.Sandbox, opts.Threaded, opts.Collide, vmProgFile)517 ctx.reproLog(2, "testing program (duration=%v, %+v): %s", duration, opts, program)518 return ctx.testImpl(inst.Instance, command, duration)519}520func (ctx *context) testCProg(p *prog.Prog, duration time.Duration, opts csource.Options) (crashed bool, err error) {521 src, err := csource.Write(p, opts)522 if err != nil {523 return false, err524 }525 srcf, err := osutil.WriteTempFile(src)526 if err != nil {527 return false, err528 }529 defer os.Remove(srcf)530 bin, err := csource.Build(p.Target, "c", srcf)531 if err != nil {532 return false, err533 }534 defer os.Remove(bin)...

Full Screen

Full Screen

testCProg

Using AI Code Generation

copy

Full Screen

1repro := NewRepro()2repro.testCProg()3repro := NewRepro()4repro.testCProg()5repro := NewRepro()6repro.testCProg()7repro := NewRepro()8repro.testCProg()9repro := NewRepro()10repro.testCProg()11repro := NewRepro()12repro.testCProg()13repro := NewRepro()14repro.testCProg()15repro := NewRepro()16repro.testCProg()17repro := NewRepro()18repro.testCProg()19repro := NewRepro()20repro.testCProg()21repro := NewRepro()22repro.testCProg()23repro := NewRepro()24repro.testCProg()25repro := NewRepro()26repro.testCProg()27repro := NewRepro()28repro.testCProg()29repro := NewRepro()30repro.testCProg()31repro := NewRepro()32repro.testCProg()

Full Screen

Full Screen

testCProg

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World")4}5import (6func main() {7 fmt.Println("Hello World")8}9import (10func main() {11 fmt.Println("Hello World")12}13import (14func main() {15 fmt.Println("Hello World")16}17import (18func main() {19 fmt.Println("Hello World")20}21import (22func main() {23 fmt.Println("Hello World")24}25import (26func main() {27 fmt.Println("Hello World")28}29import (30func main() {31 fmt.Println("Hello World")32}33import (34func main() {35 fmt.Println("Hello World")36}37import (38func main() {39 fmt.Println("Hello World")40}41import (42func main() {43 fmt.Println("Hello World")44}45import (46func main() {47 fmt.Println("Hello World")48}49import (50func main() {51 fmt.Println("Hello World")52}

Full Screen

Full Screen

testCProg

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r.TestCProg()4 fmt.Println("Hello, playground")5}6import "fmt"7type Repro struct {8}9func (r Repro) TestCProg() {10 fmt.Println("Hello, playground")11}12./2.go:8: cannot use r (type Repro) as type repro.Repro in argument to repro.TestCProg13How can I import the repro package and use the TestCProg method?

Full Screen

Full Screen

testCProg

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "C"3func main() {4 r.testCProg()5}6import "fmt"7import "C"8func main() {9 r.testCProg()10}11import "fmt"12import "C"13func main() {14 r.testCProg()15}16import "fmt"17import "C"18func main() {19 r.testCProg()20}21import "fmt"22import "C"23func main() {24 r.testCProg()25}26import "fmt"27import "C"28func main() {29 r.testCProg()30}31import "fmt"32import "C"33func main() {34 r.testCProg()35}36import "fmt"37import "C"38func main() {39 r.testCProg()40}41import "fmt"42import "C"43func main() {44 r.testCProg()45}46import "fmt"47import "C"48func main() {49 r.testCProg()50}51import "fmt"52import "C"53func main() {54 r.testCProg()

Full Screen

Full Screen

testCProg

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(repro.TestCProg())4}5import (6func main() {7 fmt.Println(repro.TestCProg())8}9import (10func main() {11 fmt.Println(repro.TestCProg())12}13import (14func main() {15 fmt.Println(repro.TestCProg())16}17import (18func main() {19 fmt.Println(repro.TestCProg())20}21import (22func main() {23 fmt.Println(repro.TestCProg())24}25import (26func main() {27 fmt.Println(repro.TestCProg())28}29import (30func main() {31 fmt.Println(repro.TestCProg())32}33import (34func main() {35 fmt.Println(repro.TestCProg())36}

Full Screen

Full Screen

testCProg

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "repro"3func main() {4 fmt.Println(a.TestCProg())5}6import "fmt"7type A struct {}8func (a A) TestCProg() string {9 return fmt.Sprint("Hello from TestCProg")10}

Full Screen

Full Screen

testCProg

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("./testCProg")4 err := cmd.Run()5 if err != nil {6 fmt.Println("Error: ", err)7 }8 fmt.Println("Exit code: ", cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus())9}

Full Screen

Full Screen

testCProg

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 canvas := svg.New(os.Stdout)4 canvas.Start(w, h)5 canvas.Rect(x, y, w, h, "fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)")6 canvas.End()7}8import (9func main() {10 canvas := svg.New(os.Stdout)11 canvas.Start(w, h)12 canvas.Rect(x, y, w, h, "fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)")13 canvas.End()14}15import (16func main() {17 canvas := svg.New(os.Stdout)18 canvas.Start(w, h)19 canvas.Rect(x, y, w, h, "fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)")20 canvas.End()21}22import (23func main() {24 canvas := svg.New(os.Stdout)

Full Screen

Full Screen

testCProg

Using AI Code Generation

copy

Full Screen

1import (2func testCProg(s *C.char) *C.char {3 fmt.Println("Inside testCProg function")4}5func main() {6 fmt.Println("Inside main function")7 s := C.CString("Hello World")8 defer C.free(unsafe.Pointer(s))9 testCProg(s)10}11import "C"12import (13func main() {14 fmt.Println("Inside main function")15 s := C.CString("Hello World")16 defer C.free(unsafe.Pointer(s))17 C.testCProg(s)18}19gcc (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful