From 219279048112b60772344969aaf61a6e67f08e08 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 3 Dec 2018 11:28:27 +0100 Subject: pkg/csource: reduce short tests pkg/csource test gets OOM-killed on travis: https://travis-ci.org/google/syzkaller/jobs/461827347 https://travis-ci.org/google/syzkaller/jobs/460226110 Add several measures: - set GOMAXPROCS=1 to restrict parallel processes - remove -g from compiler invocation - reduce set of tests run in short mode to compensate for GOMAXPROCS=1 - also reduce set of tests in full mode as they timeout now --- pkg/csource/build.go | 2 +- pkg/csource/csource_test.go | 81 +++++++++++++++++++++++---------------------- 2 files changed, 43 insertions(+), 40 deletions(-) (limited to 'pkg') diff --git a/pkg/csource/build.go b/pkg/csource/build.go index 391d437a1..bdafe9a63 100644 --- a/pkg/csource/build.go +++ b/pkg/csource/build.go @@ -39,7 +39,7 @@ func build(target *prog.Target, src []byte, file string) (string, error) { } flags := []string{ - "-Wall", "-Werror", "-O1", "-g", "-o", bin, "-pthread", + "-Wall", "-Werror", "-O1", "-o", bin, "-pthread", "-DGOOS_" + target.OS + "=1", "-DGOARCH_" + target.Arch + "=1", } diff --git a/pkg/csource/csource_test.go b/pkg/csource/csource_test.go index c8246deae..c360dac86 100644 --- a/pkg/csource/csource_test.go +++ b/pkg/csource/csource_test.go @@ -7,8 +7,8 @@ import ( "fmt" "math/rand" "os" + "os/exec" "runtime" - "strings" "testing" "time" @@ -17,11 +17,20 @@ import ( "github.com/google/syzkaller/sys/targets" ) +func init() { + if os.Getenv("TRAVIS") != "" { + // Otherwise tests OOM on travis. + runtime.GOMAXPROCS(1) + } +} + func TestGenerate(t *testing.T) { t.Parallel() + checked := make(map[string]bool) for _, target := range prog.AllTargets() { target := target - if runtime.GOOS != targets.Get(target.OS, target.Arch).BuildOS { + sysTarget := targets.Get(target.OS, target.Arch) + if runtime.GOOS != sysTarget.BuildOS { continue } t.Run(target.OS+"/"+target.Arch, func(t *testing.T) { @@ -40,50 +49,47 @@ func TestGenerate(t *testing.T) { // The same reason as linux/32. t.Skip("broken") } + if _, err := exec.LookPath(sysTarget.CCompiler); err != nil { + t.Skipf("no target compiler %v", sysTarget.CCompiler) + } + full := !checked[target.OS] + checked[target.OS] = true t.Parallel() - testTarget(t, target) + testTarget(t, target, full) }) + } } -func testTarget(t *testing.T, target *prog.Target) { +func testTarget(t *testing.T, target *prog.Target, full bool) { seed := int64(time.Now().UnixNano()) rs := rand.NewSource(seed) t.Logf("seed=%v", seed) - r := rand.New(rs) - progs := []*prog.Prog{target.GenerateSimpleProg()} - if p := target.GenerateAllSyzProg(rs); len(p.Calls) != 0 { - progs = append(progs, p) - } - if !testing.Short() { - progs = append(progs, target.Generate(rs, 10, nil)) - } - opts := allOptionsSingle(target.OS) - opts = append(opts, Options{ - Threaded: true, - Collide: true, - Repeat: true, - Procs: 2, - Sandbox: "none", - Repro: true, - UseTmpDir: true, - }) - allPermutations := allOptionsPermutations(target.OS) - if testing.Short() { - for i := 0; i < 16; i++ { - opts = append(opts, allPermutations[r.Intn(len(allPermutations))]) - } + p := target.Generate(rs, 10, nil) + p.Calls = append(p.Calls, target.GenerateAllSyzProg(rs).Calls...) + var opts []Options + if !full || testing.Short() { + opts = allOptionsSingle(target.OS) + // This is the main configuration used by executor, + // so we want to test it as well. + opts = append(opts, Options{ + Threaded: true, + Collide: true, + Repeat: true, + Procs: 2, + Sandbox: "none", + Repro: true, + UseTmpDir: true, + }) } else { - opts = allPermutations + opts = allOptionsPermutations(target.OS) } - for pi, p := range progs { - for opti, opts := range opts { - p, opts := p, opts - t.Run(fmt.Sprintf("%v/%v", pi, opti), func(t *testing.T) { - t.Parallel() - testOne(t, p, opts) - }) - } + for opti, opts := range opts { + opts := opts + t.Run(fmt.Sprintf("%v", opti), func(t *testing.T) { + t.Parallel() + testOne(t, p, opts) + }) } } @@ -95,9 +101,6 @@ func testOne(t *testing.T, p *prog.Prog, opts Options) { } bin, err := Build(p.Target, src) if err != nil { - if strings.Contains(err.Error(), "no target compiler") { - t.Skip(err) - } t.Logf("opts: %+v\nprogram:\n%s\n", opts, p.Serialize()) t.Fatalf("%v", err) } -- cgit mrf-deployment