10b57cec5SDimitry Andric //===- FuzzerExtFunctionsWeak.cpp - Interface to external functions -------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric // Implementation for Linux. This relies on the linker's support for weak
90b57cec5SDimitry Andric // symbols. We don't use this approach on Apple platforms because it requires
100b57cec5SDimitry Andric // clients of LibFuzzer to pass ``-U _<symbol_name>`` to the linker to allow
110b57cec5SDimitry Andric // weak symbols to be undefined. That is a complication we don't want to expose
120b57cec5SDimitry Andric // to clients right now.
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
145ffd83dbSDimitry Andric #include "FuzzerPlatform.h"
150b57cec5SDimitry Andric #if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FUCHSIA || \
16*e8d8bef9SDimitry Andric LIBFUZZER_FREEBSD || LIBFUZZER_EMSCRIPTEN
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric #include "FuzzerExtFunctions.h"
190b57cec5SDimitry Andric #include "FuzzerIO.h"
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric extern "C" {
220b57cec5SDimitry Andric // Declare these symbols as weak to allow them to be optionally defined.
230b57cec5SDimitry Andric #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
240b57cec5SDimitry Andric __attribute__((weak, visibility("default"))) RETURN_TYPE NAME FUNC_SIG
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric #include "FuzzerExtFunctions.def"
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric #undef EXT_FUNC
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric using namespace fuzzer;
320b57cec5SDimitry Andric
CheckFnPtr(void * FnPtr,const char * FnName,bool WarnIfMissing)330b57cec5SDimitry Andric static void CheckFnPtr(void *FnPtr, const char *FnName, bool WarnIfMissing) {
340b57cec5SDimitry Andric if (FnPtr == nullptr && WarnIfMissing) {
350b57cec5SDimitry Andric Printf("WARNING: Failed to find function \"%s\".\n", FnName);
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric namespace fuzzer {
400b57cec5SDimitry Andric
ExternalFunctions()410b57cec5SDimitry Andric ExternalFunctions::ExternalFunctions() {
420b57cec5SDimitry Andric #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
430b57cec5SDimitry Andric this->NAME = ::NAME; \
440b57cec5SDimitry Andric CheckFnPtr(reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(::NAME)), \
450b57cec5SDimitry Andric #NAME, WARN);
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric #include "FuzzerExtFunctions.def"
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric #undef EXT_FUNC
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric } // namespace fuzzer
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric #endif
55