FuzzerExtFunctionsDlsym.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //===- FuzzerExtFunctionsDlsym.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 operating systems that support dlsym(). We only use it on
  9. // Apple platforms for now. We don't use this approach on Linux because it
  10. // requires that clients of LibFuzzer pass ``--export-dynamic`` to the linker.
  11. // That is a complication we don't wish to expose to clients right now.
  12. //===----------------------------------------------------------------------===//
  13. #include "FuzzerPlatform.h"
  14. #if LIBFUZZER_APPLE
  15. #include "FuzzerExtFunctions.h"
  16. #include "FuzzerIO.h"
  17. #include <dlfcn.h>
  18. using namespace fuzzer;
  19. template <typename T>
  20. static T GetFnPtr(const char *FnName, bool WarnIfMissing) {
  21. dlerror(); // Clear any previous errors.
  22. void *Fn = dlsym(RTLD_DEFAULT, FnName);
  23. if (Fn == nullptr) {
  24. if (WarnIfMissing) {
  25. const char *ErrorMsg = dlerror();
  26. Printf("WARNING: Failed to find function \"%s\".", FnName);
  27. if (ErrorMsg)
  28. Printf(" Reason %s.", ErrorMsg);
  29. Printf("\n");
  30. }
  31. }
  32. return reinterpret_cast<T>(Fn);
  33. }
  34. namespace fuzzer {
  35. ExternalFunctions::ExternalFunctions() {
  36. #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
  37. this->NAME = GetFnPtr<decltype(ExternalFunctions::NAME)>(#NAME, WARN)
  38. #include "FuzzerExtFunctions.def"
  39. #undef EXT_FUNC
  40. }
  41. } // namespace fuzzer
  42. #endif // LIBFUZZER_APPLE