Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (c) 2011 Cristian Adam
- #include <iostream>
- #include <ctime>
- #include <vector>
- #include <windows.h>
- #include "APIHook.h"
- bool showCrashDialog = false;
- BOOL WINAPI OurHeapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem);
- typedef BOOL (WINAPI *OURHEAPFREEPROC)(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem);
- CAPIHook g_heapFree("kernel32.dll", "HeapFree", (PROC)OurHeapFree);
- BOOL WINAPI OurHeapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem)
- {
- BOOL result = ((OURHEAPFREEPROC)(PROC)*g_heapFree)(hHeap, dwFlags, lpMem);
- if (!result)
- {
- std::cout << __FUNCTION__ << " Error: 0x" << std::hex << ::GetLastError() << std::endl;
- throw 0;
- }
- return result;
- }
- LONG WINAPI RedirectedSetUnhandledExceptionFilter(EXCEPTION_POINTERS * /*ExceptionInfo*/)
- {
- // When the CRT calls SetUnhandledExceptionFilter with NULL parameter
- // our handler will not get removed.
- return 0;
- }
- LONG WINAPI OurSetUnhandledExceptionFilter(EXCEPTION_POINTERS * /*ExceptionInfo*/)
- {
- std::cout << "Gotcha!" << std::endl;
- return showCrashDialog ? EXCEPTION_CONTINUE_SEARCH : EXCEPTION_EXECUTE_HANDLER;
- }
- void MemoryAccessCrash()
- {
- std::cout << "Normal null pointer crash" << std::endl;
- char *p = 0;
- *p = 5;
- }
- void OutOfBoundsVectorCrash()
- {
- std::cout << "std::vector out of bounds crash!" << std::endl;
- std::vector<int> v;
- v[0] = 5;
- }
- void AbortCrash()
- {
- std::cout << "Calling Abort" << std::endl;
- abort();
- }
- void VirtualFunctionCallCrash()
- {
- struct B
- {
- B()
- {
- std::cout << "Pure Virtual Function Call crash!" << std::endl;
- Bar();
- }
- virtual void Foo() = 0;
- void Bar()
- {
- Foo();
- }
- };
- struct D: public B
- {
- void Foo()
- {
- }
- };
- B* b = new D;
- // Just to silence the warning C4101: 'VirtualFunctionCallCrash::B::Foo' : unreferenced local variable
- b->Foo();
- }
- void DoubleDeleteCrash()
- {
- std::cout << "Double delete" << std::endl;
- char * buf = new char [10000];
- delete [] buf;
- delete [] buf;
- }
- int main()
- {
- ::SetUnhandledExceptionFilter(OurSetUnhandledExceptionFilter);
- CAPIHook apiHook("kernel32.dll", "SetUnhandledExceptionFilter", (PROC)RedirectedSetUnhandledExceptionFilter);
- srand(static_cast<unsigned int>(std::time(0)));
- //int option = rand() % 5;
- int option = 4;
- switch (option)
- {
- case 0:
- MemoryAccessCrash();
- break;
- case 1:
- OutOfBoundsVectorCrash();
- break;
- case 2:
- AbortCrash();
- break;
- case 3:
- VirtualFunctionCallCrash();
- break;
- case 4:
- DoubleDeleteCrash();
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement