OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/mac/mach_o_image_reader.h" |
| 16 |
| 17 #include <dlfcn.h> |
| 18 #include <mach-o/dyld_images.h> |
| 19 #include <mach-o/getsect.h> |
| 20 #include <mach-o/loader.h> |
| 21 #include <stdint.h> |
| 22 |
| 23 #include "base/strings/stringprintf.h" |
| 24 #include "build/build_config.h" |
| 25 #include "gtest/gtest.h" |
| 26 #include "util/mac/mach_o_image_segment_reader.h" |
| 27 #include "util/mac/process_reader.h" |
| 28 #include "util/mac/process_types.h" |
| 29 #include "util/misc/uuid.h" |
| 30 #include "util/test/mac/dyld.h" |
| 31 |
| 32 namespace { |
| 33 |
| 34 using namespace crashpad; |
| 35 |
| 36 // Native types and constants, in cases where the 32-bit and 64-bit versions |
| 37 // are different. |
| 38 #if defined(ARCH_CPU_64_BITS) |
| 39 typedef mach_header_64 MachHeader; |
| 40 const uint32_t kMachMagic = MH_MAGIC_64; |
| 41 typedef segment_command_64 SegmentCommand; |
| 42 const uint32_t kSegmentCommand = LC_SEGMENT_64; |
| 43 typedef section_64 Section; |
| 44 #else |
| 45 typedef mach_header MachHeader; |
| 46 const uint32_t kMachMagic = MH_MAGIC; |
| 47 typedef segment_command SegmentCommand; |
| 48 const uint32_t kSegmentCommand = LC_SEGMENT; |
| 49 typedef section Section; |
| 50 #endif |
| 51 |
| 52 #if defined(ARCH_CPU_X86_64) |
| 53 const int kCPUType = CPU_TYPE_X86_64; |
| 54 #elif defined(ARCH_CPU_X86) |
| 55 const int kCPUType = CPU_TYPE_X86; |
| 56 #endif |
| 57 |
| 58 // Verifies that |expect_section| and |actual_section| agree. |
| 59 void ExpectSection(const Section* expect_section, |
| 60 const process_types::section* actual_section) { |
| 61 ASSERT_TRUE(expect_section); |
| 62 ASSERT_TRUE(actual_section); |
| 63 |
| 64 EXPECT_EQ( |
| 65 MachOImageSegmentReader::SectionNameString(expect_section->sectname), |
| 66 MachOImageSegmentReader::SectionNameString(actual_section->sectname)); |
| 67 EXPECT_EQ( |
| 68 MachOImageSegmentReader::SegmentNameString(expect_section->segname), |
| 69 MachOImageSegmentReader::SegmentNameString(actual_section->segname)); |
| 70 EXPECT_EQ(expect_section->addr, actual_section->addr); |
| 71 EXPECT_EQ(expect_section->size, actual_section->size); |
| 72 EXPECT_EQ(expect_section->offset, actual_section->offset); |
| 73 EXPECT_EQ(expect_section->align, actual_section->align); |
| 74 EXPECT_EQ(expect_section->reloff, actual_section->reloff); |
| 75 EXPECT_EQ(expect_section->nreloc, actual_section->nreloc); |
| 76 EXPECT_EQ(expect_section->flags, actual_section->flags); |
| 77 EXPECT_EQ(expect_section->reserved1, actual_section->reserved1); |
| 78 EXPECT_EQ(expect_section->reserved2, actual_section->reserved2); |
| 79 } |
| 80 |
| 81 // Verifies that |expect_segment| is a valid Mach-O segment load command for the |
| 82 // current system by checking its |cmd| field. Then, verifies that the |
| 83 // information in |actual_segment| matches that in |expect_segment|. The |
| 84 // |segname|, |vmaddr|, |vmsize|, and |fileoff| fields are examined. Each |
| 85 // section within the segment is also examined by calling ExpectSection(). |
| 86 // Access to each section via both MachOImageSegmentReader::GetSectionByName() |
| 87 // and MachOImageReader::GetSectionByName() is verified, expecting that each |
| 88 // call produces the same section. Segment and section data addresses are |
| 89 // verified against data obtained by calling getsegmentdata() and |
| 90 // getsectiondata(). The segment is checked to make sure that it behaves |
| 91 // correctly when attempting to look up a nonexistent section by name. |
| 92 // |section_index| is used to track the last-used section index in an image on |
| 93 // entry, and is reset to the last-used section index on return after the |
| 94 // sections are processed. This is used to test that |
| 95 // MachOImageReader::GetSectionAtIndex() returns the correct result. |
| 96 void ExpectSegmentCommand(const SegmentCommand* expect_segment, |
| 97 const MachHeader* expect_image, |
| 98 const MachOImageSegmentReader* actual_segment, |
| 99 mach_vm_address_t actual_segment_address, |
| 100 mach_vm_size_t actual_segment_size, |
| 101 const MachOImageReader* actual_image, |
| 102 size_t* section_index) { |
| 103 ASSERT_TRUE(expect_segment); |
| 104 ASSERT_TRUE(actual_segment); |
| 105 |
| 106 EXPECT_EQ(kSegmentCommand, expect_segment->cmd); |
| 107 |
| 108 std::string segment_name = actual_segment->Name(); |
| 109 EXPECT_EQ(MachOImageSegmentReader::SegmentNameString(expect_segment->segname), |
| 110 segment_name); |
| 111 EXPECT_EQ(expect_segment->vmaddr, actual_segment->vmaddr()); |
| 112 EXPECT_EQ(expect_segment->vmsize, actual_segment->vmsize()); |
| 113 EXPECT_EQ(expect_segment->fileoff, actual_segment->fileoff()); |
| 114 |
| 115 if (actual_segment->SegmentSlides()) { |
| 116 EXPECT_EQ(actual_segment_address, |
| 117 actual_segment->vmaddr() + actual_image->Slide()); |
| 118 |
| 119 unsigned long expect_segment_size; |
| 120 const uint8_t* expect_segment_data = getsegmentdata( |
| 121 expect_image, segment_name.c_str(), &expect_segment_size); |
| 122 mach_vm_address_t expect_segment_address = |
| 123 reinterpret_cast<mach_vm_address_t>(expect_segment_data); |
| 124 EXPECT_EQ(expect_segment_address, actual_segment_address); |
| 125 EXPECT_EQ(expect_segment_size, actual_segment->vmsize()); |
| 126 EXPECT_EQ(actual_segment->vmsize(), actual_segment_size); |
| 127 } else { |
| 128 // getsegmentdata() doesn’t return appropriate data for the __PAGEZERO |
| 129 // segment because getsegmentdata() always adjusts for slide, but the |
| 130 // __PAGEZERO segment never slides, it just grows. Skip the getsegmentdata() |
| 131 // check for that segment according to the same rules that the kernel uses |
| 132 // to identify __PAGEZERO. See 10.9.4 xnu-2422.110.17/bsd/kern/mach_loader.c |
| 133 // load_segment(). |
| 134 EXPECT_EQ(actual_segment_address, actual_segment->vmaddr()); |
| 135 EXPECT_EQ(actual_segment->vmsize() + actual_image->Slide(), |
| 136 actual_segment_size); |
| 137 } |
| 138 |
| 139 ASSERT_EQ(expect_segment->nsects, actual_segment->nsects()); |
| 140 |
| 141 // Make sure that the expected load command is big enough for the number of |
| 142 // sections that it claims to have, and set up a pointer to its first section |
| 143 // structure. |
| 144 ASSERT_EQ(sizeof(*expect_segment) + expect_segment->nsects * sizeof(Section), |
| 145 expect_segment->cmdsize); |
| 146 const Section* expect_sections = |
| 147 reinterpret_cast<const Section*>(&expect_segment[1]); |
| 148 |
| 149 for (size_t index = 0; index < actual_segment->nsects(); ++index) { |
| 150 const Section* expect_section = &expect_sections[index]; |
| 151 const process_types::section* actual_section = |
| 152 actual_segment->GetSectionAtIndex(index); |
| 153 ExpectSection(&expect_sections[index], actual_section); |
| 154 if (testing::Test::HasFatalFailure()) { |
| 155 return; |
| 156 } |
| 157 |
| 158 // Make sure that the section is accessible by GetSectionByName as well. |
| 159 std::string section_name = |
| 160 MachOImageSegmentReader::SectionNameString(expect_section->sectname); |
| 161 const process_types::section* actual_section_by_name = |
| 162 actual_segment->GetSectionByName(section_name); |
| 163 EXPECT_EQ(actual_section, actual_section_by_name); |
| 164 |
| 165 // Make sure that the section is accessible by the parent MachOImageReader’s |
| 166 // GetSectionByName. |
| 167 mach_vm_address_t actual_section_address; |
| 168 const process_types::section* actual_section_from_image_by_name = |
| 169 actual_image->GetSectionByName( |
| 170 segment_name, section_name, &actual_section_address); |
| 171 EXPECT_EQ(actual_section, actual_section_from_image_by_name); |
| 172 |
| 173 if (actual_segment->SegmentSlides()) { |
| 174 EXPECT_EQ(actual_section_address, |
| 175 actual_section->addr + actual_image->Slide()); |
| 176 |
| 177 unsigned long expect_section_size; |
| 178 const uint8_t* expect_section_data = getsectiondata(expect_image, |
| 179 segment_name.c_str(), |
| 180 section_name.c_str(), |
| 181 &expect_section_size); |
| 182 mach_vm_address_t expect_section_address = |
| 183 reinterpret_cast<mach_vm_address_t>(expect_section_data); |
| 184 EXPECT_EQ(expect_section_address, actual_section_address); |
| 185 EXPECT_EQ(expect_section_size, actual_section->size); |
| 186 } else { |
| 187 EXPECT_EQ(actual_section_address, actual_section->addr); |
| 188 } |
| 189 |
| 190 // Test the parent MachOImageReader’s GetSectionAtIndex as well. |
| 191 mach_vm_address_t actual_section_address_at_index; |
| 192 const process_types::section* actual_section_from_image_at_index = |
| 193 actual_image->GetSectionAtIndex(++(*section_index), |
| 194 &actual_section_address_at_index); |
| 195 EXPECT_EQ(actual_section, actual_section_from_image_at_index); |
| 196 EXPECT_EQ(actual_section_address, actual_section_address_at_index); |
| 197 } |
| 198 |
| 199 EXPECT_EQ(NULL, actual_segment->GetSectionByName("NoSuchSection")); |
| 200 } |
| 201 |
| 202 // Walks through the load commands of |expect_image|, finding all of the |
| 203 // expected segment commands. For each expected segment command, calls |
| 204 // actual_image->GetSegmentByName() to obtain an actual segment command, and |
| 205 // calls ExpectSegmentCommand() to compare the expected and actual segments. A |
| 206 // series of by-name lookups is also performed on the segment to ensure that it |
| 207 // behaves correctly when attempting to look up segment and section names that |
| 208 // are not present. |test_section_indices| should be true to test |
| 209 // MachOImageReader::GetSectionAtIndex() using out-of-range section indices. |
| 210 // This should be tested for at least one module, but it’s very noisy in terms |
| 211 // of logging output, so this knob is provided to suppress this portion of the |
| 212 // test when looping over all modules. |
| 213 void ExpectSegmentCommands(const MachHeader* expect_image, |
| 214 const MachOImageReader* actual_image, |
| 215 bool test_section_index_bounds) { |
| 216 ASSERT_TRUE(expect_image); |
| 217 ASSERT_TRUE(actual_image); |
| 218 |
| 219 const char* commands_base = reinterpret_cast<const char*>(&expect_image[1]); |
| 220 uint32_t position = 0; |
| 221 size_t section_index = 0; |
| 222 for (uint32_t index = 0; index < expect_image->ncmds; ++index) { |
| 223 ASSERT_LT(position, expect_image->sizeofcmds); |
| 224 const load_command* command = |
| 225 reinterpret_cast<const load_command*>(&commands_base[position]); |
| 226 ASSERT_LE(position + command->cmdsize, expect_image->sizeofcmds); |
| 227 if (command->cmd == kSegmentCommand) { |
| 228 const SegmentCommand* expect_segment = |
| 229 reinterpret_cast<const SegmentCommand*>(command); |
| 230 std::string segment_name = |
| 231 MachOImageSegmentReader::SegmentNameString(expect_segment->segname); |
| 232 mach_vm_address_t actual_segment_address; |
| 233 mach_vm_size_t actual_segment_size; |
| 234 const MachOImageSegmentReader* actual_segment = |
| 235 actual_image->GetSegmentByName( |
| 236 segment_name, &actual_segment_address, &actual_segment_size); |
| 237 ExpectSegmentCommand(expect_segment, |
| 238 expect_image, |
| 239 actual_segment, |
| 240 actual_segment_address, |
| 241 actual_segment_size, |
| 242 actual_image, |
| 243 §ion_index); |
| 244 if (testing::Test::HasFatalFailure()) { |
| 245 return; |
| 246 } |
| 247 } |
| 248 position += command->cmdsize; |
| 249 } |
| 250 EXPECT_EQ(expect_image->sizeofcmds, position); |
| 251 |
| 252 if (test_section_index_bounds) { |
| 253 // GetSectionAtIndex uses a 1-based index. Make sure that the range is |
| 254 // correct. |
| 255 EXPECT_EQ(NULL, actual_image->GetSectionAtIndex(0, NULL)); |
| 256 EXPECT_EQ(NULL, actual_image->GetSectionAtIndex(section_index + 1, NULL)); |
| 257 } |
| 258 |
| 259 // Make sure that by-name lookups for names that don’t exist work properly: |
| 260 // they should return NULL. |
| 261 EXPECT_FALSE(actual_image->GetSegmentByName("NoSuchSegment", NULL, NULL)); |
| 262 EXPECT_FALSE( |
| 263 actual_image->GetSectionByName("NoSuchSegment", "NoSuchSection", NULL)); |
| 264 |
| 265 // Make sure that there’s a __TEXT segment so that this can do a valid test of |
| 266 // a section that doesn’t exist within a segment that does. |
| 267 EXPECT_TRUE(actual_image->GetSegmentByName(SEG_TEXT, NULL, NULL)); |
| 268 EXPECT_FALSE(actual_image->GetSectionByName(SEG_TEXT, "NoSuchSection", NULL)); |
| 269 |
| 270 // Similarly, make sure that a section name that exists in one segment isn’t |
| 271 // accidentally found during a lookup for that section in a different segment. |
| 272 EXPECT_TRUE(actual_image->GetSectionByName(SEG_TEXT, SECT_TEXT, NULL)); |
| 273 EXPECT_FALSE( |
| 274 actual_image->GetSectionByName("NoSuchSegment", SECT_TEXT, NULL)); |
| 275 EXPECT_FALSE(actual_image->GetSectionByName(SEG_DATA, SECT_TEXT, NULL)); |
| 276 |
| 277 // The __LINKEDIT segment normally does exist but doesn’t have any sections. |
| 278 EXPECT_FALSE( |
| 279 actual_image->GetSectionByName(SEG_LINKEDIT, "NoSuchSection", NULL)); |
| 280 EXPECT_FALSE(actual_image->GetSectionByName(SEG_LINKEDIT, SECT_TEXT, NULL)); |
| 281 } |
| 282 |
| 283 // Verifies that |expect_image| is a vaild Mach-O header for the current system |
| 284 // by checking its |magic| and |cputype| fields. Then, verifies that the |
| 285 // information in |actual_image| matches that in |expect_image|. The |filetype| |
| 286 // field is examined, and actual_image->Address() is compared to |
| 287 // |expect_image_address|. Various other attributes of |actual_image| are |
| 288 // sanity-checked depending on the Mach-O file type. Finally, |
| 289 // ExpectSegmentCommands() is called to verify all that all of the segments |
| 290 // match; |test_section_index_bounds| is used as an argument to that function. |
| 291 void ExpectMachImage(const MachHeader* expect_image, |
| 292 mach_vm_address_t expect_image_address, |
| 293 const MachOImageReader* actual_image, |
| 294 bool test_section_index_bounds) { |
| 295 ASSERT_TRUE(expect_image); |
| 296 ASSERT_TRUE(actual_image); |
| 297 |
| 298 EXPECT_EQ(kMachMagic, expect_image->magic); |
| 299 EXPECT_EQ(kCPUType, expect_image->cputype); |
| 300 |
| 301 EXPECT_EQ(expect_image->filetype, actual_image->FileType()); |
| 302 EXPECT_EQ(expect_image_address, actual_image->Address()); |
| 303 |
| 304 mach_vm_address_t actual_text_segment_address; |
| 305 mach_vm_size_t actual_text_segment_size; |
| 306 const MachOImageSegmentReader* actual_text_segment = |
| 307 actual_image->GetSegmentByName( |
| 308 SEG_TEXT, &actual_text_segment_address, &actual_text_segment_size); |
| 309 ASSERT_TRUE(actual_text_segment); |
| 310 EXPECT_EQ(expect_image_address, actual_text_segment_address); |
| 311 EXPECT_EQ(actual_image->Size(), actual_text_segment_size); |
| 312 EXPECT_EQ(expect_image_address - actual_text_segment->vmaddr(), |
| 313 actual_image->Slide()); |
| 314 |
| 315 uint32_t file_type = actual_image->FileType(); |
| 316 EXPECT_TRUE(file_type == MH_EXECUTE || file_type == MH_DYLIB || |
| 317 file_type == MH_DYLINKER || file_type == MH_BUNDLE); |
| 318 |
| 319 if (file_type == MH_EXECUTE || file_type == MH_DYLINKER) { |
| 320 EXPECT_EQ("/usr/lib/dyld", actual_image->DylinkerName()); |
| 321 } |
| 322 |
| 323 // For these, just don’t crash or anything. |
| 324 if (file_type == MH_DYLIB) { |
| 325 actual_image->DylibVersion(); |
| 326 } |
| 327 actual_image->SourceVersion(); |
| 328 UUID uuid; |
| 329 actual_image->UUID(&uuid); |
| 330 |
| 331 ExpectSegmentCommands(expect_image, actual_image, test_section_index_bounds); |
| 332 } |
| 333 |
| 334 TEST(MachOImageReader, Self_MainExecutable) { |
| 335 ProcessReader process_reader; |
| 336 ASSERT_TRUE(process_reader.Initialize(mach_task_self())); |
| 337 |
| 338 const MachHeader* mh_execute_header = reinterpret_cast<MachHeader*>( |
| 339 dlsym(RTLD_MAIN_ONLY, "_mh_execute_header")); |
| 340 ASSERT_NE(static_cast<void*>(NULL), mh_execute_header); |
| 341 mach_vm_address_t mh_execute_header_address = |
| 342 reinterpret_cast<mach_vm_address_t>(mh_execute_header); |
| 343 |
| 344 MachOImageReader image_reader; |
| 345 ASSERT_TRUE(image_reader.Initialize( |
| 346 &process_reader, mh_execute_header_address, "mh_execute_header")); |
| 347 |
| 348 EXPECT_EQ(static_cast<uint32_t>(MH_EXECUTE), image_reader.FileType()); |
| 349 |
| 350 ExpectMachImage( |
| 351 mh_execute_header, mh_execute_header_address, &image_reader, true); |
| 352 } |
| 353 |
| 354 TEST(MachOImageReader, Self_DyldImages) { |
| 355 ProcessReader process_reader; |
| 356 ASSERT_TRUE(process_reader.Initialize(mach_task_self())); |
| 357 |
| 358 const struct dyld_all_image_infos* dyld_image_infos = |
| 359 _dyld_get_all_image_infos(); |
| 360 ASSERT_GE(dyld_image_infos->version, 1u); |
| 361 ASSERT_TRUE(dyld_image_infos->infoArray); |
| 362 |
| 363 for (uint32_t index = 0; index < dyld_image_infos->infoArrayCount; ++index) { |
| 364 const dyld_image_info* dyld_image = &dyld_image_infos->infoArray[index]; |
| 365 SCOPED_TRACE(base::StringPrintf( |
| 366 "index %u, image %s", index, dyld_image->imageFilePath)); |
| 367 |
| 368 // dyld_image_info::imageLoadAddress is poorly-declared: it’s declared as |
| 369 // const mach_header* in both 32-bit and 64-bit environments, but in the |
| 370 // 64-bit environment, it should be const mach_header_64*. |
| 371 const MachHeader* mach_header = |
| 372 reinterpret_cast<const MachHeader*>(dyld_image->imageLoadAddress); |
| 373 mach_vm_address_t image_address = |
| 374 reinterpret_cast<mach_vm_address_t>(mach_header); |
| 375 |
| 376 MachOImageReader image_reader; |
| 377 ASSERT_TRUE(image_reader.Initialize( |
| 378 &process_reader, image_address, dyld_image->imageFilePath)); |
| 379 |
| 380 uint32_t file_type = image_reader.FileType(); |
| 381 if (index == 0) { |
| 382 EXPECT_EQ(static_cast<uint32_t>(MH_EXECUTE), file_type); |
| 383 } else { |
| 384 EXPECT_TRUE(file_type == MH_DYLIB || file_type == MH_BUNDLE); |
| 385 } |
| 386 |
| 387 ExpectMachImage(mach_header, image_address, &image_reader, false); |
| 388 if (Test::HasFatalFailure()) { |
| 389 return; |
| 390 } |
| 391 } |
| 392 |
| 393 // Now that all of the modules have been verified, make sure that dyld itself |
| 394 // can be read properly too. |
| 395 if (dyld_image_infos->version >= 2) { |
| 396 SCOPED_TRACE("dyld"); |
| 397 |
| 398 // dyld_all_image_infos::dyldImageLoadAddress is poorly-declared too. |
| 399 const MachHeader* mach_header = reinterpret_cast<const MachHeader*>( |
| 400 dyld_image_infos->dyldImageLoadAddress); |
| 401 mach_vm_address_t image_address = |
| 402 reinterpret_cast<mach_vm_address_t>(mach_header); |
| 403 |
| 404 MachOImageReader image_reader; |
| 405 ASSERT_TRUE( |
| 406 image_reader.Initialize(&process_reader, image_address, "dyld")); |
| 407 |
| 408 EXPECT_EQ(static_cast<uint32_t>(MH_DYLINKER), image_reader.FileType()); |
| 409 |
| 410 ExpectMachImage(mach_header, image_address, &image_reader, false); |
| 411 if (Test::HasFatalFailure()) { |
| 412 return; |
| 413 } |
| 414 } |
| 415 |
| 416 // If dyld is new enough to record UUIDs, check the UUID of any module that |
| 417 // it says has one. Note that dyld doesn’t record UUIDs of anything that |
| 418 // loaded out of the shared cache, but it should at least have a UUID for the |
| 419 // main executable if it has one. |
| 420 if (dyld_image_infos->version >= 8 && dyld_image_infos->uuidArray) { |
| 421 for (uint32_t index = 0; |
| 422 index < dyld_image_infos->uuidArrayCount; |
| 423 ++index) { |
| 424 const dyld_uuid_info* dyld_image = &dyld_image_infos->uuidArray[index]; |
| 425 SCOPED_TRACE(base::StringPrintf("uuid index %u", index)); |
| 426 |
| 427 // dyld_uuid_info::imageLoadAddress is poorly-declared too. |
| 428 const MachHeader* mach_header = |
| 429 reinterpret_cast<const MachHeader*>(dyld_image->imageLoadAddress); |
| 430 mach_vm_address_t image_address = |
| 431 reinterpret_cast<mach_vm_address_t>(mach_header); |
| 432 |
| 433 MachOImageReader image_reader; |
| 434 ASSERT_TRUE( |
| 435 image_reader.Initialize(&process_reader, image_address, "uuid")); |
| 436 |
| 437 ExpectMachImage(mach_header, image_address, &image_reader, false); |
| 438 |
| 439 UUID expected_uuid; |
| 440 expected_uuid.InitializeFromBytes(dyld_image->imageUUID); |
| 441 UUID actual_uuid; |
| 442 image_reader.UUID(&actual_uuid); |
| 443 EXPECT_EQ(expected_uuid, actual_uuid); |
| 444 } |
| 445 } |
| 446 } |
| 447 |
| 448 } // namespace |
OLD | NEW |