FuzzerExtFunctionsWeak.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //===- FuzzerExtFunctionsWeak.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 for Linux. This relies on the linker's support for weak
  9. // symbols. We don't use this approach on Apple platforms because it requires
  10. // clients of LibFuzzer to pass ``-U _<symbol_name>`` to the linker to allow
  11. // weak symbols to be undefined. That is a complication we don't want to expose
  12. // to clients right now.
  13. //===----------------------------------------------------------------------===//
  14. #include "FuzzerPlatform.h"
  15. #if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FUCHSIA || \
  16. LIBFUZZER_FREEBSD || LIBFUZZER_EMSCRIPTEN
  17. #include "FuzzerExtFunctions.h"
  18. #include "FuzzerIO.h"
  19. extern "C" {
  20. // Declare these symbols as weak to allow them to be optionally defined.
  21. #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
  22. __attribute__((weak, visibility("default"))) RETURN_TYPE NAME FUNC_SIG
  23. #include "FuzzerExtFunctions.def"
  24. #undef EXT_FUNC
  25. }
  26. using namespace fuzzer;
  27. static void CheckFnPtr(void *FnPtr, const char *FnName, bool WarnIfMissing) {
  28. if (FnPtr == nullptr && WarnIfMissing) {
  29. Printf("WARNING: Failed to find function \"%s\".\n", FnName);
  30. }
  31. }
  32. namespace fuzzer {
  33. ExternalFunctions::ExternalFunctions() {
  34. #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
  35. this->NAME = ::NAME; \
  36. CheckFnPtr(reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(::NAME)), \
  37. #NAME, WARN);
  38. #include "FuzzerExtFunctions.def"
  39. #undef EXT_FUNC
  40. }
  41. } // namespace fuzzer
  42. #endif