1
2
3
4
5 package fuzz
6
7 import (
8 "fmt"
9 "math/bits"
10 )
11
12
13
14 func ResetCoverage() {
15 cov := coverage()
16 for i := range cov {
17 cov[i] = 0
18 }
19 }
20
21
22
23
24
25 func SnapshotCoverage() {
26 cov := coverage()
27 for i, b := range cov {
28 b |= b >> 1
29 b |= b >> 2
30 b |= b >> 4
31 b -= b >> 1
32 coverageSnapshot[i] = b
33 }
34 }
35
36
37
38 func diffCoverage(base, snapshot []byte) []byte {
39 if len(base) != len(snapshot) {
40 panic(fmt.Sprintf("the number of coverage bits changed: before=%d, after=%d", len(base), len(snapshot)))
41 }
42 found := false
43 for i := range snapshot {
44 if snapshot[i]&^base[i] != 0 {
45 found = true
46 break
47 }
48 }
49 if !found {
50 return nil
51 }
52 diff := make([]byte, len(snapshot))
53 for i := range diff {
54 diff[i] = snapshot[i] &^ base[i]
55 }
56 return diff
57 }
58
59
60
61 func countNewCoverageBits(base, snapshot []byte) int {
62 n := 0
63 for i := range snapshot {
64 n += bits.OnesCount8(snapshot[i] &^ base[i])
65 }
66 return n
67 }
68
69
70
71 func isCoverageSubset(base, snapshot []byte) bool {
72 for i, v := range base {
73 if v&snapshot[i] != v {
74 return false
75 }
76 }
77 return true
78 }
79
80
81
82 func hasCoverageBit(base, snapshot []byte) bool {
83 for i := range snapshot {
84 if snapshot[i]&base[i] != 0 {
85 return true
86 }
87 }
88 return false
89 }
90
91 func countBits(cov []byte) int {
92 n := 0
93 for _, c := range cov {
94 n += bits.OnesCount8(c)
95 }
96 return n
97 }
98
99 var (
100 coverageEnabled = len(coverage()) > 0
101 coverageSnapshot = make([]byte, len(coverage()))
102
103
104
105
106 _counters, _ecounters [0]byte
107 )
108
View as plain text