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>
25 #include <gnu/lib-names.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>
35 #define CMDLINE_OPTIONS \
36 { "restart", no_argument, &restart, 1 },
41 xdlopen ("tst-audit23mod.so", RTLD_NOW
);
42 xdlmopen (LM_ID_NEWLM
, LIBC_SO
, RTLD_NOW
);
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;
56 is_vdso (const char *str
)
58 return startswith (str
, "linux-gate")
59 || startswith (str
, "linux-vdso");
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 */
72 return handle_restart ();
75 TEST_VERIFY_EXIT (((argc
- 1) + 3) < array_length (spargv
));
77 for (; i
< argc
- 1; i
++)
78 spargv
[i
] = argv
[i
+ 1];
79 spargv
[i
++] = (char *) "--direct";
80 spargv
[i
++] = (char *) "--restart";
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:
93 5. libc.so (LM_ID_NEWLM).
94 6. vdso (optional and ignored). */
95 enum { max_objs
= 6 };
104 } objs
[max_objs
] = { [0 ... max_objs
-1] = { .closed
= false } };
107 /* The expected namespaces are one for the audit module, one for the
108 application, and another for the dlmopen on handle_restart. */
110 uintptr_t acts
[max_ns
] = { 0 };
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
);
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: "))
129 int r
= sscanf (buffer
, "la_activity: %d %"SCNxPTR
"", &this_act
,
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);
158 TEST_COMPARE (last_act
, LA_ACT_ADD
);
162 seen_first_objclose
= false;
164 else if (startswith (buffer
, "la_objopen: "))
170 int r
= sscanf (buffer
, "la_objopen: %"SCNxPTR
" %ms %"SCNxPTR
" %ld",
171 &cookie
, &lname
, &laddr
, &lmid
);
174 /* la_objclose is not triggered by vDSO because glibc does not
178 if (nobjs
== max_objs
)
179 FAIL_EXIT1 ("non expected la_objopen: %s %"PRIxPTR
" %ld",
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;
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: "))
199 int r
= sscanf (buffer
, "la_objclose: %"SCNxPTR
" %ms %"SCNxPTR
" %ld",
200 &cookie
, &lname
, &laddr
, &lmid
);
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
);
213 TEST_COMPARE (objs
[i
].namespace, last_act_cookie
);
214 objs
[i
].closed
= true;
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
);
244 #define TEST_FUNCTION_ARGV do_test
245 #include <support/test-driver.c>