nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 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 |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
ted.mielczarek | cfc8628 | 2010-10-20 15:51:38 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 32 | #include <limits.h> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 34 | #include <signal.h> |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame^] | 35 | #include <stdint.h> |
| 36 | #include <sys/poll.h> |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 37 | #include <sys/types.h> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 38 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 39 | #include "breakpad_googletest_includes.h" |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 40 | #include "client/linux/minidump_writer/linux_dumper.h" |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame^] | 41 | #include "common/linux/eintr_wrapper.h" |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 42 | #include "common/linux/file_id.h" |
ted.mielczarek | 4621ee0 | 2010-09-23 14:55:50 +0000 | [diff] [blame] | 43 | #include "common/memory.h" |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 44 | |
ted.mielczarek | cfc8628 | 2010-10-20 15:51:38 +0000 | [diff] [blame] | 45 | using std::string; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 46 | using namespace google_breakpad; |
| 47 | |
| 48 | namespace { |
| 49 | typedef testing::Test LinuxDumperTest; |
| 50 | } |
| 51 | |
| 52 | TEST(LinuxDumperTest, Setup) { |
| 53 | LinuxDumper dumper(getpid()); |
| 54 | } |
| 55 | |
| 56 | TEST(LinuxDumperTest, FindMappings) { |
| 57 | LinuxDumper dumper(getpid()); |
| 58 | ASSERT_TRUE(dumper.Init()); |
| 59 | |
| 60 | ASSERT_TRUE(dumper.FindMapping(reinterpret_cast<void*>(getpid))); |
| 61 | ASSERT_TRUE(dumper.FindMapping(reinterpret_cast<void*>(printf))); |
| 62 | ASSERT_FALSE(dumper.FindMapping(NULL)); |
| 63 | } |
| 64 | |
| 65 | TEST(LinuxDumperTest, ThreadList) { |
| 66 | LinuxDumper dumper(getpid()); |
| 67 | ASSERT_TRUE(dumper.Init()); |
| 68 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 69 | ASSERT_GE(dumper.threads().size(), (size_t)1); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 70 | bool found = false; |
| 71 | for (size_t i = 0; i < dumper.threads().size(); ++i) { |
| 72 | if (dumper.threads()[i] == getpid()) { |
| 73 | found = true; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 79 | TEST(LinuxDumperTest, VerifyStackReadWithMultipleThreads) { |
| 80 | static const int kNumberOfThreadsInHelperProgram = 5; |
| 81 | char kNumberOfThreadsArgument[2]; |
| 82 | sprintf(kNumberOfThreadsArgument, "%d", kNumberOfThreadsInHelperProgram); |
| 83 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame^] | 84 | int fds[2]; |
| 85 | ASSERT_NE(-1, pipe(fds)); |
| 86 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 87 | pid_t child_pid = fork(); |
| 88 | if (child_pid == 0) { |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame^] | 89 | // In child process. |
| 90 | close(fds[0]); |
| 91 | |
ted.mielczarek | cfc8628 | 2010-10-20 15:51:38 +0000 | [diff] [blame] | 92 | // Locate helper binary next to the current binary. |
| 93 | char self_path[PATH_MAX]; |
| 94 | if (readlink("/proc/self/exe", self_path, sizeof(self_path) - 1) == -1) { |
| 95 | FAIL() << "readlink failed: " << strerror(errno); |
| 96 | exit(1); |
| 97 | } |
| 98 | string helper_path(self_path); |
| 99 | size_t pos = helper_path.rfind('/'); |
| 100 | if (pos == string::npos) { |
| 101 | FAIL() << "no trailing slash in path: " << helper_path; |
| 102 | exit(1); |
| 103 | } |
| 104 | helper_path.erase(pos + 1); |
| 105 | helper_path += "linux_dumper_unittest_helper"; |
| 106 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame^] | 107 | // Pass the pipe fd and the number of threads as arguments. |
| 108 | char pipe_fd_string[8]; |
| 109 | sprintf(pipe_fd_string, "%d", fds[1]); |
ted.mielczarek | cfc8628 | 2010-10-20 15:51:38 +0000 | [diff] [blame] | 110 | execl(helper_path.c_str(), |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 111 | "linux_dumper_unittest_helper", |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame^] | 112 | pipe_fd_string, |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 113 | kNumberOfThreadsArgument, |
| 114 | NULL); |
| 115 | // Kill if we get here. |
| 116 | printf("Errno from exec: %d", errno); |
ted.mielczarek | cfc8628 | 2010-10-20 15:51:38 +0000 | [diff] [blame] | 117 | FAIL() << "Exec of " << helper_path << " failed: " << strerror(errno); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 118 | exit(0); |
| 119 | } |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame^] | 120 | close(fds[1]); |
| 121 | // Wait for the child process to signal that it's ready. |
| 122 | struct pollfd pfd; |
| 123 | memset(&pfd, 0, sizeof(pfd)); |
| 124 | pfd.fd = fds[0]; |
| 125 | pfd.events = POLLIN | POLLERR; |
| 126 | |
| 127 | const int r = HANDLE_EINTR(poll(&pfd, 1, 1000)); |
| 128 | ASSERT_EQ(1, r); |
| 129 | ASSERT_TRUE(pfd.revents & POLLIN); |
| 130 | uint8_t junk; |
| 131 | read(fds[0], &junk, sizeof(junk)); |
| 132 | close(fds[0]); |
| 133 | |
| 134 | // Child is ready now. |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 135 | LinuxDumper dumper(child_pid); |
ted.mielczarek | cfc8628 | 2010-10-20 15:51:38 +0000 | [diff] [blame] | 136 | ASSERT_TRUE(dumper.Init()); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 137 | EXPECT_EQ((size_t)kNumberOfThreadsInHelperProgram, dumper.threads().size()); |
| 138 | EXPECT_TRUE(dumper.ThreadsSuspend()); |
| 139 | |
| 140 | ThreadInfo one_thread; |
| 141 | for(size_t i = 0; i < dumper.threads().size(); ++i) { |
| 142 | EXPECT_TRUE(dumper.ThreadInfoGet(dumper.threads()[i], &one_thread)); |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 143 | // In the helper program, we stored a pointer to the thread id in a |
| 144 | // specific register. Check that we can recover its value. |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 145 | #if defined(__ARM_EABI__) |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 146 | pid_t *process_tid_location = (pid_t *)(one_thread.regs.uregs[3]); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 147 | #elif defined(__i386) |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 148 | pid_t *process_tid_location = (pid_t *)(one_thread.regs.ecx); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 149 | #elif defined(__x86_64) |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 150 | pid_t *process_tid_location = (pid_t *)(one_thread.regs.rcx); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 151 | #else |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 152 | #error This test has not been ported to this platform. |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 153 | #endif |
| 154 | pid_t one_thread_id; |
| 155 | dumper.CopyFromProcess(&one_thread_id, |
| 156 | dumper.threads()[i], |
| 157 | process_tid_location, |
| 158 | 4); |
| 159 | EXPECT_EQ(dumper.threads()[i], one_thread_id); |
| 160 | } |
| 161 | kill(child_pid, SIGKILL); |
| 162 | } |
| 163 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 164 | TEST(LinuxDumperTest, BuildProcPath) { |
| 165 | const pid_t pid = getpid(); |
| 166 | LinuxDumper dumper(pid); |
| 167 | |
| 168 | char maps_path[256] = "dummymappath"; |
| 169 | char maps_path_expected[256]; |
| 170 | snprintf(maps_path_expected, sizeof(maps_path_expected), |
| 171 | "/proc/%d/maps", pid); |
| 172 | dumper.BuildProcPath(maps_path, pid, "maps"); |
| 173 | ASSERT_STREQ(maps_path, maps_path_expected); |
| 174 | |
| 175 | // In release mode, we expect BuildProcPath to handle the invalid |
| 176 | // parameters correctly and fill map_path with an empty |
| 177 | // NULL-terminated string. |
| 178 | #ifdef NDEBUG |
| 179 | snprintf(maps_path, sizeof(maps_path), "dummymappath"); |
| 180 | dumper.BuildProcPath(maps_path, 0, "maps"); |
| 181 | EXPECT_STREQ(maps_path, ""); |
| 182 | |
| 183 | snprintf(maps_path, sizeof(maps_path), "dummymappath"); |
| 184 | dumper.BuildProcPath(maps_path, getpid(), ""); |
| 185 | EXPECT_STREQ(maps_path, ""); |
| 186 | |
| 187 | snprintf(maps_path, sizeof(maps_path), "dummymappath"); |
| 188 | dumper.BuildProcPath(maps_path, getpid(), NULL); |
| 189 | EXPECT_STREQ(maps_path, ""); |
| 190 | #endif |
| 191 | } |
| 192 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 193 | #if !defined(__ARM_EABI__) |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 194 | TEST(LinuxDumperTest, MappingsIncludeLinuxGate) { |
| 195 | LinuxDumper dumper(getpid()); |
| 196 | ASSERT_TRUE(dumper.Init()); |
| 197 | |
| 198 | void* linux_gate_loc = dumper.FindBeginningOfLinuxGateSharedLibrary(getpid()); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 199 | ASSERT_TRUE(linux_gate_loc); |
| 200 | bool found_linux_gate = false; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 201 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 202 | const wasteful_vector<MappingInfo*> mappings = dumper.mappings(); |
| 203 | const MappingInfo* mapping; |
| 204 | for (unsigned i = 0; i < mappings.size(); ++i) { |
| 205 | mapping = mappings[i]; |
| 206 | if (!strcmp(mapping->name, kLinuxGateLibraryName)) { |
| 207 | found_linux_gate = true; |
| 208 | break; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 209 | } |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 210 | } |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 211 | EXPECT_TRUE(found_linux_gate); |
| 212 | EXPECT_EQ(linux_gate_loc, reinterpret_cast<void*>(mapping->start_addr)); |
| 213 | EXPECT_EQ(0, memcmp(linux_gate_loc, ELFMAG, SELFMAG)); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 214 | } |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 215 | #endif |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 216 | |
| 217 | TEST(LinuxDumperTest, FileIDsMatch) { |
| 218 | // Calculate the File ID of our binary using both |
| 219 | // FileID::ElfFileIdentifier and LinuxDumper::ElfFileIdentifierForMapping |
| 220 | // and ensure that we get the same result from both. |
| 221 | char exe_name[PATH_MAX]; |
| 222 | ssize_t len = readlink("/proc/self/exe", exe_name, PATH_MAX - 1); |
| 223 | ASSERT_NE(len, -1); |
| 224 | exe_name[len] = '\0'; |
| 225 | |
| 226 | int fds[2]; |
| 227 | ASSERT_NE(-1, pipe(fds)); |
| 228 | |
| 229 | // fork a child so we can ptrace it |
| 230 | const pid_t child = fork(); |
| 231 | if (child == 0) { |
| 232 | close(fds[1]); |
| 233 | // now wait forever for the parent |
| 234 | char b; |
| 235 | HANDLE_EINTR(read(fds[0], &b, sizeof(b))); |
| 236 | close(fds[0]); |
| 237 | syscall(__NR_exit); |
| 238 | } |
| 239 | close(fds[0]); |
| 240 | |
| 241 | LinuxDumper dumper(child); |
| 242 | ASSERT_TRUE(dumper.Init()); |
| 243 | const wasteful_vector<MappingInfo*> mappings = dumper.mappings(); |
| 244 | bool found_exe = false; |
| 245 | unsigned i; |
| 246 | for (i = 0; i < mappings.size(); ++i) { |
| 247 | const MappingInfo* mapping = mappings[i]; |
| 248 | if (!strcmp(mapping->name, exe_name)) { |
| 249 | found_exe = true; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | ASSERT_TRUE(found_exe); |
| 254 | |
| 255 | uint8_t identifier1[sizeof(MDGUID)]; |
| 256 | uint8_t identifier2[sizeof(MDGUID)]; |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame^] | 257 | EXPECT_TRUE(dumper.ElfFileIdentifierForMapping(*mappings[i], i, |
| 258 | identifier1)); |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 259 | FileID fileid(exe_name); |
| 260 | EXPECT_TRUE(fileid.ElfFileIdentifier(identifier2)); |
| 261 | char identifier_string1[37]; |
| 262 | char identifier_string2[37]; |
| 263 | FileID::ConvertIdentifierToString(identifier1, identifier_string1, |
| 264 | 37); |
| 265 | FileID::ConvertIdentifierToString(identifier2, identifier_string2, |
| 266 | 37); |
| 267 | EXPECT_STREQ(identifier_string1, identifier_string2); |
| 268 | close(fds[1]); |
| 269 | } |