| 1 | // Copyright 2018 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 nilness |
| 6 | |
| 7 | import ( |
| 8 | _ "embed" |
| 9 | "fmt" |
| 10 | "go/token" |
| 11 | "go/types" |
| 12 | |
| 13 | "golang.org/x/tools/go/analysis" |
| 14 | "golang.org/x/tools/go/analysis/passes/buildssa" |
| 15 | "golang.org/x/tools/go/ssa" |
| 16 | "golang.org/x/tools/internal/analysis/analyzerutil" |
| 17 | "golang.org/x/tools/internal/typeparams" |
| 18 | ) |
| 19 | |
| 20 | //go:embed doc.go |
| 21 | var doc string |
| 22 | |
| 23 | var Analyzer = &analysis.Analyzer{ |
| 24 | Name: "nilness", |
| 25 | Doc: analyzerutil.MustExtractDoc(doc, "nilness"), |
| 26 | URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/nilness", |
| 27 | Run: run, |
| 28 | Requires: []*analysis.Analyzer{buildssa.Analyzer}, |
| 29 | } |
| 30 | |
| 31 | func run(pass *analysis.Pass) (any, error) { |
| 32 | ssainput := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) |
| 33 | for _, fn := range ssainput.SrcFuncs { |
| 34 | runFunc(pass, fn) |
| 35 | } |
| 36 | return nil, nil |
| 37 | } |
| 38 | |
| 39 | func runFunc(pass *analysis.Pass, fn *ssa.Function) { |
| 40 | reportf := func(category string, pos token.Pos, format string, args ...any) { |
| 41 | // We ignore nil-checking ssa.Instructions |
| 42 | // that don't correspond to syntax. |
| 43 | if pos.IsValid() { |
| 44 | pass.Report(analysis.Diagnostic{ |
| 45 | Pos: pos, |
| 46 | Category: category, |
| 47 | Message: fmt.Sprintf(format, args...), |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // notNil reports an error if v is provably nil. |
| 53 | notNil := func(stack []fact, instr ssa.Instruction, v ssa.Value, descr string) { |
| 54 | if nilnessOf(stack, v) == isnil { |
| 55 | reportf("nilderef", instr.Pos(), "%s", descr) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // visit visits reachable blocks of the CFG in dominance order, |
| 60 | // maintaining a stack of dominating nilness facts. |
| 61 | // |
| 62 | // By traversing the dom tree, we can pop facts off the stack as |
| 63 | // soon as we've visited a subtree. Had we traversed the CFG, |
| 64 | // we would need to retain the set of facts for each block. |
| 65 | seen := make([]bool, len(fn.Blocks)) // seen[i] means visit should ignore block i |
| 66 | var visit func(b *ssa.BasicBlock, stack []fact) |
| 67 | visit = func(b *ssa.BasicBlock, stack []fact) { |
| 68 | if seen[b.Index] { |
| 69 | return |
| 70 | } |
| 71 | seen[b.Index] = true |
| 72 | |
| 73 | // Report nil dereferences. |
| 74 | for _, instr := range b.Instrs { |
| 75 | switch instr := instr.(type) { |
| 76 | case ssa.CallInstruction: |
| 77 | // A nil receiver may be okay for type params. |
| 78 | cc := instr.Common() |
| 79 | if !(cc.IsInvoke() && typeparams.IsTypeParam(cc.Value.Type())) { |
| 80 | notNil(stack, instr, cc.Value, "nil dereference in "+cc.Description()) |
| 81 | } |
| 82 | case *ssa.FieldAddr: |
| 83 | notNil(stack, instr, instr.X, "nil dereference in field selection") |
| 84 | case *ssa.IndexAddr: |
| 85 | switch typeparams.CoreType(instr.X.Type()).(type) { |
| 86 | case *types.Pointer: // *array |
| 87 | notNil(stack, instr, instr.X, "nil dereference in array index operation") |
| 88 | case *types.Slice: |
| 89 | // This is not necessarily a runtime error, because |
| 90 | // it is usually dominated by a bounds check. |
| 91 | if isRangeIndex(instr) { |
| 92 | notNil(stack, instr, instr.X, "range of nil slice") |
| 93 | } else { |
| 94 | notNil(stack, instr, instr.X, "index of nil slice") |
| 95 | } |
| 96 | } |
| 97 | case *ssa.MapUpdate: |
| 98 | notNil(stack, instr, instr.Map, "nil dereference in map update") |
| 99 | case *ssa.Range: |
| 100 | // (Not a runtime error, but a likely mistake.) |
| 101 | notNil(stack, instr, instr.X, "range over nil map") |
| 102 | case *ssa.Slice: |
| 103 | // A nilcheck occurs in ptr[:] iff ptr is a pointer to an array. |
| 104 | if is[*types.Pointer](instr.X.Type().Underlying()) { |
| 105 | notNil(stack, instr, instr.X, "nil dereference in slice operation") |
| 106 | } |
| 107 | case *ssa.Store: |
| 108 | notNil(stack, instr, instr.Addr, "nil dereference in store") |
| 109 | case *ssa.TypeAssert: |
| 110 | if !instr.CommaOk { |
| 111 | notNil(stack, instr, instr.X, "nil dereference in type assertion") |
| 112 | } |
| 113 | case *ssa.UnOp: |
| 114 | switch instr.Op { |
| 115 | case token.MUL: // *X |
| 116 | notNil(stack, instr, instr.X, "nil dereference in load") |
| 117 | case token.ARROW: // <-ch |
| 118 | // (Not a runtime error, but a likely mistake.) |
| 119 | notNil(stack, instr, instr.X, "receive from nil channel") |
| 120 | } |
| 121 | case *ssa.Send: |
| 122 | // (Not a runtime error, but a likely mistake.) |
| 123 | notNil(stack, instr, instr.Chan, "send to nil channel") |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Look for panics with nil value |
| 128 | for _, instr := range b.Instrs { |
| 129 | switch instr := instr.(type) { |
| 130 | case *ssa.Panic: |
| 131 | if nilnessOf(stack, instr.X) == isnil { |
| 132 | reportf("nilpanic", instr.Pos(), "panic with nil value") |
| 133 | } |
| 134 | case *ssa.SliceToArrayPointer: |
| 135 | nn := nilnessOf(stack, instr.X) |
| 136 | if nn == isnil && slice2ArrayPtrLen(instr) > 0 { |
| 137 | reportf("conversionpanic", instr.Pos(), "nil slice being cast to an array of len > 0 will always panic") |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // For nil comparison blocks, report an error if the condition |
| 143 | // is degenerate, and push a nilness fact on the stack when |
| 144 | // visiting its true and false successor blocks. |
| 145 | if binop, tsucc, fsucc := eq(b); binop != nil { |
| 146 | xnil := nilnessOf(stack, binop.X) |
| 147 | ynil := nilnessOf(stack, binop.Y) |
| 148 | |
| 149 | if ynil != unknown && xnil != unknown && (xnil == isnil || ynil == isnil) { |
| 150 | // Degenerate condition: |
| 151 | // the nilness of both operands is known, |
| 152 | // and at least one of them is nil. |
| 153 | var adj string |
| 154 | if (xnil == ynil) == (binop.Op == token.EQL) { |
| 155 | adj = "tautological" |
| 156 | } else { |
| 157 | adj = "impossible" |
| 158 | } |
| 159 | reportf("cond", binop.Pos(), "%s condition: %s %s %s", adj, xnil, binop.Op, ynil) |
| 160 | |
| 161 | // If tsucc's or fsucc's sole incoming edge is impossible, |
| 162 | // it is unreachable. Prune traversal of it and |
| 163 | // all the blocks it dominates. |
| 164 | // (We could be more precise with full dataflow |
| 165 | // analysis of control-flow joins.) |
| 166 | var skip *ssa.BasicBlock |
| 167 | if xnil == ynil { |
| 168 | skip = fsucc |
| 169 | } else { |
| 170 | skip = tsucc |
| 171 | } |
| 172 | for _, d := range b.Dominees() { |
| 173 | if d == skip && len(d.Preds) == 1 { |
| 174 | continue |
| 175 | } |
| 176 | visit(d, stack) |
| 177 | } |
| 178 | return |
| 179 | } |
| 180 | |
| 181 | // "if x == nil" or "if nil == y" condition; x, y are unknown. |
| 182 | if xnil == isnil || ynil == isnil { |
| 183 | var newFacts facts |
| 184 | if xnil == isnil { |
| 185 | // x is nil, y is unknown: |
| 186 | // t successor learns y is nil. |
| 187 | newFacts = expandFacts(fact{binop.Y, isnil}) |
| 188 | } else { |
| 189 | // y is nil, x is unknown: |
| 190 | // t successor learns x is nil. |
| 191 | newFacts = expandFacts(fact{binop.X, isnil}) |
| 192 | } |
| 193 | |
| 194 | for _, d := range b.Dominees() { |
| 195 | // Successor blocks learn a fact |
| 196 | // only at non-critical edges. |
| 197 | // (We could do be more precise with full dataflow |
| 198 | // analysis of control-flow joins.) |
| 199 | s := stack |
| 200 | if len(d.Preds) == 1 { |
| 201 | if d == tsucc { |
| 202 | s = append(s, newFacts...) |
| 203 | } else if d == fsucc { |
| 204 | s = append(s, newFacts.negate()...) |
| 205 | } |
| 206 | } |
| 207 | visit(d, s) |
| 208 | } |
| 209 | return |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // In code of the form: |
| 214 | // |
| 215 | // if ptr, ok := x.(*T); ok { ... } else { fsucc } |
| 216 | // |
| 217 | // the fsucc block learns that ptr == nil, |
| 218 | // since that's its zero value. |
| 219 | if If, ok := b.Instrs[len(b.Instrs)-1].(*ssa.If); ok { |
| 220 | // Handle "if ok" and "if !ok" variants. |
| 221 | cond, fsucc := If.Cond, b.Succs[1] |
| 222 | if unop, ok := cond.(*ssa.UnOp); ok && unop.Op == token.NOT { |
| 223 | cond, fsucc = unop.X, b.Succs[0] |
| 224 | } |
| 225 | |
| 226 | // Match pattern: |
| 227 | // t0 = typeassert (pointerlike) |
| 228 | // t1 = extract t0 #0 // ptr |
| 229 | // t2 = extract t0 #1 // ok |
| 230 | // if t2 goto tsucc, fsucc |
| 231 | if extract1, ok := cond.(*ssa.Extract); ok && extract1.Index == 1 { |
| 232 | if assert, ok := extract1.Tuple.(*ssa.TypeAssert); ok && |
| 233 | isNillable(assert.AssertedType) { |
| 234 | for _, pinstr := range *assert.Referrers() { |
| 235 | if extract0, ok := pinstr.(*ssa.Extract); ok && |
| 236 | extract0.Index == 0 && |
| 237 | extract0.Tuple == extract1.Tuple { |
| 238 | for _, d := range b.Dominees() { |
| 239 | if len(d.Preds) == 1 && d == fsucc { |
| 240 | visit(d, append(stack, fact{extract0, isnil})) |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | for _, d := range b.Dominees() { |
| 250 | visit(d, stack) |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // Visit the entry block. No need to visit fn.Recover. |
| 255 | if fn.Blocks != nil { |
| 256 | visit(fn.Blocks[0], make([]fact, 0, 20)) // 20 is plenty |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // A fact records that a block is dominated |
| 261 | // by the condition v == nil or v != nil. |
| 262 | type fact struct { |
| 263 | value ssa.Value |
| 264 | nilness nilness |
| 265 | } |
| 266 | |
| 267 | func (f fact) negate() fact { return fact{f.value, -f.nilness} } |
| 268 | |
| 269 | type nilness int |
| 270 | |
| 271 | const ( |
| 272 | isnonnil = -1 |
| 273 | unknown nilness = 0 |
| 274 | isnil = 1 |
| 275 | ) |
| 276 | |
| 277 | var nilnessStrings = []string{"non-nil", "unknown", "nil"} |
| 278 | |
| 279 | func (n nilness) String() string { return nilnessStrings[n+1] } |
| 280 | |
| 281 | // nilnessOf reports whether v is definitely nil, definitely not nil, |
| 282 | // or unknown given the dominating stack of facts. |
| 283 | func nilnessOf(stack []fact, v ssa.Value) nilness { |
| 284 | |
| 285 | switch v := v.(type) { |
| 286 | // unwrap ChangeInterface and Slice values recursively, to detect if underlying |
| 287 | // values have any facts recorded or are otherwise known with regard to nilness. |
| 288 | // |
| 289 | // This work must be in addition to expanding facts about |
| 290 | // ChangeInterfaces during inference/fact gathering because this covers |
| 291 | // cases where the nilness of a value is intrinsic, rather than based |
| 292 | // on inferred facts, such as a zero value interface variable. That |
| 293 | // said, this work alone would only inform us when facts are about |
| 294 | // underlying values, rather than outer values, when the analysis is |
| 295 | // transitive in both directions. |
| 296 | case *ssa.ChangeInterface: |
| 297 | if underlying := nilnessOf(stack, v.X); underlying != unknown { |
| 298 | return underlying |
| 299 | } |
| 300 | case *ssa.MakeInterface: |
| 301 | // A MakeInterface is non-nil unless its operand is a type parameter. |
| 302 | tparam, ok := types.Unalias(v.X.Type()).(*types.TypeParam) |
| 303 | if !ok { |
| 304 | return isnonnil |
| 305 | } |
| 306 | |
| 307 | // A MakeInterface of a type parameter is non-nil if |
| 308 | // the type parameter cannot be instantiated as an |
| 309 | // interface type (#66835). |
| 310 | if terms, err := typeparams.NormalTerms(tparam.Constraint()); err == nil && len(terms) > 0 { |
| 311 | return isnonnil |
| 312 | } |
| 313 | |
| 314 | // If the type parameter can be instantiated as an |
| 315 | // interface (and thus also as a concrete type), |
| 316 | // we can't determine the nilness. |
| 317 | |
| 318 | case *ssa.Slice: |
| 319 | if underlying := nilnessOf(stack, v.X); underlying != unknown { |
| 320 | return underlying |
| 321 | } |
| 322 | case *ssa.SliceToArrayPointer: |
| 323 | nn := nilnessOf(stack, v.X) |
| 324 | if slice2ArrayPtrLen(v) > 0 { |
| 325 | if nn == isnil { |
| 326 | // We know that *(*[1]byte)(nil) is going to panic because of the |
| 327 | // conversion. So return unknown to the caller, prevent useless |
| 328 | // nil deference reporting due to * operator. |
| 329 | return unknown |
| 330 | } |
| 331 | // Otherwise, the conversion will yield a non-nil pointer to array. |
| 332 | // Note that the instruction can still panic if array length greater |
| 333 | // than slice length. If the value is used by another instruction, |
| 334 | // that instruction can assume the panic did not happen when that |
| 335 | // instruction is reached. |
| 336 | return isnonnil |
| 337 | } |
| 338 | // In case array length is zero, the conversion result depends on nilness of the slice. |
| 339 | if nn != unknown { |
| 340 | return nn |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // Is value intrinsically nil or non-nil? |
| 345 | switch v := v.(type) { |
| 346 | case *ssa.Alloc, |
| 347 | *ssa.FieldAddr, |
| 348 | *ssa.FreeVar, |
| 349 | *ssa.Function, |
| 350 | *ssa.Global, |
| 351 | *ssa.IndexAddr, |
| 352 | *ssa.MakeChan, |
| 353 | *ssa.MakeClosure, |
| 354 | *ssa.MakeMap, |
| 355 | *ssa.MakeSlice: |
| 356 | return isnonnil |
| 357 | |
| 358 | case *ssa.Const: |
| 359 | if v.IsNil() { |
| 360 | return isnil // nil or zero value of a pointer-like type |
| 361 | } else { |
| 362 | return unknown // non-pointer |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // Search dominating control-flow facts. |
| 367 | for _, f := range stack { |
| 368 | if f.value == v { |
| 369 | return f.nilness |
| 370 | } |
| 371 | } |
| 372 | return unknown |
| 373 | } |
| 374 | |
| 375 | func slice2ArrayPtrLen(v *ssa.SliceToArrayPointer) int64 { |
| 376 | return v.Type().(*types.Pointer).Elem().Underlying().(*types.Array).Len() |
| 377 | } |
| 378 | |
| 379 | // If b ends with an equality comparison, eq returns the operation and |
| 380 | // its true (equal) and false (not equal) successors. |
| 381 | func eq(b *ssa.BasicBlock) (op *ssa.BinOp, tsucc, fsucc *ssa.BasicBlock) { |
| 382 | if If, ok := b.Instrs[len(b.Instrs)-1].(*ssa.If); ok { |
| 383 | if binop, ok := If.Cond.(*ssa.BinOp); ok { |
| 384 | switch binop.Op { |
| 385 | case token.EQL: |
| 386 | return binop, b.Succs[0], b.Succs[1] |
| 387 | case token.NEQ: |
| 388 | return binop, b.Succs[1], b.Succs[0] |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | return nil, nil, nil |
| 393 | } |
| 394 | |
| 395 | // expandFacts takes a single fact and returns the set of facts that can be |
| 396 | // known about it or any of its related values. Some operations, like |
| 397 | // ChangeInterface, have transitive nilness, such that if you know the |
| 398 | // underlying value is nil, you also know the value itself is nil, and vice |
| 399 | // versa. This operation allows callers to match on any of the related values |
| 400 | // in analyses, rather than just the one form of the value that happened to |
| 401 | // appear in a comparison. |
| 402 | // |
| 403 | // This work must be in addition to unwrapping values within nilnessOf because |
| 404 | // while this work helps give facts about transitively known values based on |
| 405 | // inferred facts, the recursive check within nilnessOf covers cases where |
| 406 | // nilness facts are intrinsic to the underlying value, such as a zero value |
| 407 | // interface variables. |
| 408 | // |
| 409 | // ChangeInterface is the only expansion currently supported, but others, like |
| 410 | // Slice, could be added. At this time, this tool does not check slice |
| 411 | // operations in a way this expansion could help. See |
| 412 | // https://play.golang.org/p/mGqXEp7w4fR for an example. |
| 413 | func expandFacts(f fact) []fact { |
| 414 | ff := []fact{f} |
| 415 | |
| 416 | Loop: |
| 417 | for { |
| 418 | switch v := f.value.(type) { |
| 419 | case *ssa.ChangeInterface: |
| 420 | f = fact{v.X, f.nilness} |
| 421 | ff = append(ff, f) |
| 422 | default: |
| 423 | break Loop |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return ff |
| 428 | } |
| 429 | |
| 430 | type facts []fact |
| 431 | |
| 432 | func (ff facts) negate() facts { |
| 433 | nn := make([]fact, len(ff)) |
| 434 | for i, f := range ff { |
| 435 | nn[i] = f.negate() |
| 436 | } |
| 437 | return nn |
| 438 | } |
| 439 | |
| 440 | func is[T any](x any) bool { |
| 441 | _, ok := x.(T) |
| 442 | return ok |
| 443 | } |
| 444 | |
| 445 | func isNillable(t types.Type) bool { |
| 446 | // TODO(adonovan): CoreType (+ case *Interface) looks wrong. |
| 447 | // This should probably use Underlying, and handle TypeParam |
| 448 | // by computing the union across its normal terms. |
| 449 | switch t := typeparams.CoreType(t).(type) { |
| 450 | case *types.Pointer, |
| 451 | *types.Map, |
| 452 | *types.Signature, |
| 453 | *types.Chan, |
| 454 | *types.Interface, |
| 455 | *types.Slice: |
| 456 | return true |
| 457 | case *types.Basic: |
| 458 | return t == types.Typ[types.UnsafePointer] |
| 459 | } |
| 460 | return false |
| 461 | } |
| 462 | |
| 463 | // isRangeIndex reports whether the instruction is a slice indexing |
| 464 | // operation slice[i] within a "for range slice" loop. The operation |
| 465 | // could be explicit, such as slice[i] within (or even after) the |
| 466 | // loop, or it could be implicit, such as "for i, v := range slice {}". |
| 467 | // (These cannot be reliably distinguished.) |
| 468 | func isRangeIndex(instr *ssa.IndexAddr) bool { |
| 469 | // Here we reverse-engineer the go/ssa lowering of range-over-slice: |
| 470 | // |
| 471 | // n = len(x) |
| 472 | // jump loop |
| 473 | // loop: "rangeindex.loop" |
| 474 | // phi = φ(-1, incr) #rangeindex |
| 475 | // incr = phi + 1 |
| 476 | // cond = incr < n |
| 477 | // if cond goto body else done |
| 478 | // body: "rangeindex.body" |
| 479 | // instr = &x[incr] |
| 480 | // ... |
| 481 | // done: |
| 482 | if incr, ok := instr.Index.(*ssa.BinOp); ok && incr.Op == token.ADD { |
| 483 | if b := incr.Block(); b.Comment == "rangeindex.loop" { |
| 484 | if If, ok := b.Instrs[len(b.Instrs)-1].(*ssa.If); ok { |
| 485 | if cond := If.Cond.(*ssa.BinOp); cond.X == incr && cond.Op == token.LSS { |
| 486 | if call, ok := cond.Y.(*ssa.Call); ok { |
| 487 | common := call.Common() |
| 488 | if blt, ok := common.Value.(*ssa.Builtin); ok && blt.Name() == "len" { |
| 489 | return common.Args[0] == instr.X |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | return false |
| 497 | } |