From 9b29ef7a9fcf8e9686a26848a32d178e84f5dd63 Mon Sep 17 00:00:00 2001 From: Laurence Luo Date: Thu, 10 Oct 2024 22:41:54 +0800 Subject: [PATCH] Format codes --- NotMyFault/exe/driver.c | 278 ++++++------ NotMyFault/exe/notmyfault.c | 836 ++++++++++++++++++------------------ 2 files changed, 557 insertions(+), 557 deletions(-) diff --git a/NotMyFault/exe/driver.c b/NotMyFault/exe/driver.c index 66ab16c..37b1293 100644 --- a/NotMyFault/exe/driver.c +++ b/NotMyFault/exe/driver.c @@ -26,36 +26,36 @@ HANDLE SysHandle = INVALID_HANDLE_VALUE; ****************************************************************************/ BOOL InstallDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName, IN LPCTSTR ServiceExe) { - SC_HANDLE schService; - - // - // NOTE: This creates an entry for a standalone driver. If this - // is modified for use with a driver that requires a Tag, - // Group, and/or Dependencies, it may be necessary to - // query the registry for existing driver information - // (in order to determine a unique Tag, etc.). - // - - schService = CreateService(SchSCManager, // SCManager database - DriverName, // name of service - DriverName, // name to display - SERVICE_ALL_ACCESS, // desired access - SERVICE_KERNEL_DRIVER, // service type - SERVICE_DEMAND_START, // start type - SERVICE_ERROR_IGNORE, // error control type - ServiceExe, // service's binary - NULL, // no load ordering group - NULL, // no tag identifier - NULL, // no dependencies - NULL, // LocalSystem account - NULL // no password - ); - if (schService == NULL) - return FALSE; - - CloseServiceHandle(schService); - - return TRUE; + SC_HANDLE schService; + + // + // NOTE: This creates an entry for a standalone driver. If this + // is modified for use with a driver that requires a Tag, + // Group, and/or Dependencies, it may be necessary to + // query the registry for existing driver information + // (in order to determine a unique Tag, etc.). + // + + schService = CreateService(SchSCManager, // SCManager database + DriverName, // name of service + DriverName, // name to display + SERVICE_ALL_ACCESS, // desired access + SERVICE_KERNEL_DRIVER, // service type + SERVICE_DEMAND_START, // start type + SERVICE_ERROR_IGNORE, // error control type + ServiceExe, // service's binary + NULL, // no load ordering group + NULL, // no tag identifier + NULL, // no dependencies + NULL, // LocalSystem account + NULL // no password + ); + if (schService == NULL) + return FALSE; + + CloseServiceHandle(schService); + + return TRUE; } @@ -68,22 +68,22 @@ BOOL InstallDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName, IN LPCTSTR ****************************************************************************/ BOOL StartDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName) { - SC_HANDLE schService; - BOOL ret; - - schService = OpenService(SchSCManager, - DriverName, - SERVICE_ALL_ACCESS - ); - if (schService == NULL) - return FALSE; - - ret = StartService(schService, 0, NULL) - || GetLastError() == ERROR_SERVICE_ALREADY_RUNNING - || GetLastError() == ERROR_SERVICE_DISABLED; - - CloseServiceHandle(schService); - return ret; + SC_HANDLE schService; + BOOL ret; + + schService = OpenService(SchSCManager, + DriverName, + SERVICE_ALL_ACCESS + ); + if (schService == NULL) + return FALSE; + + ret = StartService(schService, 0, NULL) + || GetLastError() == ERROR_SERVICE_ALREADY_RUNNING + || GetLastError() == ERROR_SERVICE_DISABLED; + + CloseServiceHandle(schService); + return ret; } @@ -96,56 +96,56 @@ BOOL StartDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName) ****************************************************************************/ BOOL OpenDevice(IN LPCTSTR DriverName, HANDLE* lphDevice) { - TCHAR completeDeviceName[64]; - HANDLE hDevice; - - // - // Create a \\.\XXX device name that CreateFile can use - // - // NOTE: We're making an assumption here that the driver - // has created a symbolic link using it's own name - // (i.e. if the driver has the name "XXX" we assume - // that it used IoCreateSymbolicLink to create a - // symbolic link "\DosDevices\XXX". Usually, there - // is this understanding between related apps/drivers. - // - // An application might also peruse the DEVICEMAP - // section of the registry, or use the QueryDosDevice - // API to enumerate the existing symbolic links in the - // system. - // - - if ((GetVersion() & 0xFF) >= 5) - { - // - // We reference the global name so that the application can - // be executed in Terminal Services sessions on Win2K - // - wsprintf(completeDeviceName, TEXT("\\\\.\\Global\\%s"), DriverName); - } - else - { - wsprintf(completeDeviceName, TEXT("\\\\.\\%s"), DriverName); - } - - hDevice = CreateFile(completeDeviceName, - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL - ); - if (hDevice == ((HANDLE)-1)) - return FALSE; - - // If user wants handle, give it to them. Otherwise, just close it. - if (lphDevice) - *lphDevice = hDevice; - else - CloseHandle(hDevice); - - return TRUE; + TCHAR completeDeviceName[64]; + HANDLE hDevice; + + // + // Create a \\.\XXX device name that CreateFile can use + // + // NOTE: We're making an assumption here that the driver + // has created a symbolic link using it's own name + // (i.e. if the driver has the name "XXX" we assume + // that it used IoCreateSymbolicLink to create a + // symbolic link "\DosDevices\XXX". Usually, there + // is this understanding between related apps/drivers. + // + // An application might also peruse the DEVICEMAP + // section of the registry, or use the QueryDosDevice + // API to enumerate the existing symbolic links in the + // system. + // + + if ((GetVersion() & 0xFF) >= 5) + { + // + // We reference the global name so that the application can + // be executed in Terminal Services sessions on Win2K + // + wsprintf(completeDeviceName, TEXT("\\\\.\\Global\\%s"), DriverName); + } + else + { + wsprintf(completeDeviceName, TEXT("\\\\.\\%s"), DriverName); + } + + hDevice = CreateFile(completeDeviceName, + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL + ); + if (hDevice == ((HANDLE)-1)) + return FALSE; + + // If user wants handle, give it to them. Otherwise, just close it. + if (lphDevice) + *lphDevice = hDevice; + else + CloseHandle(hDevice); + + return TRUE; } @@ -158,19 +158,19 @@ BOOL OpenDevice(IN LPCTSTR DriverName, HANDLE* lphDevice) ****************************************************************************/ BOOL StopDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName) { - SC_HANDLE schService; - BOOL ret; - SERVICE_STATUS serviceStatus; + SC_HANDLE schService; + BOOL ret; + SERVICE_STATUS serviceStatus; - schService = OpenService(SchSCManager, DriverName, SERVICE_ALL_ACCESS); - if (schService == NULL) - return FALSE; + schService = OpenService(SchSCManager, DriverName, SERVICE_ALL_ACCESS); + if (schService == NULL) + return FALSE; - ret = ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus); + ret = ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus); - CloseServiceHandle(schService); + CloseServiceHandle(schService); - return ret; + return ret; } @@ -183,20 +183,20 @@ BOOL StopDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName) ****************************************************************************/ BOOL RemoveDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName) { - SC_HANDLE schService; - BOOL ret; + SC_HANDLE schService; + BOOL ret; - schService = OpenService(SchSCManager, - DriverName, - SERVICE_ALL_ACCESS - ); + schService = OpenService(SchSCManager, + DriverName, + SERVICE_ALL_ACCESS + ); - if (schService == NULL) - return FALSE; + if (schService == NULL) + return FALSE; - ret = DeleteService(schService); - CloseServiceHandle(schService); - return ret; + ret = DeleteService(schService); + CloseServiceHandle(schService); + return ret; } @@ -209,19 +209,19 @@ BOOL RemoveDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName) ****************************************************************************/ BOOL UnloadDeviceDriver(const TCHAR* Name) { - SC_HANDLE schSCManager; + SC_HANDLE schSCManager; - schSCManager = OpenSCManager(NULL, // machine (NULL == local) - NULL, // database (NULL == default) - SC_MANAGER_ALL_ACCESS // access required - ); + schSCManager = OpenSCManager(NULL, // machine (NULL == local) + NULL, // database (NULL == default) + SC_MANAGER_ALL_ACCESS // access required + ); - StopDriver(schSCManager, Name); - RemoveDriver(schSCManager, Name); + StopDriver(schSCManager, Name); + RemoveDriver(schSCManager, Name); - CloseServiceHandle(schSCManager); + CloseServiceHandle(schSCManager); - return TRUE; + return TRUE; } /**************************************************************************** @@ -235,24 +235,24 @@ BOOL UnloadDeviceDriver(const TCHAR* Name) BOOL LoadDeviceDriver(const TCHAR* Name, const TCHAR* Path, HANDLE* lphDevice, PDWORD Error) { - SC_HANDLE schSCManager; - BOOL okay; + SC_HANDLE schSCManager; + BOOL okay; - schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); + schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); - // Remove old instances - RemoveDriver(schSCManager, Name); + // Remove old instances + RemoveDriver(schSCManager, Name); - // Ignore success of installation: it may already be installed. - InstallDriver(schSCManager, Name, Path); + // Ignore success of installation: it may already be installed. + InstallDriver(schSCManager, Name, Path); - // Ignore success of start: it may already be started. - StartDriver(schSCManager, Name); + // Ignore success of start: it may already be started. + StartDriver(schSCManager, Name); - // Do make sure we can open it. - okay = OpenDevice(Name, lphDevice); - *Error = GetLastError(); - CloseServiceHandle(schSCManager); + // Do make sure we can open it. + okay = OpenDevice(Name, lphDevice); + *Error = GetLastError(); + CloseServiceHandle(schSCManager); - return okay; + return okay; } diff --git a/NotMyFault/exe/notmyfault.c b/NotMyFault/exe/notmyfault.c index 2510043..8c80d0b 100644 --- a/NotMyFault/exe/notmyfault.c +++ b/NotMyFault/exe/notmyfault.c @@ -30,24 +30,24 @@ COLORREF BsodBg = RGB(0xFF, 0, 0); //---------------------------------------------------------------------- LONG Abort(HWND hWnd, char* Msg, DWORD Error) { - LPVOID lpMsgBuf; - char errmsg[MAX_PATH * 2]; - DWORD error = GetLastError(); - - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, Error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR)&lpMsgBuf, 0, NULL); - UnloadDeviceDriver(SYS_NAME); - sprintf(errmsg, "%s: %s", Msg, lpMsgBuf); - if ((Error == ERROR_INVALID_HANDLE || Error == ERROR_ACCESS_DENIED || - Error == ERROR_FILE_NOT_FOUND)) - wsprintf(errmsg, "%s\nMake sure that you are an administrator and that NotMyFault is " - "not already running.", errmsg); - MessageBox(hWnd, errmsg, "NotMyFault", MB_OK | MB_ICONERROR); - PostQuitMessage(1); - LocalFree(lpMsgBuf); - return (DWORD)-1; + LPVOID lpMsgBuf; + char errmsg[MAX_PATH * 2]; + DWORD error = GetLastError(); + + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, Error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&lpMsgBuf, 0, NULL); + UnloadDeviceDriver(SYS_NAME); + sprintf(errmsg, "%s: %s", Msg, lpMsgBuf); + if ((Error == ERROR_INVALID_HANDLE || Error == ERROR_ACCESS_DENIED || + Error == ERROR_FILE_NOT_FOUND)) + wsprintf(errmsg, "%s\nMake sure that you are an administrator and that NotMyFault is " + "not already running.", errmsg); + MessageBox(hWnd, errmsg, "NotMyFault", MB_OK | MB_ICONERROR); + PostQuitMessage(1); + LocalFree(lpMsgBuf); + return (DWORD)-1; } @@ -60,17 +60,17 @@ LONG Abort(HWND hWnd, char* Msg, DWORD Error) //---------------------------------------------------------------------- VOID CenterWindow(HWND hDlg) { - RECT aRt; - - // center the dialog box - GetWindowRect(hDlg, &aRt); - OffsetRect(&aRt, -aRt.left, -aRt.top); - MoveWindow(hDlg, - ((GetSystemMetrics(SM_CXSCREEN) - - aRt.right) / 2 + 4) & ~7, - (GetSystemMetrics(SM_CYSCREEN) - - aRt.bottom) / 2, - aRt.right, aRt.bottom, 0); + RECT aRt; + + // center the dialog box + GetWindowRect(hDlg, &aRt); + OffsetRect(&aRt, -aRt.left, -aRt.top); + MoveWindow(hDlg, + ((GetSystemMetrics(SM_CXSCREEN) - + aRt.right) / 2 + 4) & ~7, + (GetSystemMetrics(SM_CYSCREEN) - + aRt.bottom) / 2, + aRt.right, aRt.bottom, 0); } //---------------------------------------------------------------------- @@ -80,64 +80,64 @@ VOID CenterWindow(HWND hDlg) //---------------------------------------------------------------------- UINT_PTR CALLBACK BsodColorsCallback(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam) { - static COLORREF newFg, newBg; - static UINT wm_colorOkString, wm_setRgbString; - HBRUSH hBack; - - switch (uiMsg) - { - case WM_INITDIALOG: - newFg = BsodFg; - newBg = BsodBg; - wm_colorOkString = RegisterWindowMessage(COLOROKSTRING); - wm_setRgbString = RegisterWindowMessage(SETRGBSTRING); - CheckRadioButton(hDlg, IDC_RADIOFG, IDC_RADIOBG, IDC_RADIOFG); - SendMessage(hDlg, wm_setRgbString, 0, newFg); - SetFocus(GetDlgItem(hDlg, IDC_DONE)); - break; - - case WM_CTLCOLORSTATIC: - if ((HWND)lParam == GetDlgItem(hDlg,IDC_PREVIEW)) - { - SetBkColor((HDC)wParam, newBg); - SetTextColor((HDC)wParam, newFg); - hBack = CreateSolidBrush(newBg); - return (BOOL)hBack; - } - break; - - case WM_COMMAND: - if (wParam == IDC_DONE) - { - BsodFg = newFg; - BsodBg = newBg; - PostMessage(hDlg, WM_COMMAND, IDABORT, 1); - return FALSE; - } - break; - - default: - if (uiMsg == wm_colorOkString) - { - CHOOSECOLOR* choose = (CHOOSECOLOR*)lParam; - if (IsDlgButtonChecked(hDlg, IDC_RADIOBG)) - { - newBg = choose->rgbResult; - InvalidateRect(GetDlgItem(hDlg,IDC_PREVIEW), NULL, TRUE); - //SendMessage( hDlg, wm_setRgbString, 0, newFg ); - return TRUE; - } - else - { - newFg = choose->rgbResult; - InvalidateRect(GetDlgItem(hDlg,IDC_PREVIEW), NULL, TRUE); - //SendMessage( hDlg, wm_setRgbString, 0, newBg ); - return TRUE; - } - } - break; - } - return 0; + static COLORREF newFg, newBg; + static UINT wm_colorOkString, wm_setRgbString; + HBRUSH hBack; + + switch (uiMsg) + { + case WM_INITDIALOG: + newFg = BsodFg; + newBg = BsodBg; + wm_colorOkString = RegisterWindowMessage(COLOROKSTRING); + wm_setRgbString = RegisterWindowMessage(SETRGBSTRING); + CheckRadioButton(hDlg, IDC_RADIOFG, IDC_RADIOBG, IDC_RADIOFG); + SendMessage(hDlg, wm_setRgbString, 0, newFg); + SetFocus(GetDlgItem(hDlg, IDC_DONE)); + break; + + case WM_CTLCOLORSTATIC: + if ((HWND)lParam == GetDlgItem(hDlg,IDC_PREVIEW)) + { + SetBkColor((HDC)wParam, newBg); + SetTextColor((HDC)wParam, newFg); + hBack = CreateSolidBrush(newBg); + return (BOOL)hBack; + } + break; + + case WM_COMMAND: + if (wParam == IDC_DONE) + { + BsodFg = newFg; + BsodBg = newBg; + PostMessage(hDlg, WM_COMMAND, IDABORT, 1); + return FALSE; + } + break; + + default: + if (uiMsg == wm_colorOkString) + { + CHOOSECOLOR* choose = (CHOOSECOLOR*)lParam; + if (IsDlgButtonChecked(hDlg, IDC_RADIOBG)) + { + newBg = choose->rgbResult; + InvalidateRect(GetDlgItem(hDlg,IDC_PREVIEW), NULL, TRUE); + //SendMessage( hDlg, wm_setRgbString, 0, newFg ); + return TRUE; + } + else + { + newFg = choose->rgbResult; + InvalidateRect(GetDlgItem(hDlg,IDC_PREVIEW), NULL, TRUE); + //SendMessage( hDlg, wm_setRgbString, 0, newBg ); + return TRUE; + } + } + break; + } + return 0; } @@ -150,62 +150,62 @@ UINT_PTR CALLBACK BsodColorsCallback(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARA //---------------------------------------------------------------------- LONG StartMyFaultDriver(HWND hDlg) { - char driverPath[MAX_PATH]; - char systemRoot[MAX_PATH]; - char path[MAX_PATH]; - WIN32_FIND_DATA findData; - HANDLE findHandle; - char* file; - DWORD error; - char msgbuf[MAX_PATH * 2]; - - // - // Load the myfault driver - // - GetCurrentDirectory(sizeof path, path); - sprintf(path + lstrlen(path), "\\%s", SYS_FILE); - - findHandle = FindFirstFile(path, &findData); - if (findHandle == INVALID_HANDLE_VALUE) - { - if (!SearchPath(NULL, SYS_FILE, NULL, sizeof(path), path, &file)) - { - sprintf(msgbuf, "%s was not found.", SYS_FILE); - return Abort(hDlg, msgbuf, GetLastError()); - } - } - else FindClose(findHandle); - - if (!GetEnvironmentVariable("SYSTEMROOT", systemRoot, sizeof(systemRoot))) - { - strcpy(msgbuf, "Could not resolve SYSTEMROOT environment variable"); - return Abort(hDlg, msgbuf, GetLastError()); - } - sprintf(driverPath, "%s\\system32\\drivers\\myfault.sys", systemRoot); - SetFileAttributes(driverPath, FILE_ATTRIBUTE_NORMAL); - CopyFile(path, driverPath, FALSE); - if (!LoadDeviceDriver(SYS_NAME, driverPath, &SysHandle, &error)) - { - if (!CopyFile(path, driverPath, FALSE)) - { - sprintf(msgbuf, "Unable to copy %s to %s\n\n" - "Make sure that %s is in the current directory.", - SYS_NAME, driverPath, SYS_FILE); - return Abort(hDlg, msgbuf, GetLastError()); - } - SetFileAttributes(driverPath, FILE_ATTRIBUTE_NORMAL); - if (!LoadDeviceDriver(SYS_NAME, driverPath, &SysHandle, &error)) - { - UnloadDeviceDriver(SYS_NAME); - if (!LoadDeviceDriver(SYS_NAME, driverPath, &SysHandle, &error)) - { - sprintf(msgbuf, "Error loading %s:", path); - DeleteFile(driverPath); - return Abort(hDlg, msgbuf, error); - } - } - } - return TRUE; + char driverPath[MAX_PATH]; + char systemRoot[MAX_PATH]; + char path[MAX_PATH]; + WIN32_FIND_DATA findData; + HANDLE findHandle; + char* file; + DWORD error; + char msgbuf[MAX_PATH * 2]; + + // + // Load the myfault driver + // + GetCurrentDirectory(sizeof path, path); + sprintf(path + lstrlen(path), "\\%s", SYS_FILE); + + findHandle = FindFirstFile(path, &findData); + if (findHandle == INVALID_HANDLE_VALUE) + { + if (!SearchPath(NULL, SYS_FILE, NULL, sizeof(path), path, &file)) + { + sprintf(msgbuf, "%s was not found.", SYS_FILE); + return Abort(hDlg, msgbuf, GetLastError()); + } + } + else FindClose(findHandle); + + if (!GetEnvironmentVariable("SYSTEMROOT", systemRoot, sizeof(systemRoot))) + { + strcpy(msgbuf, "Could not resolve SYSTEMROOT environment variable"); + return Abort(hDlg, msgbuf, GetLastError()); + } + sprintf(driverPath, "%s\\system32\\drivers\\myfault.sys", systemRoot); + SetFileAttributes(driverPath, FILE_ATTRIBUTE_NORMAL); + CopyFile(path, driverPath, FALSE); + if (!LoadDeviceDriver(SYS_NAME, driverPath, &SysHandle, &error)) + { + if (!CopyFile(path, driverPath, FALSE)) + { + sprintf(msgbuf, "Unable to copy %s to %s\n\n" + "Make sure that %s is in the current directory.", + SYS_NAME, driverPath, SYS_FILE); + return Abort(hDlg, msgbuf, GetLastError()); + } + SetFileAttributes(driverPath, FILE_ATTRIBUTE_NORMAL); + if (!LoadDeviceDriver(SYS_NAME, driverPath, &SysHandle, &error)) + { + UnloadDeviceDriver(SYS_NAME); + if (!LoadDeviceDriver(SYS_NAME, driverPath, &SysHandle, &error)) + { + sprintf(msgbuf, "Error loading %s:", path); + DeleteFile(driverPath); + return Abort(hDlg, msgbuf, error); + } + } + } + return TRUE; } //---------------------------------------------------------------------- @@ -215,8 +215,8 @@ LONG StartMyFaultDriver(HWND hDlg) //---------------------------------------------------------------------- void IoctlThreadProc(PVOID Context) { - DWORD nb; - DeviceIoControl(SysHandle, (DWORD)Context, NULL, 0, NULL, 0, &nb, NULL); + DWORD nb; + DeviceIoControl(SysHandle, (DWORD)Context, NULL, 0, NULL, 0, &nb, NULL); } @@ -227,45 +227,45 @@ void IoctlThreadProc(PVOID Context) //--------------------------------------------------------------------- void LeakPool(UINT PoolType, DWORD allocSize) { - DWORD maxAlloc, bytesAllocated, nb; - DWORD tickCount = GetTickCount(); - - maxAlloc = allocSize; - bytesAllocated = 0; - while (bytesAllocated < maxAlloc && tickCount - GetTickCount() < 1000) - { - if (!DeviceIoControl(SysHandle, - PoolType ? IOCTL_LEAK_NONPAGED : IOCTL_LEAK_PAGED, &allocSize, sizeof(allocSize), - NULL, 0, &nb, NULL)) - { - // can't even allocate 1 byte - if (allocSize == 1) break; - - allocSize /= 2; - if (allocSize == 0) allocSize = 1; - } - else - { - bytesAllocated += allocSize; - } - } - - // one more try going from 2 to 8192 - if (bytesAllocated < maxAlloc) - { - allocSize = 8192; - while (allocSize > 1 && bytesAllocated < maxAlloc - && tickCount - GetTickCount() < 1000) - { - while (DeviceIoControl(SysHandle, - PoolType ? IOCTL_LEAK_NONPAGED : IOCTL_LEAK_PAGED, &allocSize, sizeof(allocSize), - NULL, 0, &nb, NULL) && bytesAllocated < maxAlloc) - { - bytesAllocated += allocSize; - } - allocSize /= 2; - } - } + DWORD maxAlloc, bytesAllocated, nb; + DWORD tickCount = GetTickCount(); + + maxAlloc = allocSize; + bytesAllocated = 0; + while (bytesAllocated < maxAlloc && tickCount - GetTickCount() < 1000) + { + if (!DeviceIoControl(SysHandle, + PoolType ? IOCTL_LEAK_NONPAGED : IOCTL_LEAK_PAGED, &allocSize, sizeof(allocSize), + NULL, 0, &nb, NULL)) + { + // can't even allocate 1 byte + if (allocSize == 1) break; + + allocSize /= 2; + if (allocSize == 0) allocSize = 1; + } + else + { + bytesAllocated += allocSize; + } + } + + // one more try going from 2 to 8192 + if (bytesAllocated < maxAlloc) + { + allocSize = 8192; + while (allocSize > 1 && bytesAllocated < maxAlloc + && tickCount - GetTickCount() < 1000) + { + while (DeviceIoControl(SysHandle, + PoolType ? IOCTL_LEAK_NONPAGED : IOCTL_LEAK_PAGED, &allocSize, sizeof(allocSize), + NULL, 0, &nb, NULL) && bytesAllocated < maxAlloc) + { + bytesAllocated += allocSize; + } + allocSize /= 2; + } + } } @@ -279,178 +279,178 @@ void LeakPool(UINT PoolType, DWORD allocSize) LRESULT APIENTRY MainDialog(HWND hDlg, UINT message, UINT wParam, LONG lParam) { - char label[MAX_PATH]; - SYSTEM_INFO sysInfo; - DWORD i, nb, ioctl; - DWORD allocSize, maxAlloc; - static BOOLEAN leakPaged = FALSE; - static BOOLEAN leakNonpaged = FALSE; - CHOOSECOLOR colorArgs; - static DWORD rgbCurrent; - static COLORREF acrCustClr[16]; - - switch (message) - { - case WM_INITDIALOG: - - // - // Start driver - // - if (!StartMyFaultDriver(hDlg)) - { - return FALSE; - } - - // - // We can delete the driver and its Registry key now that its loaded - // - CheckDlgButton(hDlg, IDC_IRQL, BST_CHECKED); - CenterWindow(hDlg); - SetDlgItemText(hDlg, IDC_LEAKMB, "1000"); - break; - - case WM_TIMER: - - GetDlgItemText(hDlg, IDC_LEAKMB, label, _countof(label)); - allocSize = maxAlloc = (atoi(label) * 1024); - LeakPool(wParam, allocSize); - break; - - case WM_COMMAND: - switch (LOWORD(wParam)) - { - case IDOK: - - if (IsDlgButtonChecked(hDlg, IDC_BUFFEROVERFLOW) == BST_CHECKED) - { - ioctl = IOCTL_BUFFER_OVERFLOW; - } - else if (IsDlgButtonChecked(hDlg, IDC_WILDPOINTER) == BST_CHECKED) - { - ioctl = IOCTL_WILD_POINTER; - } - else if (IsDlgButtonChecked(hDlg, IDC_DEADLOCK) == BST_CHECKED) - { - ioctl = IOCTL_DEADLOCK; - } - else if (IsDlgButtonChecked(hDlg, IDC_HANG) == BST_CHECKED) - { - ioctl = IOCTL_HANG; - } - else if (IsDlgButtonChecked(hDlg, IDC_STACKTRASH) == BST_CHECKED) - { - ioctl = IOCTL_TRASH_STACK; - } - else if (IsDlgButtonChecked(hDlg, IDC_PAGEFAULT) == BST_CHECKED) - { - ioctl = IOCTL_PAGE_FAULT; - } - else if (IsDlgButtonChecked(hDlg, IDC_IRQL) == BST_CHECKED) - { - ioctl = IOCTL_IRQL; - } - else if (IsDlgButtonChecked(hDlg, IDC_HANGIRP) == BST_CHECKED) - { - _beginthread(IoctlThreadProc, 0, (PVOID)IOCTL_HANG_IRP); - break; - } - - // - // Execute hang and deadlock on each CPU - // - if (ioctl == IOCTL_HANG || ioctl == IOCTL_DEADLOCK) - { - GetSystemInfo(&sysInfo); - for (i = 0; i < sysInfo.dwNumberOfProcessors; i++) - { - DeviceIoControl(SysHandle, ioctl, NULL, 0, NULL, 0, &nb, NULL); - } - } - else - { - DeviceIoControl(SysHandle, ioctl, NULL, 0, NULL, 0, &nb, NULL); - } - break; - - case IDC_LEAK_PAGE: - - if (leakPaged) - { - KillTimer(hDlg, 0); - SetDlgItemText(hDlg, IDC_LEAK_PAGE, "Leak &Paged"); - } - else - { - SetTimer(hDlg, 0, 1000, NULL); - SetDlgItemText(hDlg, IDC_LEAK_PAGE, "Stop &Paged"); - } - leakPaged = !leakPaged; - break; - - case IDC_LEAK_NONPAGE: - - if (leakNonpaged) - { - KillTimer(hDlg, 1); - SetDlgItemText(hDlg, IDC_LEAK_NONPAGE, "Leak &Nonpaged"); - } - else - { - SetTimer(hDlg, 1, 1000, NULL); - SetDlgItemText(hDlg, IDC_LEAK_NONPAGE, "Stop &Nonpaged"); - } - leakNonpaged = !leakNonpaged; - break; - - case IDCOLOR: - { - COLORREF CustomColors[16]; - int i; - for (i = 0; i < 16; i++) - { - CustomColors[i] = RGB(255, 255, 255); - } - colorArgs.lStructSize = sizeof colorArgs; - colorArgs.Flags = CC_RGBINIT | CC_ENABLEHOOK | CC_ENABLETEMPLATE | CC_FULLOPEN; - colorArgs.hwndOwner = hDlg; - colorArgs.hInstance = (HWND)GetModuleHandle(NULL); - colorArgs.rgbResult = RGB(0, 0, 0); - colorArgs.lpCustColors = CustomColors; - colorArgs.lCustData = 0; - colorArgs.rgbResult = BsodFg; - colorArgs.lpTemplateName = "BSODCOLORS"; - colorArgs.lpfnHook = BsodColorsCallback; - if (ChooseColor(&colorArgs) == TRUE) - { - LARGE_INTEGER Color; - Color.LowPart = RGB(GetRValue(BsodBg)/4, - GetGValue(BsodBg)/4, - GetBValue(BsodBg)/4); - Color.HighPart = RGB(GetRValue(BsodFg)/4, - GetGValue(BsodFg)/4, - GetBValue(BsodFg)/4); - DeviceIoControl(SysHandle, IOCTL_BSOD_COLOR, &Color, sizeof(LARGE_INTEGER), NULL, 0, &nb, NULL); - } - } - break; - - case IDCANCEL: - - // - // Cancel - // - EndDialog(hDlg, 0); - PostQuitMessage(0); - break ; - } - break; - - case WM_CLOSE: - EndDialog(hDlg, 0); - PostQuitMessage(0); - break; - } - return DefWindowProc(hDlg, message, wParam, lParam); + char label[MAX_PATH]; + SYSTEM_INFO sysInfo; + DWORD i, nb, ioctl; + DWORD allocSize, maxAlloc; + static BOOLEAN leakPaged = FALSE; + static BOOLEAN leakNonpaged = FALSE; + CHOOSECOLOR colorArgs; + static DWORD rgbCurrent; + static COLORREF acrCustClr[16]; + + switch (message) + { + case WM_INITDIALOG: + + // + // Start driver + // + if (!StartMyFaultDriver(hDlg)) + { + return FALSE; + } + + // + // We can delete the driver and its Registry key now that its loaded + // + CheckDlgButton(hDlg, IDC_IRQL, BST_CHECKED); + CenterWindow(hDlg); + SetDlgItemText(hDlg, IDC_LEAKMB, "1000"); + break; + + case WM_TIMER: + + GetDlgItemText(hDlg, IDC_LEAKMB, label, _countof(label)); + allocSize = maxAlloc = (atoi(label) * 1024); + LeakPool(wParam, allocSize); + break; + + case WM_COMMAND: + switch (LOWORD(wParam)) + { + case IDOK: + + if (IsDlgButtonChecked(hDlg, IDC_BUFFEROVERFLOW) == BST_CHECKED) + { + ioctl = IOCTL_BUFFER_OVERFLOW; + } + else if (IsDlgButtonChecked(hDlg, IDC_WILDPOINTER) == BST_CHECKED) + { + ioctl = IOCTL_WILD_POINTER; + } + else if (IsDlgButtonChecked(hDlg, IDC_DEADLOCK) == BST_CHECKED) + { + ioctl = IOCTL_DEADLOCK; + } + else if (IsDlgButtonChecked(hDlg, IDC_HANG) == BST_CHECKED) + { + ioctl = IOCTL_HANG; + } + else if (IsDlgButtonChecked(hDlg, IDC_STACKTRASH) == BST_CHECKED) + { + ioctl = IOCTL_TRASH_STACK; + } + else if (IsDlgButtonChecked(hDlg, IDC_PAGEFAULT) == BST_CHECKED) + { + ioctl = IOCTL_PAGE_FAULT; + } + else if (IsDlgButtonChecked(hDlg, IDC_IRQL) == BST_CHECKED) + { + ioctl = IOCTL_IRQL; + } + else if (IsDlgButtonChecked(hDlg, IDC_HANGIRP) == BST_CHECKED) + { + _beginthread(IoctlThreadProc, 0, (PVOID)IOCTL_HANG_IRP); + break; + } + + // + // Execute hang and deadlock on each CPU + // + if (ioctl == IOCTL_HANG || ioctl == IOCTL_DEADLOCK) + { + GetSystemInfo(&sysInfo); + for (i = 0; i < sysInfo.dwNumberOfProcessors; i++) + { + DeviceIoControl(SysHandle, ioctl, NULL, 0, NULL, 0, &nb, NULL); + } + } + else + { + DeviceIoControl(SysHandle, ioctl, NULL, 0, NULL, 0, &nb, NULL); + } + break; + + case IDC_LEAK_PAGE: + + if (leakPaged) + { + KillTimer(hDlg, 0); + SetDlgItemText(hDlg, IDC_LEAK_PAGE, "Leak &Paged"); + } + else + { + SetTimer(hDlg, 0, 1000, NULL); + SetDlgItemText(hDlg, IDC_LEAK_PAGE, "Stop &Paged"); + } + leakPaged = !leakPaged; + break; + + case IDC_LEAK_NONPAGE: + + if (leakNonpaged) + { + KillTimer(hDlg, 1); + SetDlgItemText(hDlg, IDC_LEAK_NONPAGE, "Leak &Nonpaged"); + } + else + { + SetTimer(hDlg, 1, 1000, NULL); + SetDlgItemText(hDlg, IDC_LEAK_NONPAGE, "Stop &Nonpaged"); + } + leakNonpaged = !leakNonpaged; + break; + + case IDCOLOR: + { + COLORREF CustomColors[16]; + int i; + for (i = 0; i < 16; i++) + { + CustomColors[i] = RGB(255, 255, 255); + } + colorArgs.lStructSize = sizeof colorArgs; + colorArgs.Flags = CC_RGBINIT | CC_ENABLEHOOK | CC_ENABLETEMPLATE | CC_FULLOPEN; + colorArgs.hwndOwner = hDlg; + colorArgs.hInstance = (HWND)GetModuleHandle(NULL); + colorArgs.rgbResult = RGB(0, 0, 0); + colorArgs.lpCustColors = CustomColors; + colorArgs.lCustData = 0; + colorArgs.rgbResult = BsodFg; + colorArgs.lpTemplateName = "BSODCOLORS"; + colorArgs.lpfnHook = BsodColorsCallback; + if (ChooseColor(&colorArgs) == TRUE) + { + LARGE_INTEGER Color; + Color.LowPart = RGB(GetRValue(BsodBg)/4, + GetGValue(BsodBg)/4, + GetBValue(BsodBg)/4); + Color.HighPart = RGB(GetRValue(BsodFg)/4, + GetGValue(BsodFg)/4, + GetBValue(BsodFg)/4); + DeviceIoControl(SysHandle, IOCTL_BSOD_COLOR, &Color, sizeof(LARGE_INTEGER), NULL, 0, &nb, NULL); + } + } + break; + + case IDCANCEL: + + // + // Cancel + // + EndDialog(hDlg, 0); + PostQuitMessage(0); + break ; + } + break; + + case WM_CLOSE: + EndDialog(hDlg, 0); + PostQuitMessage(0); + break; + } + return DefWindowProc(hDlg, message, wParam, lParam); } @@ -466,66 +466,66 @@ int WINAPI WinMain(HINSTANCE hInstance, LPSTR lpCmdLine, int nCmdShow) { - static TCHAR szAppName[] = TEXT("NOTMYFAULT"); - MSG msg; - HWND hMainDlg; - WNDCLASSEX wndclass; - PWSTR* cmdLine; - int numArgs, i; - DWORD nb; - - cmdLine = CommandLineToArgvW(GetCommandLineW(), &numArgs); - for (i = 0; i < numArgs; i++) - { - if (cmdLine[i][0] == '/' || - cmdLine[i][0] == '-') - { - if (!_wcsicmp(&cmdLine[i][1], L"crash")) - { - if (StartMyFaultDriver(NULL)) - { - DeviceIoControl(SysHandle, IOCTL_IRQL, NULL, 0, NULL, 0, &nb, NULL); - } - } - else - { - MessageBox(NULL, "Usage: notmyfault [/crash]\n" - "/crash Crashes the system.", "NotMyFault", MB_ICONERROR); - return -1; - } - } - } - - // - // Create the main window class - // - wndclass.cbSize = sizeof(WNDCLASSEX); - wndclass.style = CS_HREDRAW | CS_VREDRAW; - wndclass.lpfnWndProc = (WNDPROC)MainDialog; - wndclass.cbClsExtra = 0; - wndclass.cbWndExtra = DLGWINDOWEXTRA; - wndclass.hInstance = hInstance; - wndclass.hIcon = LoadIcon(hInstance, "APPICON"); - wndclass.hIconSm = LoadIcon(hInstance, "APPICON"); - wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); - wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); - wndclass.lpszMenuName = NULL; - wndclass.lpszClassName = szAppName; - RegisterClassEx(&wndclass); - - // - // Create the dialog - // - hMainDlg = CreateDialog(hInstance, "NOTMYFAULT", NULL, (DLGPROC)MainDialog); - ShowWindow(hMainDlg, nCmdShow); - - while (GetMessage(&msg, NULL, 0, 0)) - { - if (!IsDialogMessage(hMainDlg, &msg)) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } - return (int)msg.wParam; + static TCHAR szAppName[] = TEXT("NOTMYFAULT"); + MSG msg; + HWND hMainDlg; + WNDCLASSEX wndclass; + PWSTR* cmdLine; + int numArgs, i; + DWORD nb; + + cmdLine = CommandLineToArgvW(GetCommandLineW(), &numArgs); + for (i = 0; i < numArgs; i++) + { + if (cmdLine[i][0] == '/' || + cmdLine[i][0] == '-') + { + if (!_wcsicmp(&cmdLine[i][1], L"crash")) + { + if (StartMyFaultDriver(NULL)) + { + DeviceIoControl(SysHandle, IOCTL_IRQL, NULL, 0, NULL, 0, &nb, NULL); + } + } + else + { + MessageBox(NULL, "Usage: notmyfault [/crash]\n" + "/crash Crashes the system.", "NotMyFault", MB_ICONERROR); + return -1; + } + } + } + + // + // Create the main window class + // + wndclass.cbSize = sizeof(WNDCLASSEX); + wndclass.style = CS_HREDRAW | CS_VREDRAW; + wndclass.lpfnWndProc = (WNDPROC)MainDialog; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = DLGWINDOWEXTRA; + wndclass.hInstance = hInstance; + wndclass.hIcon = LoadIcon(hInstance, "APPICON"); + wndclass.hIconSm = LoadIcon(hInstance, "APPICON"); + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); + wndclass.lpszMenuName = NULL; + wndclass.lpszClassName = szAppName; + RegisterClassEx(&wndclass); + + // + // Create the dialog + // + hMainDlg = CreateDialog(hInstance, "NOTMYFAULT", NULL, (DLGPROC)MainDialog); + ShowWindow(hMainDlg, nCmdShow); + + while (GetMessage(&msg, NULL, 0, 0)) + { + if (!IsDialogMessage(hMainDlg, &msg)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + return (int)msg.wParam; }