How to use testProg method of repro Package

Best Syzkaller code snippet using repro.testProg

repro.go

Source:repro.go Github

copy

Full Screen

...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 }...

Full Screen

Full Screen

testProg

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := repro{}4 r.testProg()5}6import (7func main() {8 r := repro{}9 r.testProg()10}11import (12func main() {13 r := repro{}14 r.testProg()15}16import (17func main() {18 r := repro{}19 r.testProg()20}21import (22func main() {23 r := repro{}24 r.testProg()25}26import (27func main() {28 r := repro{}29 r.testProg()30}31import (32func main() {33 r := repro{}34 r.testProg()35}36import (37func main() {38 r := repro{}39 r.testProg()40}41import (42func main() {43 r := repro{}44 r.testProg()45}46import (47func main() {48 r := repro{}49 r.testProg()50}51import (52func main() {53 r := repro{}54 r.testProg()55}56import (57func main() {58 r := repro{}59 r.testProg()60}61import (62func main() {63 r := repro{}

Full Screen

Full Screen

testProg

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

testProg

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

testProg

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 r.testProg()4 fmt.Println("Hello, playground")5}6import "fmt"7type repro struct {8}9func (r *repro) testProg() {10 fmt.Println("testProg")11}12import "fmt"13type repro struct {14}15func (r *repro) testProg() {16 fmt.Println("testProg")17}18import "fmt"19type repro struct {20}21func (r *repro) testProg() {22 fmt.Println("testProg")23}24import "fmt"25type repro struct {26}27func (r *repro) testProg() {28 fmt.Println("testProg")29}30import "fmt"31type repro struct {32}33func (r *repro) testProg() {34 fmt.Println("testProg")35}36import "fmt"37type repro struct {38}39func (r *repro) testProg() {40 fmt.Println("testProg")41}42import "fmt"43type repro struct {44}45func (r *repro) testProg() {46 fmt.Println("testProg")47}48import "fmt"49type repro struct {50}51func (r *repro) testProg() {52 fmt.Println("testProg")53}54import "fmt"55type repro struct {56}57func (r *repro) testProg() {58 fmt.Println("testProg")59}60import "fmt"61type repro struct {

Full Screen

Full Screen

testProg

Using AI Code Generation

copy

Full Screen

1import "repro"2func main() {3 repro.TestProg()4}5func TestProg() {6 println("Hello, World!")7}8can't find import: "repro"9can't find import: "repro"10I have tried this with both go 1.0.3 and 1.1. I have also tried running go build from the repro directory, but it still can't find the import. What am I doing wrong?

Full Screen

Full Screen

testProg

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var test = repro{}4 bar := progress.NewBar(100)5 bar2 := progress.NewBar(100)6 bar.Start()7 bar2.Start()8 for i := 0; i < 100; i++ {9 test.testProg()10 bar.Tick()11 bar2.Tick()12 }13 bar.Finish()14 bar2.Finish()15 fmt.Println(goterm.Color(goterm.Bold("Test"), goterm.GREEN))16 fmt.Println(goterm.Color(goterm.Bold("Test2"), goterm.GREEN))17}18import (19func main() {20 var test = repro{}21 bar := progress.NewBar(100)22 bar2 := progress.NewBar(100)23 bar.Start()24 bar2.Start()25 for i := 0; i < 100; i++ {26 test.testProg()27 bar.Tick()28 bar2.Tick()29 }30 bar.Finish()31 bar2.Finish()32 fmt.Println(goterm.Color(goterm.Bold("Test"), goterm.GREEN))33 fmt.Println(goterm.Color(goterm.Bold("Test2"), goterm.GREEN))34}

Full Screen

Full Screen

testProg

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(repro.TestProg())4}5import (6func TestProg() string {7 return fmt.Sprintf("Hello %s", other.TestOther())8}9func TestOther() string {10}11import (12func TestTestProg(t *testing.T) {13 other.TestOther = func() string {14 }15 result := TestProg()16 if result != "Hello Mocked World" {17 t.Errorf("TestProg() failed, expected: %s, got: %s.", "Hello Mocked World", result)18 }19}20import (21func TestTestProg(t *testing.T) {22 other.TestOther = func() string {23 }24 result := TestProg()25 if result != "Hello Mocked World" {26 t.Errorf("TestProg() failed, expected: %s, got: %s.", "Hello Mocked World", result)27 }28}29import (

Full Screen

Full Screen

testProg

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 repro.TestProg()5}6import (7func main() {8 fmt.Println("Hello, playground")9 repro.TestProg()10}11import (12func main() {13 fmt.Println("Hello, playground")14 repro.TestProg()15}16import "fmt"17func TestProg() {18 fmt.Println("testProg called")19}20import "testing"21func TestTestProg(t *testing.T) {22 TestProg()23}24import "fmt"25func TestProg2() {26 fmt.Println("testProg2 called")27}28import "testing"29func TestTestProg2(t *testing.T) {30 TestProg2()31}32import "fmt"33func TestProg3() {34 fmt.Println("testProg3 called")35}36import "testing"37func TestTestProg3(t *testing.T) {38 TestProg3()39}40import "fmt"41func TestProg4() {42 fmt.Println("testProg4 called")43}44import "testing

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