aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/cygtls.cc
blob: 6e9764d0f7ffb23ebe47c3951d69982dab6f1501 (plain)
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
/* cygtls.cc

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"
#define USE_SYS_TYPES_FD_SET
#include "cygtls.h"
#include <syslog.h>
#include <stdlib.h>
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "sigproc.h"
#include "exception.h"

/* Two calls to get the stack right... */
void
_cygtls::call (DWORD (*func) (void *, void *), void *arg)
{
  char buf[CYGTLS_PADSIZE];
  /* Initialize this thread's ability to respond to things like
     SIGSEGV or SIGFPE. */
  exception protect;
  _my_tls.call2 (func, arg, buf);
}

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
   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[] =
{
  L"advapi32.dll",
  L"kernel32.dll",
  L"kernelbase.dll",
  L"mswsock.dll",
  L"ntdll.dll",
  L"ole32.dll",
  L"shlwapi.dll",
  L"wbemprox.dll",
  L"ws2_32.dll",
};

/* 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 pthread::thread_init_wrapper() in thread.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 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 */
static void
bloda_detect (DWORD (*func) (void *, void *))
{
  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);
    }
}

void
_cygtls::call2 (DWORD (*func) (void *, void *), void *arg, void *buf)
{
  /* If func is pthread_wrapper, the final stack hasn't been set up yet.
     This only happens in pthread_wrapper itself.  Thus it doesn't make
     sense to call init_thread or perform BLODA detection.  pthread_wrapper
     eventually calls init_thread by itself. */
  if ((void *) func != (void *) pthread_wrapper)
    {
      init_thread (buf, func);
      if (detect_bloda)
	bloda_detect (func);
    }

  DWORD res = func (arg, buf);
  remove (INFINITE);
  /* Don't call ExitThread on the main thread since we may have been
     dynamically loaded.  */
  if ((void *) func != (void *) dll_crt0_1
      && (void *) func != (void *) dll_dllcrt0_1)
    ExitThread (res);
}

void
_cygtls::init_thread (void *x, DWORD (*func) (void *, void *))
{
  if (x)
    {
      memset (this, 0, sizeof (*this));
      _REENT_INIT_PTR (&local_clib);
      stackptr = stack;
      altstack.ss_flags = SS_DISABLE;
      if (_GLOBAL_REENT)
	{
	  local_clib._stdin = _GLOBAL_REENT->_stdin;
	  local_clib._stdout = _GLOBAL_REENT->_stdout;
	  local_clib._stderr = _GLOBAL_REENT->_stderr;
	  local_clib.__sdidinit = _GLOBAL_REENT->__sdidinit ? -1 : 0;
	  local_clib.__cleanup = _GLOBAL_REENT->__cleanup;
	  local_clib.__sglue._niobs = 3;
	  local_clib.__sglue._iobs = &_GLOBAL_REENT->__sf[0];
	}
    }

  thread_id = GetCurrentThreadId ();
  initialized = CYGTLS_INITIALIZED;
  errno_addr = &(local_clib._errno);
  locals.cw_timer = NULL;

  if ((void *) func == (void *) cygthread::stub
      || (void *) func == (void *) cygthread::simplestub)
    return;

  cygheap->add_tls (this);
}

void
_cygtls::fixup_after_fork ()
{
  if (sig)
    {
      pop ();
      sig = 0;
    }
  stacklock = spinning = 0;
  signal_arrived = NULL;
  locals.select.sockevt = NULL;
  locals.cw_timer = NULL;
  wq.thread_ev = NULL;
}

#define free_local(x) \
  if (locals.x) \
    { \
      free (locals.x); \
      locals.x = NULL; \
    }

void
_cygtls::remove (DWORD wait)
{
  initialized = 0;
  if (exit_state >= ES_FINAL)
    return;

  debug_printf ("wait %u", wait);

  HANDLE mutex = cygheap->remove_tls (this);
  remove_wq (wait);

  /* FIXME: Need some sort of atthreadexit function to allow things like
     select to control this themselves. */

  remove_pending_sigs ();
  if (signal_arrived)
    {
      HANDLE h = signal_arrived;
      signal_arrived = NULL;
      CloseHandle (h);
    }

  /* Close handle and free memory used by select. */
  if (locals.select.sockevt)
    {
      CloseHandle (locals.select.sockevt);
      locals.select.sockevt = NULL;
      free_local (select.ser_num);
      free_local (select.w4);
    }
  /* Free memory used by network functions. */
  free_local (ntoa_buf);
  free_local (protoent_buf);
  free_local (servent_buf);
  free_local (hostent_buf);
  /* Free temporary TLS path buffers. */
  locals.pathbufs.destroy ();
  /* Close timer handle. */
  if (locals.cw_timer)
    NtClose (locals.cw_timer);
  if (mutex)
    {
      ReleaseMutex (mutex);
      CloseHandle (mutex);
    }
}

#ifdef __x86_64__
void san::leave ()
{
  /* Restore tls_pathbuf counters in case of error. */
  _my_tls.locals.pathbufs._counters = _cnt;
  _my_tls.andreas = _clemente;
}
#endif