elf: Add glibc.mem.decorate_maps tunable
[glibc.git] / sysdeps / unix / sysv / linux / setvmaname.c
blobcd6d571772758c4f1140aab8a3b2bc9246b6435f
1 /* Utilities functions to name memory mappings.
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 <ldsodefs.h>
20 #include <setvmaname.h>
21 #include <sys/prctl.h>
22 #include <sysdep.h>
23 #include <elf/dl-tunables.h>
25 /* If PR_SET_VMA_ANON_NAME is not supported by the kernel, prctl returns
26 EINVAL. However, it also returns the same error for invalid argument.
27 Since it is an internal-only API, it assumes well formatted input:
28 aligned START, with (START, START+LEN) being a valid memory range,
29 and NAME with a limit of 80 characters without invalid one ("\\`$[]"). */
31 void
32 __set_vma_name (void *start, size_t len, const char *name)
34 static int prctl_supported = 1;
35 if (atomic_load_relaxed (&prctl_supported) == 0)
36 return;
38 /* Set the prctl as not supported to avoid checking the tunable on every
39 call. */
40 if (TUNABLE_GET (glibc, mem, decorate_maps, int32_t, NULL) != 0)
42 int r = INTERNAL_SYSCALL_CALL (prctl, PR_SET_VMA, PR_SET_VMA_ANON_NAME,
43 start, len, name);
44 if (r == 0 || r != -EINVAL)
45 return;
47 atomic_store_relaxed (&prctl_supported, 0);
48 return;