Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / fuzzer / FuzzerExtFunctionsWindows.cpp
blob688bad1d51ca5bc3454b7b7a6d8bc0c8f52b676d
1 //=== FuzzerExtWindows.cpp - Interface to external functions --------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 // Implementation of FuzzerExtFunctions for Windows. Uses alternatename when
9 // compiled with MSVC. Uses weak aliases when compiled with clang. Unfortunately
10 // the method each compiler supports is not supported by the other.
11 //===----------------------------------------------------------------------===//
12 #include "FuzzerPlatform.h"
13 #if LIBFUZZER_WINDOWS
15 #include "FuzzerExtFunctions.h"
16 #include "FuzzerIO.h"
18 using namespace fuzzer;
20 // Intermediate macro to ensure the parameter is expanded before stringified.
21 #define STRINGIFY_(A) #A
22 #define STRINGIFY(A) STRINGIFY_(A)
24 #if LIBFUZZER_MSVC
25 // Copied from compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
26 #if defined(_M_IX86) || defined(__i386__)
27 #define WIN_SYM_PREFIX "_"
28 #else
29 #define WIN_SYM_PREFIX
30 #endif
32 // Declare external functions as having alternativenames, so that we can
33 // determine if they are not defined.
34 #define EXTERNAL_FUNC(Name, Default) \
35 __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY( \
36 Name) "=" WIN_SYM_PREFIX STRINGIFY(Default)))
37 #else
38 // Declare external functions as weak to allow them to default to a specified
39 // function if not defined explicitly. We must use weak symbols because clang's
40 // support for alternatename is not 100%, see
41 // https://bugs.llvm.org/show_bug.cgi?id=40218 for more details.
42 #define EXTERNAL_FUNC(Name, Default) \
43 __attribute__((weak, alias(STRINGIFY(Default))))
44 #endif // LIBFUZZER_MSVC
46 extern "C" {
47 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
48 RETURN_TYPE NAME##Def FUNC_SIG { \
49 Printf("ERROR: Function \"%s\" not defined.\n", #NAME); \
50 exit(1); \
51 } \
52 EXTERNAL_FUNC(NAME, NAME##Def) RETURN_TYPE NAME FUNC_SIG
54 #include "FuzzerExtFunctions.def"
56 #undef EXT_FUNC
59 template <typename T>
60 static T *GetFnPtr(T *Fun, T *FunDef, const char *FnName, bool WarnIfMissing) {
61 if (Fun == FunDef) {
62 if (WarnIfMissing)
63 Printf("WARNING: Failed to find function \"%s\".\n", FnName);
64 return nullptr;
66 return Fun;
69 namespace fuzzer {
71 ExternalFunctions::ExternalFunctions() {
72 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
73 this->NAME = GetFnPtr<decltype(::NAME)>(::NAME, ::NAME##Def, #NAME, WARN);
75 #include "FuzzerExtFunctions.def"
77 #undef EXT_FUNC
80 } // namespace fuzzer
82 #endif // LIBFUZZER_WINDOWS