fuzzing_include_am.py 3.7 KB

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