[33] | 1 | //////////////////////////////////////////////////////////// |
---|
| 2 | // Copyright (C) Roman Ryltsov, 2008-2011 |
---|
| 3 | // Created by Roman Ryltsov roman@alax.info |
---|
| 4 | // |
---|
| 5 | // $Id: VirtualHeapPtr.cpp 36 2011-11-05 20:45:20Z roman $ |
---|
| 6 | |
---|
| 7 | #include "stdafx.h" |
---|
[34] | 8 | #include "VirtualHeapPtr.h" |
---|
[33] | 9 | |
---|
| 10 | //////////////////////////////////////////////////////////// |
---|
| 11 | // Main |
---|
| 12 | |
---|
| 13 | int _tmain(int argc, _TCHAR* argv[]) |
---|
| 14 | { |
---|
[34] | 15 | _tprintf(_T("g_GlobalVirtualAllocator.GetPageSize() %d\n\n"), g_GlobalVirtualAllocator.GetPageSize()); |
---|
| 16 | CDebugHeapPtr<BYTE> p; |
---|
[33] | 17 | p.Allocate(1); |
---|
| 18 | p[0] = 0x01; |
---|
| 19 | _tprintf(_T("p[0] 0x%02X\n"), p[0]); |
---|
| 20 | ATLVERIFY(p.Reallocate(2)); |
---|
| 21 | p[1] = 0x02; |
---|
| 22 | _tprintf(_T("p[0] 0x%02X, p[1] 0x%02X\n"), p[0], p[1]); |
---|
| 23 | p[0] = 0x03; |
---|
| 24 | _tprintf(_T("p[0] 0x%02X, p[1] 0x%02X\n"), p[0], p[1]); |
---|
[34] | 25 | #pragma region PAGE_READONLY |
---|
| 26 | //if(FALSE) |
---|
[33] | 27 | { |
---|
| 28 | p.SetProtection(PAGE_READONLY); |
---|
| 29 | _ATLTRY |
---|
| 30 | { |
---|
| 31 | p[1] = 0x04; |
---|
| 32 | _tprintf(_T("p[0] 0x%02X, p[1] 0x%02X\n"), p[0], p[1]); |
---|
| 33 | } |
---|
| 34 | _ATLCATCHALL() |
---|
| 35 | { |
---|
[34] | 36 | _tprintf(_T("Oopsie in line %d (Failed to write to PAGE_READONLY memory)\n"), __LINE__); |
---|
[33] | 37 | } |
---|
| 38 | p.SetProtection(PAGE_READWRITE); |
---|
| 39 | p[1] = 0x05; |
---|
| 40 | _tprintf(_T("p[0] 0x%02X, p[1] 0x%02X\n"), p[0], p[1]); |
---|
| 41 | } |
---|
[34] | 42 | #pragma endregion |
---|
| 43 | volatile BYTE n; |
---|
[33] | 44 | p.SetProtection(PAGE_READWRITE | PAGE_GUARD); |
---|
[34] | 45 | #pragma region PAGE_GUARD |
---|
[33] | 46 | _ATLTRY |
---|
| 47 | { |
---|
| 48 | n = p[0]; |
---|
| 49 | } |
---|
| 50 | _ATLCATCHALL() |
---|
| 51 | { |
---|
[34] | 52 | _tprintf(_T("Oopsie in line %d (First PAGE_GUARD access)\n"), __LINE__); |
---|
[33] | 53 | } |
---|
| 54 | _ATLTRY |
---|
| 55 | { |
---|
| 56 | n = p[1]; |
---|
| 57 | } |
---|
| 58 | _ATLCATCHALL() |
---|
| 59 | { |
---|
[34] | 60 | _tprintf(_T("Oopsie in line %d (Second PAGE_GUARD access)\n"), __LINE__); |
---|
[33] | 61 | } |
---|
[34] | 62 | #pragma endregion |
---|
| 63 | #pragma region Invalid Index |
---|
| 64 | _ATLTRY |
---|
| 65 | { |
---|
| 66 | n = p[-1]; |
---|
| 67 | } |
---|
| 68 | _ATLCATCHALL() |
---|
| 69 | { |
---|
| 70 | _tprintf(_T("Oopsie in line %d (Reading with a too small index)\n"), __LINE__); |
---|
| 71 | } |
---|
| 72 | _ATLTRY |
---|
| 73 | { |
---|
[36] | 74 | p[-1] = n + 1; |
---|
[34] | 75 | } |
---|
| 76 | _ATLCATCHALL() |
---|
| 77 | { |
---|
| 78 | _tprintf(_T("Oopsie in line %d (Writing with a too small index)\n"), __LINE__); |
---|
| 79 | } |
---|
| 80 | _ATLTRY |
---|
| 81 | { |
---|
| 82 | n = p[2]; |
---|
| 83 | } |
---|
| 84 | _ATLCATCHALL() |
---|
| 85 | { |
---|
| 86 | _tprintf(_T("Oopsie in line %d (Reading with a too large index)\n"), __LINE__); |
---|
| 87 | } |
---|
| 88 | _ATLTRY |
---|
| 89 | { |
---|
[36] | 90 | p[2] = n + 1; |
---|
[34] | 91 | } |
---|
| 92 | _ATLCATCHALL() |
---|
| 93 | { |
---|
| 94 | _tprintf(_T("Oopsie in line %d (Writing with a too large index)\n"), __LINE__); |
---|
| 95 | } |
---|
| 96 | #pragma endregion |
---|
[33] | 97 | return 0; |
---|
| 98 | } |
---|
| 99 | |
---|