fuzzing_include_am.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python
  2. # Future imports for Python 2.7, mandatory in 3.0
  3. from __future__ import division
  4. from __future__ import print_function
  5. from __future__ import unicode_literals
  6. FUZZERS = """
  7. address
  8. addressPTR
  9. consensus
  10. descriptor
  11. diff
  12. diff-apply
  13. extrainfo
  14. hsdescv3
  15. hsdescv3-inner
  16. hsdescv3-middle
  17. http
  18. http-connect
  19. microdesc
  20. socks
  21. strops
  22. vrs
  23. """
  24. PREAMBLE = r"""
  25. FUZZING_CPPFLAGS = \
  26. $(src_test_AM_CPPFLAGS) $(TEST_CPPFLAGS)
  27. FUZZING_CFLAGS = \
  28. $(AM_CFLAGS) $(TEST_CFLAGS)
  29. FUZZING_LDFLAG = \
  30. @TOR_LDFLAGS_zlib@ $(TOR_LDFLAGS_CRYPTLIB) @TOR_LDFLAGS_libevent@
  31. FUZZING_LIBS = \
  32. src/test/libtor-testing.a \
  33. @TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ \
  34. @TOR_LIBEVENT_LIBS@ $(TOR_LIBS_CRYPTLIB) \
  35. @TOR_LIB_WS32@ @TOR_LIB_IPHLPAPI@ @TOR_LIB_SHLWAPI@ @TOR_LIB_GDI@ @TOR_LIB_USERENV@ @CURVE25519_LIBS@ \
  36. @TOR_SYSTEMD_LIBS@ \
  37. @TOR_LZMA_LIBS@ \
  38. @TOR_ZSTD_LIBS@ \
  39. @TOR_TRACE_LIBS@
  40. oss-fuzz-prereqs: \
  41. src/test/libtor-testing.a
  42. noinst_HEADERS += \
  43. src/test/fuzz/fuzzing.h
  44. LIBFUZZER_CPPFLAGS = $(FUZZING_CPPFLAGS) -DLLVM_FUZZ
  45. LIBFUZZER_CFLAGS = $(FUZZING_CFLAGS)
  46. LIBFUZZER_LDFLAG = $(FUZZING_LDFLAG) -fsanitize=fuzzer
  47. LIBFUZZER_LIBS = $(FUZZING_LIBS) -lstdc++
  48. LIBOSS_FUZZ_CPPFLAGS = $(FUZZING_CPPFLAGS) -DLLVM_FUZZ
  49. LIBOSS_FUZZ_CFLAGS = $(FUZZING_CFLAGS)
  50. """
  51. POSTAMBLE = r"""
  52. noinst_PROGRAMS += $(FUZZERS) $(LIBFUZZER_FUZZERS)
  53. noinst_LIBRARIES += $(OSS_FUZZ_FUZZERS)
  54. oss-fuzz-fuzzers: oss-fuzz-prereqs $(OSS_FUZZ_FUZZERS)
  55. fuzzers: $(FUZZERS) $(LIBFUZZER_FUZZERS)
  56. test-fuzz-corpora: $(FUZZERS)
  57. $(top_srcdir)/src/test/fuzz_static_testcases.sh
  58. """
  59. ########### No user serviceable parts will follow.
  60. PREAMBLE = PREAMBLE.strip()
  61. POSTAMBLE = POSTAMBLE.strip() # If I use it, it's a word!
  62. FUZZERS = FUZZERS.split()
  63. FUZZERS.sort()
  64. WARNING = """
  65. # This file was generated by fuzzing_include_am.py; do not hand-edit unless
  66. # you enjoy having your changes erased.
  67. """.strip()
  68. print(WARNING)
  69. print(PREAMBLE)
  70. print("\n# ===== AFL fuzzers")
  71. def get_id_name(s):
  72. return s.replace("-", "_")
  73. for fuzzer in FUZZERS:
  74. idname = get_id_name(fuzzer)
  75. print("""\
  76. if UNITTESTS_ENABLED
  77. src_test_fuzz_fuzz_{name}_SOURCES = \\
  78. src/test/fuzz/fuzzing_common.c \\
  79. src/test/fuzz/fuzz_{name}.c
  80. src_test_fuzz_fuzz_{name}_CPPFLAGS = $(FUZZING_CPPFLAGS)
  81. src_test_fuzz_fuzz_{name}_CFLAGS = $(FUZZING_CFLAGS)
  82. src_test_fuzz_fuzz_{name}_LDFLAGS = $(FUZZING_LDFLAG)
  83. src_test_fuzz_fuzz_{name}_LDADD = $(FUZZING_LIBS)
  84. endif
  85. """.format(name=idname))
  86. print("if UNITTESTS_ENABLED")
  87. print("FUZZERS = \\")
  88. print(" \\\n".join("\tsrc/test/fuzz/fuzz-{name}".format(name=fuzzer)
  89. for fuzzer in FUZZERS))
  90. print("endif")
  91. print("\n# ===== libfuzzer")
  92. print("\nif LIBFUZZER_ENABLED")
  93. for fuzzer in FUZZERS:
  94. idname = get_id_name(fuzzer)
  95. print("""\
  96. if UNITTESTS_ENABLED
  97. src_test_fuzz_lf_fuzz_{name}_SOURCES = \\
  98. $(src_test_fuzz_fuzz_{name}_SOURCES)
  99. src_test_fuzz_lf_fuzz_{name}_CPPFLAGS = $(LIBFUZZER_CPPFLAGS)
  100. src_test_fuzz_lf_fuzz_{name}_CFLAGS = $(LIBFUZZER_CFLAGS)
  101. src_test_fuzz_lf_fuzz_{name}_LDFLAGS = $(LIBFUZZER_LDFLAG)
  102. src_test_fuzz_lf_fuzz_{name}_LDADD = $(LIBFUZZER_LIBS)
  103. endif
  104. """.format(name=idname))
  105. print("LIBFUZZER_FUZZERS = \\")
  106. print(" \\\n".join("\tsrc/test/fuzz/lf-fuzz-{name}".format(name=fuzzer)
  107. for fuzzer in FUZZERS))
  108. print("""
  109. else
  110. LIBFUZZER_FUZZERS =
  111. endif""")
  112. print("\n# ===== oss-fuzz\n")
  113. print("if OSS_FUZZ_ENABLED")
  114. for fuzzer in FUZZERS:
  115. idname = get_id_name(fuzzer)
  116. print("""\
  117. if UNITTESTS_ENABLED
  118. src_test_fuzz_liboss_fuzz_{name}_a_SOURCES = \\
  119. $(src_test_fuzz_fuzz_{name}_SOURCES)
  120. src_test_fuzz_liboss_fuzz_{name}_a_CPPFLAGS = $(LIBOSS_FUZZ_CPPFLAGS)
  121. src_test_fuzz_liboss_fuzz_{name}_a_CFLAGS = $(LIBOSS_FUZZ_CFLAGS)
  122. endif
  123. """.format(name=idname))
  124. print("OSS_FUZZ_FUZZERS = \\")
  125. print(" \\\n".join("\tsrc/test/fuzz/liboss-fuzz-{name}.a".format(name=fuzzer)
  126. for fuzzer in FUZZERS))
  127. print("""
  128. else
  129. OSS_FUZZ_FUZZERS =
  130. endif""")
  131. print("")
  132. print(POSTAMBLE)