github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/syz-verifier/utils_test.go (about)

     1  // Copyright 2021 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  
     4  package main
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/google/syzkaller/pkg/ipc"
    10  	"github.com/google/syzkaller/pkg/osutil"
    11  	"github.com/google/syzkaller/prog"
    12  )
    13  
    14  func createTestVerifier(t *testing.T) *Verifier {
    15  	target, err := prog.GetTarget("test", "64")
    16  	if err != nil {
    17  		t.Fatalf("failed to initialise test target: %v", err)
    18  	}
    19  	vrf := &Verifier{
    20  		target:      target,
    21  		choiceTable: target.DefaultChoiceTable(),
    22  		progIdx:     3,
    23  		reruns:      1,
    24  	}
    25  	vrf.resultsdir = makeTestResultDirectory(t)
    26  	vrf.stats = emptyTestStats()
    27  	vrf.srv, err = startRPCServer(vrf)
    28  	if err != nil {
    29  		t.Fatalf("failed to initialise RPC server: %v", err)
    30  	}
    31  	return vrf
    32  }
    33  
    34  func getTestProgram(t *testing.T) *prog.Prog {
    35  	p := "breaks_returns()\n" +
    36  		"minimize$0(0x1, 0x1)\n" +
    37  		"test$res0()\n"
    38  	target := prog.InitTargetTest(t, "test", "64")
    39  	prog, err := target.Deserialize([]byte(p), prog.Strict)
    40  	if err != nil {
    41  		t.Fatalf("failed to deserialise test program: %v", err)
    42  	}
    43  	return prog
    44  }
    45  
    46  func makeTestResultDirectory(t *testing.T) string {
    47  	return osutil.Abs(t.TempDir())
    48  }
    49  
    50  func makeExecResult(pool int, errnos []int, flags ...int) *ExecResult {
    51  	r := &ExecResult{Pool: pool, Info: ipc.ProgInfo{Calls: []ipc.CallInfo{}}}
    52  	for _, e := range errnos {
    53  		r.Info.Calls = append(r.Info.Calls, ipc.CallInfo{Errno: e})
    54  	}
    55  
    56  	for idx, f := range flags {
    57  		r.Info.Calls[idx].Flags = ipc.CallFlags(f)
    58  	}
    59  	return r
    60  }
    61  
    62  func makeExecResultCrashed(pool int) *ExecResult {
    63  	return &ExecResult{Pool: pool, Crashed: true}
    64  }
    65  
    66  func emptyTestStats() *Stats {
    67  	return (&Stats{
    68  		Calls: StatMapStringToCallStats{
    69  			mapStringToCallStats: mapStringToCallStats{
    70  				"breaks_returns": {Name: "breaks_returns", States: map[ReturnState]bool{}},
    71  				"minimize$0":     {Name: "minimize$0", States: map[ReturnState]bool{}},
    72  				"test$res0":      {Name: "test$res0", States: map[ReturnState]bool{}},
    73  			},
    74  		},
    75  	}).Init()
    76  }
    77  
    78  func makeCallStats(name string, occurrences, mismatches uint64, states map[ReturnState]bool) *CallStats {
    79  	return &CallStats{Name: name,
    80  		Occurrences: occurrences,
    81  		Mismatches:  mismatches,
    82  		States:      states}
    83  }
    84  
    85  func returnState(errno int, flags ...int) ReturnState {
    86  	rs := ReturnState{Errno: errno}
    87  	if flags != nil {
    88  		rs.Flags = ipc.CallFlags(flags[0])
    89  	}
    90  	return rs
    91  }
    92  
    93  func crashedReturnState() ReturnState {
    94  	return ReturnState{Crashed: true}
    95  }