1*9880d681SAndroid Build Coastguard Worker //===- FuzzerExtFunctionsWeak.cpp - Interface to external functions -------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker // Implementation for Linux. This relies on the linker's support for weak
10*9880d681SAndroid Build Coastguard Worker // symbols. We don't use this approach on Apple platforms because it requires
11*9880d681SAndroid Build Coastguard Worker // clients of LibFuzzer to pass ``-U _<symbol_name>`` to the linker to allow
12*9880d681SAndroid Build Coastguard Worker // weak symbols to be undefined. That is a complication we don't want to expose
13*9880d681SAndroid Build Coastguard Worker // to clients right now.
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker #include "FuzzerInternal.h"
16*9880d681SAndroid Build Coastguard Worker #if LIBFUZZER_LINUX
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard Worker #include "FuzzerExtFunctions.h"
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker extern "C" {
21*9880d681SAndroid Build Coastguard Worker // Declare these symbols as weak to allow them to be optionally defined.
22*9880d681SAndroid Build Coastguard Worker #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
23*9880d681SAndroid Build Coastguard Worker __attribute__((weak)) RETURN_TYPE NAME FUNC_SIG
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker #include "FuzzerExtFunctions.def"
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker #undef EXT_FUNC
28*9880d681SAndroid Build Coastguard Worker }
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker using namespace fuzzer;
31*9880d681SAndroid Build Coastguard Worker
CheckFnPtr(void * FnPtr,const char * FnName,bool WarnIfMissing)32*9880d681SAndroid Build Coastguard Worker static void CheckFnPtr(void *FnPtr, const char *FnName, bool WarnIfMissing) {
33*9880d681SAndroid Build Coastguard Worker if (FnPtr == nullptr && WarnIfMissing) {
34*9880d681SAndroid Build Coastguard Worker Printf("WARNING: Failed to find function \"%s\".\n", FnName);
35*9880d681SAndroid Build Coastguard Worker }
36*9880d681SAndroid Build Coastguard Worker }
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker namespace fuzzer {
39*9880d681SAndroid Build Coastguard Worker
ExternalFunctions()40*9880d681SAndroid Build Coastguard Worker ExternalFunctions::ExternalFunctions() {
41*9880d681SAndroid Build Coastguard Worker #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
42*9880d681SAndroid Build Coastguard Worker this->NAME = ::NAME; \
43*9880d681SAndroid Build Coastguard Worker CheckFnPtr((void *)::NAME, #NAME, WARN);
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker #include "FuzzerExtFunctions.def"
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker #undef EXT_FUNC
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker } // namespace fuzzer
50*9880d681SAndroid Build Coastguard Worker #endif // LIBFUZZER_LINUX
51