george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 18 | // This must be included after Windows.h. |
| 19 | #include "Psapi.h" |
| 20 | |
| 21 | namespace fuzzer { |
| 22 | |
| 23 | ExternalFunctions::ExternalFunctions() { |
| 24 | HMODULE Modules[1024]; |
| 25 | DWORD BytesNeeded; |
| 26 | HANDLE CurrentProcess = GetCurrentProcess(); |
| 27 | |
| 28 | if (!EnumProcessModules(CurrentProcess, Modules, sizeof(Modules), |
| 29 | &BytesNeeded)) { |
| 30 | Printf("EnumProcessModules failed (error: %d).\n", GetLastError()); |
| 31 | exit(1); |
| 32 | } |
| 33 | |
| 34 | if (sizeof(Modules) < BytesNeeded) { |
| 35 | Printf("Error: the array is not big enough to hold all loaded modules.\n"); |
| 36 | exit(1); |
| 37 | } |
| 38 | |
| 39 | for (size_t i = 0; i < (BytesNeeded / sizeof(HMODULE)); i++) |
| 40 | { |
| 41 | FARPROC Fn; |
| 42 | #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \ |
| 43 | if (this->NAME == nullptr) { \ |
| 44 | Fn = GetProcAddress(Modules[i], #NAME); \ |
| 45 | if (Fn == nullptr) \ |
| 46 | Fn = GetProcAddress(Modules[i], #NAME "__dll"); \ |
| 47 | this->NAME = (decltype(ExternalFunctions::NAME)) Fn; \ |
| 48 | } |
| 49 | #include "FuzzerExtFunctions.def" |
| 50 | #undef EXT_FUNC |
| 51 | } |
| 52 | |
| 53 | #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \ |
| 54 | if (this->NAME == nullptr && WARN) \ |
| 55 | Printf("WARNING: Failed to find function \"%s\".\n", #NAME); |
| 56 | #include "FuzzerExtFunctions.def" |
| 57 | #undef EXT_FUNC |
| 58 | } |
| 59 | |
| 60 | } // namespace fuzzer |
| 61 | |
| 62 | #endif // LIBFUZZER_WINDOWS |