2020-02-19 11:26:55 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-03-29 08:18:47 +00:00
|
|
|
|
2020-03-19 08:13:37 +00:00
|
|
|
import sys, os, subprocess, tempfile, shutil
|
2019-04-28 18:54:07 +00:00
|
|
|
|
|
|
|
|
2020-03-19 08:13:37 +00:00
|
|
|
def cmd (command):
|
|
|
|
# https://stackoverflow.com/a/4408409 as we might have huge output sometimes
|
|
|
|
with tempfile.TemporaryFile () as tempf:
|
2019-04-28 18:54:07 +00:00
|
|
|
p = subprocess.Popen (command, stderr=tempf)
|
|
|
|
|
|
|
|
try:
|
2020-03-19 08:13:37 +00:00
|
|
|
p.wait (timeout=int (os.environ.get ("HB_TEST_SUBSET_FUZZER_TIMEOUT", "12")))
|
2019-04-28 18:54:07 +00:00
|
|
|
tempf.seek (0)
|
2019-09-21 12:52:02 +00:00
|
|
|
text = tempf.read ()
|
|
|
|
|
|
|
|
#TODO: Detect debug mode with a better way
|
|
|
|
is_debug_mode = b"SANITIZE" in text
|
|
|
|
|
2020-03-19 08:13:37 +00:00
|
|
|
return ("" if is_debug_mode else text.decode ("utf-8").strip ()), p.returncode
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
return 'error: timeout, ' + ' '.join (command), 1
|
2019-04-28 18:54:07 +00:00
|
|
|
|
2018-03-15 02:15:33 +00:00
|
|
|
|
|
|
|
srcdir = os.environ.get ("srcdir", ".")
|
|
|
|
EXEEXT = os.environ.get ("EXEEXT", "")
|
|
|
|
top_builddir = os.environ.get ("top_builddir", ".")
|
2018-03-16 01:27:01 +00:00
|
|
|
hb_subset_fuzzer = os.path.join (top_builddir, "hb-subset-fuzzer" + EXEEXT)
|
2018-03-15 02:15:33 +00:00
|
|
|
|
|
|
|
if not os.path.exists (hb_subset_fuzzer):
|
2018-04-17 14:21:22 +00:00
|
|
|
if len (sys.argv) < 2 or not os.path.exists (sys.argv[1]):
|
2020-05-28 18:21:29 +00:00
|
|
|
sys.exit ("""Failed to find hb-subset-fuzzer binary automatically,
|
2018-03-15 02:15:33 +00:00
|
|
|
please provide it as the first argument to the tool""")
|
|
|
|
|
|
|
|
hb_subset_fuzzer = sys.argv[1]
|
|
|
|
|
|
|
|
print ('hb_subset_fuzzer:', hb_subset_fuzzer)
|
|
|
|
fails = 0
|
|
|
|
|
2019-04-28 18:54:07 +00:00
|
|
|
libtool = os.environ.get('LIBTOOL')
|
|
|
|
valgrind = None
|
|
|
|
if os.environ.get('RUN_VALGRIND', ''):
|
2020-03-18 20:28:20 +00:00
|
|
|
valgrind = shutil.which ('valgrind')
|
2019-04-28 18:54:07 +00:00
|
|
|
if valgrind is None:
|
2020-05-28 18:21:29 +00:00
|
|
|
sys.exit ("""Valgrind requested but not found.""")
|
2019-04-28 18:54:07 +00:00
|
|
|
if libtool is None:
|
|
|
|
print ("""Valgrind support is currently autotools only and needs libtool but not found.""")
|
|
|
|
|
|
|
|
|
2018-10-16 23:39:29 +00:00
|
|
|
def run_dir (parent_path):
|
|
|
|
global fails
|
|
|
|
for file in os.listdir (parent_path):
|
|
|
|
path = os.path.join(parent_path, file)
|
2019-07-11 10:01:55 +00:00
|
|
|
# TODO: Run on all the fonts not just subset related ones
|
|
|
|
if "subset" not in path: continue
|
2018-10-16 23:39:29 +00:00
|
|
|
|
|
|
|
print ("running subset fuzzer against %s" % path)
|
2019-04-28 18:54:07 +00:00
|
|
|
if valgrind:
|
|
|
|
text, returncode = cmd (libtool.split(' ') + ['--mode=execute', valgrind + ' --leak-check=full --show-leak-kinds=all --error-exitcode=1', '--', hb_subset_fuzzer, path])
|
|
|
|
else:
|
|
|
|
text, returncode = cmd ([hb_subset_fuzzer, path])
|
|
|
|
if 'error' in text:
|
|
|
|
returncode = 1
|
2018-10-16 23:39:29 +00:00
|
|
|
|
2019-07-11 10:01:55 +00:00
|
|
|
if (not valgrind or returncode) and text.strip ():
|
2019-04-28 18:54:07 +00:00
|
|
|
print (text)
|
2018-10-16 23:39:29 +00:00
|
|
|
|
2019-04-28 18:54:07 +00:00
|
|
|
if returncode != 0:
|
2018-10-16 23:39:29 +00:00
|
|
|
print ("failed for %s" % path)
|
|
|
|
fails = fails + 1
|
|
|
|
|
2019-04-28 18:54:07 +00:00
|
|
|
|
2018-10-16 23:39:29 +00:00
|
|
|
run_dir (os.path.join (srcdir, "..", "subset", "data", "fonts"))
|
2019-07-11 10:01:55 +00:00
|
|
|
run_dir (os.path.join (srcdir, "fonts"))
|
2018-04-17 14:21:22 +00:00
|
|
|
|
2018-03-15 02:15:33 +00:00
|
|
|
if fails:
|
2020-05-28 18:21:29 +00:00
|
|
|
sys.exit ("%d subset fuzzer related tests failed." % fails)
|