summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2016-12-02 19:41:17 +0000
committerZachary Turner <zturner@google.com>2016-12-02 19:41:17 +0000
commitd755e4f587033ee37445a73d84de4483d9c9a6ee (patch)
treea9e0c7c73e6ff2c0e66dd19d7066ad70fed5e984 /llvm/lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp
parent34dcfb9294a751f53a2172aa249ecd028836ee46 (diff)
downloadbcm5719-llvm-d755e4f587033ee37445a73d84de4483d9c9a6ee.tar.gz
bcm5719-llvm-d755e4f587033ee37445a73d84de4483d9c9a6ee.zip
[LibFuzzer] Introduce a portable WeakAlias implementation.
Windows doesn't really support weak aliases, but with some linker magic we can get something that's pretty close on Windows. This introduces an interface to accessing weakly aliased symbols that will work on any platform. Linker magic changes to come in a separate patch. Patch by Marcos Pividori Differential Revision: https://reviews.llvm.org/D27235 llvm-svn: 288530
Diffstat (limited to 'llvm/lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp')
-rw-r--r--llvm/lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/llvm/lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp b/llvm/lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp
new file mode 100644
index 00000000000..3c42e539fcc
--- /dev/null
+++ b/llvm/lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp
@@ -0,0 +1,54 @@
+//===- FuzzerExtFunctionsWeakAlias.cpp - Interface to external functions --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// Implementation using weak aliases. Works for Windows.
+//===----------------------------------------------------------------------===//
+#include "FuzzerDefs.h"
+#if LIBFUZZER_WINDOWS
+
+#include "FuzzerExtFunctions.h"
+#include "FuzzerIO.h"
+
+using namespace fuzzer;
+
+extern "C" {
+// Declare these symbols as weak to allow them to be optionally defined.
+#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
+ RETURN_TYPE NAME##Def FUNC_SIG { \
+ Printf("ERROR: Function \"%s\" not defined.\n", #NAME); \
+ exit(1); \
+ } \
+ RETURN_TYPE NAME FUNC_SIG __attribute__((weak, alias(#NAME "Def")));
+
+#include "FuzzerExtFunctions.def"
+
+#undef EXT_FUNC
+}
+
+template <typename T>
+static T *GetFnPtr(T *Fun, T *FunDef, const char *FnName, bool WarnIfMissing) {
+ if (Fun == FunDef) {
+ if (WarnIfMissing)
+ Printf("WARNING: Failed to find function \"%s\".\n", FnName);
+ return nullptr;
+ }
+ return Fun;
+}
+
+namespace fuzzer {
+
+ExternalFunctions::ExternalFunctions() {
+#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
+ this->NAME = GetFnPtr<decltype(::NAME)>(::NAME, ::NAME##Def, #NAME, WARN);
+
+#include "FuzzerExtFunctions.def"
+
+#undef EXT_FUNC
+}
+} // namespace fuzzer
+#endif // LIBFUZZER_WINDOWS
OpenPOWER on IntegriCloud