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 for (size_t i = 0; i < frames.size(); i++) {
83 uintptr_t frame_addr = reinterpret_cast<uintptr_t>(frames[i]);
84 if (frame_addr >= func_addr && frame_addr <= func_addr + 0x100) {
85 return i + 1;
86 }
87 }
88 return 0;
89 }
90
VerifyCalls(std::vector<void * > & frames,size_t * one_idx=nullptr,size_t * two_idx=nullptr)91 static void VerifyCalls(std::vector<void*>& frames, size_t* one_idx = nullptr,
92 size_t* two_idx = nullptr) {
93 // Try and find the CallOne and CallTwo function addresses.
94 size_t call_one_idx = FindFunction(frames, reinterpret_cast<uintptr_t>(&CallOne));
95 ASSERT_TRUE(call_one_idx != 0) << DumpFrames(frames);
96 size_t call_two_idx = FindFunction(frames, reinterpret_cast<uintptr_t>(&CallTwo));
97 ASSERT_TRUE(call_two_idx != 0) << DumpFrames(frames);
98
99 ASSERT_LT(call_two_idx, call_one_idx) << "CallTwo function found after CallOne\n"
100 << DumpFrames(frames);
101
102 if (one_idx != nullptr) *one_idx = call_one_idx;
103 if (two_idx != nullptr) *two_idx = call_two_idx;
104 }
105
TEST(execinfo,backtrace)106 TEST(execinfo, backtrace) {
107 std::vector<void*> frames(kMaxFrames);
108 ASSERT_NO_FATAL_FAILURE(CallOne(frames));
109
110 // Verfiy that there are at least two frames.
111 ASSERT_LT(3U, frames.size()) << DumpFrames(frames);
112
113 VerifyCalls(frames);
114 }
115
TEST(execinfo,backtrace_cutoff_frames)116 TEST(execinfo, backtrace_cutoff_frames) {
117 // Verify the max frames is handled properly
118 std::vector<void*> frames(1);
119 ASSERT_NO_FATAL_FAILURE(CallOne(frames));
120 ASSERT_EQ(1U, frames.size()) << DumpFrames(frames);
121 }
122
TEST(execinfo,backtrace_symbols_errors)123 TEST(execinfo, backtrace_symbols_errors) {
124 void* frames[kMaxFrames];
125 // glibc incorrectly returns memory when a zero is passed in.
126 // Since we know this works properly on bionic, only verify
127 // this there.
128 #if defined(__BIONIC__)
129 ASSERT_EQ(nullptr, backtrace_symbols(frames, 0));
130 #endif
131 ASSERT_EQ(nullptr, backtrace_symbols(frames, -1));
132 }
133
VerifyLineFormat(std::string & line)134 static void VerifyLineFormat(std::string& line) {
135 // Verify that the format of the line is one of these:
136 // elf_file(FuncName+0xFuncAddr) [0xAddress]
137 // elf_file(+0xRelAddress) [0xAddress]
138 // elf_file [0xAddress]
139 // [0xAddress]
140 #if defined(__GLIBC__)
141 // For some reason, glibc will print a space before [0xAddress] for
142 // backtrace symbols, and no space for backtrace_symbols_fd. Allow this
143 // only for glibc.
144 std::regex format1("[^\\(\\s]+\\([^\\+]+\\+0x[0-9a-fA-F]+\\) ?\\[0x[0-9a-fA-F]+\\]");
145 std::regex format2("[^\\(\\s]+\\(+\\+0x[0-9a-fA-F]+\\) ?\\[0x[0-9a-fA-F]+\\]");
146 std::regex format3("[^\\(\\s]+ ?\\[0x[0-9a-fA-F]+\\]");
147 #else
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 #endif
152 std::regex format4("\\[0x[0-9a-fA-F]+\\]");
153
154 EXPECT_TRUE(std::regex_match(line, format1) || std::regex_match(line, format2) ||
155 std::regex_match(line, format3) || std::regex_match(line, format4))
156 << "Unknown format of line:\n"
157 << line;
158 }
159
VerifyLineFormat(char * raw_line,size_t length)160 static void VerifyLineFormat(char* raw_line, size_t length) {
161 std::string line(raw_line, length);
162 VerifyLineFormat(line);
163 }
164
TEST(execinfo,backtrace_symbols)165 TEST(execinfo, backtrace_symbols) {
166 std::vector<void*> frames(kMaxFrames);
167 ASSERT_NO_FATAL_FAILURE(CallOne(frames));
168 ASSERT_LT(3U, frames.size()) << DumpFrames(frames);
169
170 char** symbols = backtrace_symbols(frames.data(), static_cast<int>(frames.size()));
171 ASSERT_TRUE(symbols != nullptr);
172 for (size_t i = 0; i < frames.size(); i++) {
173 ASSERT_TRUE(frames[i] != nullptr);
174 VerifyLineFormat(symbols[i], strlen(symbols[i]));
175 }
176
177 size_t call_one_idx;
178 size_t call_two_idx;
179 ASSERT_NO_FATAL_FAILURE(VerifyCalls(frames, &call_one_idx, &call_two_idx));
180 // Now verify that those frames contain the function names we expect.
181 SCOPED_TRACE(DumpFrames(frames));
182 ASSERT_MATCH(symbols[call_one_idx - 1], "\\(CallOne+");
183 ASSERT_MATCH(symbols[call_two_idx - 1], "\\(CallTwo+");
184 free(symbols);
185 }
186
TEST(execinfo,backtrace_symbols_fd_errors)187 TEST(execinfo, backtrace_symbols_fd_errors) {
188 void* frames[kMaxFrames];
189 frames[0] = reinterpret_cast<void*>(&backtrace_symbols);
190
191 {
192 TemporaryFile tf;
193 backtrace_symbols_fd(frames, 0, tf.fd);
194 close(tf.fd);
195 std::string content;
196 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &content));
197 // Verify that no data is written to the file.
198 ASSERT_TRUE(content.empty());
199 }
200
201 {
202 TemporaryFile tf;
203 backtrace_symbols_fd(frames, -1, tf.fd);
204 close(tf.fd);
205 std::string content;
206 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &content));
207 // Verify that no data is written to the file.
208 ASSERT_TRUE(content.empty());
209 }
210
211 // Verify that there isn't a crash.
212 backtrace_symbols_fd(frames, 0, -1);
213 }
214
TEST(execinfo,backtrace_symbols_fd)215 TEST(execinfo, backtrace_symbols_fd) {
216 std::vector<void*> frames(kMaxFrames);
217 ASSERT_NO_FATAL_FAILURE(CallOne(frames));
218 ASSERT_LT(3U, frames.size()) << DumpFrames(frames);
219
220 TemporaryFile tf;
221 backtrace_symbols_fd(frames.data(), static_cast<int>(frames.size()), tf.fd);
222 close(tf.fd);
223
224 size_t call_one_idx;
225 size_t call_two_idx;
226 ASSERT_NO_FATAL_FAILURE(VerifyCalls(frames, &call_one_idx, &call_two_idx));
227
228 std::ifstream frame_stream(tf.path);
229 ASSERT_TRUE(frame_stream.is_open());
230 size_t num_lines = 0;
231 std::string line;
232 while (std::getline(frame_stream, line)) {
233 ASSERT_FALSE(line.empty());
234 VerifyLineFormat(line);
235 num_lines++;
236
237 if (num_lines == call_one_idx) {
238 EXPECT_MATCH(line, "\\(CallOne+");
239 } else if (num_lines == call_two_idx) {
240 EXPECT_MATCH(line, "\\(CallTwo+");
241 }
242 }
243 ASSERT_EQ(num_lines, frames.size()) << "Number of lines in file does not match number of frames.";
244 }
245