Fix recursive dlopen.
[glibc.git] / dlfcn / tst-rec-dlopen.c
blob35b08d4b63ea2d6d1a3c544d80d222252187ff8f
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.
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.
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.
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/>. */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <malloc.h>
23 #include <dlfcn.h>
25 #define DSO "moddummy1.so"
26 #define FUNC "dummy1"
28 #define DSO1 "moddummy2.so"
29 #define FUNC1 "dummy2"
31 /* Result of the called function. */
32 int func_result;
34 /* Prototype for my hook. */
35 void *custom_malloc_hook (size_t, const void *);
37 /* Pointer to old malloc hooks. */
38 void *(*old_malloc_hook) (size_t, const void *);
40 /* Call function func_name in DSO dso_name via dlopen. */
41 void
42 call_func (const char *dso_name, const char *func_name)
44 int ret;
45 void *dso;
46 int (*func) (void);
47 char *err;
49 /* Open the DSO. */
50 dso = dlopen (dso_name, RTLD_NOW|RTLD_GLOBAL);
51 if (dso == NULL)
53 err = dlerror ();
54 fprintf (stderr, "%s\n", err);
55 exit (1);
57 /* Clear any errors. */
58 dlerror ();
60 /* Lookup func. */
61 *(void **) (&func) = dlsym (dso, func_name);
62 if (func == NULL)
64 err = dlerror ();
65 if (err != NULL)
67 fprintf (stderr, "%s\n", err);
68 exit (1);
71 /* Call func. */
72 func_result = (*func) ();
74 /* Close the library and look for errors too. */
75 ret = dlclose (dso);
76 if (ret != 0)
78 err = dlerror ();
79 fprintf (stderr, "%s\n", err);
80 exit (1);
85 /* Empty hook that does nothing. */
86 void *
87 custom_malloc_hook (size_t size, const void *caller)
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;
101 static int
102 do_test (void)
104 /* Save old hook. */
105 old_malloc_hook = __malloc_hook;
106 /* Install new hook. */
107 __malloc_hook = custom_malloc_hook;
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);
120 /* Restore old hook. */
121 __malloc_hook = old_malloc_hook;
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)
135 printf ("FAIL: Function call_func() not called.\n");
136 exit (1);
139 printf ("PASS: Function call_func() called more than once.\n");
140 return 0;
143 #define TEST_FUNCTION do_test ()
144 #include "../test-skeleton.c"