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, 51 Franklin Street, Fifth Floor, 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 #define PTHREAD_HASH(p) ((unsigned long) (p) % LIBMUDFLAPTH_THREADS_MAX)
109 static struct mf_thread_data
*
110 __mf_find_threadinfo (int alloc
)
112 pthread_t self
= pthread_self ();
113 unsigned long hash
= PTHREAD_HASH (self
);
114 unsigned long rehash
;
117 /* Alpha has the loosest memory ordering rules of all. We need a memory
118 barrier to flush the reorder buffer before considering a *read* of a
119 shared variable. Since we're not always taking a lock, we have to do
121 __sync_synchronize ();
127 if (mf_thread_data
[rehash
].used_p
&& mf_thread_data
[rehash
].self
== self
)
128 return &mf_thread_data
[rehash
];
131 if (rehash
>= LIBMUDFLAPTH_THREADS_MAX
)
132 rehash
-= LIBMUDFLAPTH_THREADS_MAX
;
139 pthread_mutex_lock (&mf_thread_data_lock
);
144 if (!mf_thread_data
[rehash
].used_p
)
146 mf_thread_data
[rehash
].self
= self
;
147 __sync_synchronize ();
148 mf_thread_data
[rehash
].used_p
= 1;
150 pthread_mutex_unlock (&mf_thread_data_lock
);
151 return &mf_thread_data
[rehash
];
155 if (rehash
>= LIBMUDFLAPTH_THREADS_MAX
)
156 rehash
-= LIBMUDFLAPTH_THREADS_MAX
;
161 pthread_mutex_unlock (&mf_thread_data_lock
);
168 __mf_get_state (void)
170 struct mf_thread_data
*data
= __mf_find_threadinfo (0);
174 /* If we've never seen this thread before, consider it to be in the
175 reentrant state. The state gets reset to active for the main thread
176 in __mf_init, and for child threads in __mf_pthread_spawner.
178 The trickiest bit here is that the LinuxThreads pthread_manager thread
179 should *always* be considered to be reentrant, so that none of our
180 hooks actually do anything. Why? Because that thread isn't a real
181 thread from the point of view of the thread library, and so lots of
182 stuff isn't initialized, leading to SEGV very quickly. Even calling
183 pthread_self is a bit suspect, but it happens to work. */
189 __mf_set_state (enum __mf_state_enum new_state
)
191 struct mf_thread_data
*data
= __mf_find_threadinfo (1);
192 data
->state
= new_state
;
196 /* The following two functions are used only with __mf_opts.heur_std_data.
197 We're interested in recording the location of the thread-local errno
200 Note that this doesn't handle TLS references in general; we have no
201 visibility into __tls_get_data for when that memory is allocated at
202 runtime. Hopefully we get to see the malloc or mmap operation that
203 eventually allocates the backing store. */
205 /* Describe the startup information for a new user thread. */
206 struct mf_thread_start_info
208 /* The user's thread entry point and argument. */
209 void * (*user_fn
)(void *);
215 __mf_pthread_cleanup (void *arg
)
217 if (__mf_opts
.heur_std_data
)
218 __mf_unregister (&errno
, sizeof (errno
), __MF_TYPE_GUESS
);
221 struct mf_thread_data
*data
= __mf_find_threadinfo (0);
229 __mf_pthread_spawner (void *arg
)
233 __mf_set_state (active
);
235 /* NB: We could use __MF_TYPE_STATIC here, but we guess that the thread
236 errno is coming out of some dynamically allocated pool that we already
237 know of as __MF_TYPE_HEAP. */
238 if (__mf_opts
.heur_std_data
)
239 __mf_register (&errno
, sizeof (errno
), __MF_TYPE_GUESS
,
240 "errno area (thread)");
242 /* We considered using pthread_key_t objects instead of these
243 cleanup stacks, but they were less cooperative with the
244 interposed malloc hooks in libmudflap. */
245 /* ??? The pthread_key_t problem is solved above... */
246 pthread_cleanup_push (__mf_pthread_cleanup
, NULL
);
248 /* Extract given entry point and argument. */
249 struct mf_thread_start_info
*psi
= arg
;
250 void * (*user_fn
)(void *) = psi
->user_fn
;
251 void *user_arg
= psi
->user_arg
;
252 CALL_REAL (free
, arg
);
254 result
= (*user_fn
)(user_arg
);
256 pthread_cleanup_pop (1 /* execute */);
263 /* A special bootstrap variant. */
265 __mf_0fn_pthread_create (pthread_t
*thr
, const pthread_attr_t
*attr
,
266 void * (*start
) (void *), void *arg
)
273 #undef pthread_create
274 WRAPPER(int, pthread_create
, pthread_t
*thr
, const pthread_attr_t
*attr
,
275 void * (*start
) (void *), void *arg
)
277 struct mf_thread_start_info
*si
;
279 TRACE ("pthread_create\n");
281 /* Fill in startup-control fields. */
282 si
= CALL_REAL (malloc
, sizeof (*si
));
286 /* Actually create the thread. */
287 return CALL_REAL (pthread_create
, thr
, attr
, __mf_pthread_spawner
, si
);