2004-01-14 23:45:37 +08:00
|
|
|
/* cygtls.cc
|
2003-12-24 00:43:45 +08:00
|
|
|
|
2012-02-27 19:55:27 +08:00
|
|
|
Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
|
|
|
|
2012 Red Hat, Inc.
|
2003-12-24 00:43:45 +08:00
|
|
|
|
|
|
|
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"
|
2005-07-03 10:40:30 +08:00
|
|
|
#define USE_SYS_TYPES_FD_SET
|
2003-12-24 00:43:45 +08:00
|
|
|
#include "cygtls.h"
|
|
|
|
#include <syslog.h>
|
2010-12-29 14:33:30 +08:00
|
|
|
#include <stdlib.h>
|
2004-01-14 23:45:37 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2004-01-19 13:46:54 +08:00
|
|
|
#include "sigproc.h"
|
2010-02-28 23:54:25 +08:00
|
|
|
#include "exception.h"
|
2004-01-14 23:45:37 +08:00
|
|
|
|
|
|
|
class sentry
|
|
|
|
{
|
2005-04-05 12:31:00 +08:00
|
|
|
static muto lock;
|
2004-01-14 23:45:37 +08:00
|
|
|
int destroy;
|
|
|
|
public:
|
|
|
|
void init ();
|
2005-04-05 12:31:00 +08:00
|
|
|
bool acquired () {return lock.acquired ();}
|
2004-01-14 23:45:37 +08:00
|
|
|
sentry () {destroy = 0;}
|
2005-04-05 12:31:00 +08:00
|
|
|
sentry (DWORD wait) {destroy = lock.acquire (wait);}
|
|
|
|
~sentry () {if (destroy) lock.release ();}
|
2004-02-12 11:01:58 +08:00
|
|
|
friend void _cygtls::init ();
|
2004-01-14 23:45:37 +08:00
|
|
|
};
|
|
|
|
|
2005-04-05 12:31:00 +08:00
|
|
|
muto NO_COPY sentry::lock;
|
2003-12-24 00:43:45 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
static size_t NO_COPY nthreads;
|
2003-12-24 00:43:45 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
#define THREADLIST_CHUNK 256
|
|
|
|
|
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::init ()
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
|
|
|
if (cygheap->threadlist)
|
|
|
|
memset (cygheap->threadlist, 0, cygheap->sthreads * sizeof (cygheap->threadlist[0]));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cygheap->sthreads = THREADLIST_CHUNK;
|
2007-11-27 05:30:49 +08:00
|
|
|
cygheap->threadlist = (_cygtls **) ccalloc_abort (HEAP_TLS, cygheap->sthreads,
|
|
|
|
sizeof (cygheap->threadlist[0]));
|
2004-01-14 23:45:37 +08:00
|
|
|
}
|
2005-04-05 12:31:00 +08:00
|
|
|
sentry::lock.init ("sentry_lock");
|
2004-01-14 23:45:37 +08:00
|
|
|
}
|
2003-12-24 00:43:45 +08:00
|
|
|
|
|
|
|
/* Two calls to get the stack right... */
|
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::call (DWORD (*func) (void *, void *), void *arg)
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
|
|
|
char buf[CYGTLS_PADSIZE];
|
2010-02-28 23:54:25 +08:00
|
|
|
/* Initialize this thread's ability to respond to things like
|
|
|
|
SIGSEGV or SIGFPE. */
|
|
|
|
exception protect;
|
2006-05-25 10:33:13 +08:00
|
|
|
_my_tls.call2 (func, arg, buf);
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
|
|
|
|
2012-02-27 19:55:27 +08:00
|
|
|
static int
|
|
|
|
dll_cmp (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return wcscasecmp ((const wchar_t *) a, *(const wchar_t **) b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep sorted!
|
|
|
|
This is a list of well-known core system DLLs which contain code
|
|
|
|
whiuch is started in its own thread by the system. Kernel32.dll,
|
|
|
|
for instance, contains the thread called on every Ctrl-C keypress
|
|
|
|
in a console window. The DLLs in this list are not recognized as
|
|
|
|
BLODAs. */
|
|
|
|
const wchar_t *well_known_dlls[] =
|
|
|
|
{
|
2012-03-21 18:42:24 +08:00
|
|
|
L"advapi32.dll",
|
2012-02-27 19:55:27 +08:00
|
|
|
L"kernel32.dll",
|
|
|
|
L"mswsock.dll",
|
|
|
|
L"ntdll.dll",
|
2012-02-28 17:44:14 +08:00
|
|
|
L"shlwapi.dll",
|
2012-02-27 19:55:27 +08:00
|
|
|
L"ws2_32.dll",
|
|
|
|
};
|
|
|
|
|
2003-12-24 00:43:45 +08:00
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::call2 (DWORD (*func) (void *, void *), void *arg, void *buf)
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
2006-05-25 10:33:13 +08:00
|
|
|
init_thread (buf, func);
|
2012-02-27 19:55:27 +08:00
|
|
|
|
|
|
|
/* Optional BLODA detection. The idea is that the function address is
|
|
|
|
supposed to be within Cygwin itself. This is also true for pthreads,
|
|
|
|
since pthreads are always calling thread_wrapper in miscfuncs.cc.
|
|
|
|
Therefore, every function call to a function outside of the Cygwin DLL
|
|
|
|
is potentially a thread injected into the Cygwin process by some BLODA.
|
|
|
|
|
|
|
|
But that's a bit too simple. Assuming the application itself calls
|
|
|
|
CreateThread, then this is a bad idea, but not really invalid. So we
|
|
|
|
shouldn't print a BLODA message if the address is within the loaded
|
|
|
|
image of the application. Also, ntdll.dll starts threads into the
|
|
|
|
application which */
|
|
|
|
if (detect_bloda)
|
|
|
|
{
|
|
|
|
PIMAGE_DOS_HEADER img_start = (PIMAGE_DOS_HEADER) GetModuleHandle (NULL);
|
|
|
|
PIMAGE_NT_HEADERS32 ntheader = (PIMAGE_NT_HEADERS32)
|
|
|
|
((PBYTE) img_start + img_start->e_lfanew);
|
|
|
|
void *img_end = (void *) ((PBYTE) img_start
|
|
|
|
+ ntheader->OptionalHeader.SizeOfImage);
|
|
|
|
if (((void *) func < (void *) cygwin_hmodule
|
|
|
|
|| (void *) func > (void *) cygheap)
|
|
|
|
&& ((void *) func < (void *) img_start || (void *) func >= img_end))
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
wchar_t modname[PATH_MAX];
|
|
|
|
|
|
|
|
VirtualQuery ((PVOID) func, &mbi, sizeof mbi);
|
|
|
|
GetModuleFileNameW ((HMODULE) mbi.AllocationBase, modname, PATH_MAX);
|
|
|
|
/* Fetch basename and check against list of above system DLLs. */
|
|
|
|
const wchar_t *modbasename = wcsrchr (modname, L'\\') + 1;
|
|
|
|
if (!bsearch (modbasename, well_known_dlls,
|
|
|
|
sizeof well_known_dlls / sizeof well_known_dlls[0],
|
|
|
|
sizeof well_known_dlls[0], dll_cmp))
|
|
|
|
small_printf ("\n\nPotential BLODA detected! Thread function "
|
|
|
|
"called outside of Cygwin DLL:\n %W\n\n", modname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
DWORD res = func (arg, buf);
|
2006-05-25 10:33:13 +08:00
|
|
|
remove (INFINITE);
|
2006-05-31 10:14:17 +08:00
|
|
|
/* Don't call ExitThread on the main thread since we may have been
|
|
|
|
dynamically loaded. */
|
2006-06-02 08:09:50 +08:00
|
|
|
if ((void *) func != (void *) dll_crt0_1
|
|
|
|
&& (void *) func != (void *) dll_dllcrt0_1)
|
2006-05-31 10:14:17 +08:00
|
|
|
ExitThread (res);
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::init_thread (void *x, DWORD (*func) (void *, void *))
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
|
|
|
if (x)
|
|
|
|
{
|
2006-05-16 11:14:24 +08:00
|
|
|
memset (this, 0, sizeof (*this));
|
2011-01-11 16:05:51 +08:00
|
|
|
_REENT_INIT_PTR (&local_clib);
|
2003-12-24 00:43:45 +08:00
|
|
|
stackptr = stack;
|
|
|
|
if (_GLOBAL_REENT)
|
|
|
|
{
|
|
|
|
local_clib._stdin = _GLOBAL_REENT->_stdin;
|
|
|
|
local_clib._stdout = _GLOBAL_REENT->_stdout;
|
|
|
|
local_clib._stderr = _GLOBAL_REENT->_stderr;
|
2004-09-07 12:05:14 +08:00
|
|
|
local_clib.__sdidinit = _GLOBAL_REENT->__sdidinit ? -1 : 0;
|
2003-12-24 00:43:45 +08:00
|
|
|
local_clib.__cleanup = _GLOBAL_REENT->__cleanup;
|
2004-01-27 02:52:02 +08:00
|
|
|
local_clib.__sglue._niobs = 3;
|
|
|
|
local_clib.__sglue._iobs = &_GLOBAL_REENT->__sf[0];
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-07 02:24:11 +08:00
|
|
|
thread_id = GetCurrentThreadId ();
|
2005-12-06 04:20:18 +08:00
|
|
|
initialized = CYGTLS_INITIALIZED;
|
2003-12-24 00:43:45 +08:00
|
|
|
errno_addr = &(local_clib._errno);
|
2011-08-04 00:40:48 +08:00
|
|
|
locals.cw_timer = NULL;
|
2004-01-14 23:45:37 +08:00
|
|
|
|
|
|
|
if ((void *) func == (void *) cygthread::stub
|
|
|
|
|| (void *) func == (void *) cygthread::simplestub)
|
|
|
|
return;
|
|
|
|
|
2007-02-23 23:15:50 +08:00
|
|
|
cygheap->user.reimpersonate ();
|
2005-04-03 21:06:43 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
sentry here (INFINITE);
|
|
|
|
if (nthreads >= cygheap->sthreads)
|
|
|
|
{
|
2004-02-12 11:01:58 +08:00
|
|
|
cygheap->threadlist = (_cygtls **)
|
2007-11-27 05:30:49 +08:00
|
|
|
crealloc_abort (cygheap->threadlist, (cygheap->sthreads += THREADLIST_CHUNK)
|
|
|
|
* sizeof (cygheap->threadlist[0]));
|
2004-01-14 23:45:37 +08:00
|
|
|
memset (cygheap->threadlist + nthreads, 0, THREADLIST_CHUNK * sizeof (cygheap->threadlist[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
cygheap->threadlist[nthreads++] = this;
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::fixup_after_fork ()
|
|
|
|
{
|
2004-03-16 12:39:38 +08:00
|
|
|
if (sig)
|
|
|
|
{
|
|
|
|
pop ();
|
|
|
|
sig = 0;
|
|
|
|
}
|
2005-10-24 07:47:45 +08:00
|
|
|
stacklock = spinning = 0;
|
2009-07-06 23:42:01 +08:00
|
|
|
locals.select.sockevt = NULL;
|
2011-08-04 00:40:48 +08:00
|
|
|
locals.cw_timer = NULL;
|
2004-03-12 11:09:28 +08:00
|
|
|
wq.thread_ev = NULL;
|
2004-02-12 11:01:58 +08:00
|
|
|
}
|
|
|
|
|
2005-03-17 01:07:32 +08:00
|
|
|
#define free_local(x) \
|
|
|
|
if (locals.x) \
|
|
|
|
{ \
|
|
|
|
free (locals.x); \
|
|
|
|
locals.x = NULL; \
|
|
|
|
}
|
|
|
|
|
2004-02-12 11:01:58 +08:00
|
|
|
void
|
|
|
|
_cygtls::remove (DWORD wait)
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
2006-03-13 12:26:57 +08:00
|
|
|
initialized = 0;
|
2009-07-06 23:42:01 +08:00
|
|
|
if (exit_state >= ES_FINAL)
|
2005-03-03 08:36:49 +08:00
|
|
|
return;
|
2006-01-06 00:26:22 +08:00
|
|
|
|
|
|
|
debug_printf ("wait %p", wait);
|
2011-04-21 16:10:28 +08:00
|
|
|
|
|
|
|
/* FIXME: Need some sort of atthreadexit function to allow things like
|
|
|
|
select to control this themselves. */
|
|
|
|
|
|
|
|
/* Close handle and free memory used by select. */
|
2011-04-18 23:51:54 +08:00
|
|
|
if (locals.select.sockevt)
|
2005-04-06 01:13:35 +08:00
|
|
|
{
|
2011-04-21 16:10:28 +08:00
|
|
|
CloseHandle (locals.select.sockevt);
|
|
|
|
locals.select.sockevt = NULL;
|
|
|
|
free_local (select.ser_num);
|
|
|
|
free_local (select.w4);
|
2005-04-06 01:13:35 +08:00
|
|
|
}
|
2011-04-21 16:10:28 +08:00
|
|
|
/* Free memory used by network functions. */
|
|
|
|
free_local (ntoa_buf);
|
|
|
|
free_local (protoent_buf);
|
|
|
|
free_local (servent_buf);
|
|
|
|
free_local (hostent_buf);
|
2008-03-07 19:24:51 +08:00
|
|
|
/* Free temporary TLS path buffers. */
|
|
|
|
locals.pathbufs.destroy ();
|
|
|
|
|
2004-03-15 10:47:35 +08:00
|
|
|
do
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
2004-03-15 10:47:35 +08:00
|
|
|
sentry here (wait);
|
|
|
|
if (here.acquired ())
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < nthreads; i++)
|
|
|
|
if (this == cygheap->threadlist[i])
|
|
|
|
{
|
|
|
|
if (i < --nthreads)
|
|
|
|
cygheap->threadlist[i] = cygheap->threadlist[nthreads];
|
|
|
|
debug_printf ("removed %p element %d", this, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
remove_wq (wait);
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-12-06 04:20:18 +08:00
|
|
|
_cygtls::push (__stack_t addr)
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
|
|
|
*stackptr++ = (__stack_t) addr;
|
|
|
|
}
|
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls *
|
|
|
|
_cygtls::find_tls (int sig)
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
2008-03-01 22:53:44 +08:00
|
|
|
static int NO_COPY threadlist_ix;
|
|
|
|
|
2011-11-14 09:29:49 +08:00
|
|
|
debug_printf ("signal %d\n", sig);
|
2004-01-14 23:45:37 +08:00
|
|
|
sentry here (INFINITE);
|
2008-03-01 22:53:44 +08:00
|
|
|
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls *res = NULL;
|
2008-03-01 22:53:44 +08:00
|
|
|
threadlist_ix = -1;
|
|
|
|
|
|
|
|
myfault efault;
|
|
|
|
if (efault.faulted ())
|
|
|
|
cygheap->threadlist[threadlist_ix]->remove (INFINITE);
|
|
|
|
|
|
|
|
while (++threadlist_ix < (int) nthreads)
|
2004-01-14 23:45:37 +08:00
|
|
|
if (sigismember (&(cygheap->threadlist[threadlist_ix]->sigwait_mask), sig))
|
|
|
|
{
|
|
|
|
res = cygheap->threadlist[threadlist_ix];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-01-19 13:46:54 +08:00
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::set_siginfo (sigpacket *pack)
|
2004-01-19 13:46:54 +08:00
|
|
|
{
|
|
|
|
infodata = pack->si;
|
|
|
|
}
|