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>
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 };
102 } objs
[max_objs
] = { [0 ... max_objs
-1] = { .closed
= false } };
105 /* The expected namespaces are one for the audit module, one for the
106 application, and another for the dlmopen on handle_restart. */
108 uintptr_t acts
[max_ns
] = { 0 };
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
);
117 size_t buffer_length
= 0;
118 while (xgetline (&buffer
, &buffer_length
, out
))
120 if (startswith (buffer
, "la_activity: "))
124 int r
= sscanf (buffer
, "la_activity: %d %"SCNxPTR
"", &this_act
,
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
);
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);
156 TEST_COMPARE (last_act
, LA_ACT_ADD
);
160 seen_first_objclose
= false;
162 else if (startswith (buffer
, "la_objopen: "))
168 int r
= sscanf (buffer
, "la_objopen: %"SCNxPTR
" %ms %"SCNxPTR
" %ld",
169 &cookie
, &lname
, &laddr
, &lmid
);
172 /* la_objclose is not triggered by vDSO because glibc does not
176 if (nobjs
== max_objs
)
177 FAIL_EXIT1 ("non expected la_objopen: %s %"PRIxPTR
" %ld",
179 objs
[nobjs
].lname
= lname
;
180 objs
[nobjs
].laddr
= laddr
;
181 objs
[nobjs
].lmid
= lmid
;
182 objs
[nobjs
].closed
= false;
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: "))
195 int r
= sscanf (buffer
, "la_objclose: %"SCNxPTR
" %ms %"SCNxPTR
" %ld",
196 &cookie
, &lname
, &laddr
, &lmid
);
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;
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
);
238 #define TEST_FUNCTION_ARGV do_test
239 #include <support/test-driver.c>