Home | History | Annotate | Line # | Download | only in fuzzer
      1 //===- FuzzerExtFunctionsWeakAlias.cpp - Interface to external functions --===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 // Implementation using weak aliases. Works for Windows.
     10 //===----------------------------------------------------------------------===//
     11 #include "FuzzerDefs.h"
     12 #if LIBFUZZER_WINDOWS
     13 
     14 #include "FuzzerExtFunctions.h"
     15 #include "FuzzerIO.h"
     16 
     17 using namespace fuzzer;
     18 
     19 extern "C" {
     20 // Declare these symbols as weak to allow them to be optionally defined.
     21 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
     22   RETURN_TYPE NAME##Def FUNC_SIG {                                             \
     23     Printf("ERROR: Function \"%s\" not defined.\n", #NAME);                    \
     24     exit(1);                                                                   \
     25   }                                                                            \
     26   RETURN_TYPE NAME FUNC_SIG __attribute__((weak, alias(#NAME "Def")));
     27 
     28 #include "FuzzerExtFunctions.def"
     29 
     30 #undef EXT_FUNC
     31 }
     32 
     33 template <typename T>
     34 static T *GetFnPtr(T *Fun, T *FunDef, const char *FnName, bool WarnIfMissing) {
     35   if (Fun == FunDef) {
     36     if (WarnIfMissing)
     37       Printf("WARNING: Failed to find function \"%s\".\n", FnName);
     38     return nullptr;
     39   }
     40   return Fun;
     41 }
     42 
     43 namespace fuzzer {
     44 
     45 ExternalFunctions::ExternalFunctions() {
     46 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
     47   this->NAME = GetFnPtr<decltype(::NAME)>(::NAME, ::NAME##Def, #NAME, WARN);
     48 
     49 #include "FuzzerExtFunctions.def"
     50 
     51 #undef EXT_FUNC
     52 }
     53 
     54 } // namespace fuzzer
     55 
     56 #endif // LIBFUZZER_WINDOWS
     57