FuzzerPlatform.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===-- FuzzerPlatform.h --------------------------------------------------===//
  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. // Common platform macros.
  9. //===----------------------------------------------------------------------===//
  10. #ifndef LLVM_FUZZER_PLATFORM_H
  11. #define LLVM_FUZZER_PLATFORM_H
  12. // Platform detection.
  13. #ifdef __linux__
  14. #define LIBFUZZER_APPLE 0
  15. #define LIBFUZZER_FUCHSIA 0
  16. #define LIBFUZZER_LINUX 1
  17. #define LIBFUZZER_NETBSD 0
  18. #define LIBFUZZER_FREEBSD 0
  19. #define LIBFUZZER_WINDOWS 0
  20. #define LIBFUZZER_EMSCRIPTEN 0
  21. #elif __APPLE__
  22. #define LIBFUZZER_APPLE 1
  23. #define LIBFUZZER_FUCHSIA 0
  24. #define LIBFUZZER_LINUX 0
  25. #define LIBFUZZER_NETBSD 0
  26. #define LIBFUZZER_FREEBSD 0
  27. #define LIBFUZZER_WINDOWS 0
  28. #define LIBFUZZER_EMSCRIPTEN 0
  29. #elif __NetBSD__
  30. #define LIBFUZZER_APPLE 0
  31. #define LIBFUZZER_FUCHSIA 0
  32. #define LIBFUZZER_LINUX 0
  33. #define LIBFUZZER_NETBSD 1
  34. #define LIBFUZZER_FREEBSD 0
  35. #define LIBFUZZER_WINDOWS 0
  36. #define LIBFUZZER_EMSCRIPTEN 0
  37. #elif __FreeBSD__
  38. #define LIBFUZZER_APPLE 0
  39. #define LIBFUZZER_FUCHSIA 0
  40. #define LIBFUZZER_LINUX 0
  41. #define LIBFUZZER_NETBSD 0
  42. #define LIBFUZZER_FREEBSD 1
  43. #define LIBFUZZER_WINDOWS 0
  44. #define LIBFUZZER_EMSCRIPTEN 0
  45. #elif _WIN32
  46. #define LIBFUZZER_APPLE 0
  47. #define LIBFUZZER_FUCHSIA 0
  48. #define LIBFUZZER_LINUX 0
  49. #define LIBFUZZER_NETBSD 0
  50. #define LIBFUZZER_FREEBSD 0
  51. #define LIBFUZZER_WINDOWS 1
  52. #define LIBFUZZER_EMSCRIPTEN 0
  53. #elif __Fuchsia__
  54. #define LIBFUZZER_APPLE 0
  55. #define LIBFUZZER_FUCHSIA 1
  56. #define LIBFUZZER_LINUX 0
  57. #define LIBFUZZER_NETBSD 0
  58. #define LIBFUZZER_FREEBSD 0
  59. #define LIBFUZZER_WINDOWS 0
  60. #define LIBFUZZER_EMSCRIPTEN 0
  61. #elif __EMSCRIPTEN__
  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_WINDOWS 0
  68. #define LIBFUZZER_EMSCRIPTEN 1
  69. #else
  70. #error "Support for your platform has not been implemented"
  71. #endif
  72. #if defined(_MSC_VER) && !defined(__clang__)
  73. // MSVC compiler is being used.
  74. #define LIBFUZZER_MSVC 1
  75. #else
  76. #define LIBFUZZER_MSVC 0
  77. #endif
  78. #ifndef __has_attribute
  79. #define __has_attribute(x) 0
  80. #endif
  81. #define LIBFUZZER_POSIX \
  82. (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD || \
  83. LIBFUZZER_FREEBSD || LIBFUZZER_EMSCRIPTEN)
  84. #ifdef __x86_64
  85. #if __has_attribute(target)
  86. #define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
  87. #else
  88. #define ATTRIBUTE_TARGET_POPCNT
  89. #endif
  90. #else
  91. #define ATTRIBUTE_TARGET_POPCNT
  92. #endif
  93. #ifdef __clang__ // avoid gcc warning.
  94. #if __has_attribute(no_sanitize)
  95. #define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
  96. #else
  97. #define ATTRIBUTE_NO_SANITIZE_MEMORY
  98. #endif
  99. #define ALWAYS_INLINE __attribute__((always_inline))
  100. #else
  101. #define ATTRIBUTE_NO_SANITIZE_MEMORY
  102. #define ALWAYS_INLINE
  103. #endif // __clang__
  104. #if LIBFUZZER_WINDOWS
  105. #define ATTRIBUTE_NO_SANITIZE_ADDRESS
  106. #else
  107. #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
  108. #endif
  109. #if LIBFUZZER_WINDOWS
  110. #define ATTRIBUTE_ALIGNED(X) __declspec(align(X))
  111. #define ATTRIBUTE_INTERFACE __declspec(dllexport)
  112. // This is used for __sancov_lowest_stack which is needed for
  113. // -fsanitize-coverage=stack-depth. That feature is not yet available on
  114. // Windows, so make the symbol static to avoid linking errors.
  115. #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC static
  116. #define ATTRIBUTE_NOINLINE __declspec(noinline)
  117. #else
  118. #define ATTRIBUTE_ALIGNED(X) __attribute__((aligned(X)))
  119. #define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
  120. #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC \
  121. ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) thread_local
  122. #define ATTRIBUTE_NOINLINE __attribute__((noinline))
  123. #endif
  124. #if defined(__has_feature)
  125. #if __has_feature(address_sanitizer)
  126. #define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
  127. #elif __has_feature(memory_sanitizer)
  128. #define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
  129. #else
  130. #define ATTRIBUTE_NO_SANITIZE_ALL
  131. #endif
  132. #else
  133. #define ATTRIBUTE_NO_SANITIZE_ALL
  134. #endif
  135. #endif // LLVM_FUZZER_PLATFORM_H