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