1 #!/usr/bin/env python3
2
3 import sys, os, subprocess, tempfile, shutil
4
5
6 def cmd (command):
7 # https://stackoverflow.com/a/4408409 as we might have huge output sometimes
8 with tempfile.TemporaryFile () as tempf:
9 p = subprocess.Popen (command, stderr=tempf)
10
11 try:
12 p.wait ()
13 tempf.seek (0)
14 text = tempf.read ()
15
16 #TODO: Detect debug mode with a better way
17 is_debug_mode = b"SANITIZE" in text
18
19 return ("" if is_debug_mode else text.decode ("utf-8").strip ()), p.returncode
20 except subprocess.TimeoutExpired:
21 return 'error: timeout, ' + ' '.join (command), 1
22
23
24 srcdir = os.getenv ("srcdir", ".")
25 EXEEXT = os.getenv ("EXEEXT", "")
26 top_builddir = os.getenv ("top_builddir", ".")
27 hb_repacker_fuzzer = os.path.join (top_builddir, "hb-repacker-fuzzer" + EXEEXT)
28
29 if not os.path.exists (hb_repacker_fuzzer):
30 if len (sys.argv) < 2 or not os.path.exists (sys.argv[1]):
31 sys.exit ("""Failed to find hb-repacker-fuzzer binary automatically,
32 please provide it as the first argument to the tool""")
33
34 hb_repacker_fuzzer = sys.argv[1]
35
36 print ('hb_repacker_fuzzer:', hb_repacker_fuzzer)
37 fails = 0
38
39 valgrind = None
40 if os.getenv ('RUN_VALGRIND', ''):
41 valgrind = shutil.which ('valgrind')
42 if valgrind is None:
43 sys.exit ("""Valgrind requested but not found.""")
44
45 def run_dir (parent_path):
46 global fails
47 for file in os.listdir (parent_path):
48 path = os.path.join(parent_path, file)
49 print ("running repacker fuzzer against %s" % path)
50 if valgrind:
51 text, returncode = cmd ([valgrind, '--leak-check=full', '--error-exitcode=1', hb_repacker_fuzzer, path])
52 else:
53 text, returncode = cmd ([hb_repacker_fuzzer, path])
54 if 'error' in text:
55 returncode = 1
56
57 if (not valgrind or returncode) and text.strip ():
58 print (text)
59
60 if returncode != 0:
61 print ("failed for %s" % path)
62 fails = fails + 1
63
64
65 run_dir (os.path.join (srcdir, "graphs"))
66
67 if fails:
68 sys.exit ("%d repacker fuzzer related tests failed." % fails)