From 4713b1b294d8eacbf42b65aa794acb15bb18edee Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Sun, 20 Jan 2013 22:59:58 +0000 Subject: [PATCH] * malloc_wrapper.cc: Change 'use_internal_malloc' to 'use_internal' throughout. (export_malloc_called): Delete. (internal_malloc_determined): New variable. (malloc_init): Control calculation of internal/external malloc based on 'internal_malloc_determined'. Use import_address() to determine if malloc in user_data is ours or not. * miscfuncs.cc (thread_wrapper): Make static. (__import_address): Define new function. * miscfuncs.h (import_address): New define. (__import_address): Declare new function. --- winsup/cygwin/ChangeLog | 14 ++++++++++ winsup/cygwin/malloc_wrapper.cc | 49 ++++++++++++++------------------- winsup/cygwin/miscfuncs.cc | 13 +++++++-- winsup/cygwin/miscfuncs.h | 7 +++-- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 908545c54..2c5b7e3a3 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,17 @@ +2013-01-20 Christopher Faylor + + * malloc_wrapper.cc: Change 'use_internal_malloc' to 'use_internal' + throughout. + (export_malloc_called): Delete. + (internal_malloc_determined): New variable. + (malloc_init): Control calculation of internal/external malloc based on + 'internal_malloc_determined'. Use import_address() to determine if + malloc in user_data is ours or not. + * miscfuncs.cc (thread_wrapper): Make static. + (__import_address): Define new function. + * miscfuncs.h (import_address): New define. + (__import_address): Declare new function. + 2013-01-20 Christopher Faylor * sigproc.cc (sig_dispatch_pending): Add correct regparm attributes to diff --git a/winsup/cygwin/malloc_wrapper.cc b/winsup/cygwin/malloc_wrapper.cc index 86b74d564..241766f64 100644 --- a/winsup/cygwin/malloc_wrapper.cc +++ b/winsup/cygwin/malloc_wrapper.cc @@ -1,10 +1,7 @@ /* malloc_wrapper.cc Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, - 2006, 2007 Red Hat, Inc. - - Originally written by Steve Chamberlain of Cygnus Support - sac@cygnus.com + 2006, 2007, 2013 Red Hat, Inc. This file is part of Cygwin. @@ -19,6 +16,7 @@ details. */ #include "fhandler.h" #include "dtable.h" #include "perprocess.h" +#include "miscfuncs.h" #include "cygmalloc.h" #ifndef MALLOC_DEBUG #include @@ -31,8 +29,8 @@ extern "C" struct mallinfo dlmallinfo (); problems if malloced on our heap and free'd on theirs. */ -static int export_malloc_called; -static int use_internal_malloc = 1; +static bool use_internal = true; +static bool internal_malloc_determined; /* These routines are used by the application if it doesn't provide its own malloc. */ @@ -41,7 +39,7 @@ extern "C" void free (void *p) { malloc_printf ("(%p), called by %p", p, __builtin_return_address (0)); - if (!use_internal_malloc) + if (!use_internal) user_data->free (p); else { @@ -55,8 +53,7 @@ extern "C" void * malloc (size_t size) { void *res; - export_malloc_called = 1; - if (!use_internal_malloc) + if (!use_internal) res = user_data->malloc (size); else { @@ -72,7 +69,7 @@ extern "C" void * realloc (void *p, size_t size) { void *res; - if (!use_internal_malloc) + if (!use_internal) res = user_data->realloc (p, size); else { @@ -99,7 +96,7 @@ extern "C" void * calloc (size_t nmemb, size_t size) { void *res; - if (!use_internal_malloc) + if (!use_internal) res = user_data->calloc (nmemb, size); else { @@ -117,7 +114,7 @@ posix_memalign (void **memptr, size_t alignment, size_t bytes) save_errno save; void *res; - if (!use_internal_malloc) + if (!use_internal) return ENOSYS; if ((alignment & (alignment - 1)) != 0) return EINVAL; @@ -135,7 +132,7 @@ extern "C" void * memalign (size_t alignment, size_t bytes) { void *res; - if (!use_internal_malloc) + if (!use_internal) { set_errno (ENOSYS); res = NULL; @@ -154,7 +151,7 @@ extern "C" void * valloc (size_t bytes) { void *res; - if (!use_internal_malloc) + if (!use_internal) { set_errno (ENOSYS); res = NULL; @@ -173,7 +170,7 @@ extern "C" size_t malloc_usable_size (void *p) { size_t res; - if (!use_internal_malloc) + if (!use_internal) { set_errno (ENOSYS); res = 0; @@ -192,7 +189,7 @@ extern "C" int malloc_trim (size_t pad) { size_t res; - if (!use_internal_malloc) + if (!use_internal) { set_errno (ENOSYS); res = 0; @@ -211,7 +208,7 @@ extern "C" int mallopt (int p, int v) { int res; - if (!use_internal_malloc) + if (!use_internal) { set_errno (ENOSYS); res = 0; @@ -229,7 +226,7 @@ mallopt (int p, int v) extern "C" void malloc_stats () { - if (!use_internal_malloc) + if (!use_internal) set_errno (ENOSYS); else { @@ -243,7 +240,7 @@ extern "C" struct mallinfo mallinfo () { struct mallinfo m; - if (!use_internal_malloc) + if (!use_internal) set_errno (ENOSYS); else { @@ -284,16 +281,12 @@ malloc_init () calls to malloc/free/realloc to application provided. This may happen if some other dll calls cygwin's malloc, but main code provides its own malloc */ - if (!in_forkee) + if (!internal_malloc_determined) { - user_data->free (user_data->malloc (16)); - if (export_malloc_called) - malloc_printf ("using internal malloc"); - else - { - use_internal_malloc = 0; - malloc_printf ("using external malloc"); - } + extern void *_sigfe_malloc; + use_internal = import_address (user_data->malloc) == &_sigfe_malloc; + malloc_printf ("using %s malloc", use_internal ? "internal" : "external"); + internal_malloc_determined = true; } #endif } diff --git a/winsup/cygwin/miscfuncs.cc b/winsup/cygwin/miscfuncs.cc index 001461620..2d3f52c3a 100644 --- a/winsup/cygwin/miscfuncs.cc +++ b/winsup/cygwin/miscfuncs.cc @@ -1,7 +1,7 @@ /* miscfuncs.cc: misc funcs that don't belong anywhere else Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, - 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Red Hat, Inc. + 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Red Hat, Inc. This file is part of Cygwin. @@ -433,6 +433,15 @@ slashify (const char *src, char *dst, bool trailing_slash_p) *dst++ = 0; } +/* Return an address from the import jmp table of main program. */ +void * __attribute__ ((regparm (1))) +__import_address (void *imp) +{ + const char *ptr = (const char *) imp; + const uintptr_t *jmpto = (uintptr_t *) *((uintptr_t *) (ptr + 2)); + return (void *) *jmpto; +} + /* CygwinCreateThread. Replacement function for CreateThread. What we do here is to remove @@ -448,7 +457,7 @@ struct thread_wrapper_arg PBYTE stacklimit; }; -DWORD WINAPI +static DWORD WINAPI thread_wrapper (VOID *arg) { /* Just plain paranoia. */ diff --git a/winsup/cygwin/miscfuncs.h b/winsup/cygwin/miscfuncs.h index 355fa103c..7995475d8 100644 --- a/winsup/cygwin/miscfuncs.h +++ b/winsup/cygwin/miscfuncs.h @@ -1,7 +1,7 @@ /* miscfuncs.h: main Cygwin header file. - Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, - 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Red Hat, Inc. + Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, + 2007, 2008, 2009, 2010, 2011, 2012, 2013 Red Hat, Inc. This file is part of Cygwin. @@ -26,6 +26,9 @@ BOOL WINAPI WritePipeOverlapped (HANDLE h, LPCVOID buf, DWORD len, extern "C" void yield (); +#define import_address(x) __import_address ((void *)(x)) +void * __stdcall __attribute__ ((regparm (1))) __import_address (void *); + void backslashify (const char *, char *, bool); void slashify (const char *, char *, bool); #define isslash(c) ((c) == '/')