1// Copyright 2018 syzkaller project authors. All rights reserved. 2// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4package prog 5 6import ( 7 "fmt" 8 "sort" 9 "strings" 10 "testing" 11) 12 13func TestIsComplexPtr(t *testing.T) { 14 target, rs, _ := initRandomTargetTest(t, "linux", "amd64") 15 iters := 10 16 if testing.Short() { 17 iters = 1 18 } 19 r := newRand(target, rs) 20 compl := make(map[string]bool) 21 for _, meta := range target.Syscalls { 22 for i := 0; i < iters; i++ { 23 s := newState(target, nil) 24 calls := r.generateParticularCall(s, meta) 25 p := &Prog{Target: target, Calls: calls} 26 for _, arg := range p.complexPtrs() { 27 compl[arg.Res.Type().String()] = true 28 } 29 } 30 } 31 var arr []string 32 for id := range compl { 33 arr = append(arr, id) 34 } 35 sort.Strings(arr) 36 t.Log("complex types:\n" + strings.Join(arr, "\n")) 37} 38 39func TestSquash(t *testing.T) { 40 target := initTargetTest(t, "test", "64") 41 // nolint: lll 42 tests := []struct { 43 prog string 44 squashed string 45 }{ 46 { 47 `foo$any0(&(0x7f0000000000)={0x11, 0x11223344, 0x2233, 0x1122334455667788, {0x1, 0x7, 0x1, 0x1, 0x1bc, 0x4}, [{0x0, @res32=0x0, 0x0, @i8=0x44, "aabb"}, {0x0, @res64=0x1, 0x0, @i32=0x11223344, "1122334455667788"}]})`, 48 `foo$any0(&(0x7f0000000000)=ANY=[@ANYBLOB="1100000044332211223300000000000088776655443322113d0079230000000000000000", @ANYRES32=0x0, @ANYBLOB="00000000000000000000000044aabb000000000000000000", @ANYRES64=0x1, @ANYBLOB="000000000000000044332211112233445566778800000000"])`, 49 }, 50 } 51 for i, test := range tests { 52 t.Run(fmt.Sprint(i), func(t *testing.T) { 53 p, err := target.Deserialize([]byte(test.prog)) 54 if err != nil { 55 t.Fatalf("failed to deserialize prog: %v", err) 56 } 57 ptrArg := p.Calls[0].Args[0].(*PointerArg) 58 if !target.isComplexPtr(ptrArg) { 59 t.Fatalf("arg is not complex") 60 } 61 if target.ArgContainsAny(ptrArg) { 62 t.Fatalf("arg is already squashed") 63 } 64 target.squashPtr(ptrArg, true) 65 if !target.ArgContainsAny(ptrArg) { 66 t.Fatalf("arg is not squashed") 67 } 68 p1 := strings.TrimSpace(string(p.Serialize())) 69 target.squashPtr(ptrArg, true) 70 p2 := strings.TrimSpace(string(p.Serialize())) 71 if p1 != p2 { 72 t.Fatalf("double squash changed program:\n%v\nvs:\n%v", p1, p2) 73 } 74 if p1 != test.squashed { 75 t.Fatalf("bad squash result:\n%v\nwant:\n%v", p1, test.squashed) 76 } 77 }) 78 } 79} 80