tools/fuzzing/libfuzzer/FuzzerExtFunctionsWeakAlias.cpp
author Sebastian Hengst <archaeopteryx@coole-files.de>
Tue, 31 Oct 2017 16:20:11 +0100
changeset 389327 19f207add6c8ba25e25273e807a9fff22fa41b84
parent 359065 3e17a3e6de6f156ce68c4be10093f84e786aac3a
permissions -rw-r--r--
Backed out changeset 31f259ee387b (bug 1411004) for failing flake8 at tools/mach_commands.py:307:14 | indentation is not a multiple of four. r=backout

//===- FuzzerExtFunctionsWeakAlias.cpp - Interface to external functions --===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Implementation using weak aliases. Works for Windows.
//===----------------------------------------------------------------------===//
#include "FuzzerDefs.h"
#if LIBFUZZER_WINDOWS

#include "FuzzerExtFunctions.h"
#include "FuzzerIO.h"

using namespace fuzzer;

extern "C" {
// Declare these symbols as weak to allow them to be optionally defined.
#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
  RETURN_TYPE NAME##Def FUNC_SIG {                                             \
    Printf("ERROR: Function \"%s\" not defined.\n", #NAME);                    \
    exit(1);                                                                   \
  }                                                                            \
  RETURN_TYPE NAME FUNC_SIG __attribute__((weak, alias(#NAME "Def")));

#include "FuzzerExtFunctions.def"

#undef EXT_FUNC
}

template <typename T>
static T *GetFnPtr(T *Fun, T *FunDef, const char *FnName, bool WarnIfMissing) {
  if (Fun == FunDef) {
    if (WarnIfMissing)
      Printf("WARNING: Failed to find function \"%s\".\n", FnName);
    return nullptr;
  }
  return Fun;
}

namespace fuzzer {

ExternalFunctions::ExternalFunctions() {
#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
  this->NAME = GetFnPtr<decltype(::NAME)>(::NAME, ::NAME##Def, #NAME, WARN);

#include "FuzzerExtFunctions.def"

#undef EXT_FUNC
}

} // namespace fuzzer

#endif // LIBFUZZER_WINDOWS