1 //===- FuzzerExtFunctionsDlsym.cpp - Interface to external functions ------===//
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
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"
16 #include "FuzzerExtFunctions.h"
20 using namespace fuzzer
;
23 static T
GetFnPtr(const char *FnName
, bool WarnIfMissing
) {
24 dlerror(); // Clear any previous errors.
25 void *Fn
= dlsym(RTLD_DEFAULT
, FnName
);
28 const char *ErrorMsg
= dlerror();
29 Printf("WARNING: Failed to find function \"%s\".", FnName
);
31 Printf(" Reason %s.", ErrorMsg
);
35 return reinterpret_cast<T
>(Fn
);
40 ExternalFunctions::ExternalFunctions() {
41 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
42 this->NAME = GetFnPtr<decltype(ExternalFunctions::NAME)>(#NAME, WARN)
44 #include "FuzzerExtFunctions.def"
51 #endif // LIBFUZZER_APPLE