Path: blob/main/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerExtFunctionsDlsym.cpp
35262 views
//===- FuzzerExtFunctionsDlsym.cpp - Interface to external functions ------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// Implementation for operating systems that support dlsym(). We only use it on8// Apple platforms for now. We don't use this approach on Linux because it9// requires that clients of LibFuzzer pass ``--export-dynamic`` to the linker.10// That is a complication we don't wish to expose to clients right now.11//===----------------------------------------------------------------------===//12#include "FuzzerPlatform.h"13#if LIBFUZZER_APPLE1415#include "FuzzerExtFunctions.h"16#include "FuzzerIO.h"17#include <dlfcn.h>1819using namespace fuzzer;2021template <typename T>22static T GetFnPtr(const char *FnName, bool WarnIfMissing) {23dlerror(); // Clear any previous errors.24void *Fn = dlsym(RTLD_DEFAULT, FnName);25if (Fn == nullptr) {26if (WarnIfMissing) {27const char *ErrorMsg = dlerror();28Printf("WARNING: Failed to find function \"%s\".", FnName);29if (ErrorMsg)30Printf(" Reason %s.", ErrorMsg);31Printf("\n");32}33}34return reinterpret_cast<T>(Fn);35}3637namespace fuzzer {3839ExternalFunctions::ExternalFunctions() {40#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \41this->NAME = GetFnPtr<decltype(ExternalFunctions::NAME)>(#NAME, WARN)4243#include "FuzzerExtFunctions.def"4445#undef EXT_FUNC46}4748} // namespace fuzzer4950#endif // LIBFUZZER_APPLE515253