xref: /openbsd-src/gnu/llvm/compiler-rt/lib/fuzzer/FuzzerExtFunctionsDlsym.cpp (revision 3cab2bb3f667058bece8e38b12449a63a9d73c4b)
1*3cab2bb3Spatrick //===- FuzzerExtFunctionsDlsym.cpp - Interface to external functions ------===//
2*3cab2bb3Spatrick //
3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*3cab2bb3Spatrick //
7*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
8*3cab2bb3Spatrick // Implementation for operating systems that support dlsym(). We only use it on
9*3cab2bb3Spatrick // Apple platforms for now. We don't use this approach on Linux because it
10*3cab2bb3Spatrick // requires that clients of LibFuzzer pass ``--export-dynamic`` to the linker.
11*3cab2bb3Spatrick // That is a complication we don't wish to expose to clients right now.
12*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
13*3cab2bb3Spatrick #include "FuzzerDefs.h"
14*3cab2bb3Spatrick #if LIBFUZZER_APPLE
15*3cab2bb3Spatrick 
16*3cab2bb3Spatrick #include "FuzzerExtFunctions.h"
17*3cab2bb3Spatrick #include "FuzzerIO.h"
18*3cab2bb3Spatrick #include <dlfcn.h>
19*3cab2bb3Spatrick 
20*3cab2bb3Spatrick using namespace fuzzer;
21*3cab2bb3Spatrick 
22*3cab2bb3Spatrick template <typename T>
23*3cab2bb3Spatrick static T GetFnPtr(const char *FnName, bool WarnIfMissing) {
24*3cab2bb3Spatrick   dlerror(); // Clear any previous errors.
25*3cab2bb3Spatrick   void *Fn = dlsym(RTLD_DEFAULT, FnName);
26*3cab2bb3Spatrick   if (Fn == nullptr) {
27*3cab2bb3Spatrick     if (WarnIfMissing) {
28*3cab2bb3Spatrick       const char *ErrorMsg = dlerror();
29*3cab2bb3Spatrick       Printf("WARNING: Failed to find function \"%s\".", FnName);
30*3cab2bb3Spatrick       if (ErrorMsg)
31*3cab2bb3Spatrick         Printf(" Reason %s.", ErrorMsg);
32*3cab2bb3Spatrick       Printf("\n");
33*3cab2bb3Spatrick     }
34*3cab2bb3Spatrick   }
35*3cab2bb3Spatrick   return reinterpret_cast<T>(Fn);
36*3cab2bb3Spatrick }
37*3cab2bb3Spatrick 
38*3cab2bb3Spatrick namespace fuzzer {
39*3cab2bb3Spatrick 
40*3cab2bb3Spatrick ExternalFunctions::ExternalFunctions() {
41*3cab2bb3Spatrick #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
42*3cab2bb3Spatrick   this->NAME = GetFnPtr<decltype(ExternalFunctions::NAME)>(#NAME, WARN)
43*3cab2bb3Spatrick 
44*3cab2bb3Spatrick #include "FuzzerExtFunctions.def"
45*3cab2bb3Spatrick 
46*3cab2bb3Spatrick #undef EXT_FUNC
47*3cab2bb3Spatrick }
48*3cab2bb3Spatrick 
49*3cab2bb3Spatrick } // namespace fuzzer
50*3cab2bb3Spatrick 
51*3cab2bb3Spatrick #endif // LIBFUZZER_APPLE
52