1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <gtest/gtest.h>
30
31 #include <dlfcn.h>
32 #include <execinfo.h>
33 #include <string.h>
34
35 #include <fstream>
36 #include <regex>
37 #include <string>
38 #include <vector>
39
40 #include <android-base/file.h>
41 #include <android-base/stringprintf.h>
42 #include <android-base/strings.h>
43 #include <android-base/test_utils.h>
44
TEST(execinfo,backtrace_errors)45 TEST(execinfo, backtrace_errors) {
46 void* frames[20];
47 ASSERT_EQ(0, backtrace(frames, 0));
48 ASSERT_EQ(0, backtrace(frames, -1));
49 }
50
51 static constexpr int kMaxFrames = 50;
52
53 // Disable optimizations so that these functions show up properly in
54 // the backtrace.
55 #pragma clang optimize off
CallTwo(std::vector<void * > & frames)56 extern "C" __attribute__((__noinline__)) void CallTwo(std::vector<void*>& frames) {
57 int num_frames = backtrace(frames.data(), static_cast<int>(frames.size()));
58 ASSERT_LT(0, num_frames);
59 frames.resize(static_cast<size_t>(num_frames));
60 }
61
CallOne(std::vector<void * > & frames)62 extern "C" __attribute__((__noinline__)) void CallOne(std::vector<void*>& frames) {
63 CallTwo(frames);
64 }
65 #pragma clang optimize on
66
DumpFrames(std::vector<void * > & frames)67 static std::string DumpFrames(std::vector<void*>& frames) {
68 std::string frame_data;
69 for (auto frame : frames) {
70 frame_data += android::base::StringPrintf("[%p]", frame);
71 Dl_info info;
72 if (dladdr(frame, &info) != 0 && info.dli_sname != nullptr) {
73 frame_data += ' ';
74 frame_data += info.dli_sname;
75 }
76 frame_data += '\n';
77 }
78 return frame_data;
79 }
80
FindFunction(std::vector<void * > & frames,uintptr_t func_addr)81 static size_t FindFunction(std::vector<void*>& frames, uintptr_t func_addr) {
82 Dl_info func_info;
83 if (!dladdr(reinterpret_cast<void*>(func_addr), &func_info)) {
84 return 0;
85 }
86 for (size_t i = 0; i < frames.size(); i++) {
87 Dl_info frame_info;
88 if (dladdr(frames[i], &frame_info) && func_info.dli_saddr == frame_info.dli_saddr) {
89 return i + 1;
90 }
91 }
92 return 0;
93 }
94
VerifyCalls(std::vector<void * > & frames,size_t * one_idx=nullptr,size_t * two_idx=nullptr)95 static void VerifyCalls(std::vector<void*>& frames, size_t* one_idx = nullptr,
96 size_t* two_idx = nullptr) {
97 // Try and find the CallOne and CallTwo function addresses.
98 size_t call_one_idx = FindFunction(frames, reinterpret_cast<uintptr_t>(&CallOne));
99 ASSERT_TRUE(call_one_idx != 0) << DumpFrames(frames);
100 size_t call_two_idx = FindFunction(frames, reinterpret_cast<uintptr_t>(&CallTwo));
101 ASSERT_TRUE(call_two_idx != 0) << DumpFrames(frames);
102
103 ASSERT_LT(call_two_idx, call_one_idx) << "CallTwo function found after CallOne\n"
104 << DumpFrames(frames);
105
106 if (one_idx != nullptr) *one_idx = call_one_idx;
107 if (two_idx != nullptr) *two_idx = call_two_idx;
108 }
109
TEST(execinfo,backtrace)110 TEST(execinfo, backtrace) {
111 std::vector<void*> frames(kMaxFrames);
112 ASSERT_NO_FATAL_FAILURE(CallOne(frames));
113
114 // Verfiy that there are at least two frames.
115 ASSERT_LT(3U, frames.size()) << DumpFrames(frames);
116
117 VerifyCalls(frames);
118 }
119
TEST(execinfo,backtrace_cutoff_frames)120 TEST(execinfo, backtrace_cutoff_frames) {
121 // Verify the max frames is handled properly
122 std::vector<void*> frames(1);
123 ASSERT_NO_FATAL_FAILURE(CallOne(frames));
124 ASSERT_EQ(1U, frames.size()) << DumpFrames(frames);
125 }
126
TEST(execinfo,backtrace_symbols_errors)127 TEST(execinfo, backtrace_symbols_errors) {
128 void* frames[kMaxFrames];
129 // glibc incorrectly returns memory when a zero is passed in.
130 // Since we know this works properly on bionic, only verify
131 // this there.
132 #if defined(__BIONIC__)
133 ASSERT_EQ(nullptr, backtrace_symbols(frames, 0));
134 #endif
135 ASSERT_EQ(nullptr, backtrace_symbols(frames, -1));
136 }
137
VerifyLineFormat(std::string & line)138 static void VerifyLineFormat(std::string& line) {
139 // Verify that the format of the line is one of these:
140 // elf_file(FuncName+0xFuncAddr) [0xAddress]
141 // elf_file(+0xRelAddress) [0xAddress]
142 // elf_file [0xAddress]
143 // [0xAddress]
144 #if defined(__GLIBC__)
145 // For some reason, glibc will print a space before [0xAddress] for
146 // backtrace symbols, and no space for backtrace_symbols_fd. Allow this
147 // only for glibc.
148 std::regex format1("[^\\(\\s]+\\([^\\+]+\\+0x[0-9a-fA-F]+\\) ?\\[0x[0-9a-fA-F]+\\]");
149 std::regex format2("[^\\(\\s]+\\(+\\+0x[0-9a-fA-F]+\\) ?\\[0x[0-9a-fA-F]+\\]");
150 std::regex format3("[^\\(\\s]+ ?\\[0x[0-9a-fA-F]+\\]");
151 #else
152 std::regex format1("[^\\(\\s]+\\([^\\+]+\\+0x[0-9a-fA-F]+\\) \\[0x[0-9a-fA-F]+\\]");
153 std::regex format2("[^\\(\\s]+\\(+\\+0x[0-9a-fA-F]+\\) \\[0x[0-9a-fA-F]+\\]");
154 std::regex format3("[^\\(\\s]+ \\[0x[0-9a-fA-F]+\\]");
155 #endif
156 std::regex format4("\\[0x[0-9a-fA-F]+\\]");
157
158 EXPECT_TRUE(std::regex_match(line, format1) || std::regex_match(line, format2) ||
159 std::regex_match(line, format3) || std::regex_match(line, format4))
160 << "Unknown format of line:\n"
161 << line;
162 }
163
VerifyLineFormat(char * raw_line,size_t length)164 static void VerifyLineFormat(char* raw_line, size_t length) {
165 std::string line(raw_line, length);
166 VerifyLineFormat(line);
167 }
168
TEST(execinfo,backtrace_symbols)169 TEST(execinfo, backtrace_symbols) {
170 std::vector<void*> frames(kMaxFrames);
171 ASSERT_NO_FATAL_FAILURE(CallOne(frames));
172 ASSERT_LT(3U, frames.size()) << DumpFrames(frames);
173
174 char** symbols = backtrace_symbols(frames.data(), static_cast<int>(frames.size()));
175 ASSERT_TRUE(symbols != nullptr);
176 for (size_t i = 0; i < frames.size(); i++) {
177 ASSERT_TRUE(frames[i] != nullptr);
178 VerifyLineFormat(symbols[i], strlen(symbols[i]));
179 }
180
181 size_t call_one_idx;
182 size_t call_two_idx;
183 ASSERT_NO_FATAL_FAILURE(VerifyCalls(frames, &call_one_idx, &call_two_idx));
184 // Now verify that those frames contain the function names we expect.
185 SCOPED_TRACE(DumpFrames(frames));
186 ASSERT_MATCH(symbols[call_one_idx - 1], "\\(CallOne+");
187 ASSERT_MATCH(symbols[call_two_idx - 1], "\\(CallTwo+");
188 free(symbols);
189 }
190
TEST(execinfo,backtrace_symbols_fd_errors)191 TEST(execinfo, backtrace_symbols_fd_errors) {
192 void* frames[kMaxFrames];
193 frames[0] = reinterpret_cast<void*>(&backtrace_symbols);
194
195 {
196 TemporaryFile tf;
197 backtrace_symbols_fd(frames, 0, tf.fd);
198 close(tf.fd);
199 std::string content;
200 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &content));
201 // Verify that no data is written to the file.
202 ASSERT_TRUE(content.empty());
203 }
204
205 {
206 TemporaryFile tf;
207 backtrace_symbols_fd(frames, -1, tf.fd);
208 close(tf.fd);
209 std::string content;
210 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &content));
211 // Verify that no data is written to the file.
212 ASSERT_TRUE(content.empty());
213 }
214
215 // Verify that there isn't a crash.
216 backtrace_symbols_fd(frames, 0, -1);
217 }
218
TEST(execinfo,backtrace_symbols_fd)219 TEST(execinfo, backtrace_symbols_fd) {
220 std::vector<void*> frames(kMaxFrames);
221 ASSERT_NO_FATAL_FAILURE(CallOne(frames));
222 ASSERT_LT(3U, frames.size()) << DumpFrames(frames);
223
224 TemporaryFile tf;
225 backtrace_symbols_fd(frames.data(), static_cast<int>(frames.size()), tf.fd);
226 close(tf.fd);
227
228 size_t call_one_idx;
229 size_t call_two_idx;
230 ASSERT_NO_FATAL_FAILURE(VerifyCalls(frames, &call_one_idx, &call_two_idx));
231
232 std::ifstream frame_stream(tf.path);
233 ASSERT_TRUE(frame_stream.is_open());
234 size_t num_lines = 0;
235 std::string line;
236 while (std::getline(frame_stream, line)) {
237 ASSERT_FALSE(line.empty());
238 VerifyLineFormat(line);
239 num_lines++;
240
241 if (num_lines == call_one_idx) {
242 EXPECT_MATCH(line, "\\(CallOne+");
243 } else if (num_lines == call_two_idx) {
244 EXPECT_MATCH(line, "\\(CallTwo+");
245 }
246 }
247 ASSERT_EQ(num_lines, frames.size()) << "Number of lines in file does not match number of frames.";
248 }
249