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