-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfail.go
371 lines (341 loc) · 8.25 KB
/
fail.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
// Copyright 2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/sha1"
"fmt"
"io"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
var names []string = []string{
"preformulate",
"tetracyn",
"exptl",
"extemporaneity",
"presignalled",
"licenced",
"pyelographic",
"riksmaal",
"luminesce",
"megawatt",
"boeotus",
"corporate",
"saltine",
"arsenide",
"umbrellalike",
"ecotonal",
"cocoyam",
"venetianed",
"hiordis",
"osteoma",
"unshackle",
"importability",
"petrarchan",
"elytron",
"karbala",
"haleakala",
"unflirtatious",
"emanuel",
"catholicalness",
"overawe",
"pokable",
"bacteroides",
"amplifier",
"paraphysate",
"outseen",
"wawa",
"karoline",
"excipule",
"introductoriness",
"grosgrained",
"houdon",
"interlocular",
"toniest",
"frozenly",
"asexually",
"ossification",
"earthshine",
"untransmuted",
"karaism",
"bond",
"bituminize",
"calycate",
"codon",
"sialkot",
"ctesiphon",
"griskin",
"shiftily",
"interdebate",
"thistly",
"effigiated",
"misinference",
"collinsville",
"repatriate",
"duplicatus",
"nonordination",
"geminated",
"cauliflorous",
"septembrist",
"assertional",
"incumber",
"pedagogical",
"sigher",
"technicolor",
"impugner",
"anomalousness",
"perhydrogenizing",
"periastral",
"lanchow",
"machineless",
"djinny",
"ruga",
"cerebroid",
"genip",
"environs",
"muticate",
"adamic",
"indivisibility",
"crissa",
"conjunctive",
"nonsculptured",
"keble",
"subverter",
"gelignite",
"stilettoed",
"gratulated",
"guanase",
"proselytise",
"orthrus",
"excursionary",
"ellipsoidal",
"ant",
"bat",
"cat",
"dog",
"emu",
"fox",
"gnu",
"hen",
}
type hashAndMask struct {
// a hash h matches if (h^hash)&mask == 0
hash uint64
mask uint64
name string // base name, or base name + "0", "1", etc.
}
type writeSyncer interface {
io.Writer
Sync() error
}
type HashDebug struct {
name string // base name of the flag/variable.
matches []hashAndMask // A hash matches if one of these matches.
excludes []hashAndMask // explicitly excluded hash suffixes
logfile writeSyncer
yes, no bool
}
func toHashAndMask(s, varname string) hashAndMask {
l := len(s)
if l > 64 {
s = s[l-64:]
l = 64
}
m := ^(^uint64(0) << l)
h, err := strconv.ParseUint(s, 2, 64)
if err != nil {
panic(fmt.Errorf("Could not parse %s (=%s) as a binary number", varname, s))
}
return hashAndMask{name: varname, hash: h, mask: m}
}
// NewHashDebug returns a new hash-debug tester for the
// environment variable ev. If ev is not set, it returns
// nil, allowing a lightweight check for normal-case behavior.
func NewHashDebug(ev, s string) *HashDebug {
fmt.Printf("NewHashDebug(%s,%s)\n", ev, s)
if s == "" {
return nil
}
hd := &HashDebug{name: ev}
switch s[0] {
case 'y', 'Y':
hd.yes = true
return hd
case 'n', 'N':
hd.no = true
return hd
}
var ss []string
var sc string // current
for _, c := range s {
switch c {
// Ignore leading 'v' from bisect also.
case '/', '+', ',', ' ', '\t', '-', 'v':
if len(sc) > 0 {
ss = append(ss, sc)
sc = ""
}
default:
sc += string(c)
continue
}
if c == '-' {
sc = "-"
}
}
if len(sc) > 0 {
ss = append(ss, sc)
sc = ""
}
// hash searches may use additional EVs with 0, 1, 2, ... suffixes.
i := 0
for _, s := range ss {
if s == "" {
if i != 0 || len(ss) > 1 && ss[1] != "" || len(ss) > 2 {
panic(fmt.Errorf("Empty hash match string for %s should be first (and only) one", ev))
}
// Special case of should match everything.
hd.matches = append(hd.matches, toHashAndMask("0", fmt.Sprintf("%s0", ev)))
hd.matches = append(hd.matches, toHashAndMask("1", fmt.Sprintf("%s1", ev)))
break
}
if s[0] == '-' {
hd.excludes = append(hd.excludes, toHashAndMask(s[1:], fmt.Sprintf("%s%d", "HASH_EXCLUDE", i)))
} else {
if i == 0 {
hd.matches = append(hd.matches, toHashAndMask(s, fmt.Sprintf("%s", ev)))
} else {
hd.matches = append(hd.matches, toHashAndMask(s, fmt.Sprintf("%s%d", ev, i-1)))
}
i++
}
}
return hd
}
func hashOf(pkgAndName string, param uint64) uint64 {
hbytes := sha1.Sum([]byte(pkgAndName))
hash := uint64(hbytes[7])<<56 + uint64(hbytes[6])<<48 +
uint64(hbytes[5])<<40 + uint64(hbytes[4])<<32 +
uint64(hbytes[3])<<24 + uint64(hbytes[2])<<16 +
uint64(hbytes[1])<<8 + uint64(hbytes[0])
if param != 0 {
// Because param is probably a line number, probably near zero,
// hash it up a little bit, but even so only the lower-order bits
// likely matter because search focuses on those.
p0 := param + uint64(hbytes[9]) + uint64(hbytes[10])<<8 +
uint64(hbytes[11])<<16 + uint64(hbytes[12])<<24
p1 := param + uint64(hbytes[13]) + uint64(hbytes[14])<<8 +
uint64(hbytes[15])<<16 + uint64(hbytes[16])<<24
param += p0 * p1
param ^= param>>17 ^ param<<47
}
return hash ^ param
}
// DebugHashMatch returns true if either the variable used to create d is
// unset, or if its value is y, or if it is a suffix of the base-two
// representation of the hash of pkgAndName. If the variable is not nil,
// then a true result is accompanied by stylized output to d.logfile, which
// is used for automated bug search.
func (d *HashDebug) DebugHashMatch(pkgAndName string) bool {
return d.DebugHashMatchParam(pkgAndName, 0)
}
// DebugHashMatchParam returns true if either the variable used to create d is
// unset, or if its value is y, or if it is a suffix of the base-two
// representation of the hash of pkgAndName and param. If the variable is not
// nil, then a true result is accompanied by stylized output to d.logfile,
// which is used for automated bug search.
func (d *HashDebug) DebugHashMatchParam(pkgAndName string, param uint64) bool {
if d == nil {
return true
}
if d.no {
return false
}
hash := hashOf(pkgAndName, param)
for _, m := range d.excludes {
if (m.hash^hash)&m.mask == 0 {
return false
}
}
if len(d.matches) == 0 || d.yes {
xstr := fmt.Sprintf("0x%x", hash)
d.logDebugHashMatch(d.name, pkgAndName, xstr, param)
return true
}
for _, m := range d.matches {
if (m.hash^hash)&m.mask == 0 {
xstr := fmt.Sprintf("0x%x", hash)
d.logDebugHashMatch(m.name, pkgAndName, xstr, param)
return true
}
}
return false
}
func (d *HashDebug) logDebugHashMatch(varname, name, hstr string, param uint64) {
file := d.logfile
if file == nil {
if tmpfile := os.Getenv("GSHS_LOGFILE"); tmpfile != "" {
var err error
file, err = os.OpenFile(tmpfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(fmt.Errorf("could not open hash-testing logfile %s", tmpfile))
}
}
if file == nil {
file = os.Stdout
}
d.logfile = file
}
if len(hstr) > 32 {
hstr = hstr[len(hstr)-32:]
}
// External tools depend on this string
if param == 0 {
// loopvarhash1 triggered ./a/a.go:11:6 001001011000010011100011
if !bisectSyntax {
fmt.Fprintf(file, "%s triggered %s %s\n", varname, name, hstr)
}
// ./a/a.go:11:6 [bisect-match 0x800ddd09be2584e3]
fmt.Fprintf(file, "%s [bisect-match %s]\n", name, hstr)
} else {
if !bisectSyntax {
fmt.Fprintf(file, "%s triggered %s:%d %s\n", varname, name, param, hstr)
}
fmt.Fprintf(file, "%s:%d [bisect-match %s]\n", name, param, hstr)
}
}
var doit = newDoit
var hd *HashDebug
func newDoit(name string, param int) bool {
return hd.DebugHashMatchParam(name, uint64(param))
}
// test fails when "doit" is true for 4 or more 3-letter names.
// this simulates multiple triggers required for failure.
func test() {
gcd := os.Getenv("GOCOMPILEDEBUG")
li := strings.LastIndex(gcd, "=")
hd = NewHashDebug(hash_ev_name, gcd[li+1:])
rand.Seed(time.Now().UnixNano())
threeletters := 0
for i, w := range names {
if doit(w, i) && len(w) == 3 {
threeletters++
}
}
time.Sleep(50 * time.Millisecond)
if threeletters >= 4 {
fmt.Println("FAIL!")
os.Exit(1)
}
}