WINMISC.C 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * winmisc.c: miscellaneous Windows-specific things
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "putty.h"
  7. OSVERSIONINFO osVersion;
  8. void platform_get_x11_auth(char *display, int *proto,
  9. unsigned char *data, int *datalen)
  10. {
  11. /* We don't support this at all under Windows. */
  12. }
  13. const char platform_x11_best_transport[] = "localhost";
  14. char *platform_get_x_display(void) {
  15. /* We may as well check for DISPLAY in case it's useful. */
  16. return dupstr(getenv("DISPLAY"));
  17. }
  18. Filename filename_from_str(const char *str)
  19. {
  20. Filename ret;
  21. strncpy(ret.path, str, sizeof(ret.path));
  22. ret.path[sizeof(ret.path)-1] = '\0';
  23. return ret;
  24. }
  25. const char *filename_to_str(const Filename *fn)
  26. {
  27. return fn->path;
  28. }
  29. int filename_equal(Filename f1, Filename f2)
  30. {
  31. return !strcmp(f1.path, f2.path);
  32. }
  33. int filename_is_null(Filename fn)
  34. {
  35. return !*fn.path;
  36. }
  37. char *get_username(void)
  38. {
  39. DWORD namelen;
  40. char *user;
  41. namelen = 0;
  42. if (GetUserName(NULL, &namelen) == FALSE) {
  43. /*
  44. * Apparently this doesn't work at least on Windows XP SP2.
  45. * Thus assume a maximum of 256. It will fail again if it
  46. * doesn't fit.
  47. */
  48. namelen = 256;
  49. }
  50. user = snewn(namelen, char);
  51. GetUserName(user, &namelen);
  52. return user;
  53. }
  54. BOOL init_winver(void)
  55. {
  56. ZeroMemory(&osVersion, sizeof(osVersion));
  57. osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
  58. return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
  59. }
  60. #ifdef DEBUG
  61. static FILE *debug_fp = NULL;
  62. static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
  63. static int debug_got_console = 0;
  64. void dputs(char *buf)
  65. {
  66. DWORD dw;
  67. if (!debug_got_console) {
  68. if (AllocConsole()) {
  69. debug_got_console = 1;
  70. debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
  71. }
  72. }
  73. if (!debug_fp) {
  74. debug_fp = fopen("debug.log", "w");
  75. }
  76. if (debug_hdl != INVALID_HANDLE_VALUE) {
  77. WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
  78. }
  79. fputs(buf, debug_fp);
  80. fflush(debug_fp);
  81. }
  82. #endif
  83. #ifdef MINEFIELD
  84. /*
  85. * Minefield - a Windows equivalent for Electric Fence
  86. */
  87. #define PAGESIZE 4096
  88. /*
  89. * Design:
  90. *
  91. * We start by reserving as much virtual address space as Windows
  92. * will sensibly (or not sensibly) let us have. We flag it all as
  93. * invalid memory.
  94. *
  95. * Any allocation attempt is satisfied by committing one or more
  96. * pages, with an uncommitted page on either side. The returned
  97. * memory region is jammed up against the _end_ of the pages.
  98. *
  99. * Freeing anything causes instantaneous decommitment of the pages
  100. * involved, so stale pointers are caught as soon as possible.
  101. */
  102. static int minefield_initialised = 0;
  103. static void *minefield_region = NULL;
  104. static long minefield_size = 0;
  105. static long minefield_npages = 0;
  106. static long minefield_curpos = 0;
  107. static unsigned short *minefield_admin = NULL;
  108. static void *minefield_pages = NULL;
  109. static void minefield_admin_hide(int hide)
  110. {
  111. int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
  112. VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
  113. }
  114. static void minefield_init(void)
  115. {
  116. int size;
  117. int admin_size;
  118. int i;
  119. for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
  120. minefield_region = VirtualAlloc(NULL, size,
  121. MEM_RESERVE, PAGE_NOACCESS);
  122. if (minefield_region)
  123. break;
  124. }
  125. minefield_size = size;
  126. /*
  127. * Firstly, allocate a section of that to be the admin block.
  128. * We'll need a two-byte field for each page.
  129. */
  130. minefield_admin = minefield_region;
  131. minefield_npages = minefield_size / PAGESIZE;
  132. admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
  133. minefield_npages = (minefield_size - admin_size) / PAGESIZE;
  134. minefield_pages = (char *) minefield_region + admin_size;
  135. /*
  136. * Commit the admin region.
  137. */
  138. VirtualAlloc(minefield_admin, minefield_npages * 2,
  139. MEM_COMMIT, PAGE_READWRITE);
  140. /*
  141. * Mark all pages as unused (0xFFFF).
  142. */
  143. for (i = 0; i < minefield_npages; i++)
  144. minefield_admin[i] = 0xFFFF;
  145. /*
  146. * Hide the admin region.
  147. */
  148. minefield_admin_hide(1);
  149. minefield_initialised = 1;
  150. }
  151. static void minefield_bomb(void)
  152. {
  153. div(1, *(int *) minefield_pages);
  154. }
  155. static void *minefield_alloc(int size)
  156. {
  157. int npages;
  158. int pos, lim, region_end, region_start;
  159. int start;
  160. int i;
  161. npages = (size + PAGESIZE - 1) / PAGESIZE;
  162. minefield_admin_hide(0);
  163. /*
  164. * Search from current position until we find a contiguous
  165. * bunch of npages+2 unused pages.
  166. */
  167. pos = minefield_curpos;
  168. lim = minefield_npages;
  169. while (1) {
  170. /* Skip over used pages. */
  171. while (pos < lim && minefield_admin[pos] != 0xFFFF)
  172. pos++;
  173. /* Count unused pages. */
  174. start = pos;
  175. while (pos < lim && pos - start < npages + 2 &&
  176. minefield_admin[pos] == 0xFFFF)
  177. pos++;
  178. if (pos - start == npages + 2)
  179. break;
  180. /* If we've reached the limit, reset the limit or stop. */
  181. if (pos >= lim) {
  182. if (lim == minefield_npages) {
  183. /* go round and start again at zero */
  184. lim = minefield_curpos;
  185. pos = 0;
  186. } else {
  187. minefield_admin_hide(1);
  188. return NULL;
  189. }
  190. }
  191. }
  192. minefield_curpos = pos - 1;
  193. /*
  194. * We have npages+2 unused pages starting at start. We leave
  195. * the first and last of these alone and use the rest.
  196. */
  197. region_end = (start + npages + 1) * PAGESIZE;
  198. region_start = region_end - size;
  199. /* FIXME: could align here if we wanted */
  200. /*
  201. * Update the admin region.
  202. */
  203. for (i = start + 2; i < start + npages + 1; i++)
  204. minefield_admin[i] = 0xFFFE; /* used but no region starts here */
  205. minefield_admin[start + 1] = region_start % PAGESIZE;
  206. minefield_admin_hide(1);
  207. VirtualAlloc((char *) minefield_pages + region_start, size,
  208. MEM_COMMIT, PAGE_READWRITE);
  209. return (char *) minefield_pages + region_start;
  210. }
  211. static void minefield_free(void *ptr)
  212. {
  213. int region_start, i, j;
  214. minefield_admin_hide(0);
  215. region_start = (char *) ptr - (char *) minefield_pages;
  216. i = region_start / PAGESIZE;
  217. if (i < 0 || i >= minefield_npages ||
  218. minefield_admin[i] != region_start % PAGESIZE)
  219. minefield_bomb();
  220. for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
  221. minefield_admin[j] = 0xFFFF;
  222. }
  223. VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
  224. minefield_admin_hide(1);
  225. }
  226. static int minefield_get_size(void *ptr)
  227. {
  228. int region_start, i, j;
  229. minefield_admin_hide(0);
  230. region_start = (char *) ptr - (char *) minefield_pages;
  231. i = region_start / PAGESIZE;
  232. if (i < 0 || i >= minefield_npages ||
  233. minefield_admin[i] != region_start % PAGESIZE)
  234. minefield_bomb();
  235. for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
  236. minefield_admin_hide(1);
  237. return j * PAGESIZE - region_start;
  238. }
  239. void *minefield_c_malloc(size_t size)
  240. {
  241. if (!minefield_initialised)
  242. minefield_init();
  243. return minefield_alloc(size);
  244. }
  245. void minefield_c_free(void *p)
  246. {
  247. if (!minefield_initialised)
  248. minefield_init();
  249. minefield_free(p);
  250. }
  251. /*
  252. * realloc _always_ moves the chunk, for rapid detection of code
  253. * that assumes it won't.
  254. */
  255. void *minefield_c_realloc(void *p, size_t size)
  256. {
  257. size_t oldsize;
  258. void *q;
  259. if (!minefield_initialised)
  260. minefield_init();
  261. q = minefield_alloc(size);
  262. oldsize = minefield_get_size(p);
  263. memcpy(q, p, (oldsize < size ? oldsize : size));
  264. minefield_free(p);
  265. return q;
  266. }
  267. #endif /* MINEFIELD */