mirrored from git://gcc.gnu.org/git/gcc.git
/
ms-sysv.c
443 lines (377 loc) · 10.7 KB
/
ms-sysv.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* Test program for 64-Bit Microsoft to System V function calls.
Copyright (C) 2016-2017 Free Software Foundation, Inc.
Contributed by Daniel Santos <daniel.santos@pobox.com>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC 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 General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
/* This is a single-threaded test program for Microsoft 64-bit ABI functions.
It is aimed at verifying correctness of pro/epilogues of ms_abi functions
that call sysv_abi functions to assure clobbered registers are properly
saved and restored and attempt to detect any flaws in the behavior of these
functions. The following variants are tested:
* Either uses hard frame pointer, re-aligns the stack or neither,
* Uses alloca (and thus DRAP) or not,
* Uses sibling call optimization or not,
* Uses variable argument list or not, and
* Has shrink-wrapped code or not.
In addition, an ms_abi function is generated for each of these combinations
clobbering each unique combination additional registers (excluding BP when
a frame pointer is used). Shrink-wrap variants are called in a way that
both the fast and slow path are used. Re-aligned variants are called with
an aligned and mis-aligned stack.
Each ms_abi function is called via an assembly stub that first saves all
volatile registers and fills them with random values. The ms_abi function
is then called. After the function returns, the value of all volatile
registers is verified against the random data and then restored. */
/* { dg-do run } */
/* { dg-additional-sources "do-test.S" } */
/* { dg-additional-options "-Wall" } */
/* { dg-require-effective-target alloca } */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <stdint.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#if !defined(__x86_64__) || !defined(__SSE2__)
# error Test only valid on x86_64 with -msse2
#endif
enum reg_data_sets
{
REG_SET_SAVE,
REG_SET_INPUT,
REG_SET_OUTPUT,
REG_SET_COUNT
};
enum flags
{
FLAG_ALLOCA = 0x01000000,
FLAG_SIBCALL = 0x02000000,
FLAG_SHRINK_WRAP_FAST_PATH = 0x08000000,
FLAG_SHRINK_WRAP_SLOW_PATH = 0x0c000000,
};
enum alignment_option
{
ALIGNMENT_NOT_TESTED,
ALIGNMENT_ALIGNED,
ALIGNMENT_MISALIGNED,
ALIGNMENT_COUNT,
};
enum shrink_wrap_option
{
SHRINK_WRAP_NONE,
SHRINK_WRAP_FAST_PATH,
SHRINK_WRAP_SLOW_PATH,
SHRINK_WRAP_COUNT
};
union regdata {
struct {
__uint128_t sseregs[10];
union {
uint64_t intregs[8];
struct {
uint64_t rsi;
uint64_t rdi;
uint64_t rbx;
uint64_t rbp;
uint64_t r12;
uint64_t r13;
uint64_t r14;
uint64_t r15;
};
};
};
uint32_t u32_arr[56];
} __attribute__((aligned (16)));
struct test_data
{
union regdata regdata[REG_SET_COUNT];
void *fn;
void *retaddr;
const char *name;
enum alignment_option alignment;
enum shrink_wrap_option shrink_wrap;
long ret_expected;
} test_data;
static int shrink_wrap_global;
static void __attribute((sysv_abi)) do_tests ();
static void init_test (void *fn, const char *name,
enum alignment_option alignment,
enum shrink_wrap_option shrink_wrap, long ret_expected);
static void check_results (long ret);
static __attribute__((ms_abi)) long do_sibcall (long arg);
static __attribute__((ms_abi)) long
(*const volatile do_sibcall_noinfo) (long) = do_sibcall;
/* Defines do_tests (). */
#include "ms-sysv-generated.h"
static int arbitrarily_fail;
static const char *argv0;
#define PASTE_STR2(a) #a
#define PASTE_STR1(a, b) PASTE_STR2(a ## b)
#define PASTE_STR(a, b) PASTE_STR1(a, b)
#ifdef __USER_LABEL_PREFIX__
# define ASMNAME(name) PASTE_STR(__USER_LABEL_PREFIX__, name)
#else
# define ASMNAME(name) #name
#endif
#ifdef __MACH__
# define LOAD_TEST_DATA_ADDR(dest) \
" mov " ASMNAME(test_data) "@GOTPCREL(%%rip), " dest "\n"
#else
# define LOAD_TEST_DATA_ADDR(dest) \
" lea " ASMNAME(test_data) "(%%rip), " dest "\n"
#endif
#define TEST_DATA_OFFSET(f) ((int)__builtin_offsetof(struct test_data, f))
void __attribute__((naked))
do_test_body (void)
{__asm__ (
" # rax, r10 and r11 are usable here.\n"
"\n"
" # Save registers.\n"
LOAD_TEST_DATA_ADDR("%%rax")
" lea %p0(%%rax), %%r10\n"
" call " ASMNAME(regs_to_mem) "\n"
"\n"
" # Load registers with random data.\n"
" lea %p1(%%rax), %%r10\n"
" call " ASMNAME(mem_to_regs) "\n"
"\n"
" # Pop and save original return address.\n"
" pop %%r10\n"
" mov %%r10, %p4(%%rax)\n"
"\n"
" # Call the test function, after which rcx, rdx and r8-11\n"
" # become usable.\n"
" lea %p3(%%rax), %%rax\n"
" call *(%%rax)\n"
"\n"
" # Store resulting register values.\n"
LOAD_TEST_DATA_ADDR("%%rcx")
" lea %p2(%%rcx), %%r10\n"
" call " ASMNAME(regs_to_mem) "\n"
"\n"
" # Push the original return address.\n"
" lea %p4(%%rcx), %%r10\n"
" push (%%r10)\n"
"\n"
" # Restore registers.\n"
" lea %p0(%%rcx), %%r10\n"
" call " ASMNAME(mem_to_regs) "\n"
"\n"
" retq\n"
::
"i"(TEST_DATA_OFFSET(regdata[REG_SET_SAVE])),
"i"(TEST_DATA_OFFSET(regdata[REG_SET_INPUT])),
"i"(TEST_DATA_OFFSET(regdata[REG_SET_OUTPUT])),
"i"(TEST_DATA_OFFSET(fn)),
"i"(TEST_DATA_OFFSET(retaddr)) : "memory");
}
static void __attribute__((noinline))
init_test (void *fn, const char *name, enum alignment_option alignment,
enum shrink_wrap_option shrink_wrap, long ret_expected)
{
int i;
union regdata *data = &test_data.regdata[REG_SET_INPUT];
assert (alignment < ALIGNMENT_COUNT);
assert (shrink_wrap < SHRINK_WRAP_COUNT);
memset (&test_data, 0, sizeof (test_data));
for (i = 55; i >= 0; --i)
data->u32_arr[i] = (uint32_t)lrand48 ();
test_data.fn = fn;
test_data.name = name;
test_data.alignment = alignment;
test_data.shrink_wrap = shrink_wrap;
test_data.ret_expected = ret_expected;
switch (shrink_wrap)
{
case SHRINK_WRAP_NONE:
case SHRINK_WRAP_COUNT:
break;
case SHRINK_WRAP_FAST_PATH:
shrink_wrap_global = FLAG_SHRINK_WRAP_FAST_PATH;
break;
case SHRINK_WRAP_SLOW_PATH:
shrink_wrap_global = FLAG_SHRINK_WRAP_SLOW_PATH;
break;
}
}
static const char *alignment_str[ALIGNMENT_COUNT] =
{
"", "aligned", "misaligned"
};
static const char *shrink_wrap_str[SHRINK_WRAP_COUNT] =
{
"", "shrink-wrap fast path", "shrink-wrap slow path"
};
static const char *test_descr ()
{
static char buffer[0x400];
if (test_data.alignment || test_data.shrink_wrap)
snprintf (buffer, sizeof (buffer) - 1, "`%s' (%s%s%s)",
test_data.name,
alignment_str[test_data.alignment],
(test_data.alignment && test_data.shrink_wrap ? ", " : ""),
shrink_wrap_str[test_data.shrink_wrap]);
else
snprintf (buffer, sizeof (buffer) - 1, "`%s'", test_data.name);
return buffer;
}
static const char *regnames[] = {
"XMM6",
"XMM7",
"XMM8",
"XMM9",
"XMM10",
"XMM11",
"XMM12",
"XMM13",
"XMM14",
"XMM15",
"RSI",
"RDI",
"RBX",
"RBP",
"R12",
"R13",
"R14",
"R15",
};
static void print_header (int *header_printed)
{
if (!*header_printed)
fprintf (stderr, " %-35s %-35s\n", "Expected", "Got");
*header_printed = 1;
}
static int compare_reg128 (const __uint128_t *a, const __uint128_t *b,
const char *name, int *header_printed)
{
if (!memcmp (a, b, sizeof (*a)))
return 0;
else
{
long ha = *((long*)a);
long la = *((long*)a + 16);
long hb = *((long*)b);
long lb = *((long*)a + 16);
print_header (header_printed);
fprintf (stderr, "%-5s: 0x%016lx %016lx != 0x%016lx %016lx\n",
name, ha, la, hb, lb);
return 1;
}
}
static int compare_reg64 (long a, long b, const char *name,
int *header_printed)
{
if (a == b)
return 0;
else
{
print_header (header_printed);
fprintf (stderr, "%s: 0x%016lx != 0x%016lx\n", name, a, b);
return 1;
}
}
static void __attribute__((noinline)) check_results (long ret)
{
unsigned i;
unsigned bad = 0;
int header_printed = 0;
union regdata *a = &test_data.regdata[REG_SET_INPUT];
union regdata *b = &test_data.regdata[REG_SET_OUTPUT];
a = __builtin_assume_aligned(a, 16);
b = __builtin_assume_aligned(b, 16);
if (arbitrarily_fail) {
uint64_t u64 = lrand48 ();
if (u64 % 100 == 0)
b->u32_arr[u64 % 56] = 0xfdfdfdfd;
}
for (i = 0; i < 10; ++i)
bad |= compare_reg128 (&a->sseregs[i], &b->sseregs[i], regnames[i],
&header_printed);
for (i = 0; i < 8; ++i)
bad |= compare_reg64 (a->intregs[i], b->intregs[i], regnames[i + 10],
&header_printed);
if (ret != test_data.ret_expected)
{
fprintf (stderr, "Wrong return value: got 0x%016lx, expected 0x%016lx\n",
ret, test_data.ret_expected);
bad = 1;
}
if (bad)
{
fprintf (stderr, "Failed on test function %s\n", test_descr ());
raise (SIGTRAP);
exit (-1);
}
}
static __attribute__((ms_abi, noinline)) long do_sibcall (long arg) {
return arg + FLAG_SIBCALL;
}
void usage ()
{
fprintf (stderr, "Usage: %s [-s <seed>] [-f]\n", argv0);
exit (-1);
}
static long long_optarg (const char *optarg, const char *optstr)
{
char *end;
long ret;
errno = 0;
ret = strtol(optarg, &end, 0);
while (isspace (*end))
++end;
if (errno || *end)
{
fprintf (stderr, "ERROR: Bad value for %s: `%s`\n", optstr, optarg);
if (errno)
fprintf (stderr, "%s\n", strerror (errno));
exit (-1);
}
return ret;
}
int main (int argc, char *argv[])
{
long seed = 0;
int c;
argv0 = argv[0];
assert (!((long)&test_data.regdata[REG_SET_SAVE] & 15));
assert (!((long)&test_data.regdata[REG_SET_INPUT] & 15));
assert (!((long)&test_data.regdata[REG_SET_OUTPUT] & 15));
while ((c = getopt (argc, argv, "s:f")) != -1)
{
switch (c)
{
case 's':
seed = long_optarg (optarg, "-s");
break;
case 'f':
arbitrarily_fail = 1;
fprintf (stderr, "NOTE: Aribrary failure enabled (-f).\n");
break;
}
}
srand48 (seed);
do_tests ();
/* Just in case we don't have enough tests to randomly trigger the
failure. */
if (arbitrarily_fail)
return -1;
return 0;
}