Last active
November 12, 2021 06:17
-
-
Save australeo/ab710af5829b791dbe70726c8fb18110 to your computer and use it in GitHub Desktop.
Simple loader for testing shared memory fuzz harnesses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <iostream> | |
#define MAX_SAMPLE_SIZE 1000000 | |
#define SHM_SIZE (4 + MAX_SAMPLE_SIZE) | |
char* g_pbSharedMemory = nullptr; | |
HANDLE g_hMappingFile = nullptr; | |
/// <summary> | |
/// Print out a debug message and the result of GetLastError. | |
/// </summary> | |
/// <param name="szMsg">The message to output, usually the name of the function that errored.</param> | |
void DebugPrint(const char* szMsg) { | |
printf("[!] Error: %s, GLE=0x%x\n", szMsg, GetLastError()); | |
} | |
/// <summary> | |
/// Read data from a file into the shared memory buffer. | |
/// </summary> | |
/// <param name="szFilePath">Path to file</param> | |
/// <returns>0 == fail, 1 == success</returns> | |
int ReadInputFile(const char* szFilePath) { | |
HANDLE hFile = CreateFileA(szFilePath, | |
GENERIC_READ, | |
FILE_SHARE_READ, | |
NULL, | |
OPEN_EXISTING, | |
FILE_ATTRIBUTE_NORMAL, | |
NULL); | |
if (!hFile) { | |
DebugPrint("CreateFileA"); | |
return 0; | |
} | |
unsigned int cbFile = GetFileSize(hFile, NULL); | |
if (cbFile == INVALID_FILE_SIZE || cbFile == 0) { | |
CloseHandle(hFile); | |
DebugPrint("GetFileSize"); | |
return 0; | |
} | |
if (cbFile > MAX_SAMPLE_SIZE) { | |
cbFile = MAX_SAMPLE_SIZE; | |
} | |
if (!g_pbSharedMemory) { | |
CloseHandle(hFile); | |
DebugPrint("Shared memory region does not exist!"); | |
return 0; | |
} | |
DWORD cbRead = 0; | |
bool bResult = ReadFile(hFile, g_pbSharedMemory + 4, cbFile, &cbRead, NULL); | |
if (!bResult || cbRead != cbFile) { | |
CloseHandle(hFile); | |
DebugPrint("ReadFile"); | |
return 0; | |
} | |
*reinterpret_cast<unsigned int*>(g_pbSharedMemory) = cbFile; | |
CloseHandle(hFile); | |
return 1; | |
} | |
/// <summary> | |
/// Setup a shared memory region. | |
/// </summary> | |
/// <param name="szShareName">Name of the shared region.</param> | |
/// <returns>0 == fail, 1 == success</returns> | |
int ShareInput(const char* szShareName) { | |
g_hMappingFile = CreateFileMappingA( | |
INVALID_HANDLE_VALUE, // use paging file | |
NULL, // default security | |
PAGE_READWRITE, // read/write access | |
0, // maximum object size (high-order DWORD) | |
SHM_SIZE, // maximum object size (low-order DWORD) | |
szShareName); // name of mapping object | |
if (!g_hMappingFile) { | |
DebugPrint("CreateFileMappingA"); | |
return 0; | |
} | |
g_pbSharedMemory = (char*)MapViewOfFile(g_hMappingFile, // handle to map object | |
FILE_MAP_ALL_ACCESS, // read/write permission | |
0, | |
0, | |
SHM_SIZE); | |
if (!g_pbSharedMemory) { | |
CloseHandle(g_hMappingFile); | |
DebugPrint("MapViewOfFile"); | |
return 0; | |
} | |
return 1; | |
} | |
int main(int argc, char** argv) | |
{ | |
if (argc < 3) { | |
printf("Usage: HarnessLoader.exe <file_name> <shared_memory_name>\n"); | |
return 1; | |
} | |
int iResult = ShareInput(argv[2]); | |
if (iResult == 0) { | |
return 0; | |
} | |
iResult = ReadInputFile(argv[1]); | |
if (iResult != 0) { | |
printf("File successfully mapped! Press any key to close the mapping.\n"); | |
getchar(); | |
} | |
UnmapViewOfFile(g_pbSharedMemory); | |
CloseHandle(g_hMappingFile); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment