source: trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.cpp @ 37

Last change on this file since 37 was 36, checked in by roman, 12 years ago
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
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"
8#include "VirtualHeapPtr.h"
9
10////////////////////////////////////////////////////////////
11// Main
12
13int _tmain(int argc, _TCHAR* argv[])
14{
15        _tprintf(_T("g_GlobalVirtualAllocator.GetPageSize() %d\n\n"), g_GlobalVirtualAllocator.GetPageSize());
16        CDebugHeapPtr<BYTE> p;
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]);
25        #pragma region PAGE_READONLY
26        //if(FALSE)
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                {
36                        _tprintf(_T("Oopsie in line %d (Failed to write to PAGE_READONLY memory)\n"), __LINE__);
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        }
42        #pragma endregion
43        volatile BYTE n;
44        p.SetProtection(PAGE_READWRITE | PAGE_GUARD);
45        #pragma region PAGE_GUARD
46        _ATLTRY
47        {
48                n = p[0];
49        }
50        _ATLCATCHALL()
51        {
52                _tprintf(_T("Oopsie in line %d (First PAGE_GUARD access)\n"), __LINE__);
53        }
54        _ATLTRY
55        {
56                n = p[1];
57        }
58        _ATLCATCHALL()
59        {
60                _tprintf(_T("Oopsie in line %d (Second PAGE_GUARD access)\n"), __LINE__);
61        }
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        {
74                p[-1] = n + 1;
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        {
90                p[2] = n + 1;
91        }
92        _ATLCATCHALL()
93        {
94                _tprintf(_T("Oopsie in line %d (Writing with a too large index)\n"), __LINE__);
95        }
96        #pragma endregion
97        return 0;
98}
99
Note: See TracBrowser for help on using the repository browser.