Files

177 lines
4.0 KiB
C

/* M5 robustness gate payload: dynamic alloca, byval, narrow returns, and the
* llvm.memcpy/llvm.memset intrinsics. The entry assembly calls each function
* and stores the result at a marker word; robust.sh checks the values in MAME.
*
* The gate is built at -O0 so the narrow-return calls are not constant-folded
* and the VLA keeps its runtime size. The lit tests pin the same features at
* -O2.
*/
struct pair {
int x;
int y;
};
__attribute__((noinline))
int alloca_sum(int n)
{
volatile int a[n];
int i;
int sum = 0;
for (i = 0; i < n; ++i)
a[i] = i * 3;
for (i = 0; i < n; ++i)
sum += a[i];
return sum;
}
/* A g(buf,n)-style callee used to check that the VLA reservation is real: the
* call's argument pushes start at A14 and would overwrite the VLA if the
* dynamic alloca advanced A14 by only size/8. */
__attribute__((noinline))
int sum_vla(const volatile int *p, int n)
{
int i;
int sum = 0;
for (i = 0; i < n; ++i)
sum += p[i];
return sum;
}
__attribute__((noinline))
int alloca_use(int n)
{
volatile int a[n];
volatile int b[n];
int i;
int s;
int t = 0;
for (i = 0; i < n; ++i)
a[i] = i * 3;
for (i = 0; i < n; ++i)
b[i] = i * 5;
/* Both VLAs must survive this argumented call, and a second alloca. */
s = sum_vla(a, n);
for (i = 0; i < n; ++i)
t += a[i] + b[i];
return s + t;
}
__attribute__((noinline))
int byval_sum(struct pair p)
{
return p.x * 3 + p.y;
}
__attribute__((noinline))
int robust_byval(void)
{
struct pair p;
p.x = 11;
p.y = 31;
return byval_sum(p);
}
/* Six bytes distinguishes the TI 16-bit byval slot rounding from a 32-bit
* extension: the object occupies 6 bytes, not 8. */
struct pair6 {
int x;
short y;
};
__attribute__((noinline))
int byval6_sum(struct pair6 p)
{
return p.x * 3 + p.y;
}
__attribute__((noinline))
int robust_byval6(void)
{
struct pair6 p;
p.x = 11;
p.y = 31;
return byval6_sum(p);
}
/* 0xFF9C makes the low byte 0x9C (156) and the low 16 bits -100. */
volatile int narrow_seed = 0xFF9C;
__attribute__((noinline))
unsigned char narrow8(void)
{
return (unsigned char)narrow_seed;
}
__attribute__((noinline))
signed short narrow16(void)
{
return (signed short)narrow_seed;
}
__attribute__((noinline))
int robust_narrow(void)
{
return (int)narrow8() + (int)narrow16() + 1000;
}
__attribute__((noinline))
void mem_ops(unsigned char *d, const unsigned char *s)
{
__builtin_memcpy(d, s, 12);
__builtin_memset(d + 12, 0x5A, 4);
}
/* A short public name: GSPA 2.00 truncates identifiers at 31 characters, and
* a private constant would get the sanitized module-token suffix. */
unsigned char robust_src[16] = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 0, 0, 0, 0};
__attribute__((noinline))
int robust_mem(void)
{
unsigned char dst[16];
int i;
int sum = 0;
mem_ops(dst, robust_src);
for (i = 0; i < 16; ++i)
sum += dst[i];
return sum;
}
unsigned char robust_src64[64] = {
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
};
/* Constant memmove (10 and 64 bytes) must compile: PreISel does not expand
* these sizes, so the target pass expands them into overlap-safe loops. The
* overlapping 10-byte move checks the backward direction. */
__attribute__((noinline))
int robust_memmove(void)
{
unsigned char buf[64];
unsigned char ov[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int i;
int sum = 0;
__builtin_memmove(buf, robust_src64, 64);
for (i = 0; i < 64; ++i)
sum += buf[i]; /* 1..64 = 2080 */
__builtin_memmove(ov + 1, ov, 10);
for (i = 0; i < 12; ++i)
sum += ov[i]; /* 1,1..10,12 = 68 */
return sum; /* 2148 */
}