run-shape-fuzzer-tests.py 1.8 KB

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