localedata: fix weekdays in mdf_RU locale
[glibc.git] / elf / tst-tunables.c
blob4884dd09f0ddc505319e04855cadd0337fa632a6
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 /* The test uses the tunable_list size, which is only exported for
21 ld.so. This will result in a copy of tunable_list, which is ununsed by
22 the test itself. */
23 #define TUNABLES_INTERNAL 1
24 #include <dl-tunables.h>
25 #include <getopt.h>
26 #include <intprops.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <support/capture_subprocess.h>
30 #include <support/check.h>
31 #include <support/support.h>
33 static int restart;
34 #define CMDLINE_OPTIONS \
35 { "restart", no_argument, &restart, 1 },
37 static struct test_t
39 const char *name;
40 const char *value;
41 int32_t expected_malloc_check;
42 size_t expected_mmap_threshold;
43 int32_t expected_perturb;
44 } tests[] =
46 /* Expected tunable format. */
48 "GLIBC_TUNABLES",
49 "glibc.malloc.check=2",
55 "GLIBC_TUNABLES",
56 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
58 4096,
62 "GLIBC_TUNABLES",
63 "glibc.malloc.mmap_threshold=-1",
65 SIZE_MAX,
68 /* Empty tunable are ignored. */
70 "GLIBC_TUNABLES",
71 "glibc.malloc.check=2::glibc.malloc.mmap_threshold=4096",
73 4096,
76 /* As well empty values. */
78 "GLIBC_TUNABLES",
79 "glibc.malloc.check=:glibc.malloc.mmap_threshold=4096",
81 4096,
84 /* Tunable are processed from left to right, so last one is the one set. */
86 "GLIBC_TUNABLES",
87 "glibc.malloc.check=1:glibc.malloc.check=2",
93 "GLIBC_TUNABLES",
94 "glibc.malloc.check=1:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
96 4096,
100 "GLIBC_TUNABLES",
101 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=1",
103 4096,
106 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
108 "GLIBC_TUNABLES",
109 "glibc.malloc.perturb=0x800",
115 "GLIBC_TUNABLES",
116 "glibc.malloc.perturb=0x55",
119 0x55,
121 /* Out of range values are just ignored. */
123 "GLIBC_TUNABLES",
124 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
126 4096,
129 /* Invalid keys are ignored. */
131 "GLIBC_TUNABLES",
132 ":glibc.malloc.garbage=2:glibc.malloc.check=1",
138 "GLIBC_TUNABLES",
139 "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
141 4096,
145 "GLIBC_TUNABLES",
146 "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
148 4096,
152 "GLIBC_TUNABLES",
153 "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
155 4096,
158 /* Invalid subkeys are ignored. */
160 "GLIBC_TUNABLES",
161 "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
167 "GLIBC_TUNABLES",
168 "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
174 "GLIBC_TUNABLES",
175 "not_valid.malloc.check=2",
181 "GLIBC_TUNABLES",
182 "glibc.not_valid.check=2",
187 /* An ill-formatted tunable in the for key=key=value will considere the
188 value as 'key=value' (which can not be parsed as an integer). */
190 "GLIBC_TUNABLES",
191 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
196 /* Ill-formatted tunables string is not parsed. */
198 "GLIBC_TUNABLES",
199 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
205 "GLIBC_TUNABLES",
206 "glibc.malloc.check=2=2",
212 "GLIBC_TUNABLES",
213 "glibc.malloc.check=2=2:glibc.malloc.mmap_threshold=4096",
219 "GLIBC_TUNABLES",
220 "glibc.malloc.check=2=2:glibc.malloc.check=2",
226 "GLIBC_TUNABLES",
227 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
233 "GLIBC_TUNABLES",
234 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
239 /* Invalid numbers are ignored. */
241 "GLIBC_TUNABLES",
242 "glibc.malloc.check=abc:glibc.malloc.mmap_threshold=4096",
244 4096,
248 "GLIBC_TUNABLES",
249 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=abc",
255 "GLIBC_TUNABLES",
256 /* SIZE_MAX + 1 */
257 "glibc.malloc.mmap_threshold=18446744073709551616",
262 /* Also check some tunable aliases. */
264 "MALLOC_CHECK_",
265 "2",
271 "MALLOC_MMAP_THRESHOLD_",
272 "4096",
274 4096,
278 "MALLOC_PERTURB_",
279 "0x55",
282 0x55,
284 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
286 "MALLOC_PERTURB_",
287 "0x800",
292 /* Also check for repeated tunables with a count larger than the total number
293 of tunables. */
295 "GLIBC_TUNABLES",
296 NULL,
302 "GLIBC_TUNABLES",
303 NULL,
309 "GLIBC_TUNABLES",
310 NULL,
317 static int
318 handle_restart (int i)
320 TEST_COMPARE (tests[i].expected_malloc_check,
321 TUNABLE_GET_FULL (glibc, malloc, check, int32_t, NULL));
322 TEST_COMPARE (tests[i].expected_mmap_threshold,
323 TUNABLE_GET_FULL (glibc, malloc, mmap_threshold, size_t, NULL));
324 TEST_COMPARE (tests[i].expected_perturb,
325 TUNABLE_GET_FULL (glibc, malloc, perturb, int32_t, NULL));
326 return 0;
329 static int
330 do_test (int argc, char *argv[])
332 /* We must have either:
333 - One our fource parameters left if called initially:
334 + path to ld.so optional
335 + "--library-path" optional
336 + the library path optional
337 + the application name
338 + the test to check */
340 TEST_VERIFY_EXIT (argc == 2 || argc == 5);
342 if (restart)
343 return handle_restart (atoi (argv[1]));
345 char nteststr[INT_BUFSIZE_BOUND (int)];
347 char *spargv[10];
349 int i = 0;
350 for (; i < argc - 1; i++)
351 spargv[i] = argv[i + 1];
352 spargv[i++] = (char *) "--direct";
353 spargv[i++] = (char *) "--restart";
354 spargv[i++] = nteststr;
355 spargv[i] = NULL;
358 /* Create a tunable line with the duplicate values with a total number
359 larger than the different number of tunables. */
361 enum { tunables_list_size = array_length (tunable_list) };
362 const char *value = "";
363 for (int i = 0; i < tunables_list_size; i++)
364 value = xasprintf ("%sglibc.malloc.check=2%c",
365 value,
366 i == (tunables_list_size - 1) ? '\0' : ':');
367 tests[33].value = value;
369 /* Same as before, but the last tunable values is differen than the
370 rest. */
372 enum { tunables_list_size = array_length (tunable_list) };
373 const char *value = "";
374 for (int i = 0; i < tunables_list_size - 1; i++)
375 value = xasprintf ("%sglibc.malloc.check=2:", value);
376 value = xasprintf ("%sglibc.malloc.check=1", value);
377 tests[34].value = value;
379 /* Same as before, but with an invalid last entry. */
381 enum { tunables_list_size = array_length (tunable_list) };
382 const char *value = "";
383 for (int i = 0; i < tunables_list_size - 1; i++)
384 value = xasprintf ("%sglibc.malloc.check=2:", value);
385 value = xasprintf ("%sglibc.malloc.check=1=1", value);
386 tests[35].value = value;
389 for (int i = 0; i < array_length (tests); i++)
391 snprintf (nteststr, sizeof nteststr, "%d", i);
393 printf ("[%d] Spawned test for %s=%s\n",
395 tests[i].name,
396 tests[i].value);
397 setenv (tests[i].name, tests[i].value, 1);
398 struct support_capture_subprocess result
399 = support_capture_subprogram (spargv[0], spargv, NULL);
400 support_capture_subprocess_check (&result, "tst-tunables", 0,
401 sc_allow_stderr);
402 support_capture_subprocess_free (&result);
403 unsetenv (tests[i].name);
406 return 0;
409 #define TEST_FUNCTION_ARGV do_test
410 #include <support/test-driver.c>