| 6 |
| 7 | #include <stdlib.h> |
| 8 | #include <windows.h> |
| 9 |
| 10 | #include "base/logging.h" |
| 11 | #include "base/win/windows_version.h" |
| 12 |
| 13 | namespace base { |
| 14 | namespace debug { |
| 15 | namespace win { |
| 16 |
| 17 | namespace { |
| 18 |
| 19 | void CreateSyntheticHeapCorruption() { |
| 20 | EXCEPTION_RECORD record = {}; |
| 21 | record.ExceptionCode = STATUS_HEAP_CORRUPTION; |
| 22 | RaiseFailFastException(&record, nullptr, |
| 23 | FAIL_FAST_GENERATE_EXCEPTION_ADDRESS); |
| 24 | } |
| 25 |
| 26 | } // namespace |
| 27 |
| 28 | void TerminateWithHeapCorruption() { |
| 29 | __try { |
| 30 | // Pre-Windows 10, it's hard to trigger a heap corruption fast fail, so |
| 31 | // artificially create one instead. |
| 32 | if (base::win::GetVersion() < base::win::Version::WIN10) |
| 33 | CreateSyntheticHeapCorruption(); |
| 34 | HANDLE heap = ::HeapCreate(0, 0, 0); |
| 35 | CHECK(heap); |
| 36 | CHECK(HeapSetInformation(heap, HeapEnableTerminationOnCorruption, nullptr, |
| 37 | 0)); |
| 38 | void* addr = ::HeapAlloc(heap, 0, 0x1000); |
| 39 | CHECK(addr); |
| 40 | // Corrupt heap header. |
| 41 | char* addr_mutable = reinterpret_cast<char*>(addr); |
| 42 | memset(addr_mutable - sizeof(addr), 0xCC, sizeof(addr)); |
| 43 |
| 44 | HeapFree(heap, 0, addr); |
| 45 | HeapDestroy(heap); |
| 46 | } __except (EXCEPTION_EXECUTE_HANDLER) { |
| 47 | // Heap corruption exception should never be caught. |
| 48 | CHECK(false); |
| 49 | } |
| 50 | // Should never reach here. |
| 51 | abort(); |
| 52 | } |
| 53 |
| 54 | } // namespace win |
| 55 | } // namespace debug |
| 56 | } // namespace base |
| 57 |