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