forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlibunwind_01.pass.cpp
168 lines (137 loc) · 3.99 KB
/
libunwind_01.pass.cpp
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
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// TODO: Investigate this failure on x86_64 macOS back deployment
// XFAIL: stdlib=apple-libc++ && target=x86_64-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}}
// TODO: Figure out why this fails with Memory Sanitizer.
// XFAIL: msan
#include <libunwind.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void backtrace(int lower_bound) {
unw_context_t context;
unw_getcontext(&context);
unw_cursor_t cursor;
unw_init_local(&cursor, &context);
char buffer[1024];
unw_word_t offset = 0;
int n = 0;
do {
n++;
if (unw_get_proc_name(&cursor, buffer, sizeof(buffer), &offset) == 0) {
fprintf(stderr, "Frame %d: %s+%p\n", n, buffer, (void*)offset);
} else {
fprintf(stderr, "Frame %d: Could not get name for cursor\n", n);
}
if (n > 100) {
abort();
}
} while (unw_step(&cursor) > 0);
if (n < lower_bound) {
abort();
}
}
__attribute__((noinline)) void test1(int i) {
fprintf(stderr, "starting %s\n", __func__);
backtrace(i);
fprintf(stderr, "finished %s\n", __func__); // ensure return address is saved
}
__attribute__((noinline)) void test2(int i, int j) {
fprintf(stderr, "starting %s\n", __func__);
backtrace(i);
test1(j);
fprintf(stderr, "finished %s\n", __func__); // ensure return address is saved
}
__attribute__((noinline)) void test3(int i, int j, int k) {
fprintf(stderr, "starting %s\n", __func__);
backtrace(i);
test2(j, k);
fprintf(stderr, "finished %s\n", __func__); // ensure return address is saved
}
void test_no_info() {
unw_context_t context;
unw_getcontext(&context);
unw_cursor_t cursor;
unw_init_local(&cursor, &context);
unw_proc_info_t info;
int ret = unw_get_proc_info(&cursor, &info);
if (ret != UNW_ESUCCESS)
abort();
// Set the IP to an address clearly outside any function.
unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)0);
ret = unw_get_proc_info(&cursor, &info);
if (ret != UNW_ENOINFO)
abort();
}
void test_reg_names() {
unw_context_t context;
unw_getcontext(&context);
unw_cursor_t cursor;
unw_init_local(&cursor, &context);
int max_reg_num = -100;
#if defined(__i386__)
max_reg_num = 7;
#elif defined(__x86_64__)
max_reg_num = 32;
#endif
const char prefix[] = "unknown";
for (int i = -2; i < max_reg_num; ++i) {
if (strncmp(prefix, unw_regname(&cursor, i), sizeof(prefix) - 1) == 0)
abort();
}
if (strncmp(prefix, unw_regname(&cursor, max_reg_num + 1),
sizeof(prefix) - 1) != 0)
abort();
}
#if defined(__x86_64__)
void test_reg_get_set() {
unw_context_t context;
unw_getcontext(&context);
unw_cursor_t cursor;
unw_init_local(&cursor, &context);
for (int i = 0; i < 17; ++i) {
const unw_word_t set_value = 7;
if (unw_set_reg(&cursor, i, set_value) != UNW_ESUCCESS)
abort();
unw_word_t get_value = 0;
if (unw_get_reg(&cursor, i, &get_value) != UNW_ESUCCESS)
abort();
if (set_value != get_value)
abort();
}
}
void test_fpreg_get_set() {
unw_context_t context;
unw_getcontext(&context);
unw_cursor_t cursor;
unw_init_local(&cursor, &context);
// get/set is not implemented for x86_64 fpregs.
for (int i = 17; i < 33; ++i) {
const unw_fpreg_t set_value = 7;
if (unw_set_fpreg(&cursor, i, set_value) != UNW_EBADREG)
abort();
unw_fpreg_t get_value = 0;
if (unw_get_fpreg(&cursor, i, &get_value) != UNW_EBADREG)
abort();
}
}
#else
void test_reg_get_set() {}
void test_fpreg_get_set() {}
#endif
int main(int, char**) {
test1(3);
test2(3, 4);
test3(3, 4, 5);
test_no_info();
test_reg_names();
test_reg_get_set();
test_fpreg_get_set();
return 0;
}