1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/* Verify that TLS blocks and the rseq area do not overlap.
Copyright (C) 2025 Free Software Foundation, Inc.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <array_length.h>
#include <elf.h>
#include <link.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/xdlfcn.h>
#include <sys/auxv.h>
#include <sys/param.h>
#include <sys/rseq.h>
#include <thread_pointer.h>
#include <unistd.h>
/* Used to keep track of address ranges. The ranges are sorted and
then checked for overlap. */
struct address_range
{
const char *prefix;
const char *label;
uintptr_t start;
size_t length;
};
struct address_range ranges[20];
size_t range_count;
static void
add_range (const char *prefix, const char *label,
const void *start, size_t length)
{
TEST_VERIFY (start != NULL);
TEST_VERIFY (length > 0);
TEST_VERIFY_EXIT (range_count < array_length (ranges));
ranges[range_count].prefix = prefix;
ranges[range_count].label = label;
ranges[range_count].start = (uintptr_t) start;
ranges[range_count].length = length;
++range_count;
}
static int
range_compare (const void *a1, const void *b1)
{
const struct address_range *a = a1;
const struct address_range *b = b1;
if (a->start < b->start)
return -1;
if (a->start > b->start)
return 1;
return 0;
}
static void
check_for_overlap (void)
{
qsort (ranges, range_count, sizeof (ranges[0]), range_compare);
uintptr_t previous_end = ranges[0].start + ranges[0].length - 1;
for (size_t i = 1; i < range_count; ++i)
{
uintptr_t this_end = ranges[i].start + ranges[i].length - 1;
if (ranges[i].start <= previous_end)
{
puts ("error: overlap between address ranges");
printf (" %s%s: [0x%lx, 0x%lx)\n",
ranges[i - 1].prefix, ranges[i - 1].label,
(unsigned long int) ranges[i - 1].start,
(unsigned long int) previous_end);
printf (" %s%s: [0x%lx, 0x%lx)\n",
ranges[i].prefix, ranges[i].label,
(unsigned long int) ranges[i].start,
(unsigned long int) this_end);
}
previous_end = this_end;
}
}
static void
add_rseq (void)
{
/* The initial size of 32 bytes is always allocated. The value
reported by __rseq_size does not include the alignment, which can
be larger than 32 if requested by the kernel through the
auxiliary vector. */
size_t size = 32;
if (__rseq_size > 0)
size = roundup (__rseq_size, MAX (getauxval (AT_RSEQ_ALIGN), 32));
printf ("info: adding rseq area of %zu bytes\n", size);
add_range ("", "rseq area",
(char *) __thread_pointer () + __rseq_offset, size);
}
/* These functions add the TLS data for all loaded modules to the
recorded address ranges. */
static int
dlip_callback (struct dl_phdr_info *info, size_t size, void *ignored)
{
/* If the dynamic linker does not provide TLS address information,
there is nothing to register. */
if (info->dlpi_tls_data == NULL)
return 0;
for (int i = 0; i < info->dlpi_phnum; ++i)
{
if (info->dlpi_phdr[i].p_type == PT_TLS)
{
printf ("info: adding TLS range for \"%s\" (%zu bytes)\n",
info->dlpi_name, (size_t) info->dlpi_phdr[i].p_memsz);
add_range ("TLS for ",
info->dlpi_name[0] != '\0' ? info->dlpi_name : "main",
info->dlpi_tls_data, info->dlpi_phdr[i].p_memsz);
break;
}
}
return 0;
}
/* Returns true if any TLS ranges were found. */
static void
add_tls_ranges (void)
{
dl_iterate_phdr (dlip_callback, NULL);
}
volatile __thread int thread_var __attribute__ ((aligned (MAIN_TLS_ALIGN)));
static int
do_test (void)
{
void *original_brk = sbrk (0);
void *initial_allocation = malloc (16);
/* Ensure that the variable is not optimized away. */
thread_var = 0;
printf ("info: rseq area size: %u\n", __rseq_size);
puts ("info: checking address ranges with initially loaded modules");
add_range ("", "program break", original_brk, 1);
add_range ("", "malloc allocation", initial_allocation, 16);
add_rseq ();
add_tls_ranges ();
printf ("info: %zu ranges found\n", range_count);
check_for_overlap ();
range_count = 0;
puts ("info: checking address ranges after dlopen");
void *handle = xdlopen ("tst-rseq-tls-range-mod.so", RTLD_NOW);
int *mod_thread_var = xdlsym (handle, "mod_thread_var");
add_range ("", "program break", original_brk, 1);
add_range ("", "malloc allocation", initial_allocation, 16);
add_rseq ();
add_tls_ranges ();
{
bool found_objects = false;
for (size_t i = 0; i < range_count; ++i)
if (strchr (ranges[i].label, '/') != NULL)
found_objects = true;
if (!found_objects)
/* __tls_get_addr does not fully work with static dlopen.
Add some fall-back test data. */
add_range ("", "mod_thread_var",
mod_thread_var, sizeof (*mod_thread_var));
}
printf ("info: %zu ranges found\n", range_count);
check_for_overlap ();
xdlclose (handle);
free (initial_allocation);
return 0;
}
#include <support/test-driver.c>
|