blob: 77521698c80a4decfcb2dc8e5c2e841ee2a8dfbe [file] [log] [blame]
Marcos Pividori0ae27e82017-02-10 01:35:46 +00001//===- FuzzerExtFunctionsDlsymWin.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 dynamic loading for Windows.
10//===----------------------------------------------------------------------===//
11#include "FuzzerDefs.h"
12#if LIBFUZZER_WINDOWS
13
14#include "FuzzerExtFunctions.h"
15#include "FuzzerIO.h"
16#include "Windows.h"
17#include "Psapi.h"
18
19namespace fuzzer {
20
21ExternalFunctions::ExternalFunctions() {
22 HMODULE Modules[1024];
23 DWORD BytesNeeded;
24 HANDLE CurrentProcess = GetCurrentProcess();
25
26 if (!EnumProcessModules(CurrentProcess, Modules, sizeof(Modules),
27 &BytesNeeded)) {
28 Printf("EnumProcessModules failed (error: %d).\n", GetLastError());
29 exit(1);
30 }
31
32 if (sizeof(Modules) < BytesNeeded) {
33 Printf("Error: the array is not big enough to hold all loaded modules.\n");
34 exit(1);
35 }
36
37 for (size_t i = 0; i < (BytesNeeded / sizeof(HMODULE)); i++)
38 {
39 FARPROC Fn;
40#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
41 if (this->NAME == nullptr) { \
42 Fn = GetProcAddress(Modules[i], #NAME); \
43 if (Fn == nullptr) \
44 Fn = GetProcAddress(Modules[i], #NAME "__dll"); \
45 this->NAME = (decltype(ExternalFunctions::NAME)) Fn; \
46 }
47#include "FuzzerExtFunctions.def"
48#undef EXT_FUNC
49 }
50
51#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
52 if (this->NAME == nullptr && WARN) \
53 Printf("WARNING: Failed to find function \"%s\".\n", #NAME);
54#include "FuzzerExtFunctions.def"
55#undef EXT_FUNC
56}
57
58} // namespace fuzzer
59
60#endif // LIBFUZZER_WINDOWS