-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcparegexteacher.py
279 lines (222 loc) · 11.3 KB
/
cparegexteacher.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
from __future__ import print_function
import sys, os
import tempfile, subprocess, re, shutil, errno
import lstar, minimally_adequate_teacher, tempdir, chdir, anml, brzozowski
class CPAReMat(minimally_adequate_teacher.MinimallyAdequateTeacher):
def __init__(self,
src_dir,
cpachecker_executable,
alphabet,
loggingdir,
eq,
verbose=0):
"""src_dir should contain the kernel"""
super(CPAReMat, self).__init__()
self.verbose = verbose
self.eq = eq
print(src_dir)
self.log_dir = os.path.abspath(loggingdir)
try:
os.makedirs(self.log_dir)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
if self.verbose >= lstar.LStarUtil.loud:
"logging dir already exists!"
self.src_dir = os.path.abspath(src_dir)
self.cpachecker = os.path.abspath(cpachecker_executable)
if self.verbose >= lstar.LStarUtil.loudest:
print("cpa executable:", self.cpachecker)
print("logging dir:", self.log_dir)
self.alphabet = alphabet
# compile a kernel
if self.verbose >= lstar.LStarUtil.loud:
print("====================")
print("| Compiling Kernel |")
print("====================")
#copy the kernel
shutil.copy(self.src_dir+"/kernel.c", self.log_dir)
with chdir.ChDir(self.log_dir) as tdir:
kernel_wrapper = "kernel_wrapper.c"
with open(kernel_wrapper, "w") as f:
print('#include "kernel.c"', file=f)
print('int main(int argc, char *argv[]) {', file=f)
print('if(argc == 2)', file=f)
print('{', file=f)
print(' if(kernel(argv[1]))', file=f)
print(' {', file=f)
print(' //printf("true\\n");', file=f)
print(' return 0;', file=f)
print(' } else {', file=f)
print(' //printf("false\\n");', file=f)
print(' return 1;', file=f)
print(' }', file=f)
print('} else {', file=f)
print(' //printf("just pass in the string\\n");', file=f)
print(' return 10;', file=f)
print('}', file=f)
print('}', file=f)
gcc_command = ["gcc", "-iquote{}".format(self.src_dir), "-o", "kernel", kernel_wrapper]
subprocess.call(gcc_command)
#raw_input("check kernel!")
def isMember(self, inp):
super(CPAReMat, self).isMember(inp)
cached = self.getChache(inp)
if cached is None:
#ret = subprocess.call(self.src_dir + '/kernel "'+inp+'"', shell=True)
ret = subprocess.call([self.log_dir+'/kernel',
inp])
if ret == 0:
return self.addCache(inp,True)
else:
return self.addCache(inp,False)
else:
return cached
def isEquivalent(self,anml):
"""Uses CPAChecker to try to find a counterexample quickly"""
super(CPAReMat, self).isEquivalent(anml)
if self.verbose >= lstar.LStarUtil.loud:
print("==========================")
print("| Checking if equivalent |")
print("==========================")
query_number = self.getStats()['equivalence_queries']
#use the query_number to store the equivalence logs
cur_dir = self.log_dir + "/equivalent-{}".format(str(query_number))
try:
os.makedirs(cur_dir)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
if self.verbose >= lstar.LStarUtil.loud:
"logging dir '{}' already exists!".format(cur_dir)
# first, we need to get the regular expression from the state machine
br = brzozowski.Machine(anml)
if self.verbose >= lstar.LStarUtil.loudest:
print("Adj")
br.printAdj()
print("B")
br.printB()
regex = (br.brzozowski().simplify())
if self.verbose >= lstar.LStarUtil.louder:
print("The machine represents:", regex)
# make a temp directory to do all of our work in
with chdir.ChDir(cur_dir) as tdir:
# first, save a copy of the automaton
anml_file = "automaton.anml"
with open(anml_file, "w") as f:
f.write(str(anml))
# now make the file that cpachecker will check
# this should have both the kernel function and the regex
# we then look for the symmetric difference
checker_file = "difference.c"
with open(checker_file, "w") as f:
print('#include "kernel.c"', file=f)
print('int difference(char* input) {', file=f)
#if self.eq:
# print('if(!__cpa_streq(input, "")) {', file=f)
#else:
# print('if(__cpa_strlen(input) > 0) {', file=f)
print("int k,r;", file=f)
print("k = kernel(input);", file=f)
print('r = __cpa_regex(input,"{}");'.format(regex), file=f)
if len(self.alphabet) == 1:
language_regex = "\\x"+format(ord(self.alphabet[0]))
else:
language_regex = reduce(lambda s, a: "({}|\\x{})".format(s,format(ord(a), "x")), self.alphabet[2:], "(\\x{}|\\x{})".format(format(ord(self.alphabet[0]),"x"),format(ord(self.alphabet[1]),"x")) )
print('if( __cpa_regex(input, "({})(({})*)")) {{'.format(language_regex, language_regex), file=f)
print("if (k != r) {", file=f)
#print('if( (kernel(input) && !__cpa_regex(input,"{}")) || (!kernel(input) && __cpa_regex(input,"{}")) ) {{'.format(regex,regex), file=f)
print('ERROR: return 1;', file=f)
# print('}', file=f)
print('}', file=f)
print( '} return 0; ', file=f )
print('}', file=f)
# preprocess the checker file with gcc
subprocess.call(["gcc",
"-iquote{}".format(self.log_dir),
"-E",
checker_file,
"-o", "cpa.i"])
# call CPAChecker
subprocess.call([self.cpachecker,
#"-ldv",
"-predicateAnalysis",
"-timelimit", "-1ns",
"-setprop", "solver.solver=Z3",
"-setprop", 'analysis.entryFunction=difference',
"-setprop", 'cpa.predicate.handleArrays=true',
"-setprop", "counterexample.export.model=Counterexample.%d.assignment.txt",
"-setprop", "counterexample.export.formula=Counterexample.%d.smt2",
"-setprop", "log.level=All",
"-setprop", "solver.z3.log=z3.log",
"-setprop", "cpa.predicate.blk.threshold=1", #Small Block Encoding
#"-setprop", "cpa.predicate.refinement.getUsefulBlocks=false",
#"-setprop", "cpa.predicate.refinement.strategy=TREE",
"cpa.i"])
# FIXME check for a counterexample
# get counterexample
c = self._extract_counter_example(cur_dir+"/output")
if self.verbose >= lstar.LStarUtil.loud:
print("Counterexample: ", c)
raw_input("waiting")
return c
#return (True, None)
def _extract_counter_example(self, cpa_dir):
#FIXME doesn't check to see how we fail or succeed. May just give up
with chdir.ChDir(cpa_dir):
# we are now in the cpa_checker results dir
try:
counterexample = ([x for x in os.listdir(cpa_dir) if "Counterexample" in x and "assignment.txt" in x])[0]
if self.verbose >= lstar.LStarUtil.loud:
print("==========================")
print("| Reading Counterexample |")
print("==========================")
with open(counterexample, "r") as f:
cpa_output = f.read()
if self.verbose >= lstar.LStarUtil.loudest:
print("CPA Contents:", cpa_output)
# make a dict of the data
#input_data = dict()
#data = re.compile("\*char@1\((\d+)\): (\d+)\n")
#for m in data.finditer(cpa_output):
# if self.verbose >= lstar.LStarUtil.loud:
# print("Memory({}) = {}".format(m.group(1), m.group(2)))
# sys.stdout.flush()
# try:
# os.fsync(sys.stdout.fileno())
# except:
# pass
# input_data[int(m.group(1))] = chr(int(m.group(2)))
# now find the starting point of the string
cexample = re.search("kernel::input[^:]*: ([^\n]*)(\n)?", cpa_output).group(1)
#if self.verbose >= lstar.LStarUtil.loud:
# print("start location = {}".format(start))
# sys.stdout.flush()
# try:
# os.fsync(sys.stdout.fileno())
# except:
# pass
# build up the string
#i = start
#cexample = ""
#while True:
# try:
# if input_data[i] == '\x00':
# break
# cexample = cexample + input_data[i]
#
# if self.verbose >= lstar.LStarUtil.loud:
# print("CEXAMPLE({}) = {}".format(i, input_data[i]))
# sys.stdout.flush()
# try:
# os.fsync(sys.stdout.fileno())
# except:
# pass
#
# i += 1
# except KeyError:
# break
return (False, cexample)
except IndexError:
return (True, None)
pass