2011-08-24 11:07:08 -06:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//
|
2011-08-28 10:05:00 -06:00
|
|
|
// Escape analysis.
|
2011-08-24 11:07:08 -06:00
|
|
|
//
|
2011-08-24 14:47:32 -06:00
|
|
|
// First escfunc, esc and escassign recurse over the ast of each
|
2011-08-24 11:07:08 -06:00
|
|
|
// function to dig out flow(dst,src) edges between any
|
|
|
|
// pointer-containing nodes and store them in dst->escflowsrc. For
|
|
|
|
// variables assigned to a variable in an outer scope or used as a
|
|
|
|
// return value, they store a flow(theSink, src) edge to a fake node
|
2011-08-28 10:05:00 -06:00
|
|
|
// 'the Sink'. For variables referenced in closures, an edge
|
2011-08-24 11:07:08 -06:00
|
|
|
// flow(closure, &var) is recorded and the flow of a closure itself to
|
|
|
|
// an outer scope is tracked the same way as other variables.
|
|
|
|
//
|
|
|
|
// Then escflood walks the graph starting at theSink and tags all
|
|
|
|
// variables of it can reach an & node as escaping and all function
|
|
|
|
// parameters it can reach as leaking.
|
|
|
|
//
|
2011-08-28 10:05:00 -06:00
|
|
|
// If a value's address is taken but the address does not escape,
|
|
|
|
// then the value can stay on the stack. If the value new(T) does
|
|
|
|
// not escape, then new(T) can be rewritten into a stack allocation.
|
|
|
|
// The same is true of slice literals.
|
2011-08-24 11:07:08 -06:00
|
|
|
//
|
2011-08-28 10:05:00 -06:00
|
|
|
// If escape analysis is disabled (-s), this code is not used.
|
|
|
|
// Instead, the compiler assumes that any value whose address
|
|
|
|
// is taken without being immediately dereferenced
|
|
|
|
// needs to be moved to the heap, and new(T) and slice
|
|
|
|
// literals are always real allocations.
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-25 14:25:10 -06:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
2011-08-24 11:07:08 -06:00
|
|
|
#include "go.h"
|
|
|
|
|
|
|
|
static void escfunc(Node *func);
|
2011-08-24 14:47:32 -06:00
|
|
|
static void esclist(NodeList *l);
|
|
|
|
static void esc(Node *n);
|
|
|
|
static void escassign(Node *dst, Node *src);
|
|
|
|
static void esccall(Node*);
|
|
|
|
static void escflows(Node *dst, Node *src);
|
2011-08-24 11:07:08 -06:00
|
|
|
static void escflood(Node *dst);
|
|
|
|
static void escwalk(int level, Node *dst, Node *src);
|
|
|
|
static void esctag(Node *func);
|
|
|
|
|
|
|
|
// Fake node that all
|
|
|
|
// - return values and output variables
|
|
|
|
// - parameters on imported functions not marked 'safe'
|
|
|
|
// - assignments to global variables
|
|
|
|
// flow to.
|
|
|
|
static Node theSink;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
static NodeList* dsts; // all dst nodes
|
2011-08-24 11:07:08 -06:00
|
|
|
static int loopdepth; // for detecting nested loop scopes
|
|
|
|
static int pdepth; // for debug printing in recursions.
|
|
|
|
static Strlit* safetag; // gets slapped on safe parameters' field types for export
|
|
|
|
static int dstcount, edgecount; // diagnostic
|
2011-08-28 10:05:00 -06:00
|
|
|
static NodeList* noesc; // list of possible non-escaping nodes, for printing
|
2011-08-24 11:07:08 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
escapes(void)
|
|
|
|
{
|
|
|
|
NodeList *l;
|
|
|
|
|
|
|
|
theSink.op = ONAME;
|
|
|
|
theSink.class = PEXTERN;
|
|
|
|
theSink.sym = lookup(".sink");
|
|
|
|
theSink.escloopdepth = -1;
|
|
|
|
|
|
|
|
safetag = strlit("noescape");
|
|
|
|
|
|
|
|
// flow-analyze top level functions
|
|
|
|
for(l=xtop; l; l=l->next)
|
|
|
|
if(l->n->op == ODCLFUNC || l->n->op == OCLOSURE)
|
|
|
|
escfunc(l->n);
|
|
|
|
|
|
|
|
// print("escapes: %d dsts, %d edges\n", dstcount, edgecount);
|
|
|
|
|
|
|
|
// visit the updstream of each dst, mark address nodes with
|
|
|
|
// addrescapes, mark parameters unsafe
|
2011-08-24 14:47:32 -06:00
|
|
|
for(l = dsts; l; l=l->next)
|
2011-08-24 11:07:08 -06:00
|
|
|
escflood(l->n);
|
|
|
|
|
|
|
|
// for all top level functions, tag the typenodes corresponding to the param nodes
|
|
|
|
for(l=xtop; l; l=l->next)
|
|
|
|
if(l->n->op == ODCLFUNC)
|
|
|
|
esctag(l->n);
|
2011-08-28 10:05:00 -06:00
|
|
|
|
|
|
|
if(debug['m']) {
|
|
|
|
for(l=noesc; l; l=l->next)
|
|
|
|
if(l->n->esc == EscNone)
|
|
|
|
warnl(l->n->lineno, "%S %#N does not escape",
|
|
|
|
(l->n->curfn && l->n->curfn->nname) ? l->n->curfn->nname->sym : S,
|
|
|
|
l->n);
|
|
|
|
}
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
|
|
|
|
2011-08-28 10:05:00 -06:00
|
|
|
|
2011-08-24 11:07:08 -06:00
|
|
|
static void
|
|
|
|
escfunc(Node *func)
|
|
|
|
{
|
|
|
|
Node *savefn, *n;
|
|
|
|
NodeList *ll;
|
|
|
|
int saveld;
|
|
|
|
|
|
|
|
saveld = loopdepth;
|
|
|
|
loopdepth = 1;
|
|
|
|
savefn = curfn;
|
|
|
|
curfn = func;
|
|
|
|
|
|
|
|
for(ll=curfn->dcl; ll; ll=ll->next) {
|
|
|
|
if(ll->n->op != ONAME)
|
|
|
|
continue;
|
|
|
|
switch (ll->n->class) {
|
|
|
|
case PPARAMOUT:
|
|
|
|
// output parameters flow to the sink
|
|
|
|
escflows(&theSink, ll->n);
|
|
|
|
ll->n->escloopdepth = loopdepth;
|
|
|
|
break;
|
|
|
|
case PPARAM:
|
2011-08-28 10:05:00 -06:00
|
|
|
if(ll->n->type && !haspointers(ll->n->type))
|
|
|
|
break;
|
2011-08-24 11:07:08 -06:00
|
|
|
ll->n->esc = EscNone; // prime for escflood later
|
2011-08-28 10:05:00 -06:00
|
|
|
noesc = list(noesc, ll->n);
|
2011-08-24 11:07:08 -06:00
|
|
|
ll->n->escloopdepth = loopdepth;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// walk will take the address of cvar->closure later and assign it to cvar.
|
|
|
|
// handle that here by linking a fake oaddr node directly to the closure.
|
2011-08-24 14:47:32 -06:00
|
|
|
for(ll=curfn->cvars; ll; ll=ll->next) {
|
2011-08-24 11:07:08 -06:00
|
|
|
if(ll->n->op == OXXX) // see dcl.c:398
|
|
|
|
continue;
|
|
|
|
|
|
|
|
n = nod(OADDR, ll->n->closure, N);
|
|
|
|
n->lineno = ll->n->lineno;
|
|
|
|
typecheck(&n, Erv);
|
2011-08-24 14:47:32 -06:00
|
|
|
escassign(curfn, n);
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
esclist(curfn->nbody);
|
2011-08-24 11:07:08 -06:00
|
|
|
curfn = savefn;
|
|
|
|
loopdepth = saveld;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-24 14:47:32 -06:00
|
|
|
esclist(NodeList *l)
|
2011-08-24 11:07:08 -06:00
|
|
|
{
|
2011-08-24 14:47:32 -06:00
|
|
|
for(; l; l=l->next)
|
|
|
|
esc(l->n);
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-24 14:47:32 -06:00
|
|
|
esc(Node *n)
|
2011-08-24 11:07:08 -06:00
|
|
|
{
|
2011-08-24 14:47:32 -06:00
|
|
|
int lno;
|
2011-08-28 10:05:00 -06:00
|
|
|
NodeList *ll, *lr;
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
if(n == N)
|
2011-08-24 11:07:08 -06:00
|
|
|
return;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
lno = setlineno(n);
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-25 07:26:13 -06:00
|
|
|
if(n->op == OFOR || n->op == ORANGE)
|
2011-08-24 14:47:32 -06:00
|
|
|
loopdepth++;
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-28 10:05:00 -06:00
|
|
|
esc(n->left);
|
|
|
|
esc(n->right);
|
2011-08-24 14:47:32 -06:00
|
|
|
esc(n->ntest);
|
|
|
|
esc(n->nincr);
|
2011-08-28 10:05:00 -06:00
|
|
|
esclist(n->ninit);
|
2011-08-24 14:47:32 -06:00
|
|
|
esclist(n->nbody);
|
2011-08-28 10:05:00 -06:00
|
|
|
esclist(n->nelse);
|
|
|
|
esclist(n->list);
|
|
|
|
esclist(n->rlist);
|
2011-08-24 14:47:32 -06:00
|
|
|
|
2011-08-25 07:26:13 -06:00
|
|
|
if(n->op == OFOR || n->op == ORANGE)
|
2011-08-24 14:47:32 -06:00
|
|
|
loopdepth--;
|
2011-08-24 11:07:08 -06:00
|
|
|
|
|
|
|
if(debug['m'] > 1)
|
2011-08-24 14:47:32 -06:00
|
|
|
print("%L:[%d] %#S esc: %#N\n", lineno, loopdepth,
|
|
|
|
(curfn && curfn->nname) ? curfn->nname->sym : S, n);
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
switch(n->op) {
|
2011-08-24 11:07:08 -06:00
|
|
|
case ODCL:
|
2011-08-25 07:26:13 -06:00
|
|
|
// Record loop depth at declaration.
|
2011-08-24 14:47:32 -06:00
|
|
|
if(n->left)
|
|
|
|
n->left->escloopdepth = loopdepth;
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OLABEL: // TODO: new loop/scope only if there are backjumps to it.
|
|
|
|
loopdepth++;
|
|
|
|
break;
|
|
|
|
|
2011-08-25 07:26:13 -06:00
|
|
|
case ORANGE:
|
|
|
|
// Everything but fixed array is a dereference.
|
2011-08-28 10:05:00 -06:00
|
|
|
if(isfixedarray(n->type) && n->list->next)
|
2011-08-25 07:26:13 -06:00
|
|
|
escassign(n->list->next->n, n->right);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OSWITCH:
|
2011-08-24 14:47:32 -06:00
|
|
|
if(n->ntest && n->ntest->op == OTYPESW) {
|
|
|
|
for(ll=n->list; ll; ll=ll->next) { // cases
|
2011-08-24 11:07:08 -06:00
|
|
|
// ntest->right is the argument of the .(type),
|
|
|
|
// ll->n->nname is the variable per case
|
2011-08-24 14:47:32 -06:00
|
|
|
escassign(ll->n->nname, n->ntest->right);
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OAS:
|
|
|
|
case OASOP:
|
2011-08-24 14:47:32 -06:00
|
|
|
escassign(n->left, n->right);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OAS2: // x,y = a,b
|
2011-08-24 14:47:32 -06:00
|
|
|
if(count(n->list) == count(n->rlist))
|
|
|
|
for(ll=n->list, lr=n->rlist; ll; ll=ll->next, lr=lr->next)
|
|
|
|
escassign(ll->n, lr->n);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OAS2RECV: // v, ok = <-ch
|
|
|
|
case OAS2MAPR: // v, ok = m[k]
|
|
|
|
case OAS2DOTTYPE: // v, ok = x.(type)
|
2011-08-24 14:47:32 -06:00
|
|
|
case OAS2MAPW: // m[k] = x, ok
|
|
|
|
escassign(n->list->n, n->rlist->n);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case OSEND: // ch <- x
|
2011-08-25 07:26:13 -06:00
|
|
|
escassign(&theSink, n->right);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case ODEFER:
|
|
|
|
if(loopdepth == 1) // top level
|
|
|
|
break;
|
|
|
|
// arguments leak out of scope
|
|
|
|
// TODO: leak to a dummy node instead
|
|
|
|
// fallthrough
|
|
|
|
case OPROC:
|
|
|
|
// go f(x) - f and x escape
|
|
|
|
escassign(&theSink, n->left->left);
|
|
|
|
for(ll=n->left->list; ll; ll=ll->next)
|
|
|
|
escassign(&theSink, ll->n);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case ORETURN:
|
|
|
|
for(ll=n->list; ll; ll=ll->next)
|
|
|
|
escassign(&theSink, ll->n);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case OPANIC:
|
|
|
|
// Argument could leak through recover.
|
|
|
|
escassign(&theSink, n->left);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case OAPPEND:
|
2011-08-25 07:26:13 -06:00
|
|
|
if(!n->isddd)
|
|
|
|
for(ll=n->list->next; ll; ll=ll->next)
|
|
|
|
escassign(&theSink, ll->n); // lose track of assign to dereference
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case OCALLMETH:
|
|
|
|
case OCALLFUNC:
|
|
|
|
case OCALLINTER:
|
|
|
|
esccall(n);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
2011-08-28 10:05:00 -06:00
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case OCONV:
|
|
|
|
case OCONVNOP:
|
|
|
|
case OCONVIFACE:
|
|
|
|
escassign(n, n->left);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
2011-08-28 10:05:00 -06:00
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case OARRAYLIT:
|
2011-08-28 10:05:00 -06:00
|
|
|
if(isslice(n->type)) {
|
|
|
|
n->esc = EscNone; // until proven otherwise
|
|
|
|
noesc = list(noesc, n);
|
|
|
|
n->escloopdepth = loopdepth;
|
|
|
|
// Values make it to memory, lose track.
|
|
|
|
for(ll=n->list; ll; ll=ll->next)
|
|
|
|
escassign(&theSink, ll->n->right);
|
|
|
|
} else {
|
|
|
|
// Link values to array.
|
|
|
|
for(ll=n->list; ll; ll=ll->next)
|
|
|
|
escassign(n, ll->n->right);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case OSTRUCTLIT:
|
2011-08-28 10:05:00 -06:00
|
|
|
// Link values to struct.
|
|
|
|
for(ll=n->list; ll; ll=ll->next)
|
|
|
|
escassign(n, ll->n->right);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
2011-08-28 10:05:00 -06:00
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
case OMAPLIT:
|
2011-08-28 10:05:00 -06:00
|
|
|
n->esc = EscNone; // until proven otherwise
|
|
|
|
noesc = list(noesc, n);
|
|
|
|
n->escloopdepth = loopdepth;
|
|
|
|
// Keys and values make it to memory, lose track.
|
|
|
|
for(ll=n->list; ll; ll=ll->next) {
|
|
|
|
escassign(&theSink, ll->n->left);
|
|
|
|
escassign(&theSink, ll->n->right);
|
2011-08-24 14:47:32 -06:00
|
|
|
}
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
2011-08-28 10:05:00 -06:00
|
|
|
|
|
|
|
case OADDR:
|
|
|
|
case OCLOSURE:
|
|
|
|
case OMAKECHAN:
|
|
|
|
case OMAKEMAP:
|
|
|
|
case OMAKESLICE:
|
|
|
|
case ONEW:
|
|
|
|
n->escloopdepth = loopdepth;
|
|
|
|
n->esc = EscNone; // until proven otherwise
|
|
|
|
noesc = list(noesc, n);
|
|
|
|
break;
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
lineno = lno;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that expr somehow gets assigned to dst, if non nil. for
|
|
|
|
// dst==nil, any name node expr still must be marked as being
|
|
|
|
// evaluated in curfn. For expr==nil, dst must still be examined for
|
|
|
|
// evaluations inside it (e.g *f(x) = y)
|
|
|
|
static void
|
2011-08-24 14:47:32 -06:00
|
|
|
escassign(Node *dst, Node *src)
|
2011-08-24 11:07:08 -06:00
|
|
|
{
|
|
|
|
int lno;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
if(isblank(dst) || dst == N || src == N || src->op == ONONAME || src->op == OXXX)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(debug['m'] > 1)
|
|
|
|
print("%L:[%d] %#S escassign: %hN = %hN\n", lineno, loopdepth,
|
|
|
|
(curfn && curfn->nname) ? curfn->nname->sym : S, dst, src);
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
setlineno(dst);
|
2011-08-25 07:26:13 -06:00
|
|
|
|
|
|
|
// Analyze lhs of assignment.
|
|
|
|
// Replace dst with theSink if we can't track it.
|
2011-08-24 14:47:32 -06:00
|
|
|
switch(dst->op) {
|
2011-08-25 07:26:13 -06:00
|
|
|
default:
|
|
|
|
dump("dst", dst);
|
|
|
|
fatal("escassign: unexpected dst");
|
|
|
|
|
|
|
|
case OARRAYLIT:
|
|
|
|
case OCLOSURE:
|
|
|
|
case OCONV:
|
|
|
|
case OCONVIFACE:
|
|
|
|
case OCONVNOP:
|
|
|
|
case OMAPLIT:
|
|
|
|
case OSTRUCTLIT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ONAME:
|
|
|
|
if(dst->class == PEXTERN)
|
|
|
|
dst = &theSink;
|
|
|
|
break;
|
2011-08-24 14:47:32 -06:00
|
|
|
case ODOT: // treat "dst.x = src" as "dst = src"
|
|
|
|
escassign(dst->left, src);
|
2011-08-24 11:07:08 -06:00
|
|
|
return;
|
2011-08-25 07:26:13 -06:00
|
|
|
case OINDEX:
|
|
|
|
if(isfixedarray(dst->left->type)) {
|
|
|
|
escassign(dst->left, src);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dst = &theSink; // lose track of dereference
|
|
|
|
break;
|
2011-08-24 14:47:32 -06:00
|
|
|
case OIND:
|
|
|
|
case ODOTPTR:
|
2011-08-25 07:26:13 -06:00
|
|
|
dst = &theSink; // lose track of dereference
|
|
|
|
break;
|
|
|
|
case OINDEXMAP:
|
|
|
|
// lose track of key and value
|
|
|
|
escassign(&theSink, dst->right);
|
|
|
|
dst = &theSink;
|
|
|
|
break;
|
2011-08-24 14:47:32 -06:00
|
|
|
}
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
lno = setlineno(src);
|
2011-08-24 11:07:08 -06:00
|
|
|
pdepth++;
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
switch(src->op) {
|
2011-08-24 11:07:08 -06:00
|
|
|
case OADDR: // dst = &x
|
|
|
|
case OIND: // dst = *x
|
|
|
|
case ODOTPTR: // dst = (*x).f
|
|
|
|
case ONAME:
|
|
|
|
case OPARAM:
|
2011-08-28 10:05:00 -06:00
|
|
|
case ODDDARG:
|
|
|
|
case OARRAYLIT:
|
|
|
|
case OMAPLIT:
|
|
|
|
case OSTRUCTLIT:
|
2011-08-24 11:07:08 -06:00
|
|
|
// loopdepth was set in the defining statement or function header
|
2011-08-24 14:47:32 -06:00
|
|
|
escflows(dst, src);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCONV:
|
|
|
|
case OCONVIFACE:
|
|
|
|
case OCONVNOP:
|
|
|
|
case ODOT:
|
|
|
|
case ODOTTYPE:
|
|
|
|
case ODOTTYPE2:
|
2011-08-25 07:26:13 -06:00
|
|
|
case OSLICE:
|
|
|
|
case OSLICEARR:
|
2011-08-24 14:47:32 -06:00
|
|
|
// Conversions, field access, slice all preserve the input value.
|
|
|
|
escassign(dst, src->left);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
2011-08-25 07:26:13 -06:00
|
|
|
case OAPPEND:
|
|
|
|
// Append returns first argument.
|
|
|
|
escassign(dst, src->list->n);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OINDEX:
|
|
|
|
// Index of array preserves input value.
|
|
|
|
if(isfixedarray(src->left->type))
|
|
|
|
escassign(dst, src->left);
|
|
|
|
break;
|
|
|
|
|
2011-08-24 11:07:08 -06:00
|
|
|
case OMAKECHAN:
|
|
|
|
case OMAKEMAP:
|
|
|
|
case OMAKESLICE:
|
|
|
|
case ONEW:
|
2011-08-24 14:47:32 -06:00
|
|
|
escflows(dst, src);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLOSURE:
|
2011-08-24 14:47:32 -06:00
|
|
|
escflows(dst, src);
|
|
|
|
escfunc(src);
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
2011-08-28 10:05:00 -06:00
|
|
|
|
|
|
|
case OADD:
|
|
|
|
case OSUB:
|
|
|
|
case OOR:
|
|
|
|
case OXOR:
|
|
|
|
case OMUL:
|
|
|
|
case ODIV:
|
|
|
|
case OMOD:
|
|
|
|
case OLSH:
|
|
|
|
case ORSH:
|
|
|
|
case OAND:
|
|
|
|
case OANDNOT:
|
|
|
|
case OPLUS:
|
|
|
|
case OMINUS:
|
|
|
|
case OCOM:
|
|
|
|
// Might be pointer arithmetic, in which case
|
|
|
|
// the operands flow into the result.
|
|
|
|
// TODO(rsc): Decide what the story is here. This is unsettling.
|
|
|
|
escassign(dst, src->left);
|
|
|
|
escassign(dst, src->right);
|
|
|
|
break;
|
|
|
|
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pdepth--;
|
|
|
|
lineno = lno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
// This is a bit messier than fortunate, pulled out of escassign's big
|
2011-08-24 11:07:08 -06:00
|
|
|
// switch for clarity. We either have the paramnodes, which may be
|
|
|
|
// connected to other things throug flows or we have the parameter type
|
|
|
|
// nodes, which may be marked 'n(ofloworescape)'. Navigating the ast is slightly
|
|
|
|
// different for methods vs plain functions and for imported vs
|
|
|
|
// this-package
|
|
|
|
static void
|
2011-08-24 14:47:32 -06:00
|
|
|
esccall(Node *n)
|
2011-08-24 11:07:08 -06:00
|
|
|
{
|
|
|
|
NodeList *ll, *lr;
|
2011-08-28 10:05:00 -06:00
|
|
|
Node *a, *fn, *src;
|
2011-08-24 14:47:32 -06:00
|
|
|
Type *t, *fntype;
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
fn = N;
|
|
|
|
switch(n->op) {
|
|
|
|
default:
|
|
|
|
fatal("esccall");
|
2011-08-24 11:07:08 -06:00
|
|
|
|
|
|
|
case OCALLFUNC:
|
2011-08-24 14:47:32 -06:00
|
|
|
fn = n->left;
|
2011-08-24 11:07:08 -06:00
|
|
|
fntype = fn->type;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCALLMETH:
|
2011-08-24 14:47:32 -06:00
|
|
|
fn = n->left->right->sym->def;
|
2011-08-24 11:07:08 -06:00
|
|
|
if(fn)
|
|
|
|
fntype = fn->type;
|
|
|
|
else
|
2011-08-24 14:47:32 -06:00
|
|
|
fntype = n->left->type;
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OCALLINTER:
|
2011-08-24 14:47:32 -06:00
|
|
|
fntype = n->left->type;
|
2011-08-24 11:07:08 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
ll = n->list;
|
|
|
|
if(n->list != nil && n->list->next == nil) {
|
|
|
|
a = n->list->n;
|
|
|
|
if(a->type->etype == TSTRUCT && a->type->funarg) {
|
|
|
|
// f(g()).
|
|
|
|
// Since f's arguments are g's results and
|
|
|
|
// all function results escape, we're done.
|
|
|
|
ll = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-28 21:29:34 -06:00
|
|
|
if(fn && fn->op == ONAME && fn->class == PFUNC && fn->defn && fn->defn->nbody && fn->ntype) {
|
2011-08-24 14:47:32 -06:00
|
|
|
// Local function. Incorporate into flow graph.
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
// Receiver.
|
|
|
|
if(n->op != OCALLFUNC)
|
|
|
|
escassign(fn->ntype->left->left, n->left->left);
|
2011-08-24 11:07:08 -06:00
|
|
|
|
2011-08-28 10:05:00 -06:00
|
|
|
for(lr=fn->ntype->list; ll && lr; ll=ll->next, lr=lr->next) {
|
|
|
|
src = ll->n;
|
|
|
|
if(lr->n->isddd && !n->isddd) {
|
|
|
|
// Introduce ODDDARG node to represent ... allocation.
|
|
|
|
src = nod(ODDDARG, N, N);
|
|
|
|
src->escloopdepth = loopdepth;
|
|
|
|
src->lineno = n->lineno;
|
|
|
|
src->esc = EscNone; // until we find otherwise
|
|
|
|
noesc = list(noesc, src);
|
|
|
|
n->right = src;
|
|
|
|
}
|
|
|
|
if(lr->n->left != N)
|
|
|
|
escassign(lr->n->left, src);
|
|
|
|
if(src != ll->n)
|
|
|
|
break;
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
2011-08-28 10:05:00 -06:00
|
|
|
// "..." arguments are untracked
|
|
|
|
for(; ll; ll=ll->next)
|
|
|
|
escassign(&theSink, ll->n);
|
2011-08-24 11:07:08 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
// Imported function. Use the escape tags.
|
|
|
|
if(n->op != OCALLFUNC) {
|
|
|
|
t = getthisx(fntype)->type;
|
|
|
|
if(!t->note || strcmp(t->note->s, safetag->s) != 0)
|
|
|
|
escassign(&theSink, n->left->left);
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
2011-08-24 14:47:32 -06:00
|
|
|
for(t=getinargx(fntype)->type; ll; ll=ll->next) {
|
2011-08-28 10:05:00 -06:00
|
|
|
src = ll->n;
|
|
|
|
if(t->isddd && !n->isddd) {
|
|
|
|
// Introduce ODDDARG node to represent ... allocation.
|
|
|
|
src = nod(ODDDARG, N, N);
|
|
|
|
src->escloopdepth = loopdepth;
|
|
|
|
src->lineno = n->lineno;
|
|
|
|
src->esc = EscNone; // until we find otherwise
|
|
|
|
noesc = list(noesc, src);
|
|
|
|
n->right = src;
|
|
|
|
}
|
2011-08-24 14:47:32 -06:00
|
|
|
if(!t->note || strcmp(t->note->s, safetag->s) != 0)
|
2011-08-28 10:05:00 -06:00
|
|
|
escassign(&theSink, src);
|
|
|
|
if(src != ll->n)
|
|
|
|
break;
|
|
|
|
t = t->down;
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
2011-08-28 10:05:00 -06:00
|
|
|
// "..." arguments are untracked
|
|
|
|
for(; ll; ll=ll->next)
|
|
|
|
escassign(&theSink, ll->n);
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the link src->dst in dst, throwing out some quick wins.
|
|
|
|
static void
|
2011-08-24 14:47:32 -06:00
|
|
|
escflows(Node *dst, Node *src)
|
2011-08-24 11:07:08 -06:00
|
|
|
{
|
|
|
|
if(dst == nil || src == nil || dst == src)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't bother building a graph for scalars.
|
2011-08-24 14:47:32 -06:00
|
|
|
if(src->type && !haspointers(src->type))
|
2011-08-24 11:07:08 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(debug['m']>2)
|
|
|
|
print("%L::flows:: %hN <- %hN\n", lineno, dst, src);
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
if(dst->escflowsrc == nil) {
|
2011-08-24 11:07:08 -06:00
|
|
|
dsts = list(dsts, dst);
|
|
|
|
dstcount++;
|
|
|
|
}
|
|
|
|
edgecount++;
|
|
|
|
|
|
|
|
dst->escflowsrc = list(dst->escflowsrc, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whenever we hit a reference node, the level goes up by one, and whenever
|
|
|
|
// we hit an OADDR, the level goes down by one. as long as we're on a level > 0
|
|
|
|
// finding an OADDR just means we're following the upstream of a dereference,
|
|
|
|
// so this address doesn't leak (yet).
|
|
|
|
// If level == 0, it means the /value/ of this node can reach the root of this flood.
|
|
|
|
// so if this node is an OADDR, it's argument should be marked as escaping iff
|
|
|
|
// it's currfn/loopdepth are different from the flood's root.
|
|
|
|
// Once an object has been moved to the heap, all of it's upstream should be considered
|
|
|
|
// escaping to the global scope.
|
|
|
|
static void
|
|
|
|
escflood(Node *dst)
|
|
|
|
{
|
|
|
|
NodeList *l;
|
|
|
|
|
|
|
|
switch(dst->op) {
|
|
|
|
case ONAME:
|
|
|
|
case OCLOSURE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(debug['m']>1)
|
2011-08-28 10:05:00 -06:00
|
|
|
print("\nescflood:%d: dst %hN scope:%#S[%d]\n", walkgen, dst,
|
2011-08-24 11:07:08 -06:00
|
|
|
(dst->curfn && dst->curfn->nname) ? dst->curfn->nname->sym : S,
|
|
|
|
dst->escloopdepth);
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
for(l = dst->escflowsrc; l; l=l->next) {
|
2011-08-28 10:05:00 -06:00
|
|
|
walkgen++;
|
2011-08-24 11:07:08 -06:00
|
|
|
escwalk(0, dst, l->n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
escwalk(int level, Node *dst, Node *src)
|
|
|
|
{
|
2011-08-24 14:47:32 -06:00
|
|
|
NodeList *ll;
|
2011-08-24 11:07:08 -06:00
|
|
|
int leaks;
|
|
|
|
|
2011-08-28 10:05:00 -06:00
|
|
|
if(src->walkgen == walkgen)
|
2011-08-24 11:07:08 -06:00
|
|
|
return;
|
2011-08-28 10:05:00 -06:00
|
|
|
src->walkgen = walkgen;
|
2011-08-24 11:07:08 -06:00
|
|
|
|
|
|
|
if(debug['m']>1)
|
|
|
|
print("escwalk: level:%d depth:%d %.*s %hN scope:%#S[%d]\n",
|
|
|
|
level, pdepth, pdepth, "\t\t\t\t\t\t\t\t\t\t", src,
|
|
|
|
(src->curfn && src->curfn->nname) ? src->curfn->nname->sym : S, src->escloopdepth);
|
|
|
|
|
|
|
|
pdepth++;
|
|
|
|
|
|
|
|
leaks = (level <= 0) && (dst->escloopdepth < src->escloopdepth);
|
|
|
|
|
|
|
|
switch(src->op) {
|
|
|
|
case ONAME:
|
2011-08-24 14:47:32 -06:00
|
|
|
if(src->class == PPARAM && leaks && src->esc == EscNone) {
|
2011-08-24 11:07:08 -06:00
|
|
|
src->esc = EscScope;
|
|
|
|
if(debug['m'])
|
2011-08-28 10:05:00 -06:00
|
|
|
warnl(src->lineno, "leaking param: %hN", src);
|
2011-08-24 11:07:08 -06:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OADDR:
|
2011-08-28 10:05:00 -06:00
|
|
|
if(leaks) {
|
|
|
|
src->esc = EscHeap;
|
2011-08-24 11:07:08 -06:00
|
|
|
addrescapes(src->left);
|
2011-08-28 10:05:00 -06:00
|
|
|
if(debug['m'])
|
|
|
|
warnl(src->lineno, "%#N escapes to heap", src);
|
|
|
|
}
|
2011-08-24 11:07:08 -06:00
|
|
|
escwalk(level-1, dst, src->left);
|
|
|
|
break;
|
|
|
|
|
2011-08-28 10:05:00 -06:00
|
|
|
case OARRAYLIT:
|
|
|
|
if(isfixedarray(src->type))
|
|
|
|
break;
|
|
|
|
// fall through
|
|
|
|
case ODDDARG:
|
|
|
|
case OMAKECHAN:
|
|
|
|
case OMAKEMAP:
|
|
|
|
case OMAKESLICE:
|
|
|
|
case OMAPLIT:
|
|
|
|
case ONEW:
|
|
|
|
case OCLOSURE:
|
|
|
|
if(leaks) {
|
|
|
|
src->esc = EscHeap;
|
|
|
|
if(debug['m'])
|
|
|
|
warnl(src->lineno, "%#N escapes to heap", src);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-08-24 11:07:08 -06:00
|
|
|
case OINDEX:
|
|
|
|
if(isfixedarray(src->type))
|
|
|
|
break;
|
2011-08-28 10:05:00 -06:00
|
|
|
// fall through
|
2011-08-24 11:07:08 -06:00
|
|
|
case OSLICE:
|
|
|
|
case ODOTPTR:
|
|
|
|
case OINDEXMAP:
|
|
|
|
case OIND:
|
|
|
|
escwalk(level+1, dst, src->left);
|
|
|
|
}
|
|
|
|
|
2011-08-24 14:47:32 -06:00
|
|
|
for(ll=src->escflowsrc; ll; ll=ll->next)
|
2011-08-24 11:07:08 -06:00
|
|
|
escwalk(level, dst, ll->n);
|
|
|
|
|
|
|
|
pdepth--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
esctag(Node *func)
|
|
|
|
{
|
|
|
|
Node *savefn;
|
|
|
|
NodeList *ll;
|
2011-08-28 21:29:34 -06:00
|
|
|
|
|
|
|
// External functions must be assumed unsafe.
|
|
|
|
if(func->nbody == nil)
|
|
|
|
return;
|
2011-08-24 11:07:08 -06:00
|
|
|
|
|
|
|
savefn = curfn;
|
|
|
|
curfn = func;
|
|
|
|
|
|
|
|
for(ll=curfn->dcl; ll; ll=ll->next) {
|
|
|
|
if(ll->n->op != ONAME || ll->n->class != PPARAM)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (ll->n->esc) {
|
|
|
|
case EscNone: // not touched by escflood
|
2011-08-24 14:47:32 -06:00
|
|
|
if(haspointers(ll->n->type)) // don't bother tagging for scalars
|
2011-08-24 11:07:08 -06:00
|
|
|
ll->n->paramfld->note = safetag;
|
|
|
|
case EscHeap: // touched by escflood, moved to heap
|
|
|
|
case EscScope: // touched by escflood, value leaves scope
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curfn = savefn;
|
|
|
|
}
|