forked from flyingmutant/rapid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshrink.go
408 lines (346 loc) · 9.37 KB
/
shrink.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright 2019 Gregory Petrosyan <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid
import (
"encoding/binary"
"fmt"
"math"
"math/bits"
"os"
"strings"
"time"
)
const (
labelLowerFloatExp = "lower_float_exp"
labelLowerFloatSignif = "lower_float_signif"
labelLowerFloatFrac = "lower_float_frac"
labelMinBlockBinSearch = "minblock_binsearch"
labelMinBlockShift = "minblock_shift"
labelMinBlockSort = "minblock_sort"
labelMinBlockTrySmall = "minblock_trysmall"
labelMinBlockUnset = "minblock_unset"
labelRemoveGroup = "remove_group"
labelRemoveGroupAndLower = "remove_group_lower"
labelRemoveGroupSpan = "remove_groupspan"
labelSortGroups = "sort_groups"
)
func shrink(tb tb, deadline time.Time, rec recordedBits, err *testError, prop func(*T)) ([]uint64, *testError) {
rec.prune()
s := &shrinker{
tb: tb,
rec: rec,
err: err,
prop: prop,
visBits: []recordedBits{rec},
tries: map[string]int{},
cache: map[string]struct{}{},
}
buf, err := s.shrink(deadline)
if flags.debugvis {
name := fmt.Sprintf("vis-%v.html", strings.Replace(tb.Name(), "/", "_", -1))
f, err := os.Create(name)
if err != nil {
tb.Logf("failed to create debugvis file %v: %v", name, err)
} else {
defer func() { _ = f.Close() }()
if err = visWriteHTML(f, tb.Name(), s.visBits); err != nil {
tb.Logf("failed to write debugvis file %v: %v", name, err)
}
}
}
return buf, err
}
type shrinker struct {
tb tb
rec recordedBits
err *testError
prop func(*T)
visBits []recordedBits
tries map[string]int
shrinks int
cache map[string]struct{}
hits int
}
func (s *shrinker) debugf(verbose_ bool, format string, args ...any) {
if flags.debug && (!verbose_ || flags.verbose) {
s.tb.Helper()
s.tb.Logf("[shrink] "+format, args...)
}
}
func (s *shrinker) shrink(deadline time.Time) (buf []uint64, err *testError) {
defer func() {
if r := recover(); r != nil {
buf, err = s.rec.data, r.(*testError)
}
}()
i := 0
for shrinks := -1; s.shrinks > shrinks && time.Now().Before(deadline); i++ {
shrinks = s.shrinks
s.debugf(false, "round %v start", i)
s.removeGroups(deadline)
s.minimizeBlocks(deadline)
if s.shrinks == shrinks {
s.debugf(false, "trying expensive algorithms for round %v", i)
s.lowerFloatHack(deadline)
s.removeGroupsAndLower(deadline)
s.sortGroups(deadline)
s.removeGroupSpans(deadline)
}
}
tries := 0
for _, n := range s.tries {
tries += n
}
s.debugf(false, "done, %v rounds total (%v tries, %v shrinks, %v cache hits):\n%v", i, tries, s.shrinks, s.hits, s.tries)
return s.rec.data, s.err
}
func (s *shrinker) removeGroups(deadline time.Time) {
for i := 0; i < len(s.rec.groups) && time.Now().Before(deadline); i++ {
g := s.rec.groups[i]
if !g.standalone || g.end < 0 {
continue
}
if s.accept(without(s.rec.data, g), labelRemoveGroup, "remove group %q at %v: [%v, %v)", g.label, i, g.begin, g.end) {
i--
}
}
}
func (s *shrinker) minimizeBlocks(deadline time.Time) {
for i := 0; i < len(s.rec.data) && time.Now().Before(deadline); i++ {
minimize(s.rec.data[i], func(u uint64, label string) bool {
buf := append([]uint64(nil), s.rec.data...)
buf[i] = u
return s.accept(buf, label, "minimize block %v: %v to %v", i, s.rec.data[i], u)
})
}
}
func (s *shrinker) lowerFloatHack(deadline time.Time) {
for i := 0; i < len(s.rec.groups) && time.Now().Before(deadline); i++ {
g := s.rec.groups[i]
if !g.standalone || g.end != g.begin+7 {
continue
}
buf := append([]uint64(nil), s.rec.data...)
buf[g.begin+3] -= 1
buf[g.begin+4] = math.MaxUint64
buf[g.begin+5] = math.MaxUint64
buf[g.begin+6] = math.MaxUint64
if !s.accept(buf, labelLowerFloatExp, "lower float exponent of group %q at %v to %v", g.label, i, buf[g.begin+3]) {
buf := append([]uint64(nil), s.rec.data...)
buf[g.begin+4] -= 1
buf[g.begin+5] = math.MaxUint64
buf[g.begin+6] = math.MaxUint64
if !s.accept(buf, labelLowerFloatSignif, "lower float significant of group %q at %v to %v", g.label, i, buf[g.begin+4]) {
buf := append([]uint64(nil), s.rec.data...)
buf[g.begin+5] -= 1
buf[g.begin+6] = math.MaxUint64
s.accept(buf, labelLowerFloatFrac, "lower float frac of group %q at %v to %v", g.label, i, buf[g.begin+5])
}
}
}
}
func (s *shrinker) removeGroupsAndLower(deadline time.Time) {
for i := 0; i < len(s.rec.data) && time.Now().Before(deadline); i++ {
if s.rec.data[i] == 0 {
continue
}
buf := append([]uint64(nil), s.rec.data...)
buf[i] -= 1
for j := 0; j < len(s.rec.groups); j++ {
g := s.rec.groups[j]
if !g.standalone || g.end < 0 || (i >= g.begin && i < g.end) {
continue
}
if s.accept(without(buf, g), labelRemoveGroupAndLower, "lower block %v to %v and remove group %q at %v: [%v, %v)", i, buf[i], g.label, j, g.begin, g.end) {
i--
break
}
}
}
}
func (s *shrinker) sortGroups(deadline time.Time) {
for i := 1; i < len(s.rec.groups) && time.Now().Before(deadline); i++ {
for j := i; j > 0; {
g := s.rec.groups[j]
if !g.standalone || g.end < 0 {
break
}
j_ := j
for j--; j >= 0; j-- {
h := s.rec.groups[j]
if !h.standalone || h.end < 0 || h.end > g.begin || h.label != g.label {
continue
}
buf := append([]uint64(nil), s.rec.data[:h.begin]...)
buf = append(buf, s.rec.data[g.begin:g.end]...)
buf = append(buf, s.rec.data[h.end:g.begin]...)
buf = append(buf, s.rec.data[h.begin:h.end]...)
buf = append(buf, s.rec.data[g.end:]...)
if s.accept(buf, labelSortGroups, "swap groups %q at %v: [%v, %v) and %q at %v: [%v, %v)", g.label, j_, g.begin, g.end, h.label, j, h.begin, h.end) {
break
}
}
}
}
}
func (s *shrinker) removeGroupSpans(deadline time.Time) {
for i := 0; i < len(s.rec.groups) && time.Now().Before(deadline); i++ {
g := s.rec.groups[i]
if !g.standalone || g.end < 0 {
continue
}
groups := []groupInfo{g}
for j := i + 1; j < len(s.rec.groups); j++ {
h := s.rec.groups[j]
if !h.standalone || h.end < 0 || h.begin < groups[len(groups)-1].end {
continue
}
groups = append(groups, h)
buf := without(s.rec.data, groups...)
if s.accept(buf, labelRemoveGroupSpan, "remove %v groups %v", len(groups), groups) {
i--
break
}
}
}
}
func (s *shrinker) accept(buf []uint64, label string, format string, args ...any) bool {
if compareData(buf, s.rec.data) >= 0 {
return false
}
bufStr := dataStr(buf)
if _, ok := s.cache[bufStr]; ok {
s.hits++
return false
}
s.debugf(true, label+": trying to reproduce the failure with a smaller test case: "+format, args...)
s.tries[label]++
s1 := newBufBitStream(buf, false)
err1 := checkOnce(newT(s.tb, s1, flags.debug && flags.verbose, nil), s.prop)
if traceback(err1) != traceback(s.err) {
s.cache[bufStr] = struct{}{}
return false
}
s.debugf(true, label+": trying to reproduce the failure")
s.tries[label]++
s.err = err1
s2 := newBufBitStream(buf, true)
err2 := checkOnce(newT(s.tb, s2, flags.debug && flags.verbose, nil), s.prop)
s.rec = s2.recordedBits
s.rec.prune()
assert(compareData(s.rec.data, buf) <= 0)
if flags.debugvis {
s.visBits = append(s.visBits, s.rec)
}
if !sameError(err1, err2) {
panic(err2)
}
s.debugf(false, label+" success: "+format, args...)
s.shrinks++
return true
}
func minimize(u uint64, cond func(uint64, string) bool) uint64 {
if u == 0 {
return 0
}
for i := uint64(0); i < u && i < small; i++ {
if cond(i, labelMinBlockTrySmall) {
return i
}
}
if u <= small {
return u
}
m := &minimizer{best: u, cond: cond}
m.rShift()
m.unsetBits()
m.sortBits()
m.binSearch()
return m.best
}
type minimizer struct {
best uint64
cond func(uint64, string) bool
}
func (m *minimizer) accept(u uint64, label string) bool {
if u >= m.best || u < small || !m.cond(u, label) {
return false
}
m.best = u
return true
}
func (m *minimizer) rShift() {
for m.accept(m.best>>1, labelMinBlockShift) {
}
}
func (m *minimizer) unsetBits() {
size := bits.Len64(m.best)
for i := size - 1; i >= 0; i-- {
m.accept(m.best^1<<uint(i), labelMinBlockUnset)
}
}
func (m *minimizer) sortBits() {
size := bits.Len64(m.best)
for i := size - 1; i >= 0; i-- {
h := uint64(1 << uint(i))
if m.best&h != 0 {
for j := 0; j < i; j++ {
l := uint64(1 << uint(j))
if m.best&l == 0 {
if m.accept(m.best^(l|h), labelMinBlockSort) {
break
}
}
}
}
}
}
func (m *minimizer) binSearch() {
if !m.accept(m.best-1, labelMinBlockBinSearch) {
return
}
i := uint64(0)
j := m.best
for i < j {
h := i + (j-i)/2
if m.accept(h, labelMinBlockBinSearch) {
j = h
} else {
i = h + 1
}
}
}
func without(data []uint64, groups ...groupInfo) []uint64 {
buf := append([]uint64(nil), data...)
for i := len(groups) - 1; i >= 0; i-- {
g := groups[i]
buf = append(buf[:g.begin], buf[g.end:]...)
}
return buf
}
func dataStr(data []uint64) string {
b := &strings.Builder{}
err := binary.Write(b, binary.LittleEndian, data)
assert(err == nil)
return b.String()
}
func compareData(a []uint64, b []uint64) int {
if len(a) < len(b) {
return -1
}
if len(a) > len(b) {
return 1
}
for i := range a {
if a[i] < b[i] {
return -1
}
if a[i] > b[i] {
return 1
}
}
return 0
}