FuzzerDefs.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //===- FuzzerDefs.h - Internal header for the Fuzzer ------------*- C++ -* ===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Basic definitions.
  9. //===----------------------------------------------------------------------===//
  10. #ifndef LLVM_FUZZER_DEFS_H
  11. #define LLVM_FUZZER_DEFS_H
  12. #include <cassert>
  13. #include <cstddef>
  14. #include <cstdint>
  15. #include <cstring>
  16. #include <memory>
  17. #include <set>
  18. #include <string>
  19. #include <vector>
  20. // Platform detection.
  21. #ifdef __linux__
  22. #define LIBFUZZER_APPLE 0
  23. #define LIBFUZZER_FUCHSIA 0
  24. #define LIBFUZZER_LINUX 1
  25. #define LIBFUZZER_NETBSD 0
  26. #define LIBFUZZER_FREEBSD 0
  27. #define LIBFUZZER_OPENBSD 0
  28. #define LIBFUZZER_WINDOWS 0
  29. #elif __APPLE__
  30. #define LIBFUZZER_APPLE 1
  31. #define LIBFUZZER_FUCHSIA 0
  32. #define LIBFUZZER_LINUX 0
  33. #define LIBFUZZER_NETBSD 0
  34. #define LIBFUZZER_FREEBSD 0
  35. #define LIBFUZZER_OPENBSD 0
  36. #define LIBFUZZER_WINDOWS 0
  37. #elif __NetBSD__
  38. #define LIBFUZZER_APPLE 0
  39. #define LIBFUZZER_FUCHSIA 0
  40. #define LIBFUZZER_LINUX 0
  41. #define LIBFUZZER_NETBSD 1
  42. #define LIBFUZZER_FREEBSD 0
  43. #define LIBFUZZER_OPENBSD 0
  44. #define LIBFUZZER_WINDOWS 0
  45. #elif __FreeBSD__
  46. #define LIBFUZZER_APPLE 0
  47. #define LIBFUZZER_FUCHSIA 0
  48. #define LIBFUZZER_LINUX 0
  49. #define LIBFUZZER_NETBSD 0
  50. #define LIBFUZZER_FREEBSD 1
  51. #define LIBFUZZER_OPENBSD 0
  52. #define LIBFUZZER_WINDOWS 0
  53. #elif __OpenBSD__
  54. #define LIBFUZZER_APPLE 0
  55. #define LIBFUZZER_FUCHSIA 0
  56. #define LIBFUZZER_LINUX 0
  57. #define LIBFUZZER_NETBSD 0
  58. #define LIBFUZZER_FREEBSD 0
  59. #define LIBFUZZER_OPENBSD 1
  60. #define LIBFUZZER_WINDOWS 0
  61. #elif _WIN32
  62. #define LIBFUZZER_APPLE 0
  63. #define LIBFUZZER_FUCHSIA 0
  64. #define LIBFUZZER_LINUX 0
  65. #define LIBFUZZER_NETBSD 0
  66. #define LIBFUZZER_FREEBSD 0
  67. #define LIBFUZZER_OPENBSD 0
  68. #define LIBFUZZER_WINDOWS 1
  69. #elif __Fuchsia__
  70. #define LIBFUZZER_APPLE 0
  71. #define LIBFUZZER_FUCHSIA 1
  72. #define LIBFUZZER_LINUX 0
  73. #define LIBFUZZER_NETBSD 0
  74. #define LIBFUZZER_FREEBSD 0
  75. #define LIBFUZZER_OPENBSD 0
  76. #define LIBFUZZER_WINDOWS 0
  77. #else
  78. #error "Support for your platform has not been implemented"
  79. #endif
  80. #if defined(_MSC_VER) && !defined(__clang__)
  81. // MSVC compiler is being used.
  82. #define LIBFUZZER_MSVC 1
  83. #else
  84. #define LIBFUZZER_MSVC 0
  85. #endif
  86. #ifndef __has_attribute
  87. # define __has_attribute(x) 0
  88. #endif
  89. #define LIBFUZZER_POSIX \
  90. (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD || \
  91. LIBFUZZER_FREEBSD || LIBFUZZER_OPENBSD)
  92. #ifdef __x86_64
  93. # if __has_attribute(target)
  94. # define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
  95. # else
  96. # define ATTRIBUTE_TARGET_POPCNT
  97. # endif
  98. #else
  99. # define ATTRIBUTE_TARGET_POPCNT
  100. #endif
  101. #ifdef __clang__ // avoid gcc warning.
  102. # if __has_attribute(no_sanitize)
  103. # define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
  104. # else
  105. # define ATTRIBUTE_NO_SANITIZE_MEMORY
  106. # endif
  107. # define ALWAYS_INLINE __attribute__((always_inline))
  108. #else
  109. # define ATTRIBUTE_NO_SANITIZE_MEMORY
  110. # define ALWAYS_INLINE
  111. #endif // __clang__
  112. #if LIBFUZZER_WINDOWS
  113. #define ATTRIBUTE_NO_SANITIZE_ADDRESS
  114. #else
  115. #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
  116. #endif
  117. #if LIBFUZZER_WINDOWS
  118. #define ATTRIBUTE_ALIGNED(X) __declspec(align(X))
  119. #define ATTRIBUTE_INTERFACE __declspec(dllexport)
  120. // This is used for __sancov_lowest_stack which is needed for
  121. // -fsanitize-coverage=stack-depth. That feature is not yet available on
  122. // Windows, so make the symbol static to avoid linking errors.
  123. #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC static
  124. #define ATTRIBUTE_NOINLINE __declspec(noinline)
  125. #else
  126. #define ATTRIBUTE_ALIGNED(X) __attribute__((aligned(X)))
  127. #define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
  128. #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC \
  129. ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) thread_local
  130. #define ATTRIBUTE_NOINLINE __attribute__((noinline))
  131. #endif
  132. #if defined(__has_feature)
  133. # if __has_feature(address_sanitizer)
  134. # define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
  135. # elif __has_feature(memory_sanitizer)
  136. # define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
  137. # else
  138. # define ATTRIBUTE_NO_SANITIZE_ALL
  139. # endif
  140. #else
  141. # define ATTRIBUTE_NO_SANITIZE_ALL
  142. #endif
  143. namespace fuzzer {
  144. template <class T> T Min(T a, T b) { return a < b ? a : b; }
  145. template <class T> T Max(T a, T b) { return a > b ? a : b; }
  146. class Random;
  147. class Dictionary;
  148. class DictionaryEntry;
  149. class MutationDispatcher;
  150. struct FuzzingOptions;
  151. class InputCorpus;
  152. struct InputInfo;
  153. struct ExternalFunctions;
  154. // Global interface to functions that may or may not be available.
  155. extern ExternalFunctions *EF;
  156. // We are using a custom allocator to give a different symbol name to STL
  157. // containers in order to avoid ODR violations.
  158. template<typename T>
  159. class fuzzer_allocator: public std::allocator<T> {
  160. public:
  161. fuzzer_allocator() = default;
  162. template<class U>
  163. fuzzer_allocator(const fuzzer_allocator<U>&) {}
  164. template<class Other>
  165. struct rebind { typedef fuzzer_allocator<Other> other; };
  166. };
  167. template<typename T>
  168. using Vector = std::vector<T, fuzzer_allocator<T>>;
  169. template<typename T>
  170. using Set = std::set<T, std::less<T>, fuzzer_allocator<T>>;
  171. typedef Vector<uint8_t> Unit;
  172. typedef Vector<Unit> UnitVector;
  173. typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
  174. int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
  175. uint8_t *ExtraCountersBegin();
  176. uint8_t *ExtraCountersEnd();
  177. void ClearExtraCounters();
  178. extern bool RunningUserCallback;
  179. } // namespace fuzzer
  180. #endif // LLVM_FUZZER_DEFS_H