1c5f01b2fSopenharmony_ci//===- FuzzerExtFunctionsWeak.cpp - Interface to external functions -------===//
2c5f01b2fSopenharmony_ci//
3c5f01b2fSopenharmony_ci//                     The LLVM Compiler Infrastructure
4c5f01b2fSopenharmony_ci//
5c5f01b2fSopenharmony_ci// This file is distributed under the University of Illinois Open Source
6c5f01b2fSopenharmony_ci// License. See LICENSE.TXT for details.
7c5f01b2fSopenharmony_ci//
8c5f01b2fSopenharmony_ci//===----------------------------------------------------------------------===//
9c5f01b2fSopenharmony_ci// Implementation for Linux. This relies on the linker's support for weak
10c5f01b2fSopenharmony_ci// symbols. We don't use this approach on Apple platforms because it requires
11c5f01b2fSopenharmony_ci// clients of LibFuzzer to pass ``-U _<symbol_name>`` to the linker to allow
12c5f01b2fSopenharmony_ci// weak symbols to be undefined. That is a complication we don't want to expose
13c5f01b2fSopenharmony_ci// to clients right now.
14c5f01b2fSopenharmony_ci//===----------------------------------------------------------------------===//
15c5f01b2fSopenharmony_ci#include "FuzzerDefs.h"
16c5f01b2fSopenharmony_ci#if LIBFUZZER_LINUX
17c5f01b2fSopenharmony_ci
18c5f01b2fSopenharmony_ci#include "FuzzerExtFunctions.h"
19c5f01b2fSopenharmony_ci#include "FuzzerIO.h"
20c5f01b2fSopenharmony_ci
21c5f01b2fSopenharmony_ciextern "C" {
22c5f01b2fSopenharmony_ci// Declare these symbols as weak to allow them to be optionally defined.
23c5f01b2fSopenharmony_ci#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
24c5f01b2fSopenharmony_ci  __attribute__((weak)) RETURN_TYPE NAME FUNC_SIG
25c5f01b2fSopenharmony_ci
26c5f01b2fSopenharmony_ci#include "FuzzerExtFunctions.def"
27c5f01b2fSopenharmony_ci
28c5f01b2fSopenharmony_ci#undef EXT_FUNC
29c5f01b2fSopenharmony_ci}
30c5f01b2fSopenharmony_ci
31c5f01b2fSopenharmony_ciusing namespace fuzzer;
32c5f01b2fSopenharmony_ci
33c5f01b2fSopenharmony_cistatic void CheckFnPtr(void *FnPtr, const char *FnName, bool WarnIfMissing) {
34c5f01b2fSopenharmony_ci  if (FnPtr == nullptr && WarnIfMissing) {
35c5f01b2fSopenharmony_ci    Printf("WARNING: Failed to find function \"%s\".\n", FnName);
36c5f01b2fSopenharmony_ci  }
37c5f01b2fSopenharmony_ci}
38c5f01b2fSopenharmony_ci
39c5f01b2fSopenharmony_cinamespace fuzzer {
40c5f01b2fSopenharmony_ci
41c5f01b2fSopenharmony_ciExternalFunctions::ExternalFunctions() {
42c5f01b2fSopenharmony_ci#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
43c5f01b2fSopenharmony_ci  this->NAME = ::NAME;                                                         \
44c5f01b2fSopenharmony_ci  CheckFnPtr((void *)::NAME, #NAME, WARN);
45c5f01b2fSopenharmony_ci
46c5f01b2fSopenharmony_ci#include "FuzzerExtFunctions.def"
47c5f01b2fSopenharmony_ci
48c5f01b2fSopenharmony_ci#undef EXT_FUNC
49c5f01b2fSopenharmony_ci}
50c5f01b2fSopenharmony_ci
51c5f01b2fSopenharmony_ci} // namespace fuzzer
52c5f01b2fSopenharmony_ci
53c5f01b2fSopenharmony_ci#endif // LIBFUZZER_LINUX
54