1 /* The tunable framework. See the README.tunables to know how to use the
2 tunable in a glibc module.
4 Copyright (C) 2016-2024 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. */
23 # pragma GCC visibility push(hidden)
33 #include <array_length.h>
34 #include <dl-minimal-malloc.h>
35 #include <dl-symbol-redir-ifunc.h>
37 #define TUNABLES_INTERNAL 1
38 #include "dl-tunables.h"
41 get_next_env (char **envp
, char **name
, char **val
, char ***prev_envp
)
43 while (envp
!= NULL
&& *envp
!= NULL
)
46 char *envline
= *envp
++;
49 while (envline
[len
] != '\0' && envline
[len
] != '=')
52 /* Just the name and no value, go to the next one. */
53 if (envline
[len
] == '\0')
57 *val
= &envline
[len
+ 1];
67 do_tunable_update_val (tunable_t
*cur
, const tunable_val_t
*valp
,
68 const tunable_num_t
*minp
,
69 const tunable_num_t
*maxp
)
71 tunable_num_t val
, min
, max
;
73 switch (cur
->type
.type_code
)
75 case TUNABLE_TYPE_STRING
:
76 cur
->val
.strval
= valp
->strval
;
77 cur
->initialized
= true;
79 case TUNABLE_TYPE_INT_32
:
80 val
= (int32_t) valp
->numval
;
82 case TUNABLE_TYPE_UINT_64
:
83 val
= (int64_t) valp
->numval
;
85 case TUNABLE_TYPE_SIZE_T
:
86 val
= (size_t) valp
->numval
;
89 __builtin_unreachable ();
92 bool unsigned_cmp
= unsigned_tunable_type (cur
->type
.type_code
);
94 min
= minp
!= NULL
? *minp
: cur
->type
.min
;
95 max
= maxp
!= NULL
? *maxp
: cur
->type
.max
;
97 /* We allow only increasingly restrictive bounds. */
98 if (tunable_val_lt (min
, cur
->type
.min
, unsigned_cmp
))
101 if (tunable_val_gt (max
, cur
->type
.max
, unsigned_cmp
))
104 /* Skip both bounds if they're inconsistent. */
105 if (tunable_val_gt (min
, max
, unsigned_cmp
))
111 /* Bail out if the bounds are not valid. */
112 if (tunable_val_lt (val
, min
, unsigned_cmp
)
113 || tunable_val_lt (max
, val
, unsigned_cmp
))
116 cur
->val
.numval
= val
;
119 cur
->initialized
= true;
123 tunable_parse_num (const char *strval
, size_t len
, tunable_num_t
*val
)
126 uint64_t numval
= _dl_strtoul (strval
, &endptr
);
127 if (endptr
!= strval
+ len
)
129 *val
= (tunable_num_t
) numval
;
133 /* Validate range of the input value and initialize the tunable CUR if it looks
136 tunable_initialize (tunable_t
*cur
, const char *strval
, size_t len
)
138 tunable_val_t val
= { 0 };
140 if (cur
->type
.type_code
!= TUNABLE_TYPE_STRING
)
142 if (!tunable_parse_num (strval
, len
, &val
.numval
))
146 val
.strval
= (struct tunable_str_t
) { strval
, len
};
147 do_tunable_update_val (cur
, &val
, NULL
, NULL
);
153 __tunable_is_initialized (tunable_id_t id
)
155 return tunable_list
[id
].initialized
;
157 rtld_hidden_def (__tunable_is_initialized
)
160 __tunable_set_val (tunable_id_t id
, tunable_val_t
*valp
, tunable_num_t
*minp
,
163 tunable_t
*cur
= &tunable_list
[id
];
165 do_tunable_update_val (cur
, valp
, minp
, maxp
);
168 struct tunable_toset_t
175 enum { tunables_list_size
= array_length (tunable_list
) };
177 /* Parse the tunable string VALSTRING and set TUNABLES with the found tunables
178 and their respective values. The VALSTRING is parsed in place, with the
179 tunable start and size recorded in TUNABLES.
180 Return the number of tunables found (including 0 if the string is empty)
181 or -1 if for an ill-formatted definition. */
183 parse_tunables_string (const char *valstring
, struct tunable_toset_t
*tunables
)
185 if (valstring
== NULL
|| *valstring
== '\0')
188 const char *p
= valstring
;
194 const char *name
= p
;
196 /* First, find where the name ends. */
197 while (*p
!= '=' && *p
!= ':' && *p
!= '\0')
200 /* If we reach the end of the string before getting a valid name-value
205 /* We did not find a valid name-value pair before encountering the
216 const char *value
= p
;
218 while (*p
!= '=' && *p
!= ':' && *p
!= '\0')
226 /* Add the tunable if it exists. */
227 for (size_t i
= 0; i
< tunables_list_size
; i
++)
229 tunable_t
*cur
= &tunable_list
[i
];
231 if (tunable_is_name (cur
->name
, name
))
233 tunables
[i
] = (struct tunable_toset_t
) { cur
, value
, p
- value
};
243 parse_tunable_print_error (const struct tunable_toset_t
*toset
)
245 _dl_error_printf ("WARNING: ld.so: invalid GLIBC_TUNABLES value `%.*s' "
246 "for option `%s': ignored.\n",
253 parse_tunables (const char *valstring
)
255 struct tunable_toset_t tunables
[tunables_list_size
] = { 0 };
256 if (parse_tunables_string (valstring
, tunables
) == -1)
259 "WARNING: ld.so: invalid GLIBC_TUNABLES `%s': ignored.\n", valstring
);
263 /* Ignore tunables if enable_secure is set */
264 struct tunable_toset_t
*tsec
=
265 &tunables
[TUNABLE_ENUM_NAME(glibc
, rtld
, enable_secure
)];
269 if (!tunable_parse_num (tsec
->value
, tsec
->len
, &val
))
270 parse_tunable_print_error (tsec
);
273 __libc_enable_secure
= 1;
278 for (int i
= 0; i
< tunables_list_size
; i
++)
280 if (tunables
[i
].t
== NULL
)
283 if (!tunable_initialize (tunables
[i
].t
, tunables
[i
].value
,
285 parse_tunable_print_error (&tunables
[i
]);
289 /* Initialize the tunables list from the environment. For now we only use the
290 ENV_ALIAS to find values. Later we will also use the tunable names to find
293 __tunables_init (char **envp
)
295 char *envname
= NULL
;
297 char **prev_envp
= envp
;
299 /* Ignore tunables for AT_SECURE programs. */
300 if (__libc_enable_secure
)
303 enum { tunable_num_env_alias
= array_length (tunable_env_alias_list
) };
304 struct tunable_toset_t tunables_env_alias
[tunable_num_env_alias
] = { 0 };
306 while ((envp
= get_next_env (envp
, &envname
, &envval
, &prev_envp
)) != NULL
)
308 /* The environment variable is allocated on the stack by the kernel, so
309 it is safe to keep the references to the suboptions for later parsing
310 of string tunables. */
311 if (tunable_is_name ("GLIBC_TUNABLES", envname
))
313 parse_tunables (envval
);
317 for (int i
= 0; i
< tunable_num_env_alias
; i
++)
319 tunable_t
*cur
= &tunable_list
[tunable_env_alias_list
[i
]];
320 const char *name
= cur
->env_alias
;
325 if (tunable_is_name (name
, envname
))
327 size_t envvallen
= 0;
328 /* The environment variable is always null-terminated. */
329 for (const char *p
= envval
; *p
!= '\0'; p
++, envvallen
++);
331 tunables_env_alias
[i
] =
332 (struct tunable_toset_t
) { cur
, envval
, envvallen
};
338 /* Check if glibc.rtld.enable_secure was set and skip over the environment
339 variables aliases. */
340 if (__libc_enable_secure
)
343 for (int i
= 0; i
< tunable_num_env_alias
; i
++)
345 /* Skip over tunables that have either been set or already initialized. */
346 if (tunables_env_alias
[i
].t
== NULL
347 || tunables_env_alias
[i
].t
->initialized
)
350 if (!tunable_initialize (tunables_env_alias
[i
].t
,
351 tunables_env_alias
[i
].value
,
352 tunables_env_alias
[i
].len
))
353 parse_tunable_print_error (&tunables_env_alias
[i
]);
358 __tunables_print (void)
360 for (int i
= 0; i
< array_length (tunable_list
); i
++)
362 const tunable_t
*cur
= &tunable_list
[i
];
363 if (cur
->type
.type_code
== TUNABLE_TYPE_STRING
364 && cur
->val
.strval
.str
== NULL
)
365 _dl_printf ("%s:\n", cur
->name
);
368 _dl_printf ("%s: ", cur
->name
);
369 switch (cur
->type
.type_code
)
371 case TUNABLE_TYPE_INT_32
:
372 _dl_printf ("%d (min: %d, max: %d)\n",
373 (int) cur
->val
.numval
,
375 (int) cur
->type
.max
);
377 case TUNABLE_TYPE_UINT_64
:
378 _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
379 (long int) cur
->val
.numval
,
380 (long int) cur
->type
.min
,
381 (long int) cur
->type
.max
);
383 case TUNABLE_TYPE_SIZE_T
:
384 _dl_printf ("0x%zx (min: 0x%zx, max: 0x%zx)\n",
385 (size_t) cur
->val
.numval
,
386 (size_t) cur
->type
.min
,
387 (size_t) cur
->type
.max
);
389 case TUNABLE_TYPE_STRING
:
390 _dl_printf ("%.*s\n",
391 (int) cur
->val
.strval
.len
,
392 cur
->val
.strval
.str
);
395 __builtin_unreachable ();
402 __tunable_get_default (tunable_id_t id
, void *valp
)
404 tunable_t
*cur
= &tunable_list
[id
];
406 switch (cur
->type
.type_code
)
408 case TUNABLE_TYPE_UINT_64
:
410 *((uint64_t *) valp
) = (uint64_t) cur
->def
.numval
;
413 case TUNABLE_TYPE_INT_32
:
415 *((int32_t *) valp
) = (int32_t) cur
->def
.numval
;
418 case TUNABLE_TYPE_SIZE_T
:
420 *((size_t *) valp
) = (size_t) cur
->def
.numval
;
423 case TUNABLE_TYPE_STRING
:
425 *((const struct tunable_str_t
**)valp
) = &cur
->def
.strval
;
429 __builtin_unreachable ();
432 rtld_hidden_def (__tunable_get_default
)
434 /* Set the tunable value. This is called by the module that the tunable exists
437 __tunable_get_val (tunable_id_t id
, void *valp
, tunable_callback_t callback
)
439 tunable_t
*cur
= &tunable_list
[id
];
441 switch (cur
->type
.type_code
)
443 case TUNABLE_TYPE_UINT_64
:
445 *((uint64_t *) valp
) = (uint64_t) cur
->val
.numval
;
448 case TUNABLE_TYPE_INT_32
:
450 *((int32_t *) valp
) = (int32_t) cur
->val
.numval
;
453 case TUNABLE_TYPE_SIZE_T
:
455 *((size_t *) valp
) = (size_t) cur
->val
.numval
;
458 case TUNABLE_TYPE_STRING
:
460 *((const struct tunable_str_t
**) valp
) = &cur
->val
.strval
;
464 __builtin_unreachable ();
467 if (cur
->initialized
&& callback
!= NULL
)
468 callback (&cur
->val
);
471 rtld_hidden_def (__tunable_get_val
)