1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
/* cygheap.cc: Cygwin heap manager.
Copyright 2000, 2001, 2002, 2003, 2004, 2005 Red Hat, Inc.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "cygerrno.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "child_info.h"
#include "heap.h"
#include "sync.h"
#include "sigproc.h"
init_cygheap NO_COPY *cygheap;
void NO_COPY *cygheap_max;
static NO_COPY muto *cygheap_protect;
static NO_COPY DWORD reserve_sz;
struct cygheap_entry
{
int type;
struct cygheap_entry *next;
char data[0];
};
#define NBUCKETS (sizeof (cygheap->buckets) / sizeof (cygheap->buckets[0]))
#define N0 ((_cmalloc_entry *) NULL)
#define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - (unsigned) (N0->data)))
#define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
#define MVMAP_OPTIONS (FILE_MAP_WRITE)
extern "C" {
static void __stdcall _cfree (void *ptr) __attribute__((regparm(1)));
}
static void
init_cheap ()
{
#ifndef DEBUGGING
reserve_sz = CYGHEAPSIZE;
#else
char buf[80];
DWORD initial_sz = 0;
if (!GetEnvironmentVariable ("CYGWIN_HEAPSIZE", buf, sizeof buf - 1))
initial_sz = reserve_sz = CYGHEAPSIZE;
else
{
initial_sz = reserve_sz = atoi (buf);
small_printf ("using cygheap size %d\n", reserve_sz);
}
#endif
do
if ((cygheap = (init_cygheap *) VirtualAlloc ((void *) &_cygheap_start,
reserve_sz, MEM_RESERVE,
PAGE_NOACCESS)))
break;
while ((reserve_sz -= 2 * (1024 * 1024)) >= CYGHEAPSIZE_MIN);
#ifdef DEBUGGING
if (reserve_sz != initial_sz)
small_printf ("reset initial cygheap size to %u\n", reserve_sz);
#endif
if (!cygheap)
{
MEMORY_BASIC_INFORMATION m;
if (!VirtualQuery ((LPCVOID) &_cygheap_start, &m, sizeof m))
system_printf ("couldn't get memory info, %E");
system_printf ("Couldn't reserve %d bytes of space for cygwin's heap, %E",
reserve_sz);
api_fatal ("AllocationBase %p, BaseAddress %p, RegionSize %p, State %p\n",
m.AllocationBase, m.BaseAddress, m.RegionSize, m.State);
}
cygheap_max = cygheap;
}
static void dup_now (void *, child_info *, unsigned) __attribute__ ((regparm(3)));
static void
dup_now (void *newcygheap, child_info *ci, unsigned n)
{
if (!VirtualAlloc (newcygheap, n, MEM_COMMIT, PAGE_READWRITE))
api_fatal ("couldn't allocate new cygwin heap %p, %d for child, %E",
newcygheap, n);
memcpy (newcygheap, cygheap, n);
}
void *__stdcall
cygheap_setup_for_child (child_info *ci, bool dup_later)
{
void *newcygheap;
cygheap_protect->acquire ();
unsigned n = (char *) cygheap_max - (char *) cygheap;
unsigned size = reserve_sz;
if (size < n)
size = n + (128 * 1024);
ci->cygheap_h = CreateFileMapping (INVALID_HANDLE_VALUE, &sec_none,
CFMAP_OPTIONS, 0, size, NULL);
if (!ci->cygheap_h)
api_fatal ("Couldn't create heap for child, size %d, %E", size);
newcygheap = MapViewOfFileEx (ci->cygheap_h, MVMAP_OPTIONS, 0, 0, 0, NULL);
if (!newcygheap)
api_fatal ("couldn't map space for new cygheap, %E");
ProtectHandle1INH (ci->cygheap_h, passed_cygheap_h);
if (!dup_later)
dup_now (newcygheap, ci, n);
cygheap_protect->release ();
ci->cygheap = cygheap;
ci->cygheap_max = cygheap_max;
ci->cygheap_reserve_sz = size;
return newcygheap;
}
void __stdcall
cygheap_setup_for_child_cleanup (void *newcygheap, child_info *ci,
bool dup_it_now)
{
if (dup_it_now)
{
/* NOTE: There is an assumption here that cygheap_max has not changed
between the time that cygheap_setup_for_child was called and now.
Make sure that this is a correct assumption. */
cygheap_protect->acquire ();
dup_now (newcygheap, ci, (char *) cygheap_max - (char *) cygheap);
cygheap_protect->release ();
}
UnmapViewOfFile (newcygheap);
ForceCloseHandle1 (ci->cygheap_h, passed_cygheap_h);
}
/* Called by fork or spawn to reallocate cygwin heap */
void __stdcall
cygheap_fixup_in_child (bool execed)
{
cygheap = child_proc_info->cygheap;
cygheap_max = child_proc_info->cygheap_max;
void *addr = !wincap.map_view_of_file_ex_sucks () ? cygheap : NULL;
void *newaddr;
newaddr = MapViewOfFileEx (child_proc_info->cygheap_h, MVMAP_OPTIONS, 0, 0, 0, addr);
reserve_sz = child_proc_info->cygheap_reserve_sz;
if (newaddr != cygheap)
{
if (!newaddr)
newaddr = MapViewOfFileEx (child_proc_info->cygheap_h, MVMAP_OPTIONS, 0, 0, 0, NULL);
DWORD n = (DWORD) cygheap_max - (DWORD) cygheap;
/* Reserve cygwin heap in same spot as parent */
if (!VirtualAlloc (cygheap, reserve_sz, MEM_RESERVE, PAGE_NOACCESS))
{
MEMORY_BASIC_INFORMATION m;
memset (&m, 0, sizeof m);
if (!VirtualQuery ((LPCVOID) cygheap, &m, sizeof m))
system_printf ("couldn't get memory info, %E");
system_printf ("Couldn't reserve %d bytes of space for cygwin's heap (%p <%p>) in child, %E",
reserve_sz, cygheap, newaddr);
api_fatal ("m.AllocationBase %p, m.BaseAddress %p, m.RegionSize %p, m.State %p\n",
m.AllocationBase, m.BaseAddress, m.RegionSize, m.State);
}
/* Allocate same amount of memory as parent */
if (!VirtualAlloc (cygheap, n, MEM_COMMIT, PAGE_READWRITE))
api_fatal ("Couldn't allocate space for child's heap %p, size %d, %E",
cygheap, n);
memcpy (cygheap, newaddr, n);
UnmapViewOfFile (newaddr);
}
ForceCloseHandle1 (child_proc_info->cygheap_h, passed_cygheap_h);
cygheap_init ();
debug_fixup_after_fork_exec ();
if (execed)
{
cygheap->hooks.next = NULL;
cygheap->user_heap.base = NULL; /* We can allocate the heap anywhere */
/* Walk the allocated memory chain looking for orphaned memory from
previous execs */
for (_cmalloc_entry *rvc = cygheap->chain; rvc; rvc = rvc->prev)
{
cygheap_entry *ce = (cygheap_entry *) rvc->data;
if (!rvc->ptr || rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
continue;
else if (ce->type < HEAP_1_MAX)
ce->type += HEAP_1_MAX; /* Mark for freeing after next exec */
else
_cfree (ce); /* Marked by parent for freeing in child */
}
}
}
void
init_cygheap::close_ctty ()
{
debug_printf ("closing cygheap->ctty %p", cygheap->ctty);
#ifdef NEWVFORK
int usecount = cygheap->ctty->usecount;
#endif
cygheap->ctty->close ();
#ifndef NEWVFORK
cygheap->ctty = NULL;
#else // FIXME: This code ain't right
if (cygheap->ctty_on_hold == cygheap->ctty)
cygheap->ctty_on_hold = NULL;
if (usecount == 1)
{
cygheap->ctty = NULL;
debug_printf ("setting cygheap->ctty to NULL");
}
#endif
}
#define pagetrunc(x) ((void *) (((DWORD) (x)) & ~(4096 - 1)))
static void *__stdcall
_csbrk (int sbs)
{
void *prebrk = cygheap_max;
void *prebrka = pagetrunc (prebrk);
cygheap_max = (char *) cygheap_max + sbs;
if (!sbs || (prebrk != prebrka && prebrka == pagetrunc (cygheap_max)))
/* nothing to do */;
else if (!VirtualAlloc (prebrk, (DWORD) sbs, MEM_COMMIT, PAGE_READWRITE))
{
#ifdef DEBUGGING
system_printf ("couldn't commit memory for cygwin heap, prebrk %p, size %d, heapsize now %d, max heap size %u, %E",
prebrk, sbs, (char *) cygheap_max - (char *) cygheap,
reserve_sz);
#else
malloc_printf ("couldn't commit memory for cygwin heap, prebrk %p, size %d, heapsize now %d, max heap size %u, %E",
prebrk, sbs, (char *) cygheap_max - (char *) cygheap,
reserve_sz);
#endif
__seterrno ();
cygheap_max = (char *) cygheap_max - sbs;
return NULL;
}
return prebrk;
}
extern "C" void __stdcall
cygheap_init ()
{
new_muto (cygheap_protect);
if (!cygheap)
{
init_cheap ();
(void) _csbrk (sizeof (*cygheap));
}
if (!cygheap->fdtab)
cygheap->fdtab.init ();
if (!cygheap->sigs)
sigalloc ();
if (!cygheap->shared_prefix)
cygheap->shared_prefix = cstrdup (
wincap.has_terminal_services ()
&& (set_process_privilege (SE_CREATE_GLOBAL_PRIV, true) >= 0
|| GetLastError () == ERROR_NO_SUCH_PRIVILEGE)
? "Global\\" : "");
}
/* Copyright (C) 1997, 2000 DJ Delorie */
static void *_cmalloc (unsigned size) __attribute ((regparm(1)));
static void *__stdcall _crealloc (void *ptr, unsigned size) __attribute ((regparm(2)));
static void *__stdcall
_cmalloc (unsigned size)
{
_cmalloc_entry *rvc;
unsigned b, sz;
/* Calculate "bit bucket" and size as a power of two. */
for (b = 3, sz = 8; sz && sz < size; b++, sz <<= 1)
continue;
cygheap_protect->acquire ();
if (cygheap->buckets[b])
{
rvc = (_cmalloc_entry *) cygheap->buckets[b];
cygheap->buckets[b] = rvc->ptr;
rvc->b = b;
}
else
{
rvc = (_cmalloc_entry *) _csbrk (sz + sizeof (_cmalloc_entry));
if (!rvc)
{
cygheap_protect->release ();
return NULL;
}
rvc->b = b;
rvc->prev = cygheap->chain;
cygheap->chain = rvc;
}
cygheap_protect->release ();
return rvc->data;
}
static void __stdcall
_cfree (void *ptr)
{
cygheap_protect->acquire ();
_cmalloc_entry *rvc = to_cmalloc (ptr);
DWORD b = rvc->b;
rvc->ptr = cygheap->buckets[b];
cygheap->buckets[b] = (char *) rvc;
cygheap_protect->release ();
}
static void *__stdcall
_crealloc (void *ptr, unsigned size)
{
void *newptr;
if (ptr == NULL)
newptr = _cmalloc (size);
else
{
unsigned oldsize = 1 << to_cmalloc (ptr)->b;
if (size <= oldsize)
return ptr;
newptr = _cmalloc (size);
memcpy (newptr, ptr, oldsize);
_cfree (ptr);
}
return newptr;
}
/* End Copyright (C) 1997 DJ Delorie */
#define sizeof_cygheap(n) ((n) + sizeof (cygheap_entry))
#define N ((cygheap_entry *) NULL)
#define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
inline static void *
creturn (cygheap_types x, cygheap_entry * c, unsigned len)
{
if (!c)
{
set_errno (ENOMEM);
return NULL;
}
c->type = x;
char *cend = ((char *) c + sizeof (*c) + len);
if (cygheap_max < cend)
cygheap_max = cend;
MALLOC_CHECK;
return (void *) c->data;
}
extern "C" void *__stdcall
cmalloc (cygheap_types x, DWORD n)
{
cygheap_entry *c;
MALLOC_CHECK;
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
if (!c)
{
system_printf ("cmalloc returned NULL");
#ifdef DEBUGGING
try_to_debug ();
#endif
}
return creturn (x, c, n);
}
extern "C" void *__stdcall
crealloc (void *s, DWORD n)
{
MALLOC_CHECK;
if (s == NULL)
return cmalloc (HEAP_STR, n); // kludge
assert (!inheap (s));
cygheap_entry *c = tocygheap (s);
cygheap_types t = (cygheap_types) c->type;
c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
if (!c)
system_printf ("crealloc returned NULL");
return creturn (t, c, n);
}
extern "C" void __stdcall
cfree (void *s)
{
assert (!inheap (s));
(void) _cfree (tocygheap (s));
MALLOC_CHECK;
}
extern "C" void __stdcall
cfree_and_set (char *&s, char *what)
{
if (s && s != almost_null)
cfree (s);
s = what;
}
extern "C" void *__stdcall
ccalloc (cygheap_types x, DWORD n, DWORD size)
{
cygheap_entry *c;
MALLOC_CHECK;
n *= size;
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
if (c)
memset (c->data, 0, n);
if (!c)
system_printf ("ccalloc returned NULL");
return creturn (x, c, n);
}
extern "C" char *__stdcall
cstrdup (const char *s)
{
MALLOC_CHECK;
char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
if (!p)
return NULL;
strcpy (p, s);
MALLOC_CHECK;
return p;
}
extern "C" char *__stdcall
cstrdup1 (const char *s)
{
MALLOC_CHECK;
char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
if (!p)
return NULL;
strcpy (p, s);
MALLOC_CHECK;
return p;
}
void
cygheap_root::set (const char *posix, const char *native)
{
if (*posix == '/' && posix[1] == '\0')
{
if (m)
{
cfree (m);
m = NULL;
}
return;
}
if (!m)
m = (struct cygheap_root_mount_info *) ccalloc (HEAP_MOUNT, 1, sizeof (*m));
strcpy (m->posix_path, posix);
m->posix_pathlen = strlen (posix);
if (m->posix_pathlen >= 1 && m->posix_path[m->posix_pathlen - 1] == '/')
m->posix_path[--m->posix_pathlen] = '\0';
strcpy (m->native_path, native);
m->native_pathlen = strlen (native);
if (m->native_pathlen >= 1 && m->native_path[m->native_pathlen - 1] == '\\')
m->native_path[--m->native_pathlen] = '\0';
}
cygheap_user::~cygheap_user ()
{
#if 0
if (pname)
cfree (pname);
if (plogsrv)
cfree (plogsrv - 2);
if (pdomain)
cfree (pdomain);
if (psid)
cfree (psid);
#endif
}
void
cygheap_user::set_name (const char *new_name)
{
bool allocated = !!pname;
if (allocated)
{
if (strcasematch (new_name, pname))
return;
cfree (pname);
}
pname = cstrdup (new_name ? new_name : "");
if (!allocated)
return; /* Initializing. Don't bother with other stuff. */
cfree_and_set (homedrive);
cfree_and_set (homepath);
cfree_and_set (plogsrv);
cfree_and_set (pdomain);
cfree_and_set (pwinname);
}
|