1 /* Mudflap: narrow-pointer bounds-checking by tree rewriting.
2 Copyright (C) 2002, 2003, 2004, 2005, 2009, 2011
3 Free Software Foundation, Inc.
4 Contributed by Frank Ch. Eigler <fche@redhat.com>
5 and Graydon Hoare <graydon@redhat.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
31 #ifndef HAVE_SOCKLEN_T
35 /* These attempt to coax various unix flavours to declare all our
36 needed tidbits in the system headers. */
37 #if !defined(__FreeBSD__) && !defined(__APPLE__)
39 #endif /* Some BSDs break <sys/socket.h> if this is defined. */
43 #define __EXTENSIONS__
45 #define _LARGE_FILE_API
46 #define _XOPEN_SOURCE_EXTENDED 1
57 #include "mf-runtime.h"
61 #error "Do not compile this file with -fmudflap!"
65 #error "pthreadstuff is to be included only in libmudflapth"
68 /* ??? Why isn't this done once in the header files. */
69 DECLARE(void *, malloc
, size_t sz
);
70 DECLARE(void, free
, void *ptr
);
71 DECLARE(int, pthread_create
, pthread_t
*thr
, const pthread_attr_t
*attr
,
72 void * (*start
) (void *), void *arg
);
75 /* Multithreading support hooks. */
78 #if !defined(HAVE_TLS) || defined(USE_EMUTLS)
79 /* We don't have TLS. Ordinarily we could use pthread keys, but since we're
80 commandeering malloc/free that presents a few problems. The first is that
81 we'll recurse from __mf_get_state to pthread_setspecific to malloc back to
82 __mf_get_state during thread startup. This can be solved with clever uses
83 of a mutex. The second problem is that thread shutdown is indistinguishable
84 from thread startup, since libpthread is deallocating our state variable.
85 I've no good solution for this.
87 Which leaves us to handle this mess by totally by hand. */
89 /* Yes, we want this prime. If pthread_t is a pointer, it's almost always
90 page aligned, and if we use a smaller power of 2, this results in "%N"
91 being the worst possible hash -- all threads hash to zero. */
92 #define LIBMUDFLAPTH_THREADS_MAX 1021
101 static struct mf_thread_data mf_thread_data
[LIBMUDFLAPTH_THREADS_MAX
];
102 static pthread_mutex_t mf_thread_data_lock
= PTHREAD_MUTEX_INITIALIZER
;
104 #define PTHREAD_HASH(p) ((unsigned long) (p) % LIBMUDFLAPTH_THREADS_MAX)
106 static struct mf_thread_data
*
107 __mf_find_threadinfo (int alloc
)
109 pthread_t self
= pthread_self ();
110 unsigned long hash
= PTHREAD_HASH (self
);
111 unsigned long rehash
;
114 /* Alpha has the loosest memory ordering rules of all. We need a memory
115 barrier to flush the reorder buffer before considering a *read* of a
116 shared variable. Since we're not always taking a lock, we have to do
118 __sync_synchronize ();
124 if (mf_thread_data
[rehash
].used_p
&& mf_thread_data
[rehash
].self
== self
)
125 return &mf_thread_data
[rehash
];
128 if (rehash
>= LIBMUDFLAPTH_THREADS_MAX
)
129 rehash
-= LIBMUDFLAPTH_THREADS_MAX
;
136 pthread_mutex_lock (&mf_thread_data_lock
);
141 if (!mf_thread_data
[rehash
].used_p
)
143 mf_thread_data
[rehash
].self
= self
;
144 __sync_synchronize ();
145 mf_thread_data
[rehash
].used_p
= 1;
147 pthread_mutex_unlock (&mf_thread_data_lock
);
148 return &mf_thread_data
[rehash
];
152 if (rehash
>= LIBMUDFLAPTH_THREADS_MAX
)
153 rehash
-= LIBMUDFLAPTH_THREADS_MAX
;
158 pthread_mutex_unlock (&mf_thread_data_lock
);
165 __mf_get_state (void)
167 struct mf_thread_data
*data
= __mf_find_threadinfo (0);
171 /* If we've never seen this thread before, consider it to be in the
172 reentrant state. The state gets reset to active for the main thread
173 in __mf_init, and for child threads in __mf_pthread_spawner.
175 The trickiest bit here is that the LinuxThreads pthread_manager thread
176 should *always* be considered to be reentrant, so that none of our
177 hooks actually do anything. Why? Because that thread isn't a real
178 thread from the point of view of the thread library, and so lots of
179 stuff isn't initialized, leading to SEGV very quickly. Even calling
180 pthread_self is a bit suspect, but it happens to work. */
186 __mf_set_state (enum __mf_state_enum new_state
)
188 struct mf_thread_data
*data
= __mf_find_threadinfo (1);
189 data
->state
= new_state
;
193 /* The following two functions are used only with __mf_opts.heur_std_data.
194 We're interested in recording the location of the thread-local errno
197 Note that this doesn't handle TLS references in general; we have no
198 visibility into __tls_get_data for when that memory is allocated at
199 runtime. Hopefully we get to see the malloc or mmap operation that
200 eventually allocates the backing store. */
202 /* Describe the startup information for a new user thread. */
203 struct mf_thread_start_info
205 /* The user's thread entry point and argument. */
206 void * (*user_fn
)(void *);
212 __mf_pthread_cleanup (void *arg
)
214 if (__mf_opts
.heur_std_data
)
215 __mf_unregister (&errno
, sizeof (errno
), __MF_TYPE_GUESS
);
217 #if !defined(HAVE_TLS) || defined(USE_EMUTLS)
218 struct mf_thread_data
*data
= __mf_find_threadinfo (0);
226 __mf_pthread_spawner (void *arg
)
230 __mf_set_state (active
);
232 /* NB: We could use __MF_TYPE_STATIC here, but we guess that the thread
233 errno is coming out of some dynamically allocated pool that we already
234 know of as __MF_TYPE_HEAP. */
235 if (__mf_opts
.heur_std_data
)
236 __mf_register (&errno
, sizeof (errno
), __MF_TYPE_GUESS
,
237 "errno area (thread)");
239 /* We considered using pthread_key_t objects instead of these
240 cleanup stacks, but they were less cooperative with the
241 interposed malloc hooks in libmudflap. */
242 /* ??? The pthread_key_t problem is solved above... */
243 pthread_cleanup_push (__mf_pthread_cleanup
, NULL
);
245 /* Extract given entry point and argument. */
246 struct mf_thread_start_info
*psi
= arg
;
247 void * (*user_fn
)(void *) = psi
->user_fn
;
248 void *user_arg
= psi
->user_arg
;
249 CALL_REAL (free
, arg
);
251 result
= (*user_fn
)(user_arg
);
253 pthread_cleanup_pop (1 /* execute */);
260 /* A special bootstrap variant. */
262 __mf_0fn_pthread_create (pthread_t
*thr
, const pthread_attr_t
*attr
,
263 void * (*start
) (void *), void *arg
)
270 #undef pthread_create
271 WRAPPER(int, pthread_create
, pthread_t
*thr
, const pthread_attr_t
*attr
,
272 void * (*start
) (void *), void *arg
)
274 struct mf_thread_start_info
*si
;
276 TRACE ("pthread_create\n");
278 /* Fill in startup-control fields. */
279 si
= CALL_REAL (malloc
, sizeof (*si
));
283 /* Actually create the thread. */
284 return CALL_REAL (pthread_create
, thr
, attr
, __mf_pthread_spawner
, si
);