1#!/usr/bin/env python3 2 3import sys, os, subprocess, tempfile, shutil 4 5 6def 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 24srcdir = os.getenv ("srcdir", ".") 25EXEEXT = os.getenv ("EXEEXT", "") 26top_builddir = os.getenv ("top_builddir", ".") 27hb_shape_fuzzer = os.path.join (top_builddir, "hb-shape-fuzzer" + EXEEXT) 28 29if not os.path.exists (hb_shape_fuzzer): 30 if len (sys.argv) == 1 or not os.path.exists (sys.argv[1]): 31 sys.exit ("""Failed to find hb-shape-fuzzer binary automatically, 32please provide it as the first argument to the tool""") 33 34 hb_shape_fuzzer = sys.argv[1] 35 36print ('hb_shape_fuzzer:', hb_shape_fuzzer) 37fails = 0 38 39valgrind = None 40if os.getenv ('RUN_VALGRIND', ''): 41 valgrind = shutil.which ('valgrind') 42 if valgrind is None: 43 sys.exit ("""Valgrind requested but not found.""") 44 45parent_path = os.path.join (srcdir, "fonts") 46for file in os.listdir (parent_path): 47 path = os.path.join (parent_path, file) 48 49 if valgrind: 50 text, returncode = cmd ([valgrind, '--leak-check=full', '--error-exitcode=1', hb_shape_fuzzer, path]) 51 else: 52 text, returncode = cmd ([hb_shape_fuzzer, path]) 53 if 'error' in text: 54 returncode = 1 55 56 if (not valgrind or returncode) and text.strip (): 57 print (text) 58 59 if returncode != 0: 60 print ('failure on %s' % file) 61 fails = fails + 1 62 63 64if fails: 65 sys.exit ("%d shape fuzzer related tests failed." % fails) 66