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