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
23 #define TUNABLES_INTERNAL 1
24 #include <dl-tunables.h>
29 #include <support/capture_subprocess.h>
30 #include <support/check.h>
31 #include <support/support.h>
34 #define CMDLINE_OPTIONS \
35 { "restart", no_argument, &restart, 1 },
41 int32_t expected_malloc_check
;
42 size_t expected_mmap_threshold
;
43 int32_t expected_perturb
;
46 /* Expected tunable format. */
49 "glibc.malloc.check=2",
56 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
63 "glibc.malloc.mmap_threshold=-1",
68 /* Empty tunable are ignored. */
71 "glibc.malloc.check=2::glibc.malloc.mmap_threshold=4096",
76 /* As well empty values. */
79 "glibc.malloc.check=:glibc.malloc.mmap_threshold=4096",
84 /* Tunable are processed from left to right, so last one is the one set. */
87 "glibc.malloc.check=1:glibc.malloc.check=2",
94 "glibc.malloc.check=1:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
101 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=1",
106 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
109 "glibc.malloc.perturb=0x800",
116 "glibc.malloc.perturb=0x55",
121 /* Out of range values are just ignored. */
124 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
129 /* Invalid keys are ignored. */
132 ":glibc.malloc.garbage=2:glibc.malloc.check=1",
139 "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
146 "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
153 "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
158 /* Invalid subkeys are ignored. */
161 "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
168 "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
175 "not_valid.malloc.check=2",
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). */
191 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
196 /* Ill-formatted tunables string is not parsed. */
199 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
206 "glibc.malloc.check=2=2",
213 "glibc.malloc.check=2=2:glibc.malloc.mmap_threshold=4096",
220 "glibc.malloc.check=2=2:glibc.malloc.check=2",
227 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
234 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
239 /* Invalid numbers are ignored. */
242 "glibc.malloc.check=abc:glibc.malloc.mmap_threshold=4096",
249 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=abc",
257 "glibc.malloc.mmap_threshold=18446744073709551616",
262 /* Also check some tunable aliases. */
271 "MALLOC_MMAP_THRESHOLD_",
284 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
292 /* Also check for repeated tunables with a count larger than the total number
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
));
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);
343 return handle_restart (atoi (argv
[1]));
345 char nteststr
[INT_BUFSIZE_BOUND (int)];
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
;
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",
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
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",
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,
402 support_capture_subprocess_free (&result
);
403 unsetenv (tests
[i
].name
);
409 #define TEST_FUNCTION_ARGV do_test
410 #include <support/test-driver.c>