This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
executable file
·1446 lines (1240 loc) · 48.1 KB
/
main.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import copy
import hashlib
import json
import logging
import os
import random
import re
import subprocess
import sys
import tempfile
import time
import urllib.parse
from multiprocessing import Pool
from pathlib import Path
from typing import Any, Dict, Optional, cast
import requests
import ccbuilder
from ccbuilder import (
Builder,
BuildException,
PatchDB,
Repo,
get_compiler_info,
get_compiler_project,
)
from ccbuilder.utils.utils import select_repo
import bisector
import checker
import database
import generator
import init
import parsers
import preprocessing
import reducer
import utils
def get_llvm_github_commit_author(rev: str) -> Optional[str]:
html = requests.get(
"https://github.com/llvm/llvm-project/commit/" + rev
).content.decode()
p = re.compile(r'.*\/llvm\/llvm-project\/commits\?author=(.*)".*')
for l in html.split("\n"):
l = l.strip()
if m := p.match(l):
return m.group(1)
return None
def check_llvm_issues(rev: str) -> bool:
print(f"Looking for existing issues...", end="", file=sys.stderr)
url_pre = f"https://api.github.com/search/issues?q={rev} repo:llvm/llvm-project"
open_issues = json.loads(requests.get(url_pre + " is:open").content)
closed_issues = json.loads(requests.get(url_pre + " is:closed").content)
issues = open_issues["items"] + closed_issues["items"]
if issues:
print(
f"!!!\nWarning: The following issues already contain the revision {rev}!",
file=sys.stderr,
)
for issue in issues:
print(issue["html_url"], file=sys.stderr)
return False
print(f"found none", file=sys.stderr)
return True
def check_gcc_issues(rev: str) -> bool:
print(f"Looking for existing issues...", end="", file=sys.stderr)
url_pre = f"https://gcc.gnu.org/bugzilla/rest/bug?quicksearch={rev}"
issues = json.loads(requests.get(url_pre).content)["bugs"]
if issues:
print(
f"!!!\nWarning: The following issues already contain the revision {rev}!",
file=sys.stderr,
)
for issue in issues:
print(
f"https://gcc.gnu.org/bugzilla/show_bug.cgi?id={issue['id']}",
file=sys.stderr,
)
return False
print(f"found none", file=sys.stderr)
return True
def get_all_bisections(ddb: database.CaseDatabase) -> list[str]:
res = ddb.con.execute("select distinct bisection from cases")
return [r[0] for r in res]
def _run() -> None:
scenario = utils.get_scenario(config, args)
counter = 0
output_directory = (
Path(args.output_directory).absolute() if args.output_directory else None
)
parallel_generator = (
gnrtr.parallel_interesting_case(config, scenario, args.cores, start_stop=True)
if args.parallel_generation
else None
)
pipeline_components = (
["Generator<" + "parallel>" if args.parallel_generation else "single>"]
+ (["Bisector"] if args.bisector else [])
+ (
["Reducer<Only New>"]
if args.reducer is None
else (["Reducer<Always>"] if args.reducer == True else [])
)
)
print("Pipeline:", " -> ".join(pipeline_components), file=sys.stderr)
last_update_time = time.time()
while True:
if args.amount and args.amount != 0:
if counter >= args.amount:
break
if args.update_trunk_after_X_hours is not None:
if (
time.time() - last_update_time
) / 3600 > args.update_trunk_after_X_hours:
logging.info("Updating repositories...")
last_update_time = time.time()
known: Dict[str, list[int]] = dict()
for i, s in enumerate(scenario.target_settings):
cname = s.compiler_project.to_string()
if cname not in known:
known[cname] = []
known[cname].append(i)
for cname, l in known.items():
repo = select_repo(
scenario.target_settings[l[0]].compiler_project,
bldr.gcc_repo,
bldr.llvm_repo,
)
old_trunk_commit = repo.rev_to_commit("trunk")
repo.pull()
new_trunk_commit = repo.rev_to_commit("trunk")
for i in l:
if scenario.target_settings[i].rev == old_trunk_commit:
scenario.target_settings[i].rev = new_trunk_commit
# Time db values
generator_time: Optional[float] = None
generator_try_count: Optional[int] = None
bisector_time: Optional[float] = None
bisector_steps: Optional[int] = None
reducer_time: Optional[float] = None
if parallel_generator:
case = next(parallel_generator)
else:
time_start_gen = time.perf_counter()
case = gnrtr.generate_interesting_case(scenario)
time_end_gen = time.perf_counter()
generator_time = time_end_gen - time_start_gen
generator_try_count = gnrtr.try_counter
if args.bisector:
try:
time_start_bisector = time.perf_counter()
bisect_worked = bsctr.bisect_case(case)
time_end_bisector = time.perf_counter()
bisector_time = time_end_bisector - time_start_bisector
bisector_steps = bsctr.steps
if not bisect_worked:
continue
except bisector.BisectionException as e:
print(f"BisectionException: '{e}'", file=sys.stderr)
continue
except AssertionError as e:
print(f"AssertionError: '{e}'", file=sys.stderr)
continue
except BuildException as e:
print(f"BuildException: '{e}'", file=sys.stderr)
continue
if args.reducer is not False:
if (
args.reducer
or case.bisection
and not case.bisection in get_all_bisections(ddb)
):
try:
time_start_reducer = time.perf_counter()
worked = rdcr.reduce_case(case)
time_end_reducer = time.perf_counter()
reducer_time = time_end_reducer - time_start_reducer
except BuildException as e:
print(f"BuildException: {e}")
continue
if not output_directory:
case_id = ddb.record_case(case)
ddb.record_timing(
case_id,
generator_time,
generator_try_count,
bisector_time,
bisector_steps,
reducer_time,
)
else:
h = abs(hash(str(case)))
path = output_directory / Path(f"case_{counter:08}-{h:019}.tar")
logging.debug("Writing case to {path}...")
case.to_file(path)
counter += 1
def _absorb() -> None:
def read_into_db(file: Path) -> None:
# Why another db here?
# https://docs.python.org/3/library/sqlite3.html#sqlite3.threadsafety
# “Threads may share the module, but not connections.”
# Of course we are using multiple processes here, but the processes
# are a copy of eachother and who knows how things are implemented,
# so better be safe than sorry and create a new connection,
# especially when the next sentence is:
# "However, this may not always be true."
# (They may just refer to the option of having sqlite compiled with
# SQLITE_THREADSAFE=0)
db = database.CaseDatabase(config, config.casedb)
case = utils.Case.from_file(config, file)
db.record_case(case)
if Path(args.absorb_object).is_file():
read_into_db(Path(args.absorb_object))
exit(0)
pool = Pool(10)
absorb_directory = Path(args.absorb_object).absolute()
paths = [p for p in absorb_directory.iterdir() if p.match("*.tar")]
len_paths = len(paths)
len_len_paths = len(str(len_paths))
print("Absorbing... ", end="", flush=True)
status_str = ""
counter = 0
start_time = time.perf_counter()
for _ in pool.imap_unordered(read_into_db, paths):
counter += 1
print("\b" * len(status_str), end="", flush=True)
delta_t = time.perf_counter() - start_time
status_str = f"{{: >{len_len_paths}}}/{len_paths} {delta_t:.2f}s".format(
counter
)
print(status_str, end="", flush=True)
print("")
def _tofile() -> None:
case_pre = ddb.get_case_from_id(args.case_id)
if not case_pre:
print(f"Found no case for ID {args.case_id}")
exit(1)
else:
case = case_pre
print(f"Saving case to ./case_{args.case_id}.tar")
case.to_file(Path(f"./case_{args.case_id}.tar"))
def _rereduce() -> None:
with open(args.code_path, "r") as f:
rereduce_code = f.read()
case = ddb.get_case_from_id_or_die(args.case_id)
print(f"Re-reducing code with respect to Case {args.case_id}", file=sys.stderr)
res = rdcr.reduce_code(
rereduce_code,
case.marker,
case.bad_setting,
case.good_settings,
bisection=case.bisection,
preprocess=False,
)
print(res)
def _report() -> None:
pre_check_case = ddb.get_case_from_id(args.case_id)
if not pre_check_case:
print("No case with this ID.", file=sys.stderr)
exit(1)
else:
case = pre_check_case
if not case.bisection:
print("Case is not bisected. Starting bisection...", file=sys.stderr)
start_time = time.perf_counter()
worked = bsctr.bisect_case(case)
bisector_time = time.perf_counter() - start_time
if worked:
ddb.update_case(args.case_id, case)
g_time, gtc, b_time, b_steps, r_time = ddb.get_timing_from_id(args.case_id)
b_time = bisector_time
b_steps = bsctr.steps
ddb.record_timing(args.case_id, g_time, gtc, b_time, b_steps, r_time)
else:
print("Could not bisect case. Aborting...", file=sys.stderr)
exit(1)
# check for reduced and massaged code
if not case.reduced_code:
print("Case is not reduced. Starting reduction...", file=sys.stderr)
if rdcr.reduce_case(case):
ddb.update_case(args.case_id, case)
else:
print("Could not reduce case. Aborting...", file=sys.stderr)
exit(1)
massaged_code, _, _ = ddb.get_report_info_from_id(args.case_id)
if massaged_code:
case.reduced_code = massaged_code
bad_setting = case.bad_setting
bad_repo = select_repo(
bad_setting.compiler_project, gcc_repo=bldr.gcc_repo, llvm_repo=bldr.llvm_repo
)
is_gcc: bool = bad_setting.compiler_project.to_string() == "gcc"
# Last sanity check
cpy = copy.deepcopy(case)
cpy.code = cast(str, case.reduced_code)
print("Normal interestingness test...", end="", file=sys.stderr, flush=True)
if not chkr.is_interesting(cpy, preprocess=False):
print("\nCase is not interesting! Aborting...", file=sys.stderr)
exit(1)
else:
print("OK", file=sys.stderr)
# Check against newest upstream
if args.pull:
print("Pulling Repo...", file=sys.stderr)
bad_repo.pull()
print("Interestingness test against main...", end="", file=sys.stderr)
cpy.bad_setting.rev = bad_repo.rev_to_commit(f"{bad_repo.main_branch}")
if not chkr.is_interesting(cpy, preprocess=False):
print(
"\nCase is not interesting on main! Might be fixed. Stopping...",
file=sys.stderr,
)
exit(0)
else:
print("OK", file=sys.stderr)
# Use newest main in report
case.bad_setting.rev = cpy.bad_setting.rev
# Check if bisection commit is what it should be
print("Checking bisection commit...", file=sys.stderr)
marker_prefix = utils.get_marker_prefix(case.marker)
bisection_setting = copy.deepcopy(cpy.bad_setting)
bisection_setting.rev = cast(str, cpy.bisection)
prebisection_setting = copy.deepcopy(bisection_setting)
repo = select_repo(
bisection_setting.compiler_project,
llvm_repo=bldr.llvm_repo,
gcc_repo=bldr.gcc_repo,
)
prebisection_setting.rev = repo.rev_to_commit(f"{case.bisection}~")
bis_set = utils.find_alive_markers(cpy.code, bisection_setting, marker_prefix, bldr)
rebis_set = utils.find_alive_markers(
cpy.code, prebisection_setting, marker_prefix, bldr
)
if not cpy.marker in bis_set or cpy.marker in rebis_set:
print("Bisection commit is not correct! Aborting...", file=sys.stderr)
exit(1)
# Choose same opt level and newest version
possible_good_compiler = [
gs for gs in case.good_settings if gs.opt_level == bad_setting.opt_level
]
good_setting = utils.get_latest_compiler_setting_from_list(
bad_repo, possible_good_compiler
)
# Replace markers
source = cpy.code.replace(cpy.marker, "foo").replace(
utils.get_marker_prefix(cpy.marker), "bar"
)
bad_setting_tag = bad_setting.rev + " (trunk)"
bad_setting_str = f"{bad_setting.compiler_project.to_string()}-{bad_setting_tag} -O{bad_setting.opt_level}"
tmp = bad_repo.rev_to_tag(good_setting.rev)
if not tmp:
good_setting_tag = good_setting.rev
else:
good_setting_tag = tmp
good_setting_str = f"{good_setting.compiler_project.to_string()}-{good_setting_tag} -O{good_setting.opt_level}"
def to_collapsed(
s: str, is_gcc: bool, summary: str = "Output", open: bool = False
) -> str:
if is_gcc:
s = (
"--------- OUTPUT ---------\n"
+ s
+ "\n---------- END OUTPUT ---------\n"
)
else:
sopen = "open" if open else ""
s = (
f"<details {sopen}><summary>{summary}</summary><p>\n"
+ s
+ "\n</p></details>"
)
return s
def to_code(code: str, is_gcc: bool, stype: str = "") -> str:
if not is_gcc:
return f"\n```{stype}\n" + code.rstrip() + "\n```"
return code
def print_cody_str(s: str, is_gcc: bool) -> None:
s = "`" + s + "`"
print(s)
def to_cody_str(s: str, is_gcc: bool) -> str:
if not is_gcc:
s = "`" + s + "`"
return s
def replace_rand(code: str) -> str:
# Replace .file with case.c
ex = re.compile(r"\t\.file\t(\".*\")")
m = ex.search(code)
if m:
res = m.group(1)
return code.replace(res, '"case.c"')
return code
def replace_file_name_IR(ir: str) -> str:
head = "; ModuleID = 'case.c'\n" + 'source_filename = "case.c"\n'
tail = ir.split("\n")[2:]
ir = head + "\n".join(tail)
return ir
def keep_only_main(code: str) -> str:
lines = list(code.split("\n"))
first = 0
for i, line in enumerate(lines):
if "main:" in line:
first = i
break
last = first + 1
ex = re.compile(".*.cfi_endproc")
for i, line in enumerate(lines[last:], start=last):
if ex.match(line):
last = i
break
return "\n".join(lines[first:last])
def prep_asm(asm: str, is_gcc: bool) -> str:
asm = replace_rand(asm)
asm = keep_only_main(asm)
asm = to_code(asm, is_gcc, "asm")
asm = to_collapsed(asm, is_gcc, summary="Reduced assembly")
return asm
def prep_IR(ir: str) -> str:
ir = replace_file_name_IR(ir)
ir = to_code(ir, False, "ll")
ir = to_collapsed(ir, False, summary="Emitted IR")
return ir
# Title
gcc_describe_name_parts: list[str] = []
gcc_describe_name: str = ""
if is_gcc:
# GCC uses something close to `git describe` to identify commits in bugzilla.
# Example: r13-1759-gdbb093f4f15
# We were asked to use this style in the title and the report.
gcc_describe_name_parts = utils.run_cmd(
f"git -C {repo.path} describe {case.bisection}"
).split("-")[1:]
gcc_describe_name = "r" + "-".join(gcc_describe_name_parts)
print(
f"[{gcc_describe_name_parts[0]} Regression] Dead Code Elimination Regression at -O{bad_setting.opt_level} since {gcc_describe_name}"
)
# Get email to CC
print(
"CC to include:",
utils.run_cmd(f"git -C {repo.path} log -1 --format='%ae' {case.bisection}"),
)
else:
print(
f"Dead Code Elimination Regression at -O{bad_setting.opt_level} (trunk vs. {good_setting_tag.split('-')[-1]}) {args.case_id}"
)
print("---------------")
print(to_cody_str(f"cat case.c #{args.case_id}", is_gcc))
print(to_code(source, is_gcc, "c"))
print(
f"`{bad_setting_str}` can not eliminate `foo` but `{good_setting_str}` can.\n"
)
# Compile
if is_gcc:
asm_bad = utils.get_asm_str(source, case.bad_setting, bldr)
asm_good = utils.get_asm_str(source, good_setting, bldr)
print_cody_str(f"{bad_setting_str} -S -o /dev/stdout case.c", is_gcc)
print(prep_asm(asm_bad, is_gcc))
print()
print_cody_str(f"{good_setting_str} -S -o /dev/stdout case.c", is_gcc)
print(prep_asm(asm_good, is_gcc))
print()
print(f"Bisects to: {gcc_describe_name}")
print()
print(utils.run_cmd(f"git -C {repo.path} log -1 {case.bisection}"))
else:
print("Target: `x86_64-unknown-linux-gnu`")
ir_bad = utils.get_llvm_IR(source, case.bad_setting, bldr)
ir_good = utils.get_llvm_IR(source, good_setting, bldr)
asm_bad = utils.get_asm_str(source, case.bad_setting, bldr)
asm_good = utils.get_asm_str(source, good_setting, bldr)
print("\n------------------------------------------------\n")
print_cody_str(f"{bad_setting_str} -emit-llvm -S -o /dev/stdout case.c", is_gcc)
print(prep_IR(ir_bad))
print()
print("\n------------------------------------------------\n")
print_cody_str(
f"{good_setting_str} -emit-llvm -S -o /dev/stdout case.c", is_gcc
)
print()
print(prep_IR(ir_good))
print("\n------------------------------------------------\n")
print("### Bisection")
bisection_setting = copy.deepcopy(case.bad_setting)
bisection_setting.rev = cast(str, case.bisection)
print(f"Bisected to: {case.bisection}")
author = get_llvm_github_commit_author(cast(str, case.bisection))
if author:
print(f"Committed by: @{author}")
print("\n------------------------------------------------\n")
bisection_ir = utils.get_llvm_IR(source, bisection_setting, bldr)
print(
to_cody_str(
f"{bisection_setting.report_string()} -emit-llvm -S -o /dev/stdout case.c",
is_gcc,
)
)
print(prep_IR(bisection_ir))
print("\n------------------------------------------------\n")
prebisection_setting = copy.deepcopy(bisection_setting)
prebisection_setting.rev = bad_repo.rev_to_commit(f"{bisection_setting.rev}~")
print(f"Previous commit: {prebisection_setting.rev}")
print(
"\n"
+ to_cody_str(
f"{prebisection_setting.report_string()} -emit-llvm -S -o /dev/stdout case.c",
is_gcc,
)
)
prebisection_ir = utils.get_llvm_IR(source, prebisection_setting, bldr)
print()
print(prep_IR(prebisection_ir))
with open("case.txt", "w") as f:
f.write(source)
with open("case.c", "w") as f:
f.write(source)
print("Saved case.txt and case.c...", file=sys.stderr)
print("Manual check commands:", file=sys.stderr)
for s in [bad_setting, good_setting]:
exe_path = bldr.build(s.compiler_project, s.rev, get_executable=True)
print(
f"{exe_path} -O{s.opt_level} -Wall -Wextra -Wpedantic -S -o out.s case.c",
file=sys.stderr,
)
if is_gcc:
check_gcc_issues(cast(str, case.bisection))
else:
check_llvm_issues(cast(str, case.bisection))
def _diagnose() -> None:
width = 50
def ok_fail(b: bool) -> str:
if b:
return "OK"
else:
return "FAIL"
def nice_print(name: str, value: str) -> None:
print(("{:.<" f"{width}}}").format(name), value)
if args.case_id:
case = ddb.get_case_from_id_or_die(args.case_id)
else:
case = utils.Case.from_file(config, Path(args.file))
repo = select_repo(
case.bad_setting.compiler_project,
llvm_repo=bldr.llvm_repo,
gcc_repo=bldr.gcc_repo,
)
if args.targets or args.additional_compilers:
if not args.targets_default_opt_levels:
args.targets_default_opt_levels = [case.bad_setting.opt_level]
if not args.additional_compilers_default_opt_levels:
args.additional_compilers_default_opt_levels = [case.bad_setting.opt_level]
scenario = utils.get_scenario(config, args)
if scenario.target_settings:
scenario.target_settings[
0
].additional_flags = case.bad_setting.additional_flags
case.bad_setting = scenario.target_settings[0]
if scenario.attacker_settings:
for gs in scenario.attacker_settings:
gs.additional_flags = case.good_settings[0].additional_flags
case.good_settings = scenario.attacker_settings
# Replace
def sanitize_values(
config: utils.NestedNamespace,
case: utils.Case,
prefix: str,
chkr: checker.Checker,
) -> None:
empty_body_code = chkr._empty_marker_code_str(case)
with tempfile.NamedTemporaryFile(suffix=".c") as tf:
with open(tf.name, "w") as f:
f.write(empty_body_code)
res_comp_warnings = checker.check_compiler_warnings(
config.llvm.sane_version,
config.gcc.sane_version,
Path(tf.name),
case.bad_setting.get_flag_str(),
10,
)
nice_print(
prefix + "Sanity: compiler warnings",
ok_fail(res_comp_warnings),
)
res_use_ub_san = checker.use_ub_sanitizers(
config.llvm.sane_version,
Path(tf.name),
case.bad_setting.get_flag_str(),
10,
10,
)
nice_print(
prefix + "Sanity: undefined behaviour", ok_fail(res_use_ub_san)
)
res_ccomp = checker.verify_with_ccomp(
config.ccomp,
Path(tf.name),
case.bad_setting.get_flag_str(),
10,
)
nice_print(
prefix + "Sanity: ccomp",
ok_fail(res_ccomp),
)
def checks(case: utils.Case, prefix: str) -> None:
nice_print(
prefix + "Check marker", ok_fail(chkr.is_interesting_wrt_marker(case))
)
nice_print(prefix + "Check CCC", ok_fail(chkr.is_interesting_wrt_ccc(case)))
nice_print(
prefix + "Check static. annotated",
ok_fail(chkr.is_interesting_with_static_globals(case)),
)
res_empty = chkr.is_interesting_with_empty_marker_bodies(case)
nice_print(prefix + "Check empty bodies", ok_fail(res_empty))
if not res_empty:
sanitize_values(config, case, prefix, chkr)
print(("{:=^" f"{width}}}").format(" Values "))
nice_print("Marker", case.marker)
nice_print("Code lenght", str(len(case.code)))
nice_print("Bad Setting", str(case.bad_setting))
same_opt = [
gs for gs in case.good_settings if gs.opt_level == case.bad_setting.opt_level
]
nice_print(
"Newest Good Setting",
str(utils.get_latest_compiler_setting_from_list(repo, same_opt)),
)
checks(case, "")
cpy = copy.deepcopy(case)
if not (
code_pp := preprocessing.preprocess_csmith_code(
case.code, utils.get_marker_prefix(case.marker), case.bad_setting, bldr
)
):
print("Code could not be preprocessed. Skipping perprocessed checks")
else:
cpy.code = code_pp
checks(cpy, "PP: ")
if case.reduced_code:
cpy = copy.deepcopy(case)
cpy.code = case.reduced_code
checks(cpy, "Reduced: ")
if args.case_id:
massaged_code, _, _ = ddb.get_report_info_from_id(args.case_id)
if massaged_code:
cpy.code = massaged_code
checks(cpy, "Massaged: ")
if case.bisection:
cpy = copy.deepcopy(case)
nice_print("Bisection", case.bisection)
cpy.bad_setting.rev = case.bisection
prev_rev = repo.rev_to_commit(case.bisection + "~")
nice_print("Bisection prev commit", prev_rev)
bis_res_og = chkr.is_interesting(cpy, preprocess=False)
cpy.bad_setting.rev = prev_rev
bis_prev_res_og = chkr.is_interesting(cpy, preprocess=False)
nice_print(
"Bisection test original code", ok_fail(bis_res_og and not bis_prev_res_og)
)
cpy = copy.deepcopy(case)
if cpy.reduced_code:
cpy.code = cpy.reduced_code
cpy.bad_setting.rev = case.bisection
bis_res = chkr.is_interesting(cpy, preprocess=False)
cpy.bad_setting.rev = prev_rev
bis_prev_res = chkr.is_interesting(cpy, preprocess=False)
nice_print(
"Bisection test reduced code", ok_fail(bis_res and not bis_prev_res)
)
if case.reduced_code:
print(case.reduced_code)
def _check_reduced() -> None:
"""Check code against every good and bad setting of a case.
Args:
Returns:
None:
"""
def ok_fail(b: bool) -> str:
if b:
return "OK"
else:
return "FAIL"
def nice_print(name: str, value: str) -> None:
width = 100
print(("{:.<" f"{width}}}").format(name), value)
with open(args.code_path, "r") as f:
new_code = f.read()
case = ddb.get_case_from_id_or_die(args.case_id)
prefix = utils.get_marker_prefix(case.marker)
bad_alive = utils.find_alive_markers(new_code, case.bad_setting, prefix, bldr)
nice_print(f"Bad {case.bad_setting}", ok_fail(case.marker in bad_alive))
for gs in case.good_settings:
good_alive = utils.find_alive_markers(new_code, gs, prefix, bldr)
nice_print(f"Good {gs}", ok_fail(case.marker not in good_alive))
case.code = new_code
case.reduced_code = new_code
if case.bisection:
project_repo = select_repo(
case.bad_setting.compiler_project,
llvm_repo=bldr.llvm_repo,
gcc_repo=bldr.gcc_repo,
)
prev_rev = project_repo.rev_to_commit(f"{case.bisection}~")
cpy = copy.deepcopy(case)
cpy.bad_setting.rev = case.bisection
bis_res_og = case.marker in utils.find_alive_markers(
new_code, cpy.bad_setting, prefix, bldr
)
cpy.bad_setting.rev = prev_rev
bis_prev_res_og = case.marker in utils.find_alive_markers(
new_code, cpy.bad_setting, prefix, bldr
)
nice_print("Bisection test", ok_fail(bis_res_og and not bis_prev_res_og))
cpy = copy.deepcopy(case)
else:
print("No bisection found! Please bisect the case first.")
nice_print("Check", ok_fail(chkr.is_interesting(case, preprocess=False)))
# Useful when working with watch -n 0 to see that something happened
print(random.randint(0, 1000))
def _cache() -> None:
if args.what == "clean":
print("Cleaning...")
for c in Path(config.cachedir).iterdir():
if not (c / "DONE").exists():
try:
os.rmdir(c)
except FileNotFoundError:
print(c, "spooky. It just disappeared...")
except OSError:
print(c, "is not empty but also not done!")
print("Done")
elif args.what == "stats":
count_gcc = 0
count_clang = 0
for c in Path(config.cachedir).iterdir():
if c.name.startswith("clang"):
count_clang += 1
else:
count_gcc += 1
tot = count_gcc + count_clang
print("Amount compilers:", tot)
print("Amount clang: {} {:.2f}%".format(count_clang, count_clang / tot * 100))
print("Amount GCC: {} {:.2f}%".format(count_gcc, count_gcc / tot * 100))
def _asm() -> None:
def save_wrapper(name: str, content: str) -> None:
utils.save_to_file(Path(name + ".s"), content)
print(f"Saving {name + '.s'}...")
case = ddb.get_case_from_id_or_die(args.case_id)
bad_repo = select_repo(
case.bad_setting.compiler_project,
llvm_repo=bldr.llvm_repo,
gcc_repo=bldr.gcc_repo,
)
same_opt = [
gs for gs in case.good_settings if gs.opt_level == case.bad_setting.opt_level
]
good_setting = utils.get_latest_compiler_setting_from_list(bad_repo, same_opt)
asmbad = utils.get_asm_str(case.code, case.bad_setting, bldr)
asmgood = utils.get_asm_str(case.code, good_setting, bldr)
save_wrapper("asmbad", asmbad)
save_wrapper("asmgood", asmgood)
if case.reduced_code:
reducedasmbad = utils.get_asm_str(case.reduced_code, case.bad_setting, bldr)
reducedasmgood = utils.get_asm_str(case.reduced_code, good_setting, bldr)
save_wrapper("reducedasmbad", reducedasmbad)
save_wrapper("reducedasmgood", reducedasmgood)
if case.bisection:
bisection_setting = copy.deepcopy(case.bad_setting)
bisection_setting.rev = case.bisection
asmbisect = utils.get_asm_str(case.code, bisection_setting, bldr)
save_wrapper("asmbisect", asmbisect)
if case.reduced_code:
reducedasmbisect = utils.get_asm_str(
case.reduced_code, bisection_setting, bldr
)
save_wrapper("reducedasmbisect", reducedasmbisect)
print(case.marker)
def _get() -> None:
# Why are you printing code with end=""?
case_id: int = int(args.case_id)
if args.what in ["ocode", "rcode", "bisection", "marker"]:
case = ddb.get_case_from_id_or_die(args.case_id)
if args.what == "ocode":
print(case.code, end="")
return
elif args.what == "rcode":
print(case.reduced_code, end="")
return
elif args.what == "bisection":
print(case.bisection, end="")
return
elif args.what == "marker":
print(case.marker, end="")
return
else:
mcode, link, fixed = ddb.get_report_info_from_id(case_id)
if args.what == "link":
print(link)
return
elif args.what == "fixed":
print(fixed)
return
elif args.what == "mcode":
print(mcode, end="")
return
logging.warning(
"Whoops, this should not have"
" happened because the parser forces "
"`what` to only allow some strings."
)
return
def _set() -> None:
case_id: int = int(args.case_id)
case = ddb.get_case_from_id_or_die(case_id)
mcode, link, fixed = ddb.get_report_info_from_id(case_id)
repo = select_repo(
case.bad_setting.compiler_project,
llvm_repo=bldr.llvm_repo,
gcc_repo=bldr.gcc_repo,
)
if args.what == "ocode":
with open(args.var, "r") as f:
new_code = f.read()
case.code = new_code
if chkr.is_interesting(case):
ddb.update_case(case_id, case)
else:
logging.critical(
"The provided code is not interesting wrt to the case. Will not save!"
)
exit(1)
return
elif args.what == "rcode":
if args.var == "null":
print("Old reduced_code:")
print(case.reduced_code)
case.reduced_code = None
ddb.update_case(case_id, case)
return
with open(args.var, "r") as f:
rcode = f.read()
old_code = case.code
case.code = rcode
if chkr.is_interesting(case):
case.code = old_code
case.reduced_code = rcode
ddb.update_case(case_id, case)
else:
logging.critical(
"The provided code is not interesting wrt to the case. Will not save!"
)
exit(1)
return
elif args.what == "bisection":
if args.var == "null":
print("Old bisection:", case.bisection)
case.bisection = None
ddb.update_case(case_id, case)
return
# Also acts as check that the given rev is ok
rev = repo.rev_to_commit(args.var)
# Just in case someone accidentally overrides things...
logging.info(f"Previous bisection for case {case_id}: {case.bisection}")
case.bisection = rev
ddb.update_case(case_id, case)
return