github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/corpus/prio_test.go (about)

     1  // Copyright 2024 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 corpus
     5  
     6  import (
     7  	"context"
     8  	"math"
     9  	"math/rand"
    10  	"testing"
    11  
    12  	"github.com/google/syzkaller/prog"
    13  	"github.com/google/syzkaller/sys/targets"
    14  )
    15  
    16  func TestChooseProgram(t *testing.T) {
    17  	rs := rand.NewSource(0)
    18  	r := rand.New(rs)
    19  	target := getTarget(t, targets.TestOS, targets.TestArch64)
    20  	corpus := NewCorpus(context.Background())
    21  
    22  	const (
    23  		maxIters   = 1000
    24  		sizeCorpus = 1000
    25  		eps        = 0.01
    26  	)
    27  
    28  	priorities := make(map[*prog.Prog]int64)
    29  	for i := 0; i < sizeCorpus; i++ {
    30  		sizeSig := i + 1
    31  		if sizeSig%250 == 0 {
    32  			sizeSig = 0
    33  		}
    34  		inp := generateInput(target, rs, 10, sizeSig)
    35  		corpus.Save(inp)
    36  		priorities[inp.Prog] = int64(len(inp.Signal))
    37  	}
    38  	counters := make(map[*prog.Prog]int)
    39  	for it := 0; it < maxIters; it++ {
    40  		counters[corpus.ChooseProgram(r)]++
    41  	}
    42  	for p, prio := range priorities {
    43  		prob := float64(prio) / float64(corpus.sumPrios)
    44  		diff := math.Abs(prob*maxIters - float64(counters[p]))
    45  		if diff > eps*maxIters {
    46  			t.Fatalf("the difference (%f) is higher than %f%%", diff, eps*100)
    47  		}
    48  	}
    49  }