duma_hlp.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * DUMA - Red-Zone memory allocator.
  3. * Copyright (C) 2002-2009 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
  4. * License: GNU LGPL (GNU Lesser General Public License, see COPYING-GPL)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * FILE CONTENTS:
  21. * internal implementation file
  22. * contains helper functions for DUMA
  23. */
  24. /* Function: reduceProtectedMemory
  25. *
  26. * delete reductionSizekB amount of memory, which has already
  27. * been freed but got protected
  28. * return != 0 when more memory reducable
  29. */
  30. static int
  31. reduceProtectedMemory( size_t reductionSizekB )
  32. {
  33. struct _DUMA_Slot * slot = _duma_g.allocList;
  34. size_t count = _duma_s.slotCount;
  35. size_t alreadyReducekB = 0;
  36. #ifndef WIN32
  37. /* Windows VirtualFree(,,MEM_RELEASE) can only free whole allocations. not parts */
  38. size_t delSize, newSize;
  39. /* 1- try reducing memory to just keep page(s) with userAddress */
  40. for ( ; count > 0 && alreadyReducekB < reductionSizekB; --count, ++slot )
  41. if ( DUMAST_ALL_PROTECTED == slot->state )
  42. {
  43. /* free memory above userAddr; keep userAddr protected */
  44. newSize = (char*)slot->userAddress - (char*)slot->internalAddress;
  45. newSize = (newSize + DUMA_PAGE_SIZE) & ~(DUMA_PAGE_SIZE -1);
  46. delSize = slot->internalSize - newSize;
  47. Page_Delete( (char*)slot->internalAddress + newSize, delSize );
  48. alreadyReducekB += (delSize+1023) >>10;
  49. slot->state = DUMAST_BEGIN_PROTECTED;
  50. /* but keep the slot and userAddr */
  51. slot->internalSize = newSize;
  52. if ( alreadyReducekB >= reductionSizekB )
  53. {
  54. _duma_s.sumProtectedMem -= alreadyReducekB;
  55. _duma_s.sumAllocatedMem -= alreadyReducekB;
  56. return 1;
  57. }
  58. }
  59. #endif
  60. /* 2- deallocate all page(s) with userAddress, empty whole slot */
  61. slot = _duma_g.allocList;
  62. count = _duma_s.slotCount;
  63. for ( ; count > 0 && alreadyReducekB < reductionSizekB; --count, ++slot )
  64. if ( DUMAST_BEGIN_PROTECTED == slot->state
  65. #if defined(WIN32)
  66. || DUMAST_ALL_PROTECTED == slot->state
  67. #endif
  68. )
  69. {
  70. /* free all the memory */
  71. Page_Delete(slot->internalAddress, slot->internalSize);
  72. alreadyReducekB += (slot->internalSize+1023) >>10;
  73. /* free slot and userAddr */
  74. slot->internalAddress = slot->userAddress = 0;
  75. slot->internalSize = slot->userSize = 0;
  76. slot->state = DUMAST_EMPTY;
  77. slot->allocator = EFA_INT_ALLOC;
  78. #ifndef DUMA_NO_LEAKDETECTION
  79. slot->fileSource = DUMAFS_EMPTY;
  80. slot->filename = 0;
  81. slot->lineno = 0;
  82. #endif
  83. if ( alreadyReducekB >= reductionSizekB )
  84. {
  85. _duma_s.sumProtectedMem -= alreadyReducekB;
  86. _duma_s.sumAllocatedMem -= alreadyReducekB;
  87. return 1;
  88. }
  89. }
  90. return 0;
  91. }
  92. /* Function: slotForUserAddress
  93. *
  94. * Find the slot structure for a user address.
  95. */
  96. static struct _DUMA_Slot *
  97. slotForUserAddress(void * address)
  98. {
  99. struct _DUMA_Slot * slot = _duma_g.allocList;
  100. size_t count = _duma_s.slotCount;
  101. for ( ; count > 0; --count, ++slot )
  102. if ( slot->userAddress == address )
  103. return slot;
  104. return 0;
  105. }
  106. /* Function: nearestSlotForUserAddress
  107. *
  108. * Find the nearest slot structure for a user address.
  109. */
  110. static struct _DUMA_Slot *
  111. nearestSlotForUserAddress(void * userAddress)
  112. {
  113. struct _DUMA_Slot * slot = _duma_g.allocList;
  114. size_t count = _duma_s.slotCount;
  115. for ( ; count > 0; --count, ++slot )
  116. if ( (char*)slot->internalAddress <= (char*)userAddress
  117. && (char*)userAddress <= (char*)slot->internalAddress + slot->internalSize
  118. )
  119. return slot;
  120. return 0;
  121. }
  122. /* Function: _duma_init_slack
  123. *
  124. * Initialise the no mans land, for a given slot
  125. */
  126. static
  127. void _duma_init_slack( struct _DUMA_Slot * slot )
  128. {
  129. char * accBegAddr, * accEndAddr;
  130. char * tmpBegAddr, * tmpEndAddr;
  131. #ifdef DUMA_EXPLICIT_INIT
  132. slot->slackfill = _duma_s.SLACKFILL;
  133. #endif
  134. /* nothing to do for zero userSize */
  135. if ( !slot->userSize )
  136. return;
  137. /* calculate accessible non-protectable address area */
  138. if ( (char*)slot->protAddress < (char*)slot->userAddress )
  139. {
  140. /* DUMA_PROTECT_BELOW was 1 when allocating this piece of memory */
  141. accBegAddr = (char*)slot->userAddress;
  142. accEndAddr = (char*)slot->internalAddress + slot->internalSize;
  143. }
  144. else
  145. {
  146. /* DUMA_PROTECT_BELOW was 0 when allocating this piece of memory */
  147. accBegAddr = (char*)slot->internalAddress;
  148. accEndAddr = (char*)slot->protAddress;
  149. }
  150. tmpBegAddr = accBegAddr;
  151. tmpEndAddr = (char*)slot->userAddress;
  152. while (tmpBegAddr < tmpEndAddr)
  153. *tmpBegAddr++ = (char)_duma_s.SLACKFILL;
  154. tmpBegAddr = (char*)slot->userAddress + slot->userSize;
  155. tmpEndAddr = accEndAddr;
  156. while (tmpBegAddr < tmpEndAddr)
  157. *tmpBegAddr++ = (char)_duma_s.SLACKFILL;
  158. }
  159. /* Function: _duma_check_slack
  160. *
  161. * Checks the integrity of no mans land, for a given slot
  162. */
  163. static
  164. void _duma_check_slack( struct _DUMA_Slot * slot )
  165. {
  166. char * accBegAddr, * accEndAddr;
  167. char * tmpBegAddr, * tmpEndAddr;
  168. char slackfill;
  169. #ifdef DUMA_EXPLICIT_INIT
  170. slackfill = (char)slot->slackfill;
  171. #else
  172. slackfill = (char)_duma_s.SLACKFILL;
  173. #endif
  174. /* nothing to do for zero userSize */
  175. if ( !slot->userSize )
  176. return;
  177. if ( !slot->internalAddress )
  178. return;
  179. /* calculate accessible non-protectable address area */
  180. if ( (char*)slot->protAddress < (char*)slot->userAddress )
  181. {
  182. /* DUMA_PROTECT_BELOW was 1 when allocating this piece of memory */
  183. accBegAddr = (char*)slot->userAddress;
  184. accEndAddr = (char*)slot->internalAddress + slot->internalSize;
  185. }
  186. else
  187. {
  188. /* DUMA_PROTECT_BELOW was 0 when allocating this piece of memory */
  189. accBegAddr = (char*)slot->internalAddress;
  190. accEndAddr = (char*)slot->protAddress;
  191. }
  192. tmpBegAddr = accBegAddr;
  193. tmpEndAddr = (char*)slot->userAddress;
  194. while (tmpBegAddr < tmpEndAddr)
  195. {
  196. if ( (char)slackfill != *tmpBegAddr++ )
  197. {
  198. #ifndef DUMA_NO_LEAKDETECTION
  199. DUMA_Abort("ptr=%a: detected overwrite of ptrs no mans land below userSpace, size=%d alloced from %s(%i)",
  200. (DUMA_ADDR)slot->userAddress, (DUMA_SIZE)slot->userSize, slot->filename, slot->lineno);
  201. #else
  202. DUMA_Abort("ptr=%a: detected overwrite of ptrs no mans land below userSpace", (DUMA_ADDR)slot->userAddress);
  203. #endif
  204. }
  205. }
  206. tmpBegAddr = (char*)slot->userAddress + slot->userSize;
  207. tmpEndAddr = accEndAddr;
  208. while (tmpBegAddr < tmpEndAddr)
  209. {
  210. if ( (char)slackfill != *tmpBegAddr++ )
  211. {
  212. #ifndef DUMA_NO_LEAKDETECTION
  213. DUMA_Abort("detected overwrite of no mans land above userSpace: ptr=%a, size=%d\nalloced from %s(%i)",
  214. (DUMA_ADDR)slot->userAddress, (DUMA_SIZE)slot->userSize, slot->filename, slot->lineno);
  215. #else
  216. DUMA_Abort("detected overwrite of no mans land above userSpace: ptr=%a", (DUMA_ADDR)slot->userAddress);
  217. #endif
  218. }
  219. }
  220. }
  221. /* Function: _duma_check_all_slacks
  222. *
  223. * Checks the integrity of all no mans land
  224. */
  225. static void
  226. _duma_check_all_slacks( void )
  227. {
  228. struct _DUMA_Slot * slot = _duma_g.allocList;
  229. size_t count = _duma_s.slotCount;
  230. for ( ; count > 0; --count, ++slot )
  231. {
  232. /* CHECK INTEGRITY OF NO MANS LAND */
  233. if ( DUMAST_IN_USE == slot->state && slot->userSize )
  234. _duma_check_slack( slot );
  235. }
  236. }