1 /* Check GLIBC_TUNABLES parsing.
2 Copyright (C) 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 <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",
56 /* Empty tunable are ignored. */
59 "glibc.malloc.check=2::glibc.malloc.mmap_threshold=4096",
64 /* As well empty values. */
67 "glibc.malloc.check=:glibc.malloc.mmap_threshold=4096",
72 /* Tunable are processed from left to right, so last one is the one set. */
75 "glibc.malloc.check=1:glibc.malloc.check=2",
82 "glibc.malloc.check=1:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
89 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=1",
94 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
97 "glibc.malloc.perturb=0x800",
104 "glibc.malloc.perturb=0x55",
109 /* Out of range values are just ignored. */
112 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
117 /* Invalid keys are ignored. */
120 ":glibc.malloc.garbage=2:glibc.malloc.check=1",
127 "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
134 "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
141 "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
146 /* Invalid subkeys are ignored. */
149 "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
156 "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
163 "not_valid.malloc.check=2",
170 "glibc.not_valid.check=2",
175 /* An ill-formatted tunable in the for key=key=value will considere the
176 value as 'key=value' (which can not be parsed as an integer). */
179 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
184 /* Ill-formatted tunables string is not parsed. */
187 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
194 "glibc.malloc.check=2=2",
201 "glibc.malloc.check=2=2:glibc.malloc.mmap_threshold=4096",
208 "glibc.malloc.check=2=2:glibc.malloc.check=2",
215 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
222 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
227 /* Also check some tunable aliases. */
236 "MALLOC_MMAP_THRESHOLD_",
249 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
260 handle_restart (int i
)
262 TEST_COMPARE (tests
[i
].expected_malloc_check
,
263 TUNABLE_GET_FULL (glibc
, malloc
, check
, int32_t, NULL
));
264 TEST_COMPARE (tests
[i
].expected_mmap_threshold
,
265 TUNABLE_GET_FULL (glibc
, malloc
, mmap_threshold
, size_t, NULL
));
266 TEST_COMPARE (tests
[i
].expected_perturb
,
267 TUNABLE_GET_FULL (glibc
, malloc
, perturb
, int32_t, NULL
));
272 do_test (int argc
, char *argv
[])
274 /* We must have either:
275 - One our fource parameters left if called initially:
276 + path to ld.so optional
277 + "--library-path" optional
278 + the library path optional
279 + the application name
280 + the test to check */
282 TEST_VERIFY_EXIT (argc
== 2 || argc
== 5);
285 return handle_restart (atoi (argv
[1]));
287 char nteststr
[INT_BUFSIZE_BOUND (int)];
292 for (; i
< argc
- 1; i
++)
293 spargv
[i
] = argv
[i
+ 1];
294 spargv
[i
++] = (char *) "--direct";
295 spargv
[i
++] = (char *) "--restart";
296 spargv
[i
++] = nteststr
;
300 for (int i
= 0; i
< array_length (tests
); i
++)
302 snprintf (nteststr
, sizeof nteststr
, "%d", i
);
304 printf ("[%d] Spawned test for %s=%s\n",
308 setenv (tests
[i
].name
, tests
[i
].value
, 1);
309 struct support_capture_subprocess result
310 = support_capture_subprogram (spargv
[0], spargv
);
311 support_capture_subprocess_check (&result
, "tst-tunables", 0,
313 support_capture_subprocess_free (&result
);
314 unsetenv (tests
[i
].name
);
320 #define TEST_FUNCTION_ARGV do_test
321 #include <support/test-driver.c>