elf: Do not duplicate the GLIBC_TUNABLES string
[glibc.git] / elf / dl-tunables.c
blob3d41e8e28e364a7ff4df7d58b7cb5f039af7c8bf
1 /* The tunable framework. See the README.tunables to know how to use the
2 tunable in a glibc module.
4 Copyright (C) 2016-2023 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
21 /* Mark symbols hidden in static PIE for early self relocation to work. */
22 #if BUILD_PIE_DEFAULT
23 # pragma GCC visibility push(hidden)
24 #endif
25 #include <startup.h>
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <sysdep.h>
31 #include <fcntl.h>
32 #include <ldsodefs.h>
33 #include <array_length.h>
34 #include <dl-minimal-malloc.h>
36 #define TUNABLES_INTERNAL 1
37 #include "dl-tunables.h"
39 static char **
40 get_next_env (char **envp, char **name, char **val, char ***prev_envp)
42 while (envp != NULL && *envp != NULL)
44 char **prev = envp;
45 char *envline = *envp++;
46 int len = 0;
48 while (envline[len] != '\0' && envline[len] != '=')
49 len++;
51 /* Just the name and no value, go to the next one. */
52 if (envline[len] == '\0')
53 continue;
55 *name = envline;
56 *val = &envline[len + 1];
57 *prev_envp = prev;
59 return envp;
62 return NULL;
65 static void
66 do_tunable_update_val (tunable_t *cur, const tunable_val_t *valp,
67 const tunable_num_t *minp,
68 const tunable_num_t *maxp)
70 tunable_num_t val, min, max;
72 if (cur->type.type_code == TUNABLE_TYPE_STRING)
74 cur->val.strval = valp->strval;
75 cur->initialized = true;
76 return;
79 bool unsigned_cmp = unsigned_tunable_type (cur->type.type_code);
81 val = valp->numval;
82 min = minp != NULL ? *minp : cur->type.min;
83 max = maxp != NULL ? *maxp : cur->type.max;
85 /* We allow only increasingly restrictive bounds. */
86 if (tunable_val_lt (min, cur->type.min, unsigned_cmp))
87 min = cur->type.min;
89 if (tunable_val_gt (max, cur->type.max, unsigned_cmp))
90 max = cur->type.max;
92 /* Skip both bounds if they're inconsistent. */
93 if (tunable_val_gt (min, max, unsigned_cmp))
95 min = cur->type.min;
96 max = cur->type.max;
99 /* Bail out if the bounds are not valid. */
100 if (tunable_val_lt (val, min, unsigned_cmp)
101 || tunable_val_lt (max, val, unsigned_cmp))
102 return;
104 cur->val.numval = val;
105 cur->type.min = min;
106 cur->type.max = max;
107 cur->initialized = true;
110 /* Validate range of the input value and initialize the tunable CUR if it looks
111 good. */
112 static void
113 tunable_initialize (tunable_t *cur, const char *strval, size_t len)
115 tunable_val_t val = { 0 };
117 if (cur->type.type_code != TUNABLE_TYPE_STRING)
118 val.numval = (tunable_num_t) _dl_strtoul (strval, NULL);
119 else
120 val.strval = (struct tunable_str_t) { strval, len };
121 do_tunable_update_val (cur, &val, NULL, NULL);
124 bool
125 __tunable_is_initialized (tunable_id_t id)
127 return tunable_list[id].initialized;
129 rtld_hidden_def (__tunable_is_initialized)
131 void
132 __tunable_set_val (tunable_id_t id, tunable_val_t *valp, tunable_num_t *minp,
133 tunable_num_t *maxp)
135 tunable_t *cur = &tunable_list[id];
137 do_tunable_update_val (cur, valp, minp, maxp);
140 struct tunable_toset_t
142 tunable_t *t;
143 const char *value;
144 size_t len;
147 enum { tunables_list_size = array_length (tunable_list) };
149 /* Parse the tunable string VALSTRING and set TUNABLES with the found tunables
150 and their respective values. The VALSTRING is parsed in place, with the
151 tunable start and size recorded in TUNABLES.
152 Return the number of tunables found (including 0 if the string is empty)
153 or -1 if for an ill-formatted definition. */
154 static int
155 parse_tunables_string (const char *valstring, struct tunable_toset_t *tunables)
157 if (valstring == NULL || *valstring == '\0')
158 return 0;
160 const char *p = valstring;
161 bool done = false;
162 int ntunables = 0;
164 while (!done)
166 const char *name = p;
168 /* First, find where the name ends. */
169 while (*p != '=' && *p != ':' && *p != '\0')
170 p++;
172 /* If we reach the end of the string before getting a valid name-value
173 pair, bail out. */
174 if (*p == '\0')
175 return -1;
177 /* We did not find a valid name-value pair before encountering the
178 colon. */
179 if (*p == ':')
181 p++;
182 continue;
185 /* Skip the '='. */
186 p++;
188 const char *value = p;
190 while (*p != '=' && *p != ':' && *p != '\0')
191 p++;
193 if (*p == '=')
194 return -1;
195 else if (*p == '\0')
196 done = true;
198 /* Add the tunable if it exists. */
199 for (size_t i = 0; i < tunables_list_size; i++)
201 tunable_t *cur = &tunable_list[i];
203 if (tunable_is_name (cur->name, name))
205 tunables[ntunables++] =
206 (struct tunable_toset_t) { cur, value, p - value };
207 break;
212 return ntunables;
215 static void
216 parse_tunables (const char *valstring)
218 struct tunable_toset_t tunables[tunables_list_size];
219 int ntunables = parse_tunables_string (valstring, tunables);
220 if (ntunables == -1)
222 _dl_error_printf (
223 "WARNING: ld.so: invalid GLIBC_TUNABLES `%s': ignored.\n", valstring);
224 return;
227 for (int i = 0; i < ntunables; i++)
228 tunable_initialize (tunables[i].t, tunables[i].value, tunables[i].len);
231 /* Initialize the tunables list from the environment. For now we only use the
232 ENV_ALIAS to find values. Later we will also use the tunable names to find
233 values. */
234 void
235 __tunables_init (char **envp)
237 char *envname = NULL;
238 char *envval = NULL;
239 char **prev_envp = envp;
241 /* Ignore tunables for AT_SECURE programs. */
242 if (__libc_enable_secure)
243 return;
245 while ((envp = get_next_env (envp, &envname, &envval, &prev_envp)) != NULL)
247 /* The environment variable is allocated on the stack by the kernel, so
248 it is safe to keep the references to the suboptions for later parsing
249 of string tunables. */
250 if (tunable_is_name ("GLIBC_TUNABLES", envname))
252 parse_tunables (envval);
253 continue;
256 for (int i = 0; i < tunables_list_size; i++)
258 tunable_t *cur = &tunable_list[i];
260 /* Skip over tunables that have either been set already or should be
261 skipped. */
262 if (cur->initialized || cur->env_alias[0] == '\0')
263 continue;
265 const char *name = cur->env_alias;
267 /* We have a match. Initialize and move on to the next line. */
268 if (tunable_is_name (name, envname))
270 size_t envvallen = 0;
271 /* The environment variable is always null-terminated. */
272 for (const char *p = envval; *p != '\0'; p++, envvallen++);
274 tunable_initialize (cur, envval, envvallen);
275 break;
281 void
282 __tunables_print (void)
284 for (int i = 0; i < array_length (tunable_list); i++)
286 const tunable_t *cur = &tunable_list[i];
287 if (cur->type.type_code == TUNABLE_TYPE_STRING
288 && cur->val.strval.str == NULL)
289 _dl_printf ("%s:\n", cur->name);
290 else
292 _dl_printf ("%s: ", cur->name);
293 switch (cur->type.type_code)
295 case TUNABLE_TYPE_INT_32:
296 _dl_printf ("%d (min: %d, max: %d)\n",
297 (int) cur->val.numval,
298 (int) cur->type.min,
299 (int) cur->type.max);
300 break;
301 case TUNABLE_TYPE_UINT_64:
302 _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
303 (long int) cur->val.numval,
304 (long int) cur->type.min,
305 (long int) cur->type.max);
306 break;
307 case TUNABLE_TYPE_SIZE_T:
308 _dl_printf ("0x%zx (min: 0x%zx, max: 0x%zx)\n",
309 (size_t) cur->val.numval,
310 (size_t) cur->type.min,
311 (size_t) cur->type.max);
312 break;
313 case TUNABLE_TYPE_STRING:
314 _dl_printf ("%.*s\n",
315 (int) cur->val.strval.len,
316 cur->val.strval.str);
317 break;
318 default:
319 __builtin_unreachable ();
325 void
326 __tunable_get_default (tunable_id_t id, void *valp)
328 tunable_t *cur = &tunable_list[id];
330 switch (cur->type.type_code)
332 case TUNABLE_TYPE_UINT_64:
334 *((uint64_t *) valp) = (uint64_t) cur->def.numval;
335 break;
337 case TUNABLE_TYPE_INT_32:
339 *((int32_t *) valp) = (int32_t) cur->def.numval;
340 break;
342 case TUNABLE_TYPE_SIZE_T:
344 *((size_t *) valp) = (size_t) cur->def.numval;
345 break;
347 case TUNABLE_TYPE_STRING:
349 *((const struct tunable_str_t **)valp) = &cur->def.strval;
350 break;
352 default:
353 __builtin_unreachable ();
356 rtld_hidden_def (__tunable_get_default)
358 /* Set the tunable value. This is called by the module that the tunable exists
359 in. */
360 void
361 __tunable_get_val (tunable_id_t id, void *valp, tunable_callback_t callback)
363 tunable_t *cur = &tunable_list[id];
365 switch (cur->type.type_code)
367 case TUNABLE_TYPE_UINT_64:
369 *((uint64_t *) valp) = (uint64_t) cur->val.numval;
370 break;
372 case TUNABLE_TYPE_INT_32:
374 *((int32_t *) valp) = (int32_t) cur->val.numval;
375 break;
377 case TUNABLE_TYPE_SIZE_T:
379 *((size_t *) valp) = (size_t) cur->val.numval;
380 break;
382 case TUNABLE_TYPE_STRING:
384 *((const struct tunable_str_t **) valp) = &cur->val.strval;
385 break;
387 default:
388 __builtin_unreachable ();
391 if (cur->initialized && callback != NULL)
392 callback (&cur->val);
395 rtld_hidden_def (__tunable_get_val)