remove_close_messages.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2014 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Removes WidgetHostMsg_Close and alike from testcases. These messages are an
  5. annoyance for corpus distillation. They cause the browser to exit, so no
  6. further messages are processed. On the other hand, WidgetHostMsg_Close is useful
  7. for fuzzing - many found bugs are related to a renderer disappearing. So the
  8. fuzzer should be crafting random WidgetHostMsg_Close messages.
  9. """
  10. from __future__ import print_function
  11. import argparse
  12. import os
  13. import platform
  14. import shutil
  15. import subprocess
  16. import sys
  17. import tempfile
  18. def create_temp_file():
  19. temp_file = tempfile.NamedTemporaryFile(delete=False)
  20. temp_file.close()
  21. return temp_file.name
  22. def main():
  23. desc = 'Remove WidgetHostMsg_Close and alike from the testcases.'
  24. parser = argparse.ArgumentParser(description=desc)
  25. parser.add_argument(
  26. '--out-dir',
  27. dest='out_dir',
  28. default='out',
  29. help='ouput directory under src/ directory')
  30. parser.add_argument(
  31. '--build-type',
  32. dest='build_type',
  33. default='Release',
  34. help='Debug vs. Release build')
  35. parser.add_argument('testcase_dir', help='Directory containing testcases')
  36. parsed = parser.parse_args()
  37. message_util_binary = 'ipc_message_util'
  38. script_path = os.path.realpath(__file__)
  39. ipc_fuzzer_dir = os.path.join(os.path.dirname(script_path), os.pardir)
  40. src_dir = os.path.abspath(os.path.join(ipc_fuzzer_dir, os.pardir, os.pardir))
  41. out_dir = os.path.join(src_dir, parsed.out_dir)
  42. build_dir = os.path.join(out_dir, parsed.build_type)
  43. message_util_path = os.path.join(build_dir, message_util_binary)
  44. if not os.path.exists(message_util_path):
  45. print('ipc_message_util executable not found at ', message_util_path)
  46. return 1
  47. filter_command = [
  48. message_util_path,
  49. '--invert',
  50. '--regexp=WidgetHostMsg_Close|WidgetHostMsg_ClosePage_ACK',
  51. 'input',
  52. 'output',
  53. ]
  54. testcase_list = os.listdir(parsed.testcase_dir)
  55. testcase_count = len(testcase_list)
  56. index = 0
  57. for testcase in testcase_list:
  58. index += 1
  59. print('[%d/%d] Processing %s' % (index, testcase_count, testcase))
  60. testcase_path = os.path.join(parsed.testcase_dir, testcase)
  61. filtered_path = create_temp_file()
  62. filter_command[-2] = testcase_path
  63. filter_command[-1] = filtered_path
  64. subprocess.call(filter_command)
  65. shutil.move(filtered_path, testcase_path)
  66. return 0
  67. if __name__ == '__main__':
  68. sys.exit(main())