blob: da7d1e570b45fef48e1c6f08b1e1c2619306738b [file] [log] [blame]
Ebrahim Byagowi8d199072020-02-19 14:56:55 +03301#!/usr/bin/env python3
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +04302
Ebrahim Byagowi76169522020-03-19 11:43:37 +03303import sys, os, subprocess, tempfile, shutil
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -07004
5
Ebrahim Byagowi76169522020-03-19 11:43:37 +03306def cmd (command):
7 # https://stackoverflow.com/a/4408409 as we might have huge output sometimes
8 with tempfile.TemporaryFile () as tempf:
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -07009 p = subprocess.Popen (command, stderr=tempf)
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -070010
11 try:
Khaled Hosny84dd65a2020-10-13 20:21:28 +020012 p.wait ()
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -070013 tempf.seek (0)
Ebrahim Byagowi0665dce2019-09-21 17:22:02 +043014 text = tempf.read ()
15
16 #TODO: Detect debug mode with a better way
17 is_debug_mode = b"SANITIZE" in text
18
Ebrahim Byagowi76169522020-03-19 11:43:37 +033019 return ("" if is_debug_mode else text.decode ("utf-8").strip ()), p.returncode
20 except subprocess.TimeoutExpired:
21 return 'error: timeout, ' + ' '.join (command), 1
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -070022
Garret Rieger474afaa2018-03-14 19:15:33 -070023
Ebrahim Byagowia07672d2020-07-04 14:12:55 +043024srcdir = os.getenv ("srcdir", ".")
25EXEEXT = os.getenv ("EXEEXT", "")
26top_builddir = os.getenv ("top_builddir", ".")
Garret Rieger584693e2018-03-15 18:27:01 -070027hb_subset_fuzzer = os.path.join (top_builddir, "hb-subset-fuzzer" + EXEEXT)
Garret Rieger474afaa2018-03-14 19:15:33 -070028
29if not os.path.exists (hb_subset_fuzzer):
Garret Riegerc02b40e2018-04-17 08:21:22 -060030 if len (sys.argv) < 2 or not os.path.exists (sys.argv[1]):
Ebrahim Byagowi7554f612020-05-28 22:51:29 +043031 sys.exit ("""Failed to find hb-subset-fuzzer binary automatically,
Garret Rieger474afaa2018-03-14 19:15:33 -070032please provide it as the first argument to the tool""")
Garret Rieger474afaa2018-03-14 19:15:33 -070033
34 hb_subset_fuzzer = sys.argv[1]
35
36print ('hb_subset_fuzzer:', hb_subset_fuzzer)
37fails = 0
38
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -070039valgrind = None
Ebrahim Byagowia07672d2020-07-04 14:12:55 +043040if os.getenv ('RUN_VALGRIND', ''):
Ebrahim Byagowib5526a02020-03-18 23:58:20 +033041 valgrind = shutil.which ('valgrind')
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -070042 if valgrind is None:
Ebrahim Byagowi7554f612020-05-28 22:51:29 +043043 sys.exit ("""Valgrind requested but not found.""")
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -070044
Behdad Esfahbodc0c190c2018-10-16 16:39:29 -070045def run_dir (parent_path):
46 global fails
Behdad Esfahbodc0c190c2018-10-16 16:39:29 -070047 for file in os.listdir (parent_path):
48 path = os.path.join(parent_path, file)
Ebrahim Byagowib65bad12019-07-11 14:31:55 +043049 # TODO: Run on all the fonts not just subset related ones
50 if "subset" not in path: continue
Garret Riegerc02b40e2018-04-17 08:21:22 -060051
Behdad Esfahbodc0c190c2018-10-16 16:39:29 -070052 print ("running subset fuzzer against %s" % path)
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -070053 if valgrind:
Ebrahim Byagowie4f99692020-07-08 15:10:25 +043054 text, returncode = cmd ([valgrind, '--leak-check=full', '--error-exitcode=1', hb_subset_fuzzer, path])
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -070055 else:
56 text, returncode = cmd ([hb_subset_fuzzer, path])
57 if 'error' in text:
58 returncode = 1
Garret Rieger474afaa2018-03-14 19:15:33 -070059
Ebrahim Byagowib65bad12019-07-11 14:31:55 +043060 if (not valgrind or returncode) and text.strip ():
Ebrahim Byagowi6d6edc82019-04-28 11:54:07 -070061 print (text)
62
63 if returncode != 0:
Behdad Esfahbodc0c190c2018-10-16 16:39:29 -070064 print ("failed for %s" % path)
65 fails = fails + 1
Garret Rieger474afaa2018-03-14 19:15:33 -070066
Behdad Esfahbodc0c190c2018-10-16 16:39:29 -070067
68run_dir (os.path.join (srcdir, "..", "subset", "data", "fonts"))
Ebrahim Byagowib65bad12019-07-11 14:31:55 +043069run_dir (os.path.join (srcdir, "fonts"))
Garret Riegerc02b40e2018-04-17 08:21:22 -060070
Garret Rieger474afaa2018-03-14 19:15:33 -070071if fails:
Ebrahim Byagowi7554f612020-05-28 22:51:29 +043072 sys.exit ("%d subset fuzzer related tests failed." % fails)