This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
semioptimistic.js
99 lines (84 loc) · 3.02 KB
/
semioptimistic.js
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
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Max Schaefer - initial API and implementation
*******************************************************************************/
/* Optimistic call graph builder that tries to be clever about
* which interprocedural flows to propagate: it only propagates
* along edges that lead to a function call. */
if(typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function(require, exports) {
var graph = require('./graph'),
natives = require('./natives'),
flowgraph = require('./flowgraph'),
callgraph = require('./callgraph');
function buildCallGraph(ast) {
var fg = new graph.Graph();
natives.addNativeFlowEdges(fg);
flowgraph.addIntraproceduralFlowGraphEdges(ast, fg);
var changed;
do {
changed = false;
var reach = fg.reachability(function(nd) { return nd.type !== 'UnknownVertex'; });
ast.attr.calls.forEach(function(call) {
var res = flowgraph.resVertex(call);
if(!res.attr.interesting)
reach.iterReachable(res, function(nd) {
if(nd.type === 'CalleeVertex') {
//console.log(res.attr.pp() + " is interesting");
res.attr.interesting = true;
}
});
});
ast.attr.functions.forEach(function(fn) {
var interesting = false, nparams = fn.params.length;
for(var i=0;i<=nparams;++i) {
var param = flowgraph.parmVertex(fn, i);
if(!param.attr.interesting) {
reach.iterReachable(param, function(nd) {
if(nd.type === 'CalleeVertex') {
param.attr.interesting = true;
}
});
}
interesting = interesting || param.attr.interesting;
}
reach.iterReachable(flowgraph.funcVertex(fn), function(nd) {
if(nd.type === 'CalleeVertex') {
var call = nd.call, res = flowgraph.resVertex(call);
if(res.attr.interesting) {
var ret = flowgraph.retVertex(fn);
if(!fg.hasEdge(ret, res)) {
changed = true;
fg.addEdge(ret, res);
}
}
if(interesting)
for(var i=0;i<=nparams;++i) {
if(i > call.arguments.length)
break;
var param = flowgraph.parmVertex(fn, i);
if(param.attr.interesting) {
var arg = flowgraph.argVertex(call, i);
if(!fg.hasEdge(arg, param)) {
changed = true;
fg.addEdge(arg, param);
}
}
}
}
});
});
} while(changed);
return callgraph.extractCG(ast, fg);
}
exports.buildCallGraph = buildCallGraph;
return exports;
});