hdac SDK
SDK for hdac blockchain development
allocators.h
1 #ifndef ALLOCATORS_H
2 #define ALLOCATORS_H
3 
4 #include <boost/thread/mutex.hpp>
5 #include <boost/thread/once.hpp>
6 
7 #include <openssl/crypto.h> // for OPENSSL_cleanse()
8 
9 #include "zero_after_free_allocator.h"
10 #include <vector>
11 
23 template <class Locker>
25 {
26 public:
27  LockedPageManagerBase(size_t page_size) : page_size(page_size)
28  {
29  // Determine bitmask for extracting page from address
30  assert(!(page_size & (page_size - 1))); // size must be power of two
31  page_mask = ~(page_size - 1);
32  }
33 
35  {
36  assert(this->GetLockedPageCount() == 0);
37  }
38 
39 
40  // For all pages in affected range, increase lock count
41  void LockRange(void* p, size_t size)
42  {
43  boost::mutex::scoped_lock lock(mutex);
44  if (!size)
45  return;
46  const size_t base_addr = reinterpret_cast<size_t>(p);
47  const size_t start_page = base_addr & page_mask;
48  const size_t end_page = (base_addr + size - 1) & page_mask;
49  for (size_t page = start_page; page <= end_page; page += page_size) {
50  Histogram::iterator it = histogram.find(page);
51  if (it == histogram.end()) // Newly locked page
52  {
53  locker.Lock(reinterpret_cast<void*>(page), page_size);
54  histogram.insert(std::make_pair(page, 1));
55  } else // Page was already locked; increase counter
56  {
57  it->second += 1;
58  }
59  }
60  }
61 
62  // For all pages in affected range, decrease lock count
63  void UnlockRange(void* p, size_t size)
64  {
65  boost::mutex::scoped_lock lock(mutex);
66  if (!size)
67  return;
68  const size_t base_addr = reinterpret_cast<size_t>(p);
69  const size_t start_page = base_addr & page_mask;
70  const size_t end_page = (base_addr + size - 1) & page_mask;
71  for (size_t page = start_page; page <= end_page; page += page_size) {
72  Histogram::iterator it = histogram.find(page);
73  assert(it != histogram.end()); // Cannot unlock an area that was not locked
74  // Decrease counter for page, when it is zero, the page will be unlocked
75  it->second -= 1;
76  if (it->second == 0) // Nothing on the page anymore that keeps it locked
77  {
78  // Unlock page and remove the count from histogram
79  locker.Unlock(reinterpret_cast<void*>(page), page_size);
80  histogram.erase(it);
81  }
82  }
83  }
84 
85  // Get number of locked pages for diagnostics
86  int GetLockedPageCount()
87  {
88  boost::mutex::scoped_lock lock(mutex);
89  return histogram.size();
90  }
91 
92 private:
93  Locker locker;
94  boost::mutex mutex;
95  size_t page_size, page_mask;
96  // map of page base address to lock count
97  typedef std::map<size_t, int> Histogram;
98  Histogram histogram;
99 };
100 
106 {
107 public:
111  bool Lock(const void* addr, size_t len);
115  bool Unlock(const void* addr, size_t len);
116 };
117 
129 class LockedPageManager : public LockedPageManagerBase<MemoryPageLocker>
130 {
131 public:
132  static LockedPageManager& Instance()
133  {
134  boost::call_once(LockedPageManager::CreateInstance, LockedPageManager::init_flag);
135  return *LockedPageManager::_instance;
136  }
137 
138 private:
140 
141  static void CreateInstance()
142  {
143  // Using a local static instance guarantees that the object is initialized
144  // when it's first needed and also deinitialized after all objects that use
145  // it are done with it. I can think of one unlikely scenario where we may
146  // have a static deinitialization order/problem, but the check in
147  // LockedPageManagerBase's destructor helps us detect if that ever happens.
148  static LockedPageManager instance;
149  LockedPageManager::_instance = &instance;
150  }
151 
152  static LockedPageManager* _instance;
153  static boost::once_flag init_flag;
154 };
155 
156 //
157 // Functions for directly locking/unlocking memory objects.
158 // Intended for non-dynamically allocated structures.
159 //
160 template <typename T>
161 void LockObject(const T& t)
162 {
163  LockedPageManager::Instance().LockRange((void*)(&t), sizeof(T));
164 }
165 
166 template <typename T>
167 void UnlockObject(const T& t)
168 {
169  OPENSSL_cleanse((void*)(&t), sizeof(T));
170  LockedPageManager::Instance().UnlockRange((void*)(&t), sizeof(T));
171 }
172 
173 //
174 // Allocator that locks its contents from being paged
175 // out of memory and clears its contents before deletion.
176 //
177 template <typename T>
178 struct secure_allocator : public std::allocator<T> {
179  // MSVC8 default copy constructor is broken
180  typedef std::allocator<T> base;
181  typedef typename base::size_type size_type;
182  typedef typename base::difference_type difference_type;
183  typedef typename base::pointer pointer;
184  typedef typename base::const_pointer const_pointer;
185  typedef typename base::reference reference;
186  typedef typename base::const_reference const_reference;
187  typedef typename base::value_type value_type;
188  secure_allocator() throw() {}
189  secure_allocator(const secure_allocator& a) throw() : base(a) {}
190  template <typename U>
191  secure_allocator(const secure_allocator<U>& a) throw() : base(a)
192  {
193  }
194  ~secure_allocator() throw() {}
195  template <typename _Other>
196  struct rebind {
198  };
199 
200  T* allocate(std::size_t n, const void* hint = 0)
201  {
202  T* p;
203  p = std::allocator<T>::allocate(n, hint);
204  if (p != NULL)
205  LockedPageManager::Instance().LockRange(p, sizeof(T) * n);
206  return p;
207  }
208 
209  void deallocate(T* p, std::size_t n)
210  {
211  if (p != NULL) {
212  OPENSSL_cleanse(p, sizeof(T) * n);
213  LockedPageManager::Instance().UnlockRange(p, sizeof(T) * n);
214  }
215  std::allocator<T>::deallocate(p, n);
216  }
217 };
218 
219 // Byte-vector that clears its contents before deletion.
220 typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;
221 
222 #endif // ALLOCATORS_H
Definition: allocators.h:24
Definition: allocators.h:178
Definition: allocators.h:196
Definition: allocators.h:129
Definition: allocators.h:105