Debian Codegraph @

simd/fuzz_test.go

raw
1 package simd
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 // Fuzz targets comparing every search function against the scalar
9 // references from the unit tests. The seeds cover the known traps (SWAR
10 // borrow adjacency, high-bit bytes on signed vector compares, vector-tail
11 // boundaries); go test runs the seeds as regular tests, go test -fuzz
12 // explores beyond them.
13
14 func FuzzMemchr2(f *testing.F) {
15 f.Add([]byte("hello world"), byte('o'), byte('w'))
16 f.Add([]byte{0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01}, byte(0), byte(2))
17 f.Add(bytes.Repeat([]byte{'x'}, 65), byte('a'), byte('x'))
18 f.Add([]byte{}, byte(0), byte(0))
19 f.Fuzz(func(t *testing.T, h []byte, a, b byte) {
20 want := refMemchr2(h, a, b)
21 if got := Memchr2(h, a, b); got != want {
22 t.Fatalf("Memchr2(%q, %#x, %#x) = %d, want %d", h, a, b, got, want)
23 }
24 if got := memchr2Generic(h, a, b); got != want {
25 t.Fatalf("memchr2Generic(%q, %#x, %#x) = %d, want %d", h, a, b, got, want)
26 }
27 })
28 }
29
30 func FuzzMemchr3(f *testing.F) {
31 f.Add([]byte("hello\tworld\nfoo"), byte(' '), byte('\t'), byte('\n'))
32 f.Add([]byte{0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01}, byte(0), byte(2), byte(3))
33 f.Add(bytes.Repeat([]byte{0xff}, 40), byte(0x80), byte(0xfe), byte(0xff))
34 f.Fuzz(func(t *testing.T, h []byte, a, b, c byte) {
35 want := refMemchr3(h, a, b, c)
36 if got := Memchr3(h, a, b, c); got != want {
37 t.Fatalf("Memchr3(%q, %#x, %#x, %#x) = %d, want %d", h, a, b, c, got, want)
38 }
39 if got := memchr3Generic(h, a, b, c); got != want {
40 t.Fatalf("memchr3Generic(%q, %#x, %#x, %#x) = %d, want %d", h, a, b, c, got, want)
41 }
42 })
43 }
44
45 func FuzzMemchrPair(f *testing.F) {
46 f.Add([]byte("hello example world"), byte('e'), byte('x'), 1)
47 f.Add([]byte{0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01}, byte(1), byte(0), 1)
48 f.Add(bytes.Repeat([]byte{'a'}, 100), byte('a'), byte('a'), 33)
49 f.Add([]byte("abc"), byte('a'), byte('b'), -1)
50 f.Fuzz(func(t *testing.T, h []byte, b1, b2 byte, offset int) {
51 if offset > len(h) {
52 offset %= len(h) + 1 // keep the search space interesting
53 }
54 want := refMemchrPair(h, b1, b2, offset)
55 if offset == 0 && b1 != b2 {
56 want = -1 // same position cannot hold two different bytes
57 }
58 if got := MemchrPair(h, b1, b2, offset); got != want {
59 t.Fatalf("MemchrPair(%q, %#x, %#x, %d) = %d, want %d", h, b1, b2, offset, got, want)
60 }
61 // Pin the SWAR fallback directly: through the exported function it
62 // is only reachable below the AVX2 dispatch length on amd64 hosts.
63 if offset >= 1 && len(h) > offset {
64 if got := memchrPairGeneric(h, b1, b2, offset); got != want {
65 t.Fatalf("memchrPairGeneric(%q, %#x, %#x, %d) = %d, want %d", h, b1, b2, offset, got, want)
66 }
67 }
68 })
69 }
70
71 func FuzzMemmem(f *testing.F) {
72 f.Add([]byte("hello world"), []byte("world"))
73 f.Add(bytes.Repeat([]byte("ab"), 100), []byte("aba"))
74 f.Add(bytes.Repeat([]byte{'a'}, 200), []byte("aab"))
75 f.Add([]byte{}, []byte{})
76 // Miss-budget handover seeds: every position is an anchor and the
77 // needle keeps mismatching, so memmemSingle (all-'z' needle) and
78 // memmemPaired ('zqe' over dense false pairs) both reach
79 // memmemFallback from the seed corpus alone — with the match absent
80 // and with it found only by the fallback.
81 f.Add(bytes.Repeat([]byte{'z'}, 200), append(bytes.Repeat([]byte{'z'}, 30), 'b'))
82 f.Add(bytes.Repeat([]byte("zqf"), 40), []byte("zqe"))
83 f.Add(append(bytes.Repeat([]byte("zqf"), 40), 'z', 'q', 'e'), []byte("zqe"))
84 f.Fuzz(func(t *testing.T, h, needle []byte) {
85 want := bytes.Index(h, needle)
86 if got := Memmem(h, needle); got != want {
87 t.Fatalf("Memmem(%q, %q) = %d, want %d", h, needle, got, want)
88 }
89 // Drive the prefilter helpers directly so they are fuzzed even
90 // where Memmem's routing (no AVX2, sub-128B haystack) would skip
91 // them; both are pure Go and must match bytes.Index everywhere.
92 if len(needle) >= 2 && len(h) >= len(needle) {
93 info := SelectRareBytes(needle)
94 if got := memmemSingle(h, needle, info.Byte1, info.Index1); got != want {
95 t.Fatalf("memmemSingle(%q, %q) = %d, want %d", h, needle, got, want)
96 }
97 if info.Byte1 != info.Byte2 {
98 if got := memmemPaired(h, needle, info); got != want {
99 t.Fatalf("memmemPaired(%q, %q) = %d, want %d", h, needle, got, want)
100 }
101 }
102 }
103 })
104 }
105
106 func FuzzIsASCII(f *testing.F) {
107 f.Add([]byte("hello world"))
108 f.Add([]byte("h\xc3\xa9llo"))
109 f.Add(bytes.Repeat([]byte{0x7f}, 64))
110 f.Add(append(bytes.Repeat([]byte{'a'}, 63), 0x80))
111 f.Fuzz(func(t *testing.T, h []byte) {
112 want := refIsASCII(h)
113 if got := IsASCII(h); got != want {
114 t.Fatalf("IsASCII(%q) = %v, want %v", h, got, want)
115 }
116 if got := isASCIIGeneric(h); got != want {
117 t.Fatalf("isASCIIGeneric(%q) = %v, want %v", h, got, want)
118 }
119 // Check the index itself, not just its sign: the AVX2 kernel
120 // locates the byte by re-extracting four per-vector masks in
121 // address order, so a transposed arm returns a wrong-but-positive
122 // index that a consistency check with IsASCII cannot see.
123 wantFirst := refFirstNonASCII(h)
124 if got := FirstNonASCII(h); got != wantFirst {
125 t.Fatalf("FirstNonASCII(%q) = %d, want %d", h, got, wantFirst)
126 }
127 if got := firstNonASCIIGeneric(h); got != wantFirst {
128 t.Fatalf("firstNonASCIIGeneric(%q) = %d, want %d", h, got, wantFirst)
129 }
130 wantCount := refCountNonASCII(h)
131 if got := CountNonASCII(h); got != wantCount {
132 t.Fatalf("CountNonASCII(%q) = %d, want %d", h, got, wantCount)
133 }
134 if got := countNonASCIIGeneric(h); got != wantCount {
135 t.Fatalf("countNonASCIIGeneric(%q) = %d, want %d", h, got, wantCount)
136 }
137 })
138 }
139
140 // FuzzMemchrClass covers the \w classifier, which the AVX2 kernels compute
141 // from a hand-built pair of VPSHUFB nibble tables and the fallbacks from
142 // SWAR range masks — two independent derivations of one character class,
143 // so a table nibble that is wrong for some byte value shows up here.
144 func FuzzMemchrClass(f *testing.F) {
145 f.Add([]byte("hello world"))
146 f.Add([]byte("________"))
147 f.Add([]byte("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~ "))
148 f.Add(append(bytes.Repeat([]byte{'a'}, 63), '.'))
149 f.Add(append(bytes.Repeat([]byte{'.'}, 129), '_'))
150 f.Add([]byte{0x2f, 0x30, 0x39, 0x3a, 0x40, 0x41, 0x5a, 0x5b, 0x5e, 0x5f, 0x60, 0x7a, 0x7b, 0x7f, 0x80, 0xff})
151 f.Fuzz(func(t *testing.T, h []byte) {
152 wantWord, wantNot, wantDigit := -1, -1, -1
153 for i, b := range h {
154 if wantWord < 0 && refIsWord(b) {
155 wantWord = i
156 }
157 if wantNot < 0 && !refIsWord(b) {
158 wantNot = i
159 }
160 if wantDigit < 0 && b >= '0' && b <= '9' {
161 wantDigit = i
162 }
163 }
164 if got := MemchrWord(h); got != wantWord {
165 t.Fatalf("MemchrWord(%q) = %d, want %d", h, got, wantWord)
166 }
167 if got := memchrWordGeneric(h); got != wantWord {
168 t.Fatalf("memchrWordGeneric(%q) = %d, want %d", h, got, wantWord)
169 }
170 if got := MemchrNotWord(h); got != wantNot {
171 t.Fatalf("MemchrNotWord(%q) = %d, want %d", h, got, wantNot)
172 }
173 if got := memchrNotWordGeneric(h); got != wantNot {
174 t.Fatalf("memchrNotWordGeneric(%q) = %d, want %d", h, got, wantNot)
175 }
176 if got := MemchrDigit(h); got != wantDigit {
177 t.Fatalf("MemchrDigit(%q) = %d, want %d", h, got, wantDigit)
178 }
179 if got := memchrDigitGeneric(h); got != wantDigit {
180 t.Fatalf("memchrDigitGeneric(%q) = %d, want %d", h, got, wantDigit)
181 }
182 })
183 }