-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalternativeCharacterize.py
350 lines (279 loc) · 11.8 KB
/
alternativeCharacterize.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# ==============================================================================
# File : alternativeCharacterize.py (gets renamed by Dockerfile
# to Characterize.py)
# Author : Max von Hippel and Cole Vick
# Authored : 30 November 2019 - 13 March 2020
# Purpose : Checks when models do or do not satisfy properties. Also inter-
# prets various outputs of Spin.
#
# NOTE - this file is included so that the Dockerfile can reproduce
# the results from the ArXiV paper, where we used partial order
# reduction. But moving forward, in order to support RFCNLP and
# other projects, we are keeping partial order reduction turned OFF
# by default. This is what the -DNOREDUCE option does in the
# file korg/Characterize.py.
#
# How to run: This code is used by Korg.py, which is what you want to run.
# ==============================================================================
import subprocess
import sys
import os
from glob import glob
from korg.Construct import makeAttackTransferCheck
from korg.printUtils import *
def nontrivialProps(P, Q, props):
ret = set()
for phi in props:
# We assume this file does not yet exist
tmpname = str(abs(hash(P + Q + phi))) + ".temporary.pml"
if models(P, phi, Q, tmpname, True):
ret.add(phi)
else:
print(makeRed( \
phi \
+ " was not supported by " \
+ P + " || " + Q \
+ " so cannot be attacked."))
bigCleanUp(tmpname)
return list(ret)
def testRemaining(attackPath, P, Q, props, comparing=False, testWithoutRecovery=False):
if attackPath[-1] != "/":
attackPath += "/"
# Remove any trivial props
props = nontrivialProps(P, Q, props)
recoveryOptions = [ True, False ] if testWithoutRecovery == True else [ True ]
for attackerModel in glob(attackPath + "*.pml"):
for phi in props:
for _recovery in recoveryOptions:
checkText = makeAttackTransferCheck(attackerModel, P, phi, _recovery)
newname = str(abs(int(hash(checkText)))) + ".pml"
with open(newname, "w") as fw:
fw.write(checkText)
result = check(newname)
if result == False:
print(
("" if comparing == False
else
makeBlue("[ comparing to " + P + " ]\n\t\t")) +
makeGreen(
attackerModel +
" is an attack " +
("with " if _recovery == True else "without ") +
"recovery against\n\t\t\t" +
phi))
elif result == True and comparing == True:
print(
makeFail(
"\n\t\t" +
attackerModel +
" does ") +
makeBold("NOT") +
makeFail(
"\n\t\twork as an attack " +
( "with " if _recovery == True else "without ") +
"recovery against " +
phi +
"\n\t\twhen applied to " +
P))
os.remove(newname)
# Also adapted from RFCNLP code.
def bigCleanUp(cur_model_name):
for file in glob("*.trail") + \
glob("*tmp*") + \
glob("pan") + \
glob("*.pml") + \
glob("._n_i_p_s_"):
if file != cur_model_name + ".pml" and \
file != cur_model_name + "_CORRECT.pml":
os.remove(file)
"""
Adapted from some logic in RFCNLP.
Deletes syntactically redundant attacks.
"""
def removeRedundant(attackPath):
attacker_hashes = set()
if attackPath[-1] != "/":
attackPath += "/"
for attacker in glob(attackPath + "*.pml"):
print("Considering attacker " + attacker + " for possible removal")
attacker_body = ""
l = 0
with open(attacker, "r") as fr:
for line in fr:
if (l > 0):
attacker_body += "\n" + line
l += 1
attacker_hash = hash(attacker_body)
if (attacker_hash in attacker_hashes):
print("Removing redundant attacker " + attacker)
os.remove(attacker)
else:
attacker_hashes.add(attacker_hash)
print("Did not remove.")
'''
Given the path to a model containing one or more properties,
checks if that model violates one or more of those properties.
INPUT:
modelFile - the string containing the path to the model file
OUTPUT:
true iff modelFile runs successfully without acceptance cycles
or violations else false
'''
def check(modelFile, maxDepth=60000):
if maxDepth > 10000 * 20:
print("maxDepth too large for any realistic run. Edit the code if " \
+ "you actually really want to do this ... in Characterize.py.")
return False
# args = "spin -run -a -DNOREDUCE -m" + str(maxDepth) + " -RS88 " + modelFile
args = "spin -run -a -m" + str(maxDepth) + " -RS88 " + modelFile
args = [a.strip() for a in args.split(" ")]
args = [a for a in args if len(a) > 0]
ret = None
with open(os.devnull, 'w') as devnull:
try:
ret = subprocess.check_output(args, stderr=devnull)
if sys.stdout.encoding != None:
ret = ret.decode(sys.stdout.encoding).strip()
else:
ret = str(ret)
except Exception as e:
print("+++++++++ Problem in check() ++++++++++")
print("---------------- ret ------------------")
print(ret)
print("--------------- exception -------------")
print(e)
print("----------------- model ---------------")
try:
with open(modelFile, "r") as fr:
print(fr.read())
except Exception as e_inner:
print(e_inner)
print("+++++++++++++++++++++++++++++++++++++++")
raise e
if ret == None:
return False
if "depth too small" in ret:
print("Search depth was too small at " \
+ str(maxDepth), \
" doubling depth ...")
return check(modelFile, maxDepth * 2)
return not ("violated" in ret or "acceptance cycle" in ret)
def makeAllTrails(modelFile, numTrails=100):
args = ""
if numTrails <= 1:
# args = "spin -run -a -DNOREDUCE " + modelFile
args = "spin -run -a " + modelFile
else:
# args = "spin -run -a -DNOREDUCE -e -c" + str(numTrails - 1) + " " + modelFile
args = "spin -run -a -e -c" + str(numTrails - 1) + " " + modelFile
subprocess.run(args.split(" "))
'''
Given the paths to a model and a property, checks if that
model makes true that property.
INPUT:
model - the string containing the path to the model file
phi - the string containing the path to the ltl model file
N - the string containing the path to the N model file
name - where to save the intermediary model file to
OUTPUTS:
true iff the model (model || phi || N) runs successfully without
acceptance cycles or violations else false
'''
def models(model, phi, N, name, removeAfter=False):
if None in { model, phi, N, name }:
return False
fmrLines = ""
fNrLines = ""
fprLines = ""
with open(model, 'r') as fmr:
fmrLines = fmr.read()
with open(N, 'r') as fNr:
fNrLines = fNr.read()
with open(phi, 'r') as fpr:
fprLines = fpr.read()
with open(name, 'w') as fw:
fw.write(fmrLines + "\n" + fNrLines + "\n" + fprLines)
assert(os.path.isfile(name))
ret = check(name)
if removeAfter == True:
os.remove(name)
return ret
# Parses output of reading trail using Spin.
def parseTrail(trail_body, cycle_indicator=None):
print("\n\n-------------- PARSING TRAIL: \n")
print(trail_body)
with_recovery = True
if cycle_indicator == None:
with_recovery = False
cycle_indicator = "CYCLE"
ret, i = [[], []], 0
for line in trail_body.split("\n"):
if "(daisy:" in line:
# https://stackoverflow.com/a/29571669/1586231
LL = line.rstrip("*")
chan = LL[line.rfind("(")+1:-1]
msg, evt = None, None
if "Recv " in line:
msg = LL[line.rfind("Recv ") + 5:].split()[0]
evt = "?"
if "Send" in line and msg == None and evt == None:
msg = LL[line.rfind("Send ") + 5:].split()[0]
evt = "!"
if "Sent" in line and msg == None and evt == None:
msg = LL[line.rfind("Sent ") + 5:].split()[0]
evt = "!"
if evt != None and msg != None:
ret[i].append(chan + " " + evt + " " + msg)
if cycle_indicator in line and (with_recovery == True):
i = 1
elif cycle_indicator in line and (with_recovery == False):
i = 1
print("\n\n----- parsed to: ")
print("\n".join([str(r) for r in ret]))
print("\n````````````````````````````````````````````````````")
return ret
def parseAllTrails(cmds, with_recovery=False, debug=False, cycle_indicator=None):
print("\n----> Calling parseAllTrails()")
ret = []
prov = []
with open(os.devnull, 'w') as devnull:
for cmd in cmds:
output = subprocess.check_output(cmd, stderr=devnull)
if sys.stdout.encoding != None:
output = output.decode(sys.stdout.encoding)
output = str(output).strip().replace("\\n", "\n")\
.replace("\\t", "\t")
parsed = parseTrail(output, cycle_indicator)
ret.append(parsed)
prov.append(cmd)
return ret, prov
def attackType(A, E):
# if A == 1 then E == 1
# <=> not (A == 1 and E == 0)
assert(not (A and not E))
if A:
return "A-attack"
if E:
return "E-attack"
return "NOT AN ATTACK"
def characterizeAttacks(model, phi, with_recovery=True, name="run"):
assert(os.path.isdir("out/" + name))
nE, nA = 0, 0
with open("out/" + name + "/log.txt", "w") as fw:
if not (os.path.isdir("out/" + name + "/artifacts")):
os.mkdir("out/" + name + "/artifacts")
fw.write("model,A/E,with_recovery?\n")
for attackModel in glob("out/" + name + "/attacker*.pml"):
# is it a forall attack?
attackName = os.path.basename(attackModel).replace(".pml", "")
aName = "out/" + name + "/artifacts/" + attackName + "_A.pml"
eName = "out/" + name + "/artifacts/" + attackName + "_E.pml"
A = (models(model, "negated.pml", attackModel, aName) == True )
E = (models(model, phi, attackModel, eName) == False)
if A:
nA += 1
elif E:
nE += 1
fw.write(",".join([ \
attackModel, attackType(A, E), str(with_recovery)]) + "\n")
return (nE, nA)