run-repacker-fuzzer-tests.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_repacker_fuzzer = os.path.join (top_builddir, "hb-repacker-fuzzer" + EXEEXT)
  20. if not os.path.exists (hb_repacker_fuzzer):
  21. if len (sys.argv) < 2 or not os.path.exists (sys.argv[1]):
  22. sys.exit ("""Failed to find hb-repacker-fuzzer binary automatically,
  23. please provide it as the first argument to the tool""")
  24. hb_repacker_fuzzer = sys.argv[1]
  25. print ('hb_repacker_fuzzer:', hb_repacker_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. def run_dir (parent_path):
  33. global fails
  34. for file in os.listdir (parent_path):
  35. path = os.path.join(parent_path, file)
  36. print ("running repacker fuzzer against %s" % path)
  37. if valgrind:
  38. text, returncode = cmd ([valgrind, '--leak-check=full', '--error-exitcode=1', hb_repacker_fuzzer, path])
  39. else:
  40. text, returncode = cmd ([hb_repacker_fuzzer, path])
  41. if 'error' in text:
  42. returncode = 1
  43. if (not valgrind or returncode) and text.strip ():
  44. print (text)
  45. if returncode != 0:
  46. print ("failed for %s" % path)
  47. fails = fails + 1
  48. run_dir (os.path.join (srcdir, "graphs"))
  49. if fails:
  50. sys.exit ("%d repacker fuzzer related tests failed." % fails)