aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/csource
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-09-14 19:25:01 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-09-15 16:02:37 +0200
commit52a33fd516102a98d3753bf69417235b655a68dc (patch)
tree351ab73db934d3b4e4babbe27e8801c659f2631b /pkg/csource
parent25f4fe0662f7f3b390d16b2e786f2ba0aa0293f1 (diff)
prog: remove default target and all global state
Now each prog function accepts the desired target explicitly. No global, implicit state involved. This is much cleaner and allows cross-OS/arch testing, etc.
Diffstat (limited to 'pkg/csource')
-rw-r--r--pkg/csource/csource.go6
-rw-r--r--pkg/csource/csource_test.go24
2 files changed, 15 insertions, 15 deletions
diff --git a/pkg/csource/csource.go b/pkg/csource/csource.go
index e1b32e752..8b7c84ad3 100644
--- a/pkg/csource/csource.go
+++ b/pkg/csource/csource.go
@@ -101,7 +101,7 @@ func Write(p *prog.Prog, opts Options) ([]byte, error) {
fmt.Fprint(w, hdr)
fmt.Fprint(w, "\n")
- calls, nvar := generateCalls(exec, opts)
+ calls, nvar := generateCalls(p.Target, exec, opts)
fmt.Fprintf(w, "long r[%v];\n", nvar)
if !opts.Repeat {
@@ -258,7 +258,7 @@ func generateTestFunc(w io.Writer, opts Options, calls []string, name string) {
}
}
-func generateCalls(exec []byte, opts Options) ([]string, int) {
+func generateCalls(target *prog.Target, exec []byte, opts Options) ([]string, int) {
read := func() uint64 {
if len(exec) < 8 {
panic("exec program overflow")
@@ -367,7 +367,7 @@ loop:
fmt.Fprintf(w, "\twrite_file(\"/sys/kernel/debug/fail_futex/ignore-private\", \"N\");\n")
fmt.Fprintf(w, "\tinject_fault(%v);\n", opts.FaultNth)
}
- meta := prog.Syscalls[instr]
+ meta := target.Syscalls[instr]
emitCall := true
if meta.CallName == "syz_test" {
emitCall = false
diff --git a/pkg/csource/csource_test.go b/pkg/csource/csource_test.go
index 941274cb9..c192566e3 100644
--- a/pkg/csource/csource_test.go
+++ b/pkg/csource/csource_test.go
@@ -17,17 +17,17 @@ import (
_ "github.com/google/syzkaller/sys"
)
-func init() {
- prog.SetDefaultTarget("linux", runtime.GOARCH)
-}
-
-func initTest(t *testing.T) (rand.Source, int) {
+func initTest(t *testing.T) (*prog.Target, rand.Source, int) {
t.Parallel()
iters := 1
seed := int64(time.Now().UnixNano())
rs := rand.NewSource(seed)
t.Logf("seed=%v", seed)
- return rs, iters
+ target, err := prog.GetTarget("linux", runtime.GOARCH)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return target, rs, iters
}
func enumerateField(opt Options, field int) []Options {
@@ -89,7 +89,7 @@ func allOptionsPermutations() []Options {
}
func TestOne(t *testing.T) {
- rs, _ := initTest(t)
+ target, rs, _ := initTest(t)
opts := Options{
Threaded: true,
Collide: true,
@@ -99,13 +99,13 @@ func TestOne(t *testing.T) {
Repro: true,
UseTmpDir: true,
}
- p := prog.GenerateAllSyzProg(rs)
+ p := target.GenerateAllSyzProg(rs)
testOne(t, p, opts)
}
func TestOptions(t *testing.T) {
- rs, _ := initTest(t)
- syzProg := prog.GenerateAllSyzProg(rs)
+ target, rs, _ := initTest(t)
+ syzProg := target.GenerateAllSyzProg(rs)
t.Logf("syz program:\n%s\n", syzProg.Serialize())
permutations := allOptionsSingle()
allPermutations := allOptionsPermutations()
@@ -119,10 +119,10 @@ func TestOptions(t *testing.T) {
}
for i, opts := range permutations {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
- rs, iters := initTest(t)
+ target, rs, iters := initTest(t)
t.Logf("opts: %+v", opts)
for i := 0; i < iters; i++ {
- p := prog.Generate(rs, 10, nil)
+ p := target.Generate(rs, 10, nil)
testOne(t, p, opts)
}
testOne(t, syzProg, opts)