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
32static int restart;
33#define CMDLINE_OPTIONS \
34 { "restart", no_argument, &restart, 1 },
35
36void tst_audit25mod1_func1 (void);
37void tst_audit25mod1_func2 (void);
38void tst_audit25mod2_func1 (void);
39void tst_audit25mod2_func2 (void);
40
41static int
42handle_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
52static inline bool
53startswith (const char *str, const char *pre)
54{
55 size_t lenpre = strlen (s: pre);
56 size_t lenstr = strlen (s: str);
57 return lenstr < lenpre ? false : memcmp (s1: pre, s2: str, n: lenpre) == 0;
58}
59
60static int
61do_test (int argc, char *argv[])
62{
63 /* We must have either:
64 - One or four parameters left if called initially:
65 + path to ld.so optional
66 + "--library-path" optional
67 + the library path optional
68 + the application name */
69
70 if (restart)
71 return handle_restart ();
72
73 setenv (name: "LD_AUDIT", value: "tst-auditmod25.so", replace: 0);
74
75 char *spargv[9];
76 int i = 0;
77 for (; i < argc - 1; i++)
78 spargv[i] = argv[i + 1];
79 spargv[i++] = (char *) "--direct";
80 spargv[i++] = (char *) "--restart";
81 spargv[i] = NULL;
82 TEST_VERIFY_EXIT (i < array_length (spargv));
83
84 {
85 struct support_capture_subprocess result
86 = support_capture_subprogram (file: spargv[0], argv: spargv);
87 support_capture_subprocess_check (&result, context: "tst-audit25a", status_or_signal: 0,
88 allowed: sc_allow_stderr);
89
90 /* tst-audit25a is build with -Wl,-z,lazy and tst-audit25mod1 with
91 -Wl,-z,now; so only tst_audit25mod3_func1 should be expected to
92 have LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT. */
93 TEST_COMPARE_STRING (result.err.buffer,
94 "la_symbind: tst_audit25mod3_func1 1\n"
95 "la_symbind: tst_audit25mod1_func1 0\n"
96 "la_symbind: tst_audit25mod1_func2 0\n"
97 "la_symbind: tst_audit25mod2_func1 0\n"
98 "la_symbind: tst_audit25mod4_func1 0\n"
99 "la_symbind: tst_audit25mod2_func2 0\n");
100
101 support_capture_subprocess_free (&result);
102 }
103
104 {
105 setenv (name: "LD_BIND_NOW", value: "1", replace: 0);
106 struct support_capture_subprocess result
107 = support_capture_subprogram (file: spargv[0], argv: spargv);
108 support_capture_subprocess_check (&result, context: "tst-audit25a", status_or_signal: 0,
109 allowed: sc_allow_stderr);
110
111 /* With LD_BIND_NOW all symbols are expected to have
112 LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT. Also the resolution
113 order is done in breadth-first order. */
114 TEST_COMPARE_STRING (result.err.buffer,
115 "la_symbind: tst_audit25mod4_func1 1\n"
116 "la_symbind: tst_audit25mod3_func1 1\n"
117 "la_symbind: tst_audit25mod1_func1 1\n"
118 "la_symbind: tst_audit25mod2_func1 1\n"
119 "la_symbind: tst_audit25mod1_func2 1\n"
120 "la_symbind: tst_audit25mod2_func2 1\n");
121
122 support_capture_subprocess_free (&result);
123 }
124
125 return 0;
126}
127
128#define TEST_FUNCTION_ARGV do_test
129#include <support/test-driver.c>
130

source code of glibc/elf/tst-audit25a.c