-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.c
46 lines (39 loc) · 847 Bytes
/
basic.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
#include <stdio.h>
#define MAX_CALL_DEPTH 10
#ifdef DEBUG
#if defined(__aarch64__)
#define asm_read_rsp(rsp) \
asm volatile ("mov %0, x28" \
: "=r" (rsp))
#elif defined (__x86_64__)
#define asm_read_rsp(rsp) \
asm volatile ("movq %%r15, %0" \
: "=r" (rsp))
#endif
#endif
static int callee (int depth) {
#ifdef DEBUG
unsigned long rsp;
asm_read_rsp(rsp);
printf("(Callee#%d) RSP=0x%lx\n", depth, rsp);
#endif
depth++;
if (depth != MAX_CALL_DEPTH)
return callee(depth);
return depth;
}
int main (void) {
int depth;
#ifdef DEBUG
unsigned long rsp;
asm_read_rsp(rsp);
printf("(Main) RSP=0x%lx\n", rsp);
#endif
depth = callee(0);
if (depth != MAX_CALL_DEPTH) {
fprintf(stderr, "main() failed: Not all callees were executed.\n");
return 1;
}
printf("OK\n");
return 0;
}