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 14 15 #include "FuzzerExtFunctions.h" 16 #include "FuzzerIO.h" 17 #include <stdlib.h> 18 19 using namespace fuzzer; 20 21 // Intermediate macro to ensure the parameter is expanded before stringified. 22 #define STRINGIFY_(A) #A 23 #define STRINGIFY(A) STRINGIFY_(A) 24 25 #if LIBFUZZER_MSVC 26 #define GET_FUNCTION_ADDRESS(fn) &fn 27 #else 28 #define GET_FUNCTION_ADDRESS(fn) __builtin_function_start(fn) 29 #endif // LIBFUZER_MSVC 30 31 // Copied from compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h 32 #if defined(_M_IX86) || defined(__i386__) 33 #define WIN_SYM_PREFIX "_" 34 #else 35 #define WIN_SYM_PREFIX 36 #endif 37 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))) 43 44 extern "C" { 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); \ 48 exit(1); \ 49 } \ 50 EXTERNAL_FUNC(NAME, NAME##Def) RETURN_TYPE NAME FUNC_SIG 51 52 #include "FuzzerExtFunctions.def" 53 54 #undef EXT_FUNC 55 } 56 57 template <typename T> 58 static T *GetFnPtr(void *Fun, void *FunDef, const char *FnName, 59 bool WarnIfMissing) { 60 if (Fun == FunDef) { 61 if (WarnIfMissing) 62 Printf("WARNING: Failed to find function \"%s\".\n", FnName); 63 return nullptr; 64 } 65 return (T *)Fun; 66 } 67 68 namespace fuzzer { 69 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), \ 74 #NAME, WARN); 75 76 #include "FuzzerExtFunctions.def" 77 78 #undef EXT_FUNC 79 } 80 81 } // namespace fuzzer 82 83 #endif // LIBFUZZER_WINDOWS 84