Source file src/cmd/compile/internal/escape/call.go

     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 escape
     6  
     7  import (
     8  	"cmd/compile/internal/base"
     9  	"cmd/compile/internal/ir"
    10  	"cmd/compile/internal/typecheck"
    11  	"cmd/compile/internal/types"
    12  	"cmd/internal/src"
    13  	"strings"
    14  )
    15  
    16  // call evaluates a call expressions, including builtin calls. ks
    17  // should contain the holes representing where the function callee's
    18  // results flows.
    19  func (e *escape) call(ks []hole, call ir.Node) {
    20  	argument := func(k hole, arg ir.Node) {
    21  		// TODO(mdempsky): Should be "call argument".
    22  		e.expr(k.note(call, "call parameter"), arg)
    23  	}
    24  
    25  	switch call.Op() {
    26  	default:
    27  		ir.Dump("esc", call)
    28  		base.Fatalf("unexpected call op: %v", call.Op())
    29  
    30  	case ir.OCALLFUNC, ir.OCALLINTER:
    31  		call := call.(*ir.CallExpr)
    32  		typecheck.AssertFixedCall(call)
    33  
    34  		// Pick out the function callee, if statically known.
    35  		//
    36  		// TODO(mdempsky): Change fn from *ir.Name to *ir.Func, but some
    37  		// functions (e.g., runtime builtins, method wrappers, generated
    38  		// eq/hash functions) don't have it set. Investigate whether
    39  		// that's a concern.
    40  		var fn *ir.Name
    41  		switch call.Op() {
    42  		case ir.OCALLFUNC:
    43  			// TODO(thepudds): use an ir.ReassignOracle here.
    44  			v := ir.StaticValue(call.Fun)
    45  			fn = ir.StaticCalleeName(v)
    46  		}
    47  
    48  		fntype := call.Fun.Type()
    49  		if fn != nil {
    50  			fntype = fn.Type()
    51  		}
    52  
    53  		if ks != nil && fn != nil && e.inMutualBatch(fn) {
    54  			for i, result := range fn.Type().Results() {
    55  				e.expr(ks[i], result.Nname.(*ir.Name))
    56  			}
    57  		}
    58  
    59  		var recvArg ir.Node
    60  		if call.Op() == ir.OCALLFUNC {
    61  			// Evaluate callee function expression.
    62  			calleeK := e.discardHole()
    63  			if fn == nil { // unknown callee
    64  				for _, k := range ks {
    65  					if k.dst != &e.blankLoc {
    66  						// The results flow somewhere, but we don't statically
    67  						// know the callee function. If a closure flows here, we
    68  						// need to conservatively assume its results might flow to
    69  						// the heap.
    70  						calleeK = e.calleeHole().note(call, "callee operand")
    71  						break
    72  					}
    73  				}
    74  			}
    75  			e.expr(calleeK, call.Fun)
    76  		} else {
    77  			recvArg = call.Fun.(*ir.SelectorExpr).X
    78  		}
    79  
    80  		// argumentParam handles escape analysis of assigning a call
    81  		// argument to its corresponding parameter.
    82  		argumentParam := func(param *types.Field, arg ir.Node) {
    83  			e.rewriteArgument(arg, call, fn)
    84  			argument(e.tagHole(ks, fn, param), arg)
    85  		}
    86  
    87  		// internal/abi.EscapeNonString forces its argument to be on
    88  		// the heap, if it contains a non-string pointer.
    89  		// This is used in hash/maphash.Comparable, where we cannot
    90  		// hash pointers to local variables, as the address of the
    91  		// local variable might change on stack growth.
    92  		// Strings are okay as the hash depends on only the content,
    93  		// not the pointer.
    94  		// This is also used in unique.clone, to model the data flow
    95  		// edge on the value with strings excluded, because strings
    96  		// are cloned (by content).
    97  		// The actual call we match is
    98  		//   internal/abi.EscapeNonString[go.shape.T](dict, go.shape.T)
    99  		if fn != nil && fn.Sym().Pkg.Path == "internal/abi" && strings.HasPrefix(fn.Sym().Name, "EscapeNonString[") {
   100  			ps := fntype.Params()
   101  			if len(ps) == 2 && ps[1].Type.IsShape() {
   102  				if !hasNonStringPointers(ps[1].Type) {
   103  					argumentParam = func(param *types.Field, arg ir.Node) {
   104  						argument(e.discardHole(), arg)
   105  					}
   106  				} else {
   107  					argumentParam = func(param *types.Field, arg ir.Node) {
   108  						argument(e.heapHole(), arg)
   109  					}
   110  				}
   111  			}
   112  		}
   113  
   114  		args := call.Args
   115  		if recvParam := fntype.Recv(); recvParam != nil {
   116  			if recvArg == nil {
   117  				// Function call using method expression. Receiver argument is
   118  				// at the front of the regular arguments list.
   119  				recvArg, args = args[0], args[1:]
   120  			}
   121  
   122  			argumentParam(recvParam, recvArg)
   123  		}
   124  
   125  		for i, param := range fntype.Params() {
   126  			argumentParam(param, args[i])
   127  		}
   128  
   129  	case ir.OINLCALL:
   130  		call := call.(*ir.InlinedCallExpr)
   131  		e.stmts(call.Body)
   132  		for i, result := range call.ReturnVars {
   133  			k := e.discardHole()
   134  			if ks != nil {
   135  				k = ks[i]
   136  			}
   137  			e.expr(k, result)
   138  		}
   139  
   140  	case ir.OAPPEND:
   141  		call := call.(*ir.CallExpr)
   142  		args := call.Args
   143  
   144  		// Appendee slice may flow directly to the result, if
   145  		// it has enough capacity. Alternatively, a new heap
   146  		// slice might be allocated, and all slice elements
   147  		// might flow to heap.
   148  		appendeeK := e.teeHole(ks[0], e.mutatorHole())
   149  		if args[0].Type().Elem().HasPointers() {
   150  			appendeeK = e.teeHole(appendeeK, e.heapHole().deref(call, "appendee slice"))
   151  		}
   152  		argument(appendeeK, args[0])
   153  
   154  		if call.IsDDD {
   155  			appendedK := e.discardHole()
   156  			if args[1].Type().IsSlice() && args[1].Type().Elem().HasPointers() {
   157  				appendedK = e.heapHole().deref(call, "appended slice...")
   158  			}
   159  			argument(appendedK, args[1])
   160  		} else {
   161  			for i := 1; i < len(args); i++ {
   162  				argument(e.heapHole(), args[i])
   163  			}
   164  		}
   165  		e.discard(call.RType)
   166  
   167  		// Model the new backing store that might be allocated by append.
   168  		// Its address flows to the result.
   169  		// Users of escape analysis can look at the escape information for OAPPEND
   170  		// and use that to decide where to allocate the backing store.
   171  		backingStore := e.spill(ks[0], call)
   172  		// As we have a boolean to prevent reuse, we can treat these allocations as outside any loops.
   173  		backingStore.dst.loopDepth = 0
   174  
   175  	case ir.OCOPY:
   176  		call := call.(*ir.BinaryExpr)
   177  		argument(e.mutatorHole(), call.X)
   178  
   179  		copiedK := e.discardHole()
   180  		if call.Y.Type().IsSlice() && call.Y.Type().Elem().HasPointers() {
   181  			copiedK = e.heapHole().deref(call, "copied slice")
   182  		}
   183  		argument(copiedK, call.Y)
   184  		e.discard(call.RType)
   185  
   186  	case ir.OPANIC:
   187  		call := call.(*ir.UnaryExpr)
   188  		argument(e.heapHole(), call.X)
   189  
   190  	case ir.OCOMPLEX:
   191  		call := call.(*ir.BinaryExpr)
   192  		e.discard(call.X)
   193  		e.discard(call.Y)
   194  
   195  	case ir.ODELETE, ir.OPRINT, ir.OPRINTLN, ir.ORECOVERFP:
   196  		call := call.(*ir.CallExpr)
   197  		for _, arg := range call.Args {
   198  			e.discard(arg)
   199  		}
   200  		e.discard(call.RType)
   201  
   202  	case ir.OMIN, ir.OMAX:
   203  		call := call.(*ir.CallExpr)
   204  		for _, arg := range call.Args {
   205  			argument(ks[0], arg)
   206  		}
   207  		e.discard(call.RType)
   208  
   209  	case ir.OLEN, ir.OCAP, ir.OREAL, ir.OIMAG, ir.OCLOSE:
   210  		call := call.(*ir.UnaryExpr)
   211  		e.discard(call.X)
   212  
   213  	case ir.OCLEAR:
   214  		call := call.(*ir.UnaryExpr)
   215  		argument(e.mutatorHole(), call.X)
   216  
   217  	case ir.OUNSAFESTRINGDATA, ir.OUNSAFESLICEDATA:
   218  		call := call.(*ir.UnaryExpr)
   219  		argument(ks[0], call.X)
   220  
   221  	case ir.OUNSAFEADD, ir.OUNSAFESLICE, ir.OUNSAFESTRING:
   222  		call := call.(*ir.BinaryExpr)
   223  		argument(ks[0], call.X)
   224  		e.discard(call.Y)
   225  		e.discard(call.RType)
   226  	}
   227  }
   228  
   229  // goDeferStmt analyzes a "go" or "defer" statement.
   230  func (e *escape) goDeferStmt(n *ir.GoDeferStmt) {
   231  	k := e.heapHole()
   232  	if n.Op() == ir.ODEFER && e.loopDepth == 1 && n.DeferAt == nil {
   233  		// Top-level defer arguments don't escape to the heap,
   234  		// but they do need to last until they're invoked.
   235  		k = e.later(e.discardHole())
   236  
   237  		// force stack allocation of defer record, unless
   238  		// open-coded defers are used (see ssa.go)
   239  		n.SetEsc(ir.EscNever)
   240  	}
   241  
   242  	// If the function is already a zero argument/result function call,
   243  	// just escape analyze it normally.
   244  	//
   245  	// Note that the runtime is aware of this optimization for
   246  	// "go" statements that start in reflect.makeFuncStub or
   247  	// reflect.methodValueCall.
   248  
   249  	call, ok := n.Call.(*ir.CallExpr)
   250  	if !ok || call.Op() != ir.OCALLFUNC {
   251  		base.FatalfAt(n.Pos(), "expected function call: %v", n.Call)
   252  	}
   253  	if sig := call.Fun.Type(); sig.NumParams()+sig.NumResults() != 0 {
   254  		base.FatalfAt(n.Pos(), "expected signature without parameters or results: %v", sig)
   255  	}
   256  
   257  	if clo, ok := call.Fun.(*ir.ClosureExpr); ok && n.Op() == ir.OGO {
   258  		clo.IsGoWrap = true
   259  	}
   260  
   261  	e.expr(k, call.Fun)
   262  }
   263  
   264  // rewriteArgument rewrites the argument arg of the given call expression.
   265  // fn is the static callee function, if known.
   266  func (e *escape) rewriteArgument(arg ir.Node, call *ir.CallExpr, fn *ir.Name) {
   267  	if fn == nil || fn.Func == nil {
   268  		return
   269  	}
   270  	pragma := fn.Func.Pragma
   271  	if pragma&(ir.UintptrKeepAlive|ir.UintptrEscapes) == 0 {
   272  		return
   273  	}
   274  
   275  	// unsafeUintptr rewrites "uintptr(ptr)" arguments to syscall-like
   276  	// functions, so that ptr is kept alive and/or escaped as
   277  	// appropriate. unsafeUintptr also reports whether it modified arg0.
   278  	unsafeUintptr := func(arg ir.Node) {
   279  		// If the argument is really a pointer being converted to uintptr,
   280  		// arrange for the pointer to be kept alive until the call
   281  		// returns, by copying it into a temp and marking that temp still
   282  		// alive when we pop the temp stack.
   283  		conv, ok := arg.(*ir.ConvExpr)
   284  		if !ok || conv.Op() != ir.OCONVNOP {
   285  			return // not a conversion
   286  		}
   287  		if !conv.X.Type().IsUnsafePtr() || !conv.Type().IsUintptr() {
   288  			return // not an unsafe.Pointer->uintptr conversion
   289  		}
   290  
   291  		// Create and declare a new pointer-typed temp variable.
   292  		//
   293  		// TODO(mdempsky): This potentially violates the Go spec's order
   294  		// of evaluations, by evaluating arg.X before any other
   295  		// operands.
   296  		tmp := e.copyExpr(conv.Pos(), conv.X, call.PtrInit())
   297  		conv.X = tmp
   298  
   299  		k := e.mutatorHole()
   300  		if pragma&ir.UintptrEscapes != 0 {
   301  			k = e.heapHole().note(conv, "//go:uintptrescapes")
   302  		}
   303  		e.flow(k, e.oldLoc(tmp))
   304  
   305  		if pragma&ir.UintptrKeepAlive != 0 {
   306  			tmp.SetAddrtaken(true) // ensure SSA keeps the tmp variable
   307  			call.KeepAlive = append(call.KeepAlive, tmp)
   308  		}
   309  	}
   310  
   311  	// For variadic functions, the compiler has already rewritten:
   312  	//
   313  	//     f(a, b, c)
   314  	//
   315  	// to:
   316  	//
   317  	//     f([]T{a, b, c}...)
   318  	//
   319  	// So we need to look into slice elements to handle uintptr(ptr)
   320  	// arguments to variadic syscall-like functions correctly.
   321  	if arg.Op() == ir.OSLICELIT {
   322  		list := arg.(*ir.CompLitExpr).List
   323  		for _, el := range list {
   324  			if el.Op() == ir.OKEY {
   325  				el = el.(*ir.KeyExpr).Value
   326  			}
   327  			unsafeUintptr(el)
   328  		}
   329  	} else {
   330  		unsafeUintptr(arg)
   331  	}
   332  }
   333  
   334  // copyExpr creates and returns a new temporary variable within fn;
   335  // appends statements to init to declare and initialize it to expr;
   336  // and escape analyzes the data flow.
   337  func (e *escape) copyExpr(pos src.XPos, expr ir.Node, init *ir.Nodes) *ir.Name {
   338  	if ir.HasUniquePos(expr) {
   339  		pos = expr.Pos()
   340  	}
   341  
   342  	tmp := typecheck.TempAt(pos, e.curfn, expr.Type())
   343  
   344  	stmts := []ir.Node{
   345  		ir.NewDecl(pos, ir.ODCL, tmp),
   346  		ir.NewAssignStmt(pos, tmp, expr),
   347  	}
   348  	typecheck.Stmts(stmts)
   349  	init.Append(stmts...)
   350  
   351  	e.newLoc(tmp, true)
   352  	e.stmts(stmts)
   353  
   354  	return tmp
   355  }
   356  
   357  // tagHole returns a hole for evaluating an argument passed to param.
   358  // ks should contain the holes representing where the function
   359  // callee's results flows. fn is the statically-known callee function,
   360  // if any.
   361  func (e *escape) tagHole(ks []hole, fn *ir.Name, param *types.Field) hole {
   362  	// If this is a dynamic call, we can't rely on param.Note.
   363  	if fn == nil {
   364  		return e.heapHole()
   365  	}
   366  
   367  	if e.inMutualBatch(fn) {
   368  		if param.Nname == nil {
   369  			return e.discardHole()
   370  		}
   371  		return e.addr(param.Nname.(*ir.Name))
   372  	}
   373  
   374  	// Call to previously tagged function.
   375  
   376  	var tagKs []hole
   377  	esc := parseLeaks(param.Note)
   378  
   379  	if x := esc.Heap(); x >= 0 {
   380  		tagKs = append(tagKs, e.heapHole().shift(x))
   381  	}
   382  	if x := esc.Mutator(); x >= 0 {
   383  		tagKs = append(tagKs, e.mutatorHole().shift(x))
   384  	}
   385  	if x := esc.Callee(); x >= 0 {
   386  		tagKs = append(tagKs, e.calleeHole().shift(x))
   387  	}
   388  
   389  	if ks != nil {
   390  		for i := 0; i < numEscResults; i++ {
   391  			if x := esc.Result(i); x >= 0 {
   392  				tagKs = append(tagKs, ks[i].shift(x))
   393  			}
   394  		}
   395  	}
   396  
   397  	return e.teeHole(tagKs...)
   398  }
   399  
   400  func hasNonStringPointers(t *types.Type) bool {
   401  	if !t.HasPointers() {
   402  		return false
   403  	}
   404  	switch t.Kind() {
   405  	case types.TSTRING:
   406  		return false
   407  	case types.TSTRUCT:
   408  		for _, f := range t.Fields() {
   409  			if hasNonStringPointers(f.Type) {
   410  				return true
   411  			}
   412  		}
   413  		return false
   414  	case types.TARRAY:
   415  		return hasNonStringPointers(t.Elem())
   416  	}
   417  	return true
   418  }
   419  

View as plain text