]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/gconv_dl.c
gconv: Consistently mangle NULL function pointers [BZ #22025]
[thirdparty/glibc.git] / iconv / gconv_dl.c
CommitLineData
6973fc01 1/* Handle loading/unloading of shared object for transformation.
bfff8b1b 2 Copyright (C) 1997-2017 Free Software Foundation, Inc.
6973fc01
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
6973fc01
UD
10
11 The GNU C Library 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 GNU
41bdb6e2 14 Lesser General Public License for more details.
6973fc01 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
6973fc01 19
add40772 20#include <assert.h>
6973fc01 21#include <dlfcn.h>
6973fc01 22#include <inttypes.h>
6973fc01
UD
23#include <search.h>
24#include <stdlib.h>
25#include <string.h>
ec999b8e 26#include <libc-lock.h>
6973fc01
UD
27#include <sys/param.h>
28
e62c19f1 29#include <gconv_int.h>
915a6c51 30#include <sysdep.h>
e62c19f1 31
76a2102b
UD
32
33#ifdef DEBUG
34/* For debugging purposes. */
35static void print_all (void);
36#endif
37
38
6973fc01
UD
39/* This is a tuning parameter. If a transformation module is not used
40 anymore it gets not immediately unloaded. Instead we wait a certain
41 number of load attempts for further modules. If none of the
42 subsequent load attempts name the same object it finally gets unloaded.
43 Otherwise it is still available which hopefully is the frequent case.
44 The following number is the number of unloading attempts we wait
45 before unloading. */
46#define TRIES_BEFORE_UNLOAD 2
47
6973fc01
UD
48/* Array of loaded objects. This is shared by all threads so we have
49 to use semaphores to access it. */
50static void *loaded;
6973fc01 51
6973fc01
UD
52/* Comparison function for searching `loaded_object' tree. */
53static int
54known_compare (const void *p1, const void *p2)
55{
d64b6ad0
UD
56 const struct __gconv_loaded_object *s1 =
57 (const struct __gconv_loaded_object *) p1;
58 const struct __gconv_loaded_object *s2 =
59 (const struct __gconv_loaded_object *) p2;
6973fc01 60
76a2102b 61 return strcmp (s1->name, s2->name);
6973fc01
UD
62}
63
6973fc01
UD
64/* Open the gconv database if necessary. A non-negative return value
65 means success. */
d64b6ad0 66struct __gconv_loaded_object *
e62c19f1 67internal_function
6973fc01
UD
68__gconv_find_shlib (const char *name)
69{
d64b6ad0 70 struct __gconv_loaded_object *found;
7a68c94a 71 void *keyp;
6973fc01
UD
72
73 /* Search the tree of shared objects previously requested. Data in
74 the tree are `loaded_object' structures, whose first member is a
75 `const char *', the lookup key. The search returns a pointer to
76 the tree node structure; the first member of the is a pointer to
77 our structure (i.e. what will be a `loaded_object'); since the
78 first member of that is the lookup key string, &FCT_NAME is close
79 enough to a pointer to our structure to use as a lookup key that
80 will be passed to `known_compare' (above). */
81
7a68c94a
UD
82 keyp = __tfind (&name, &loaded, known_compare);
83 if (keyp == NULL)
6973fc01
UD
84 {
85 /* This name was not known before. */
0db59742
UD
86 size_t namelen = strlen (name) + 1;
87
88 found = malloc (sizeof (struct __gconv_loaded_object) + namelen);
6973fc01
UD
89 if (found != NULL)
90 {
91 /* Point the tree node at this new structure. */
0db59742 92 found->name = (char *) memcpy (found + 1, name, namelen);
6973fc01
UD
93 found->counter = -TRIES_BEFORE_UNLOAD - 1;
94 found->handle = NULL;
95
365afefc
UD
96 if (__builtin_expect (__tsearch (found, &loaded, known_compare)
97 == NULL, 0))
6973fc01
UD
98 {
99 /* Something went wrong while inserting the entry. */
100 free (found);
101 found = NULL;
102 }
103 }
104 }
7a68c94a 105 else
d64b6ad0 106 found = *(struct __gconv_loaded_object **) keyp;
6973fc01
UD
107
108 /* Try to load the shared object if the usage count is 0. This
109 implies that if the shared object is not loadable, the handle is
110 NULL and the usage count > 0. */
111 if (found != NULL)
112 {
113 if (found->counter < -TRIES_BEFORE_UNLOAD)
114 {
fc5f4a97 115 assert (found->handle == NULL);
b3fc5f84
UD
116 found->handle = __libc_dlopen (found->name);
117 if (found->handle != NULL)
0d9f6793 118 {
b3fc5f84 119 found->fct = __libc_dlsym (found->handle, "gconv");
0d9f6793
UD
120 if (found->fct == NULL)
121 {
122 /* Argh, no conversion function. There is something
123 wrong here. */
124 __gconv_release_shlib (found);
125 found = NULL;
126 }
127 else
128 {
b3fc5f84
UD
129 found->init_fct = __libc_dlsym (found->handle, "gconv_init");
130 found->end_fct = __libc_dlsym (found->handle, "gconv_end");
0d9f6793 131
915a6c51
UD
132#ifdef PTR_MANGLE
133 PTR_MANGLE (found->fct);
1cf1232c
PF
134 PTR_MANGLE (found->init_fct);
135 PTR_MANGLE (found->end_fct);
915a6c51
UD
136#endif
137
0d9f6793
UD
138 /* We have succeeded in loading the shared object. */
139 found->counter = 1;
140 }
141 }
142 else
143 /* Error while loading the shared object. */
144 found = NULL;
6973fc01
UD
145 }
146 else if (found->handle != NULL)
147 found->counter = MAX (found->counter + 1, 1);
6973fc01
UD
148 }
149
0d9f6793 150 return found;
6973fc01
UD
151}
152
153
154/* This is very ugly but the tsearch functions provide no way to pass
155 information to the walker function. So we use a global variable.
156 It is MT safe since we use a lock. */
d64b6ad0 157static struct __gconv_loaded_object *release_handle;
6973fc01
UD
158
159static void
17427edd 160do_release_shlib (void *nodep, VISIT value, int level)
6973fc01 161{
d64b6ad0 162 struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
6973fc01
UD
163
164 if (value != preorder && value != leaf)
165 return;
166
0d9f6793 167 if (obj == release_handle)
add40772
UD
168 {
169 /* This is the object we want to unload. Now decrement the
170 reference counter. */
171 assert (obj->counter > 0);
172 --obj->counter;
173 }
fc5f4a97
UD
174 else if (obj->counter <= 0 && obj->counter >= -TRIES_BEFORE_UNLOAD
175 && --obj->counter < -TRIES_BEFORE_UNLOAD && obj->handle != NULL)
6973fc01 176 {
fc5f4a97
UD
177 /* Unload the shared object. */
178 __libc_dlclose (obj->handle);
179 obj->handle = NULL;
6973fc01
UD
180 }
181}
182
183
184/* Notify system that a shared object is not longer needed. */
b79f74cd 185void
e62c19f1 186internal_function
d64b6ad0 187__gconv_release_shlib (struct __gconv_loaded_object *handle)
6973fc01 188{
6973fc01
UD
189 /* Urgh, this is ugly but we have no other possibility. */
190 release_handle = handle;
191
192 /* Process all entries. Please note that we also visit entries
193 with release counts <= 0. This way we can finally unload them
194 if necessary. */
17427edd 195 __twalk (loaded, (__action_fn_t) do_release_shlib);
6973fc01 196}
e6df9a56
UD
197
198
199/* We run this if we debug the memory allocation. */
7c11c4a1 200static void __libc_freeres_fn_section
74454183 201do_release_all (void *nodep)
e6df9a56 202{
d64b6ad0 203 struct __gconv_loaded_object *obj = (struct __gconv_loaded_object *) nodep;
e6df9a56 204
b3fc5f84 205 /* Unload the shared object. */
74454183 206 if (obj->handle != NULL)
9a0a9895 207 __libc_dlclose (obj->handle);
e6df9a56
UD
208
209 free (obj);
210}
211
c877418f 212libc_freeres_fn (free_mem)
e6df9a56 213{
74454183 214 __tdestroy (loaded, do_release_all);
f6e50e66 215 loaded = NULL;
e6df9a56 216}
76a2102b
UD
217
218
219#ifdef DEBUG
f2b3078e
MS
220
221#include <stdio.h>
222
76a2102b
UD
223static void
224do_print (const void *nodep, VISIT value, int level)
225{
226 struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
227
228 printf ("%10s: \"%s\", %d\n",
229 value == leaf ? "leaf" :
230 value == preorder ? "preorder" :
231 value == postorder ? "postorder" : "endorder",
232 obj->name, obj->counter);
233}
234
f2b3078e 235static void __attribute__ ((used))
76a2102b
UD
236print_all (void)
237{
238 __twalk (loaded, do_print);
239}
240#endif