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