Fix recursive dlopen.
[platform/upstream/glibc.git] / dlfcn / tst-rec-dlopen.c
1 /* Test recursive dlopen using malloc hooks.
2    Copyright (C) 1998-2014 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6    The GNU C Library is free software; you can redistribute it and/or
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.
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
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <malloc.h>
23 #include <dlfcn.h>
24
25 #define DSO "moddummy1.so"
26 #define FUNC "dummy1"
27
28 #define DSO1 "moddummy2.so"
29 #define FUNC1 "dummy2"
30
31 /* Result of the called function.  */
32 int func_result;
33
34 /* Prototype for my hook.  */
35 void *custom_malloc_hook (size_t, const void *);
36
37 /* Pointer to old malloc hooks.  */
38 void *(*old_malloc_hook) (size_t, const void *);
39
40 /* Call function func_name in DSO dso_name via dlopen.  */
41 void
42 call_func (const char *dso_name, const char *func_name)
43 {
44   int ret;
45   void *dso;
46   int (*func) (void);
47   char *err;
48
49   /* Open the DSO.  */
50   dso = dlopen (dso_name, RTLD_NOW|RTLD_GLOBAL);
51   if (dso == NULL)
52     {
53       err = dlerror ();
54       fprintf (stderr, "%s\n", err);
55       exit (1);
56     }
57   /* Clear any errors.  */
58   dlerror ();
59
60   /* Lookup func.  */
61   *(void **) (&func) = dlsym (dso, func_name);
62   if (func == NULL)
63     {
64       err = dlerror ();
65       if (err != NULL)
66         {
67           fprintf (stderr, "%s\n", err);
68           exit (1);
69         }
70     }
71   /* Call func.  */
72   func_result = (*func) ();
73
74   /* Close the library and look for errors too.  */
75   ret = dlclose (dso);
76   if (ret != 0)
77     {
78       err = dlerror ();
79       fprintf (stderr, "%s\n", err);
80       exit (1);
81     }
82
83 }
84
85 /* Empty hook that does nothing.  */
86 void *
87 custom_malloc_hook (size_t size, const void *caller)
88 {
89   void *result;
90   /* Restore old hooks.  */
91   __malloc_hook = old_malloc_hook;
92   /* First call a function in another library via dlopen.  */
93   call_func (DSO1, FUNC1);
94   /* Called recursively.  */
95   result = malloc (size);
96   /* Restore new hooks.  */
97   __malloc_hook = custom_malloc_hook;
98   return result;
99 }
100
101 static int
102 do_test (void)
103 {
104   /* Save old hook.  */
105   old_malloc_hook = __malloc_hook;
106   /* Install new hook.  */
107   __malloc_hook = custom_malloc_hook;
108
109   /* Bug 17702 fixes two things:
110        * A recursive dlopen unmapping the ld.so.cache.
111        * An assertion that _r_debug is RT_CONSISTENT at entry to dlopen.
112      We can only test the latter. Testing the former requires modifying
113      ld.so.conf to cache the dummy libraries, then running ldconfig,
114      then run the test. If you do all of that (and glibc's test
115      infrastructure doesn't support that yet) then the test will
116      SEGFAULT without the fix. If you don't do that, then the test
117      will abort because of the assert described in detail below.  */
118   call_func (DSO, FUNC);
119
120   /* Restore old hook.  */
121   __malloc_hook = old_malloc_hook;
122
123   /* The function dummy2() is called by the malloc hook. Check to
124      see that it was called. This ensures the second recursive
125      dlopen happened and we called the function in that library.
126      Before the fix you either get a SIGSEGV when accessing mmap'd
127      ld.so.cache data or an assertion failure about _r_debug not
128      beint RT_CONSISTENT.  We don't test for the SIGSEGV since it
129      would require finding moddummy1 or moddummy2 in the cache and
130      we don't have any infrastructure to test that, but the _r_debug
131      assertion triggers.  */
132   printf ("Returned result is %d\n", func_result);
133   if (func_result <= 0)
134     {
135       printf ("FAIL: Function call_func() not called.\n");
136       exit (1);
137     }
138
139   printf ("PASS: Function call_func() called more than once.\n");
140   return 0;
141 }
142
143 #define TEST_FUNCTION do_test ()
144 #include "../test-skeleton.c"