-
Notifications
You must be signed in to change notification settings - Fork 13
/
hdf5logger.py
600 lines (516 loc) · 18.3 KB
/
hdf5logger.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
# Copyright (c) 2021 Florian Andreas Hauschild
# Copyright (c) 2021 Fraunhofer AISEC
# Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import queue
import signal
import logging
import time
import numpy
import prctl
import tables
from tqdm import tqdm
logger = logging.getLogger(__name__)
def register_signal_handlers():
"""
Ignore signals, they will be handled by the controller.py anyway
"""
signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGINT, signal.SIG_IGN)
# Tables for storing the elements from queue
class translation_block_exec_table(tables.IsDescription):
tb = tables.UInt64Col()
pos = tables.UInt64Col()
class memory_information_table(tables.IsDescription):
insaddr = tables.Int64Col()
tbid = tables.UInt64Col()
size = tables.UInt64Col()
address = tables.Int64Col()
direction = tables.UInt8Col()
counter = tables.UInt64Col()
class fault_table(tables.IsDescription):
trigger_address = tables.UInt64Col()
trigger_hitcounter = tables.UInt64Col()
fault_address = tables.UInt64Col()
fault_type = tables.UInt8Col()
fault_model = tables.UInt8Col()
fault_lifespan = tables.UInt64Col()
fault_mask_upper = tables.UInt64Col()
fault_mask = tables.UInt64Col()
fault_num_bytes = tables.UInt8Col()
fault_wildcard = tables.BoolCol()
class memory_dump_table(tables.IsDescription):
address = tables.UInt64Col()
length = tables.UInt64Col()
numdumps = tables.UInt64Col()
class arm_registers_table(tables.IsDescription):
pc = tables.UInt64Col()
tbcounter = tables.UInt64Col()
r0 = tables.UInt64Col()
r1 = tables.UInt64Col()
r2 = tables.UInt64Col()
r3 = tables.UInt64Col()
r4 = tables.UInt64Col()
r5 = tables.UInt64Col()
r6 = tables.UInt64Col()
r7 = tables.UInt64Col()
r8 = tables.UInt64Col()
r9 = tables.UInt64Col()
r10 = tables.UInt64Col()
r11 = tables.UInt64Col()
r12 = tables.UInt64Col()
r13 = tables.UInt64Col()
r14 = tables.UInt64Col()
r15 = tables.UInt64Col()
xpsr = tables.UInt64Col()
class riscv_registers_table(tables.IsDescription):
pc = tables.UInt64Col()
tbcounter = tables.UInt64Col()
x0 = tables.UInt64Col()
x1 = tables.UInt64Col()
x2 = tables.UInt64Col()
x3 = tables.UInt64Col()
x4 = tables.UInt64Col()
x5 = tables.UInt64Col()
x6 = tables.UInt64Col()
x7 = tables.UInt64Col()
x8 = tables.UInt64Col()
x9 = tables.UInt64Col()
x10 = tables.UInt64Col()
x11 = tables.UInt64Col()
x12 = tables.UInt64Col()
x13 = tables.UInt64Col()
x14 = tables.UInt64Col()
x15 = tables.UInt64Col()
x16 = tables.UInt64Col()
x17 = tables.UInt64Col()
x18 = tables.UInt64Col()
x19 = tables.UInt64Col()
x20 = tables.UInt64Col()
x21 = tables.UInt64Col()
x22 = tables.UInt64Col()
x23 = tables.UInt64Col()
x24 = tables.UInt64Col()
x25 = tables.UInt64Col()
x26 = tables.UInt64Col()
x27 = tables.UInt64Col()
x28 = tables.UInt64Col()
x29 = tables.UInt64Col()
x30 = tables.UInt64Col()
x31 = tables.UInt64Col()
x32 = tables.UInt64Col()
class config_table(tables.IsDescription):
qemu = tables.StringCol(1000)
kernel = tables.StringCol(1000)
plugin = tables.StringCol(1000)
machine = tables.StringCol(1000)
additional_qemu_args = tables.StringCol(1000)
bios = tables.StringCol(1000)
ring_buffer = tables.BoolCol()
tb_exec_list = tables.BoolCol()
tb_info = tables.BoolCol()
mem_info = tables.BoolCol()
max_instruction_count = tables.UInt64Col()
memory_dump = tables.BoolCol()
fault_count = tables.UInt64Col()
class hash_table(tables.IsDescription):
qemu_hash = tables.StringCol(32)
fault_hash = tables.StringCol(32)
kernel_hash = tables.StringCol(32)
bios_hash = tables.StringCol(32)
hash_function = tables.StringCol(16)
class address_table(tables.IsDescription):
address = tables.UInt64Col()
counter = tables.UInt64Col()
binary_atom = tables.UInt8Atom()
def process_tb_faulted(f, group, tbfaulted_list, myfilter):
assembler_size = max(
(len(tbfaulted["assembly"]) for tbfaulted in tbfaulted_list), default=1
)
class translation_block_faulted_table(tables.IsDescription):
faultaddress = tables.UInt64Col()
assembler = tables.StringCol(assembler_size)
tbfaultedtable = f.create_table(
group,
"tbfaulted",
translation_block_faulted_table,
"Table contains the faulted assembly instructions",
expectedrows=(len(tbfaulted_list)),
filters=myfilter,
)
tbfaultedrow = tbfaultedtable.row
for tbfaulted in tbfaulted_list:
tbfaultedrow["faultaddress"] = tbfaulted["faultaddress"]
tbfaultedrow["assembler"] = tbfaulted["assembly"]
tbfaultedrow.append()
tbfaultedtable.flush()
tbfaultedtable.close()
def process_riscv_registers(f, group, riscvregister_list, myfilter):
riscvregistertable = f.create_table(
group,
"riscvregisters",
riscv_registers_table,
"Table contains riscv registers at specific points.",
expectedrows=(len(riscvregister_list)),
filters=myfilter,
)
riscvregsrow = riscvregistertable.row
for regs in riscvregister_list:
riscvregsrow["pc"] = regs["pc"]
riscvregsrow["tbcounter"] = regs["tbcounter"]
for i in range(0, 33):
riscvregsrow[f"x{i}"] = regs[f"x{i}"]
riscvregsrow.append()
riscvregistertable.flush()
riscvregistertable.close()
def process_arm_registers(f, group, armregisters_list, myfilter):
armregisterstable = f.create_table(
group,
"armregisters",
arm_registers_table,
"Table contains arm registers at specific points.",
expectedrows=(len(armregisters_list)),
filters=myfilter,
)
armregsrow = armregisterstable.row
for regs in armregisters_list:
armregsrow["pc"] = regs["pc"]
armregsrow["tbcounter"] = regs["tbcounter"]
for i in range(0, 16):
armregsrow[f"r{i}"] = regs[f"r{i}"]
armregsrow["xpsr"] = regs["xpsr"]
armregsrow.append()
armregisterstable.flush()
armregisterstable.close()
def process_dumps(f, group, memdumplist, myfilter):
memdumpsgroup = f.create_group(group, "memdumps")
memdumpstable = f.create_table(
memdumpsgroup,
"memdumps",
memory_dump_table,
"Table containing description about the dumps, that are saved as carry.",
expectedrows=(len(memdumplist)),
filters=myfilter,
)
memdumpsrow = memdumpstable.row
for memdump in memdumplist:
name = "location_{:08x}_{:d}_{:d}".format(
memdump["address"], memdump["len"], memdump["numdumps"]
)
if name not in memdumpsgroup._v_children:
memdumpsrow["address"] = memdump["address"]
memdumpsrow["length"] = memdump["len"]
memdumpsrow["numdumps"] = memdump["numdumps"]
# shape = (len(memdump['dumps']), memdump['len'])
dumpnarray = numpy.array(memdump["dumps"])
dumparray = f.create_carray(
memdumpsgroup, name, binary_atom, dumpnarray.shape, filters=myfilter
)
dumparray[:] = dumpnarray
memdumpsrow.append()
memdumpstable.flush()
memdumpstable.close()
def process_faults(f, group, faultlist, endpoint, end_reason, myfilter, name="faults"):
# create table
faulttable = f.create_table(
group,
name,
fault_table,
"Fault list table that contains the fault configuration used for this experiment",
expectedrows=(len(faultlist)),
filters=myfilter,
)
faulttable.attrs.endpoint = endpoint
faulttable.attrs.end_reason = end_reason
faultrow = faulttable.row
for fault in faultlist:
faultrow["trigger_address"] = fault.trigger.address
faultrow["trigger_hitcounter"] = fault.trigger.hitcounter
faultrow["fault_address"] = fault.address
faultrow["fault_type"] = fault.type
faultrow["fault_model"] = fault.model
faultrow["fault_lifespan"] = fault.lifespan
faultrow["fault_mask_upper"] = (fault.mask >> 64) & (pow(2, 64) - 1)
faultrow["fault_mask"] = fault.mask & (pow(2, 64) - 1)
faultrow["fault_num_bytes"] = fault.num_bytes
faultrow["fault_wildcard"] = fault.wildcard
faultrow.append()
faulttable.flush()
faulttable.close()
def process_tbinfo(f, group, tbinfolist, myfilter):
assembler_size = max((len(tbinfo["assembler"]) for tbinfo in tbinfolist), default=1)
class translation_block_table(tables.IsDescription):
identity = tables.UInt64Col()
size = tables.UInt64Col()
ins_count = tables.UInt64Col()
num_exec = tables.UInt64Col()
assembler = tables.StringCol(assembler_size)
tbinfotable = f.create_table(
group,
"tbinfo",
translation_block_table,
"Translation block table containing all information collected by qemu",
expectedrows=(len(tbinfolist)),
filters=myfilter,
)
tbinforow = tbinfotable.row
for tbinfo in tbinfolist:
tbinforow["identity"] = tbinfo["id"]
tbinforow["size"] = tbinfo["size"]
tbinforow["ins_count"] = tbinfo["ins_count"]
tbinforow["num_exec"] = tbinfo["num_exec"]
tbinforow["assembler"] = tbinfo["assembler"]
tbinforow.append()
tbinfotable.flush()
tbinfotable.close()
def process_tbexec(f, group, tbexeclist, myfilter):
tbexectable = f.create_table(
group,
"tbexeclist",
translation_block_exec_table,
"Translation block execution list table",
expectedrows=(len(tbexeclist)),
filters=myfilter,
)
tbexecrow = tbexectable.row
for tbexec in tbexeclist:
tbexecrow["tb"] = tbexec["tb"]
tbexecrow["pos"] = tbexec["pos"]
tbexecrow.append()
tbexectable.flush()
tbexectable.close()
def process_memory_info(f, group, meminfolist, myfilter):
meminfotable = f.create_table(
group,
"meminfo",
memory_information_table,
"",
expectedrows=(len(meminfolist)),
filters=myfilter,
)
meminforow = meminfotable.row
for meminfo in meminfolist:
meminforow["insaddr"] = meminfo["ins"]
meminforow["tbid"] = meminfo["tbid"]
meminforow["size"] = meminfo["size"]
meminforow["address"] = meminfo["address"]
meminforow["direction"] = meminfo["direction"]
meminforow["counter"] = meminfo["counter"]
meminforow.append()
meminfotable.flush()
meminfotable.close()
def process_config(f, configgroup, exp, myfilter):
hashtable = f.create_table(
configgroup,
"hash",
hash_table,
"",
expectedrows=1,
filters=myfilter,
)
hash_table_row = hashtable.row
for file, f_hash in exp["hash"].items():
hash_table_row[file] = f_hash
hash_table_row["hash_function"] = exp["hash_function"]
hash_table_row.append()
hashtable.flush()
hashtable.close()
configtable = f.create_table(
configgroup,
"parameters",
config_table,
"",
expectedrows=1,
filters=myfilter,
)
config_row = configtable.row
config_row["qemu"] = exp["qemu"]
config_row["kernel"] = exp["kernel"]
config_row["plugin"] = exp["plugin"]
config_row["machine"] = exp["machine"]
config_row["additional_qemu_args"] = exp["additional_qemu_args"]
config_row["bios"] = exp["bios"]
config_row["ring_buffer"] = exp["ring_buffer"]
config_row["tb_exec_list"] = exp["tb_exec_list"]
config_row["tb_info"] = exp["tb_info"]
config_row["mem_info"] = exp["mem_info"]
config_row["max_instruction_count"] = exp["max_instruction_count"]
config_row["fault_count"] = exp["fault_count"]
config_row.append()
configtable.flush()
configtable.close()
starttable = f.create_table(
configgroup,
"start_address",
address_table,
"",
expectedrows=(len(exp["start"])),
filters=myfilter,
)
start_row = starttable.row
start_row["address"] = exp["start"]["address"]
start_row["counter"] = exp["start"]["counter"]
start_row.append()
starttable.flush()
starttable.close()
endtable = f.create_table(
configgroup,
"end_addresses",
address_table,
"",
expectedrows=(len(exp["end"])),
filters=myfilter,
)
end_row = endtable.row
for endpoint in exp["end"]:
end_row["address"] = endpoint["address"]
end_row["counter"] = endpoint["counter"]
end_row.append()
endtable.flush()
endtable.close()
def process_backup(f, configgroup, exp, myfilter, stop_signal):
exp["config"]["fault_count"] = len(exp["expanded_faultlist"])
process_config(f, configgroup, exp["config"], myfilter)
fault_expanded_group = f.create_group(
configgroup, "expanded_faults", "Group containing expanded input faults"
)
tmp = "{}".format(len(exp["expanded_faultlist"]))
exp_name = "experiment{:0" + "{}".format(len(tmp)) + "d}"
for exp_number in tqdm(
range(len(exp["expanded_faultlist"])), desc="Creating backup"
):
if stop_signal.value == 1:
break
exp_group = f.create_group(
fault_expanded_group, exp_name.format(exp_number), "Group containing faults"
)
process_faults(
f,
exp_group,
exp["expanded_faultlist"][exp_number]["faultlist"],
0,
"not executed",
myfilter,
)
def hdf5collector(
hdf5path,
mode,
queue_output,
num_exp,
stop_signal,
compressionlevel,
logger_postprocess=None,
log_goldenrun=True,
log_config=False,
overwrite_faults=False,
):
register_signal_handlers()
prctl.set_name("logger")
prctl.set_proctitle("logger")
f = tables.open_file(hdf5path, mode, max_group_width=65536)
if "fault" in f.root:
fault_group = f.root.fault
else:
fault_group = f.create_group("/", "fault", "Group containing fault results")
myfilter = tables.Filters(complevel=compressionlevel, complib="zlib")
t0 = time.time()
tmp = "{}".format(num_exp)
groupname = "experiment{:0" + "{}".format(len(tmp)) + "d}"
log_pregoldenrun = log_goldenrun
if overwrite_faults:
for n in tqdm(
f.root.fault._f_iter_nodes(),
desc="Clearing old faults",
total=f.root.fault._v_nchildren,
):
n._f_remove(recursive=True)
pbar = tqdm(total=num_exp, desc="Simulating faults", disable=not num_exp)
while num_exp > 0 or log_goldenrun or log_pregoldenrun or log_config:
if stop_signal.value == 1:
break
# readout queue and get next output from qemu. Will block
try:
exp = queue_output.get_nowait()
except queue.Empty:
continue
t1 = time.time()
logger.debug(
"got exp {}, {} still need to be performed. Took {}s. Elements in queu: {}".format(
exp["index"], num_exp, t1 - t0, queue_output.qsize()
)
)
t0 = t1
# create experiment group in file
if exp["index"] >= 0:
index = exp["index"]
while groupname.format(index) in fault_group:
index = index + 1
exp_group = f.create_group(fault_group, groupname.format(index))
if exp["index"] != index:
logger.warning(
"The index provided was already used. found new one: {}".format(
index
)
)
num_exp = num_exp - 1
pbar.update(1)
elif exp["index"] == -2 and log_pregoldenrun:
if "Pregoldenrun" in f.root:
raise ValueError("Pregoldenrun already exists!")
exp_group = f.create_group(
"/",
"Pregoldenrun",
"Group containing all information regarding firmware running before start point is reached",
)
log_pregoldenrun = False
elif exp["index"] == -1 and log_goldenrun:
if "Goldenrun" in f.root:
raise ValueError("Goldenrun already exists!")
exp_group = f.create_group(
"/", "Goldenrun", "Group containing all information about goldenrun"
)
log_goldenrun = False
elif exp["index"] == -3 and log_config:
if "backup" in f.root:
raise ValueError("Backup already exists!")
exp_group = f.create_group(
"/", "Backup", "Group containing backup and run information"
)
process_backup(f, exp_group, exp, myfilter, stop_signal)
log_config = False
continue
else:
continue
datasets = []
datasets.append((process_tbinfo, "tbinfo"))
datasets.append((process_tbexec, "tbexec"))
datasets.append((process_memory_info, "meminfo"))
datasets.append((process_dumps, "memdumplist"))
datasets.append((process_arm_registers, "armregisters"))
datasets.append((process_riscv_registers, "riscvregisters"))
datasets.append((process_tb_faulted, "tbfaulted"))
for fn_ptr, keyword in datasets:
if keyword not in exp:
continue
fn_ptr(f, exp_group, exp[keyword], myfilter)
# safe fault config
process_faults(
f, exp_group, exp["faultlist"], exp["endpoint"], exp["end_reason"], myfilter
)
if callable(logger_postprocess):
logger_postprocess(f, exp_group, exp, myfilter)
del exp
pbar.close()
f.close()
logger.debug("Data Logging done")