Source file src/cmd/compile/internal/base/hashdebug.go

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package base
     6  
     7  import (
     8  	"bytes"
     9  	"cmd/internal/obj"
    10  	"cmd/internal/src"
    11  	"fmt"
    12  	"internal/bisect"
    13  	"io"
    14  	"os"
    15  	"path/filepath"
    16  	"strconv"
    17  	"strings"
    18  	"sync"
    19  )
    20  
    21  type hashAndMask struct {
    22  	// a hash h matches if (h^hash)&mask == 0
    23  	hash uint64
    24  	mask uint64
    25  	name string // base name, or base name + "0", "1", etc.
    26  }
    27  
    28  type HashDebug struct {
    29  	mu   sync.Mutex // for logfile, posTmp, bytesTmp
    30  	name string     // base name of the flag/variable.
    31  	// what file (if any) receives the yes/no logging?
    32  	// default is os.Stdout
    33  	logfile          io.Writer
    34  	posTmp           []src.Pos
    35  	bytesTmp         bytes.Buffer
    36  	matches          []hashAndMask // A hash matches if one of these matches.
    37  	excludes         []hashAndMask // explicitly excluded hash suffixes
    38  	bisect           *bisect.Matcher
    39  	fileSuffixOnly   bool // for Pos hashes, remove the directory prefix.
    40  	inlineSuffixOnly bool // for Pos hashes, remove all but the most inline position.
    41  }
    42  
    43  // SetInlineSuffixOnly controls whether hashing and reporting use the entire
    44  // inline position, or just the most-inline suffix.  Compiler debugging tends
    45  // to want the whole inlining, debugging user problems (loopvarhash, e.g.)
    46  // typically does not need to see the entire inline tree, there is just one
    47  // copy of the source code.
    48  func (d *HashDebug) SetInlineSuffixOnly(b bool) *HashDebug {
    49  	d.inlineSuffixOnly = b
    50  	return d
    51  }
    52  
    53  // The default compiler-debugging HashDebug, for "-d=gossahash=..."
    54  var hashDebug *HashDebug
    55  
    56  var FmaHash *HashDebug          // for debugging fused-multiply-add floating point changes
    57  var LoopVarHash *HashDebug      // for debugging shared/private loop variable changes
    58  var PGOHash *HashDebug          // for debugging PGO optimization decisions
    59  var MergeLocalsHash *HashDebug  // for debugging local stack slot merging changes
    60  var VariableMakeHash *HashDebug // for debugging variable-sized make optimizations
    61  
    62  // DebugHashMatchPkgFunc reports whether debug variable Gossahash
    63  //
    64  //  1. is empty (returns true; this is a special more-quickly implemented case of 4 below)
    65  //
    66  //  2. is "y" or "Y" (returns true)
    67  //
    68  //  3. is "n" or "N" (returns false)
    69  //
    70  //  4. does not explicitly exclude the sha1 hash of pkgAndName (see step 6)
    71  //
    72  //  5. is a suffix of the sha1 hash of pkgAndName (returns true)
    73  //
    74  //  6. OR
    75  //     if the (non-empty) value is in the regular language
    76  //     "(-[01]+/)+?([01]+(/[01]+)+?"
    77  //     (exclude..)(....include...)
    78  //     test the [01]+ exclude substrings, if any suffix-match, return false (4 above)
    79  //     test the [01]+ include substrings, if any suffix-match, return true
    80  //     The include substrings AFTER the first slash are numbered 0,1, etc and
    81  //     are named fmt.Sprintf("%s%d", varname, number)
    82  //     As an extra-special case for multiple failure search,
    83  //     an excludes-only string ending in a slash (terminated, not separated)
    84  //     implicitly specifies the include string "0/1", that is, match everything.
    85  //     (Exclude strings are used for automated search for multiple failures.)
    86  //     Clause 6 is not really intended for human use and only
    87  //     matters for failures that require multiple triggers.
    88  //
    89  // Otherwise it returns false.
    90  //
    91  // Unless Flags.Gossahash is empty, when DebugHashMatchPkgFunc returns true the message
    92  //
    93  //	"%s triggered %s\n", varname, pkgAndName
    94  //
    95  // is printed on the file named in environment variable GSHS_LOGFILE,
    96  // or standard out if that is empty.  "Varname" is either the name of
    97  // the variable or the name of the substring, depending on which matched.
    98  //
    99  // Typical use:
   100  //
   101  //  1. you make a change to the compiler, say, adding a new phase
   102  //
   103  //  2. it is broken in some mystifying way, for example, make.bash builds a broken
   104  //     compiler that almost works, but crashes compiling a test in run.bash.
   105  //
   106  //  3. add this guard to the code, which by default leaves it broken, but does not
   107  //     run the broken new code if Flags.Gossahash is non-empty and non-matching:
   108  //
   109  //     if !base.DebugHashMatch(ir.PkgFuncName(fn)) {
   110  //     return nil // early exit, do nothing
   111  //     }
   112  //
   113  //  4. rebuild w/o the bad code,
   114  //     GOCOMPILEDEBUG=gossahash=n ./all.bash
   115  //     to verify that you put the guard in the right place with the right sense of the test.
   116  //
   117  //  5. use github.com/dr2chase/gossahash to search for the error:
   118  //
   119  //     go install github.com/dr2chase/gossahash@latest
   120  //
   121  //     gossahash -- <the thing that fails>
   122  //
   123  //     for example: GOMAXPROCS=1 gossahash -- ./all.bash
   124  //
   125  //  6. gossahash should return a single function whose miscompilation
   126  //     causes the problem, and you can focus on that.
   127  func DebugHashMatchPkgFunc(pkg, fn string) bool {
   128  	return hashDebug.MatchPkgFunc(pkg, fn, nil)
   129  }
   130  
   131  func DebugHashMatchPos(pos src.XPos) bool {
   132  	return hashDebug.MatchPos(pos, nil)
   133  }
   134  
   135  // HasDebugHash returns true if Flags.Gossahash is non-empty, which
   136  // results in hashDebug being not-nil.  I.e., if !HasDebugHash(),
   137  // there is no need to create the string for hashing and testing.
   138  func HasDebugHash() bool {
   139  	return hashDebug != nil
   140  }
   141  
   142  // TODO: Delete when we switch to bisect-only.
   143  func toHashAndMask(s, varname string) hashAndMask {
   144  	l := len(s)
   145  	if l > 64 {
   146  		s = s[l-64:]
   147  		l = 64
   148  	}
   149  	m := ^(^uint64(0) << l)
   150  	h, err := strconv.ParseUint(s, 2, 64)
   151  	if err != nil {
   152  		Fatalf("Could not parse %s (=%s) as a binary number", varname, s)
   153  	}
   154  
   155  	return hashAndMask{name: varname, hash: h, mask: m}
   156  }
   157  
   158  // NewHashDebug returns a new hash-debug tester for the
   159  // environment variable ev.  If ev is not set, it returns
   160  // nil, allowing a lightweight check for normal-case behavior.
   161  func NewHashDebug(ev, s string, file io.Writer) *HashDebug {
   162  	if s == "" {
   163  		return nil
   164  	}
   165  
   166  	hd := &HashDebug{name: ev, logfile: file}
   167  	if !strings.Contains(s, "/") {
   168  		m, err := bisect.New(s)
   169  		if err != nil {
   170  			Fatalf("%s: %v", ev, err)
   171  		}
   172  		hd.bisect = m
   173  		return hd
   174  	}
   175  
   176  	// TODO: Delete remainder of function when we switch to bisect-only.
   177  	ss := strings.Split(s, "/")
   178  	// first remove any leading exclusions; these are preceded with "-"
   179  	i := 0
   180  	for len(ss) > 0 {
   181  		s := ss[0]
   182  		if len(s) == 0 || len(s) > 0 && s[0] != '-' {
   183  			break
   184  		}
   185  		ss = ss[1:]
   186  		hd.excludes = append(hd.excludes, toHashAndMask(s[1:], fmt.Sprintf("%s%d", "HASH_EXCLUDE", i)))
   187  		i++
   188  	}
   189  	// hash searches may use additional EVs with 0, 1, 2, ... suffixes.
   190  	i = 0
   191  	for _, s := range ss {
   192  		if s == "" {
   193  			if i != 0 || len(ss) > 1 && ss[1] != "" || len(ss) > 2 {
   194  				Fatalf("Empty hash match string for %s should be first (and only) one", ev)
   195  			}
   196  			// Special case of should match everything.
   197  			hd.matches = append(hd.matches, toHashAndMask("0", fmt.Sprintf("%s0", ev)))
   198  			hd.matches = append(hd.matches, toHashAndMask("1", fmt.Sprintf("%s1", ev)))
   199  			break
   200  		}
   201  		if i == 0 {
   202  			hd.matches = append(hd.matches, toHashAndMask(s, ev))
   203  		} else {
   204  			hd.matches = append(hd.matches, toHashAndMask(s, fmt.Sprintf("%s%d", ev, i-1)))
   205  		}
   206  		i++
   207  	}
   208  	return hd
   209  }
   210  
   211  // TODO: Delete when we switch to bisect-only.
   212  func (d *HashDebug) excluded(hash uint64) bool {
   213  	for _, m := range d.excludes {
   214  		if (m.hash^hash)&m.mask == 0 {
   215  			return true
   216  		}
   217  	}
   218  	return false
   219  }
   220  
   221  // TODO: Delete when we switch to bisect-only.
   222  func hashString(hash uint64) string {
   223  	hstr := ""
   224  	if hash == 0 {
   225  		hstr = "0"
   226  	} else {
   227  		for ; hash != 0; hash = hash >> 1 {
   228  			hstr = string('0'+byte(hash&1)) + hstr
   229  		}
   230  	}
   231  	if len(hstr) > 24 {
   232  		hstr = hstr[len(hstr)-24:]
   233  	}
   234  	return hstr
   235  }
   236  
   237  // TODO: Delete when we switch to bisect-only.
   238  func (d *HashDebug) match(hash uint64) *hashAndMask {
   239  	for i, m := range d.matches {
   240  		if (m.hash^hash)&m.mask == 0 {
   241  			return &d.matches[i]
   242  		}
   243  	}
   244  	return nil
   245  }
   246  
   247  // MatchPkgFunc returns true if either the variable used to create d is
   248  // unset, or if its value is y, or if it is a suffix of the base-two
   249  // representation of the hash of pkg and fn.  If the variable is not nil,
   250  // then a true result is accompanied by stylized output to d.logfile, which
   251  // is used for automated bug search.
   252  func (d *HashDebug) MatchPkgFunc(pkg, fn string, note func() string) bool {
   253  	if d == nil {
   254  		return true
   255  	}
   256  	// Written this way to make inlining likely.
   257  	return d.matchPkgFunc(pkg, fn, note)
   258  }
   259  
   260  func (d *HashDebug) matchPkgFunc(pkg, fn string, note func() string) bool {
   261  	hash := bisect.Hash(pkg, fn)
   262  	return d.matchAndLog(hash, func() string { return pkg + "." + fn }, note)
   263  }
   264  
   265  // MatchPos is similar to MatchPkgFunc, but for hash computation
   266  // it uses the source position including all inlining information instead of
   267  // package name and path.
   268  // Note that the default answer for no environment variable (d == nil)
   269  // is "yes", do the thing.
   270  func (d *HashDebug) MatchPos(pos src.XPos, desc func() string) bool {
   271  	if d == nil {
   272  		return true
   273  	}
   274  	// Written this way to make inlining likely.
   275  	return d.matchPos(Ctxt, pos, desc)
   276  }
   277  
   278  func (d *HashDebug) matchPos(ctxt *obj.Link, pos src.XPos, note func() string) bool {
   279  	return d.matchPosWithInfo(ctxt, pos, nil, note)
   280  }
   281  
   282  func (d *HashDebug) matchPosWithInfo(ctxt *obj.Link, pos src.XPos, info any, note func() string) bool {
   283  	hash := d.hashPos(ctxt, pos)
   284  	if info != nil {
   285  		hash = bisect.Hash(hash, info)
   286  	}
   287  	return d.matchAndLog(hash,
   288  		func() string {
   289  			r := d.fmtPos(ctxt, pos)
   290  			if info != nil {
   291  				r += fmt.Sprintf(" (%v)", info)
   292  			}
   293  			return r
   294  		},
   295  		note)
   296  }
   297  
   298  // MatchPosWithInfo is similar to MatchPos, but with additional information
   299  // that is included for hash computation, so it can distinguish multiple
   300  // matches on the same source location.
   301  // Note that the default answer for no environment variable (d == nil)
   302  // is "yes", do the thing.
   303  func (d *HashDebug) MatchPosWithInfo(pos src.XPos, info any, desc func() string) bool {
   304  	if d == nil {
   305  		return true
   306  	}
   307  	// Written this way to make inlining likely.
   308  	return d.matchPosWithInfo(Ctxt, pos, info, desc)
   309  }
   310  
   311  // matchAndLog is the core matcher. It reports whether the hash matches the pattern.
   312  // If a report needs to be printed, match prints that report to the log file.
   313  // The text func must be non-nil and should return a user-readable
   314  // representation of what was hashed. The note func may be nil; if non-nil,
   315  // it should return additional information to display to the user when this
   316  // change is selected.
   317  func (d *HashDebug) matchAndLog(hash uint64, text, note func() string) bool {
   318  	if d.bisect != nil {
   319  		enabled := d.bisect.ShouldEnable(hash)
   320  		if d.bisect.ShouldPrint(hash) {
   321  			disabled := ""
   322  			if !enabled {
   323  				disabled = " [DISABLED]"
   324  			}
   325  			var t string
   326  			if !d.bisect.MarkerOnly() {
   327  				t = text()
   328  				if note != nil {
   329  					if n := note(); n != "" {
   330  						t += ": " + n + disabled
   331  						disabled = ""
   332  					}
   333  				}
   334  			}
   335  			d.log(d.name, hash, strings.TrimSpace(t+disabled))
   336  		}
   337  		return enabled
   338  	}
   339  
   340  	// TODO: Delete rest of function body when we switch to bisect-only.
   341  	if d.excluded(hash) {
   342  		return false
   343  	}
   344  	if m := d.match(hash); m != nil {
   345  		d.log(m.name, hash, text())
   346  		return true
   347  	}
   348  	return false
   349  }
   350  
   351  // short returns the form of file name to use for d.
   352  // The default is the full path, but fileSuffixOnly selects
   353  // just the final path element.
   354  func (d *HashDebug) short(name string) string {
   355  	if d.fileSuffixOnly {
   356  		return filepath.Base(name)
   357  	}
   358  	return name
   359  }
   360  
   361  // hashPos returns a hash of the position pos, including its entire inline stack.
   362  // If d.inlineSuffixOnly is true, hashPos only considers the innermost (leaf) position on the inline stack.
   363  func (d *HashDebug) hashPos(ctxt *obj.Link, pos src.XPos) uint64 {
   364  	if d.inlineSuffixOnly {
   365  		p := ctxt.InnermostPos(pos)
   366  		return bisect.Hash(d.short(p.Filename()), p.Line(), p.Col())
   367  	}
   368  	h := bisect.Hash()
   369  	ctxt.AllPos(pos, func(p src.Pos) {
   370  		h = bisect.Hash(h, d.short(p.Filename()), p.Line(), p.Col())
   371  	})
   372  	return h
   373  }
   374  
   375  // fmtPos returns a textual formatting of the position pos, including its entire inline stack.
   376  // If d.inlineSuffixOnly is true, fmtPos only considers the innermost (leaf) position on the inline stack.
   377  func (d *HashDebug) fmtPos(ctxt *obj.Link, pos src.XPos) string {
   378  	format := func(p src.Pos) string {
   379  		return fmt.Sprintf("%s:%d:%d", d.short(p.Filename()), p.Line(), p.Col())
   380  	}
   381  	if d.inlineSuffixOnly {
   382  		return format(ctxt.InnermostPos(pos))
   383  	}
   384  	var stk []string
   385  	ctxt.AllPos(pos, func(p src.Pos) {
   386  		stk = append(stk, format(p))
   387  	})
   388  	return strings.Join(stk, "; ")
   389  }
   390  
   391  // log prints a match with the given hash and textual formatting.
   392  // TODO: Delete varname parameter when we switch to bisect-only.
   393  func (d *HashDebug) log(varname string, hash uint64, text string) {
   394  	d.mu.Lock()
   395  	defer d.mu.Unlock()
   396  
   397  	file := d.logfile
   398  	if file == nil {
   399  		if tmpfile := os.Getenv("GSHS_LOGFILE"); tmpfile != "" {
   400  			var err error
   401  			file, err = os.OpenFile(tmpfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
   402  			if err != nil {
   403  				Fatalf("could not open hash-testing logfile %s", tmpfile)
   404  				return
   405  			}
   406  		}
   407  		if file == nil {
   408  			file = os.Stdout
   409  		}
   410  		d.logfile = file
   411  	}
   412  
   413  	// Bisect output.
   414  	fmt.Fprintf(file, "%s %s\n", text, bisect.Marker(hash))
   415  
   416  	// Gossahash output.
   417  	// TODO: Delete rest of function when we switch to bisect-only.
   418  	fmt.Fprintf(file, "%s triggered %s %s\n", varname, text, hashString(hash))
   419  }
   420  

View as plain text