OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libmudflap/] [mf-hooks3.c] - Blame information for rev 791

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 738 jeremybenn
/* 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>
6
 
7
This file is part of GCC.
8
 
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
12
version.
13
 
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
17
for more details.
18
 
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.
22
 
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/>.  */
27
 
28
 
29
#include "config.h"
30
 
31
#ifndef HAVE_SOCKLEN_T
32
#define socklen_t int
33
#endif
34
 
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__)
38
#define _POSIX_SOURCE
39
#endif /* Some BSDs break <sys/socket.h> if this is defined. */
40
#define _GNU_SOURCE
41
#define _XOPEN_SOURCE
42
#define _BSD_TYPES
43
#define __EXTENSIONS__
44
#define _ALL_SOURCE
45
#define _LARGE_FILE_API
46
#define _XOPEN_SOURCE_EXTENDED 1
47
#define _REENTRANT
48
 
49
#include <string.h>
50
#include <stdio.h>
51
#include <stdlib.h>
52
#include <unistd.h>
53
#include <assert.h>
54
#include <errno.h>
55
#include <stdbool.h>
56
 
57
#include "mf-runtime.h"
58
#include "mf-impl.h"
59
 
60
#ifdef _MUDFLAP
61
#error "Do not compile this file with -fmudflap!"
62
#endif
63
 
64
#ifndef LIBMUDFLAPTH
65
#error "pthreadstuff is to be included only in libmudflapth"
66
#endif
67
 
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);
73
 
74
 
75
/* Multithreading support hooks.  */
76
 
77
 
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.
86
 
87
   Which leaves us to handle this mess by totally by hand.  */
88
 
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
93
 
94
struct mf_thread_data
95
{
96
  pthread_t self;
97
  unsigned char used_p;
98
  unsigned char state;
99
};
100
 
101
static struct mf_thread_data mf_thread_data[LIBMUDFLAPTH_THREADS_MAX];
102
static pthread_mutex_t mf_thread_data_lock = PTHREAD_MUTEX_INITIALIZER;
103
 
104
#define PTHREAD_HASH(p) ((unsigned long) (p) % LIBMUDFLAPTH_THREADS_MAX)
105
 
106
static struct mf_thread_data *
107
__mf_find_threadinfo (int alloc)
108
{
109
  pthread_t self = pthread_self ();
110
  unsigned long hash = PTHREAD_HASH (self);
111
  unsigned long rehash;
112
 
113
#ifdef __alpha__
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
117
     this by hand.  */
118
  __sync_synchronize ();
119
#endif
120
 
121
  rehash = hash;
122
  while (1)
123
    {
124
      if (mf_thread_data[rehash].used_p && mf_thread_data[rehash].self == self)
125
        return &mf_thread_data[rehash];
126
 
127
      rehash += 7;
128
      if (rehash >= LIBMUDFLAPTH_THREADS_MAX)
129
        rehash -= LIBMUDFLAPTH_THREADS_MAX;
130
      if (rehash == hash)
131
        break;
132
    }
133
 
134
  if (alloc)
135
    {
136
      pthread_mutex_lock (&mf_thread_data_lock);
137
 
138
      rehash = hash;
139
      while (1)
140
        {
141
          if (!mf_thread_data[rehash].used_p)
142
            {
143
              mf_thread_data[rehash].self = self;
144
              __sync_synchronize ();
145
              mf_thread_data[rehash].used_p = 1;
146
 
147
              pthread_mutex_unlock (&mf_thread_data_lock);
148
              return &mf_thread_data[rehash];
149
            }
150
 
151
          rehash += 7;
152
          if (rehash >= LIBMUDFLAPTH_THREADS_MAX)
153
            rehash -= LIBMUDFLAPTH_THREADS_MAX;
154
          if (rehash == hash)
155
            break;
156
        }
157
 
158
      pthread_mutex_unlock (&mf_thread_data_lock);
159
    }
160
 
161
  return NULL;
162
}
163
 
164
enum __mf_state_enum
165
__mf_get_state (void)
166
{
167
  struct mf_thread_data *data = __mf_find_threadinfo (0);
168
  if (data)
169
    return data->state;
170
 
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.
174
 
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.  */
181
 
182
  return reentrant;
183
}
184
 
185
void
186
__mf_set_state (enum __mf_state_enum new_state)
187
{
188
  struct mf_thread_data *data = __mf_find_threadinfo (1);
189
  data->state = new_state;
190
}
191
#endif
192
 
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
195
   variable.
196
 
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.  */
201
 
202
/* Describe the startup information for a new user thread.  */
203
struct mf_thread_start_info
204
{
205
  /* The user's thread entry point and argument.  */
206
  void * (*user_fn)(void *);
207
  void *user_arg;
208
};
209
 
210
 
211
static void
212
__mf_pthread_cleanup (void *arg)
213
{
214
  if (__mf_opts.heur_std_data)
215
    __mf_unregister (&errno, sizeof (errno), __MF_TYPE_GUESS);
216
 
217
#if !defined(HAVE_TLS) || defined(USE_EMUTLS)
218
  struct mf_thread_data *data = __mf_find_threadinfo (0);
219
  if (data)
220
    data->used_p = 0;
221
#endif
222
}
223
 
224
 
225
static void *
226
__mf_pthread_spawner (void *arg)
227
{
228
  void *result = NULL;
229
 
230
  __mf_set_state (active);
231
 
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)");
238
 
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);
244
 
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);
250
 
251
  result = (*user_fn)(user_arg);
252
 
253
  pthread_cleanup_pop (1 /* execute */);
254
 
255
  return result;
256
}
257
 
258
 
259
#if PIC
260
/* A special bootstrap variant. */
261
int
262
__mf_0fn_pthread_create (pthread_t *thr, const pthread_attr_t *attr,
263
                         void * (*start) (void *), void *arg)
264
{
265
  return -1;
266
}
267
#endif
268
 
269
 
270
#undef pthread_create
271
WRAPPER(int, pthread_create, pthread_t *thr, const pthread_attr_t *attr,
272
         void * (*start) (void *), void *arg)
273
{
274
  struct mf_thread_start_info *si;
275
 
276
  TRACE ("pthread_create\n");
277
 
278
  /* Fill in startup-control fields.  */
279
  si = CALL_REAL (malloc, sizeof (*si));
280
  si->user_fn = start;
281
  si->user_arg = arg;
282
 
283
  /* Actually create the thread.  */
284
  return CALL_REAL (pthread_create, thr, attr, __mf_pthread_spawner, si);
285
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.