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"
19 using namespace fuzzer
;
21 // Intermediate macro to ensure the parameter is expanded before stringified.
22 #define STRINGIFY_(A) #A
23 #define STRINGIFY(A) STRINGIFY_(A)
26 #define GET_FUNCTION_ADDRESS(fn) &fn
28 #define GET_FUNCTION_ADDRESS(fn) __builtin_function_start(fn)
29 #endif // LIBFUZER_MSVC
31 // Copied from compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
32 #if defined(_M_IX86) || defined(__i386__)
33 #define WIN_SYM_PREFIX "_"
35 #define WIN_SYM_PREFIX
38 // Declare external functions as having alternativenames, so that we can
39 // determine if they are not defined.
40 #define EXTERNAL_FUNC(Name, Default) \
41 __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY( \
42 Name) "=" WIN_SYM_PREFIX STRINGIFY(Default)))
45 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
46 RETURN_TYPE NAME##Def FUNC_SIG { \
47 Printf("ERROR: Function \"%s\" not defined.\n", #NAME); \
50 EXTERNAL_FUNC(NAME, NAME##Def) RETURN_TYPE NAME FUNC_SIG
52 #include "FuzzerExtFunctions.def"
58 static T
*GetFnPtr(void *Fun
, void *FunDef
, const char *FnName
,
62 Printf("WARNING: Failed to find function \"%s\".\n", FnName
);
70 ExternalFunctions::ExternalFunctions() {
71 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
72 this->NAME = GetFnPtr<decltype(::NAME)>(GET_FUNCTION_ADDRESS(::NAME), \
73 GET_FUNCTION_ADDRESS(::NAME##Def), \
76 #include "FuzzerExtFunctions.def"
83 #endif // LIBFUZZER_WINDOWS