Initialize tunable list with the GLIBC_TUNABLES environment variable

Read tunables values from the users using the GLIBC_TUNABLES
environment variable.  The value of this variable is a colon-separated
list of name=value pairs.  So a typical string would look like this:

GLIBC_TUNABLES=glibc.malloc.mmap_threshold=2048:glibc.malloc.trim_threshold=1024

	* config.make.in (have-loop-to-function): Define.
	* elf/Makefile (CFLAGS-dl-tunables.c): Add
	-fno-tree-loop-distribute-patterns.
	* elf/dl-tunables.c: Include libc-internals.h.
	(GLIBC_TUNABLES): New macro.
	(tunables_strdup): New function.
	(parse_tunables): New function.
	(min_strlen): New function.
	(__tunables_init): Use the new functions and macro.
	(disable_tunable): Disable tunable from GLIBC_TUNABLES.
	* malloc/tst-malloc-usable-tunables.c: New test case.
	* malloc/tst-malloc-usable-static-tunables.c: New test case.
	* malloc/Makefile (tests, tests-static): Add tests.
This commit is contained in:
Siddhesh Poyarekar 2016-12-31 23:33:27 +05:30
parent 67e58f3941
commit 9dd409a5f4
7 changed files with 156 additions and 2 deletions

View File

@ -1,5 +1,19 @@
2016-12-31 Siddhesh Poyarekar <siddhesh@sourceware.org> 2016-12-31 Siddhesh Poyarekar <siddhesh@sourceware.org>
* config.make.in (have-loop-to-function): Define.
* elf/Makefile (CFLAGS-dl-tunables.c): Add
-fno-tree-loop-distribute-patterns.
* elf/dl-tunables.c: Include libc-internals.h.
(GLIBC_TUNABLES): New macro.
(tunables_strdup): New function.
(parse_tunables): New function.
(min_strlen): New function.
(__tunables_init): Use the new functions and macro.
(disable_tunable): Disable tunable from GLIBC_TUNABLES.
* malloc/tst-malloc-usable-tunables.c: New test case.
* malloc/tst-malloc-usable-static-tunables.c: New test case.
* malloc/Makefile (tests, tests-static): Add tests.
* manual/install.texi: Add --enable-tunables option. * manual/install.texi: Add --enable-tunables option.
* INSTALL: Regenerate. * INSTALL: Regenerate.
* README.tunables: New file. * README.tunables: New file.

View File

@ -71,6 +71,7 @@ have-hash-style = @libc_cv_hashstyle@
use-default-link = @use_default_link@ use-default-link = @use_default_link@
output-format = @libc_cv_output_format@ output-format = @libc_cv_output_format@
have-cxx-thread_local = @libc_cv_cxx_thread_local@ have-cxx-thread_local = @libc_cv_cxx_thread_local@
have-loop-to-function = @libc_cv_cc_loop_to_function@
multi-arch = @multi_arch@ multi-arch = @multi_arch@

View File

@ -38,6 +38,12 @@ endif
ifeq (yes,$(have-tunables)) ifeq (yes,$(have-tunables))
dl-routines += dl-tunables dl-routines += dl-tunables
# Make sure that the compiler does not insert any library calls in tunables
# code paths.
ifeq (yes,$(have-loop-to-function))
CFLAGS-dl-tunables.c = -fno-tree-loop-distribute-patterns
endif
endif endif
all-dl-routines = $(dl-routines) $(sysdep-dl-routines) all-dl-routines = $(dl-routines) $(sysdep-dl-routines)

View File

@ -30,7 +30,10 @@
#define TUNABLES_INTERNAL 1 #define TUNABLES_INTERNAL 1
#include "dl-tunables.h" #include "dl-tunables.h"
/* Compare environment names, bounded by the name hardcoded in glibc. */ #define GLIBC_TUNABLES "GLIBC_TUNABLES"
/* Compare environment or tunable names, bounded by the name hardcoded in
glibc. */
static bool static bool
is_name (const char *orig, const char *envname) is_name (const char *orig, const char *envname)
{ {
@ -45,6 +48,29 @@ is_name (const char *orig, const char *envname)
return false; return false;
} }
static char *
tunables_strdup (const char *in)
{
size_t i = 0;
while (in[i++] != '\0');
char *out = __sbrk (i);
/* FIXME: In reality if the allocation fails, __sbrk will crash attempting to
set the thread-local errno since the TCB has not yet been set up. This
needs to be fixed with an __sbrk implementation that does not set
errno. */
if (out == (void *)-1)
return NULL;
i--;
while (i-- > 0)
out[i] = in[i];
return out;
}
static char ** static char **
get_next_env (char **envp, char **name, size_t *namelen, char **val) get_next_env (char **envp, char **name, size_t *namelen, char **val)
{ {
@ -218,6 +244,82 @@ tunable_initialize (tunable_t *cur, const char *strval)
} }
} }
static void
parse_tunables (char *tunestr)
{
if (tunestr == NULL || *tunestr == '\0')
return;
char *p = tunestr;
while (true)
{
char *name = p;
size_t len = 0;
/* First, find where the name ends. */
while (p[len] != '=' && p[len] != ':' && p[len] != '\0')
len++;
/* If we reach the end of the string before getting a valid name-value
pair, bail out. */
if (p[len] == '\0')
return;
/* We did not find a valid name-value pair before encountering the
colon. */
if (p[len]== ':')
{
p += len + 1;
continue;
}
p += len + 1;
char *value = p;
len = 0;
while (p[len] != ':' && p[len] != '\0')
len++;
char end = p[len];
p[len] = '\0';
/* Add the tunable if it exists. */
for (size_t i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++)
{
tunable_t *cur = &tunable_list[i];
/* If we are in a secure context (AT_SECURE) then ignore the tunable
unless it is explicitly marked as secure. Tunable values take
precendence over their envvar aliases. */
if (__libc_enable_secure && !cur->is_secure)
continue;
if (is_name (cur->name, name))
{
tunable_initialize (cur, value);
break;
}
}
if (end == ':')
p += len + 1;
else
return;
}
}
static size_t
min_strlen (const char *s)
{
size_t i = 0;
while (*s++ != '\0')
i++;
return i;
}
/* Disable a tunable if it is set. */ /* Disable a tunable if it is set. */
static void static void
disable_tunable (tunable_id_t id, char **envp) disable_tunable (tunable_id_t id, char **envp)
@ -226,6 +328,23 @@ disable_tunable (tunable_id_t id, char **envp)
if (env_alias != NULL) if (env_alias != NULL)
tunables_unsetenv (envp, tunable_list[id].env_alias); tunables_unsetenv (envp, tunable_list[id].env_alias);
char *tunable = getenv (GLIBC_TUNABLES);
const char *cmp = tunable_list[id].name;
const size_t len = min_strlen (cmp);
while (tunable && *tunable != '\0' && *tunable != ':')
{
if (is_name (tunable, cmp))
{
tunable += len;
/* Overwrite the = and the value with colons. */
while (*tunable != '\0' && *tunable != ':')
*tunable++ = ':';
break;
}
tunable++;
}
} }
/* Disable the glibc.malloc.check tunable in SETUID/SETGID programs unless /* Disable the glibc.malloc.check tunable in SETUID/SETGID programs unless
@ -256,6 +375,14 @@ __tunables_init (char **envp)
while ((envp = get_next_env (envp, &envname, &len, &envval)) != NULL) while ((envp = get_next_env (envp, &envname, &len, &envval)) != NULL)
{ {
if (is_name (GLIBC_TUNABLES, envname))
{
char *val = tunables_strdup (envval);
if (val != NULL)
parse_tunables (val);
continue;
}
for (int i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++) for (int i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++)
{ {
tunable_t *cur = &tunable_list[i]; tunable_t *cur = &tunable_list[i];

View File

@ -33,11 +33,13 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
tst-mallocfork2 \ tst-mallocfork2 \
tst-interpose-nothread \ tst-interpose-nothread \
tst-interpose-thread \ tst-interpose-thread \
tst-malloc-usable-tunables
tests-static := \ tests-static := \
tst-interpose-static-nothread \ tst-interpose-static-nothread \
tst-interpose-static-thread \ tst-interpose-static-thread \
tst-malloc-usable-static tst-malloc-usable-static \
tst-malloc-usable-static-tunables
tests += $(tests-static) tests += $(tests-static)
test-srcs = tst-mtrace test-srcs = tst-mtrace
@ -160,6 +162,8 @@ endif
tst-mcheck-ENV = MALLOC_CHECK_=3 tst-mcheck-ENV = MALLOC_CHECK_=3
tst-malloc-usable-ENV = MALLOC_CHECK_=3 tst-malloc-usable-ENV = MALLOC_CHECK_=3
tst-malloc-usable-static-ENV = $(tst-malloc-usable-ENV) tst-malloc-usable-static-ENV = $(tst-malloc-usable-ENV)
tst-malloc-usable-tunables-ENV = GLIBC_TUNABLES=glibc.malloc.check=3
tst-malloc-usable-static-tunables-ENV = $(tst-malloc-usable-tunables-ENV)
# Uncomment this for test releases. For public releases it is too expensive. # Uncomment this for test releases. For public releases it is too expensive.
#CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1 #CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1

View File

@ -0,0 +1 @@
#include <malloc/tst-malloc-usable.c>

View File

@ -0,0 +1 @@
#include <malloc/tst-malloc-usable.c>