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