1 //=== FuzzerExtWindows.cpp - Interface to external functions --------------===//
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
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"
15 #include "FuzzerExtFunctions.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)
25 // Copied from compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
26 #if defined(_M_IX86) || defined(__i386__)
27 #define WIN_SYM_PREFIX "_"
29 #define WIN_SYM_PREFIX
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)))
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
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); \
52 EXTERNAL_FUNC(NAME, NAME##Def) RETURN_TYPE NAME FUNC_SIG
54 #include "FuzzerExtFunctions.def"
60 static T
*GetFnPtr(T
*Fun
, T
*FunDef
, const char *FnName
, bool WarnIfMissing
) {
63 Printf("WARNING: Failed to find function \"%s\".\n", FnName
);
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"
82 #endif // LIBFUZZER_WINDOWS