1 /* Check LD_AUDIT and LD_BIND_NOW.
2    Copyright (C) 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 <array_length.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <inttypes.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <support/capture_subprocess.h>
27 #include <support/check.h>
28 #include <support/xstdio.h>
29 #include <support/support.h>
30 #include <sys/auxv.h>
31 
32 static int restart;
33 #define CMDLINE_OPTIONS \
34   { "restart", no_argument, &restart, 1 },
35 
36 void tst_audit25mod1_func1 (void);
37 void tst_audit25mod1_func2 (void);
38 void tst_audit25mod2_func1 (void);
39 void tst_audit25mod2_func2 (void);
40 
41 static int
handle_restart(void)42 handle_restart (void)
43 {
44   tst_audit25mod1_func1 ();
45   tst_audit25mod1_func2 ();
46   tst_audit25mod2_func1 ();
47   tst_audit25mod2_func2 ();
48 
49   return 0;
50 }
51 
52 static int
do_test(int argc,char * argv[])53 do_test (int argc, char *argv[])
54 {
55   /* We must have either:
56      - One or four parameters left if called initially:
57        + path to ld.so         optional
58        + "--library-path"      optional
59        + the library path      optional
60        + the application name  */
61 
62   if (restart)
63     return handle_restart ();
64 
65   setenv ("LD_AUDIT", "tst-auditmod25.so", 0);
66 
67   char *spargv[9];
68   int i = 0;
69   for (; i < argc - 1; i++)
70     spargv[i] = argv[i + 1];
71   spargv[i++] = (char *) "--direct";
72   spargv[i++] = (char *) "--restart";
73   spargv[i] = NULL;
74   TEST_VERIFY_EXIT (i < array_length (spargv));
75 
76   {
77     struct support_capture_subprocess result
78       = support_capture_subprogram (spargv[0], spargv);
79     support_capture_subprocess_check (&result, "tst-audit25a", 0,
80 				      sc_allow_stderr);
81 
82     /* tst-audit25a is build with -Wl,-z,lazy and tst-audit25mod1 with
83        -Wl,-z,now; so only tst_audit25mod3_func1 should be expected to
84        have LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  */
85     TEST_COMPARE_STRING (result.err.buffer,
86 			 "la_symbind: tst_audit25mod3_func1 1\n"
87 			 "la_symbind: tst_audit25mod1_func1 0\n"
88 			 "la_symbind: tst_audit25mod1_func2 0\n"
89 			 "la_symbind: tst_audit25mod2_func1 0\n"
90 			 "la_symbind: tst_audit25mod4_func1 0\n"
91 			 "la_symbind: tst_audit25mod2_func2 0\n");
92 
93     support_capture_subprocess_free (&result);
94   }
95 
96   {
97     setenv ("LD_BIND_NOW", "1", 0);
98     struct support_capture_subprocess result
99       = support_capture_subprogram (spargv[0], spargv);
100     support_capture_subprocess_check (&result, "tst-audit25a", 0,
101 				      sc_allow_stderr);
102 
103     /* With LD_BIND_NOW all symbols are expected to have
104        LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  Also the resolution
105        order is done in breadth-first order.  */
106     TEST_COMPARE_STRING (result.err.buffer,
107 			 "la_symbind: tst_audit25mod4_func1 1\n"
108 			 "la_symbind: tst_audit25mod3_func1 1\n"
109 			 "la_symbind: tst_audit25mod1_func1 1\n"
110 			 "la_symbind: tst_audit25mod2_func1 1\n"
111 			 "la_symbind: tst_audit25mod1_func2 1\n"
112 			 "la_symbind: tst_audit25mod2_func2 1\n");
113 
114     support_capture_subprocess_free (&result);
115   }
116 
117   return 0;
118 }
119 
120 #define TEST_FUNCTION_ARGV do_test
121 #include <support/test-driver.c>
122