| 1 |
/* Test program for 64-Bit Microsoft to System V function calls. |
| 2 |
Copyright (C) 2016-2017 Free Software Foundation, Inc. |
| 3 |
Contributed by Daniel Santos <[email protected]> |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC 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 |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
Under Section 7 of GPL version 3, you are granted additional |
| 18 |
permissions described in the GCC Runtime Library Exception, version |
| 19 |
3.1, as published by the Free Software Foundation. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License and |
| 22 |
a copy of the GCC Runtime Library Exception along with this program; |
| 23 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 24 |
<http://www.gnu.org/licenses/>. */ |
| 25 |
|
| 26 |
/* This is a single-threaded test program for Microsoft 64-bit ABI functions. |
| 27 |
It is aimed at verifying correctness of pro/epilogues of ms_abi functions |
| 28 |
that call sysv_abi functions to assure clobbered registers are properly |
| 29 |
saved and restored and attempt to detect any flaws in the behavior of these |
| 30 |
functions. The following variants are tested: |
| 31 |
|
| 32 |
* Either uses hard frame pointer, re-aligns the stack or neither, |
| 33 |
* Uses alloca (and thus DRAP) or not, |
| 34 |
* Uses sibling call optimization or not, |
| 35 |
* Uses variable argument list or not, and |
| 36 |
* Has shrink-wrapped code or not. |
| 37 |
|
| 38 |
In addition, an ms_abi function is generated for each of these combinations |
| 39 |
clobbering each unique combination additional registers (excluding BP when |
| 40 |
a frame pointer is used). Shrink-wrap variants are called in a way that |
| 41 |
both the fast and slow path are used. Re-aligned variants are called with |
| 42 |
an aligned and mis-aligned stack. |
| 43 |
|
| 44 |
Each ms_abi function is called via an assembly stub that first saves all |
| 45 |
volatile registers and fills them with random values. The ms_abi function |
| 46 |
is then called. After the function returns, the value of all volatile |
| 47 |
registers is verified against the random data and then restored. */ |
| 48 |
|
| 49 |
/* { dg-do run } */ |
| 50 |
/* { dg-additional-sources "do-test.S" } */ |
| 51 |
/* { dg-additional-options "-Wall" } */ |
| 52 |
|
| 53 |
#include <stdio.h> |
| 54 |
#include <string.h> |
| 55 |
#include <stdlib.h> |
| 56 |
#include <signal.h> |
| 57 |
#include <unistd.h> |
| 58 |
#include <stdint.h> |
| 59 |
#include <stdarg.h> |
| 60 |
#include <assert.h> |
| 61 |
#include <errno.h> |
| 62 |
#include <ctype.h> |
| 63 |
|
| 64 |
#if !defined(__x86_64__) || !defined(__SSE2__) |
| 65 |
# error Test only valid on x86_64 with -msse2 |
| 66 |
#endif |
| 67 |
|
| 68 |
enum reg_data_sets |
| 69 |
{ |
| 70 |
REG_SET_SAVE, |
| 71 |
REG_SET_INPUT, |
| 72 |
REG_SET_OUTPUT, |
| 73 |
|
| 74 |
REG_SET_COUNT |
| 75 |
}; |
| 76 |
|
| 77 |
enum flags |
| 78 |
{ |
| 79 |
FLAG_ALLOCA = 0x01000000, |
| 80 |
FLAG_SIBCALL = 0x02000000, |
| 81 |
FLAG_SHRINK_WRAP_FAST_PATH = 0x08000000, |
| 82 |
FLAG_SHRINK_WRAP_SLOW_PATH = 0x0c000000, |
| 83 |
}; |
| 84 |
|
| 85 |
enum alignment_option |
| 86 |
{ |
| 87 |
ALIGNMENT_NOT_TESTED, |
| 88 |
ALIGNMENT_ALIGNED, |
| 89 |
ALIGNMENT_MISALIGNED, |
| 90 |
|
| 91 |
ALIGNMENT_COUNT, |
| 92 |
}; |
| 93 |
|
| 94 |
enum shrink_wrap_option |
| 95 |
{ |
| 96 |
SHRINK_WRAP_NONE, |
| 97 |
SHRINK_WRAP_FAST_PATH, |
| 98 |
SHRINK_WRAP_SLOW_PATH, |
| 99 |
|
| 100 |
SHRINK_WRAP_COUNT |
| 101 |
}; |
| 102 |
|
| 103 |
union regdata { |
| 104 |
struct { |
| 105 |
__uint128_t sseregs[10]; |
| 106 |
union { |
| 107 |
uint64_t intregs[8]; |
| 108 |
struct { |
| 109 |
uint64_t rsi; |
| 110 |
uint64_t rdi; |
| 111 |
uint64_t rbx; |
| 112 |
uint64_t rbp; |
| 113 |
uint64_t r12; |
| 114 |
uint64_t r13; |
| 115 |
uint64_t r14; |
| 116 |
uint64_t r15; |
| 117 |
}; |
| 118 |
}; |
| 119 |
}; |
| 120 |
uint32_t u32_arr[56]; |
| 121 |
} __attribute__((aligned (16))); |
| 122 |
|
| 123 |
struct test_data |
| 124 |
{ |
| 125 |
union regdata regdata[REG_SET_COUNT]; |
| 126 |
void *fn; |
| 127 |
void *retaddr; |
| 128 |
const char *name; |
| 129 |
enum alignment_option alignment; |
| 130 |
enum shrink_wrap_option shrink_wrap; |
| 131 |
long ret_expected; |
| 132 |
} test_data; |
| 133 |
|
| 134 |
static int shrink_wrap_global; |
| 135 |
static void __attribute((sysv_abi)) do_tests (); |
| 136 |
static void init_test (void *fn, const char *name, |
| 137 |
enum alignment_option alignment, |
| 138 |
enum shrink_wrap_option shrink_wrap, long ret_expected); |
| 139 |
static void check_results (long ret); |
| 140 |
static __attribute__((ms_abi)) long do_sibcall (long arg); |
| 141 |
static __attribute__((ms_abi)) long |
| 142 |
(*const volatile do_sibcall_noinfo) (long) = do_sibcall; |
| 143 |
|
| 144 |
/* Defines do_tests (). */ |
| 145 |
#include "ms-sysv-generated.h" |
| 146 |
|
| 147 |
static int arbitrarily_fail; |
| 148 |
static const char *argv0; |
| 149 |
|
| 150 |
|
| 151 |
#define PASTE_STR2(a) #a |
| 152 |
#define PASTE_STR1(a, b) PASTE_STR2(a ## b) |
| 153 |
#define PASTE_STR(a, b) PASTE_STR1(a, b) |
| 154 |
|
| 155 |
#ifdef __USER_LABEL_PREFIX__ |
| 156 |
# define ASMNAME(name) PASTE_STR(__USER_LABEL_PREFIX__, name) |
| 157 |
#else |
| 158 |
# define ASMNAME(name) #name |
| 159 |
#endif |
| 160 |
|
| 161 |
#ifdef __MACH__ |
| 162 |
# define LOAD_TEST_DATA_ADDR(dest) \ |
| 163 |
" mov " ASMNAME(test_data) "@GOTPCREL(%%rip), " dest "\n" |
| 164 |
#else |
| 165 |
# define LOAD_TEST_DATA_ADDR(dest) \ |
| 166 |
" lea " ASMNAME(test_data) "(%%rip), " dest "\n" |
| 167 |
#endif |
| 168 |
|
| 169 |
#define TEST_DATA_OFFSET(f) ((int)__builtin_offsetof(struct test_data, f)) |
| 170 |
|
| 171 |
void __attribute__((naked)) |
| 172 |
do_test_body (void) |
| 173 |
{__asm__ ( |
| 174 |
" # rax, r10 and r11 are usable here.\n" |
| 175 |
"\n" |
| 176 |
" # Save registers.\n" |
| 177 |
LOAD_TEST_DATA_ADDR("%%rax") |
| 178 |
" lea %p0(%%rax), %%r10\n" |
| 179 |
" call " ASMNAME(regs_to_mem) "\n" |
| 180 |
"\n" |
| 181 |
" # Load registers with random data.\n" |
| 182 |
" lea %p1(%%rax), %%r10\n" |
| 183 |
" call " ASMNAME(mem_to_regs) "\n" |
| 184 |
"\n" |
| 185 |
" # Pop and save original return address.\n" |
| 186 |
" pop %%r10\n" |
| 187 |
" mov %%r10, %p4(%%rax)\n" |
| 188 |
"\n" |
| 189 |
" # Call the test function, after which rcx, rdx and r8-11\n" |
| 190 |
" # become usable.\n" |
| 191 |
" lea %p3(%%rax), %%rax\n" |
| 192 |
" call *(%%rax)\n" |
| 193 |
"\n" |
| 194 |
" # Store resulting register values.\n" |
| 195 |
LOAD_TEST_DATA_ADDR("%%rcx") |
| 196 |
" lea %p2(%%rcx), %%r10\n" |
| 197 |
" call " ASMNAME(regs_to_mem) "\n" |
| 198 |
"\n" |
| 199 |
" # Push the original return address.\n" |
| 200 |
" lea %p4(%%rcx), %%r10\n" |
| 201 |
" push (%%r10)\n" |
| 202 |
"\n" |
| 203 |
" # Restore registers.\n" |
| 204 |
" lea %p0(%%rcx), %%r10\n" |
| 205 |
" call " ASMNAME(mem_to_regs) "\n" |
| 206 |
"\n" |
| 207 |
" retq\n" |
| 208 |
:: |
| 209 |
"i"(TEST_DATA_OFFSET(regdata[REG_SET_SAVE])), |
| 210 |
"i"(TEST_DATA_OFFSET(regdata[REG_SET_INPUT])), |
| 211 |
"i"(TEST_DATA_OFFSET(regdata[REG_SET_OUTPUT])), |
| 212 |
"i"(TEST_DATA_OFFSET(fn)), |
| 213 |
"i"(TEST_DATA_OFFSET(retaddr)) : "memory"); |
| 214 |
} |
| 215 |
|
| 216 |
static void __attribute__((noinline)) |
| 217 |
init_test (void *fn, const char *name, enum alignment_option alignment, |
| 218 |
enum shrink_wrap_option shrink_wrap, long ret_expected) |
| 219 |
{ |
| 220 |
int i; |
| 221 |
union regdata *data = &test_data.regdata[REG_SET_INPUT]; |
| 222 |
|
| 223 |
assert (alignment < ALIGNMENT_COUNT); |
| 224 |
assert (shrink_wrap < SHRINK_WRAP_COUNT); |
| 225 |
|
| 226 |
memset (&test_data, 0, sizeof (test_data)); |
| 227 |
for (i = 55; i >= 0; --i) |
| 228 |
data->u32_arr[i] = (uint32_t)lrand48 (); |
| 229 |
test_data.fn = fn; |
| 230 |
test_data.name = name; |
| 231 |
test_data.alignment = alignment; |
| 232 |
test_data.shrink_wrap = shrink_wrap; |
| 233 |
test_data.ret_expected = ret_expected; |
| 234 |
|
| 235 |
switch (shrink_wrap) |
| 236 |
{ |
| 237 |
case SHRINK_WRAP_NONE: |
| 238 |
case SHRINK_WRAP_COUNT: |
| 239 |
break; |
| 240 |
case SHRINK_WRAP_FAST_PATH: |
| 241 |
shrink_wrap_global = FLAG_SHRINK_WRAP_FAST_PATH; |
| 242 |
break; |
| 243 |
case SHRINK_WRAP_SLOW_PATH: |
| 244 |
shrink_wrap_global = FLAG_SHRINK_WRAP_SLOW_PATH; |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
static const char *alignment_str[ALIGNMENT_COUNT] = |
| 250 |
{ |
| 251 |
"", "aligned", "misaligned" |
| 252 |
}; |
| 253 |
|
| 254 |
static const char *shrink_wrap_str[SHRINK_WRAP_COUNT] = |
| 255 |
{ |
| 256 |
"", "shrink-wrap fast path", "shrink-wrap slow path" |
| 257 |
}; |
| 258 |
|
| 259 |
static const char *test_descr () |
| 260 |
{ |
| 261 |
static char buffer[0x400]; |
| 262 |
|
| 263 |
if (test_data.alignment || test_data.shrink_wrap) |
| 264 |
snprintf (buffer, sizeof (buffer) - 1, "`%s' (%s%s%s)", |
| 265 |
test_data.name, |
| 266 |
alignment_str[test_data.alignment], |
| 267 |
(test_data.alignment && test_data.shrink_wrap ? ", " : ""), |
| 268 |
shrink_wrap_str[test_data.shrink_wrap]); |
| 269 |
else |
| 270 |
snprintf (buffer, sizeof (buffer) - 1, "`%s'", test_data.name); |
| 271 |
|
| 272 |
return buffer; |
| 273 |
} |
| 274 |
|
| 275 |
static const char *regnames[] = { |
| 276 |
"XMM6", |
| 277 |
"XMM7", |
| 278 |
"XMM8", |
| 279 |
"XMM9", |
| 280 |
"XMM10", |
| 281 |
"XMM11", |
| 282 |
"XMM12", |
| 283 |
"XMM13", |
| 284 |
"XMM14", |
| 285 |
"XMM15", |
| 286 |
"RSI", |
| 287 |
"RDI", |
| 288 |
"RBX", |
| 289 |
"RBP", |
| 290 |
"R12", |
| 291 |
"R13", |
| 292 |
"R14", |
| 293 |
"R15", |
| 294 |
}; |
| 295 |
|
| 296 |
static void print_header (int *header_printed) |
| 297 |
{ |
| 298 |
if (!*header_printed) |
| 299 |
fprintf (stderr, " %-35s %-35s\n", "Expected", "Got"); |
| 300 |
*header_printed = 1; |
| 301 |
} |
| 302 |
|
| 303 |
static int compare_reg128 (const __uint128_t *a, const __uint128_t *b, |
| 304 |
const char *name, int *header_printed) |
| 305 |
{ |
| 306 |
if (!memcmp (a, b, sizeof (*a))) |
| 307 |
return 0; |
| 308 |
else |
| 309 |
{ |
| 310 |
long ha = *((long*)a); |
| 311 |
long la = *((long*)a + 16); |
| 312 |
long hb = *((long*)b); |
| 313 |
long lb = *((long*)a + 16); |
| 314 |
print_header (header_printed); |
| 315 |
fprintf (stderr, "%-5s: 0x%016lx %016lx != 0x%016lx %016lx\n", |
| 316 |
name, ha, la, hb, lb); |
| 317 |
return 1; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
static int compare_reg64 (long a, long b, const char *name, |
| 322 |
int *header_printed) |
| 323 |
{ |
| 324 |
if (a == b) |
| 325 |
return 0; |
| 326 |
else |
| 327 |
{ |
| 328 |
print_header (header_printed); |
| 329 |
fprintf (stderr, "%s: 0x%016lx != 0x%016lx\n", name, a, b); |
| 330 |
return 1; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
static void __attribute__((noinline)) check_results (long ret) |
| 336 |
{ |
| 337 |
unsigned i; |
| 338 |
unsigned bad = 0; |
| 339 |
int header_printed = 0; |
| 340 |
|
| 341 |
union regdata *a = &test_data.regdata[REG_SET_INPUT]; |
| 342 |
union regdata *b = &test_data.regdata[REG_SET_OUTPUT]; |
| 343 |
|
| 344 |
a = __builtin_assume_aligned(a, 16); |
| 345 |
b = __builtin_assume_aligned(b, 16); |
| 346 |
|
| 347 |
if (arbitrarily_fail) { |
| 348 |
uint64_t u64 = lrand48 (); |
| 349 |
if (u64 % 100 == 0) |
| 350 |
b->u32_arr[u64 % 56] = 0xfdfdfdfd; |
| 351 |
} |
| 352 |
|
| 353 |
for (i = 0; i < 10; ++i) |
| 354 |
bad |= compare_reg128 (&a->sseregs[i], &b->sseregs[i], regnames[i], |
| 355 |
&header_printed); |
| 356 |
|
| 357 |
for (i = 0; i < 8; ++i) |
| 358 |
bad |= compare_reg64 (a->intregs[i], b->intregs[i], regnames[i + 10], |
| 359 |
&header_printed); |
| 360 |
|
| 361 |
if (ret != test_data.ret_expected) |
| 362 |
{ |
| 363 |
fprintf (stderr, "Wrong return value: got 0x%016lx, expected 0x%016lx\n", |
| 364 |
ret, test_data.ret_expected); |
| 365 |
bad = 1; |
| 366 |
} |
| 367 |
|
| 368 |
if (bad) |
| 369 |
{ |
| 370 |
fprintf (stderr, "Failed on test function %s\n", test_descr ()); |
| 371 |
raise (SIGTRAP); |
| 372 |
exit (-1); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
static __attribute__((ms_abi, noinline)) long do_sibcall (long arg) { |
| 377 |
return arg + FLAG_SIBCALL; |
| 378 |
} |
| 379 |
|
| 380 |
void usage () |
| 381 |
{ |
| 382 |
fprintf (stderr, "Usage: %s [-s <seed>] [-f]\n", argv0); |
| 383 |
exit (-1); |
| 384 |
} |
| 385 |
|
| 386 |
static long long_optarg (const char *optarg, const char *optstr) |
| 387 |
{ |
| 388 |
char *end; |
| 389 |
long ret; |
| 390 |
|
| 391 |
errno = 0; |
| 392 |
ret = strtol(optarg, &end, 0); |
| 393 |
|
| 394 |
while (isspace (*end)) |
| 395 |
++end; |
| 396 |
|
| 397 |
if (errno || *end) |
| 398 |
{ |
| 399 |
fprintf (stderr, "ERROR: Bad value for %s: `%s`\n", optstr, optarg); |
| 400 |
if (errno) |
| 401 |
fprintf (stderr, "%s\n", strerror (errno)); |
| 402 |
exit (-1); |
| 403 |
} |
| 404 |
|
| 405 |
return ret; |
| 406 |
} |
| 407 |
|
| 408 |
int main (int argc, char *argv[]) |
| 409 |
{ |
| 410 |
long seed = 0; |
| 411 |
int c; |
| 412 |
argv0 = argv[0]; |
| 413 |
|
| 414 |
assert (!((long)&test_data.regdata[REG_SET_SAVE] & 15)); |
| 415 |
assert (!((long)&test_data.regdata[REG_SET_INPUT] & 15)); |
| 416 |
assert (!((long)&test_data.regdata[REG_SET_OUTPUT] & 15)); |
| 417 |
|
| 418 |
while ((c = getopt (argc, argv, "s:f")) != -1) |
| 419 |
{ |
| 420 |
switch (c) |
| 421 |
{ |
| 422 |
case 's': |
| 423 |
seed = long_optarg (optarg, "-s"); |
| 424 |
break; |
| 425 |
|
| 426 |
case 'f': |
| 427 |
arbitrarily_fail = 1; |
| 428 |
fprintf (stderr, "NOTE: Aribrary failure enabled (-f).\n"); |
| 429 |
break; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
srand48 (seed); |
| 434 |
do_tests (); |
| 435 |
|
| 436 |
/* Just in case we don't have enough tests to randomly trigger the |
| 437 |
failure. */ |
| 438 |
if (arbitrarily_fail) |
| 439 |
return -1; |
| 440 |
|
| 441 |
return 0; |
| 442 |
} |