374330e2 |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
5471d09a |
4 | #include <assert.h> |
374330e2 |
5 | #include "putty.h" |
6 | |
03f64569 |
7 | /* ---------------------------------------------------------------------- |
8 | * String handling routines. |
9 | */ |
10 | |
11 | char *dupstr(char *s) |
12 | { |
13 | int len = strlen(s); |
14 | char *p = smalloc(len + 1); |
15 | strcpy(p, s); |
16 | return p; |
17 | } |
18 | |
19 | /* Allocate the concatenation of N strings. Terminate arg list with NULL. */ |
20 | char *dupcat(char *s1, ...) |
21 | { |
22 | int len; |
23 | char *p, *q, *sn; |
24 | va_list ap; |
25 | |
26 | len = strlen(s1); |
27 | va_start(ap, s1); |
28 | while (1) { |
29 | sn = va_arg(ap, char *); |
30 | if (!sn) |
31 | break; |
32 | len += strlen(sn); |
33 | } |
34 | va_end(ap); |
35 | |
36 | p = smalloc(len + 1); |
37 | strcpy(p, s1); |
38 | q = p + strlen(p); |
39 | |
40 | va_start(ap, s1); |
41 | while (1) { |
42 | sn = va_arg(ap, char *); |
43 | if (!sn) |
44 | break; |
45 | strcpy(q, sn); |
46 | q += strlen(q); |
47 | } |
48 | va_end(ap); |
49 | |
50 | return p; |
51 | } |
52 | |
53 | /* ---------------------------------------------------------------------- |
5471d09a |
54 | * Generic routines to deal with send buffers: a linked list of |
55 | * smallish blocks, with the operations |
56 | * |
57 | * - add an arbitrary amount of data to the end of the list |
58 | * - remove the first N bytes from the list |
59 | * - return a (pointer,length) pair giving some initial data in |
60 | * the list, suitable for passing to a send or write system |
61 | * call |
62 | * - return the current size of the buffer chain in bytes |
63 | */ |
64 | |
65 | #define BUFFER_GRANULE 512 |
66 | |
67 | struct bufchain_granule { |
68 | struct bufchain_granule *next; |
69 | int buflen, bufpos; |
70 | char buf[BUFFER_GRANULE]; |
71 | }; |
72 | |
73 | void bufchain_init(bufchain *ch) |
74 | { |
75 | ch->head = ch->tail = NULL; |
76 | ch->buffersize = 0; |
77 | } |
78 | |
79 | void bufchain_clear(bufchain *ch) |
80 | { |
81 | struct bufchain_granule *b; |
82 | while (ch->head) { |
83 | b = ch->head; |
84 | ch->head = ch->head->next; |
85 | sfree(b); |
86 | } |
87 | ch->tail = NULL; |
88 | ch->buffersize = 0; |
89 | } |
90 | |
91 | int bufchain_size(bufchain *ch) |
92 | { |
93 | return ch->buffersize; |
94 | } |
95 | |
96 | void bufchain_add(bufchain *ch, void *data, int len) |
97 | { |
98 | char *buf = (char *)data; |
99 | |
100 | ch->buffersize += len; |
101 | |
102 | if (ch->tail && ch->tail->buflen < BUFFER_GRANULE) { |
103 | int copylen = min(len, BUFFER_GRANULE - ch->tail->buflen); |
104 | memcpy(ch->tail->buf + ch->tail->buflen, buf, copylen); |
105 | buf += copylen; |
106 | len -= copylen; |
107 | ch->tail->buflen += copylen; |
108 | } |
109 | while (len > 0) { |
110 | int grainlen = min(len, BUFFER_GRANULE); |
111 | struct bufchain_granule *newbuf; |
112 | newbuf = smalloc(sizeof(struct bufchain_granule)); |
113 | newbuf->bufpos = 0; |
114 | newbuf->buflen = grainlen; |
115 | memcpy(newbuf->buf, buf, grainlen); |
116 | buf += grainlen; |
117 | len -= grainlen; |
118 | if (ch->tail) |
119 | ch->tail->next = newbuf; |
120 | else |
121 | ch->head = ch->tail = newbuf; |
122 | newbuf->next = NULL; |
123 | ch->tail = newbuf; |
124 | } |
125 | } |
126 | |
127 | void bufchain_consume(bufchain *ch, int len) |
128 | { |
129 | assert(ch->buffersize >= len); |
130 | assert(ch->head != NULL && ch->head->bufpos + len <= ch->head->buflen); |
131 | ch->head->bufpos += len; |
132 | ch->buffersize -= len; |
133 | if (ch->head->bufpos >= ch->head->buflen) { |
134 | struct bufchain_granule *tmp = ch->head; |
135 | ch->head = tmp->next; |
136 | sfree(tmp); |
137 | if (!ch->head) |
138 | ch->tail = NULL; |
139 | } |
140 | } |
141 | |
142 | void bufchain_prefix(bufchain *ch, void **data, int *len) |
143 | { |
144 | *len = ch->head->buflen - ch->head->bufpos; |
145 | *data = ch->head->buf + ch->head->bufpos; |
146 | } |
147 | |
03f64569 |
148 | /* ---------------------------------------------------------------------- |
b191636d |
149 | * My own versions of malloc, realloc and free. Because I want |
150 | * malloc and realloc to bomb out and exit the program if they run |
151 | * out of memory, realloc to reliably call malloc if passed a NULL |
152 | * pointer, and free to reliably do nothing if passed a NULL |
153 | * pointer. We can also put trace printouts in, if we need to; and |
154 | * we can also replace the allocator with an ElectricFence-like |
155 | * one. |
156 | */ |
157 | |
158 | #ifdef MINEFIELD |
159 | /* |
160 | * Minefield - a Windows equivalent for Electric Fence |
161 | */ |
162 | |
163 | #define PAGESIZE 4096 |
164 | |
165 | /* |
166 | * Design: |
167 | * |
168 | * We start by reserving as much virtual address space as Windows |
169 | * will sensibly (or not sensibly) let us have. We flag it all as |
170 | * invalid memory. |
171 | * |
172 | * Any allocation attempt is satisfied by committing one or more |
173 | * pages, with an uncommitted page on either side. The returned |
174 | * memory region is jammed up against the _end_ of the pages. |
175 | * |
176 | * Freeing anything causes instantaneous decommitment of the pages |
177 | * involved, so stale pointers are caught as soon as possible. |
178 | */ |
179 | |
180 | static int minefield_initialised = 0; |
181 | static void *minefield_region = NULL; |
182 | static long minefield_size = 0; |
183 | static long minefield_npages = 0; |
184 | static long minefield_curpos = 0; |
185 | static unsigned short *minefield_admin = NULL; |
186 | static void *minefield_pages = NULL; |
187 | |
32874aea |
188 | static void minefield_admin_hide(int hide) |
189 | { |
b191636d |
190 | int access = hide ? PAGE_NOACCESS : PAGE_READWRITE; |
32874aea |
191 | VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL); |
b191636d |
192 | } |
193 | |
32874aea |
194 | static void minefield_init(void) |
195 | { |
b191636d |
196 | int size; |
197 | int admin_size; |
198 | int i; |
199 | |
32874aea |
200 | for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) { |
201 | minefield_region = VirtualAlloc(NULL, size, |
202 | MEM_RESERVE, PAGE_NOACCESS); |
203 | if (minefield_region) |
204 | break; |
b191636d |
205 | } |
206 | minefield_size = size; |
b191636d |
207 | |
208 | /* |
209 | * Firstly, allocate a section of that to be the admin block. |
210 | * We'll need a two-byte field for each page. |
211 | */ |
212 | minefield_admin = minefield_region; |
213 | minefield_npages = minefield_size / PAGESIZE; |
32874aea |
214 | admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1); |
b191636d |
215 | minefield_npages = (minefield_size - admin_size) / PAGESIZE; |
32874aea |
216 | minefield_pages = (char *) minefield_region + admin_size; |
b191636d |
217 | |
218 | /* |
219 | * Commit the admin region. |
220 | */ |
221 | VirtualAlloc(minefield_admin, minefield_npages * 2, |
32874aea |
222 | MEM_COMMIT, PAGE_READWRITE); |
b191636d |
223 | |
224 | /* |
225 | * Mark all pages as unused (0xFFFF). |
226 | */ |
227 | for (i = 0; i < minefield_npages; i++) |
32874aea |
228 | minefield_admin[i] = 0xFFFF; |
b191636d |
229 | |
230 | /* |
231 | * Hide the admin region. |
232 | */ |
233 | minefield_admin_hide(1); |
234 | |
235 | minefield_initialised = 1; |
236 | } |
237 | |
32874aea |
238 | static void minefield_bomb(void) |
239 | { |
240 | div(1, *(int *) minefield_pages); |
b191636d |
241 | } |
242 | |
32874aea |
243 | static void *minefield_alloc(int size) |
244 | { |
b191636d |
245 | int npages; |
246 | int pos, lim, region_end, region_start; |
247 | int start; |
248 | int i; |
249 | |
32874aea |
250 | npages = (size + PAGESIZE - 1) / PAGESIZE; |
b191636d |
251 | |
252 | minefield_admin_hide(0); |
253 | |
254 | /* |
255 | * Search from current position until we find a contiguous |
256 | * bunch of npages+2 unused pages. |
257 | */ |
258 | pos = minefield_curpos; |
259 | lim = minefield_npages; |
260 | while (1) { |
32874aea |
261 | /* Skip over used pages. */ |
262 | while (pos < lim && minefield_admin[pos] != 0xFFFF) |
263 | pos++; |
264 | /* Count unused pages. */ |
265 | start = pos; |
266 | while (pos < lim && pos - start < npages + 2 && |
267 | minefield_admin[pos] == 0xFFFF) |
268 | pos++; |
269 | if (pos - start == npages + 2) |
270 | break; |
271 | /* If we've reached the limit, reset the limit or stop. */ |
272 | if (pos >= lim) { |
273 | if (lim == minefield_npages) { |
274 | /* go round and start again at zero */ |
275 | lim = minefield_curpos; |
276 | pos = 0; |
277 | } else { |
278 | minefield_admin_hide(1); |
279 | return NULL; |
280 | } |
281 | } |
b191636d |
282 | } |
283 | |
32874aea |
284 | minefield_curpos = pos - 1; |
b191636d |
285 | |
286 | /* |
287 | * We have npages+2 unused pages starting at start. We leave |
288 | * the first and last of these alone and use the rest. |
289 | */ |
32874aea |
290 | region_end = (start + npages + 1) * PAGESIZE; |
b191636d |
291 | region_start = region_end - size; |
292 | /* FIXME: could align here if we wanted */ |
293 | |
294 | /* |
295 | * Update the admin region. |
296 | */ |
32874aea |
297 | for (i = start + 2; i < start + npages - 1; i++) |
298 | minefield_admin[i] = 0xFFFE; /* used but no region starts here */ |
299 | minefield_admin[start + 1] = region_start % PAGESIZE; |
b191636d |
300 | |
301 | minefield_admin_hide(1); |
302 | |
32874aea |
303 | VirtualAlloc((char *) minefield_pages + region_start, size, |
304 | MEM_COMMIT, PAGE_READWRITE); |
305 | return (char *) minefield_pages + region_start; |
b191636d |
306 | } |
307 | |
32874aea |
308 | static void minefield_free(void *ptr) |
309 | { |
b191636d |
310 | int region_start, i, j; |
311 | |
312 | minefield_admin_hide(0); |
313 | |
32874aea |
314 | region_start = (char *) ptr - (char *) minefield_pages; |
b191636d |
315 | i = region_start / PAGESIZE; |
316 | if (i < 0 || i >= minefield_npages || |
32874aea |
317 | minefield_admin[i] != region_start % PAGESIZE) |
318 | minefield_bomb(); |
b191636d |
319 | for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) { |
32874aea |
320 | minefield_admin[j] = 0xFFFF; |
b191636d |
321 | } |
322 | |
32874aea |
323 | VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT); |
b191636d |
324 | |
325 | minefield_admin_hide(1); |
326 | } |
327 | |
32874aea |
328 | static int minefield_get_size(void *ptr) |
329 | { |
b191636d |
330 | int region_start, i, j; |
331 | |
332 | minefield_admin_hide(0); |
333 | |
32874aea |
334 | region_start = (char *) ptr - (char *) minefield_pages; |
b191636d |
335 | i = region_start / PAGESIZE; |
336 | if (i < 0 || i >= minefield_npages || |
32874aea |
337 | minefield_admin[i] != region_start % PAGESIZE) |
338 | minefield_bomb(); |
b191636d |
339 | for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++); |
340 | |
341 | minefield_admin_hide(1); |
342 | |
32874aea |
343 | return j * PAGESIZE - region_start; |
b191636d |
344 | } |
345 | |
32874aea |
346 | static void *minefield_c_malloc(size_t size) |
347 | { |
348 | if (!minefield_initialised) |
349 | minefield_init(); |
b191636d |
350 | return minefield_alloc(size); |
351 | } |
352 | |
32874aea |
353 | static void minefield_c_free(void *p) |
354 | { |
355 | if (!minefield_initialised) |
356 | minefield_init(); |
b191636d |
357 | minefield_free(p); |
358 | } |
359 | |
360 | /* |
361 | * realloc _always_ moves the chunk, for rapid detection of code |
362 | * that assumes it won't. |
363 | */ |
32874aea |
364 | static void *minefield_c_realloc(void *p, size_t size) |
365 | { |
b191636d |
366 | size_t oldsize; |
367 | void *q; |
32874aea |
368 | if (!minefield_initialised) |
369 | minefield_init(); |
b191636d |
370 | q = minefield_alloc(size); |
371 | oldsize = minefield_get_size(p); |
372 | memcpy(q, p, (oldsize < size ? oldsize : size)); |
373 | minefield_free(p); |
374 | return q; |
375 | } |
376 | |
32874aea |
377 | #endif /* MINEFIELD */ |
374330e2 |
378 | |
379 | #ifdef MALLOC_LOG |
380 | static FILE *fp = NULL; |
381 | |
d7da76ca |
382 | static char *mlog_file = NULL; |
383 | static int mlog_line = 0; |
384 | |
32874aea |
385 | void mlog(char *file, int line) |
386 | { |
d7da76ca |
387 | mlog_file = file; |
388 | mlog_line = line; |
c662dbc0 |
389 | if (!fp) { |
374330e2 |
390 | fp = fopen("putty_mem.log", "w"); |
c662dbc0 |
391 | setvbuf(fp, NULL, _IONBF, BUFSIZ); |
392 | } |
374330e2 |
393 | if (fp) |
32874aea |
394 | fprintf(fp, "%s:%d: ", file, line); |
374330e2 |
395 | } |
396 | #endif |
397 | |
32874aea |
398 | void *safemalloc(size_t size) |
399 | { |
b191636d |
400 | void *p; |
401 | #ifdef MINEFIELD |
32874aea |
402 | p = minefield_c_malloc(size); |
b191636d |
403 | #else |
32874aea |
404 | p = malloc(size); |
b191636d |
405 | #endif |
374330e2 |
406 | if (!p) { |
d7da76ca |
407 | char str[200]; |
408 | #ifdef MALLOC_LOG |
409 | sprintf(str, "Out of memory! (%s:%d, size=%d)", |
410 | mlog_file, mlog_line, size); |
411 | #else |
412 | strcpy(str, "Out of memory!"); |
413 | #endif |
414 | MessageBox(NULL, str, "PuTTY Fatal Error", |
374330e2 |
415 | MB_SYSTEMMODAL | MB_ICONERROR | MB_OK); |
416 | exit(1); |
417 | } |
418 | #ifdef MALLOC_LOG |
419 | if (fp) |
420 | fprintf(fp, "malloc(%d) returns %p\n", size, p); |
421 | #endif |
422 | return p; |
423 | } |
424 | |
32874aea |
425 | void *saferealloc(void *ptr, size_t size) |
426 | { |
374330e2 |
427 | void *p; |
b191636d |
428 | if (!ptr) { |
429 | #ifdef MINEFIELD |
32874aea |
430 | p = minefield_c_malloc(size); |
b191636d |
431 | #else |
32874aea |
432 | p = malloc(size); |
b191636d |
433 | #endif |
434 | } else { |
435 | #ifdef MINEFIELD |
32874aea |
436 | p = minefield_c_realloc(ptr, size); |
b191636d |
437 | #else |
32874aea |
438 | p = realloc(ptr, size); |
b191636d |
439 | #endif |
440 | } |
374330e2 |
441 | if (!p) { |
d7da76ca |
442 | char str[200]; |
443 | #ifdef MALLOC_LOG |
444 | sprintf(str, "Out of memory! (%s:%d, size=%d)", |
445 | mlog_file, mlog_line, size); |
446 | #else |
447 | strcpy(str, "Out of memory!"); |
448 | #endif |
449 | MessageBox(NULL, str, "PuTTY Fatal Error", |
374330e2 |
450 | MB_SYSTEMMODAL | MB_ICONERROR | MB_OK); |
451 | exit(1); |
452 | } |
453 | #ifdef MALLOC_LOG |
454 | if (fp) |
455 | fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p); |
456 | #endif |
457 | return p; |
458 | } |
459 | |
32874aea |
460 | void safefree(void *ptr) |
461 | { |
374330e2 |
462 | if (ptr) { |
463 | #ifdef MALLOC_LOG |
464 | if (fp) |
465 | fprintf(fp, "free(%p)\n", ptr); |
466 | #endif |
b191636d |
467 | #ifdef MINEFIELD |
32874aea |
468 | minefield_c_free(ptr); |
b191636d |
469 | #else |
32874aea |
470 | free(ptr); |
b191636d |
471 | #endif |
374330e2 |
472 | } |
473 | #ifdef MALLOC_LOG |
474 | else if (fp) |
475 | fprintf(fp, "freeing null pointer - no action taken\n"); |
476 | #endif |
477 | } |
c82bda52 |
478 | |
03f64569 |
479 | /* ---------------------------------------------------------------------- |
480 | * Debugging routines. |
481 | */ |
482 | |
c82bda52 |
483 | #ifdef DEBUG |
484 | static FILE *debug_fp = NULL; |
485 | static int debug_got_console = 0; |
486 | |
32874aea |
487 | static void dputs(char *buf) |
488 | { |
c82bda52 |
489 | DWORD dw; |
c82bda52 |
490 | |
491 | if (!debug_got_console) { |
492 | AllocConsole(); |
493 | debug_got_console = 1; |
494 | } |
495 | if (!debug_fp) { |
496 | debug_fp = fopen("debug.log", "w"); |
497 | } |
498 | |
32874aea |
499 | WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw, |
500 | NULL); |
c82bda52 |
501 | fputs(buf, debug_fp); |
502 | fflush(debug_fp); |
db9c0f86 |
503 | } |
504 | |
505 | |
32874aea |
506 | void dprintf(char *fmt, ...) |
507 | { |
db9c0f86 |
508 | char buf[2048]; |
509 | va_list ap; |
510 | |
511 | va_start(ap, fmt); |
512 | vsprintf(buf, fmt, ap); |
32874aea |
513 | dputs(buf); |
c82bda52 |
514 | va_end(ap); |
515 | } |
db9c0f86 |
516 | |
517 | |
32874aea |
518 | void debug_memdump(void *buf, int len, int L) |
519 | { |
db9c0f86 |
520 | int i; |
521 | unsigned char *p = buf; |
765c4200 |
522 | char foo[17]; |
db9c0f86 |
523 | if (L) { |
524 | int delta; |
32874aea |
525 | dprintf("\t%d (0x%x) bytes:\n", len, len); |
db9c0f86 |
526 | delta = 15 & (int) p; |
527 | p -= delta; |
528 | len += delta; |
529 | } |
530 | for (; 0 < len; p += 16, len -= 16) { |
32874aea |
531 | dputs(" "); |
532 | if (L) |
533 | dprintf("%p: ", p); |
534 | strcpy(foo, "................"); /* sixteen dots */ |
db9c0f86 |
535 | for (i = 0; i < 16 && i < len; ++i) { |
536 | if (&p[i] < (unsigned char *) buf) { |
32874aea |
537 | dputs(" "); /* 3 spaces */ |
765c4200 |
538 | foo[i] = ' '; |
db9c0f86 |
539 | } else { |
32874aea |
540 | dprintf("%c%02.2x", |
541 | &p[i] != (unsigned char *) buf |
542 | && i % 4 ? '.' : ' ', p[i] |
543 | ); |
765c4200 |
544 | if (p[i] >= ' ' && p[i] <= '~') |
32874aea |
545 | foo[i] = (char) p[i]; |
db9c0f86 |
546 | } |
547 | } |
765c4200 |
548 | foo[i] = '\0'; |
32874aea |
549 | dprintf("%*s%s\n", (16 - i) * 3 + 2, "", foo); |
db9c0f86 |
550 | } |
551 | } |
552 | |
32874aea |
553 | #endif /* def DEBUG */ |