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