| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 // sections that it claims to have, and set up a pointer to its first section | 152 // sections that it claims to have, and set up a pointer to its first section |
| 153 // structure. | 153 // structure. |
| 154 ASSERT_EQ(sizeof(*expect_segment) + expect_segment->nsects * sizeof(Section), | 154 ASSERT_EQ(sizeof(*expect_segment) + expect_segment->nsects * sizeof(Section), |
| 155 expect_segment->cmdsize); | 155 expect_segment->cmdsize); |
| 156 const Section* expect_sections = | 156 const Section* expect_sections = |
| 157 reinterpret_cast<const Section*>(&expect_segment[1]); | 157 reinterpret_cast<const Section*>(&expect_segment[1]); |
| 158 | 158 |
| 159 for (size_t index = 0; index < actual_segment->nsects(); ++index) { | 159 for (size_t index = 0; index < actual_segment->nsects(); ++index) { |
| 160 const Section* expect_section = &expect_sections[index]; | 160 const Section* expect_section = &expect_sections[index]; |
| 161 const process_types::section* actual_section = | 161 const process_types::section* actual_section = |
| 162 actual_segment->GetSectionAtIndex(index, NULL); | 162 actual_segment->GetSectionAtIndex(index, nullptr); |
| 163 ASSERT_NO_FATAL_FAILURE( | 163 ASSERT_NO_FATAL_FAILURE( |
| 164 ExpectSection(&expect_sections[index], actual_section)); | 164 ExpectSection(&expect_sections[index], actual_section)); |
| 165 | 165 |
| 166 // Make sure that the section is accessible by GetSectionByName as well. | 166 // Make sure that the section is accessible by GetSectionByName as well. |
| 167 std::string section_name = | 167 std::string section_name = |
| 168 MachOImageSegmentReader::SectionNameString(expect_section->sectname); | 168 MachOImageSegmentReader::SectionNameString(expect_section->sectname); |
| 169 const process_types::section* actual_section_by_name = | 169 const process_types::section* actual_section_by_name = |
| 170 actual_segment->GetSectionByName(section_name, NULL); | 170 actual_segment->GetSectionByName(section_name, nullptr); |
| 171 EXPECT_EQ(actual_section, actual_section_by_name); | 171 EXPECT_EQ(actual_section, actual_section_by_name); |
| 172 | 172 |
| 173 // Make sure that the section is accessible by the parent MachOImageReader’s | 173 // Make sure that the section is accessible by the parent MachOImageReader’s |
| 174 // GetSectionByName. | 174 // GetSectionByName. |
| 175 mach_vm_address_t actual_section_address; | 175 mach_vm_address_t actual_section_address; |
| 176 const process_types::section* actual_section_from_image_by_name = | 176 const process_types::section* actual_section_from_image_by_name = |
| 177 actual_image->GetSectionByName( | 177 actual_image->GetSectionByName( |
| 178 segment_name, section_name, &actual_section_address); | 178 segment_name, section_name, &actual_section_address); |
| 179 EXPECT_EQ(actual_section, actual_section_from_image_by_name); | 179 EXPECT_EQ(actual_section, actual_section_from_image_by_name); |
| 180 | 180 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 200 mach_vm_address_t actual_section_address_at_index; | 200 mach_vm_address_t actual_section_address_at_index; |
| 201 const process_types::section* actual_section_from_image_at_index = | 201 const process_types::section* actual_section_from_image_at_index = |
| 202 actual_image->GetSectionAtIndex(++(*section_index), | 202 actual_image->GetSectionAtIndex(++(*section_index), |
| 203 &containing_segment, | 203 &containing_segment, |
| 204 &actual_section_address_at_index); | 204 &actual_section_address_at_index); |
| 205 EXPECT_EQ(actual_section, actual_section_from_image_at_index); | 205 EXPECT_EQ(actual_section, actual_section_from_image_at_index); |
| 206 EXPECT_EQ(actual_segment, containing_segment); | 206 EXPECT_EQ(actual_segment, containing_segment); |
| 207 EXPECT_EQ(actual_section_address, actual_section_address_at_index); | 207 EXPECT_EQ(actual_section_address, actual_section_address_at_index); |
| 208 } | 208 } |
| 209 | 209 |
| 210 EXPECT_EQ(NULL, actual_segment->GetSectionByName("NoSuchSection", NULL)); | 210 EXPECT_EQ(nullptr, |
| 211 actual_segment->GetSectionByName("NoSuchSection", nullptr)); |
| 211 } | 212 } |
| 212 | 213 |
| 213 // Walks through the load commands of |expect_image|, finding all of the | 214 // Walks through the load commands of |expect_image|, finding all of the |
| 214 // expected segment commands. For each expected segment command, calls | 215 // expected segment commands. For each expected segment command, calls |
| 215 // actual_image->GetSegmentByName() to obtain an actual segment command, and | 216 // actual_image->GetSegmentByName() to obtain an actual segment command, and |
| 216 // calls ExpectSegmentCommand() to compare the expected and actual segments. A | 217 // calls ExpectSegmentCommand() to compare the expected and actual segments. A |
| 217 // series of by-name lookups is also performed on the segment to ensure that it | 218 // series of by-name lookups is also performed on the segment to ensure that it |
| 218 // behaves correctly when attempting to look up segment and section names that | 219 // behaves correctly when attempting to look up segment and section names that |
| 219 // are not present. |test_section_indices| should be true to test | 220 // are not present. |test_section_indices| should be true to test |
| 220 // MachOImageReader::GetSectionAtIndex() using out-of-range section indices. | 221 // MachOImageReader::GetSectionAtIndex() using out-of-range section indices. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 251 actual_image, | 252 actual_image, |
| 252 §ion_index)); | 253 §ion_index)); |
| 253 } | 254 } |
| 254 position += command->cmdsize; | 255 position += command->cmdsize; |
| 255 } | 256 } |
| 256 EXPECT_EQ(expect_image->sizeofcmds, position); | 257 EXPECT_EQ(expect_image->sizeofcmds, position); |
| 257 | 258 |
| 258 if (test_section_index_bounds) { | 259 if (test_section_index_bounds) { |
| 259 // GetSectionAtIndex uses a 1-based index. Make sure that the range is | 260 // GetSectionAtIndex uses a 1-based index. Make sure that the range is |
| 260 // correct. | 261 // correct. |
| 261 EXPECT_EQ(NULL, actual_image->GetSectionAtIndex(0, NULL, NULL)); | 262 EXPECT_EQ(nullptr, actual_image->GetSectionAtIndex(0, nullptr, nullptr)); |
| 262 EXPECT_EQ(NULL, | 263 EXPECT_EQ( |
| 263 actual_image->GetSectionAtIndex(section_index + 1, NULL, NULL)); | 264 nullptr, |
| 265 actual_image->GetSectionAtIndex(section_index + 1, nullptr, nullptr)); |
| 264 } | 266 } |
| 265 | 267 |
| 266 // Make sure that by-name lookups for names that don’t exist work properly: | 268 // Make sure that by-name lookups for names that don’t exist work properly: |
| 267 // they should return NULL. | 269 // they should return nullptr. |
| 268 EXPECT_FALSE(actual_image->GetSegmentByName("NoSuchSegment")); | 270 EXPECT_FALSE(actual_image->GetSegmentByName("NoSuchSegment")); |
| 269 EXPECT_FALSE( | 271 EXPECT_FALSE(actual_image->GetSectionByName( |
| 270 actual_image->GetSectionByName("NoSuchSegment", "NoSuchSection", NULL)); | 272 "NoSuchSegment", "NoSuchSection", nullptr)); |
| 271 | 273 |
| 272 // Make sure that there’s a __TEXT segment so that this can do a valid test of | 274 // Make sure that there’s a __TEXT segment so that this can do a valid test of |
| 273 // a section that doesn’t exist within a segment that does. | 275 // a section that doesn’t exist within a segment that does. |
| 274 EXPECT_TRUE(actual_image->GetSegmentByName(SEG_TEXT)); | 276 EXPECT_TRUE(actual_image->GetSegmentByName(SEG_TEXT)); |
| 275 EXPECT_FALSE(actual_image->GetSectionByName(SEG_TEXT, "NoSuchSection", NULL)); | 277 EXPECT_FALSE( |
| 278 actual_image->GetSectionByName(SEG_TEXT, "NoSuchSection", nullptr)); |
| 276 | 279 |
| 277 // Similarly, make sure that a section name that exists in one segment isn’t | 280 // Similarly, make sure that a section name that exists in one segment isn’t |
| 278 // accidentally found during a lookup for that section in a different segment. | 281 // accidentally found during a lookup for that section in a different segment. |
| 279 // | 282 // |
| 280 // If the image has no sections (unexpected), then any section lookup should | 283 // If the image has no sections (unexpected), then any section lookup should |
| 281 // fail, and these initial values of test_segment and test_section are fine | 284 // fail, and these initial values of test_segment and test_section are fine |
| 282 // for the EXPECT_FALSE checks on GetSectionByName() below. | 285 // for the EXPECT_FALSE checks on GetSectionByName() below. |
| 283 std::string test_segment = SEG_DATA; | 286 std::string test_segment = SEG_DATA; |
| 284 std::string test_section = SECT_TEXT; | 287 std::string test_section = SECT_TEXT; |
| 285 | 288 |
| 286 const process_types::section* section = | 289 const process_types::section* section = |
| 287 actual_image->GetSectionAtIndex(1, NULL, NULL); | 290 actual_image->GetSectionAtIndex(1, nullptr, nullptr); |
| 288 if (section) { | 291 if (section) { |
| 289 // Use the name of the first section in the image as the section that | 292 // Use the name of the first section in the image as the section that |
| 290 // shouldn’t appear in a different segment. If the first section is in the | 293 // shouldn’t appear in a different segment. If the first section is in the |
| 291 // __TEXT segment (as it is normally), then a section by the same name | 294 // __TEXT segment (as it is normally), then a section by the same name |
| 292 // wouldn’t be expected in the __DATA segment. But if the first section is | 295 // wouldn’t be expected in the __DATA segment. But if the first section is |
| 293 // in any other segment, then it wouldn’t be expected in the __TEXT segment. | 296 // in any other segment, then it wouldn’t be expected in the __TEXT segment. |
| 294 if (MachOImageSegmentReader::SegmentNameString(section->segname) == | 297 if (MachOImageSegmentReader::SegmentNameString(section->segname) == |
| 295 SEG_TEXT) { | 298 SEG_TEXT) { |
| 296 test_segment = SEG_DATA; | 299 test_segment = SEG_DATA; |
| 297 } else { | 300 } else { |
| 298 test_segment = SEG_TEXT; | 301 test_segment = SEG_TEXT; |
| 299 } | 302 } |
| 300 test_section = | 303 test_section = |
| 301 MachOImageSegmentReader::SectionNameString(section->sectname); | 304 MachOImageSegmentReader::SectionNameString(section->sectname); |
| 302 | 305 |
| 303 // It should be possible to look up the first section by name. | 306 // It should be possible to look up the first section by name. |
| 304 EXPECT_EQ(section, | 307 EXPECT_EQ(section, |
| 305 actual_image->GetSectionByName( | 308 actual_image->GetSectionByName( |
| 306 section->segname, section->sectname, NULL)); | 309 section->segname, section->sectname, nullptr)); |
| 307 } | 310 } |
| 308 EXPECT_FALSE( | 311 EXPECT_FALSE( |
| 309 actual_image->GetSectionByName("NoSuchSegment", test_section, NULL)); | 312 actual_image->GetSectionByName("NoSuchSegment", test_section, nullptr)); |
| 310 EXPECT_FALSE( | 313 EXPECT_FALSE( |
| 311 actual_image->GetSectionByName(test_segment, test_section, NULL)); | 314 actual_image->GetSectionByName(test_segment, test_section, nullptr)); |
| 312 | 315 |
| 313 // The __LINKEDIT segment normally does exist but doesn’t have any sections. | 316 // The __LINKEDIT segment normally does exist but doesn’t have any sections. |
| 314 EXPECT_FALSE( | 317 EXPECT_FALSE( |
| 315 actual_image->GetSectionByName(SEG_LINKEDIT, "NoSuchSection", NULL)); | 318 actual_image->GetSectionByName(SEG_LINKEDIT, "NoSuchSection", nullptr)); |
| 316 EXPECT_FALSE(actual_image->GetSectionByName(SEG_LINKEDIT, SECT_TEXT, NULL)); | 319 EXPECT_FALSE( |
| 320 actual_image->GetSectionByName(SEG_LINKEDIT, SECT_TEXT, nullptr)); |
| 317 } | 321 } |
| 318 | 322 |
| 319 // In some cases, the expected slide value for an image is unknown, because no | 323 // In some cases, the expected slide value for an image is unknown, because no |
| 320 // reasonable API to return it is provided. When this happens, use kSlideUnknown | 324 // reasonable API to return it is provided. When this happens, use kSlideUnknown |
| 321 // to avoid checking the actual slide value against anything. | 325 // to avoid checking the actual slide value against anything. |
| 322 const mach_vm_size_t kSlideUnknown = std::numeric_limits<mach_vm_size_t>::max(); | 326 const mach_vm_size_t kSlideUnknown = std::numeric_limits<mach_vm_size_t>::max(); |
| 323 | 327 |
| 324 // Verifies that |expect_image| is a vaild Mach-O header for the current system | 328 // Verifies that |expect_image| is a vaild Mach-O header for the current system |
| 325 // by checking its |magic| and |cputype| fields. Then, verifies that the | 329 // by checking its |magic| and |cputype| fields. Then, verifies that the |
| 326 // information in |actual_image| matches that in |expect_image|. The |filetype| | 330 // information in |actual_image| matches that in |expect_image|. The |filetype| |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 // values in |actual_image|. ExpectSymbol() is used to verify the actual symbol. | 440 // values in |actual_image|. ExpectSymbol() is used to verify the actual symbol. |
| 437 void ExpectSymbolTable(const MachHeader* expect_image, | 441 void ExpectSymbolTable(const MachHeader* expect_image, |
| 438 const MachOImageReader* actual_image) { | 442 const MachOImageReader* actual_image) { |
| 439 // This intentionally consults only LC_SYMTAB and not LC_DYSYMTAB so that it | 443 // This intentionally consults only LC_SYMTAB and not LC_DYSYMTAB so that it |
| 440 // can look at the larger set of all symbols. The actual implementation being | 444 // can look at the larger set of all symbols. The actual implementation being |
| 441 // tested is free to consult LC_DYSYMTAB, but that’s considered an | 445 // tested is free to consult LC_DYSYMTAB, but that’s considered an |
| 442 // optimization. It’s not necessary for the test, and it’s better for the test | 446 // optimization. It’s not necessary for the test, and it’s better for the test |
| 443 // to expose bugs in that optimization rather than duplicate them. | 447 // to expose bugs in that optimization rather than duplicate them. |
| 444 const char* commands_base = reinterpret_cast<const char*>(&expect_image[1]); | 448 const char* commands_base = reinterpret_cast<const char*>(&expect_image[1]); |
| 445 uint32_t position = 0; | 449 uint32_t position = 0; |
| 446 const symtab_command* symtab = NULL; | 450 const symtab_command* symtab = nullptr; |
| 447 const SegmentCommand* linkedit = NULL; | 451 const SegmentCommand* linkedit = nullptr; |
| 448 for (uint32_t index = 0; index < expect_image->ncmds; ++index) { | 452 for (uint32_t index = 0; index < expect_image->ncmds; ++index) { |
| 449 ASSERT_LT(position, expect_image->sizeofcmds); | 453 ASSERT_LT(position, expect_image->sizeofcmds); |
| 450 const load_command* command = | 454 const load_command* command = |
| 451 reinterpret_cast<const load_command*>(&commands_base[position]); | 455 reinterpret_cast<const load_command*>(&commands_base[position]); |
| 452 ASSERT_LE(position + command->cmdsize, expect_image->sizeofcmds); | 456 ASSERT_LE(position + command->cmdsize, expect_image->sizeofcmds); |
| 453 if (command->cmd == LC_SYMTAB) { | 457 if (command->cmd == LC_SYMTAB) { |
| 454 ASSERT_FALSE(symtab); | 458 ASSERT_FALSE(symtab); |
| 455 ASSERT_EQ(sizeof(symtab_command), command->cmdsize); | 459 ASSERT_EQ(sizeof(symtab_command), command->cmdsize); |
| 456 symtab = reinterpret_cast<const symtab_command*>(command); | 460 symtab = reinterpret_cast<const symtab_command*>(command); |
| 457 } else if (command->cmd == kSegmentCommand) { | 461 } else if (command->cmd == kSegmentCommand) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 EXPECT_FALSE( | 495 EXPECT_FALSE( |
| 492 actual_image->LookUpExternalDefinedSymbol("_NoSuchSymbolName", &ignore)); | 496 actual_image->LookUpExternalDefinedSymbol("_NoSuchSymbolName", &ignore)); |
| 493 } | 497 } |
| 494 | 498 |
| 495 TEST(MachOImageReader, Self_MainExecutable) { | 499 TEST(MachOImageReader, Self_MainExecutable) { |
| 496 ProcessReader process_reader; | 500 ProcessReader process_reader; |
| 497 ASSERT_TRUE(process_reader.Initialize(mach_task_self())); | 501 ASSERT_TRUE(process_reader.Initialize(mach_task_self())); |
| 498 | 502 |
| 499 const MachHeader* mh_execute_header = | 503 const MachHeader* mh_execute_header = |
| 500 reinterpret_cast<MachHeader*>(dlsym(RTLD_MAIN_ONLY, MH_EXECUTE_SYM)); | 504 reinterpret_cast<MachHeader*>(dlsym(RTLD_MAIN_ONLY, MH_EXECUTE_SYM)); |
| 501 ASSERT_NE(static_cast<void*>(NULL), mh_execute_header); | 505 ASSERT_NE(nullptr, mh_execute_header); |
| 502 mach_vm_address_t mh_execute_header_address = | 506 mach_vm_address_t mh_execute_header_address = |
| 503 reinterpret_cast<mach_vm_address_t>(mh_execute_header); | 507 reinterpret_cast<mach_vm_address_t>(mh_execute_header); |
| 504 | 508 |
| 505 MachOImageReader image_reader; | 509 MachOImageReader image_reader; |
| 506 ASSERT_TRUE(image_reader.Initialize( | 510 ASSERT_TRUE(image_reader.Initialize( |
| 507 &process_reader, mh_execute_header_address, "executable")); | 511 &process_reader, mh_execute_header_address, "executable")); |
| 508 | 512 |
| 509 EXPECT_EQ(static_cast<uint32_t>(MH_EXECUTE), image_reader.FileType()); | 513 EXPECT_EQ(static_cast<uint32_t>(MH_EXECUTE), image_reader.FileType()); |
| 510 | 514 |
| 511 // The main executable has image index 0. | 515 // The main executable has image index 0. |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 image_reader.UUID(&actual_uuid); | 631 image_reader.UUID(&actual_uuid); |
| 628 EXPECT_EQ(expected_uuid, actual_uuid); | 632 EXPECT_EQ(expected_uuid, actual_uuid); |
| 629 } | 633 } |
| 630 } | 634 } |
| 631 #endif | 635 #endif |
| 632 } | 636 } |
| 633 | 637 |
| 634 } // namespace | 638 } // namespace |
| 635 } // namespace test | 639 } // namespace test |
| 636 } // namespace crashpad | 640 } // namespace crashpad |
| OLD | NEW |