Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: util/mac/mach_o_image_reader_test.cc

Issue 666483002: Create snapshot/mac and move some files from snapshot and util to there (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad/+/master
Patch Set: Move process_reader, process_types, and mach_o_image*_reader from util/mac to snapshot/mac Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/mac/mach_o_image_reader.cc ('k') | util/mac/mach_o_image_segment_reader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <AvailabilityMacros.h>
18 #include <dlfcn.h>
19 #include <mach-o/dyld.h>
20 #include <mach-o/dyld_images.h>
21 #include <mach-o/getsect.h>
22 #include <mach-o/ldsyms.h>
23 #include <mach-o/loader.h>
24 #include <mach-o/nlist.h>
25 #include <stdint.h>
26
27 #include "base/strings/stringprintf.h"
28 #include "build/build_config.h"
29 #include "gtest/gtest.h"
30 #include "util/mac/mach_o_image_segment_reader.h"
31 #include "util/mac/process_reader.h"
32 #include "util/mac/process_types.h"
33 #include "util/misc/uuid.h"
34 #include "util/test/mac/dyld.h"
35
36 // This file is responsible for testing MachOImageReader,
37 // MachOImageSegmentReader, and MachOImageSymbolTableReader.
38
39 namespace crashpad {
40 namespace test {
41 namespace {
42
43 // Native types and constants, in cases where the 32-bit and 64-bit versions
44 // are different.
45 #if defined(ARCH_CPU_64_BITS)
46 typedef mach_header_64 MachHeader;
47 const uint32_t kMachMagic = MH_MAGIC_64;
48 typedef segment_command_64 SegmentCommand;
49 const uint32_t kSegmentCommand = LC_SEGMENT_64;
50 typedef section_64 Section;
51 typedef nlist_64 Nlist;
52 #else
53 typedef mach_header MachHeader;
54 const uint32_t kMachMagic = MH_MAGIC;
55 typedef segment_command SegmentCommand;
56 const uint32_t kSegmentCommand = LC_SEGMENT;
57 typedef section Section;
58
59 // This needs to be called “struct nlist” because “nlist” without the struct
60 // refers to the nlist() function.
61 typedef struct nlist Nlist;
62 #endif
63
64 #if defined(ARCH_CPU_X86_64)
65 const int kCPUType = CPU_TYPE_X86_64;
66 #elif defined(ARCH_CPU_X86)
67 const int kCPUType = CPU_TYPE_X86;
68 #endif
69
70 // Verifies that |expect_section| and |actual_section| agree.
71 void ExpectSection(const Section* expect_section,
72 const process_types::section* actual_section) {
73 ASSERT_TRUE(expect_section);
74 ASSERT_TRUE(actual_section);
75
76 EXPECT_EQ(
77 MachOImageSegmentReader::SectionNameString(expect_section->sectname),
78 MachOImageSegmentReader::SectionNameString(actual_section->sectname));
79 EXPECT_EQ(
80 MachOImageSegmentReader::SegmentNameString(expect_section->segname),
81 MachOImageSegmentReader::SegmentNameString(actual_section->segname));
82 EXPECT_EQ(expect_section->addr, actual_section->addr);
83 EXPECT_EQ(expect_section->size, actual_section->size);
84 EXPECT_EQ(expect_section->offset, actual_section->offset);
85 EXPECT_EQ(expect_section->align, actual_section->align);
86 EXPECT_EQ(expect_section->reloff, actual_section->reloff);
87 EXPECT_EQ(expect_section->nreloc, actual_section->nreloc);
88 EXPECT_EQ(expect_section->flags, actual_section->flags);
89 EXPECT_EQ(expect_section->reserved1, actual_section->reserved1);
90 EXPECT_EQ(expect_section->reserved2, actual_section->reserved2);
91 }
92
93 // Verifies that |expect_segment| is a valid Mach-O segment load command for the
94 // current system by checking its |cmd| field. Then, verifies that the
95 // information in |actual_segment| matches that in |expect_segment|. The
96 // |segname|, |vmaddr|, |vmsize|, and |fileoff| fields are examined. Each
97 // section within the segment is also examined by calling ExpectSection().
98 // Access to each section via both MachOImageSegmentReader::GetSectionByName()
99 // and MachOImageReader::GetSectionByName() is verified, expecting that each
100 // call produces the same section. Segment and section data addresses are
101 // verified against data obtained by calling getsegmentdata() and
102 // getsectiondata(). The segment is checked to make sure that it behaves
103 // correctly when attempting to look up a nonexistent section by name.
104 // |section_index| is used to track the last-used section index in an image on
105 // entry, and is reset to the last-used section index on return after the
106 // sections are processed. This is used to test that
107 // MachOImageReader::GetSectionAtIndex() returns the correct result.
108 void ExpectSegmentCommand(const SegmentCommand* expect_segment,
109 const MachHeader* expect_image,
110 const MachOImageSegmentReader* actual_segment,
111 const MachOImageReader* actual_image,
112 size_t* section_index) {
113 ASSERT_TRUE(expect_segment);
114 ASSERT_TRUE(actual_segment);
115
116 EXPECT_EQ(kSegmentCommand, expect_segment->cmd);
117
118 std::string segment_name = actual_segment->Name();
119 EXPECT_EQ(MachOImageSegmentReader::SegmentNameString(expect_segment->segname),
120 segment_name);
121 EXPECT_EQ(expect_segment->vmaddr, actual_segment->vmaddr());
122 EXPECT_EQ(expect_segment->vmsize, actual_segment->vmsize());
123 EXPECT_EQ(expect_segment->fileoff, actual_segment->fileoff());
124
125 if (actual_segment->SegmentSlides()) {
126 EXPECT_EQ(actual_segment->Address(),
127 actual_segment->vmaddr() + actual_image->Slide());
128
129 unsigned long expect_segment_size;
130 const uint8_t* expect_segment_data = getsegmentdata(
131 expect_image, segment_name.c_str(), &expect_segment_size);
132 mach_vm_address_t expect_segment_address =
133 reinterpret_cast<mach_vm_address_t>(expect_segment_data);
134 EXPECT_EQ(expect_segment_address, actual_segment->Address());
135 EXPECT_EQ(expect_segment_size, actual_segment->vmsize());
136 EXPECT_EQ(actual_segment->vmsize(), actual_segment->Size());
137 } else {
138 // getsegmentdata() doesn’t return appropriate data for the __PAGEZERO
139 // segment because getsegmentdata() always adjusts for slide, but the
140 // __PAGEZERO segment never slides, it just grows. Skip the getsegmentdata()
141 // check for that segment according to the same rules that the kernel uses
142 // to identify __PAGEZERO. See 10.9.4 xnu-2422.110.17/bsd/kern/mach_loader.c
143 // load_segment().
144 EXPECT_EQ(actual_segment->Address(), actual_segment->vmaddr());
145 EXPECT_EQ(actual_segment->vmsize() + actual_image->Slide(),
146 actual_segment->Size());
147 }
148
149 ASSERT_EQ(expect_segment->nsects, actual_segment->nsects());
150
151 // Make sure that the expected load command is big enough for the number of
152 // sections that it claims to have, and set up a pointer to its first section
153 // structure.
154 ASSERT_EQ(sizeof(*expect_segment) + expect_segment->nsects * sizeof(Section),
155 expect_segment->cmdsize);
156 const Section* expect_sections =
157 reinterpret_cast<const Section*>(&expect_segment[1]);
158
159 for (size_t index = 0; index < actual_segment->nsects(); ++index) {
160 const Section* expect_section = &expect_sections[index];
161 const process_types::section* actual_section =
162 actual_segment->GetSectionAtIndex(index, nullptr);
163 ASSERT_NO_FATAL_FAILURE(
164 ExpectSection(&expect_sections[index], actual_section));
165
166 // Make sure that the section is accessible by GetSectionByName as well.
167 std::string section_name =
168 MachOImageSegmentReader::SectionNameString(expect_section->sectname);
169 const process_types::section* actual_section_by_name =
170 actual_segment->GetSectionByName(section_name, nullptr);
171 EXPECT_EQ(actual_section, actual_section_by_name);
172
173 // Make sure that the section is accessible by the parent MachOImageReader’s
174 // GetSectionByName.
175 mach_vm_address_t actual_section_address;
176 const process_types::section* actual_section_from_image_by_name =
177 actual_image->GetSectionByName(
178 segment_name, section_name, &actual_section_address);
179 EXPECT_EQ(actual_section, actual_section_from_image_by_name);
180
181 if (actual_segment->SegmentSlides()) {
182 EXPECT_EQ(actual_section_address,
183 actual_section->addr + actual_image->Slide());
184
185 unsigned long expect_section_size;
186 const uint8_t* expect_section_data = getsectiondata(expect_image,
187 segment_name.c_str(),
188 section_name.c_str(),
189 &expect_section_size);
190 mach_vm_address_t expect_section_address =
191 reinterpret_cast<mach_vm_address_t>(expect_section_data);
192 EXPECT_EQ(expect_section_address, actual_section_address);
193 EXPECT_EQ(expect_section_size, actual_section->size);
194 } else {
195 EXPECT_EQ(actual_section_address, actual_section->addr);
196 }
197
198 // Test the parent MachOImageReader’s GetSectionAtIndex as well.
199 const MachOImageSegmentReader* containing_segment;
200 mach_vm_address_t actual_section_address_at_index;
201 const process_types::section* actual_section_from_image_at_index =
202 actual_image->GetSectionAtIndex(++(*section_index),
203 &containing_segment,
204 &actual_section_address_at_index);
205 EXPECT_EQ(actual_section, actual_section_from_image_at_index);
206 EXPECT_EQ(actual_segment, containing_segment);
207 EXPECT_EQ(actual_section_address, actual_section_address_at_index);
208 }
209
210 EXPECT_EQ(nullptr,
211 actual_segment->GetSectionByName("NoSuchSection", nullptr));
212 }
213
214 // Walks through the load commands of |expect_image|, finding all of the
215 // expected segment commands. For each expected segment command, calls
216 // actual_image->GetSegmentByName() to obtain an actual segment command, and
217 // calls ExpectSegmentCommand() to compare the expected and actual segments. A
218 // series of by-name lookups is also performed on the segment to ensure that it
219 // behaves correctly when attempting to look up segment and section names that
220 // are not present. |test_section_indices| should be true to test
221 // MachOImageReader::GetSectionAtIndex() using out-of-range section indices.
222 // This should be tested for at least one module, but it’s very noisy in terms
223 // of logging output, so this knob is provided to suppress this portion of the
224 // test when looping over all modules.
225 void ExpectSegmentCommands(const MachHeader* expect_image,
226 const MachOImageReader* actual_image,
227 bool test_section_index_bounds) {
228 ASSERT_TRUE(expect_image);
229 ASSERT_TRUE(actual_image);
230
231 // &expect_image[1] points right past the end of the mach_header[_64], to the
232 // start of the load commands.
233 const char* commands_base = reinterpret_cast<const char*>(&expect_image[1]);
234 uint32_t position = 0;
235 size_t section_index = 0;
236 for (uint32_t index = 0; index < expect_image->ncmds; ++index) {
237 ASSERT_LT(position, expect_image->sizeofcmds);
238 const load_command* command =
239 reinterpret_cast<const load_command*>(&commands_base[position]);
240 ASSERT_LE(position + command->cmdsize, expect_image->sizeofcmds);
241 if (command->cmd == kSegmentCommand) {
242 ASSERT_GE(command->cmdsize, sizeof(SegmentCommand));
243 const SegmentCommand* expect_segment =
244 reinterpret_cast<const SegmentCommand*>(command);
245 std::string segment_name =
246 MachOImageSegmentReader::SegmentNameString(expect_segment->segname);
247 const MachOImageSegmentReader* actual_segment =
248 actual_image->GetSegmentByName(segment_name);
249 ASSERT_NO_FATAL_FAILURE(ExpectSegmentCommand(expect_segment,
250 expect_image,
251 actual_segment,
252 actual_image,
253 &section_index));
254 }
255 position += command->cmdsize;
256 }
257 EXPECT_EQ(expect_image->sizeofcmds, position);
258
259 if (test_section_index_bounds) {
260 // GetSectionAtIndex uses a 1-based index. Make sure that the range is
261 // correct.
262 EXPECT_EQ(nullptr, actual_image->GetSectionAtIndex(0, nullptr, nullptr));
263 EXPECT_EQ(
264 nullptr,
265 actual_image->GetSectionAtIndex(section_index + 1, nullptr, nullptr));
266 }
267
268 // Make sure that by-name lookups for names that don’t exist work properly:
269 // they should return nullptr.
270 EXPECT_FALSE(actual_image->GetSegmentByName("NoSuchSegment"));
271 EXPECT_FALSE(actual_image->GetSectionByName(
272 "NoSuchSegment", "NoSuchSection", nullptr));
273
274 // Make sure that there’s a __TEXT segment so that this can do a valid test of
275 // a section that doesn’t exist within a segment that does.
276 EXPECT_TRUE(actual_image->GetSegmentByName(SEG_TEXT));
277 EXPECT_FALSE(
278 actual_image->GetSectionByName(SEG_TEXT, "NoSuchSection", nullptr));
279
280 // Similarly, make sure that a section name that exists in one segment isn’t
281 // accidentally found during a lookup for that section in a different segment.
282 //
283 // If the image has no sections (unexpected), then any section lookup should
284 // fail, and these initial values of test_segment and test_section are fine
285 // for the EXPECT_FALSE checks on GetSectionByName() below.
286 std::string test_segment = SEG_DATA;
287 std::string test_section = SECT_TEXT;
288
289 const process_types::section* section =
290 actual_image->GetSectionAtIndex(1, nullptr, nullptr);
291 if (section) {
292 // Use the name of the first section in the image as the section that
293 // shouldn’t appear in a different segment. If the first section is in the
294 // __TEXT segment (as it is normally), then a section by the same name
295 // wouldn’t be expected in the __DATA segment. But if the first section is
296 // in any other segment, then it wouldn’t be expected in the __TEXT segment.
297 if (MachOImageSegmentReader::SegmentNameString(section->segname) ==
298 SEG_TEXT) {
299 test_segment = SEG_DATA;
300 } else {
301 test_segment = SEG_TEXT;
302 }
303 test_section =
304 MachOImageSegmentReader::SectionNameString(section->sectname);
305
306 // It should be possible to look up the first section by name.
307 EXPECT_EQ(section,
308 actual_image->GetSectionByName(
309 section->segname, section->sectname, nullptr));
310 }
311 EXPECT_FALSE(
312 actual_image->GetSectionByName("NoSuchSegment", test_section, nullptr));
313 EXPECT_FALSE(
314 actual_image->GetSectionByName(test_segment, test_section, nullptr));
315
316 // The __LINKEDIT segment normally does exist but doesn’t have any sections.
317 EXPECT_FALSE(
318 actual_image->GetSectionByName(SEG_LINKEDIT, "NoSuchSection", nullptr));
319 EXPECT_FALSE(
320 actual_image->GetSectionByName(SEG_LINKEDIT, SECT_TEXT, nullptr));
321 }
322
323 // In some cases, the expected slide value for an image is unknown, because no
324 // reasonable API to return it is provided. When this happens, use kSlideUnknown
325 // to avoid checking the actual slide value against anything.
326 const mach_vm_size_t kSlideUnknown = std::numeric_limits<mach_vm_size_t>::max();
327
328 // Verifies that |expect_image| is a vaild Mach-O header for the current system
329 // by checking its |magic| and |cputype| fields. Then, verifies that the
330 // information in |actual_image| matches that in |expect_image|. The |filetype|
331 // field is examined, actual_image->Address() is compared to
332 // |expect_image_address|, and actual_image->Slide() is compared to
333 // |expect_image_slide|, unless |expect_image_slide| is kSlideUnknown. Various
334 // other attributes of |actual_image| are sanity-checked depending on the Mach-O
335 // file type. Finally, ExpectSegmentCommands() is called to verify all that all
336 // of the segments match; |test_section_index_bounds| is used as an argument to
337 // that function.
338 void ExpectMachImage(const MachHeader* expect_image,
339 mach_vm_address_t expect_image_address,
340 mach_vm_size_t expect_image_slide,
341 const MachOImageReader* actual_image,
342 bool test_section_index_bounds) {
343 ASSERT_TRUE(expect_image);
344 ASSERT_TRUE(actual_image);
345
346 EXPECT_EQ(kMachMagic, expect_image->magic);
347 EXPECT_EQ(kCPUType, expect_image->cputype);
348
349 EXPECT_EQ(expect_image->filetype, actual_image->FileType());
350 EXPECT_EQ(expect_image_address, actual_image->Address());
351 if (expect_image_slide != kSlideUnknown) {
352 EXPECT_EQ(expect_image_slide, actual_image->Slide());
353 }
354
355 const MachOImageSegmentReader* actual_text_segment =
356 actual_image->GetSegmentByName(SEG_TEXT);
357 ASSERT_TRUE(actual_text_segment);
358 EXPECT_EQ(expect_image_address, actual_text_segment->Address());
359 EXPECT_EQ(actual_image->Size(), actual_text_segment->Size());
360 EXPECT_EQ(expect_image_address - actual_text_segment->vmaddr(),
361 actual_image->Slide());
362
363 uint32_t file_type = actual_image->FileType();
364 EXPECT_TRUE(file_type == MH_EXECUTE || file_type == MH_DYLIB ||
365 file_type == MH_DYLINKER || file_type == MH_BUNDLE);
366
367 if (file_type == MH_EXECUTE || file_type == MH_DYLINKER) {
368 EXPECT_EQ("/usr/lib/dyld", actual_image->DylinkerName());
369 }
370
371 // For these, just don’t crash or anything.
372 if (file_type == MH_DYLIB) {
373 actual_image->DylibVersion();
374 }
375 actual_image->SourceVersion();
376 UUID uuid;
377 actual_image->UUID(&uuid);
378
379 ASSERT_NO_FATAL_FAILURE(ExpectSegmentCommands(
380 expect_image, actual_image, test_section_index_bounds));
381 }
382
383 // Verifies the symbol whose Nlist structure is |entry| and whose name is |name|
384 // matches the value of a symbol by the same name looked up in |actual_image|.
385 // MachOImageReader::LookUpExternalDefinedSymbol() is used for this purpose.
386 // Only external defined symbols are considered, other types of symbols are
387 // excluded because LookUpExternalDefinedSymbol() only deals with external
388 // defined symbols.
389 void ExpectSymbol(const Nlist* entry,
390 const char* name,
391 const MachOImageReader* actual_image) {
392 SCOPED_TRACE(name);
393
394 uint32_t entry_type = entry->n_type & N_TYPE;
395 if ((entry->n_type & N_STAB) == 0 && (entry->n_type & N_PEXT) == 0 &&
396 entry_type != N_UNDF && entry_type != N_PBUD &&
397 (entry->n_type & N_EXT) == 1) {
398 // Note that this catches more symbols than MachOImageSymbolTableReader
399 // does. This test looks for all external defined symbols, but the
400 // implementation excludes indirect (N_INDR) symbols. This is intentional,
401 // because indirect symbols are currently not seen in the wild, but if they
402 // begin to be used more widely, this test is expected to catch them so that
403 // a decision can be made regarding whether support ought to be implemented.
404 mach_vm_address_t actual_address;
405 ASSERT_TRUE(
406 actual_image->LookUpExternalDefinedSymbol(name, &actual_address));
407
408 // Since the nlist interface was used to read the symbol, use it to compute
409 // the symbol address too. This isn’t perfect, and it should be possible in
410 // theory to use dlsym() to get the expected address of a symbol. In
411 // practice, dlsym() is difficult to use when only a MachHeader* is
412 // available as in this function, as opposed to a void* opaque handle. It is
413 // possible to get a void* handle by using dladdr() to find the file name
414 // corresponding to the MachHeader*, and using dlopen() again on that name,
415 // assuming it hasn’t changed on disk since being loaded. However, even with
416 // that being done, dlsym() can only deal with symbols whose names begin
417 // with an underscore (and requires that the leading underscore be trimmed).
418 // dlsym() will also return different addresses for symbols that are
419 // resolved via symbol resolver.
420 mach_vm_address_t expect_address = entry->n_value;
421 if (entry_type == N_SECT) {
422 EXPECT_GE(entry->n_sect, 1u);
423 expect_address += actual_image->Slide();
424 } else {
425 EXPECT_EQ(NO_SECT, entry->n_sect);
426 }
427
428 EXPECT_EQ(expect_address, actual_address);
429 }
430
431 // You’d think that it might be a good idea to verify that if the conditions
432 // above weren’t met, that the symbol didn’t show up in actual_image’s symbol
433 // table at all. Unfortunately, it’s possible for the same name to show up as
434 // both an external defined symbol and as something else, so it’s not possible
435 // to verify this reliably.
436 }
437
438 // Locates the symbol table in |expect_image| and verifies that all of the
439 // external defined symbols found there are also present and have the same
440 // values in |actual_image|. ExpectSymbol() is used to verify the actual symbol.
441 void ExpectSymbolTable(const MachHeader* expect_image,
442 const MachOImageReader* actual_image) {
443 // This intentionally consults only LC_SYMTAB and not LC_DYSYMTAB so that it
444 // can look at the larger set of all symbols. The actual implementation being
445 // tested is free to consult LC_DYSYMTAB, but that’s considered an
446 // optimization. It’s not necessary for the test, and it’s better for the test
447 // to expose bugs in that optimization rather than duplicate them.
448 const char* commands_base = reinterpret_cast<const char*>(&expect_image[1]);
449 uint32_t position = 0;
450 const symtab_command* symtab = nullptr;
451 const SegmentCommand* linkedit = nullptr;
452 for (uint32_t index = 0; index < expect_image->ncmds; ++index) {
453 ASSERT_LT(position, expect_image->sizeofcmds);
454 const load_command* command =
455 reinterpret_cast<const load_command*>(&commands_base[position]);
456 ASSERT_LE(position + command->cmdsize, expect_image->sizeofcmds);
457 if (command->cmd == LC_SYMTAB) {
458 ASSERT_FALSE(symtab);
459 ASSERT_EQ(sizeof(symtab_command), command->cmdsize);
460 symtab = reinterpret_cast<const symtab_command*>(command);
461 } else if (command->cmd == kSegmentCommand) {
462 ASSERT_GE(command->cmdsize, sizeof(SegmentCommand));
463 const SegmentCommand* segment =
464 reinterpret_cast<const SegmentCommand*>(command);
465 std::string segment_name =
466 MachOImageSegmentReader::SegmentNameString(segment->segname);
467 if (segment_name == SEG_LINKEDIT) {
468 ASSERT_FALSE(linkedit);
469 linkedit = segment;
470 }
471 }
472 position += command->cmdsize;
473 }
474
475 if (symtab) {
476 ASSERT_TRUE(linkedit);
477
478 const char* linkedit_base =
479 reinterpret_cast<const char*>(linkedit->vmaddr + actual_image->Slide());
480 const Nlist* nlist = reinterpret_cast<const Nlist*>(
481 linkedit_base + symtab->symoff - linkedit->fileoff);
482 const char* strtab = linkedit_base + symtab->stroff - linkedit->fileoff;
483
484 for (uint32_t index = 0; index < symtab->nsyms; ++index) {
485 const Nlist* entry = nlist + index;
486 const char* name = strtab + entry->n_un.n_strx;
487 ASSERT_NO_FATAL_FAILURE(ExpectSymbol(entry, name, actual_image));
488 }
489 }
490
491 mach_vm_address_t ignore;
492 EXPECT_FALSE(actual_image->LookUpExternalDefinedSymbol("", &ignore));
493 EXPECT_FALSE(
494 actual_image->LookUpExternalDefinedSymbol("NoSuchSymbolName", &ignore));
495 EXPECT_FALSE(
496 actual_image->LookUpExternalDefinedSymbol("_NoSuchSymbolName", &ignore));
497 }
498
499 TEST(MachOImageReader, Self_MainExecutable) {
500 ProcessReader process_reader;
501 ASSERT_TRUE(process_reader.Initialize(mach_task_self()));
502
503 const MachHeader* mh_execute_header =
504 reinterpret_cast<MachHeader*>(dlsym(RTLD_MAIN_ONLY, MH_EXECUTE_SYM));
505 ASSERT_NE(nullptr, mh_execute_header);
506 mach_vm_address_t mh_execute_header_address =
507 reinterpret_cast<mach_vm_address_t>(mh_execute_header);
508
509 MachOImageReader image_reader;
510 ASSERT_TRUE(image_reader.Initialize(
511 &process_reader, mh_execute_header_address, "executable"));
512
513 EXPECT_EQ(static_cast<uint32_t>(MH_EXECUTE), image_reader.FileType());
514
515 // The main executable has image index 0.
516 intptr_t image_slide = _dyld_get_image_vmaddr_slide(0);
517
518 ASSERT_NO_FATAL_FAILURE(ExpectMachImage(mh_execute_header,
519 mh_execute_header_address,
520 image_slide,
521 &image_reader,
522 true));
523
524 // This symbol, __mh_execute_header, is known to exist in all MH_EXECUTE
525 // Mach-O files.
526 mach_vm_address_t symbol_address;
527 ASSERT_TRUE(image_reader.LookUpExternalDefinedSymbol(_MH_EXECUTE_SYM,
528 &symbol_address));
529 EXPECT_EQ(mh_execute_header_address, symbol_address);
530
531 ASSERT_NO_FATAL_FAILURE(ExpectSymbolTable(mh_execute_header, &image_reader));
532 }
533
534 TEST(MachOImageReader, Self_DyldImages) {
535 ProcessReader process_reader;
536 ASSERT_TRUE(process_reader.Initialize(mach_task_self()));
537
538 uint32_t count = _dyld_image_count();
539 ASSERT_GE(count, 1u);
540
541 for (uint32_t index = 0; index < count; ++index) {
542 const char* image_name = _dyld_get_image_name(index);
543 SCOPED_TRACE(base::StringPrintf("index %u, image %s", index, image_name));
544
545 // _dyld_get_image_header() is poorly-declared: it’s declared as returning
546 // const mach_header* in both 32-bit and 64-bit environments, but in the
547 // 64-bit environment, it should be const mach_header_64*.
548 const MachHeader* mach_header =
549 reinterpret_cast<const MachHeader*>(_dyld_get_image_header(index));
550 mach_vm_address_t image_address =
551 reinterpret_cast<mach_vm_address_t>(mach_header);
552
553 MachOImageReader image_reader;
554 ASSERT_TRUE(
555 image_reader.Initialize(&process_reader, image_address, image_name));
556
557 uint32_t file_type = image_reader.FileType();
558 if (index == 0) {
559 EXPECT_EQ(static_cast<uint32_t>(MH_EXECUTE), file_type);
560 } else {
561 EXPECT_TRUE(file_type == MH_DYLIB || file_type == MH_BUNDLE);
562 }
563
564 intptr_t image_slide = _dyld_get_image_vmaddr_slide(index);
565 ASSERT_NO_FATAL_FAILURE(ExpectMachImage(
566 mach_header, image_address, image_slide, &image_reader, false));
567
568 ASSERT_NO_FATAL_FAILURE(ExpectSymbolTable(mach_header, &image_reader));
569 }
570
571 // Now that all of the modules have been verified, make sure that dyld itself
572 // can be read properly too.
573 const struct dyld_all_image_infos* dyld_image_infos =
574 _dyld_get_all_image_infos();
575 ASSERT_GE(dyld_image_infos->version, 1u);
576 EXPECT_EQ(count, dyld_image_infos->infoArrayCount);
577
578 if (dyld_image_infos->version >= 2) {
579 SCOPED_TRACE("dyld");
580
581 // dyld_all_image_infos::dyldImageLoadAddress is poorly-declared too.
582 const MachHeader* mach_header = reinterpret_cast<const MachHeader*>(
583 dyld_image_infos->dyldImageLoadAddress);
584 mach_vm_address_t image_address =
585 reinterpret_cast<mach_vm_address_t>(mach_header);
586
587 MachOImageReader image_reader;
588 ASSERT_TRUE(
589 image_reader.Initialize(&process_reader, image_address, "dyld"));
590
591 EXPECT_EQ(static_cast<uint32_t>(MH_DYLINKER), image_reader.FileType());
592
593 // There’s no good API to get dyld’s slide, so don’t bother checking it.
594 ASSERT_NO_FATAL_FAILURE(ExpectMachImage(
595 mach_header, image_address, kSlideUnknown, &image_reader, false));
596
597 ASSERT_NO_FATAL_FAILURE(ExpectSymbolTable(mach_header, &image_reader));
598 }
599
600 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
601 // If dyld is new enough to record UUIDs, check the UUID of any module that
602 // it says has one. Note that dyld doesn’t record UUIDs of anything that
603 // loaded out of the shared cache, but it should at least have a UUID for the
604 // main executable if it has one.
605 if (dyld_image_infos->version >= 8 && dyld_image_infos->uuidArray) {
606 for (uint32_t index = 0;
607 index < dyld_image_infos->uuidArrayCount;
608 ++index) {
609 const dyld_uuid_info* dyld_image = &dyld_image_infos->uuidArray[index];
610 SCOPED_TRACE(base::StringPrintf("uuid index %u", index));
611
612 // dyld_uuid_info::imageLoadAddress is poorly-declared too.
613 const MachHeader* mach_header =
614 reinterpret_cast<const MachHeader*>(dyld_image->imageLoadAddress);
615 mach_vm_address_t image_address =
616 reinterpret_cast<mach_vm_address_t>(mach_header);
617
618 MachOImageReader image_reader;
619 ASSERT_TRUE(
620 image_reader.Initialize(&process_reader, image_address, "uuid"));
621
622 // There’s no good way to get the image’s slide here, although the image
623 // should have already been checked along with its slide above, in the
624 // loop through all images.
625 ExpectMachImage(
626 mach_header, image_address, kSlideUnknown, &image_reader, false);
627
628 UUID expected_uuid;
629 expected_uuid.InitializeFromBytes(dyld_image->imageUUID);
630 UUID actual_uuid;
631 image_reader.UUID(&actual_uuid);
632 EXPECT_EQ(expected_uuid, actual_uuid);
633 }
634 }
635 #endif
636 }
637
638 } // namespace
639 } // namespace test
640 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/mac/mach_o_image_reader.cc ('k') | util/mac/mach_o_image_segment_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698