minefield.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * 'Minefield' - a crude Windows memory debugger, similar in concept
  3. * to the old Unix 'Electric Fence'. The main difference is that
  4. * Electric Fence can be imposed on a program from outside, via
  5. * LD_PRELOAD, whereas this has to be included in the program at
  6. * compile time with its own cooperation.
  7. *
  8. * This module provides the Minefield allocator. Actually enabling it
  9. * is done by a #define in force when the main utils/memory.c is
  10. * compiled.
  11. */
  12. #include "putty.h"
  13. #include "puttymem.h"
  14. #define PAGESIZE 4096
  15. /*
  16. * Design:
  17. *
  18. * We start by reserving as much virtual address space as Windows
  19. * will sensibly (or not sensibly) let us have. We flag it all as
  20. * invalid memory.
  21. *
  22. * Any allocation attempt is satisfied by committing one or more
  23. * pages, with an uncommitted page on either side. The returned
  24. * memory region is jammed up against the _end_ of the pages.
  25. *
  26. * Freeing anything causes instantaneous decommitment of the pages
  27. * involved, so stale pointers are caught as soon as possible.
  28. */
  29. static int minefield_initialised = 0;
  30. static void *minefield_region = NULL;
  31. static long minefield_size = 0;
  32. static long minefield_npages = 0;
  33. static long minefield_curpos = 0;
  34. static unsigned short *minefield_admin = NULL;
  35. static void *minefield_pages = NULL;
  36. static void minefield_admin_hide(int hide)
  37. {
  38. int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
  39. VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
  40. }
  41. static void minefield_init(void)
  42. {
  43. int size;
  44. int admin_size;
  45. int i;
  46. for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
  47. minefield_region = VirtualAlloc(NULL, size,
  48. MEM_RESERVE, PAGE_NOACCESS);
  49. if (minefield_region)
  50. break;
  51. }
  52. minefield_size = size;
  53. /*
  54. * Firstly, allocate a section of that to be the admin block.
  55. * We'll need a two-byte field for each page.
  56. */
  57. minefield_admin = minefield_region;
  58. minefield_npages = minefield_size / PAGESIZE;
  59. admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
  60. minefield_npages = (minefield_size - admin_size) / PAGESIZE;
  61. minefield_pages = (char *) minefield_region + admin_size;
  62. /*
  63. * Commit the admin region.
  64. */
  65. VirtualAlloc(minefield_admin, minefield_npages * 2,
  66. MEM_COMMIT, PAGE_READWRITE);
  67. /*
  68. * Mark all pages as unused (0xFFFF).
  69. */
  70. for (i = 0; i < minefield_npages; i++)
  71. minefield_admin[i] = 0xFFFF;
  72. /*
  73. * Hide the admin region.
  74. */
  75. minefield_admin_hide(1);
  76. minefield_initialised = 1;
  77. }
  78. static void minefield_bomb(void)
  79. {
  80. div(1, *(int *) minefield_pages);
  81. }
  82. static void *minefield_alloc(int size)
  83. {
  84. int npages;
  85. int pos, lim, region_end, region_start;
  86. int start;
  87. int i;
  88. npages = (size + PAGESIZE - 1) / PAGESIZE;
  89. minefield_admin_hide(0);
  90. /*
  91. * Search from current position until we find a contiguous
  92. * bunch of npages+2 unused pages.
  93. */
  94. pos = minefield_curpos;
  95. lim = minefield_npages;
  96. while (1) {
  97. /* Skip over used pages. */
  98. while (pos < lim && minefield_admin[pos] != 0xFFFF)
  99. pos++;
  100. /* Count unused pages. */
  101. start = pos;
  102. while (pos < lim && pos - start < npages + 2 &&
  103. minefield_admin[pos] == 0xFFFF)
  104. pos++;
  105. if (pos - start == npages + 2)
  106. break;
  107. /* If we've reached the limit, reset the limit or stop. */
  108. if (pos >= lim) {
  109. if (lim == minefield_npages) {
  110. /* go round and start again at zero */
  111. lim = minefield_curpos;
  112. pos = 0;
  113. } else {
  114. minefield_admin_hide(1);
  115. return NULL;
  116. }
  117. }
  118. }
  119. minefield_curpos = pos - 1;
  120. /*
  121. * We have npages+2 unused pages starting at start. We leave
  122. * the first and last of these alone and use the rest.
  123. */
  124. region_end = (start + npages + 1) * PAGESIZE;
  125. region_start = region_end - size;
  126. /* FIXME: could align here if we wanted */
  127. /*
  128. * Update the admin region.
  129. */
  130. for (i = start + 2; i < start + npages + 1; i++)
  131. minefield_admin[i] = 0xFFFE; /* used but no region starts here */
  132. minefield_admin[start + 1] = region_start % PAGESIZE;
  133. minefield_admin_hide(1);
  134. VirtualAlloc((char *) minefield_pages + region_start, size,
  135. MEM_COMMIT, PAGE_READWRITE);
  136. return (char *) minefield_pages + region_start;
  137. }
  138. static void minefield_free(void *ptr)
  139. {
  140. int region_start, i, j;
  141. minefield_admin_hide(0);
  142. region_start = (char *) ptr - (char *) minefield_pages;
  143. i = region_start / PAGESIZE;
  144. if (i < 0 || i >= minefield_npages ||
  145. minefield_admin[i] != region_start % PAGESIZE)
  146. minefield_bomb();
  147. for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
  148. minefield_admin[j] = 0xFFFF;
  149. }
  150. VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
  151. minefield_admin_hide(1);
  152. }
  153. static int minefield_get_size(void *ptr)
  154. {
  155. int region_start, i, j;
  156. minefield_admin_hide(0);
  157. region_start = (char *) ptr - (char *) minefield_pages;
  158. i = region_start / PAGESIZE;
  159. if (i < 0 || i >= minefield_npages ||
  160. minefield_admin[i] != region_start % PAGESIZE)
  161. minefield_bomb();
  162. for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
  163. minefield_admin_hide(1);
  164. return j * PAGESIZE - region_start;
  165. }
  166. void *minefield_c_malloc(size_t size)
  167. {
  168. if (!minefield_initialised)
  169. minefield_init();
  170. return minefield_alloc(size);
  171. }
  172. void minefield_c_free(void *p)
  173. {
  174. if (!minefield_initialised)
  175. minefield_init();
  176. minefield_free(p);
  177. }
  178. /*
  179. * realloc _always_ moves the chunk, for rapid detection of code
  180. * that assumes it won't.
  181. */
  182. void *minefield_c_realloc(void *p, size_t size)
  183. {
  184. size_t oldsize;
  185. void *q;
  186. if (!minefield_initialised)
  187. minefield_init();
  188. q = minefield_alloc(size);
  189. oldsize = minefield_get_size(p);
  190. memcpy(q, p, (oldsize < size ? oldsize : size));
  191. minefield_free(p);
  192. return q;
  193. }