Skip to content

Commit e6003b4

Browse files
committed
Initial commit
0 parents  commit e6003b4

11 files changed

+542
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Return Stack Test Suite
2+
3+
This is a small collection of C/C++ files to test return stack support.
4+
5+
Before executing the tests, make sure to export the following variables:
6+
7+
```
8+
export SYSROOT=<path to where the custom LLVM compiler, GNU C library, and GCC are installed>
9+
export CC=$SYSROOT/<path to clang compiler>
10+
export CXX=$SYSROOT/<path to clang++ compiler>
11+
export LDSO_PATH=$SYSROOT/<path to ld.so>
12+
export LIBC_PATH=$SYSROOT/<path to where libc.so.6 resides>
13+
export LIBGCC_PATH=$SYSROOT/<path to where libgcc_s.so.1 resides>
14+
```
15+
16+
Then, simply run:
17+
18+
```
19+
cd test && ./run.sh
20+
```
21+
22+
**Note** that only `CC` and `CXX` must be set in case the tests are executed
23+
within a return stack-aware chroot or Yocto Linux environment (see
24+
[here](https://github.com/llvm-return-stack/return-stack) for more details).

args.c

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdio.h>
2+
3+
#ifdef DEBUG
4+
#if defined(__aarch64__)
5+
#define asm_read_rsp(rsp) \
6+
asm volatile ("mov %0, x28" \
7+
: "=r" (rsp))
8+
#elif defined (__x86_64__)
9+
#define asm_read_rsp(rsp) \
10+
asm volatile ("movq %%r15, %0" \
11+
: "=r" (rsp))
12+
#endif
13+
#endif
14+
15+
static long callee (long a, long b, long c, long d, long e, long f, long g,
16+
long h, long i, long j, long k, long l, long m, long n,
17+
long o, long p) {
18+
long q, r, s;
19+
20+
#ifdef DEBUG
21+
unsigned long rsp;
22+
asm_read_rsp(rsp);
23+
printf("(Callee) RSP=0x%lx\n", rsp);
24+
#endif
25+
26+
q = a + b + c + d + e + f;
27+
r = g + h + i + j + k + l;
28+
s = m + n + o + p + q + r;
29+
30+
return s;
31+
}
32+
33+
int main (void) {
34+
long a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, s;
35+
36+
#ifdef DEBUG
37+
unsigned long rsp;
38+
asm_read_rsp(rsp);
39+
printf("(Main) RSP=0x%lx\n", rsp);
40+
#endif
41+
42+
a = 0x0000000000000000L;
43+
b = 0x1000000000000000L;
44+
c = 0x1100000000000000L;
45+
d = 0x1110000000000000L;
46+
e = 0x1111000000000000L;
47+
f = 0x1111100000000000L;
48+
g = 0x1111110000000000L;
49+
h = 0x1111111000000000L;
50+
i = 0x1111111100000000L;
51+
j = 0x1111111110000000L;
52+
k = 0x1111111111000000L;
53+
l = 0x1111111111100000L;
54+
m = 0x1111111111110000L;
55+
n = 0x1111111111111000L;
56+
o = 0x1111111111111100L;
57+
p = 0x1111111111111110L;
58+
59+
s = callee(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p);
60+
if (s != 0xfedcba9876543210L) {
61+
fprintf(stderr, "main() failed: The sum of arguments was not computed correctly.\n");
62+
return 1;
63+
}
64+
65+
printf("OK\n");
66+
return 0;
67+
}
68+

basic.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <stdio.h>
2+
3+
#define MAX_CALL_DEPTH 10
4+
5+
#ifdef DEBUG
6+
#if defined(__aarch64__)
7+
#define asm_read_rsp(rsp) \
8+
asm volatile ("mov %0, x28" \
9+
: "=r" (rsp))
10+
#elif defined (__x86_64__)
11+
#define asm_read_rsp(rsp) \
12+
asm volatile ("movq %%r15, %0" \
13+
: "=r" (rsp))
14+
#endif
15+
#endif
16+
17+
static int callee (int depth) {
18+
#ifdef DEBUG
19+
unsigned long rsp;
20+
asm_read_rsp(rsp);
21+
printf("(Callee#%d) RSP=0x%lx\n", depth, rsp);
22+
#endif
23+
depth++;
24+
if (depth != MAX_CALL_DEPTH)
25+
return callee(depth);
26+
return depth;
27+
}
28+
29+
int main (void) {
30+
int depth;
31+
#ifdef DEBUG
32+
unsigned long rsp;
33+
asm_read_rsp(rsp);
34+
printf("(Main) RSP=0x%lx\n", rsp);
35+
#endif
36+
37+
depth = callee(0);
38+
if (depth != MAX_CALL_DEPTH) {
39+
fprintf(stderr, "main() failed: Not all callees were executed.\n");
40+
return 1;
41+
}
42+
43+
printf("OK\n");
44+
return 0;
45+
}
46+

exception.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
3+
#ifdef DEBUG
4+
#if defined(__aarch64__)
5+
#define asm_read_rsp(rsp) \
6+
asm volatile ("mov %0, x28" \
7+
: "=r" (rsp))
8+
#elif defined (__x86_64__)
9+
#define asm_read_rsp(rsp) \
10+
asm volatile ("movq %%r15, %0" \
11+
: "=r" (rsp))
12+
#endif
13+
#endif
14+
15+
struct Exception {};
16+
17+
void raise (void) {
18+
#ifdef DEBUG
19+
unsigned long rsp;
20+
asm_read_rsp(rsp);
21+
printf("(Raise) RSP=0x%lx\n", rsp);
22+
#endif
23+
throw Exception();
24+
}
25+
26+
int main (void) {
27+
bool handled = false;
28+
#ifdef DEBUG
29+
unsigned long rsp;
30+
asm_read_rsp(rsp);
31+
printf("(Main) RSP=0x%lx\n", rsp);
32+
#endif
33+
34+
try {
35+
raise();
36+
} catch(Exception &) {
37+
handled = true;
38+
}
39+
40+
if (!handled) {
41+
fprintf(stderr, "main() failed: catch-clause was not executed correctly.\n");
42+
return 1;
43+
}
44+
45+
printf("OK\n");
46+
return 0;
47+
}
48+

libsum.c

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int sum (int a, int b) {
2+
return a + b;
3+
}
4+

link.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
3+
extern int sum (int a, int b);
4+
5+
int main (void) {
6+
int s;
7+
8+
s = sum(1, 2);
9+
if (s != 3) {
10+
fprintf(stderr, "main() failed: Calling library function did not succeed correctly.\n");
11+
return 1;
12+
}
13+
14+
printf("OK\n");
15+
return 0;
16+
}
17+

run.sh

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
3+
if [ -z "$CC" ]; then
4+
CC=$(which clang)
5+
if [ -z "$CC" ]; then
6+
echo "Clang compiler not found."
7+
exit
8+
fi
9+
fi
10+
11+
if [ -z "$CXX" ]; then
12+
CXX=$(which clang++)
13+
if [ -z "$CXX" ]; then
14+
echo "Clang++ compiler not found."
15+
exit
16+
fi
17+
fi
18+
19+
if [ -n "$SYSROOT" ]; then
20+
SYSROOT_FLAG="--sysroot=$SYSROOT"
21+
fi
22+
23+
if [ -n "$LDSO_PATH" ]; then
24+
LINKER_FLAG="-Wl,--dynamic-linker=$LDSO_PATH"
25+
fi
26+
27+
LIBRARY_PATH="."
28+
29+
if [ -n "$LIBC_PATH" ]; then
30+
LIBRARY_PATH="$LIBRARY_PATH:$LIBC_PATH"
31+
fi
32+
33+
if [ -n "$LIBGCC_PATH" ]; then
34+
LIBRARY_PATH="$LIBRARY_PATH:$LIBGCC_PATH"
35+
fi
36+
37+
export LD_LIBRARY_PATH="$LIBRARY_PATH"
38+
39+
cmd() {
40+
"$@"
41+
} &> /dev/null
42+
43+
build() {
44+
EXT="${1##*.}"
45+
if [ "$EXT" = "c" ]; then
46+
C="$CC"
47+
elif [ "$EXT" = "cpp" ]; then
48+
C="$CXX"
49+
fi
50+
cmd "$C" "$@" -fsanitize=return-stack "$SYSROOT_FLAG"
51+
}
52+
53+
check() {
54+
echo -n " Testing $1..."
55+
build "$@" "$LINKER_FLAG" -fuse-ld=gold -o bin
56+
if [ "$?" -eq "0" ]; then
57+
cmd ./bin
58+
if [ "$?" -eq "0" ]; then
59+
echo "OK"
60+
else
61+
echo "FAILED"
62+
fi
63+
else
64+
echo "BUILDERROR"
65+
fi
66+
rm -f bin
67+
}
68+
69+
echo "Basic functionality tests"
70+
check args.c -O0
71+
check basic.c -O0 # Requires -O0
72+
check exception.cpp -O1
73+
check setjmp.c -O1
74+
check tailcall.c -O1 # Requires -O1
75+
check thread.c -O1 -lpthread
76+
check varargs.c -O0
77+
echo ""
78+
echo "Static library test"
79+
build libsum.c -c -O1 -o libsum.a
80+
check link.c -static -O1 -L. -lsum
81+
rm -f libsum.a
82+
echo ""
83+
echo "Dynamic library test"
84+
build libsum.c -O1 -fPIC -shared -fuse-ld=gold -o libsum.so
85+
check link.c -O1 -L. -lsum
86+
rm -f libsum.so
87+

0 commit comments

Comments
 (0)