374330e2 |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include "putty.h" |
5 | |
b191636d |
6 | /* |
7 | * My own versions of malloc, realloc and free. Because I want |
8 | * malloc and realloc to bomb out and exit the program if they run |
9 | * out of memory, realloc to reliably call malloc if passed a NULL |
10 | * pointer, and free to reliably do nothing if passed a NULL |
11 | * pointer. We can also put trace printouts in, if we need to; and |
12 | * we can also replace the allocator with an ElectricFence-like |
13 | * one. |
14 | */ |
15 | |
16 | #ifdef MINEFIELD |
17 | /* |
18 | * Minefield - a Windows equivalent for Electric Fence |
19 | */ |
20 | |
21 | #define PAGESIZE 4096 |
22 | |
23 | /* |
24 | * Design: |
25 | * |
26 | * We start by reserving as much virtual address space as Windows |
27 | * will sensibly (or not sensibly) let us have. We flag it all as |
28 | * invalid memory. |
29 | * |
30 | * Any allocation attempt is satisfied by committing one or more |
31 | * pages, with an uncommitted page on either side. The returned |
32 | * memory region is jammed up against the _end_ of the pages. |
33 | * |
34 | * Freeing anything causes instantaneous decommitment of the pages |
35 | * involved, so stale pointers are caught as soon as possible. |
36 | */ |
37 | |
38 | static int minefield_initialised = 0; |
39 | static void *minefield_region = NULL; |
40 | static long minefield_size = 0; |
41 | static long minefield_npages = 0; |
42 | static long minefield_curpos = 0; |
43 | static unsigned short *minefield_admin = NULL; |
44 | static void *minefield_pages = NULL; |
45 | |
46 | static void minefield_admin_hide(int hide) { |
47 | int access = hide ? PAGE_NOACCESS : PAGE_READWRITE; |
48 | VirtualProtect(minefield_admin, minefield_npages*2, access, NULL); |
49 | } |
50 | |
51 | static void minefield_init(void) { |
52 | int size; |
53 | int admin_size; |
54 | int i; |
55 | |
56 | for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) &~ 0xFFF) { |
b191636d |
57 | minefield_region = VirtualAlloc(NULL, size, |
58 | MEM_RESERVE, PAGE_NOACCESS); |
59 | if (minefield_region) |
60 | break; |
61 | } |
62 | minefield_size = size; |
b191636d |
63 | |
64 | /* |
65 | * Firstly, allocate a section of that to be the admin block. |
66 | * We'll need a two-byte field for each page. |
67 | */ |
68 | minefield_admin = minefield_region; |
69 | minefield_npages = minefield_size / PAGESIZE; |
70 | admin_size = (minefield_npages * 2 + PAGESIZE-1) &~ (PAGESIZE-1); |
71 | minefield_npages = (minefield_size - admin_size) / PAGESIZE; |
72 | minefield_pages = (char *)minefield_region + admin_size; |
b191636d |
73 | |
74 | /* |
75 | * Commit the admin region. |
76 | */ |
77 | VirtualAlloc(minefield_admin, minefield_npages * 2, |
78 | MEM_COMMIT, PAGE_READWRITE); |
79 | |
80 | /* |
81 | * Mark all pages as unused (0xFFFF). |
82 | */ |
83 | for (i = 0; i < minefield_npages; i++) |
84 | minefield_admin[i] = 0xFFFF; |
85 | |
86 | /* |
87 | * Hide the admin region. |
88 | */ |
89 | minefield_admin_hide(1); |
90 | |
91 | minefield_initialised = 1; |
92 | } |
93 | |
94 | static void minefield_bomb(void) { |
95 | div(1, *(int*)minefield_pages); |
96 | } |
97 | |
98 | static void *minefield_alloc(int size) { |
99 | int npages; |
100 | int pos, lim, region_end, region_start; |
101 | int start; |
102 | int i; |
103 | |
104 | npages = (size + PAGESIZE-1) / PAGESIZE; |
105 | |
106 | minefield_admin_hide(0); |
107 | |
108 | /* |
109 | * Search from current position until we find a contiguous |
110 | * bunch of npages+2 unused pages. |
111 | */ |
112 | pos = minefield_curpos; |
113 | lim = minefield_npages; |
114 | while (1) { |
115 | /* Skip over used pages. */ |
116 | while (pos < lim && minefield_admin[pos] != 0xFFFF) |
117 | pos++; |
118 | /* Count unused pages. */ |
119 | start = pos; |
120 | while (pos < lim && pos - start < npages+2 && |
121 | minefield_admin[pos] == 0xFFFF) |
122 | pos++; |
123 | if (pos - start == npages+2) |
124 | break; |
125 | /* If we've reached the limit, reset the limit or stop. */ |
126 | if (pos >= lim) { |
127 | if (lim == minefield_npages) { |
128 | /* go round and start again at zero */ |
129 | lim = minefield_curpos; |
130 | pos = 0; |
131 | } else { |
132 | minefield_admin_hide(1); |
133 | return NULL; |
134 | } |
135 | } |
136 | } |
137 | |
138 | minefield_curpos = pos-1; |
139 | |
140 | /* |
141 | * We have npages+2 unused pages starting at start. We leave |
142 | * the first and last of these alone and use the rest. |
143 | */ |
144 | region_end = (start + npages+1) * PAGESIZE; |
145 | region_start = region_end - size; |
146 | /* FIXME: could align here if we wanted */ |
147 | |
148 | /* |
149 | * Update the admin region. |
150 | */ |
151 | for (i = start + 2; i < start + npages-1; i++) |
152 | minefield_admin[i] = 0xFFFE; /* used but no region starts here */ |
153 | minefield_admin[start+1] = region_start % PAGESIZE; |
154 | |
155 | minefield_admin_hide(1); |
156 | |
157 | VirtualAlloc((char *)minefield_pages + region_start, size, |
158 | MEM_COMMIT, PAGE_READWRITE); |
159 | return (char *)minefield_pages + region_start; |
160 | } |
161 | |
162 | static void minefield_free(void *ptr) { |
163 | int region_start, i, j; |
164 | |
165 | minefield_admin_hide(0); |
166 | |
167 | region_start = (char *)ptr - (char *)minefield_pages; |
168 | i = region_start / PAGESIZE; |
169 | if (i < 0 || i >= minefield_npages || |
170 | minefield_admin[i] != region_start % PAGESIZE) |
171 | minefield_bomb(); |
172 | for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) { |
173 | minefield_admin[j] = 0xFFFF; |
174 | } |
175 | |
176 | VirtualFree(ptr, j*PAGESIZE - region_start, MEM_DECOMMIT); |
177 | |
178 | minefield_admin_hide(1); |
179 | } |
180 | |
181 | static int minefield_get_size(void *ptr) { |
182 | int region_start, i, j; |
183 | |
184 | minefield_admin_hide(0); |
185 | |
186 | region_start = (char *)ptr - (char *)minefield_pages; |
187 | i = region_start / PAGESIZE; |
188 | if (i < 0 || i >= minefield_npages || |
189 | minefield_admin[i] != region_start % PAGESIZE) |
190 | minefield_bomb(); |
191 | for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++); |
192 | |
193 | minefield_admin_hide(1); |
194 | |
195 | return j*PAGESIZE - region_start; |
196 | } |
197 | |
198 | static void *minefield_c_malloc(size_t size) { |
199 | if (!minefield_initialised) minefield_init(); |
200 | return minefield_alloc(size); |
201 | } |
202 | |
203 | static void minefield_c_free(void *p) { |
204 | if (!minefield_initialised) minefield_init(); |
205 | minefield_free(p); |
206 | } |
207 | |
208 | /* |
209 | * realloc _always_ moves the chunk, for rapid detection of code |
210 | * that assumes it won't. |
211 | */ |
212 | static void *minefield_c_realloc(void *p, size_t size) { |
213 | size_t oldsize; |
214 | void *q; |
215 | if (!minefield_initialised) minefield_init(); |
216 | q = minefield_alloc(size); |
217 | oldsize = minefield_get_size(p); |
218 | memcpy(q, p, (oldsize < size ? oldsize : size)); |
219 | minefield_free(p); |
220 | return q; |
221 | } |
222 | |
223 | #endif /* MINEFIELD */ |
374330e2 |
224 | |
225 | #ifdef MALLOC_LOG |
226 | static FILE *fp = NULL; |
227 | |
228 | void mlog(char *file, int line) { |
c662dbc0 |
229 | if (!fp) { |
374330e2 |
230 | fp = fopen("putty_mem.log", "w"); |
c662dbc0 |
231 | setvbuf(fp, NULL, _IONBF, BUFSIZ); |
232 | } |
374330e2 |
233 | if (fp) |
234 | fprintf (fp, "%s:%d: ", file, line); |
235 | } |
236 | #endif |
237 | |
238 | void *safemalloc(size_t size) { |
b191636d |
239 | void *p; |
240 | #ifdef MINEFIELD |
241 | p = minefield_c_malloc (size); |
242 | #else |
243 | p = malloc (size); |
244 | #endif |
374330e2 |
245 | if (!p) { |
246 | MessageBox(NULL, "Out of memory!", "PuTTY Fatal Error", |
247 | MB_SYSTEMMODAL | MB_ICONERROR | MB_OK); |
248 | exit(1); |
249 | } |
250 | #ifdef MALLOC_LOG |
251 | if (fp) |
252 | fprintf(fp, "malloc(%d) returns %p\n", size, p); |
253 | #endif |
254 | return p; |
255 | } |
256 | |
257 | void *saferealloc(void *ptr, size_t size) { |
258 | void *p; |
b191636d |
259 | if (!ptr) { |
260 | #ifdef MINEFIELD |
261 | p = minefield_c_malloc (size); |
262 | #else |
374330e2 |
263 | p = malloc (size); |
b191636d |
264 | #endif |
265 | } else { |
266 | #ifdef MINEFIELD |
267 | p = minefield_c_realloc (ptr, size); |
268 | #else |
374330e2 |
269 | p = realloc (ptr, size); |
b191636d |
270 | #endif |
271 | } |
374330e2 |
272 | if (!p) { |
273 | MessageBox(NULL, "Out of memory!", "PuTTY Fatal Error", |
274 | MB_SYSTEMMODAL | MB_ICONERROR | MB_OK); |
275 | exit(1); |
276 | } |
277 | #ifdef MALLOC_LOG |
278 | if (fp) |
279 | fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p); |
280 | #endif |
281 | return p; |
282 | } |
283 | |
284 | void safefree(void *ptr) { |
285 | if (ptr) { |
286 | #ifdef MALLOC_LOG |
287 | if (fp) |
288 | fprintf(fp, "free(%p)\n", ptr); |
289 | #endif |
b191636d |
290 | #ifdef MINEFIELD |
291 | minefield_c_free (ptr); |
292 | #else |
374330e2 |
293 | free (ptr); |
b191636d |
294 | #endif |
374330e2 |
295 | } |
296 | #ifdef MALLOC_LOG |
297 | else if (fp) |
298 | fprintf(fp, "freeing null pointer - no action taken\n"); |
299 | #endif |
300 | } |
c82bda52 |
301 | |
302 | #ifdef DEBUG |
303 | static FILE *debug_fp = NULL; |
304 | static int debug_got_console = 0; |
305 | |
306 | void dprintf(char *fmt, ...) { |
307 | char buf[2048]; |
308 | DWORD dw; |
309 | va_list ap; |
310 | |
311 | if (!debug_got_console) { |
312 | AllocConsole(); |
313 | debug_got_console = 1; |
314 | } |
315 | if (!debug_fp) { |
316 | debug_fp = fopen("debug.log", "w"); |
317 | } |
318 | |
319 | va_start(ap, fmt); |
320 | vsprintf(buf, fmt, ap); |
321 | WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw, NULL); |
322 | fputs(buf, debug_fp); |
323 | fflush(debug_fp); |
324 | va_end(ap); |
325 | } |
326 | #endif |