Changeset 34


Ignore:
Timestamp:
Nov 5, 2011, 12:19:24 PM (12 years ago)
Author:
roman
Message:

CDebugPtr with additional descriptor and inaccessible sanity pages

Location:
trunk/Utilities/VirtualHeapPtr
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Utilities/VirtualHeapPtr

    • Property svn:ignore
      •  

        old new  
        44*.suo
        55*.user
         6*.opensdf
  • trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.cpp

    r33 r34  
    66
    77#include "stdafx.h"
    8 #include <psapi.h>
    9 
    10 #pragma comment(lib, "psapi.lib")
    11 
    12 ////////////////////////////////////////////////////////////
    13 // CGlobalVirtualAllocator
    14 
    15 class CGlobalVirtualAllocator
    16 {
    17 private:
    18         SIZE_T m_nGranularity;
    19 
    20 public:
    21 // CGlobalVirtualAllocator
    22         CGlobalVirtualAllocator() throw() :
    23                 m_nGranularity(4 << 10) // 4L
    24         {
    25                 PERFORMANCE_INFORMATION Information;
    26                 ZeroMemory(&Information, sizeof Information);
    27                 ATLVERIFY(GetPerformanceInfo(&Information, sizeof Information));
    28                 m_nGranularity = Information.PageSize;
    29                 ATLASSERT(m_nGranularity);
    30                 ATLASSERT(!(m_nGranularity & (m_nGranularity - 1)));
    31         }
    32         SIZE_T GetGranularity() const throw()
    33         {
    34                 return m_nGranularity;
    35         }
    36 };
    37 
    38 CGlobalVirtualAllocator g_GlobalVirtualAllocator;
    39 
    40 ////////////////////////////////////////////////////////////
    41 // CVirtualAllocator
    42 
    43 class CVirtualAllocator
    44 {
    45 public:
    46 // CVirtualAllocator
    47         static SIZE_T Align(SIZE_T nDataSize) throw()
    48         {
    49                 const SIZE_T nGranularity = g_GlobalVirtualAllocator.GetGranularity();
    50                 ATLASSERT(nGranularity);
    51                 ATLASSERT(!(nGranularity & (nGranularity - 1)));
    52                 return (nDataSize + nGranularity - 1) & ~(nGranularity - 1);
    53         }
    54         static VOID* Reallocate(_In_opt_ VOID* pvData, _In_ SIZE_T nDataSize)
    55         {
    56                 MEMORY_BASIC_INFORMATION DataInformation;
    57                 if(pvData)
    58                 {
    59                         ATLVERIFY(VirtualQuery(pvData, &DataInformation, sizeof DataInformation));
    60                         if(nDataSize <= DataInformation.RegionSize)
    61                                 return pvData;
    62                 }
    63                 VOID* pvNewData = NULL;
    64                 if(nDataSize)
    65                 {
    66                         pvNewData = VirtualAlloc(NULL, Align(nDataSize), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    67                         ATLENSURE_THROW(pvNewData, AtlHresultFromLastError());
    68                         _ATLTRY
    69                         {
    70                                 ATLENSURE_THROW(VirtualLock(pvNewData, nDataSize), AtlHresultFromLastError());
    71                                 if(pvNewData && pvData)
    72                                 {
    73                                         ATLASSERT(DataInformation.AllocationProtect == PAGE_READWRITE);
    74                                         SIZE_T nCopyDataSize = nDataSize;
    75                                         if(nCopyDataSize > DataInformation.RegionSize)
    76                                                 nCopyDataSize = DataInformation.RegionSize;
    77                                         Checked::memcpy_s(pvNewData, nDataSize, pvData, nCopyDataSize);
    78                                 }
    79                         }
    80                         _ATLCATCHALL()
    81                         {
    82                                 Free(pvData);
    83                                 _ATLRETHROW;
    84                         }
    85                 }
    86                 if(pvData)
    87                         Free(pvData);
    88                 return pvNewData;
    89         }
    90         static VOID* Allocate(_In_ SIZE_T nDataSize)
    91         {
    92                 if(!nDataSize)
    93                         return NULL;
    94                 VOID* pvData = VirtualAlloc(NULL, Align(nDataSize), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    95                 ATLENSURE_THROW(pvData, AtlHresultFromLastError());
    96                 _ATLTRY
    97                 {
    98                         ATLENSURE_THROW(VirtualLock(pvData, nDataSize), AtlHresultFromLastError());
    99                 }
    100                 _ATLCATCHALL()
    101                 {
    102                         Free(pvData);
    103                         _ATLRETHROW;
    104                 }
    105                 return pvData;
    106         }
    107         static VOID Free(_In_opt_ VOID* pvData) throw()
    108         {
    109                 if(!pvData)
    110                         return;
    111                 //VirtualUnlock(pvData,
    112                 ATLVERIFY(VirtualFree(pvData, 0, MEM_RELEASE));
    113         }
    114 };
    115 
    116 ////////////////////////////////////////////////////////////
    117 // CVirtualHeapPtr
    118 
    119 template <typename T>
    120 class CVirtualHeapPtr :
    121         public CHeapPtr<T, CVirtualAllocator>
    122 {
    123 public:
    124 // CVirtualHeapPtr
    125         CVirtualHeapPtr() throw()
    126         {
    127         }
    128         explicit CVirtualHeapPtr(_In_ T* pData) throw() :
    129                 CHeapPtr<T, CVirtualAllocator>(pData)
    130         {
    131         }
    132         VOID SetProtection(DWORD nProtection)
    133         {
    134                 if(!m_pData)
    135                         return;
    136                 MEMORY_BASIC_INFORMATION DataInformation;
    137                 ATLENSURE_THROW(VirtualQuery(m_pData, &DataInformation, sizeof DataInformation), AtlHresultFromLastError());
    138                 DWORD nCurrentProtection;
    139                 ATLENSURE_THROW(VirtualProtect(m_pData, DataInformation.RegionSize, nProtection, &nCurrentProtection), AtlHresultFromLastError());
    140         }
    141 };
     8#include "VirtualHeapPtr.h"
    1429
    14310////////////////////////////////////////////////////////////
     
    14613int _tmain(int argc, _TCHAR* argv[])
    14714{
    148         CVirtualHeapPtr<BYTE> p;
     15        _tprintf(_T("g_GlobalVirtualAllocator.GetPageSize() %d\n\n"), g_GlobalVirtualAllocator.GetPageSize());
     16        CDebugHeapPtr<BYTE> p;
    14917        p.Allocate(1);
    15018        p[0] = 0x01;
     
    15523        p[0] = 0x03;
    15624        _tprintf(_T("p[0] 0x%02X, p[1] 0x%02X\n"), p[0], p[1]);
    157         if(FALSE)
     25        #pragma region PAGE_READONLY
     26        //if(FALSE)
    15827        {
    15928                p.SetProtection(PAGE_READONLY);
     
    16534                _ATLCATCHALL()
    16635                {
    167                         _tprintf(_T("Oopsie in line %d\n"), __LINE__);
     36                        _tprintf(_T("Oopsie in line %d (Failed to write to PAGE_READONLY memory)\n"), __LINE__);
    16837                }
    16938                p.SetProtection(PAGE_READWRITE);
     
    17140                _tprintf(_T("p[0] 0x%02X, p[1] 0x%02X\n"), p[0], p[1]);
    17241        }
     42        #pragma endregion
     43        volatile BYTE n;
    17344        p.SetProtection(PAGE_READWRITE | PAGE_GUARD);
    174         volatile BYTE n;
     45        #pragma region PAGE_GUARD
    17546        _ATLTRY
    17647        {
     
    17950        _ATLCATCHALL()
    18051        {
    181                 _tprintf(_T("Oopsie in line %d\n"), __LINE__);
     52                _tprintf(_T("Oopsie in line %d (First PAGE_GUARD access)\n"), __LINE__);
    18253        }
    18354        _ATLTRY
     
    18758        _ATLCATCHALL()
    18859        {
    189                 _tprintf(_T("Oopsie in line %d\n"), __LINE__);
     60                _tprintf(_T("Oopsie in line %d (Second PAGE_GUARD access)\n"), __LINE__);
    19061        }
     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;
     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;
     91        }
     92        _ATLCATCHALL()
     93        {
     94                _tprintf(_T("Oopsie in line %d (Writing with a too large index)\n"), __LINE__);
     95        }
     96        #pragma endregion
    19197        return 0;
    19298}
  • trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.vcxproj

    r33 r34  
    8383    <ClInclude Include="stdafx.h" />
    8484    <ClInclude Include="targetver.h" />
     85    <ClInclude Include="VirtualHeapPtr.h" />
    8586  </ItemGroup>
    8687  <ItemGroup>
  • trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.vcxproj.filters

    r33 r34  
    2525      <Filter>Header Files</Filter>
    2626    </ClInclude>
     27    <ClInclude Include="VirtualHeapPtr.h">
     28      <Filter>Header Files</Filter>
     29    </ClInclude>
    2730  </ItemGroup>
    2831  <ItemGroup>
Note: See TracChangeset for help on using the changeset viewer.