• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- FuzzerExtFunctionsDlsym.cpp - Interface to external functions ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // Implementation for operating systems that support dlsym(). We only use it on
10 // Apple platforms for now. We don't use this approach on Linux because it
11 // requires that clients of LibFuzzer pass ``--export-dynamic`` to the linker.
12 // That is a complication we don't wish to expose to clients right now.
13 //===----------------------------------------------------------------------===//
14 #include "FuzzerInternal.h"
15 #if LIBFUZZER_APPLE
16 
17 #include "FuzzerExtFunctions.h"
18 #include <dlfcn.h>
19 
20 using namespace fuzzer;
21 
22 template <typename T>
GetFnPtr(const char * FnName,bool WarnIfMissing)23 static T GetFnPtr(const char *FnName, bool WarnIfMissing) {
24   dlerror(); // Clear any previous errors.
25   void *Fn = dlsym(RTLD_DEFAULT, FnName);
26   if (Fn == nullptr) {
27     if (WarnIfMissing) {
28       const char *ErrorMsg = dlerror();
29       Printf("WARNING: Failed to find function \"%s\".", FnName);
30       if (ErrorMsg)
31         Printf(" Reason %s.", ErrorMsg);
32       Printf("\n");
33     }
34   }
35   return reinterpret_cast<T>(Fn);
36 }
37 
38 namespace fuzzer {
39 
ExternalFunctions()40 ExternalFunctions::ExternalFunctions() {
41 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
42   this->NAME = GetFnPtr<decltype(ExternalFunctions::NAME)>(#NAME, WARN)
43 
44 #include "FuzzerExtFunctions.def"
45 
46 #undef EXT_FUNC
47 }
48 } // namespace fuzzer
49 #endif // LIBFUZZER_APPLE
50