]> git.sesse.net Git - vlc/blob - bin/override.c
Work-around non-thread-safe use of the C random number generator
[vlc] / bin / override.c
1 /*****************************************************************************
2  * override.c: overriden function calls for VLC media player
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdbool.h>
26
27 void vlc_enable_override (void);
28
29 static bool override = false;
30
31 void vlc_enable_override (void)
32 {
33     override = true;
34 }
35
36 #if defined (__GNUC__) /* typeof and statement-expression */ \
37  && (defined (__ELF__) && !defined (__sun__))
38 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
39
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <dlfcn.h>
44 #include <pthread.h>
45
46 static void vlogbug (const char *level, const char *func, const char *fmt,
47                      va_list ap)
48 {
49     flockfile (stderr);
50     fprintf (stderr, "%s: call to %s(", level, func);
51     vfprintf (stderr, fmt, ap);
52     fputs (")\n", stderr);
53     funlockfile (stderr);
54 }
55
56 static void logbug (const char *level, const char *func, const char *fmt, ...)
57 {
58     va_list ap;
59
60     va_start (ap, fmt);
61     vlogbug (level, func, fmt, ap);
62     va_end (ap);
63 }
64
65 static void *getsym (const char *name)
66 {
67     void *sym = dlsym (RTLD_NEXT, name);
68     if (sym == NULL)
69     {
70         fprintf (stderr, "Cannot resolve symbol %s!\n", name);
71         abort ();
72     }
73     return sym;
74 }
75
76 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
77 #define CALL(func, ...) \
78     ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
79
80
81 /*** Environment ***
82  *
83  * "Conforming multi-threaded applications shall not use the environ variable
84  *  to access or modify any environment variable while any other thread is
85  *  concurrently modifying any environment variable." -- POSIX.
86  *
87  * Some evil libraries modify the environment. We currently ignore the calls as
88  * they could crash the process. This may cause funny behaviour though. */
89 int putenv (char *str)
90 {
91     if (override)
92     {
93         LOG("Blocked", "\"%s\"", str);
94         return 0;
95     }
96     return CALL(putenv, str);
97 }
98
99 int setenv (const char *name, const char *value, int overwrite)
100 {
101     if (override)
102     {
103         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
104         return 0;
105     }
106     return CALL(setenv, name, value, overwrite);
107 }
108
109 int unsetenv (const char *name)
110 {
111     if (override)
112     {
113         LOG("Blocked", "\"%s\"", name);
114         return 0;
115     }
116     return CALL(unsetenv, name);
117 }
118
119
120 /*** Pseudo random numbers ***
121  *
122  * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
123  * is much better as a reproducible non-secure PRNG). To work around this, we
124  * force evil callers to serialize. This makes the call safe, but fails to
125  * preserve reproducibility of the number sequence (which usually does not
126  * matter).
127  **/
128 static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
129
130 void srand (unsigned int seed)
131 {
132     pthread_mutex_lock (&prng_lock);
133     LOG("Warning", "%d", seed);
134     CALL(srand, seed);
135     pthread_mutex_unlock (&prng_lock);
136 }
137
138 int rand (void)
139 {
140     int ret;
141
142     pthread_mutex_lock (&prng_lock);
143     LOG("Warning", "");
144     ret = CALL(rand);
145     pthread_mutex_unlock (&prng_lock);
146     return ret;
147 }
148
149
150 #endif /* __ELF__ */