rtld: properly handle root directory in load path (bug 30435)
[platform/upstream/glibc.git] / elf / tst-initfinilazyfail.c
1 /* Test that lazy binding failures in constructors and destructors are fatal.
2    Copyright (C) 2019-2023 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 <dlfcn.h>
20 #include <string.h>
21 #include <support/capture_subprocess.h>
22 #include <support/check.h>
23 #include <support/xdlfcn.h>
24
25 static void
26 test_constructor (void *closure)
27 {
28   void *handle = dlopen ("tst-initlazyfailmod.so", RTLD_LAZY);
29   if (handle == NULL)
30     FAIL_EXIT (2, "dlopen did not terminate the process: %s", dlerror ());
31   else
32     FAIL_EXIT (2, "dlopen did not terminate the process (%p)", handle);
33 }
34
35 static void
36 test_destructor (void *closure)
37 {
38   void *handle = xdlopen ("tst-finilazyfailmod.so", RTLD_LAZY);
39   int ret = dlclose (handle);
40   const char *message = dlerror ();
41   if (message != NULL)
42     FAIL_EXIT (2, "dlclose did not terminate the process: %d, %s",
43                ret, message);
44   else
45     FAIL_EXIT (2, "dlopen did not terminate the process: %d", ret);
46 }
47
48 static int
49 do_test (void)
50 {
51   {
52     struct support_capture_subprocess proc
53       = support_capture_subprocess (test_constructor, NULL);
54     support_capture_subprocess_check (&proc, "constructor", 127,
55                                       sc_allow_stderr);
56     printf ("info: constructor failure output: [[%s]]\n", proc.err.buffer);
57     TEST_VERIFY (strstr (proc.err.buffer,
58                          "tst-initfinilazyfail: symbol lookup error: ")
59                  != NULL);
60     TEST_VERIFY (strstr (proc.err.buffer,
61                          "tst-initlazyfailmod.so: undefined symbol:"
62                          " undefined_function\n") != NULL);
63     support_capture_subprocess_free (&proc);
64   }
65
66   {
67     struct support_capture_subprocess proc
68       = support_capture_subprocess (test_destructor, NULL);
69     support_capture_subprocess_check (&proc, "destructor", 127,
70                                       sc_allow_stderr);
71     printf ("info: destructor failure output: [[%s]]\n", proc.err.buffer);
72     TEST_VERIFY (strstr (proc.err.buffer,
73                          "tst-initfinilazyfail: symbol lookup error: ")
74                  != NULL);
75     TEST_VERIFY (strstr (proc.err.buffer,
76                          "tst-finilazyfailmod.so: undefined symbol:"
77                          " undefined_function\n") != NULL);
78     support_capture_subprocess_free (&proc);
79   }
80
81   return 0;
82 }
83
84 #include <support/test-driver.c>