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