File size: 13,378 Bytes
e36aeda | 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | // Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package base
import (
"bytes"
"cmd/internal/obj"
"cmd/internal/src"
"fmt"
"internal/bisect"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
)
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 HashDebug struct {
mu sync.Mutex // for logfile, posTmp, bytesTmp
name string // base name of the flag/variable.
// what file (if any) receives the yes/no logging?
// default is os.Stdout
logfile io.Writer
posTmp []src.Pos
bytesTmp bytes.Buffer
matches []hashAndMask // A hash matches if one of these matches.
excludes []hashAndMask // explicitly excluded hash suffixes
bisect *bisect.Matcher
fileSuffixOnly bool // for Pos hashes, remove the directory prefix.
inlineSuffixOnly bool // for Pos hashes, remove all but the most inline position.
}
// SetInlineSuffixOnly controls whether hashing and reporting use the entire
// inline position, or just the most-inline suffix. Compiler debugging tends
// to want the whole inlining, debugging user problems (loopvarhash, e.g.)
// typically does not need to see the entire inline tree, there is just one
// copy of the source code.
func (d *HashDebug) SetInlineSuffixOnly(b bool) *HashDebug {
d.inlineSuffixOnly = b
return d
}
// The default compiler-debugging HashDebug, for "-d=gossahash=..."
var hashDebug *HashDebug
var ConvertHash *HashDebug // for debugging float-to-[u]int conversion changes
var FmaHash *HashDebug // for debugging fused-multiply-add floating point changes
var LoopVarHash *HashDebug // for debugging shared/private loop variable changes
var PGOHash *HashDebug // for debugging PGO optimization decisions
var LiteralAllocHash *HashDebug // for debugging literal allocation optimizations
var MergeLocalsHash *HashDebug // for debugging local stack slot merging changes
var VariableMakeHash *HashDebug // for debugging variable-sized make optimizations
// DebugHashMatchPkgFunc reports whether debug variable Gossahash
//
// 1. is empty (returns true; this is a special more-quickly implemented case of 4 below)
//
// 2. is "y" or "Y" (returns true)
//
// 3. is "n" or "N" (returns false)
//
// 4. does not explicitly exclude the sha1 hash of pkgAndName (see step 6)
//
// 5. is a suffix of the sha1 hash of pkgAndName (returns true)
//
// 6. OR
// if the (non-empty) value is in the regular language
// "(-[01]+/)+?([01]+(/[01]+)+?"
// (exclude..)(....include...)
// test the [01]+ exclude substrings, if any suffix-match, return false (4 above)
// test the [01]+ include substrings, if any suffix-match, return true
// The include substrings AFTER the first slash are numbered 0,1, etc and
// are named fmt.Sprintf("%s%d", varname, number)
// As an extra-special case for multiple failure search,
// an excludes-only string ending in a slash (terminated, not separated)
// implicitly specifies the include string "0/1", that is, match everything.
// (Exclude strings are used for automated search for multiple failures.)
// Clause 6 is not really intended for human use and only
// matters for failures that require multiple triggers.
//
// Otherwise it returns false.
//
// Unless Flags.Gossahash is empty, when DebugHashMatchPkgFunc returns true the message
//
// "%s triggered %s\n", varname, pkgAndName
//
// is printed on the file named in environment variable GSHS_LOGFILE,
// or standard out if that is empty. "Varname" is either the name of
// the variable or the name of the substring, depending on which matched.
//
// Typical use:
//
// 1. you make a change to the compiler, say, adding a new phase
//
// 2. it is broken in some mystifying way, for example, make.bash builds a broken
// compiler that almost works, but crashes compiling a test in run.bash.
//
// 3. add this guard to the code, which by default leaves it broken, but does not
// run the broken new code if Flags.Gossahash is non-empty and non-matching:
//
// if !base.DebugHashMatch(ir.PkgFuncName(fn)) {
// return nil // early exit, do nothing
// }
//
// 4. rebuild w/o the bad code,
// GOCOMPILEDEBUG=gossahash=n ./all.bash
// to verify that you put the guard in the right place with the right sense of the test.
//
// 5. use github.com/dr2chase/gossahash to search for the error:
//
// go install github.com/dr2chase/gossahash@latest
//
// gossahash -- <the thing that fails>
//
// for example: GOMAXPROCS=1 gossahash -- ./all.bash
//
// 6. gossahash should return a single function whose miscompilation
// causes the problem, and you can focus on that.
func DebugHashMatchPkgFunc(pkg, fn string) bool {
return hashDebug.MatchPkgFunc(pkg, fn, nil)
}
func DebugHashMatchPos(pos src.XPos) bool {
return hashDebug.MatchPos(pos, nil)
}
// HasDebugHash returns true if Flags.Gossahash is non-empty, which
// results in hashDebug being not-nil. I.e., if !HasDebugHash(),
// there is no need to create the string for hashing and testing.
func HasDebugHash() bool {
return hashDebug != nil
}
// TODO: Delete when we switch to bisect-only.
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 {
Fatalf("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, file io.Writer) *HashDebug {
if s == "" {
return nil
}
hd := &HashDebug{name: ev, logfile: file}
if !strings.Contains(s, "/") {
m, err := bisect.New(s)
if err != nil {
Fatalf("%s: %v", ev, err)
}
hd.bisect = m
return hd
}
// TODO: Delete remainder of function when we switch to bisect-only.
ss := strings.Split(s, "/")
// first remove any leading exclusions; these are preceded with "-"
i := 0
for len(ss) > 0 {
s := ss[0]
if len(s) == 0 || len(s) > 0 && s[0] != '-' {
break
}
ss = ss[1:]
hd.excludes = append(hd.excludes, toHashAndMask(s[1:], fmt.Sprintf("%s%d", "HASH_EXCLUDE", i)))
i++
}
// 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 {
Fatalf("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 i == 0 {
hd.matches = append(hd.matches, toHashAndMask(s, ev))
} else {
hd.matches = append(hd.matches, toHashAndMask(s, fmt.Sprintf("%s%d", ev, i-1)))
}
i++
}
return hd
}
// TODO: Delete when we switch to bisect-only.
func (d *HashDebug) excluded(hash uint64) bool {
for _, m := range d.excludes {
if (m.hash^hash)&m.mask == 0 {
return true
}
}
return false
}
// TODO: Delete when we switch to bisect-only.
func hashString(hash uint64) string {
hstr := ""
if hash == 0 {
hstr = "0"
} else {
for ; hash != 0; hash = hash >> 1 {
hstr = string('0'+byte(hash&1)) + hstr
}
}
if len(hstr) > 24 {
hstr = hstr[len(hstr)-24:]
}
return hstr
}
// TODO: Delete when we switch to bisect-only.
func (d *HashDebug) match(hash uint64) *hashAndMask {
for i, m := range d.matches {
if (m.hash^hash)&m.mask == 0 {
return &d.matches[i]
}
}
return nil
}
// MatchPkgFunc 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 pkg and fn. 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) MatchPkgFunc(pkg, fn string, note func() string) bool {
if d == nil {
return true
}
// Written this way to make inlining likely.
return d.matchPkgFunc(pkg, fn, note)
}
func (d *HashDebug) matchPkgFunc(pkg, fn string, note func() string) bool {
hash := bisect.Hash(pkg, fn)
return d.matchAndLog(hash, func() string { return pkg + "." + fn }, note)
}
// MatchPos is similar to MatchPkgFunc, but for hash computation
// it uses the source position including all inlining information instead of
// package name and path.
// Note that the default answer for no environment variable (d == nil)
// is "yes", do the thing.
func (d *HashDebug) MatchPos(pos src.XPos, desc func() string) bool {
if d == nil {
return true
}
// Written this way to make inlining likely.
return d.matchPos(Ctxt, pos, desc)
}
func (d *HashDebug) matchPos(ctxt *obj.Link, pos src.XPos, note func() string) bool {
return d.matchPosWithInfo(ctxt, pos, nil, note)
}
func (d *HashDebug) matchPosWithInfo(ctxt *obj.Link, pos src.XPos, info any, note func() string) bool {
hash := d.hashPos(ctxt, pos)
if info != nil {
hash = bisect.Hash(hash, info)
}
return d.matchAndLog(hash,
func() string {
r := d.fmtPos(ctxt, pos)
if info != nil {
r += fmt.Sprintf(" (%v)", info)
}
return r
},
note)
}
// MatchPosWithInfo is similar to MatchPos, but with additional information
// that is included for hash computation, so it can distinguish multiple
// matches on the same source location.
// Note that the default answer for no environment variable (d == nil)
// is "yes", do the thing.
func (d *HashDebug) MatchPosWithInfo(pos src.XPos, info any, desc func() string) bool {
if d == nil {
return true
}
// Written this way to make inlining likely.
return d.matchPosWithInfo(Ctxt, pos, info, desc)
}
// matchAndLog is the core matcher. It reports whether the hash matches the pattern.
// If a report needs to be printed, match prints that report to the log file.
// The text func must be non-nil and should return a user-readable
// representation of what was hashed. The note func may be nil; if non-nil,
// it should return additional information to display to the user when this
// change is selected.
func (d *HashDebug) matchAndLog(hash uint64, text, note func() string) bool {
if d.bisect != nil {
enabled := d.bisect.ShouldEnable(hash)
if d.bisect.ShouldPrint(hash) {
disabled := ""
if !enabled {
disabled = " [DISABLED]"
}
var t string
if !d.bisect.MarkerOnly() {
t = text()
if note != nil {
if n := note(); n != "" {
t += ": " + n + disabled
disabled = ""
}
}
}
d.log(d.name, hash, strings.TrimSpace(t+disabled))
}
return enabled
}
// TODO: Delete rest of function body when we switch to bisect-only.
if d.excluded(hash) {
return false
}
if m := d.match(hash); m != nil {
d.log(m.name, hash, text())
return true
}
return false
}
// short returns the form of file name to use for d.
// The default is the full path, but fileSuffixOnly selects
// just the final path element.
func (d *HashDebug) short(name string) string {
if d.fileSuffixOnly {
return filepath.Base(name)
}
return name
}
// hashPos returns a hash of the position pos, including its entire inline stack.
// If d.inlineSuffixOnly is true, hashPos only considers the innermost (leaf) position on the inline stack.
func (d *HashDebug) hashPos(ctxt *obj.Link, pos src.XPos) uint64 {
if d.inlineSuffixOnly {
p := ctxt.InnermostPos(pos)
return bisect.Hash(d.short(p.Filename()), p.Line(), p.Col())
}
h := bisect.Hash()
ctxt.AllPos(pos, func(p src.Pos) {
h = bisect.Hash(h, d.short(p.Filename()), p.Line(), p.Col())
})
return h
}
// fmtPos returns a textual formatting of the position pos, including its entire inline stack.
// If d.inlineSuffixOnly is true, fmtPos only considers the innermost (leaf) position on the inline stack.
func (d *HashDebug) fmtPos(ctxt *obj.Link, pos src.XPos) string {
format := func(p src.Pos) string {
return fmt.Sprintf("%s:%d:%d", d.short(p.Filename()), p.Line(), p.Col())
}
if d.inlineSuffixOnly {
return format(ctxt.InnermostPos(pos))
}
var stk []string
ctxt.AllPos(pos, func(p src.Pos) {
stk = append(stk, format(p))
})
return strings.Join(stk, "; ")
}
// log prints a match with the given hash and textual formatting.
// TODO: Delete varname parameter when we switch to bisect-only.
func (d *HashDebug) log(varname string, hash uint64, text string) {
d.mu.Lock()
defer d.mu.Unlock()
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 {
Fatalf("could not open hash-testing logfile %s", tmpfile)
return
}
}
if file == nil {
file = os.Stdout
}
d.logfile = file
}
// Bisect output.
fmt.Fprintf(file, "%s %s\n", text, bisect.Marker(hash))
// Gossahash output.
// TODO: Delete rest of function when we switch to bisect-only.
fmt.Fprintf(file, "%s triggered %s %s\n", varname, text, hashString(hash))
}
|