nss: Sort tests and tests-container and put one test per line
[glibc.git] / elf / tst-audit23.c
blob4904cf1340a97ee1b202259bd97a34bfd66fa84d
1 /* Check for expected la_objopen and la_objeclose for all objects.
2 Copyright (C) 2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <array_length.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <link.h>
23 #include <limits.h>
24 #include <inttypes.h>
25 #include <gnu/lib-names.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <support/capture_subprocess.h>
29 #include <support/check.h>
30 #include <support/xstdio.h>
31 #include <support/xdlfcn.h>
32 #include <support/support.h>
34 static int restart;
35 #define CMDLINE_OPTIONS \
36 { "restart", no_argument, &restart, 1 },
38 static int
39 handle_restart (void)
41 xdlopen ("tst-audit23mod.so", RTLD_NOW);
42 xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
44 return 0;
47 static inline bool
48 startswith (const char *str, const char *pre)
50 size_t lenpre = strlen (pre);
51 size_t lenstr = strlen (str);
52 return lenstr >= lenpre && memcmp (pre, str, lenpre) == 0;
55 static inline bool
56 is_vdso (const char *str)
58 return startswith (str, "linux-gate")
59 || startswith (str, "linux-vdso");
62 static int
63 do_test (int argc, char *argv[])
65 /* We must have either:
66 - One or four parameters left if called initially:
67 + path to ld.so optional
68 + "--library-path" optional
69 + the library path optional
70 + the application name */
71 if (restart)
72 return handle_restart ();
74 char *spargv[9];
75 TEST_VERIFY_EXIT (((argc - 1) + 3) < array_length (spargv));
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;
83 setenv ("LD_AUDIT", "tst-auditmod23.so", 0);
84 struct support_capture_subprocess result
85 = support_capture_subprogram (spargv[0], spargv);
86 support_capture_subprocess_check (&result, "tst-audit22", 0, sc_allow_stderr);
88 /* The expected la_objopen/la_objclose:
89 1. executable
90 2. loader
91 3. libc.so
92 4. tst-audit23mod.so
93 5. libc.so (LM_ID_NEWLM).
94 6. vdso (optional and ignored). */
95 enum { max_objs = 6 };
96 struct la_obj_t
98 char *lname;
99 uintptr_t laddr;
100 Lmid_t lmid;
101 bool closed;
102 } objs[max_objs] = { [0 ... max_objs-1] = { .closed = false } };
103 size_t nobjs = 0;
105 /* The expected namespaces are one for the audit module, one for the
106 application, and another for the dlmopen on handle_restart. */
107 enum { max_ns = 3 };
108 uintptr_t acts[max_ns] = { 0 };
109 size_t nacts = 0;
110 int last_act = -1;
111 uintptr_t last_act_cookie = -1;
112 bool seen_first_objclose = false;
114 FILE *out = fmemopen (result.err.buffer, result.err.length, "r");
115 TEST_VERIFY (out != NULL);
116 char *buffer = NULL;
117 size_t buffer_length = 0;
118 while (xgetline (&buffer, &buffer_length, out))
120 if (startswith (buffer, "la_activity: "))
122 uintptr_t cookie;
123 int this_act;
124 int r = sscanf (buffer, "la_activity: %d %"SCNxPTR"", &this_act,
125 &cookie);
126 TEST_COMPARE (r, 2);
128 /* The cookie identifies the object at the head of the link map,
129 so we only add a new namespace if it changes from the previous
130 one. This works since dlmopen is the last in the test body. */
131 if (cookie != last_act_cookie && last_act_cookie != -1)
132 TEST_COMPARE (last_act, LA_ACT_CONSISTENT);
134 if (this_act == LA_ACT_ADD && acts[nacts] != cookie)
136 acts[nacts++] = cookie;
137 last_act_cookie = cookie;
139 /* The LA_ACT_DELETE is called in the reverse order of LA_ACT_ADD
140 at program termination (if the tests adds a dlclose or a library
141 with extra dependencies this will need to be adapted). */
142 else if (this_act == LA_ACT_DELETE)
144 last_act_cookie = acts[--nacts];
145 TEST_COMPARE (acts[nacts], cookie);
146 acts[nacts] = 0;
148 else if (this_act == LA_ACT_CONSISTENT)
150 TEST_COMPARE (cookie, last_act_cookie);
152 /* LA_ACT_DELETE must always be followed by an la_objclose. */
153 if (last_act == LA_ACT_DELETE)
154 TEST_COMPARE (seen_first_objclose, true);
155 else
156 TEST_COMPARE (last_act, LA_ACT_ADD);
159 last_act = this_act;
160 seen_first_objclose = false;
162 else if (startswith (buffer, "la_objopen: "))
164 char *lname;
165 uintptr_t laddr;
166 Lmid_t lmid;
167 uintptr_t cookie;
168 int r = sscanf (buffer, "la_objopen: %"SCNxPTR" %ms %"SCNxPTR" %ld",
169 &cookie, &lname, &laddr, &lmid);
170 TEST_COMPARE (r, 4);
172 /* la_objclose is not triggered by vDSO because glibc does not
173 unload it. */
174 if (is_vdso (lname))
175 continue;
176 if (nobjs == max_objs)
177 FAIL_EXIT1 ("non expected la_objopen: %s %"PRIxPTR" %ld",
178 lname, laddr, lmid);
179 objs[nobjs].lname = lname;
180 objs[nobjs].laddr = laddr;
181 objs[nobjs].lmid = lmid;
182 objs[nobjs].closed = false;
183 nobjs++;
185 /* This indirectly checks that la_objopen always comes before
186 la_objclose btween la_activity calls. */
187 seen_first_objclose = false;
189 else if (startswith (buffer, "la_objclose: "))
191 char *lname;
192 uintptr_t laddr;
193 Lmid_t lmid;
194 uintptr_t cookie;
195 int r = sscanf (buffer, "la_objclose: %"SCNxPTR" %ms %"SCNxPTR" %ld",
196 &cookie, &lname, &laddr, &lmid);
197 TEST_COMPARE (r, 4);
199 for (size_t i = 0; i < nobjs; i++)
201 if (strcmp (lname, objs[i].lname) == 0 && lmid == objs[i].lmid)
203 TEST_COMPARE (objs[i].closed, false);
204 objs[i].closed = true;
205 break;
209 /* la_objclose should be called after la_activity(LA_ACT_DELETE) for
210 the closed object's namespace. */
211 TEST_COMPARE (last_act, LA_ACT_DELETE);
212 if (!seen_first_objclose)
214 TEST_COMPARE (last_act_cookie, cookie);
215 seen_first_objclose = true;
220 for (size_t i = 0; i < nobjs; i++)
222 TEST_COMPARE (objs[i].closed, true);
223 free (objs[i].lname);
226 /* la_activity(LA_ACT_CONSISTENT) should be the last callback received.
227 Since only one link map may be not-CONSISTENT at a time, this also
228 ensures la_activity(LA_ACT_CONSISTENT) is the last callback received
229 for every namespace. */
230 TEST_COMPARE (last_act, LA_ACT_CONSISTENT);
232 free (buffer);
233 xfclose (out);
235 return 0;
238 #define TEST_FUNCTION_ARGV do_test
239 #include <support/test-driver.c>