Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. //
  2. // Die Klasse MemoryProtect hat die Fähigkeit den Speicher
  3. // aus dem ein Objekt das von ihr abgleitet hat gegen Schreiben
  4. // zu schützen. Nützlich nur zu debug-Zwecken
  5. //
  6.  
  7. #include "stdafx.h"
  8. #include <new>          // std::bad_alloc
  9. #include <unordered_set>
  10. #include <windows.h>
  11. #include <mutex>        // std::mutex
  12.  
  13. using namespace std;
  14.  
  15. DWORD getPageSize() {
  16.   static DWORD pagesize = [] {
  17.     SYSTEM_INFO si;
  18.     GetSystemInfo(&si);
  19.     return si.dwPageSize;
  20.   }();
  21.   return pagesize;
  22. }
  23.  
  24. template <class T>
  25. class MemoryProtect {
  26.   protected:
  27.     typedef T cageclass_t;
  28.  
  29.   public:
  30.     MemoryProtect() {}
  31.  
  32.     /* static */ void *operator new(size_t size);
  33.     /* static */ void *operator new[](size_t size);
  34.     /* static */ void operator delete(void *p);
  35.     /* static */ void operator delete[](void *p);
  36.  
  37.     bool protectMemoryCage();
  38.     bool unprotectMemoryCage();
  39.  
  40.     static bool checkPointer(const T *);
  41.     static void g_beValidOrCrash(const T *);
  42.     void beValidOrCrash() const;
  43.   private:
  44.     static unordered_set<const T*> c_validpointerlist;
  45.     static mutex c_mtx;  
  46.     size_t m_actualsize;
  47. };
  48.  
  49. template <class T>
  50. /* static */
  51. unordered_set<const T*> MemoryProtect<T>::c_validpointerlist;
  52.  
  53. template <class T>
  54. /* static */
  55. mutex MemoryProtect<T>::c_mtx;
  56.  
  57. template <class T>
  58. /* static */
  59. bool MemoryProtect<T>::checkPointer(const T *obj) {
  60.   lock_guard<mutex> lck (c_mtx);
  61.   return c_validpointerlist.end() != c_validpointerlist.find(obj);
  62. }
  63.  
  64. template <class T>
  65. /* static */
  66. void MemoryProtect<T>::g_beValidOrCrash(const T *obj) {
  67.   if (!checkPointer(obj)) {
  68.     // Purposefully crash and spawn the Crashreport...
  69. /*    void *p = 0;
  70.     *p = 0;*/
  71.   }
  72. }
  73.  
  74. template <class T>
  75. void MemoryProtect<T>::beValidOrCrash() const {
  76.   g_beValidOrCrash(static_cast<const T *>(this));
  77. }
  78.  
  79. template <class T>
  80. bool MemoryProtect<T>::protectMemoryCage() {
  81.   DWORD oldprotect;
  82.   return (TRUE == VirtualProtect(this, m_actualsize, PAGE_READONLY, &oldprotect));
  83. }
  84.  
  85. template <class T>
  86. bool MemoryProtect<T>::unprotectMemoryCage() {
  87.   DWORD oldprotect;
  88.   return (TRUE == VirtualProtect(this, m_actualsize, PAGE_READWRITE, &oldprotect));
  89. }
  90.  
  91. template <class T>
  92. // /* static */
  93. void *MemoryProtect<T>::operator new(size_t size)
  94. {
  95.   // we dont use VirtualAlloc to be similar to the
  96.   // original malloc behaviour
  97.   // we need to use memory that spans over cpu-pages
  98.   // for VirtualProtect
  99.   DWORD pagesize = getPageSize();
  100.   size_t actualsize = size_t((size + (pagesize - 1)) / pagesize) * pagesize;
  101.   void *p = _aligned_malloc(actualsize, pagesize);
  102.   if (!p)
  103.     throw bad_alloc();
  104.   ((T *)p)->m_actualsize = actualsize;
  105.   lock_guard<mutex> lck (c_mtx);
  106.   MemoryProtect<T>::c_validpointerlist.insert(((T *)p));
  107.   return p;
  108. }
  109.  
  110. template <class T>
  111. void *MemoryProtect<T>::operator new[](size_t size)
  112. {
  113.   // we dont use VirtualAlloc to be similar to the
  114.   // original malloc behaviour
  115.   // we need to use memory that spans over cpu-pages
  116.   // for VirtualProtect
  117.   DWORD pagesize = getPageSize();
  118.   size_t actualsize = size_t(size + (pagesize - 1) / pagesize) * pagesize;
  119.   void *p = _aligned_malloc(actualsize, pagesize);
  120.   if (!p)
  121.     throw bad_alloc();
  122.   ((T *)p)->m_actualsize = actualsize;
  123.   lock_guard<mutex> lck (c_mtx);
  124.   MemoryProtect<T>::c_validpointerlist.insert(((T *)p));
  125.   return p;
  126. }
  127.  
  128. template <class T>
  129. void MemoryProtect<T>::operator delete(void *p)
  130. {
  131.   if (nullptr == p)
  132.     throw std::bad_alloc();
  133.   ((T *)p)->unprotectMemoryCage();
  134.   lock_guard<mutex> lck (c_mtx);
  135.   if (0 == MemoryProtect<T>::c_validpointerlist.erase(((T*)p)))
  136.     throw std::bad_alloc();
  137.   _aligned_free(p);
  138. }
  139.  
  140. template <class T>
  141. void MemoryProtect<T>::operator delete[](void *p)
  142. {
  143.   if (nullptr == p)
  144.     throw bad_alloc();
  145.   (T *)p->unprotectMemoryCage();
  146.   lock_guard<mutex> lck (c_mtx);
  147.   if (0 == c_validpointerlist.erase(p))
  148.     throw bad_alloc();
  149.   _aligned_free(p);
  150. }
  151.  
  152. //
  153.  
  154. class A : public MemoryProtect<A> {
  155.   int blubber;
  156. };
  157.  
  158. int _tmain(int argc, _TCHAR* argv[])
  159. {
  160.   A *blah = new A();
  161.   blah->protectMemoryCage();
  162.   if (!A::checkPointer(blah)) {
  163.     int damn = 4;
  164.   }
  165.   if (A::checkPointer(blah+1)) {
  166.     int damn = 5;
  167.   }
  168.   blah->beValidOrCrash();
  169.   //((unsigned int *)blah)[0] = 9;
  170.   delete blah;
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement