fuzzing_include_am.py 3.7 KB

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