-
Notifications
You must be signed in to change notification settings - Fork 5
/
Extractor.py
executable file
·179 lines (144 loc) · 4.68 KB
/
Extractor.py
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
#!/usr/bin/python2
import sys
from CPG import CPG
from CPGPath import CPGPath
from PHPBuiltIn import *
from WPBuiltIn import *
from utils import *
MAX_NODE = 500
class Extractor:
def __init__(self, CPG, Sink, Source):
self.CPG = CPG
self.Sink = Sink
self.Source = Source
self.Paths = []
def findCallers(self, Id, Callers):
if self.CPG.isTerminalNode(Id): return
if self.CPG.hasCallEdge(Id): Callers += [Id]
for c in self.CPG.getChildNodes(Id):
self.findCallers(c, Callers)
def followCallEdge(self, Id, Path):
# Call stmt at top-level
if self.CPG.hasCallEdge(Id):
Callee = self.CPG.getCallee(Id)
CalleeEntry = self.CPG.getFuncEntry(Callee)
# Prevent multiple calls on same function
if Callee in Path: return [[]]
IntraPaths = []
self.findPath(CalleeEntry, [Callee, CalleeEntry], IntraPaths)
return IntraPaths
# Explore call stmts under this subtree
else:
Callers = []
self.findCallers(Id, Callers)
# TODO: handle multiple call stmts under this subtree
if len(Callers) == 1:
return self.followCallEdge(Callers[0], Path)
else:
return [[]]
def followCFEdge(self, Id, Path, IntraPaths):
if self.CPG.isFuncExit(Id) or Id == self.Sink:
IntraPaths += [Path]
return
if self.CPG.isCFSrc(Id):
for DesId in self.CPG.getCFDes(Id):
if DesId not in Path:
self.findPath(DesId, Path + [DesId], IntraPaths)
def reachedSink(self, Path):
return (Path[0] == self.Source and Path[-1] == self.Sink) or \
(Path[0] == self.Source and Path[0] == self.Sink)
def findPath(self, Id, Path, CurIntraPaths):
if len(Path) > MAX_NODE:
print 'Error: Maximum # of nodes reached'
return
# Follow call edge at this node
IntraPaths = self.followCallEdge(Id, Path)
# Follow control flow edges at this node
for IntraPath in IntraPaths:
if self.reachedSink(Path + IntraPath):
TopPath = Path + IntraPath
if TopPath[0] == self.Sink and TopPath[-1] != self.Sink:
self.Paths += [TopPath + [self.Sink]]
else:
self.Paths += [TopPath]
else:
self.followCFEdge(Id, Path + IntraPath, CurIntraPaths)
if self.reachedSink(Path) and IntraPaths == []:
self.Paths += [Path]
def temp(DirPath, Source, Sink, PathNum = None, PathDumpFileName = None):
# Initialize CPG
cpg = CPG(DirPath)
# Extract path
print cpg, Sink, Source
e = Extractor(cpg, Sink, Source)
e.findPath(Source, [Source], [])
if PathNum == None:
print "Number of path: %s" % len(e.Paths)
for p in e.Paths:
print p
'''
for Id in p:
print cpg.getSubtree(Id)
'''
return e.Paths, cpg
topLevelPath = e.Paths[PathNum]
pathAllNodes = []
for Id in topLevelPath:
pathAllNodes.extend(cpg.getSubtree(Id))
pathNodes = {}
pathEdges = {}
pathCFEdges = {}
pathDDEdges = {}
pathCallEdges = {}
pathCDEdges = {}
for Id in pathAllNodes:
if cpg.Nodes[Id][1] is not None:
pathNodes[Id] = (cpg.Nodes[Id][0], cpg.Nodes[Id][1][1:-1])
else:
pathNodes[Id] = (cpg.Nodes[Id][0], None)
if Id in cpg.ASTEdge.keys():
if cpg.Nodes[Id][0] in ['AST_FUNC_DECL', 'AST_METHOD']:
pathEdges[Id] = []
else:
pathEdges[Id] = cpg.ASTEdge[Id]
if Id in cpg.CFEdge.keys():
inpath_edge = list(set(pathAllNodes) & set(cpg.CFEdge[Id]))
if inpath_edge != []:
pathCFEdges[Id] = inpath_edge
if Id in cpg.DDEdge.keys():
inpath_edge = list(set(pathAllNodes) & set(cpg.DDEdge[Id]))
if inpath_edge != []:
pathDDEdges[Id] = inpath_edge
if Id in cpg.CallEdge.keys():
inpath_edge = list(set(pathAllNodes) & set([cpg.CallEdge[Id]]))
if inpath_edge != []:
pathCallEdges[Id] = inpath_edge
if Id in cpg.CDEdge.keys():
inpath_edge = list(set(pathAllNodes) & set(cpg.CDEdge[Id]))
if inpath_edge != []:
pathCDEdges[Id] = inpath_edge
path = CPGPath(e.Paths[PathNum], \
pathNodes, \
pathEdges, \
pathCFEdges, \
pathDDEdges, \
pathCallEdges, \
pathCDEdges)
print path
save_cpg_path(PathDumpFileName, path)
if __name__ == '__main__':
if len(sys.argv) == 4:
DirPath = sys.argv[1]
Source = int(sys.argv[2])
Sink = int(sys.argv[3])
temp(DirPath, Source, Sink)
elif len(sys.argv) == 6:
DirPath = sys.argv[1]
Source = int(sys.argv[2])
Sink = int(sys.argv[3])
PathNum = int(sys.argv[4])
PathDumpFileName = sys.argv[5]
temp(DirPath, Source, Sink, PathNum, PathDumpFileName)
else:
print("TODO: Usage")
sys.exit()