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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
import subprocess
import sys
import os
import time
import argparse
import tempfile
# Test derivations
def all_jit(assembly_line):
"""Add all available jitter options to assembly_line thanks to '--jitter'
option.
@assembly_line: list(str)
Return a list of assembly lines: list(list(str)).
"""
out = []
for jitter in ["tcc", "llvm", "python"]:
out.append(assembly_line + ["--jitter", jitter])
return out
# Available tests
all_tests = {
"test": {
"architecture": [
["arch/x86/arch.py"],
["arch/x86/sem.py"],
["arch/arm/arch.py"],
["arch/arm/sem.py"],
["arch/msp430/arch.py"],
["arch/msp430/sem.py"],
["arch/sh4/arch.py"],
["arch/mips32/arch.py"],
],
"core": [
["core/interval.py"],
["core/graph.py"],
["core/parse_asm.py"],
],
"expression": [
["expression/modint.py"],
["expression/stp.py"],
["expression/simplifications.py"],
],
"ir": [
["ir/ir2C.py"],
["ir/symbexec.py"],
],
"jitter": [
["jitter/os_dep/win_api_x86_32.py"],
],
"order": [
"architecture",
"core",
"expression",
"ir",
"jitter",
],
},
"example": {
"assembler": [
["asm_x86.py"],
["asm_arm.py"],
["asm_box_x86_32.py"],
["asm_box_x86_32_enc.py"],
["asm_box_x86_32_mod.py"],
["asm_box_x86_32_mod_self.py"],
["asm_box_x86_32_repmod.py"],
["asm_msp430_sc.py"],
["asm_mips32.py"],
["disasm_01.py"],
["disasm_02.py"],
["disasm_03.py", "box_upx.exe", "0x410f90"],
],
"expression": [
["symbol_exec.py"],
["expression/manip_expression1.py"],
["expression/manip_expression2.py"],
["expression/manip_expression3.py"],
["expression/manip_expression4.py",
"expression/sc_connect_back.bin", "0x2e"],
["expression/manip_expression5.py"],
["expression/manip_expression6.py"],
["expression/manip_expression7.py"],
["test_dis.py", "-g", "-s", "-m", "arm", "demo_arm.bin", "0"],
["test_dis.py", "-g", "-s", "-m",
"x86_32", "box_x86_32.bin", "0x401000"],
["test_dis.py", "-g", "-s", "-m", "msp430", "msp430_sc.bin", "0"],
["test_dis.py", "-g", "-s", "-m", "mips32l", "mips32_sc.bin", "0"],
["expression/solve_condition_stp.py",
"expression/simple_test.bin"],
],
"jitter": reduce(lambda x, y: x + y,
map(all_jit, [
["unpack_upx.py", "box_upx.exe"], # Take 5 mins on a Core i5
["test_jit_x86_32.py", "x86_32_sc.bin"],
["test_jit_arm.py", "md5_arm", "A684"],
["test_jit_msp430.py", "msp430_sc.bin", "0"],
["test_jit_mips32.py", "mips32_sc.bin", "0"],
["sandbox_pe_x86_32.py", "box_x86_32.bin"],
["sandbox_pe_x86_32.py", "box_x86_32_enc.bin"],
["sandbox_pe_x86_32.py", "box_x86_32_mod.bin"],
["sandbox_pe_x86_32.py", "box_x86_32_repmod.bin"],
["sandbox_pe_x86_32.py", "box_x86_32_mod_self.bin"],
])),
"order": [
"assembler",
"expression",
"jitter",
],
},
"order": [
"test",
"example",
],
}
# Cosmetic
def getTerminalSize():
"Return the size of the terminal : COLUMNS, LINES"
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
import os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
WIDTH = getTerminalSize()[0]
colors = {"red": "\033[91;1m",
"end": "\033[0m",
"green": "\033[92;1m",
"lightcyan": "\033[96m",
"blue": "\033[94;1m"}
def write_colored(text, color, already_printed=0):
text_colored = colors[color] + text + colors["end"]
print " " * (WIDTH - already_printed - len(text)) + text_colored
def write_underline(text):
print "\033[4m" + text + colors["end"]
def print_conf(conf, value):
return colors["green"] + conf + ": " + colors["end"] + str(value)
def clr_screen(global_state, pstate):
"Update the screen to display some information"
# Header
to_print = []
to_print.append(" " * (global_state["termSize"][0] / 2 - 10) + colors[
"blue"] + "Miasm2 Regression tests" + colors["end"])
to_print.append("")
to_print.append("=" * global_state["termSize"][0])
to_print.append("")
to_print.append(print_conf("Current mode", "Multiprocessing"))
to_print.append(print_conf("Nb CPU detected", global_state["cpu_c"]))
to_print.append("")
to_print.append("=" * global_state["termSize"][0])
to_print.append("")
to_print.append(
print_conf("Current section", global_state["section"].upper()))
to_print.append(
print_conf("Current subsection", global_state["subsection"].upper()))
test_done = 0
test_failed = 0
message = global_state["message"] + "\n"
for k, v in pstate.items():
if v["status"] != "running":
test_done += 1
if v["status"] != 0:
test_failed += 1
message += colors["red"] + "FAIL: " + colors["end"] + k
message += v["message"] + "\n"
to_print.append(print_conf("Success rate", "%d/%d" %
(test_done - test_failed, test_done)))
printed_time = time.strftime(
"%M:%S", time.gmtime(time.time() - global_state["init_time"]))
to_print.append(print_conf("Cumulated time", printed_time))
to_print.append("")
to_print.append("=" * global_state["termSize"][0])
cur = "\n".join(to_print)
cur += "\n"
# Message
cur += message
print cur
already_printed = cur.count("\n")
# Current state
current_job = []
for t in pstate.values():
if t["status"] == "running":
current_job.append(t)
print "\n" * (global_state["termSize"][1] - already_printed - 3 - len(current_job))
for j in current_job:
s = "[" + colors["lightcyan"] + j["command"] + colors["end"]
s_end = time.strftime(
"%M:%Ss", time.gmtime(time.time() - j["init_time"]))
l = len(j["command"]) + len(s_end) + 4 + len(str(j["pid"])) + 2
s_end += " " + colors["blue"] + str(j["pid"]) + colors["end"] + "]"
print "%s%s%s" % (s, " " * (global_state["termSize"][0] - l), s_end)
# Tests handling
def are_tests_finished(test_names, done):
for t in test_names:
if t not in done:
return False
return True
def are_tests_finished_multi(test_names, pstate):
for t in test_names:
t = " ".join(t)
if t not in pstate.keys():
return False
if pstate[t]["status"] == "running":
return False
return True
def test_iter(done):
"Return an iterator on next tests, wait for previous sections"
for section_name in all_tests["order"]:
# Go to the right directory
os.chdir(os.path.join("..", section_name))
# Update global state
section_content = all_tests[section_name]
write_underline(section_name.upper())
for subsection_name in section_content["order"]:
subsection_content = section_content[subsection_name]
write_underline("%s > %s" % (section_name.upper(),
subsection_name.upper()))
for test_line in subsection_content:
yield test_line
while not(are_tests_finished(subsection_content, done)):
time.sleep(0.050)
def test_iter_multi(global_state, pstate):
"Multiprocessor version of test_iter"
# Global message : subsections done
message = ""
for section_name in all_tests["order"]:
# Update global state
section_content = all_tests[section_name]
global_state["section"] = section_name
for subsection_name in section_content["order"]:
subsection_content = section_content[subsection_name]
beg_time = time.time()
global_state["subsection"] = subsection_name
for test_line in subsection_content:
yield test_line
while not(are_tests_finished_multi(subsection_content, pstate)):
# Wait for task to finish, update the screen
time.sleep(0.100)
clr_screen(global_state, pstate)
message += "%s > %s completed in %.08f seconds\n" % (section_name.upper(),
subsection_name.upper(
),
time.time() - beg_time)
global_state["message"] = message
# Final update
clr_screen(global_state, pstate)
def run_test(test, coveragerc=None):
s = "Running tests on %s ..." % " ".join(test)
sys.stdout.write(s)
sys.stdout.flush()
args = test
if coveragerc is not None:
args = ["-m", "coverage", "run", "--rcfile", coveragerc, "-a"] + test
# Launch test
testpy = subprocess.Popen(["python"] + args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
outputs = testpy.communicate()
# Check result
if testpy.returncode == 0:
write_colored("OK", "green", len(s))
else:
write_colored("ERROR", "red", len(s))
print outputs[1]
def run_test_parallel(test, current, global_state):
pid = os.getpid()
test_key = " ".join(test)
# Keep current PID
current[test_key] = {"status": "running",
"pid": pid,
"command": test_key,
"init_time": time.time()}
# Go to the right directory
os.chdir(os.path.join("..", global_state["section"]))
# Launch test
testpy = subprocess.Popen(["python"] + test, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
outputs = testpy.communicate()
# Check result
message = ""
if testpy.returncode != 0:
message = outputs[1]
# Update result
current[test_key] = {"status": testpy.returncode,
"message": message}
# Multiprocessing handling
try:
from multiprocessing import Manager, Pool, cpu_count
multiproc = True
except ImportError:
multiproc = False
# Argument parsing
parser = argparse.ArgumentParser(description="Miasm2 testing tool")
parser.add_argument("-m", "--mono", help="Force monothreading",
action="store_true")
parser.add_argument("-c", "--coverage", help="Include code coverage",
action="store_true")
args = parser.parse_args()
if args.mono is True or args.coverage is True:
multiproc = False
# Handle coverage
coveragerc = None
if args.coverage is True:
try:
import coverage
except ImportError:
print "%(red)s[Coverage]%(end)s Python 'coverage' module is required" % colors
exit(-1)
# Create directory
suffix = "_" + str(int(time.time()))
cov_dir = tempfile.mkdtemp(suffix, "m2_coverage_")
# Create configuration file
coveragerc = os.path.join(cov_dir, ".coveragerc")
coverage = os.path.join(cov_dir, ".coverage")
from ConfigParser import ConfigParser
from os.path import expanduser
config = ConfigParser()
config.read(['/etc/coveragerc', expanduser('~/.coveragerc')])
if not config.has_section('run'):
config.add_section('run')
config.set('run', 'data_file', coverage)
config.write(open(coveragerc, 'w'))
# Inform the user
d = {"blue": colors['blue'],
"end": colors['end'],
"cov_dir": cov_dir}
print "[%(blue)sCoverage%(end)s] Report will be written in %(cov_dir)s" % d
# Handle llvm modularity
llvm = True
try:
import llvm
except ImportError:
llvm = False
# if llvm.version != (3,2):
# llvm = False
if llvm is False:
print "%(red)s[LLVM]%(end)s Python 'py-llvm 3.2' module is required for llvm tests" % colors
# Remove llvm tests
for test in all_tests["example"]["jitter"]:
if "llvm" in test:
all_tests["example"]["jitter"].remove(test)
print "%(red)s[LLVM]%(end)s Remove" % colors, " ".join(test)
# Let the user see messages
time.sleep(0.5)
# Run tests
if multiproc is False:
done = list()
for test in test_iter(done):
run_test(test, coveragerc=coveragerc)
done.append(test)
else:
# Parallel version
cpu_c = cpu_count()
global_state = {"cpu_c": cpu_c,
"init_time": time.time(),
"termSize": getTerminalSize(),
"message": ""}
manager = Manager()
pool = Pool(processes=cpu_c)
current = manager.dict()
for test in test_iter_multi(global_state, current):
pool.apply_async(run_test_parallel, (test,
current,
global_state))
pool.close()
pool.join()
|