1 /* Check GLIBC_TUNABLES parsing.
2 Copyright (C) 2023-2024 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 <dl-tunables.h>
25 #include <support/capture_subprocess.h>
26 #include <support/check.h>
29 #define CMDLINE_OPTIONS \
30 { "restart", no_argument, &restart, 1 },
32 static const struct test_t
36 int32_t expected_malloc_check
;
37 size_t expected_mmap_threshold
;
38 int32_t expected_perturb
;
41 /* Expected tunable format. */
44 "glibc.malloc.check=2",
51 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
58 "glibc.malloc.mmap_threshold=-1",
63 /* Empty tunable are ignored. */
66 "glibc.malloc.check=2::glibc.malloc.mmap_threshold=4096",
71 /* As well empty values. */
74 "glibc.malloc.check=:glibc.malloc.mmap_threshold=4096",
79 /* Tunable are processed from left to right, so last one is the one set. */
82 "glibc.malloc.check=1:glibc.malloc.check=2",
89 "glibc.malloc.check=1:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
96 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=1",
101 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
104 "glibc.malloc.perturb=0x800",
111 "glibc.malloc.perturb=0x55",
116 /* Out of range values are just ignored. */
119 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
124 /* Invalid keys are ignored. */
127 ":glibc.malloc.garbage=2:glibc.malloc.check=1",
134 "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
141 "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
148 "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
153 /* Invalid subkeys are ignored. */
156 "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
163 "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
170 "not_valid.malloc.check=2",
177 "glibc.not_valid.check=2",
182 /* An ill-formatted tunable in the for key=key=value will considere the
183 value as 'key=value' (which can not be parsed as an integer). */
186 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
191 /* Ill-formatted tunables string is not parsed. */
194 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
201 "glibc.malloc.check=2=2",
208 "glibc.malloc.check=2=2:glibc.malloc.mmap_threshold=4096",
215 "glibc.malloc.check=2=2:glibc.malloc.check=2",
222 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
229 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
234 /* Invalid numbers are ignored. */
237 "glibc.malloc.check=abc:glibc.malloc.mmap_threshold=4096",
244 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=abc",
252 "glibc.malloc.mmap_threshold=18446744073709551616",
257 /* Also check some tunable aliases. */
266 "MALLOC_MMAP_THRESHOLD_",
279 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
290 handle_restart (int i
)
292 TEST_COMPARE (tests
[i
].expected_malloc_check
,
293 TUNABLE_GET_FULL (glibc
, malloc
, check
, int32_t, NULL
));
294 TEST_COMPARE (tests
[i
].expected_mmap_threshold
,
295 TUNABLE_GET_FULL (glibc
, malloc
, mmap_threshold
, size_t, NULL
));
296 TEST_COMPARE (tests
[i
].expected_perturb
,
297 TUNABLE_GET_FULL (glibc
, malloc
, perturb
, int32_t, NULL
));
302 do_test (int argc
, char *argv
[])
304 /* We must have either:
305 - One our fource parameters left if called initially:
306 + path to ld.so optional
307 + "--library-path" optional
308 + the library path optional
309 + the application name
310 + the test to check */
312 TEST_VERIFY_EXIT (argc
== 2 || argc
== 5);
315 return handle_restart (atoi (argv
[1]));
317 char nteststr
[INT_BUFSIZE_BOUND (int)];
322 for (; i
< argc
- 1; i
++)
323 spargv
[i
] = argv
[i
+ 1];
324 spargv
[i
++] = (char *) "--direct";
325 spargv
[i
++] = (char *) "--restart";
326 spargv
[i
++] = nteststr
;
330 for (int i
= 0; i
< array_length (tests
); i
++)
332 snprintf (nteststr
, sizeof nteststr
, "%d", i
);
334 printf ("[%d] Spawned test for %s=%s\n",
338 setenv (tests
[i
].name
, tests
[i
].value
, 1);
339 struct support_capture_subprocess result
340 = support_capture_subprogram (spargv
[0], spargv
);
341 support_capture_subprocess_check (&result
, "tst-tunables", 0,
343 support_capture_subprocess_free (&result
);
344 unsetenv (tests
[i
].name
);
350 #define TEST_FUNCTION_ARGV do_test
351 #include <support/test-driver.c>