elf: Do not duplicate the GLIBC_TUNABLES string
[glibc.git] / elf / tst-tunables.c
blob188345b07090eacb4283e5ed96b0ba81abebac16
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>
21 #include <getopt.h>
22 #include <intprops.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <support/capture_subprocess.h>
26 #include <support/check.h>
28 static int restart;
29 #define CMDLINE_OPTIONS \
30 { "restart", no_argument, &restart, 1 },
32 static const struct test_t
34 const char *name;
35 const char *value;
36 int32_t expected_malloc_check;
37 size_t expected_mmap_threshold;
38 int32_t expected_perturb;
39 } tests[] =
41 /* Expected tunable format. */
43 "GLIBC_TUNABLES",
44 "glibc.malloc.check=2",
50 "GLIBC_TUNABLES",
51 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
53 4096,
56 /* Empty tunable are ignored. */
58 "GLIBC_TUNABLES",
59 "glibc.malloc.check=2::glibc.malloc.mmap_threshold=4096",
61 4096,
64 /* As well empty values. */
66 "GLIBC_TUNABLES",
67 "glibc.malloc.check=:glibc.malloc.mmap_threshold=4096",
69 4096,
72 /* Tunable are processed from left to right, so last one is the one set. */
74 "GLIBC_TUNABLES",
75 "glibc.malloc.check=1:glibc.malloc.check=2",
81 "GLIBC_TUNABLES",
82 "glibc.malloc.check=1:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
84 4096,
88 "GLIBC_TUNABLES",
89 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=1",
91 4096,
94 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
96 "GLIBC_TUNABLES",
97 "glibc.malloc.perturb=0x800",
103 "GLIBC_TUNABLES",
104 "glibc.malloc.perturb=0x55",
107 0x55,
109 /* Out of range values are just ignored. */
111 "GLIBC_TUNABLES",
112 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
114 4096,
117 /* Invalid keys are ignored. */
119 "GLIBC_TUNABLES",
120 ":glibc.malloc.garbage=2:glibc.malloc.check=1",
126 "GLIBC_TUNABLES",
127 "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
129 4096,
133 "GLIBC_TUNABLES",
134 "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
136 4096,
140 "GLIBC_TUNABLES",
141 "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
143 4096,
146 /* Invalid subkeys are ignored. */
148 "GLIBC_TUNABLES",
149 "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
155 "GLIBC_TUNABLES",
156 "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
162 "GLIBC_TUNABLES",
163 "not_valid.malloc.check=2",
169 "GLIBC_TUNABLES",
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). */
178 "GLIBC_TUNABLES",
179 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
184 /* Ill-formatted tunables string is not parsed. */
186 "GLIBC_TUNABLES",
187 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
193 "GLIBC_TUNABLES",
194 "glibc.malloc.check=2=2",
200 "GLIBC_TUNABLES",
201 "glibc.malloc.check=2=2:glibc.malloc.mmap_threshold=4096",
207 "GLIBC_TUNABLES",
208 "glibc.malloc.check=2=2:glibc.malloc.check=2",
214 "GLIBC_TUNABLES",
215 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
221 "GLIBC_TUNABLES",
222 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
227 /* Also check some tunable aliases. */
229 "MALLOC_CHECK_",
230 "2",
236 "MALLOC_MMAP_THRESHOLD_",
237 "4096",
239 4096,
243 "MALLOC_PERTURB_",
244 "0x55",
247 0x55,
249 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
251 "MALLOC_PERTURB_",
252 "0x800",
259 static int
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));
268 return 0;
271 static int
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);
284 if (restart)
285 return handle_restart (atoi (argv[1]));
287 char nteststr[INT_BUFSIZE_BOUND (int)];
289 char *spargv[10];
291 int i = 0;
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;
297 spargv[i] = NULL;
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",
306 tests[i].name,
307 tests[i].value);
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,
312 sc_allow_stderr);
313 support_capture_subprocess_free (&result);
314 unsetenv (tests[i].name);
317 return 0;
320 #define TEST_FUNCTION_ARGV do_test
321 #include <support/test-driver.c>