math: Add a no-mathvec flag for sin (-0.0)
[glibc.git] / elf / tst-audit23.c
blob503699c36a8e0a0d1c55822057a76db9ece259c9
1 /* Check for expected la_objopen and la_objeclose for all objects.
2 Copyright (C) 2022-2023 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 uintptr_t cookie;
102 uintptr_t namespace;
103 bool closed;
104 } objs[max_objs] = { [0 ... max_objs-1] = { .closed = false } };
105 size_t nobjs = 0;
107 /* The expected namespaces are one for the audit module, one for the
108 application, and another for the dlmopen on handle_restart. */
109 enum { max_ns = 3 };
110 uintptr_t acts[max_ns] = { 0 };
111 size_t nacts = 0;
112 int last_act = -1;
113 uintptr_t last_act_cookie = -1;
114 bool seen_first_objclose = false;
116 FILE *out = fmemopen (result.err.buffer, result.err.length, "r");
117 TEST_VERIFY (out != NULL);
118 char *buffer = NULL;
119 size_t buffer_length = 0;
120 while (xgetline (&buffer, &buffer_length, out))
122 *strchrnul (buffer, '\n') = '\0';
123 printf ("info: subprocess output: %s\n", buffer);
125 if (startswith (buffer, "la_activity: "))
127 uintptr_t cookie;
128 int this_act;
129 int r = sscanf (buffer, "la_activity: %d %"SCNxPTR"", &this_act,
130 &cookie);
131 TEST_COMPARE (r, 2);
133 if (this_act == LA_ACT_ADD && acts[nacts] != cookie)
135 /* The cookie identifies the object at the head of the
136 link map, so we only add a new namespace if it
137 changes from the previous one. This works since
138 dlmopen is the last in the test body. */
139 if (cookie != last_act_cookie && last_act_cookie != -1)
140 TEST_COMPARE (last_act, LA_ACT_CONSISTENT);
142 acts[nacts++] = cookie;
143 last_act_cookie = cookie;
145 /* LA_ACT_DELETE is called multiple times for each
146 namespace, depending on destruction order. */
147 else if (this_act == LA_ACT_DELETE)
148 last_act_cookie = cookie;
149 else if (this_act == LA_ACT_CONSISTENT)
151 TEST_COMPARE (cookie, last_act_cookie);
152 last_act_cookie = -1;
154 /* LA_ACT_DELETE must always be followed by an la_objclose. */
155 if (last_act == LA_ACT_DELETE)
156 TEST_COMPARE (seen_first_objclose, true);
157 else
158 TEST_COMPARE (last_act, LA_ACT_ADD);
161 last_act = this_act;
162 seen_first_objclose = false;
164 else if (startswith (buffer, "la_objopen: "))
166 char *lname;
167 uintptr_t laddr;
168 Lmid_t lmid;
169 uintptr_t cookie;
170 int r = sscanf (buffer, "la_objopen: %"SCNxPTR" %ms %"SCNxPTR" %ld",
171 &cookie, &lname, &laddr, &lmid);
172 TEST_COMPARE (r, 4);
174 /* la_objclose is not triggered by vDSO because glibc does not
175 unload it. */
176 if (is_vdso (lname))
177 continue;
178 if (nobjs == max_objs)
179 FAIL_EXIT1 ("non expected la_objopen: %s %"PRIxPTR" %ld",
180 lname, laddr, lmid);
181 objs[nobjs].lname = lname;
182 objs[nobjs].laddr = laddr;
183 objs[nobjs].lmid = lmid;
184 objs[nobjs].cookie = cookie;
185 objs[nobjs].namespace = last_act_cookie;
186 objs[nobjs].closed = false;
187 nobjs++;
189 /* This indirectly checks that la_objopen always comes before
190 la_objclose btween la_activity calls. */
191 seen_first_objclose = false;
193 else if (startswith (buffer, "la_objclose: "))
195 char *lname;
196 uintptr_t laddr;
197 Lmid_t lmid;
198 uintptr_t cookie;
199 int r = sscanf (buffer, "la_objclose: %"SCNxPTR" %ms %"SCNxPTR" %ld",
200 &cookie, &lname, &laddr, &lmid);
201 TEST_COMPARE (r, 4);
203 for (size_t i = 0; i < nobjs; i++)
205 if (strcmp (lname, objs[i].lname) == 0 && lmid == objs[i].lmid)
207 TEST_COMPARE (objs[i].closed, false);
208 TEST_COMPARE (objs[i].cookie, cookie);
209 if (objs[i].namespace == -1)
210 /* No LA_ACT_ADD before the first la_objopen call. */
211 TEST_COMPARE (acts[0], last_act_cookie);
212 else
213 TEST_COMPARE (objs[i].namespace, last_act_cookie);
214 objs[i].closed = true;
215 break;
219 /* la_objclose should be called after la_activity(LA_ACT_DELETE) for
220 the closed object's namespace. */
221 TEST_COMPARE (last_act, LA_ACT_DELETE);
222 seen_first_objclose = true;
226 for (size_t i = 0; i < nobjs; i++)
228 TEST_COMPARE (objs[i].closed, true);
229 free (objs[i].lname);
232 /* la_activity(LA_ACT_CONSISTENT) should be the last callback received.
233 Since only one link map may be not-CONSISTENT at a time, this also
234 ensures la_activity(LA_ACT_CONSISTENT) is the last callback received
235 for every namespace. */
236 TEST_COMPARE (last_act, LA_ACT_CONSISTENT);
238 free (buffer);
239 xfclose (out);
241 return 0;
244 #define TEST_FUNCTION_ARGV do_test
245 #include <support/test-driver.c>