| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _GNU_SOURCE |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <sys/mman.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <sys/wait.h> |
| 28 |
#include <elf.h> |
| 29 |
#include <link.h> |
| 30 |
|
| 31 |
#include "hugetests.h" |
| 32 |
|
| 33 |
#define BLOCK_SIZE 16384 |
| 34 |
#define CONST 0xdeadbeef |
| 35 |
#define RETURN_ADDRESS 0x0 |
| 36 |
|
| 37 |
#define BIG_INIT { \ |
| 38 |
[0] = CONST, [17] = CONST, [BLOCK_SIZE-1] = CONST, \ |
| 39 |
} |
| 40 |
static int small_data = 1; |
| 41 |
static int big_data[BLOCK_SIZE] = BIG_INIT; |
| 42 |
|
| 43 |
static int small_bss; |
| 44 |
static int big_bss[BLOCK_SIZE]; |
| 45 |
|
| 46 |
const int small_const = CONST; |
| 47 |
const int big_const[BLOCK_SIZE] = BIG_INIT; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static inline void *get_text_addr(void *data) |
| 62 |
{ |
| 63 |
long (*gettext)(long) = data; |
| 64 |
|
| 65 |
return (void *)gettext(RETURN_ADDRESS); |
| 66 |
} |
| 67 |
|
| 68 |
static void __attribute__ ((noinline)) *get_pc(void) |
| 69 |
{ |
| 70 |
#if defined(__s390__) && __WORDSIZE == 32 |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
return (void *) |
| 75 |
((unsigned long) __builtin_return_address(0) & 0x7fffffffUL); |
| 76 |
#else |
| 77 |
return __builtin_return_address(0); |
| 78 |
#endif |
| 79 |
} |
| 80 |
|
| 81 |
static long static_func(long x) |
| 82 |
{ |
| 83 |
if (x == RETURN_ADDRESS) |
| 84 |
return (long)get_pc(); |
| 85 |
return x; |
| 86 |
} |
| 87 |
|
| 88 |
long global_func(long x) |
| 89 |
{ |
| 90 |
if (x == RETURN_ADDRESS) |
| 91 |
return (long)get_pc(); |
| 92 |
return x; |
| 93 |
} |
| 94 |
|
| 95 |
static struct test_entry { |
| 96 |
const char *name; |
| 97 |
void *data; |
| 98 |
int size; |
| 99 |
int writable; |
| 100 |
int execable; |
| 101 |
int is_huge; |
| 102 |
} testtab[] = { |
| 103 |
#define ENT(entry_name, exec) { \ |
| 104 |
.name = #entry_name, \ |
| 105 |
.data = (void *)&entry_name, \ |
| 106 |
.size = sizeof(entry_name), \ |
| 107 |
.writable = 0, \ |
| 108 |
.execable = exec } |
| 109 |
|
| 110 |
ENT(small_data, 0), |
| 111 |
ENT(big_data, 0), |
| 112 |
ENT(small_bss, 0), |
| 113 |
ENT(big_bss, 0), |
| 114 |
ENT(small_const, 0), |
| 115 |
ENT(big_const, 0), |
| 116 |
ENT(static_func, 1), |
| 117 |
ENT(global_func, 1), |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
#define NUM_TESTS (sizeof(testtab) / sizeof(testtab[0])) |
| 122 |
|
| 123 |
static |
| 124 |
int parse_elf(struct dl_phdr_info *info, size_t size, void *data) |
| 125 |
{ |
| 126 |
int i; |
| 127 |
unsigned long text_end, data_start; |
| 128 |
long *min_align = (long *)data; |
| 129 |
long actual_align; |
| 130 |
|
| 131 |
text_end = data_start = 0; |
| 132 |
for (i = 0; i < info->dlpi_phnum; i++) { |
| 133 |
if (info->dlpi_phdr[i].p_type != PT_LOAD) |
| 134 |
continue; |
| 135 |
|
| 136 |
if (info->dlpi_phdr[i].p_flags & PF_X) |
| 137 |
text_end = info->dlpi_phdr[i].p_vaddr + |
| 138 |
info->dlpi_phdr[i].p_memsz; |
| 139 |
else if (info->dlpi_phdr[i].p_flags & PF_W) |
| 140 |
data_start = info->dlpi_phdr[i].p_vaddr; |
| 141 |
|
| 142 |
if (text_end && data_start) |
| 143 |
break; |
| 144 |
} |
| 145 |
|
| 146 |
actual_align = (data_start - text_end) / 1024; |
| 147 |
if (actual_align < *min_align) |
| 148 |
FAIL("Binary not suitably aligned"); |
| 149 |
|
| 150 |
return 1; |
| 151 |
} |
| 152 |
|
| 153 |
static void check_if_writable(struct test_entry *te) |
| 154 |
{ |
| 155 |
int pid, ret, status; |
| 156 |
|
| 157 |
pid = fork(); |
| 158 |
if (pid < 0) |
| 159 |
FAIL("fork: %s", strerror(errno)); |
| 160 |
else if (pid == 0) { |
| 161 |
void *data; |
| 162 |
|
| 163 |
if (te->execable) |
| 164 |
data = get_text_addr(te->data); |
| 165 |
else |
| 166 |
data = te->data; |
| 167 |
|
| 168 |
(*(char *)data) = 0; |
| 169 |
exit (0); |
| 170 |
} else { |
| 171 |
ret = waitpid(pid, &status, 0); |
| 172 |
if (ret < 0) |
| 173 |
FAIL("waitpid(): %s", strerror(errno)); |
| 174 |
if (WIFSIGNALED(status)) |
| 175 |
te->writable = 0; |
| 176 |
else |
| 177 |
te->writable = 1; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
static void do_test(struct test_entry *te) |
| 182 |
{ |
| 183 |
int i; |
| 184 |
void *data = te->data; |
| 185 |
|
| 186 |
check_if_writable(te); |
| 187 |
verbose_printf("entry: %s, data: %p, writable: %d\n", |
| 188 |
te->name, data, te->writable); |
| 189 |
|
| 190 |
if (te->writable) { |
| 191 |
volatile int *p = data; |
| 192 |
|
| 193 |
for (i = 0; i < (te->size / sizeof(*p)); i++) |
| 194 |
p[i] = CONST ^ i; |
| 195 |
|
| 196 |
barrier(); |
| 197 |
|
| 198 |
for (i = 0; i < (te->size / sizeof(*p)); i++) |
| 199 |
if (p[i] != (CONST ^ i)) |
| 200 |
FAIL("mismatch on %s", te->name); |
| 201 |
} else if (te->execable) { |
| 202 |
long (*pf)(long) = data; |
| 203 |
|
| 204 |
data = get_text_addr(data); |
| 205 |
|
| 206 |
if ((*pf)(CONST) != CONST) |
| 207 |
FAIL("%s returns incorrect results", te->name); |
| 208 |
} else { |
| 209 |
|
| 210 |
volatile int *p = data; |
| 211 |
|
| 212 |
for (i = 0; i < (te->size / sizeof(*p)); i++) |
| 213 |
p[i]; |
| 214 |
} |
| 215 |
|
| 216 |
te->is_huge = (test_addr_huge(data) == 1); |
| 217 |
verbose_printf("entry: %s, data: %p, is_huge: %d\n", |
| 218 |
te->name, data, te->is_huge); |
| 219 |
} |
| 220 |
|
| 221 |
int main(int argc, char *argv[]) |
| 222 |
{ |
| 223 |
int i; |
| 224 |
char *env; |
| 225 |
int elfmap_readonly, elfmap_writable; |
| 226 |
long hpage_size = gethugepagesize() / 1024; |
| 227 |
|
| 228 |
test_init(argc, argv); |
| 229 |
|
| 230 |
|
| 231 |
if ((argc > 1) && !strcmp("--test-alignment", argv[1])) |
| 232 |
dl_iterate_phdr(parse_elf, &hpage_size); |
| 233 |
|
| 234 |
env = getenv("HUGETLB_ELFMAP"); |
| 235 |
verbose_printf("HUGETLB_ELFMAP=%s\n", env); |
| 236 |
|
| 237 |
elfmap_readonly = env && strchr(env, 'R'); |
| 238 |
elfmap_writable = env && strchr(env, 'W'); |
| 239 |
|
| 240 |
for (i = 0; i < NUM_TESTS; i++) { |
| 241 |
do_test(testtab + i); |
| 242 |
} |
| 243 |
|
| 244 |
verbose_printf("Hugepages used for:"); |
| 245 |
for (i = 0; i < NUM_TESTS; i++) |
| 246 |
if (testtab[i].is_huge) |
| 247 |
verbose_printf(" %s", testtab[i].name); |
| 248 |
verbose_printf("\n"); |
| 249 |
|
| 250 |
for (i = 0; i < NUM_TESTS; i++) { |
| 251 |
if (testtab[i].writable) { |
| 252 |
if (elfmap_writable && !testtab[i].is_huge) |
| 253 |
FAIL("%s is not hugepage", testtab[i].name); |
| 254 |
if (!elfmap_writable && testtab[i].is_huge) |
| 255 |
FAIL("%s is hugepage", testtab[i].name); |
| 256 |
} else if (!testtab[i].writable) { |
| 257 |
if (elfmap_readonly && !testtab[i].is_huge) |
| 258 |
FAIL("%s is not hugepage", testtab[i].name); |
| 259 |
if (!elfmap_readonly && testtab[i].is_huge) |
| 260 |
FAIL("%s is hugepage", testtab[i].name); |
| 261 |
} |
| 262 |
} |
| 263 |
PASS(); |
| 264 |
} |