brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · cb29af9 Raw
84 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8// Implementation of FuzzerExtFunctions for Windows. Uses alternatename when9// compiled with MSVC. Uses weak aliases when compiled with clang. Unfortunately10// the method each compiler supports is not supported by the other.11//===----------------------------------------------------------------------===//12#include "FuzzerPlatform.h"13#if LIBFUZZER_WINDOWS14 15#include "FuzzerExtFunctions.h"16#include "FuzzerIO.h"17#include <stdlib.h>18 19using namespace fuzzer;20 21// Intermediate macro to ensure the parameter is expanded before stringified.22#define STRINGIFY_(A) #A23#define STRINGIFY(A) STRINGIFY_(A)24 25#if LIBFUZZER_MSVC26#define GET_FUNCTION_ADDRESS(fn) &fn27#else28#define GET_FUNCTION_ADDRESS(fn) __builtin_function_start(fn)29#endif // LIBFUZER_MSVC30 31// Copied from compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h32#if defined(_M_IX86) || defined(__i386__)33#define WIN_SYM_PREFIX "_"34#else35#define WIN_SYM_PREFIX36#endif37 38// Declare external functions as having alternative names, so that we can39// 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 44extern "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_SIG51 52#include "FuzzerExtFunctions.def"53 54#undef EXT_FUNC55}56 57template <typename T>58static 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 68namespace fuzzer {69 70ExternalFunctions::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_FUNC79}80 81}  // namespace fuzzer82 83#endif // LIBFUZZER_WINDOWS84