1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
package ir
import (
"go/types"
)
func (b *builder) buildExits(fn *Function) {
if obj := fn.Object(); obj != nil {
switch obj.Pkg().Path() {
case "runtime":
switch obj.Name() {
case "exit":
fn.NoReturn = AlwaysExits
return
case "throw":
fn.NoReturn = AlwaysExits
return
case "Goexit":
fn.NoReturn = AlwaysUnwinds
return
}
case "go.uber.org/zap":
switch obj.(*types.Func).FullName() {
case "(*go.uber.org/zap.Logger).Fatal",
"(*go.uber.org/zap.SugaredLogger).Fatal",
"(*go.uber.org/zap.SugaredLogger).Fatalw",
"(*go.uber.org/zap.SugaredLogger).Fatalf":
// Technically, this method does not unconditionally exit
// the process. It dynamically calls a function stored in
// the logger. If the function is nil, it defaults to
// os.Exit.
//
// The main intent of this method is to terminate the
// process, and that's what the vast majority of people
// will use it for. We'll happily accept some false
// negatives to avoid a lot of false positives.
fn.NoReturn = AlwaysExits
case "(*go.uber.org/zap.Logger).Panic",
"(*go.uber.org/zap.SugaredLogger).Panicw",
"(*go.uber.org/zap.SugaredLogger).Panicf":
fn.NoReturn = AlwaysUnwinds
return
case "(*go.uber.org/zap.Logger).DPanic",
"(*go.uber.org/zap.SugaredLogger).DPanicf",
"(*go.uber.org/zap.SugaredLogger).DPanicw":
// These methods will only panic in development.
}
case "github.com/sirupsen/logrus":
switch obj.(*types.Func).FullName() {
case "(*github.com/sirupsen/logrus.Logger).Exit":
// Technically, this method does not unconditionally exit
// the process. It dynamically calls a function stored in
// the logger. If the function is nil, it defaults to
// os.Exit.
//
// The main intent of this method is to terminate the
// process, and that's what the vast majority of people
// will use it for. We'll happily accept some false
// negatives to avoid a lot of false positives.
fn.NoReturn = AlwaysExits
return
case "(*github.com/sirupsen/logrus.Logger).Panic",
"(*github.com/sirupsen/logrus.Logger).Panicf",
"(*github.com/sirupsen/logrus.Logger).Panicln":
// These methods will always panic, but that's not
// statically known from the code alone, because they
// take a detour through the generic Log methods.
fn.NoReturn = AlwaysUnwinds
return
case "(*github.com/sirupsen/logrus.Entry).Panicf",
"(*github.com/sirupsen/logrus.Entry).Panicln":
// Entry.Panic has an explicit panic, but Panicf and
// Panicln do not, relying fully on the generic Log
// method.
fn.NoReturn = AlwaysUnwinds
return
case "(*github.com/sirupsen/logrus.Logger).Log",
"(*github.com/sirupsen/logrus.Logger).Logf",
"(*github.com/sirupsen/logrus.Logger).Logln":
// TODO(dh): we cannot handle these cases. Whether they
// exit or unwind depends on the level, which is set
// via the first argument. We don't currently support
// call-site-specific exit information.
}
case "github.com/golang/glog":
switch obj.(*types.Func).FullName() {
case "github.com/golang/glog.Exit",
"github.com/golang/glog.ExitDepth",
"github.com/golang/glog.Exitf",
"github.com/golang/glog.Exitln",
"github.com/golang/glog.Fatal",
"github.com/golang/glog.FatalDepth",
"github.com/golang/glog.Fatalf",
"github.com/golang/glog.Fatalln":
// all of these call os.Exit after logging
fn.NoReturn = AlwaysExits
}
case "k8s.io/klog":
switch obj.(*types.Func).FullName() {
case "k8s.io/klog.Exit",
"k8s.io/klog.ExitDepth",
"k8s.io/klog.Exitf",
"k8s.io/klog.Exitln",
"k8s.io/klog.Fatal",
"k8s.io/klog.FatalDepth",
"k8s.io/klog.Fatalf",
"k8s.io/klog.Fatalln":
// all of these call os.Exit after logging
fn.NoReturn = AlwaysExits
}
case "k8s.io/klog/v2":
switch obj.(*types.Func).FullName() {
case "k8s.io/klog/v2.Exit",
"k8s.io/klog/v2.ExitDepth",
"k8s.io/klog/v2.Exitf",
"k8s.io/klog/v2.Exitln",
"k8s.io/klog/v2.Fatal",
"k8s.io/klog/v2.FatalDepth",
"k8s.io/klog/v2.Fatalf",
"k8s.io/klog/v2.Fatalln":
// all of these call os.Exit after logging
fn.NoReturn = AlwaysExits
}
}
}
isRecoverCall := func(instr Instruction) bool {
if instr, ok := instr.(*Call); ok {
if builtin, ok := instr.Call.Value.(*Builtin); ok {
if builtin.Name() == "recover" {
return true
}
}
}
return false
}
both := NewBlockSet(len(fn.Blocks))
exits := NewBlockSet(len(fn.Blocks))
unwinds := NewBlockSet(len(fn.Blocks))
recovers := false
for _, u := range fn.Blocks {
for _, instr := range u.Instrs {
instrSwitch:
switch instr := instr.(type) {
case *Defer:
if recovers {
// avoid doing extra work, we already know that this function calls recover
continue
}
call := instr.Call.StaticCallee()
if call == nil {
// not a static call, so we can't be sure the
// deferred call isn't calling recover
recovers = true
break
}
if call.Package() == fn.Package() {
b.buildFunction(call)
}
if len(call.Blocks) == 0 {
// external function, we don't know what's
// happening inside it
//
// TODO(dh): this includes functions from
// imported packages, due to how go/analysis
// works. We could introduce another fact,
// like we've done for exiting and unwinding.
recovers = true
break
}
for _, y := range call.Blocks {
for _, instr2 := range y.Instrs {
if isRecoverCall(instr2) {
recovers = true
break instrSwitch
}
}
}
case *Panic:
both.Add(u)
unwinds.Add(u)
case CallInstruction:
switch instr.(type) {
case *Defer, *Call:
default:
continue
}
if instr.Common().IsInvoke() {
// give up
return
}
var call *Function
switch instr.Common().Value.(type) {
case *Function, *MakeClosure:
call = instr.Common().StaticCallee()
case *Builtin:
// the only builtins that affect control flow are
// panic and recover, and we've already handled
// those
continue
default:
// dynamic dispatch
return
}
// buildFunction is idempotent. if we're part of a
// (mutually) recursive call chain, then buildFunction
// will immediately return, and fn.WillExit will be false.
if call.Package() == fn.Package() {
b.buildFunction(call)
}
switch call.NoReturn {
case AlwaysExits:
both.Add(u)
exits.Add(u)
case AlwaysUnwinds:
both.Add(u)
unwinds.Add(u)
case NeverReturns:
both.Add(u)
}
}
}
}
// depth-first search trying to find a path to the exit block that
// doesn't cross any of the blacklisted blocks
seen := NewBlockSet(len(fn.Blocks))
var findPath func(root *BasicBlock, bl *BlockSet) bool
findPath = func(root *BasicBlock, bl *BlockSet) bool {
if root == fn.Exit {
return true
}
if seen.Has(root) {
return false
}
if bl.Has(root) {
return false
}
seen.Add(root)
for _, succ := range root.Succs {
if findPath(succ, bl) {
return true
}
}
return false
}
findPathEntry := func(root *BasicBlock, bl *BlockSet) bool {
if bl.Num() == 0 {
return true
}
seen.Clear()
return findPath(root, bl)
}
if !findPathEntry(fn.Blocks[0], exits) {
fn.NoReturn = AlwaysExits
} else if !recovers {
// Only consider unwinding and "never returns" if we don't
// call recover. If we do call recover, then panics don't
// bubble up the stack.
// TODO(dh): the position of the defer matters. If we
// unconditionally terminate before we defer a recover, then
// the recover is ineffective.
if !findPathEntry(fn.Blocks[0], unwinds) {
fn.NoReturn = AlwaysUnwinds
} else if !findPathEntry(fn.Blocks[0], both) {
fn.NoReturn = NeverReturns
}
}
}
func (b *builder) addUnreachables(fn *Function) {
var unreachable *BasicBlock
for _, bb := range fn.Blocks {
instrLoop:
for i, instr := range bb.Instrs {
if instr, ok := instr.(*Call); ok {
var call *Function
switch v := instr.Common().Value.(type) {
case *Function:
call = v
case *MakeClosure:
call = v.Fn.(*Function)
}
if call == nil {
continue
}
if call.Package() == fn.Package() {
// make sure we have information on all functions in this package
b.buildFunction(call)
}
switch call.NoReturn {
case AlwaysExits:
// This call will cause the process to terminate.
// Remove remaining instructions in the block and
// replace any control flow with Unreachable.
for _, succ := range bb.Succs {
succ.removePred(bb)
}
bb.Succs = bb.Succs[:0]
bb.Instrs = bb.Instrs[:i+1]
bb.emit(new(Unreachable), instr.Source())
addEdge(bb, fn.Exit)
break instrLoop
case AlwaysUnwinds:
// This call will cause the goroutine to terminate
// and defers to run (i.e. a panic or
// runtime.Goexit). Remove remaining instructions
// in the block and replace any control flow with
// an unconditional jump to the exit block.
for _, succ := range bb.Succs {
succ.removePred(bb)
}
bb.Succs = bb.Succs[:0]
bb.Instrs = bb.Instrs[:i+1]
bb.emit(new(Jump), instr.Source())
addEdge(bb, fn.Exit)
break instrLoop
case NeverReturns:
// This call will either cause the goroutine to
// terminate, or the process to terminate. Remove
// remaining instructions in the block and replace
// any control flow with a conditional jump to
// either the exit block, or Unreachable.
for _, succ := range bb.Succs {
succ.removePred(bb)
}
bb.Succs = bb.Succs[:0]
bb.Instrs = bb.Instrs[:i+1]
var c Call
c.Call.Value = &Builtin{
name: "ir:noreturnWasPanic",
sig: types.NewSignatureType(nil, nil, nil,
types.NewTuple(),
types.NewTuple(anonVar(types.Typ[types.Bool])),
false,
),
}
c.setType(types.Typ[types.Bool])
if unreachable == nil {
unreachable = fn.newBasicBlock("unreachable")
unreachable.emit(&Unreachable{}, nil)
addEdge(unreachable, fn.Exit)
}
bb.emit(&c, instr.Source())
bb.emit(&If{Cond: &c}, instr.Source())
addEdge(bb, fn.Exit)
addEdge(bb, unreachable)
break instrLoop
}
}
}
}
}
|