forked from sosy-lab/sv-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.py
executable file
·610 lines (512 loc) · 28 KB
/
check.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
#!/usr/bin/env python3
import collections
import fnmatch
import glob
import hashlib
import logging
import os
import re
import sys
try:
import yaml
except ImportError:
yaml = None
LOG_FORMAT = "%(levelname)-7s %(message)s"
try:
import coloredlogs
coloredlogs.install(fmt=LOG_FORMAT, level='INFO')
except ImportError:
logging.basicConfig(format=LOG_FORMAT, level='INFO')
README_PATTERN = re.compile('^readme(\.(txt|md))?$', re.I)
LICENSE_PATTERN = re.compile('^license([-.].*)?(\.(txt|md))?$', re.I)
BENCHMARK_PATTERN = re.compile('^.*\.yml$')
EXPECTED_FILE_PATTERN = re.compile(
'^(.*\.(c|h|i|verdict|yml)|(readme|license([-.].*)?|.*\.error_trace)(\.(txt|md))?|Makefile)$',
re.I)
CONFIG_KEYS = set(["Architecture", "Description"])
PROPERTIES = set(["def-behavior", "no-overflow", "termination", "unreach-call", "valid-deref", "valid-free", "valid-memcleanup", "valid-memsafety", "valid-memtrack",
"coverage-error-call", "coverage-branches", "coverage-conditions", "coverage-statements"])
# multiple properties for eca-rers2018-files
for i in range(100):
PROPERTIES.add("unreach-call-%d" % i)
# Ignore ldv-multiproperty and regression
# as long as no yml-task definitions exist for the tasks in these directories
IGNORED_DIRECTORIES = set(["properties", "ldv-multiproperty", "regression"])
"""Directories which are completely ignored by this script"""
UNUSED_DIRECTORIES = set(["ldv-challenges", "ldv-multiproperty", "regression"])
"""Directories which expected to contain tasks that are not included in any category"""
EXPECTED_SUBDIRECTORIES = set(["model", "todo", "properties"])
"""Directories that can appear inside directories with tasks but contain other files"""
LINE_DIRECTIVE = re.compile('^#(line| [0-9]+) ')
PREPROCESSOR_DIRECTIVE = re.compile('^ *#(define|include)')
KNOWN_DIRECTORY_PROBLEMS = [
# TODO Please fix
("bitvector", "missing license"),
("ddv-machzwd", "missing license"),
("ldv-linux-4.2-rc1", "missing readme"),
("ldv-memsafety", "missing license"),
("ldv-memsafety", "missing readme"),
("ldv-races", "missing readme"),
("ldv-regression", "missing license"),
("ldv-validator-v0.8", "missing readme"),
("list-properties", "missing license"),
("locks", "missing license"),
("loops", "missing license"),
("memsafety-ext", "missing license"),
("product-lines", "missing license"),
("pthread", "missing license"),
("pthread-ext", "missing license"),
("seq-pthread", "missing license"),
("systemc", "missing license"),
("termination-memory-alloca", "BradleyMannaSipma-2005CAV-Fig1-alloca_unknown-termination.c has no known verdict"),
("termination-memory-alloca", "BradleyMannaSipma-2005CAV-Fig1-alloca_unknown-termination.c.i has no known verdict"),
("termination-memory-alloca", "BradleyMannaSipma-2005CAV-Fig1-modified-alloca_unknown-termination.c has no known verdict"),
("termination-memory-alloca", "BradleyMannaSipma-2005CAV-Fig1-modified-alloca_unknown-termination.c.i has no known verdict"),
("termination-memory-alloca", "BradleyMannaSipma-2005ICALP-Fig1-alloca_unknown-termination.c has no known verdict"),
("termination-memory-alloca", "BradleyMannaSipma-2005ICALP-Fig1-alloca_unknown-termination.c.i has no known verdict"),
("termination-memory-alloca", "HarrisLalNoriRajamani-2010SAS-Fig2-alloca_unknown-termination.c has no known verdict"),
("termination-memory-alloca", "HarrisLalNoriRajamani-2010SAS-Fig2-alloca_unknown-termination.c.i has no known verdict"),
("termination-memory-alloca", "LarrazOliverasRodriguez-CarbonellRubio-2013FMCAD-Fig1-alloca_unknown-termination.c has no known verdict"),
("termination-memory-alloca", "LarrazOliverasRodriguez-CarbonellRubio-2013FMCAD-Fig1-alloca_unknown-termination.c.i has no known verdict"),
("ldv-memsafety", "unexpected subdirectory memleaks-notpreprocessed"),
("ldv-multiproperty", "unexpected file ALL-multi.prp"), # special property file
("eca-rers2018", "unexpected file RERS_18_solutions_dot_petri.csv"),
("eca-rers2018", "unexpected file createYml.py"),
# historical
("ntdrivers", "missing license"),
("ntdrivers-simplified", "missing license"),
("ssh", "missing license"),
("ssh-simplified", "missing license"),
("ntdrivers", "missing readme"),
("ntdrivers-simplified", "missing readme"),
("ssh", "missing readme"),
("ssh-simplified", "missing readme"),
]
KNOWN_BENCHMARK_FILE_PROBLEMS = [
("forester-heap/dll-rb-cnstr_1-2.yml", "has expected undefined behavior but also a verdict for some other property"),
("forester-heap/sll-01-2.yml", "has expected undefined behavior but also a verdict for some other property"),
("forester-heap/sll-rb-cnstr_1-2.yml", "has expected undefined behavior but also a verdict for some other property"),
("heap-manipulation/tree-2.yml", "has expected undefined behavior but also a verdict for some other property"),
("list-ext-properties/list-ext.yml", "has expected undefined behavior but also a verdict for some other property"),
("list-ext-properties/list-ext_flag.yml", "has expected undefined behavior but also a verdict for some other property"),
("termination-crafted/NonTermination3-1.yml", "has expected undefined behavior but also a verdict for some other property"),
("termination-numeric/Binomial.yml", "has expected undefined behavior but also a verdict for some other property"),
("termination-numeric/TerminatorRec02.yml", "has expected undefined behavior but also a verdict for some other property"),
("ldv-multiproperty/linux-4.0-rc1---drivers--char--ipmi--ipmi_msghandler.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--hwmon--applesmc.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--hwmon--nct6775.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--media--rc--lirc_dev.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--media--usb--dvb-usb-v2--dvb-usb-mxl111sf.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--mmc--card--mmc_test.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--mtd--devices--docg3.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--net--ethernet--amd--amd8111e.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--net--ethernet--atheros--atl1e--atl1e.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--net--ethernet--dec--tulip--dmfe.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--net--ethernet--ethoc.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--net--ethernet--icplus--ipg.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--net--ethernet--intel--igbvf--igbvf.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--net--wireless--rtl818x--rtl8180--rtl818x_pci.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--scsi--BusLogic.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--usb--host--u132-hcd.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--vme--bridges--vme_ca91cx42.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---drivers--xen--xen-pciback--xen-pciback.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---fs--nfs--nfsv2.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---fs--squashfs--squashfs.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---net--rose--rose.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("ldv-multiproperty/linux-4.0-rc1---sound--drivers--vx--snd-vx-lib.ko_true-unreach-call.cil.c_false-unreach-call.cil.c", "has duplicate verdict for property unreach-call"),
("termination-memory-alloca/Avery-2006FLOPS-Tabel1_true-alloca.yml", "has unknown property alloca"),
("termination-memory-alloca/aviad_true-alloca.yml", "has unknown property alloca"),
]
KNOWN_SET_PROBLEMS = [
]
KNOWN_GLOBAL_PROBLEMS = [
]
class Checks(object):
"""Collections of checks that should be implemented in methods named "check*" in subclasses."""
def __init__(self, name, known_problems, quiet=False):
super(Checks, self).__init__()
self.name = name
self._known_problems = known_problems
self._quiet = quiet
self._errors = False
self._warnings = False
def run(self):
"""Run all checks of this instance and return success status."""
attrs = [getattr(self, a) for a in dir(self)]
tests = [a for a in attrs if callable(a) and a.__name__.startswith('check')]
for test in tests:
test()
if not self._quiet and not (self._errors or self._warnings) and self.name:
logging.info("%s: OK", self.name)
return not self._errors
def error(self, msg, *args):
"""Mark the current check as failed."""
msg = msg % args
error_id = (self.name, msg) if self.name else msg
if error_id in self._known_problems:
self._warnings = True
log = logging.warning
else:
self._errors = True
log = logging.error
if self.name:
log("%s: %s", self.name, msg)
else:
log("%s", msg)
class DirectoryChecks(Checks):
"""Checks for a directory in the repository, e.g. about what files are in it.
Also executes specific checks for each benchmark file in the directory."""
def __init__(self, path, all_patterns, *args, **kwargs):
super(DirectoryChecks, self).__init__(known_problems=KNOWN_DIRECTORY_PROBLEMS, *args, **kwargs)
self.path = path
self.content = os.listdir(path)
self.all_patterns = all_patterns
def run(self):
ok = super(DirectoryChecks, self).run()
for entry in self.content:
if BENCHMARK_PATTERN.match(entry):
dir_and_name = os.path.join(self.name, entry)
ok &= TaskDefinitionFileChecks(
path=os.path.join(self.path, entry),
name=dir_and_name,
contained_in_category=self.all_patterns.match(dir_and_name)
).run()
return ok
def check_has_benchmarks(self):
for entry in self.content:
if BENCHMARK_PATTERN.match(entry):
return
self.error("contains no benchmark files")
def check_has_subdirectories(self):
for entry in self.content:
if os.path.isdir(os.path.join(self.path, entry)) and not entry in EXPECTED_SUBDIRECTORIES:
self.error("unexpected subdirectory %s", entry)
def check_has_unexpected_file(self):
for entry in self.content:
if (not EXPECTED_FILE_PATTERN.match(entry) and
not os.path.isdir(os.path.join(self.path, entry))):
self.error("unexpected file %s", entry)
def check_has_readme(self):
for entry in self.content:
if README_PATTERN.match(entry):
return
self.error("missing readme")
def check_has_license(self):
for entry in self.content:
if LICENSE_PATTERN.match(entry):
return
self.error("missing license")
def check_has_Makefile(self):
if not 'Makefile' in self.content:
self.error("missing Makefile")
def check_files_contained_in_category(self):
if self.name in UNUSED_DIRECTORIES:
return
for entry in self.content:
if BENCHMARK_PATTERN.match(entry)\
and not self.all_patterns.match(os.path.join(self.name, entry)):
self.error("%s is not contained in any category", entry)
class FileChecks(Checks):
"""Checks about the content of a file."""
def __init__(self, path, *args, **kwargs):
super(FileChecks, self).__init__(known_problems=KNOWN_SET_PROBLEMS, quiet=True, *args, **kwargs)
with open(path, 'r') as f:
self.lines = f.readlines()
def check_file_has_no_windows_line_ending(self):
if any('\r' in line for line in self.lines):
self.error("Windows line endings")
class TaskDefinitionFileChecks(FileChecks):
"""Checks about the content of a single task definition .yml file."""
def __init__(self, path, contained_in_category, *args, **kwargs):
super(TaskDefinitionFileChecks, self).__init__(path, *args, **kwargs)
self.path = path
self.directory = os.path.dirname(path)
self.filename = os.path.basename(self.name)
self.contained_in_category = contained_in_category
if yaml:
with open(self.path) as f:
self.content = yaml.safe_load(f)
def check_format_version(self):
if not 'format_version' in self.content:
self.error("has no format_version")
elif not (self.content['format_version']
and type(self.content['format_version']) is str):
self.error("has invalid format version")
def check_input_files(self):
if not self.content:
return None
input_files = self._get_input_files()
properties_to_verdicts = self._get_properties()
for f in input_files:
f_path = os.path.join(self.directory, f)
if not os.path.exists(f_path):
self.error("references inaccessible file: " + f_path)
else:
InputFileChecks(
path=f_path,
name=f_path,
contained_in_category=self.contained_in_category,
properties_to_verdicts=properties_to_verdicts
).run()
def check_properties(self):
prop_to_verdict = self._get_properties()
PropertiesChecks(
properties=prop_to_verdict,
contained_in_category=self.contained_in_category,
name=self.name
).run()
def _get_input_files(self) -> list:
if 'input_files' not in self.content:
self.error("has no input file definition")
return []
input_files = self.content['input_files']
if not input_files:
self.error("has no input file definition")
return []
if type(input_files) is not list:
input_files = [input_files]
return input_files
def _get_properties(self) -> dict:
if not 'properties' in self.content\
or not self.content['properties']:
self.error("No properties")
# Return empty dict instead of None so that calling check stops gracefully
return dict()
props = []
for prop_def in self.content['properties']:
if type(prop_def) is not dict\
or 'property_file' not in prop_def:
self.error("invalid property definition: %s" % prop_def)
else:
props.append(prop_def)
prop_to_verdict = dict()
for prop_def in props:
# Strip directories and ".prp" suffix
prop = os.path.basename(prop_def['property_file'])
if prop.endswith('.prp'):
prop = prop[:-4]
if 'expected_verdict' in prop_def:
verdict = prop_def['expected_verdict']
else:
verdict = None
if 'subproperty' in prop_def:
prop = prop_def['subproperty']
prop_to_verdict[prop] = verdict
return prop_to_verdict
class PropertiesChecks(Checks):
"""Checks about the properties of task definitions."""
def __init__(self, properties, contained_in_category, *args, **kwargs):
super(PropertiesChecks, self).__init__(known_problems=KNOWN_BENCHMARK_FILE_PROBLEMS, quiet=True, *args, **kwargs)
self.prop_to_verdict = properties
self.prop_names = list(properties.keys())
def check_no_unknown_property(self):
[self.error("has unknown property " + p)
for p in self.prop_names if p not in PROPERTIES]
def check_no_duplicate_properties(self):
counts = collections.Counter(self.prop_to_verdict.keys())
[self.error("has duplicate property " + p)
for p, c in counts.items() if c > 1]
def check_no_multiple_memsafety_verdicts(self):
distinct_prop_count = self.prop_names.count("valid-deref")\
+ self.prop_names.count("valid-free")\
+ self.prop_names.count("valid-memtrack")\
+ self.prop_names.count("valid-memsafety")
if distinct_prop_count > 1:
self.error("has verdicts for multiple memsafety properties")
def check_no_contradicting_verdicts(self):
# Properties may also have no verdict (None), i.e., (not violates) != fulfills. Thus we need both methods
def violates(prop):
return prop in self.prop_to_verdict\
and self.prop_to_verdict[prop] is False
def fulfills(prop):
return prop in self.prop_to_verdict\
and self.prop_to_verdict[prop] is True
if violates("valid-deref")\
or violates("valid-free")\
or violates("no-overflow")\
or violates("def-behavior"):
if any(fulfills(p) for p in self.prop_to_verdict)\
or len([p for p in self.prop_to_verdict if violates(p)]) > 1:
self.error(
"has expected undefined behavior but also a verdict for some other property")
if violates("unreach-call") and fulfills("valid-memcleanup"):
# __VERIFIER_error() aborts the program, and if there is still any
# allocated memory this would violate memcleanup.
# We think this is probable (though not guaranteed), so we issue a warning.
self.error("has reachable error location but claims to have no memory leaks (this is not necessarily wrong but should be checked)")
def check_no_invalid_verdicts(self):
for prop, verdict in self.prop_to_verdict.items():
if prop.startswith("coverage-") and verdict is not None:
self.error("has verdict for property " + prop)
class InputFileChecks(FileChecks):
"""Checks about the contents of a single benchmark input file."""
def __init__(self, path, contained_in_category, properties_to_verdicts, *args, **kwargs):
super(InputFileChecks, self).__init__(path, *args, **kwargs)
self.path = path
self.filename = os.path.basename(self.path)
self.contained_in_category = contained_in_category
self.prop_to_verdict = properties_to_verdicts
def check_file_has_no_line_directive(self):
if any(LINE_DIRECTIVE.match(line) for line in self.lines):
self.error(
"line directives from preprocessor present, "
"please use 'cpp -P' for preprocessing")
def check_unreach_call_tasks_have_verifier_error(self):
if not "unreach-call" in self.prop_to_verdict:
return
if not self.contained_in_category:
# Some such files have calls to __VERIFIER_error inside #include
return
if not any("__VERIFIER_error()" in line for line in self.lines if not "extern" in line):
self.error("has property unreach-call, but does not call __VERIFIER_error")
def check_no_include_or_define(self):
if not self.contained_in_category:
return
if any(PREPROCESSOR_DIRECTIVE.match(line) for line in self.lines):
self.error("#define or #include statement present, please add preprocessed version")
class SetFileChecks(Checks):
"""Checks about the .set files that define categories."""
def __init__(self, path, *args, **kwargs):
super(SetFileChecks, self).__init__(known_problems=KNOWN_SET_PROBLEMS, *args, **kwargs)
self.path = path
self.base_path = os.path.dirname(path)
self.category = os.path.basename(path)
if self.category.endswith(".set"):
self.category = self.category[:-4]
self.patterns = list(read_set_file(path))
self.matched_files = [file for pattern in self.patterns
for file in glob.iglob(os.path.join(self.base_path, pattern))]
self.cfg_file = os.path.join(self.base_path, self.category + ".cfg")
def check_all_patterns_match_files(self):
for pattern in self.patterns:
first_match = next(glob.iglob(os.path.join(self.base_path, pattern)), None)
if not first_match:
self.error("Pattern <%s> does not match anything.", pattern)
def check_patterns_match_only_expected_files(self):
unexpected_files = [
file for file in self.matched_files if not BENCHMARK_PATTERN.match(os.path.basename(file))]
if unexpected_files:
self.error("includes files %s that do have unexpected file names", unexpected_files)
def check_declared_architecture_of_benchmarks(self):
cfg = self._load_config()
expected_arch = int(cfg["Architecture"].split(" ")[0]) if cfg else None
directories = set(os.path.dirname(file) for file in self.matched_files)
for directory in directories:
makefile_path = os.path.join(directory, "Makefile")
if os.path.exists(makefile_path):
with open(makefile_path) as makefile:
archs = [line for line in makefile if "CC.Arch" in line]
if len(archs) > 1:
self.error("multiple architecture declarations in %s", makefile_path)
arch = int(next(iter(archs), "32").split(" ")[-1])
if expected_arch and arch != expected_arch:
self.error(
"%d bit category contains %d bit benchmarks in %s",
expected_arch,
arch,
os.path.basename(directory))
def check_has_config_file(self):
if not os.path.isfile(self.cfg_file):
self.error("missing configuration file")
def _load_config(self):
if not yaml:
return None
if not os.path.isfile(self.cfg_file):
return None
with open(self.cfg_file) as f:
return yaml.safe_load(f)
def check_config_file(self):
cfg = self._load_config()
if not cfg:
return
unknown_keys = set(cfg.keys()).difference(CONFIG_KEYS)
missing_keys = CONFIG_KEYS.difference(cfg.keys())
if unknown_keys:
self.error("unexpected config entries <%s>", ">, <".join(unknown_keys))
if missing_keys:
self.error("missing config entries <%s>", ">, <".join(missing_keys))
if not cfg.get("Description", "dummy"):
self.error("missing description")
if cfg.get("Architecture", "32 bit") not in ["32 bit", "64 bit"]:
self.error("invalid architecture <%s>", cfg.get("Architecture"))
def read_set_file(path):
with open(path) as f:
for line in f:
line = line.strip()
if line and not line[0] == '#':
yield line
class GlobalChecks(Checks):
def __init__(self, all_matched_files, base_path, *args, **kwargs):
super(GlobalChecks, self).__init__(
name=None, known_problems=KNOWN_GLOBAL_PROBLEMS, *args, **kwargs)
self.all_matched_files = all_matched_files
self.base_path = base_path
def check_no_duplicate_filenames(self):
all_filenames = collections.defaultdict(list)
for file in self.all_matched_files:
all_filenames[os.path.basename(file).lower()].append(file)
for filename, files in all_filenames.items():
if len(files) > 1:
self.error(
"files %s have names that are easy to confuse",
", ".join(os.path.relpath(file, self.base_path) for file in files))
def check_no_duplicate_files(self):
sizes = collections.defaultdict(list)
for filename in self.all_matched_files:
sizes[os.path.getsize(filename)].append(filename)
for file_candidates in sizes.values():
if len(file_candidates) > 1:
hashes = collections.defaultdict(list)
for filename in file_candidates:
hashes[hash_file(filename)].append(filename)
for files in hashes.values():
if len(files) > 1:
self.error(
"files %s have the same content",
", ".join(os.path.relpath(file, self.base_path) for file in sorted(files)))
def hash_file(filename, hash_alg=hashlib.sha1, block_size_factor=100000):
with open(filename, 'rb') as f:
hasher = hash_alg()
block_size = hasher.block_size * block_size_factor
while True:
block = f.read(block_size)
if not block:
break
hasher.update(block)
return hasher.hexdigest()
def _check_known_errors_consistent(main_dir):
for i, _ in KNOWN_SET_PROBLEMS + KNOWN_DIRECTORY_PROBLEMS + KNOWN_BENCHMARK_FILE_PROBLEMS:
path = os.path.join(main_dir, i)
assert os.path.exists(path), "Whitelisted file doesn't exist: %s" % path
def main():
if not yaml:
logging.warning("Missing python-yaml, not all checks can be executed")
main_directory = os.path.relpath(os.path.dirname(__file__))
_check_known_errors_consistent(main_directory)
entries = sorted(os.listdir(main_directory))
all_patterns_re = (
fnmatch.translate(pattern)
for entry in entries if entry.endswith(".set")
for pattern in read_set_file(os.path.join(main_directory, entry)))
all_patterns = re.compile("^(" + "|".join(all_patterns_re) + ")$")
all_matched_files = set()
ok = True
for entry in entries:
path = os.path.join(main_directory, entry)
if not (entry[0] == '.' or entry == "bin" or entry.endswith("-todo")):
if os.path.isdir(path) and not entry in IGNORED_DIRECTORIES:
ok &= DirectoryChecks(path, all_patterns, entry).run()
elif entry.endswith(".set"):
check = SetFileChecks(path, entry)
all_matched_files.update(check.matched_files)
ok &= check.run()
else:
logging.debug("%s: skipped", entry)
else:
logging.debug("%s: skipped", entry)
ok &= GlobalChecks(all_matched_files, main_directory).run()
if not ok:
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()