Path: blob/main/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerExtFunctionsWindows.cpp
35262 views
//=== FuzzerExtWindows.cpp - Interface to external functions --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// Implementation of FuzzerExtFunctions for Windows. Uses alternatename when8// compiled with MSVC. Uses weak aliases when compiled with clang. Unfortunately9// the method each compiler supports is not supported by the other.10//===----------------------------------------------------------------------===//11#include "FuzzerPlatform.h"12#if LIBFUZZER_WINDOWS1314#include "FuzzerExtFunctions.h"15#include "FuzzerIO.h"1617using namespace fuzzer;1819// Intermediate macro to ensure the parameter is expanded before stringified.20#define STRINGIFY_(A) #A21#define STRINGIFY(A) STRINGIFY_(A)2223#if LIBFUZZER_MSVC24// Copied from compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h25#if defined(_M_IX86) || defined(__i386__)26#define WIN_SYM_PREFIX "_"27#else28#define WIN_SYM_PREFIX29#endif3031// Declare external functions as having alternativenames, so that we can32// determine if they are not defined.33#define EXTERNAL_FUNC(Name, Default) \34__pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY( \35Name) "=" WIN_SYM_PREFIX STRINGIFY(Default)))36#else37// Declare external functions as weak to allow them to default to a specified38// function if not defined explicitly. We must use weak symbols because clang's39// support for alternatename is not 100%, see40// https://bugs.llvm.org/show_bug.cgi?id=40218 for more details.41#define EXTERNAL_FUNC(Name, Default) \42__attribute__((weak, alias(STRINGIFY(Default))))43#endif // LIBFUZZER_MSVC4445extern "C" {46#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \47RETURN_TYPE NAME##Def FUNC_SIG { \48Printf("ERROR: Function \"%s\" not defined.\n", #NAME); \49exit(1); \50} \51EXTERNAL_FUNC(NAME, NAME##Def) RETURN_TYPE NAME FUNC_SIG5253#include "FuzzerExtFunctions.def"5455#undef EXT_FUNC56}5758template <typename T>59static T *GetFnPtr(T *Fun, T *FunDef, const char *FnName, bool WarnIfMissing) {60if (Fun == FunDef) {61if (WarnIfMissing)62Printf("WARNING: Failed to find function \"%s\".\n", FnName);63return nullptr;64}65return Fun;66}6768namespace fuzzer {6970ExternalFunctions::ExternalFunctions() {71#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \72this->NAME = GetFnPtr<decltype(::NAME)>(::NAME, ::NAME##Def, #NAME, WARN);7374#include "FuzzerExtFunctions.def"7576#undef EXT_FUNC77}7879} // namespace fuzzer8081#endif // LIBFUZZER_WINDOWS828384