blob: 7c47dfae3124da8783033f1e609212d67172dc40 [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- 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"
kamil21423232018-01-12 17:15:05 +000016#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FUCHSIA || LIBFUZZER_FREEBSD
george.karpenkov29efa6d2017-08-21 23:25:50 +000017
18#include "FuzzerExtFunctions.h"
19#include "FuzzerIO.h"
20
21extern "C" {
22// Declare these symbols as weak to allow them to be optionally defined.
23#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
24 __attribute__((weak)) RETURN_TYPE NAME FUNC_SIG
25
26#include "FuzzerExtFunctions.def"
27
28#undef EXT_FUNC
29}
30
31using namespace fuzzer;
32
33static 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
39namespace fuzzer {
40
41ExternalFunctions::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
kamil21423232018-01-12 17:15:05 +000054#endif // LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FUSCHIA || LIBFUZZER_FREEBSD