-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzeANSI.py
executable file
·289 lines (238 loc) · 9.85 KB
/
analyzeANSI.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
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
#!/usr/bin/python3
import subprocess
import json
import sys
import functools
from intervals import round_robin,state_init
def printjson(jso):
print(json.dumps(jso,indent=2))
def parseANSIC(inputfile):
p = subprocess.Popen(['nodejs',
"-e",
"console.log(JSON.stringify(require(\"./ansic.js\").parser.parse(require('fs').readFileSync(require('path').normalize(\""+
inputfile
+"\"),\"utf8\"))))"
], stdout=subprocess.PIPE)
out=json.loads(p.stdout.read())
return out
def scanforfunctions(compilationunit):
funs={}
for bla in compilationunit:
if bla["declarator"]["type"]=="function":
funname=bla["declarator"]["base"]["name"]
funs[funname]=bla["body"]
return funs
nodecounter = 0
def createNode():
global nodecounter
nodecounter = 1 + nodecounter
return nodecounter
edgekeeper = []
def createEdge(fro,label,to):
label = { "from":fro,"label":label,"to":to }
global edgekeeper
edgekeeper.append(label)
return label
def scan_block(block,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
lastinstr=supposedstart
currentedge={}
for stmt in block["code"]:
nextinstr=createNode()
currentedge=scan_stmt(stmt,lastinstr,nextinstr,breakpoint,continuepoint,retpoint)
lastinstr=nextinstr
currentedge["to"]=supposedend
return currentedge
def scan_expressionstmt(exprstmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
label=exprstmt["expr"]
return createEdge(supposedstart,label,supposedend)
def scan_whilestmt(whlstmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
stmt=whlstmt["stmt"]
cond =whlstmt["cond"]
truebranch = createNode()
createEdge(supposedstart,cond,truebranch)
scan_stmt(stmt,truebranch,supposedstart,supposedend,supposedstart,retpoint)
neg=cond.copy()
neg["inverted"]=True
return createEdge(supposedstart,neg,supposedend)
def scan_forstmt(forstmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
init = forstmt["e1"]["expr"]
cond = forstmt["e2"]["expr"]
inc = forstmt["e3"]
loopstart=createNode()
createEdge(supposedstart,init,loopstart)
stmt=forstmt["stmt"]
truebranch = createNode()
createEdge(loopstart,cond,truebranch)
incstart = createNode()
scan_stmt(stmt,truebranch,incstart,supposedend,loopstart,retpoint)
createEdge(incstart,inc,loopstart)
neg=cond.copy()
neg["inverted"]=True
return createEdge(loopstart,neg,supposedend)
def scan_dostmt(dostmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
stmt=dostmt["stmt"]
cond =dostmt["cond"]
condpoint = createNode()
scan_stmt(stmt,supposedstart,condpoint,supposedend,condpoint,retpoint)
createEdge(condpoint,cond,supposedstart)
neg=cond.copy()
neg["inverted"]=True
return createEdge(condpoint,neg,supposedend)
def scan_ifstmt(ifstmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
stmt=ifstmt["stmt"]
cond =ifstmt["cond"]
truebranch = createNode()
falsebranch = createNode()
end = createNode()
createEdge(supposedstart,cond,truebranch)
scan_stmt(stmt,truebranch,end,breakpoint,continuepoint,retpoint)
neg=cond.copy()
neg["inverted"]=True
if ("else" in ifstmt):
createEdge(supposedstart,neg,falsebranch)
elsestmt=ifstmt["else"]
scan_stmt(elsestmt,falsebranch,end,breakpoint,continuepoint,retpoint)
else:
createEdge(supposedstart,neg,end)
return createEdge(end,"empty",supposedend)
def scan_return(stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
return createEdge(supposedstart,"return "+stmt["expr"],retpoint)
def scan_breakstmt(stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
return createEdge(supposedstart,"empty",breakpoint)
def scan_continuestmt(stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
return createEdge(supposedstart,"empty",continuepoint)
def scan_stmt(stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint):
match stmt["type"]:
case "block": return scan_block (stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint)
case "expr": return scan_expressionstmt(stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint)
case "do": return scan_dostmt (stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint)
case "while": return scan_whilestmt (stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint)
case "if": return scan_ifstmt (stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint)
case "return": return scan_return (stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint)
case "for": return scan_forstmt (stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint)
case "break": return scan_breakstmt (stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint)
case "continue": return scan_continuestmt (stmt,supposedstart,supposedend,breakpoint,continuepoint,retpoint)
# case "goto":
# case "switch":
case _: return createEdge(supposedstart,"not implemented",supposedend)
def renderEdge(edgelabel):
if (not type(edgelabel) is dict):
ret= str(edgelabel)
else:
if (not "left" in edgelabel):
print(edgelabel)
left = edgelabel["left"]
if (left):
ret= str(renderEdge(left))+edgelabel["operator"]+str(renderEdge(edgelabel["right"]))
if ("inverted" in edgelabel):
ret="!("+ret+")"
return ret
def iterateEdges(collector,joinbytarget=True):
if (joinbytarget==False):
values = set(map(lambda x:x["from"], edgekeeper))
packed = [[x,[[y["label"],y["to"]] for y in edgekeeper if y["from"]==x]] for x in values]
else:
values = set(map(lambda x:x["to"], edgekeeper))
packed = [[x,[[y["label"],y["from"]] for y in edgekeeper if y["to"]==x]] for x in values]
for p in packed:
collector(p[0],p[1])
def defaultCollector(src,edges):
print("("+str(src)+","+str(edges)+")")
def graphvizCollector(out,src,edges):
src=str(src)
edgecounter=0
for edge in edges:
edgecounter+=1
edgelab=src+"e"+str(edgecounter)
to=str(edge[1])
out.append("\n s"+src+" [shape=circle,fillcolor=yellow,style=filled,label=\""+src+"\"] ")
out.append("\n s"+to+" [shape=circle,fillcolor=yellow,style=filled,label=\""+to+"\"] ")
out.append("\n e"+edgelab+" [shape=box, color=blue, label=\""+renderEdge(edge[0])+"\"]")
out.append("\n s"+src+"-> e"+edgelab+" -> s"+to)
def intervalCollector(out,tgt,edges):
target=str(tgt)
rhs=[]
for edge in edges:
label=renderEdge(edge[0])
rhs.append("⟦"+label+"⟧# I["+str(edge[1])+"]")
rhs=" ⊔ ".join(rhs)
out.append("I["+target+"] ⊒ "+rhs)
def realIntervals(cfg,tgt,edges):
cfg[tgt]=edges
def polyCollector(out,src,edges):
src=str(src)
for edge in edges:
label=renderEdge(edge[0])
target=str(edge[1])
out.append("I["+src+"] ⊒ ⟦"+label+"⟧♮ I["+target+"]")
def printres(out,values):
for k,v in values.items():
label=str(v)
label=""
for s,w in v.items():
label=label+s+":"
for x in w:
label+="["+str(int(x[0]))+","+str(int(x[1]))+"]"
out.append(" { s"+str(k)+"\n")
out.append("r"+str(k)+"[shape=box,fillcolor=bisque,style=\"filled,rounded\",color=pink,label=\""+label+"\",fontsize=8,height=.25]\n")
out.append("rank=same }\n")
out.append("r"+str(k)+" -> s"+str(k)+" [color=bisque]\n")
CFG=0
INTERVALS=1
POLYNOMIALS=2
INTERVALS2=3
if __name__ == '__main__':
if (len(sys.argv) <2):
print("usage: almostANSI.py [inputfile.c] [option]")
print(" -cfg CFG in dot format, combines well with | xdot /dev/stdin")
print(" -intervals interval analysis")
print(" -intervals2 interval analysis graphically represented with | xdot /dev/stdin")
print(" -polynomials polynomial relations analysis")
quit()
infile=sys.argv[1]
out=parseANSIC(infile)
funs = scanforfunctions(out)
start=createNode()
end=createNode()
scan_stmt(funs["main"],start,end,end,start,end)
#iterateEdges(defaultCollector)
option=CFG
if (len(sys.argv)==3):
if (sys.argv[2]=="-intervals"):
option=INTERVALS
if (sys.argv[2]=="-polynomials"):
option=POLYNOMIALS
if (sys.argv[2]=="-intervals2"):
option=INTERVALS2
if option==CFG:
out = ["digraph cfg { \n rankdir=TB "]
collector=functools.partial(graphvizCollector,out)
iterateEdges(collector,joinbytarget=False)
out.append("\n}")
print("".join(out))
if option==INTERVALS:
out = []
collector=functools.partial(intervalCollector,out)
iterateEdges(collector)
print("\n".join(out))
if option==POLYNOMIALS:
out = []
collector=functools.partial(polyCollector,out)
iterateEdges(collector,joinbytarget=False)
print("\n".join(out))
if option==INTERVALS2:
out = ["digraph cfg { \n rankdir=TB "]
collector=functools.partial(graphvizCollector,out)
iterateEdges(collector,joinbytarget=False)
constraints = {}
values = { 1:state_init(["i"])}
collector=functools.partial(realIntervals,constraints)
iterateEdges(collector)
round_robin(constraints,values)
printres(out,values)
out.append("\n}")
print("".join(out))
# printjson(funs["main"]["code"])
# printjson(out)
# printjson(out[1])
# printjson(out[1]["loc"])