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