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 | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 30 | #include <limits.h> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 32 | #include <signal.h> |
| 33 | #include <sys/types.h> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 34 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 35 | #include "breakpad_googletest_includes.h" |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 36 | #include "client/linux/minidump_writer/linux_dumper.h" |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 37 | #include "common/linux/file_id.h" |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 38 | #include "common/linux/memory.h" |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace google_breakpad; |
| 41 | |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 42 | // This provides a wrapper around system calls which may be |
| 43 | // interrupted by a signal and return EINTR. See man 7 signal. |
| 44 | #define HANDLE_EINTR(x) ({ \ |
| 45 | typeof(x) __eintr_result__; \ |
| 46 | do { \ |
| 47 | __eintr_result__ = x; \ |
| 48 | } while (__eintr_result__ == -1 && errno == EINTR); \ |
| 49 | __eintr_result__;\ |
| 50 | }) |
| 51 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 52 | namespace { |
| 53 | typedef testing::Test LinuxDumperTest; |
| 54 | } |
| 55 | |
| 56 | TEST(LinuxDumperTest, Setup) { |
| 57 | LinuxDumper dumper(getpid()); |
| 58 | } |
| 59 | |
| 60 | TEST(LinuxDumperTest, FindMappings) { |
| 61 | LinuxDumper dumper(getpid()); |
| 62 | ASSERT_TRUE(dumper.Init()); |
| 63 | |
| 64 | ASSERT_TRUE(dumper.FindMapping(reinterpret_cast<void*>(getpid))); |
| 65 | ASSERT_TRUE(dumper.FindMapping(reinterpret_cast<void*>(printf))); |
| 66 | ASSERT_FALSE(dumper.FindMapping(NULL)); |
| 67 | } |
| 68 | |
| 69 | TEST(LinuxDumperTest, ThreadList) { |
| 70 | LinuxDumper dumper(getpid()); |
| 71 | ASSERT_TRUE(dumper.Init()); |
| 72 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 73 | ASSERT_GE(dumper.threads().size(), (size_t)1); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 74 | bool found = false; |
| 75 | for (size_t i = 0; i < dumper.threads().size(); ++i) { |
| 76 | if (dumper.threads()[i] == getpid()) { |
| 77 | found = true; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Ken Mixter | 5492dd2 | 2010-11-11 15:58:22 -0800 | [diff] [blame^] | 83 | // Comment out this test due to crosbug.com/6712. Only seems to |
| 84 | // fail on heavily loaded buildbots and is written with timing |
| 85 | // assumptions. |
| 86 | #if 0 |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 87 | TEST(LinuxDumperTest, VerifyStackReadWithMultipleThreads) { |
| 88 | static const int kNumberOfThreadsInHelperProgram = 5; |
| 89 | char kNumberOfThreadsArgument[2]; |
| 90 | sprintf(kNumberOfThreadsArgument, "%d", kNumberOfThreadsInHelperProgram); |
| 91 | |
| 92 | pid_t child_pid = fork(); |
| 93 | if (child_pid == 0) { |
| 94 | // Set the number of threads |
| 95 | execl("src/client/linux/linux_dumper_unittest_helper", |
| 96 | "linux_dumper_unittest_helper", |
| 97 | kNumberOfThreadsArgument, |
| 98 | NULL); |
| 99 | // Kill if we get here. |
| 100 | printf("Errno from exec: %d", errno); |
| 101 | FAIL() << "Exec failed: " << strerror(errno); |
| 102 | exit(0); |
| 103 | } |
| 104 | // The sleep is flaky, but prevents us from reading |
| 105 | // the child process before all threads have been created. |
| 106 | sleep(1); |
| 107 | LinuxDumper dumper(child_pid); |
| 108 | EXPECT_TRUE(dumper.Init()); |
| 109 | EXPECT_EQ((size_t)kNumberOfThreadsInHelperProgram, dumper.threads().size()); |
| 110 | EXPECT_TRUE(dumper.ThreadsSuspend()); |
| 111 | |
| 112 | ThreadInfo one_thread; |
| 113 | for(size_t i = 0; i < dumper.threads().size(); ++i) { |
| 114 | EXPECT_TRUE(dumper.ThreadInfoGet(dumper.threads()[i], &one_thread)); |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 115 | // In the helper program, we stored a pointer to the thread id in a |
| 116 | // specific register. Check that we can recover its value. |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 117 | #if defined(__ARM_EABI__) |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 118 | pid_t *process_tid_location = (pid_t *)(one_thread.regs.uregs[3]); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 119 | #elif defined(__i386) |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 120 | pid_t *process_tid_location = (pid_t *)(one_thread.regs.ecx); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 121 | #elif defined(__x86_64) |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 122 | pid_t *process_tid_location = (pid_t *)(one_thread.regs.rcx); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 123 | #else |
jimblandy | b68b800 | 2010-03-29 18:23:42 +0000 | [diff] [blame] | 124 | #error This test has not been ported to this platform. |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 125 | #endif |
| 126 | pid_t one_thread_id; |
| 127 | dumper.CopyFromProcess(&one_thread_id, |
| 128 | dumper.threads()[i], |
| 129 | process_tid_location, |
| 130 | 4); |
| 131 | EXPECT_EQ(dumper.threads()[i], one_thread_id); |
| 132 | } |
| 133 | kill(child_pid, SIGKILL); |
| 134 | } |
Ken Mixter | 5492dd2 | 2010-11-11 15:58:22 -0800 | [diff] [blame^] | 135 | #endif |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 136 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 137 | TEST(LinuxDumperTest, BuildProcPath) { |
| 138 | const pid_t pid = getpid(); |
| 139 | LinuxDumper dumper(pid); |
| 140 | |
| 141 | char maps_path[256] = "dummymappath"; |
| 142 | char maps_path_expected[256]; |
| 143 | snprintf(maps_path_expected, sizeof(maps_path_expected), |
| 144 | "/proc/%d/maps", pid); |
| 145 | dumper.BuildProcPath(maps_path, pid, "maps"); |
| 146 | ASSERT_STREQ(maps_path, maps_path_expected); |
| 147 | |
| 148 | // In release mode, we expect BuildProcPath to handle the invalid |
| 149 | // parameters correctly and fill map_path with an empty |
| 150 | // NULL-terminated string. |
| 151 | #ifdef NDEBUG |
| 152 | snprintf(maps_path, sizeof(maps_path), "dummymappath"); |
| 153 | dumper.BuildProcPath(maps_path, 0, "maps"); |
| 154 | EXPECT_STREQ(maps_path, ""); |
| 155 | |
| 156 | snprintf(maps_path, sizeof(maps_path), "dummymappath"); |
| 157 | dumper.BuildProcPath(maps_path, getpid(), ""); |
| 158 | EXPECT_STREQ(maps_path, ""); |
| 159 | |
| 160 | snprintf(maps_path, sizeof(maps_path), "dummymappath"); |
| 161 | dumper.BuildProcPath(maps_path, getpid(), NULL); |
| 162 | EXPECT_STREQ(maps_path, ""); |
| 163 | #endif |
| 164 | } |
| 165 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 166 | #if !defined(__ARM_EABI__) |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 167 | TEST(LinuxDumperTest, MappingsIncludeLinuxGate) { |
| 168 | LinuxDumper dumper(getpid()); |
| 169 | ASSERT_TRUE(dumper.Init()); |
| 170 | |
| 171 | void* linux_gate_loc = dumper.FindBeginningOfLinuxGateSharedLibrary(getpid()); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 172 | ASSERT_TRUE(linux_gate_loc); |
| 173 | bool found_linux_gate = false; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 174 | |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 175 | const wasteful_vector<MappingInfo*> mappings = dumper.mappings(); |
| 176 | const MappingInfo* mapping; |
| 177 | for (unsigned i = 0; i < mappings.size(); ++i) { |
| 178 | mapping = mappings[i]; |
| 179 | if (!strcmp(mapping->name, kLinuxGateLibraryName)) { |
| 180 | found_linux_gate = true; |
| 181 | break; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 182 | } |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 183 | } |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 184 | EXPECT_TRUE(found_linux_gate); |
| 185 | EXPECT_EQ(linux_gate_loc, reinterpret_cast<void*>(mapping->start_addr)); |
| 186 | EXPECT_EQ(0, memcmp(linux_gate_loc, ELFMAG, SELFMAG)); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 187 | } |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 188 | #endif |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 189 | |
| 190 | TEST(LinuxDumperTest, FileIDsMatch) { |
| 191 | // Calculate the File ID of our binary using both |
| 192 | // FileID::ElfFileIdentifier and LinuxDumper::ElfFileIdentifierForMapping |
| 193 | // and ensure that we get the same result from both. |
| 194 | char exe_name[PATH_MAX]; |
| 195 | ssize_t len = readlink("/proc/self/exe", exe_name, PATH_MAX - 1); |
| 196 | ASSERT_NE(len, -1); |
| 197 | exe_name[len] = '\0'; |
| 198 | |
| 199 | int fds[2]; |
| 200 | ASSERT_NE(-1, pipe(fds)); |
| 201 | |
| 202 | // fork a child so we can ptrace it |
| 203 | const pid_t child = fork(); |
| 204 | if (child == 0) { |
| 205 | close(fds[1]); |
| 206 | // now wait forever for the parent |
| 207 | char b; |
| 208 | HANDLE_EINTR(read(fds[0], &b, sizeof(b))); |
| 209 | close(fds[0]); |
| 210 | syscall(__NR_exit); |
| 211 | } |
| 212 | close(fds[0]); |
| 213 | |
| 214 | LinuxDumper dumper(child); |
| 215 | ASSERT_TRUE(dumper.Init()); |
| 216 | const wasteful_vector<MappingInfo*> mappings = dumper.mappings(); |
| 217 | bool found_exe = false; |
| 218 | unsigned i; |
| 219 | for (i = 0; i < mappings.size(); ++i) { |
| 220 | const MappingInfo* mapping = mappings[i]; |
| 221 | if (!strcmp(mapping->name, exe_name)) { |
| 222 | found_exe = true; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | ASSERT_TRUE(found_exe); |
| 227 | |
| 228 | uint8_t identifier1[sizeof(MDGUID)]; |
| 229 | uint8_t identifier2[sizeof(MDGUID)]; |
| 230 | EXPECT_TRUE(dumper.ElfFileIdentifierForMapping(i, identifier1)); |
| 231 | FileID fileid(exe_name); |
| 232 | EXPECT_TRUE(fileid.ElfFileIdentifier(identifier2)); |
| 233 | char identifier_string1[37]; |
| 234 | char identifier_string2[37]; |
| 235 | FileID::ConvertIdentifierToString(identifier1, identifier_string1, |
| 236 | 37); |
| 237 | FileID::ConvertIdentifierToString(identifier2, identifier_string2, |
| 238 | 37); |
| 239 | EXPECT_STREQ(identifier_string1, identifier_string2); |
| 240 | close(fds[1]); |
| 241 | } |