blob: f23466df6bf23171c8bcf2923b8d201d43a8bc01 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package corpus
import (
"math/rand"
"sort"
"github.com/google/syzkaller/pkg/signal"
"github.com/google/syzkaller/prog"
)
type ProgramsList struct {
progs []*prog.Prog
sumPrios int64
accPrios []int64
}
func (pl *ProgramsList) chooseProgram(r *rand.Rand) *prog.Prog {
if len(pl.progs) == 0 {
return nil
}
randVal := r.Int63n(pl.sumPrios + 1)
idx := sort.Search(len(pl.accPrios), func(i int) bool {
return pl.accPrios[i] >= randVal
})
return pl.progs[idx]
}
func (pl *ProgramsList) saveProgram(p *prog.Prog, signal signal.Signal) {
prio := int64(len(signal))
if prio == 0 {
prio = 1
}
pl.sumPrios += prio
pl.accPrios = append(pl.accPrios, pl.sumPrios)
pl.progs = append(pl.progs, p)
}
func (corpus *Corpus) ChooseProgram(r *rand.Rand) *prog.Prog {
corpus.mu.RLock()
defer corpus.mu.RUnlock()
if len(corpus.progsMap) == 0 {
return nil
}
// We could have used an approach similar to chooseProgram(), but for small number
// of focus areas that is an overkill.
var randArea *focusAreaState
if len(corpus.focusAreas) > 0 {
sum := 0.0
nonEmpty := make([]*focusAreaState, 0, len(corpus.focusAreas))
for _, area := range corpus.focusAreas {
if len(area.progs) == 0 {
continue
}
sum += area.Weight
nonEmpty = append(nonEmpty, area)
}
val := r.Float64() * sum
currSum := 0.0
for _, area := range nonEmpty {
if val >= currSum {
randArea = area
}
currSum += area.Weight
}
}
if randArea != nil {
return randArea.chooseProgram(r)
}
return corpus.chooseProgram(r)
}
func (corpus *Corpus) Programs() []*prog.Prog {
corpus.mu.RLock()
defer corpus.mu.RUnlock()
return corpus.progs
}
|