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

View as plain text