FuzzerExtFunctionsWindows.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //=== FuzzerExtWindows.cpp - Interface to external functions --------------===//
  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. // Implementation of FuzzerExtFunctions for Windows. Uses alternatename when
  9. // compiled with MSVC. Uses weak aliases when compiled with clang. Unfortunately
  10. // the method each compiler supports is not supported by the other.
  11. //===----------------------------------------------------------------------===//
  12. #include "FuzzerPlatform.h"
  13. #if LIBFUZZER_WINDOWS
  14. #include "FuzzerExtFunctions.h"
  15. #include "FuzzerIO.h"
  16. using namespace fuzzer;
  17. // Intermediate macro to ensure the parameter is expanded before stringified.
  18. #define STRINGIFY_(A) #A
  19. #define STRINGIFY(A) STRINGIFY_(A)
  20. #if LIBFUZZER_MSVC
  21. // Copied from compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
  22. #if defined(_M_IX86) || defined(__i386__)
  23. #define WIN_SYM_PREFIX "_"
  24. #else
  25. #define WIN_SYM_PREFIX
  26. #endif
  27. // Declare external functions as having alternativenames, so that we can
  28. // determine if they are not defined.
  29. #define EXTERNAL_FUNC(Name, Default) \
  30. __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY( \
  31. Name) "=" WIN_SYM_PREFIX STRINGIFY(Default)))
  32. #else
  33. // Declare external functions as weak to allow them to default to a specified
  34. // function if not defined explicitly. We must use weak symbols because clang's
  35. // support for alternatename is not 100%, see
  36. // https://bugs.llvm.org/show_bug.cgi?id=40218 for more details.
  37. #define EXTERNAL_FUNC(Name, Default) \
  38. __attribute__((weak, alias(STRINGIFY(Default))))
  39. #endif // LIBFUZZER_MSVC
  40. extern "C" {
  41. #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
  42. RETURN_TYPE NAME##Def FUNC_SIG { \
  43. Printf("ERROR: Function \"%s\" not defined.\n", #NAME); \
  44. exit(1); \
  45. } \
  46. EXTERNAL_FUNC(NAME, NAME##Def) RETURN_TYPE NAME FUNC_SIG
  47. #include "FuzzerExtFunctions.def"
  48. #undef EXT_FUNC
  49. }
  50. template <typename T>
  51. static T *GetFnPtr(T *Fun, T *FunDef, const char *FnName, bool WarnIfMissing) {
  52. if (Fun == FunDef) {
  53. if (WarnIfMissing)
  54. Printf("WARNING: Failed to find function \"%s\".\n", FnName);
  55. return nullptr;
  56. }
  57. return Fun;
  58. }
  59. namespace fuzzer {
  60. ExternalFunctions::ExternalFunctions() {
  61. #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
  62. this->NAME = GetFnPtr<decltype(::NAME)>(::NAME, ::NAME##Def, #NAME, WARN);
  63. #include "FuzzerExtFunctions.def"
  64. #undef EXT_FUNC
  65. }
  66. } // namespace fuzzer
  67. #endif // LIBFUZZER_WINDOWS