elf: Do not parse ill-formatted strings
[glibc.git] / elf / tst-tunables.c
blobe1ad44f27c544a12dcb1213edc66ca0009a882a9
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 *env;
35 int32_t expected_malloc_check;
36 size_t expected_mmap_threshold;
37 int32_t expected_perturb;
38 } tests[] =
40 /* Expected tunable format. */
42 "glibc.malloc.check=2",
48 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
50 4096,
53 /* Empty tunable are ignored. */
55 "glibc.malloc.check=2::glibc.malloc.mmap_threshold=4096",
57 4096,
60 /* As well empty values. */
62 "glibc.malloc.check=:glibc.malloc.mmap_threshold=4096",
64 4096,
67 /* Tunable are processed from left to right, so last one is the one set. */
69 "glibc.malloc.check=1:glibc.malloc.check=2",
75 "glibc.malloc.check=1:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
77 4096,
81 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=1",
83 4096,
86 /* 0x800 is larger than tunable maxval (0xff), so the tunable is unchanged. */
88 "glibc.malloc.perturb=0x800",
94 "glibc.malloc.perturb=0x55",
97 0x55,
99 /* Out of range values are just ignored. */
101 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
103 4096,
106 /* Invalid keys are ignored. */
108 ":glibc.malloc.garbage=2:glibc.malloc.check=1",
114 "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
116 4096,
120 "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
122 4096,
126 "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
128 4096,
131 /* Invalid subkeys are ignored. */
133 "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
139 "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
145 "not_valid.malloc.check=2",
151 "glibc.not_valid.check=2",
156 /* An ill-formatted tunable in the for key=key=value will considere the
157 value as 'key=value' (which can not be parsed as an integer). */
159 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
164 /* Ill-formatted tunables string is not parsed. */
166 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
172 "glibc.malloc.check=2=2",
178 "glibc.malloc.check=2=2:glibc.malloc.mmap_threshold=4096",
184 "glibc.malloc.check=2=2:glibc.malloc.check=2",
190 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
196 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096",
203 static int
204 handle_restart (int i)
206 TEST_COMPARE (tests[i].expected_malloc_check,
207 TUNABLE_GET_FULL (glibc, malloc, check, int32_t, NULL));
208 TEST_COMPARE (tests[i].expected_mmap_threshold,
209 TUNABLE_GET_FULL (glibc, malloc, mmap_threshold, size_t, NULL));
210 TEST_COMPARE (tests[i].expected_perturb,
211 TUNABLE_GET_FULL (glibc, malloc, perturb, int32_t, NULL));
212 return 0;
215 static int
216 do_test (int argc, char *argv[])
218 /* We must have either:
219 - One our fource parameters left if called initially:
220 + path to ld.so optional
221 + "--library-path" optional
222 + the library path optional
223 + the application name
224 + the test to check */
226 TEST_VERIFY_EXIT (argc == 2 || argc == 5);
228 if (restart)
229 return handle_restart (atoi (argv[1]));
231 char nteststr[INT_BUFSIZE_BOUND (int)];
233 char *spargv[10];
235 int i = 0;
236 for (; i < argc - 1; i++)
237 spargv[i] = argv[i + 1];
238 spargv[i++] = (char *) "--direct";
239 spargv[i++] = (char *) "--restart";
240 spargv[i++] = nteststr;
241 spargv[i] = NULL;
244 for (int i = 0; i < array_length (tests); i++)
246 snprintf (nteststr, sizeof nteststr, "%d", i);
248 printf ("[%d] Spawned test for %s\n", i, tests[i].env);
249 setenv ("GLIBC_TUNABLES", tests[i].env, 1);
250 struct support_capture_subprocess result
251 = support_capture_subprogram (spargv[0], spargv);
252 support_capture_subprocess_check (&result, "tst-tunables", 0,
253 sc_allow_stderr);
254 support_capture_subprocess_free (&result);
257 return 0;
260 #define TEST_FUNCTION_ARGV do_test
261 #include <support/test-driver.c>