github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/go/ssa/sanity.go (about)

     1  // Copyright 2013 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 ssa
     6  
     7  // An optional pass for sanity-checking invariants of the SSA representation.
     8  // Currently it checks CFG invariants but little at the instruction level.
     9  
    10  import (
    11  	"fmt"
    12  	"go/types"
    13  	"io"
    14  	"os"
    15  	"strings"
    16  )
    17  
    18  type sanity struct {
    19  	reporter io.Writer
    20  	fn       *Function
    21  	block    *BasicBlock
    22  	instrs   map[Instruction]struct{}
    23  	insane   bool
    24  }
    25  
    26  // sanityCheck performs integrity checking of the SSA representation
    27  // of the function fn and returns true if it was valid.  Diagnostics
    28  // are written to reporter if non-nil, os.Stderr otherwise.  Some
    29  // diagnostics are only warnings and do not imply a negative result.
    30  //
    31  // Sanity-checking is intended to facilitate the debugging of code
    32  // transformation passes.
    33  func sanityCheck(fn *Function, reporter io.Writer) bool {
    34  	if reporter == nil {
    35  		reporter = os.Stderr
    36  	}
    37  	return (&sanity{reporter: reporter}).checkFunction(fn)
    38  }
    39  
    40  // mustSanityCheck is like sanityCheck but panics instead of returning
    41  // a negative result.
    42  func mustSanityCheck(fn *Function, reporter io.Writer) {
    43  	if !sanityCheck(fn, reporter) {
    44  		fn.WriteTo(os.Stderr)
    45  		panic("SanityCheck failed")
    46  	}
    47  }
    48  
    49  func (s *sanity) diagnostic(prefix, format string, args ...interface{}) {
    50  	fmt.Fprintf(s.reporter, "%s: function %s", prefix, s.fn)
    51  	if s.block != nil {
    52  		fmt.Fprintf(s.reporter, ", block %s", s.block)
    53  	}
    54  	io.WriteString(s.reporter, ": ")
    55  	fmt.Fprintf(s.reporter, format, args...)
    56  	io.WriteString(s.reporter, "\n")
    57  }
    58  
    59  func (s *sanity) errorf(format string, args ...interface{}) {
    60  	s.insane = true
    61  	s.diagnostic("Error", format, args...)
    62  }
    63  
    64  func (s *sanity) warnf(format string, args ...interface{}) {
    65  	s.diagnostic("Warning", format, args...)
    66  }
    67  
    68  // findDuplicate returns an arbitrary basic block that appeared more
    69  // than once in blocks, or nil if all were unique.
    70  func findDuplicate(blocks []*BasicBlock) *BasicBlock {
    71  	if len(blocks) < 2 {
    72  		return nil
    73  	}
    74  	if blocks[0] == blocks[1] {
    75  		return blocks[0]
    76  	}
    77  	// Slow path:
    78  	m := make(map[*BasicBlock]bool)
    79  	for _, b := range blocks {
    80  		if m[b] {
    81  			return b
    82  		}
    83  		m[b] = true
    84  	}
    85  	return nil
    86  }
    87  
    88  func (s *sanity) checkInstr(idx int, instr Instruction) {
    89  	switch instr := instr.(type) {
    90  	case *If, *Jump, *Return, *Panic:
    91  		s.errorf("control flow instruction not at end of block")
    92  	case *Phi:
    93  		if idx == 0 {
    94  			// It suffices to apply this check to just the first phi node.
    95  			if dup := findDuplicate(s.block.Preds); dup != nil {
    96  				s.errorf("phi node in block with duplicate predecessor %s", dup)
    97  			}
    98  		} else {
    99  			prev := s.block.Instrs[idx-1]
   100  			if _, ok := prev.(*Phi); !ok {
   101  				s.errorf("Phi instruction follows a non-Phi: %T", prev)
   102  			}
   103  		}
   104  		if ne, np := len(instr.Edges), len(s.block.Preds); ne != np {
   105  			s.errorf("phi node has %d edges but %d predecessors", ne, np)
   106  
   107  		} else {
   108  			for i, e := range instr.Edges {
   109  				if e == nil {
   110  					s.errorf("phi node '%s' has no value for edge #%d from %s", instr.Comment, i, s.block.Preds[i])
   111  				} else if !types.Identical(instr.typ, e.Type()) {
   112  					s.errorf("phi node '%s' has a different type (%s) for edge #%d from %s (%s)",
   113  						instr.Comment, instr.Type(), i, s.block.Preds[i], e.Type())
   114  				}
   115  			}
   116  		}
   117  
   118  	case *Alloc:
   119  		if !instr.Heap {
   120  			found := false
   121  			for _, l := range s.fn.Locals {
   122  				if l == instr {
   123  					found = true
   124  					break
   125  				}
   126  			}
   127  			if !found {
   128  				s.errorf("local alloc %s = %s does not appear in Function.Locals", instr.Name(), instr)
   129  			}
   130  		}
   131  
   132  	case *BinOp:
   133  	case *Call:
   134  	case *ChangeInterface:
   135  	case *ChangeType:
   136  	case *SliceToArrayPointer:
   137  	case *Convert:
   138  		if from := instr.X.Type(); !isBasicConvTypes(typeSetOf(from)) {
   139  			if to := instr.Type(); !isBasicConvTypes(typeSetOf(to)) {
   140  				s.errorf("convert %s -> %s: at least one type must be basic (or all basic, []byte, or []rune)", from, to)
   141  			}
   142  		}
   143  	case *MultiConvert:
   144  	case *Defer:
   145  	case *Extract:
   146  	case *Field:
   147  	case *FieldAddr:
   148  	case *Go:
   149  	case *Index:
   150  	case *IndexAddr:
   151  	case *Lookup:
   152  	case *MakeChan:
   153  	case *MakeClosure:
   154  		numFree := len(instr.Fn.(*Function).FreeVars)
   155  		numBind := len(instr.Bindings)
   156  		if numFree != numBind {
   157  			s.errorf("MakeClosure has %d Bindings for function %s with %d free vars",
   158  				numBind, instr.Fn, numFree)
   159  
   160  		}
   161  		if recv := instr.Type().(*types.Signature).Recv(); recv != nil {
   162  			s.errorf("MakeClosure's type includes receiver %s", recv.Type())
   163  		}
   164  
   165  	case *MakeInterface:
   166  	case *MakeMap:
   167  	case *MakeSlice:
   168  	case *MapUpdate:
   169  	case *Next:
   170  	case *Range:
   171  	case *RunDefers:
   172  	case *Select:
   173  	case *Send:
   174  	case *Slice:
   175  	case *Store:
   176  	case *TypeAssert:
   177  	case *UnOp:
   178  	case *DebugRef:
   179  		// TODO(adonovan): implement checks.
   180  	default:
   181  		panic(fmt.Sprintf("Unknown instruction type: %T", instr))
   182  	}
   183  
   184  	if call, ok := instr.(CallInstruction); ok {
   185  		if call.Common().Signature() == nil {
   186  			s.errorf("nil signature: %s", call)
   187  		}
   188  	}
   189  
   190  	// Check that value-defining instructions have valid types
   191  	// and a valid referrer list.
   192  	if v, ok := instr.(Value); ok {
   193  		t := v.Type()
   194  		if t == nil {
   195  			s.errorf("no type: %s = %s", v.Name(), v)
   196  		} else if t == tRangeIter {
   197  			// not a proper type; ignore.
   198  		} else if b, ok := t.Underlying().(*types.Basic); ok && b.Info()&types.IsUntyped != 0 {
   199  			s.errorf("instruction has 'untyped' result: %s = %s : %s", v.Name(), v, t)
   200  		}
   201  		s.checkReferrerList(v)
   202  	}
   203  
   204  	// Untyped constants are legal as instruction Operands(),
   205  	// for example:
   206  	//   _ = "foo"[0]
   207  	// or:
   208  	//   if wordsize==64 {...}
   209  
   210  	// All other non-Instruction Values can be found via their
   211  	// enclosing Function or Package.
   212  }
   213  
   214  func (s *sanity) checkFinalInstr(instr Instruction) {
   215  	switch instr := instr.(type) {
   216  	case *If:
   217  		if nsuccs := len(s.block.Succs); nsuccs != 2 {
   218  			s.errorf("If-terminated block has %d successors; expected 2", nsuccs)
   219  			return
   220  		}
   221  		if s.block.Succs[0] == s.block.Succs[1] {
   222  			s.errorf("If-instruction has same True, False target blocks: %s", s.block.Succs[0])
   223  			return
   224  		}
   225  
   226  	case *Jump:
   227  		if nsuccs := len(s.block.Succs); nsuccs != 1 {
   228  			s.errorf("Jump-terminated block has %d successors; expected 1", nsuccs)
   229  			return
   230  		}
   231  
   232  	case *Return:
   233  		if nsuccs := len(s.block.Succs); nsuccs != 0 {
   234  			s.errorf("Return-terminated block has %d successors; expected none", nsuccs)
   235  			return
   236  		}
   237  		if na, nf := len(instr.Results), s.fn.Signature.Results().Len(); nf != na {
   238  			s.errorf("%d-ary return in %d-ary function", na, nf)
   239  		}
   240  
   241  	case *Panic:
   242  		if nsuccs := len(s.block.Succs); nsuccs != 0 {
   243  			s.errorf("Panic-terminated block has %d successors; expected none", nsuccs)
   244  			return
   245  		}
   246  
   247  	default:
   248  		s.errorf("non-control flow instruction at end of block")
   249  	}
   250  }
   251  
   252  func (s *sanity) checkBlock(b *BasicBlock, index int) {
   253  	s.block = b
   254  
   255  	if b.Index != index {
   256  		s.errorf("block has incorrect Index %d", b.Index)
   257  	}
   258  	if b.parent != s.fn {
   259  		s.errorf("block has incorrect parent %s", b.parent)
   260  	}
   261  
   262  	// Check all blocks are reachable.
   263  	// (The entry block is always implicitly reachable,
   264  	// as is the Recover block, if any.)
   265  	if (index > 0 && b != b.parent.Recover) && len(b.Preds) == 0 {
   266  		s.warnf("unreachable block")
   267  		if b.Instrs == nil {
   268  			// Since this block is about to be pruned,
   269  			// tolerating transient problems in it
   270  			// simplifies other optimizations.
   271  			return
   272  		}
   273  	}
   274  
   275  	// Check predecessor and successor relations are dual,
   276  	// and that all blocks in CFG belong to same function.
   277  	for _, a := range b.Preds {
   278  		found := false
   279  		for _, bb := range a.Succs {
   280  			if bb == b {
   281  				found = true
   282  				break
   283  			}
   284  		}
   285  		if !found {
   286  			s.errorf("expected successor edge in predecessor %s; found only: %s", a, a.Succs)
   287  		}
   288  		if a.parent != s.fn {
   289  			s.errorf("predecessor %s belongs to different function %s", a, a.parent)
   290  		}
   291  	}
   292  	for _, c := range b.Succs {
   293  		found := false
   294  		for _, bb := range c.Preds {
   295  			if bb == b {
   296  				found = true
   297  				break
   298  			}
   299  		}
   300  		if !found {
   301  			s.errorf("expected predecessor edge in successor %s; found only: %s", c, c.Preds)
   302  		}
   303  		if c.parent != s.fn {
   304  			s.errorf("successor %s belongs to different function %s", c, c.parent)
   305  		}
   306  	}
   307  
   308  	// Check each instruction is sane.
   309  	n := len(b.Instrs)
   310  	if n == 0 {
   311  		s.errorf("basic block contains no instructions")
   312  	}
   313  	var rands [10]*Value // reuse storage
   314  	for j, instr := range b.Instrs {
   315  		if instr == nil {
   316  			s.errorf("nil instruction at index %d", j)
   317  			continue
   318  		}
   319  		if b2 := instr.Block(); b2 == nil {
   320  			s.errorf("nil Block() for instruction at index %d", j)
   321  			continue
   322  		} else if b2 != b {
   323  			s.errorf("wrong Block() (%s) for instruction at index %d ", b2, j)
   324  			continue
   325  		}
   326  		if j < n-1 {
   327  			s.checkInstr(j, instr)
   328  		} else {
   329  			s.checkFinalInstr(instr)
   330  		}
   331  
   332  		// Check Instruction.Operands.
   333  	operands:
   334  		for i, op := range instr.Operands(rands[:0]) {
   335  			if op == nil {
   336  				s.errorf("nil operand pointer %d of %s", i, instr)
   337  				continue
   338  			}
   339  			val := *op
   340  			if val == nil {
   341  				continue // a nil operand is ok
   342  			}
   343  
   344  			// Check that "untyped" types only appear on constant operands.
   345  			if _, ok := (*op).(*Const); !ok {
   346  				if basic, ok := (*op).Type().(*types.Basic); ok {
   347  					if basic.Info()&types.IsUntyped != 0 {
   348  						s.errorf("operand #%d of %s is untyped: %s", i, instr, basic)
   349  					}
   350  				}
   351  			}
   352  
   353  			// Check that Operands that are also Instructions belong to same function.
   354  			// TODO(adonovan): also check their block dominates block b.
   355  			if val, ok := val.(Instruction); ok {
   356  				if val.Block() == nil {
   357  					s.errorf("operand %d of %s is an instruction (%s) that belongs to no block", i, instr, val)
   358  				} else if val.Parent() != s.fn {
   359  					s.errorf("operand %d of %s is an instruction (%s) from function %s", i, instr, val, val.Parent())
   360  				}
   361  			}
   362  
   363  			// Check that each function-local operand of
   364  			// instr refers back to instr.  (NB: quadratic)
   365  			switch val := val.(type) {
   366  			case *Const, *Global, *Builtin:
   367  				continue // not local
   368  			case *Function:
   369  				if val.parent == nil {
   370  					continue // only anon functions are local
   371  				}
   372  			}
   373  
   374  			// TODO(adonovan): check val.Parent() != nil <=> val.Referrers() is defined.
   375  
   376  			if refs := val.Referrers(); refs != nil {
   377  				for _, ref := range *refs {
   378  					if ref == instr {
   379  						continue operands
   380  					}
   381  				}
   382  				s.errorf("operand %d of %s (%s) does not refer to us", i, instr, val)
   383  			} else {
   384  				s.errorf("operand %d of %s (%s) has no referrers", i, instr, val)
   385  			}
   386  		}
   387  	}
   388  }
   389  
   390  func (s *sanity) checkReferrerList(v Value) {
   391  	refs := v.Referrers()
   392  	if refs == nil {
   393  		s.errorf("%s has missing referrer list", v.Name())
   394  		return
   395  	}
   396  	for i, ref := range *refs {
   397  		if _, ok := s.instrs[ref]; !ok {
   398  			s.errorf("%s.Referrers()[%d] = %s is not an instruction belonging to this function", v.Name(), i, ref)
   399  		}
   400  	}
   401  }
   402  
   403  func (s *sanity) checkFunction(fn *Function) bool {
   404  	// TODO(adonovan): check Function invariants:
   405  	// - check params match signature
   406  	// - check transient fields are nil
   407  	// - warn if any fn.Locals do not appear among block instructions.
   408  
   409  	// TODO(taking): Sanity check origin, typeparams, and typeargs.
   410  	s.fn = fn
   411  	if fn.Prog == nil {
   412  		s.errorf("nil Prog")
   413  	}
   414  
   415  	_ = fn.String()               // must not crash
   416  	_ = fn.RelString(fn.relPkg()) // must not crash
   417  
   418  	// All functions have a package, except delegates (which are
   419  	// shared across packages, or duplicated as weak symbols in a
   420  	// separate-compilation model), and error.Error.
   421  	if fn.Pkg == nil {
   422  		if strings.HasPrefix(fn.Synthetic, "wrapper ") ||
   423  			strings.HasPrefix(fn.Synthetic, "bound ") ||
   424  			strings.HasPrefix(fn.Synthetic, "thunk ") ||
   425  			strings.HasSuffix(fn.name, "Error") ||
   426  			strings.HasPrefix(fn.Synthetic, "instance ") ||
   427  			strings.HasPrefix(fn.Synthetic, "instantiation ") ||
   428  			(fn.parent != nil && len(fn.typeargs) > 0) /* anon fun in instance */ {
   429  			// ok
   430  		} else {
   431  			s.errorf("nil Pkg")
   432  		}
   433  	}
   434  	if src, syn := fn.Synthetic == "", fn.Syntax() != nil; src != syn {
   435  		if len(fn.typeargs) > 0 && fn.Prog.mode&InstantiateGenerics != 0 {
   436  			// ok (instantiation with InstantiateGenerics on)
   437  		} else if fn.topLevelOrigin != nil && len(fn.typeargs) > 0 {
   438  			// ok (we always have the syntax set for instantiation)
   439  		} else {
   440  			s.errorf("got fromSource=%t, hasSyntax=%t; want same values", src, syn)
   441  		}
   442  	}
   443  	for i, l := range fn.Locals {
   444  		if l.Parent() != fn {
   445  			s.errorf("Local %s at index %d has wrong parent", l.Name(), i)
   446  		}
   447  		if l.Heap {
   448  			s.errorf("Local %s at index %d has Heap flag set", l.Name(), i)
   449  		}
   450  	}
   451  	// Build the set of valid referrers.
   452  	s.instrs = make(map[Instruction]struct{})
   453  	for _, b := range fn.Blocks {
   454  		for _, instr := range b.Instrs {
   455  			s.instrs[instr] = struct{}{}
   456  		}
   457  	}
   458  	for i, p := range fn.Params {
   459  		if p.Parent() != fn {
   460  			s.errorf("Param %s at index %d has wrong parent", p.Name(), i)
   461  		}
   462  		// Check common suffix of Signature and Params match type.
   463  		if sig := fn.Signature; sig != nil {
   464  			j := i - len(fn.Params) + sig.Params().Len() // index within sig.Params
   465  			if j < 0 {
   466  				continue
   467  			}
   468  			if !types.Identical(p.Type(), sig.Params().At(j).Type()) {
   469  				s.errorf("Param %s at index %d has wrong type (%s, versus %s in Signature)", p.Name(), i, p.Type(), sig.Params().At(j).Type())
   470  
   471  			}
   472  		}
   473  		s.checkReferrerList(p)
   474  	}
   475  	for i, fv := range fn.FreeVars {
   476  		if fv.Parent() != fn {
   477  			s.errorf("FreeVar %s at index %d has wrong parent", fv.Name(), i)
   478  		}
   479  		s.checkReferrerList(fv)
   480  	}
   481  
   482  	if fn.Blocks != nil && len(fn.Blocks) == 0 {
   483  		// Function _had_ blocks (so it's not external) but
   484  		// they were "optimized" away, even the entry block.
   485  		s.errorf("Blocks slice is non-nil but empty")
   486  	}
   487  	for i, b := range fn.Blocks {
   488  		if b == nil {
   489  			s.warnf("nil *BasicBlock at f.Blocks[%d]", i)
   490  			continue
   491  		}
   492  		s.checkBlock(b, i)
   493  	}
   494  	if fn.Recover != nil && fn.Blocks[fn.Recover.Index] != fn.Recover {
   495  		s.errorf("Recover block is not in Blocks slice")
   496  	}
   497  
   498  	s.block = nil
   499  	for i, anon := range fn.AnonFuncs {
   500  		if anon.Parent() != fn {
   501  			s.errorf("AnonFuncs[%d]=%s but %s.Parent()=%s", i, anon, anon, anon.Parent())
   502  		}
   503  		if i != int(anon.anonIdx) {
   504  			s.errorf("AnonFuncs[%d]=%s but %s.anonIdx=%d", i, anon, anon, anon.anonIdx)
   505  		}
   506  	}
   507  	s.fn = nil
   508  	return !s.insane
   509  }
   510  
   511  // sanityCheckPackage checks invariants of packages upon creation.
   512  // It does not require that the package is built.
   513  // Unlike sanityCheck (for functions), it just panics at the first error.
   514  func sanityCheckPackage(pkg *Package) {
   515  	if pkg.Pkg == nil {
   516  		panic(fmt.Sprintf("Package %s has no Object", pkg))
   517  	}
   518  	_ = pkg.String() // must not crash
   519  
   520  	for name, mem := range pkg.Members {
   521  		if name != mem.Name() {
   522  			panic(fmt.Sprintf("%s: %T.Name() = %s, want %s",
   523  				pkg.Pkg.Path(), mem, mem.Name(), name))
   524  		}
   525  		obj := mem.Object()
   526  		if obj == nil {
   527  			// This check is sound because fields
   528  			// {Global,Function}.object have type
   529  			// types.Object.  (If they were declared as
   530  			// *types.{Var,Func}, we'd have a non-empty
   531  			// interface containing a nil pointer.)
   532  
   533  			continue // not all members have typechecker objects
   534  		}
   535  		if obj.Name() != name {
   536  			if obj.Name() == "init" && strings.HasPrefix(mem.Name(), "init#") {
   537  				// Ok.  The name of a declared init function varies between
   538  				// its types.Func ("init") and its ssa.Function ("init#%d").
   539  			} else {
   540  				panic(fmt.Sprintf("%s: %T.Object().Name() = %s, want %s",
   541  					pkg.Pkg.Path(), mem, obj.Name(), name))
   542  			}
   543  		}
   544  		if obj.Pos() != mem.Pos() {
   545  			panic(fmt.Sprintf("%s Pos=%d obj.Pos=%d", mem, mem.Pos(), obj.Pos()))
   546  		}
   547  	}
   548  }