-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsighandler.py
655 lines (628 loc) · 34.5 KB
/
sighandler.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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import os
import sys
import re
import pexpect
import objdump
import faultinject
import configure
import random
import shutil
import datetime
GDB_PROMOPT = "\(gdb\)"
GDB_RUN = "run"
GDB_LAUNCH = "gdb " + configure.benchmark
GDB_HANDLE_BUS = "handle SIGBUS nopass"
GDB_HANDLE_SEGV = "handle SIGSEGV nopass"
GDB_HANDLE_ABT = "handle SIGABRT nopass"
GDB_PRINT_PC = "print $pc"
GDB_CONTINUE = "c"
GDB_NEXT = "stepi"
GDB_PRINT_REG = "print"
GDB_SET_REG = "set"
GDB_FAKE = "0"
GDB_DELETE_BP = "delete breakpoints 1"
GDB_DISPLAY = "x/i $pc"
GDB_ERROR_SEGV = "Program received signal SIGSEGV"
GDB_ERROR_BUS = "Program received signal SIGBUS"
GDB_ERROR_ABT = "Program received signal SIGABT"
is_fake = 1
is_rewind = 1
def is_hexnumber(s):
try:
int(s,16)
return True
except ValueError:
return False
class SigHandler:
def __init__(self, insts, trial):
self.insts = int(insts)
self.trial = trial
def executeProgram(self):
global GDB_LAUNCH, GDB_ARG, GDB_PROMOPT, GDB_RUN, GDB_HANDLE, GDB_ERROR, GDB_NEXT, GDB_CONTINUE, GDB_FAKE
GDB_RUN = "run"
for item in configure.args:
GDB_RUN += " " + item
log = open(str(self.trial), "w")
sys.stdout = log
ori_reg = ""
process = pexpect.spawn(GDB_LAUNCH)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print('ERROR! Could not run GDB')
print(process.before, process.after)
print(str(process))
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
temp = process.before ## just to flush the before buffer
print('Program starts!')
process.sendline(GDB_HANDLE_BUS)
process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
print(process.before)
process.sendline(GDB_HANDLE_SEGV)
process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
print(process.before)
process.sendline(GDB_HANDLE_ABT)
process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
print(process.before)
##
# Set a breakpoint: need pc and iteration number
##
fi = faultinject.FaultInjector(self.insts)
args = fi.getBreakpoint # [regmm, reg, pc, iteration]
if len(args) != 4:
print "Wrong return values! Exit!"
log.close()
process.close()
sys.stdout = sys.__stdout__
return
try:
shutil.rmtree("graphics_output")
print "remove output file 2"
except:
print "Oops, no x.vec file found. Ignoring. 2"
regmm = args[0].rstrip("\n")
reg = args[1].rstrip("\n")
pc = args[2].rstrip("\n")
iteration = int(args[3].rstrip("\n"))
# next = hex(int(args[4]))
print args
hexpc = hex(int(pc))
print hexpc
GDB_BREAKPOINT = "break *" + str(hexpc)
process.sendline(GDB_BREAKPOINT)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print'ERROR! Could not set the breakpoint'
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print process.before
print 'Successfully set the breakpoint'
process.sendline(GDB_RUN)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print 'ERROR! Could not run the program'
print process.before, process.after
print str(process)
log.close()
process.terminate()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
output = process.before
if "Breakpoint" in output:
print "Pause at the breakpoint for the first time!"
# inject a fault
if iteration > 1024:
iteration = random.randint(0, 1024)
print iteration
while iteration > 0:
process.sendline(GDB_CONTINUE)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print 'ERROR while continuing the program'
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
iteration -= 1
###
# print out the current instruction for more info
###
process.sendline(GDB_DISPLAY)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when displaying the inst"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
output = process.before
print output
if regmm == "": # it means that it is a normal instruction and we need to inject the fault to the dest reg
process.sendline(GDB_NEXT)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print 'ERROR! Can not step in'
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
process.sendline(GDB_PRINT_REG + " $" + reg)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print 'ERROR while analyzing the content of the register'
print process.before, process.after
print str(process)
log.close()
sys.stdout = sys.__stdout__
sys.exit(1)
if i == 1:
output = process.before
content = ""
if "0x" in output:
items = output.split(" ")
for item in items:
if "0x" in item:
content = item
else:
items = output.split(" ")
content = items[len(items) - 1]
content = content.lstrip("nan")
content = content.lstrip("-nan")
content = fi.generateFaults(content)
process.sendline(GDB_SET_REG + " $" + reg + "=" + content)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print 'ERROR while waiting for changing the value'
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
output = process.before
if "=" in output:
print "Fault injection is done"
if reg == "": # it means that it is a memory instruction. Need to inject before it is executed.
process.sendline(GDB_PRINT_REG + " $" + regmm)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print 'ERROR while analyzing the content of the register mem'
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
output = process.before
content = ""
if "0x" in output:
items = output.split(" ")
for item in items:
if "0x" in item:
content = item
else:
items = output.split(" ")
content = items[len(items) - 1]
content = content.lstrip("nan")
content = content.lstrip("-nan")
ori_reg = content.rstrip("\r\n")
content = fi.generateFaults(content)
process.sendline(GDB_SET_REG + " $" + regmm + "=" + content)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print 'ERROR while waiting for changing the value mem'
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
output = process.before
if "=" in output:
print "Fault injection is done mem"
## change the regmm back to its original data after execution
## need to single step one inst
'''
process.sendline(GDB_NEXT)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when single step"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "Single step"
output = process.before
if GDB_ERROR_BUS in output or GDB_ERROR_SEGV in output:
print "Crash after single step, considered working!"
process.sendline(GDB_SET_REG+" $"+regmm+"="+ori_reg)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when setting the regmm back after single step"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "Change the value back"
'''
process.sendline(GDB_DELETE_BP)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when deleting breakpoints"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "Delete all breakpoints"
process.sendline(GDB_CONTINUE)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when passing to signal handler"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
output = process.before
print output
if GDB_ERROR_SEGV in output or GDB_ERROR_BUS in output or GDB_ERROR_ABT in output:
##
# Need to pass the current pc to pin, and get all the info
##
if reg == "" and ori_reg != "":
process.sendline(GDB_SET_REG + " $" + regmm + "=" + ori_reg)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when setting the regmm back after single step"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "Change the value back"
######
### LetGo framework steps in
#####
process.sendline(GDB_PRINT_PC)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 1:
# parse the pc value by regex 0x
# send the pc to pin, and get all info we need
print process.before
match = re.findall('0[xX]?[A-Fa-f0-9]+', process.before)
if len(match) == 0:
print "Error while getting no PC!"
log.close()
process.close()
sys.stdout = sys.__stdout__
return
decpc = int(match[0], 0)
args = fi.getNextPC(decpc)
if len(args) != 8:
print "Error while returning incorrect length"
log.close()
process.close()
sys.stdout = sys.__stdout__
return
print args
try:
shutil.rmtree("graphics_output")
print "remove output file 1"
except:
print "Oops, no x.vec file found. Ignoring. 1"
nextpc = args[0]
regwlist = args[1]
stack = args[2]
flag = args[3]
base = args[4]
index = args[5]
displacement = args[6]
scale = args[7]
process.sendline(GDB_PRINT_REG + " $pc=" + str(nextpc))
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when setting the pc value"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
#####
# We can have multiple options here. For now, we feed the value (0) to the supposed-to-write register
#####
if is_fake == 1:
for regw in regwlist:
if flag == 2:
final_b = 0
final_i = 0
final_d = 0
final_s = 0
## we can try to calculate a valid number for regw
if base == "":
print "no base"
continue
process.sendline(GDB_PRINT_REG + " $" + base)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when getting the base"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
basestr = process.before
content = ""
if "0x" in basestr:
items = basestr.split(" ")
for item in items:
if "0x" in item:
content = item
else:
items = basestr.split(" ")
content = items[len(items) - 1]
content = content.lstrip("nan")
content = content.lstrip("-nan")
if "0x" in content:
final_b = int(content, 16)
else:
final_b = int(content)
if index == "null":
print "no index"
else:
process.sendline(GDB_PRINT_REG + " $" + index)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when getting the index"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
indexstr = process.before
content = ""
if "0x" in indexstr:
items = indexstr.split(" ")
for item in items:
if "0x" in item:
content = item
else:
items = indexstr.split(" ")
content = items[len(items) - 1]
content = content.lstrip("nan")
content = content.lstrip("-nan")
if "0x" in content:
final_i = int(content, 16)
else:
final_i = int(content)
final_d = int(displacement)
final_s = int(scale)
address = final_b + final_d + final_i * final_s
process.sendline(GDB_PRINT_REG + " *" + str(address))
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when getting the final value"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
finalres = process.before
content = ""
if "0x" in finalres:
items = finalres.split(" ")
for item in items:
if "0x" in item:
content = item
else:
items = finalres.split(" ")
content = items[len(items) - 1]
content = content.lstrip("nan")
content = content.lstrip("-nan")
process.sendline(GDB_SET_REG + " $" + regw + "=" + content)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when setting the final value"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "set reg with address calculation "
print content
else:
if "xmm" in regw:
regw = regw+".uint128"
process.sendline(GDB_SET_REG + " $" + regw + "=" + GDB_FAKE)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when setting the reg value"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "set reg with fake"
# try to set the rbp and rsp to reasonable values
if is_rewind == 1 and flag == 1:
stackinfo = ["rbp", "rsp"]
if stack != "":
size = fi.get_stack_size()
if size != "":
stackinfo.remove(stack)
rxp = stackinfo[0]
process.sendline(GDB_PRINT_REG + " $" + rxp)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when getting the value of the rbp or rsp"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
output = process.before
content_rxp = ""
if "0x" in output:
items = output.split(" ")
for item in items:
if "0x" in item:
content_rxp = item
else:
items = output.split(" ")
content_rxp = items[len(items) - 1]
content_rxp = content_rxp.lstrip("nan")
content_rxp = content_rxp.lstrip("-nan")
size_rxp = 0
if "0x" in content_rxp:
if is_hexnumber(content_rxp):
size_rxp = int(content_rxp, 16)
else:
if is_number(content_rxp):
size_rxp = int(content_rxp)
process.sendline(GDB_PRINT_REG + " $" + stack)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when getting the value of the rbp or rsp"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
output = process.before
print output
content_stack = ""
if "0x" in output:
items = output.split(" ")
for item in items:
if "0x" in item:
content_stack = item
else:
items = output.split(" ")
content_stack = items[len(items) - 1]
content_stack = content_stack.lstrip("nan")
content_stack = content_stack.lstrip("-nan")
size_stack = 0
if "0x" in content_stack:
if is_hexnumber(content_stack):
size_stack = int(content_stack, 16)
else:
if is_number(content_stack):
size_stack = int(content_stack)
if abs(size_rxp - size_stack) > int(size, 16) and size_stack > size and size_rxp > size:
process.sendline(GDB_SET_REG + " $" + stack + "=" + content_rxp)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when resetting the " + stack
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "Set the " + stack + " back! "
print process.before, process.after
else:
print "Cannot get the size of the current stack frame"
'''
if reg == "":
if "rbp" in regmm or "rsp" in regmm:
process.sendline(GDB_SET_REG+" $"+regmm+"="+str(ori_reg))
i = process.expect([pexpect.TIMEOUT,GDB_PROMOPT])
if i == 0:
print "ERROR when resetting the rbp and rsp"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "Set memory base back!"
print process.before, process.after
if regmm == "":
if "rsi" in reg or "rdi" in reg:
process.sendline(GDB_SET_REG+" $"+reg+"="+str(ori_reg))
process.expect([pexpect.TIMEOUT,GDB_PROMOPT])
if i == 0:
print "ERROR when resetting the rsi and rdi"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "Set index base back!"
print process.before, process.after
'''
print datetime.datetime.now()
process.sendline(GDB_CONTINUE)
i = process.expect([pexpect.TIMEOUT, GDB_PROMOPT])
if i == 0:
print "ERROR when continue after feeding the regsters"
print process.before, process.after
print str(process)
log.close()
process.close()
sys.stdout = sys.__stdout__
return
if i == 1:
print "Application output"
print process.before, process.after
log.close()
process.close()
sys.stdout = sys.__stdout__
print datetime.datetime.now()
else:
print "No triggering crashes"
print "Application output"
print process.before
sys.stdout = sys.__stdout__