Skip to content

Commit

Permalink
pkg/csource: test sys/*/test programs
Browse files Browse the repository at this point in the history
Running sys/*/test programs requires real machines and kernels for each OS.
We can't do that in unit tests, but at least try to deserialize these programs
so that they don't get rotten.
  • Loading branch information
dvyukov committed Jul 22, 2019
1 parent d8b4c31 commit e530ec1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
63 changes: 52 additions & 11 deletions pkg/csource/csource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ package csource

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/prog"
_ "github.com/google/syzkaller/sys"
"github.com/google/syzkaller/sys/targets"
Expand Down Expand Up @@ -48,6 +52,17 @@ func TestGenerate(t *testing.T) {
}
}

// This is the main configuration used by executor, so we want to test it as well.
var executorOpts = Options{
Threaded: true,
Collide: true,
Repeat: true,
Procs: 2,
Sandbox: "none",
Repro: true,
UseTmpDir: true,
}

func testTarget(t *testing.T, target *prog.Target, full bool) {
seed := time.Now().UnixNano()
if os.Getenv("TRAVIS") != "" {
Expand All @@ -67,17 +82,7 @@ func testTarget(t *testing.T, target *prog.Target, full bool) {
if !full || testing.Short() {
p.Calls = append(p.Calls, syzProg.Calls...)
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,
})
opts = append(opts, executorOpts)
} else {
minimized, _ := prog.Minimize(syzProg, -1, false, func(p *prog.Prog, call int) bool {
return len(p.Calls) == len(syzProg.Calls)
Expand Down Expand Up @@ -107,3 +112,39 @@ func testOne(t *testing.T, p *prog.Prog, opts Options) {
}
defer os.Remove(bin)
}

func TestSysTests(t *testing.T) {
t.Parallel()
for _, target := range prog.AllTargets() {
target := target
t.Run(target.OS+"/"+target.Arch, func(t *testing.T) {
t.Parallel()
dir := filepath.Join("..", "..", "sys", target.OS, "test")
if !osutil.IsExist(dir) {
return
}
files, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatalf("failed to read %v: %v", dir, err)
}
for _, finfo := range files {
file := filepath.Join(dir, finfo.Name())
if strings.HasSuffix(file, "~") || strings.HasSuffix(file, ".swp") {
continue
}
data, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("failed to read %v: %v", file, err)
}
p, err := target.Deserialize(data, prog.Strict)
if err != nil {
t.Fatalf("failed to parse program %v: %v", file, err)
}
_, err = Write(p, executorOpts)
if err != nil {
t.Fatalf("failed to generate C source for %v: %v", file, err)
}
}
})
}
}
4 changes: 3 additions & 1 deletion sys/linux/test/vusb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# requires: -sandbox=setuid -sandbox=namespace

syz_usb_connect(0x0, 0x24, &(0x7f0000000000)={0x12, 0x1, 0x0, 0x97, 0xff, 0x82, 0x8, 0x2058, 0x1005, 0xc19b, 0x0, 0x0, 0x0, 0x1, [{0x9, 0x2, 0x12, 0x1, 0x0, 0x0, 0x0, 0x0, [{0x9, 0x4, 0x8f, 0x0, 0x0, 0xbf, 0x57, 0x5a, 0x0, [], []}]}]}, 0x0)
# Temporary disabled because deserialization fails (run go test ./pkg/csource).
#syz_usb_connect(0x0, 0x24, &(0x7f0000000000)={0x12, 0x1, 0x0, 0x97, 0xff, 0x82, 0x8, 0x2058, 0x1005, 0xc19b, 0x0, 0x0, 0x0, 0x1, [{0x9, 0x2, 0x12, 0x1, 0x0, 0x0, 0x0, 0x0, [{0x9, 0x4, 0x8f, 0x0, 0x0, 0xbf, 0x57, 0x5a, 0x0, [], []}]}]}, 0x0)

This comment has been minimized.

Copy link
@dvyukov

dvyukov Jul 22, 2019

Author Collaborator

@xairy this program needs some updating

This comment has been minimized.

Copy link
@dvyukov

dvyukov Jul 22, 2019

Author Collaborator
    --- FAIL: TestSysTests/linux/ppc64le (0.57s)
        csource_test.go:142: failed to parse program ../../sys/linux/test/vusb: wrong int arg
            line #3:50: syz_usb_connect(0x0, 0x24, &(0x7f0000000000)={0x12, 0x1, 0x0, 0x97, 0xff, 0x82, 0x8, 0x2058, 0x1005, 0xc19b, 0x0, 0x0, 0x0, 0x1, [{0x9, 0x2, 0x12, 0x1, 0x0, 0x0, 0x0, 0x0, [{0x9, 0x4, 0x8f, 0x0, 0x0, 0xbf, 0x57, 0x5a, 0x0, [], []}]}]}, 0x0)
getpid()

0 comments on commit e530ec1

Please sign in to comment.