1 /* Mudflap: narrow-pointer bounds-checking by tree rewriting.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Frank Ch. Eigler <fche@redhat.com>
4 and Graydon Hoare <graydon@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23 WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING. If not, write to the Free
29 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
35 #ifndef HAVE_SOCKLEN_T
39 /* These attempt to coax various unix flavours to declare all our
40 needed tidbits in the system headers. */
41 #if !defined(__FreeBSD__) && !defined(__APPLE__)
43 #endif /* Some BSDs break <sys/socket.h> if this is defined. */
47 #define __EXTENSIONS__
49 #define _LARGE_FILE_API
50 #define _XOPEN_SOURCE_EXTENDED 1
60 #include "mf-runtime.h"
64 #error "Do not compile this file with -fmudflap!"
68 #error "pthreadstuff is to be included only in libmudflapth"
71 /* ??? Why isn't this done once in the header files. */
72 DECLARE(void *, malloc
, size_t sz
);
73 DECLARE(void, free
, void *ptr
);
74 DECLARE(int, pthread_create
, pthread_t
*thr
, const pthread_attr_t
*attr
,
75 void * (*start
) (void *), void *arg
);
78 /* Multithreading support hooks. */
82 /* We don't have TLS. Ordinarily we could use pthread keys, but since we're
83 commandeering malloc/free that presents a few problems. The first is that
84 we'll recurse from __mf_get_state to pthread_setspecific to malloc back to
85 __mf_get_state during thread startup. This can be solved with clever uses
86 of a mutex. The second problem is that thread shutdown is indistinguishable
87 from thread startup, since libpthread is deallocating our state variable.
88 I've no good solution for this.
90 Which leaves us to handle this mess by totally by hand. */
92 /* Yes, we want this prime. If pthread_t is a pointer, it's almost always
93 page aligned, and if we use a smaller power of 2, this results in "%N"
94 being the worst possible hash -- all threads hash to zero. */
95 #define LIBMUDFLAPTH_THREADS_MAX 1021
100 unsigned char used_p
;
104 static struct mf_thread_data mf_thread_data
[LIBMUDFLAPTH_THREADS_MAX
];
105 static pthread_mutex_t mf_thread_data_lock
= PTHREAD_MUTEX_INITIALIZER
;
107 /* Try to identify the main thread when filling in mf_thread_data. We
108 should always be called at least once from the main thread before
109 any new threads are spawned. */
110 static int main_seen_p
;
112 #define PTHREAD_HASH(p) ((unsigned long) (p) % LIBMUDFLAPTH_THREADS_MAX)
114 static struct mf_thread_data
*
115 __mf_find_threadinfo (int alloc
)
117 pthread_t self
= pthread_self ();
118 unsigned long hash
= PTHREAD_HASH (self
);
119 unsigned long rehash
;
122 /* Alpha has the loosest memory ordering rules of all. We need a memory
123 barrier to flush the reorder buffer before considering a *read* of a
124 shared variable. Since we're not always taking a lock, we have to do
126 __sync_synchronize ();
132 if (mf_thread_data
[rehash
].used_p
&& mf_thread_data
[rehash
].self
== self
)
133 return &mf_thread_data
[rehash
];
136 if (rehash
>= LIBMUDFLAPTH_THREADS_MAX
)
137 rehash
-= LIBMUDFLAPTH_THREADS_MAX
;
144 pthread_mutex_lock (&mf_thread_data_lock
);
149 if (!mf_thread_data
[rehash
].used_p
)
151 mf_thread_data
[rehash
].self
= self
;
152 __sync_synchronize ();
153 mf_thread_data
[rehash
].used_p
= 1;
155 pthread_mutex_unlock (&mf_thread_data_lock
);
156 return &mf_thread_data
[rehash
];
160 if (rehash
>= LIBMUDFLAPTH_THREADS_MAX
)
161 rehash
-= LIBMUDFLAPTH_THREADS_MAX
;
166 pthread_mutex_unlock (&mf_thread_data_lock
);
173 __mf_get_state (void)
175 struct mf_thread_data
*data
= __mf_find_threadinfo (0);
179 /* The main thread needs to default to active state, so that the global
180 constructors are processed in the active state. Child threads should
181 be considered to be in the reentrant state, so that we don't wind up
182 doing Screwy Things inside the thread library; it'll get reset to
183 active state in __mf_pthread_spawner before user code is invoked.
185 The trickiest bit here is that the LinuxThreads pthread_manager thread
186 should *always* be considered to be reentrant, so that none of our
187 hooks actually do anything. Why? Because that thread isn't a real
188 thread from the point of view of the thread library, and so lots of
189 stuff isn't initialized, leading to SEGV very quickly. Even calling
190 pthread_self is a bit suspect, but it happens to work. */
197 data
= __mf_find_threadinfo (1);
198 data
->state
= active
;
204 __mf_set_state (enum __mf_state_enum new_state
)
206 struct mf_thread_data
*data
= __mf_find_threadinfo (1);
207 data
->state
= new_state
;
211 /* The following two functions are used only with __mf_opts.heur_std_data.
212 We're interested in recording the location of the thread-local errno
215 Note that this doesn't handle TLS references in general; we have no
216 visibility into __tls_get_data for when that memory is allocated at
217 runtime. Hopefully we get to see the malloc or mmap operation that
218 eventually allocates the backing store. */
220 /* Describe the startup information for a new user thread. */
221 struct mf_thread_start_info
223 /* The user's thread entry point and argument. */
224 void * (*user_fn
)(void *);
230 __mf_pthread_cleanup (void *arg
)
232 if (__mf_opts
.heur_std_data
)
233 __mf_unregister (&errno
, sizeof (errno
), __MF_TYPE_GUESS
);
236 struct mf_thread_data
*data
= __mf_find_threadinfo (0);
244 __mf_pthread_spawner (void *arg
)
249 __mf_set_state (active
);
252 /* NB: We could use __MF_TYPE_STATIC here, but we guess that the thread
253 errno is coming out of some dynamically allocated pool that we already
254 know of as __MF_TYPE_HEAP. */
255 if (__mf_opts
.heur_std_data
)
256 __mf_register (&errno
, sizeof (errno
), __MF_TYPE_GUESS
,
257 "errno area (thread)");
259 /* We considered using pthread_key_t objects instead of these
260 cleanup stacks, but they were less cooperative with the
261 interposed malloc hooks in libmudflap. */
262 /* ??? The pthread_key_t problem is solved above... */
263 pthread_cleanup_push (__mf_pthread_cleanup
, NULL
);
265 /* Extract given entry point and argument. */
266 struct mf_thread_start_info
*psi
= arg
;
267 void * (*user_fn
)(void *) = psi
->user_fn
;
268 void *user_arg
= psi
->user_arg
;
269 CALL_REAL (free
, arg
);
271 result
= (*user_fn
)(user_arg
);
273 pthread_cleanup_pop (1 /* execute */);
280 /* A special bootstrap variant. */
282 __mf_0fn_pthread_create (pthread_t
*thr
, const pthread_attr_t
*attr
,
283 void * (*start
) (void *), void *arg
)
290 #undef pthread_create
291 WRAPPER(int, pthread_create
, pthread_t
*thr
, const pthread_attr_t
*attr
,
292 void * (*start
) (void *), void *arg
)
294 int result
, need_wrapper
= 0;
296 TRACE ("pthread_create\n");
301 need_wrapper
|= __mf_opts
.heur_std_data
!= 0;
305 struct mf_thread_start_info
*si
= CALL_REAL (malloc
, sizeof (*si
));
307 /* Fill in startup-control fields. */
311 /* Actually create the thread. */
312 result
= CALL_REAL (pthread_create
, thr
, attr
, __mf_pthread_spawner
, si
);
316 /* If we're not handling heur_std_data, nothing special to do. */
317 result
= CALL_REAL (pthread_create
, thr
, attr
, start
, arg
);