Skip to content

Instantly share code, notes, and snippets.

@goldshtn
Created February 16, 2014 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldshtn/9032551 to your computer and use it in GitHub Desktop.
Save goldshtn/9032551 to your computer and use it in GitHub Desktop.
Non-Paged Pool Leak
#include <Windows.h>
#include <Psapi.h>
#include <string>
#include <iostream>
void VERIFY(bool condition, std::string message)
{
if (!condition)
{
DWORD lastError = GetLastError();
std::cerr << "Assertion failed: " << message
<< ", last error " << std::hex << lastError
<< std::endl;
exit(1);
}
}
int main()
{
HANDLE iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 0);
VERIFY(iocp != nullptr, "Error creating I/O completion port");
HANDLE file = CreateFile(L"File.txt", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
VERIFY(file != nullptr, "Error opening file for reading");
VERIFY(CreateIoCompletionPort(file, iocp, 0, 0) != nullptr, "Error associating file and IOCP");
for (int i = 0; ; ++i)
{
OVERLAPPED overlapped = { 0 };
overlapped.hEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
VERIFY(overlapped.hEvent != nullptr, "Error creating event for overlapped I/O");
char buffer[10];
VERIFY(ReadFile(file, buffer, sizeof(buffer), nullptr, &overlapped) == TRUE || GetLastError() == ERROR_IO_PENDING, "Error reading from file");
VERIFY(WaitForSingleObject(overlapped.hEvent, INFINITE) == WAIT_OBJECT_0, "Error waiting for overlapped I/O completion");
DWORD bytesRead;
VERIFY(GetOverlappedResult(file, &overlapped, &bytesRead, TRUE) == TRUE, "Error obtaining overlapped result");
VERIFY(CloseHandle(overlapped.hEvent) == TRUE, "Error closing event handle");
PROCESS_MEMORY_COUNTERS memoryInfo = { 0 };
memoryInfo.cb = sizeof(memoryInfo);
VERIFY(GetProcessMemoryInfo(GetCurrentProcess(), &memoryInfo, sizeof(memoryInfo)) == TRUE, "Error getting process memory info");
std::cout << std::dec << "Operations: " << i
<< ", non-paged pool usage (KB): " << memoryInfo.QuotaNonPagedPoolUsage/1024
<< std::endl;
Sleep(50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment