toolkit/crashreporter/google-breakpad/src/common/mac/macho_reader.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // -*- mode: C++ -*-
michael@0 2
michael@0 3 // Copyright (c) 2010, Google Inc.
michael@0 4 // All rights reserved.
michael@0 5 //
michael@0 6 // Redistribution and use in source and binary forms, with or without
michael@0 7 // modification, are permitted provided that the following conditions are
michael@0 8 // met:
michael@0 9 //
michael@0 10 // * Redistributions of source code must retain the above copyright
michael@0 11 // notice, this list of conditions and the following disclaimer.
michael@0 12 // * Redistributions in binary form must reproduce the above
michael@0 13 // copyright notice, this list of conditions and the following disclaimer
michael@0 14 // in the documentation and/or other materials provided with the
michael@0 15 // distribution.
michael@0 16 // * Neither the name of Google Inc. nor the names of its
michael@0 17 // contributors may be used to endorse or promote products derived from
michael@0 18 // this software without specific prior written permission.
michael@0 19 //
michael@0 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31
michael@0 32 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
michael@0 33
michael@0 34 // macho_reader.h: A class for parsing Mach-O files.
michael@0 35
michael@0 36 #ifndef BREAKPAD_COMMON_MAC_MACHO_READER_H_
michael@0 37 #define BREAKPAD_COMMON_MAC_MACHO_READER_H_
michael@0 38
michael@0 39 #include <mach-o/loader.h>
michael@0 40 #include <mach-o/fat.h>
michael@0 41 #include <stdint.h>
michael@0 42 #include <stdlib.h>
michael@0 43 #include <unistd.h>
michael@0 44
michael@0 45 #include <map>
michael@0 46 #include <string>
michael@0 47 #include <vector>
michael@0 48
michael@0 49 #include "common/byte_cursor.h"
michael@0 50
michael@0 51 namespace google_breakpad {
michael@0 52 namespace mach_o {
michael@0 53
michael@0 54 using std::map;
michael@0 55 using std::string;
michael@0 56 using std::vector;
michael@0 57
michael@0 58 // The Mac headers don't specify particular types for these groups of
michael@0 59 // constants, but defining them here provides some documentation
michael@0 60 // value. We also give them the same width as the fields in which
michael@0 61 // they appear, which makes them a bit easier to use with ByteCursors.
michael@0 62 typedef uint32_t Magic;
michael@0 63 typedef uint32_t FileType;
michael@0 64 typedef uint32_t FileFlags;
michael@0 65 typedef uint32_t LoadCommandType;
michael@0 66 typedef uint32_t SegmentFlags;
michael@0 67 typedef uint32_t SectionFlags;
michael@0 68
michael@0 69 // A parser for fat binary files, used to store universal binaries.
michael@0 70 // When applied to a (non-fat) Mach-O file, this behaves as if the
michael@0 71 // file were a fat file containing a single object file.
michael@0 72 class FatReader {
michael@0 73 public:
michael@0 74
michael@0 75 // A class for reporting errors found while parsing fat binary files. The
michael@0 76 // default definitions of these methods print messages to stderr.
michael@0 77 class Reporter {
michael@0 78 public:
michael@0 79 // Create a reporter that attributes problems to |filename|.
michael@0 80 explicit Reporter(const string &filename) : filename_(filename) { }
michael@0 81
michael@0 82 virtual ~Reporter() { }
michael@0 83
michael@0 84 // The data does not begin with a fat binary or Mach-O magic number.
michael@0 85 // This is a fatal error.
michael@0 86 virtual void BadHeader();
michael@0 87
michael@0 88 // The Mach-O fat binary file ends abruptly, without enough space
michael@0 89 // to contain an object file it claims is present.
michael@0 90 virtual void MisplacedObjectFile();
michael@0 91
michael@0 92 // The file ends abruptly: either it is not large enough to hold a
michael@0 93 // complete header, or the header implies that contents are present
michael@0 94 // beyond the actual end of the file.
michael@0 95 virtual void TooShort();
michael@0 96
michael@0 97 private:
michael@0 98 // The filename to which the reader should attribute problems.
michael@0 99 string filename_;
michael@0 100 };
michael@0 101
michael@0 102 // Create a fat binary file reader that uses |reporter| to report problems.
michael@0 103 explicit FatReader(Reporter *reporter) : reporter_(reporter) { }
michael@0 104
michael@0 105 // Read the |size| bytes at |buffer| as a fat binary file. On success,
michael@0 106 // return true; on failure, report the problem to reporter_ and return
michael@0 107 // false.
michael@0 108 //
michael@0 109 // If the data is a plain Mach-O file, rather than a fat binary file,
michael@0 110 // then the reader behaves as if it had found a fat binary file whose
michael@0 111 // single object file is the Mach-O file.
michael@0 112 bool Read(const uint8_t *buffer, size_t size);
michael@0 113
michael@0 114 // Return an array of 'struct fat_arch' structures describing the
michael@0 115 // object files present in this fat binary file. Set |size| to the
michael@0 116 // number of elements in the array.
michael@0 117 //
michael@0 118 // Assuming Read returned true, the entries are validated: it is
michael@0 119 // safe to assume that the offsets and sizes in each 'struct
michael@0 120 // fat_arch' refer to subranges of the bytes passed to Read.
michael@0 121 //
michael@0 122 // If there are no object files in this fat binary, then this
michael@0 123 // function can return NULL.
michael@0 124 //
michael@0 125 // The array is owned by this FatReader instance; it will be freed when
michael@0 126 // this FatReader is destroyed.
michael@0 127 //
michael@0 128 // This function returns a C-style array instead of a vector to make it
michael@0 129 // possible to use the result with OS X functions like NXFindBestFatArch,
michael@0 130 // so that the symbol dumper will behave consistently with other OS X
michael@0 131 // utilities that work with fat binaries.
michael@0 132 const struct fat_arch *object_files(size_t *count) const {
michael@0 133 *count = object_files_.size();
michael@0 134 if (object_files_.size() > 0)
michael@0 135 return &object_files_[0];
michael@0 136 return NULL;
michael@0 137 }
michael@0 138
michael@0 139 private:
michael@0 140 // We use this to report problems parsing the file's contents. (WEAK)
michael@0 141 Reporter *reporter_;
michael@0 142
michael@0 143 // The contents of the fat binary or Mach-O file we're parsing. We do not
michael@0 144 // own the storage it refers to.
michael@0 145 ByteBuffer buffer_;
michael@0 146
michael@0 147 // The magic number of this binary, in host byte order.
michael@0 148 Magic magic_;
michael@0 149
michael@0 150 // The list of object files in this binary.
michael@0 151 // object_files_.size() == fat_header.nfat_arch
michael@0 152 vector<struct fat_arch> object_files_;
michael@0 153 };
michael@0 154
michael@0 155 // A segment in a Mach-O file. All these fields have been byte-swapped as
michael@0 156 // appropriate for use by the executing architecture.
michael@0 157 struct Segment {
michael@0 158 // The ByteBuffers below point into the bytes passed to the Reader that
michael@0 159 // created this Segment.
michael@0 160
michael@0 161 ByteBuffer section_list; // This segment's section list.
michael@0 162 ByteBuffer contents; // This segment's contents.
michael@0 163
michael@0 164 // This segment's name.
michael@0 165 string name;
michael@0 166
michael@0 167 // The address at which this segment should be loaded in memory. If
michael@0 168 // bits_64 is false, only the bottom 32 bits of this value are valid.
michael@0 169 uint64_t vmaddr;
michael@0 170
michael@0 171 // The size of this segment when loaded into memory. This may be larger
michael@0 172 // than contents.Size(), in which case the extra area will be
michael@0 173 // initialized with zeros. If bits_64 is false, only the bottom 32 bits
michael@0 174 // of this value are valid.
michael@0 175 uint64_t vmsize;
michael@0 176
michael@0 177 // The maximum and initial VM protection of this segment's contents.
michael@0 178 uint32_t maxprot;
michael@0 179 uint32_t initprot;
michael@0 180
michael@0 181 // The number of sections in section_list.
michael@0 182 uint32_t nsects;
michael@0 183
michael@0 184 // Flags describing this segment, from SegmentFlags.
michael@0 185 uint32_t flags;
michael@0 186
michael@0 187 // True if this is a 64-bit section; false if it is a 32-bit section.
michael@0 188 bool bits_64;
michael@0 189 };
michael@0 190
michael@0 191 // A section in a Mach-O file. All these fields have been byte-swapped as
michael@0 192 // appropriate for use by the executing architecture.
michael@0 193 struct Section {
michael@0 194 // This section's contents. This points into the bytes passed to the
michael@0 195 // Reader that created this Section.
michael@0 196 ByteBuffer contents;
michael@0 197
michael@0 198 // This section's name.
michael@0 199 string section_name; // section[_64].sectname
michael@0 200 // The name of the segment this section belongs to.
michael@0 201 string segment_name; // section[_64].segname
michael@0 202
michael@0 203 // The address at which this section's contents should be loaded in
michael@0 204 // memory. If bits_64 is false, only the bottom 32 bits of this value
michael@0 205 // are valid.
michael@0 206 uint64_t address;
michael@0 207
michael@0 208 // The contents of this section should be loaded into memory at an
michael@0 209 // address which is a multiple of (two raised to this power).
michael@0 210 uint32_t align;
michael@0 211
michael@0 212 // Flags from SectionFlags describing the section's contents.
michael@0 213 uint32_t flags;
michael@0 214
michael@0 215 // We don't support reading relocations yet.
michael@0 216
michael@0 217 // True if this is a 64-bit section; false if it is a 32-bit section.
michael@0 218 bool bits_64;
michael@0 219 };
michael@0 220
michael@0 221 // A map from section names to Sections.
michael@0 222 typedef map<string, Section> SectionMap;
michael@0 223
michael@0 224 // A reader for a Mach-O file.
michael@0 225 //
michael@0 226 // This does not handle fat binaries; see FatReader above. FatReader
michael@0 227 // provides a friendly interface for parsing data that could be either a
michael@0 228 // fat binary or a Mach-O file.
michael@0 229 class Reader {
michael@0 230 public:
michael@0 231
michael@0 232 // A class for reporting errors found while parsing Mach-O files. The
michael@0 233 // default definitions of these member functions print messages to
michael@0 234 // stderr.
michael@0 235 class Reporter {
michael@0 236 public:
michael@0 237 // Create a reporter that attributes problems to |filename|.
michael@0 238 explicit Reporter(const string &filename) : filename_(filename) { }
michael@0 239 virtual ~Reporter() { }
michael@0 240
michael@0 241 // Reporter functions for fatal errors return void; the reader will
michael@0 242 // definitely return an error to its caller after calling them
michael@0 243
michael@0 244 // The data does not begin with a Mach-O magic number, or the magic
michael@0 245 // number does not match the expected value for the cpu architecture.
michael@0 246 // This is a fatal error.
michael@0 247 virtual void BadHeader();
michael@0 248
michael@0 249 // The data contained in a Mach-O fat binary (|cpu_type|, |cpu_subtype|)
michael@0 250 // does not match the expected CPU architecture
michael@0 251 // (|expected_cpu_type|, |expected_cpu_subtype|).
michael@0 252 virtual void CPUTypeMismatch(cpu_type_t cpu_type,
michael@0 253 cpu_subtype_t cpu_subtype,
michael@0 254 cpu_type_t expected_cpu_type,
michael@0 255 cpu_subtype_t expected_cpu_subtype);
michael@0 256
michael@0 257 // The file ends abruptly: either it is not large enough to hold a
michael@0 258 // complete header, or the header implies that contents are present
michael@0 259 // beyond the actual end of the file.
michael@0 260 virtual void HeaderTruncated();
michael@0 261
michael@0 262 // The file's load command region, as given in the Mach-O header, is
michael@0 263 // too large for the file.
michael@0 264 virtual void LoadCommandRegionTruncated();
michael@0 265
michael@0 266 // The file's Mach-O header claims the file contains |claimed| load
michael@0 267 // commands, but the I'th load command, of type |type|, extends beyond
michael@0 268 // the end of the load command region, as given by the Mach-O header.
michael@0 269 // If |type| is zero, the command's type was unreadable.
michael@0 270 virtual void LoadCommandsOverrun(size_t claimed, size_t i,
michael@0 271 LoadCommandType type);
michael@0 272
michael@0 273 // The contents of the |i|'th load command, of type |type|, extend beyond
michael@0 274 // the size given in the load command's header.
michael@0 275 virtual void LoadCommandTooShort(size_t i, LoadCommandType type);
michael@0 276
michael@0 277 // The LC_SEGMENT or LC_SEGMENT_64 load command for the segment named
michael@0 278 // |name| is too short to hold the sections that its header says it does.
michael@0 279 // (This more specific than LoadCommandTooShort.)
michael@0 280 virtual void SectionsMissing(const string &name);
michael@0 281
michael@0 282 // The segment named |name| claims that its contents lie beyond the end
michael@0 283 // of the file.
michael@0 284 virtual void MisplacedSegmentData(const string &name);
michael@0 285
michael@0 286 // The section named |section| in the segment named |segment| claims that
michael@0 287 // its contents do not lie entirely within the segment.
michael@0 288 virtual void MisplacedSectionData(const string &section,
michael@0 289 const string &segment);
michael@0 290
michael@0 291 // The LC_SYMTAB command claims that symbol table contents are located
michael@0 292 // beyond the end of the file.
michael@0 293 virtual void MisplacedSymbolTable();
michael@0 294
michael@0 295 // An attempt was made to read a Mach-O file of the unsupported
michael@0 296 // CPU architecture |cpu_type|.
michael@0 297 virtual void UnsupportedCPUType(cpu_type_t cpu_type);
michael@0 298
michael@0 299 private:
michael@0 300 string filename_;
michael@0 301 };
michael@0 302
michael@0 303 // A handler for sections parsed from a segment. The WalkSegmentSections
michael@0 304 // member function accepts an instance of this class, and applies it to
michael@0 305 // each section defined in a given segment.
michael@0 306 class SectionHandler {
michael@0 307 public:
michael@0 308 virtual ~SectionHandler() { }
michael@0 309
michael@0 310 // Called to report that the segment's section list contains |section|.
michael@0 311 // This should return true if the iteration should continue, or false
michael@0 312 // if it should stop.
michael@0 313 virtual bool HandleSection(const Section &section) = 0;
michael@0 314 };
michael@0 315
michael@0 316 // A handler for the load commands in a Mach-O file.
michael@0 317 class LoadCommandHandler {
michael@0 318 public:
michael@0 319 LoadCommandHandler() { }
michael@0 320 virtual ~LoadCommandHandler() { }
michael@0 321
michael@0 322 // When called from WalkLoadCommands, the following handler functions
michael@0 323 // should return true if they wish to continue iterating over the load
michael@0 324 // command list, or false if they wish to stop iterating.
michael@0 325 //
michael@0 326 // When called from LoadCommandIterator::Handle or Reader::Handle,
michael@0 327 // these functions' return values are simply passed through to Handle's
michael@0 328 // caller.
michael@0 329 //
michael@0 330 // The definitions provided by this base class simply return true; the
michael@0 331 // default is to silently ignore sections whose member functions the
michael@0 332 // subclass doesn't override.
michael@0 333
michael@0 334 // COMMAND is load command we don't recognize. We provide only the
michael@0 335 // command type and a ByteBuffer enclosing the command's data (If we
michael@0 336 // cannot parse the command type or its size, we call
michael@0 337 // reporter_->IncompleteLoadCommand instead.)
michael@0 338 virtual bool UnknownCommand(LoadCommandType type,
michael@0 339 const ByteBuffer &contents) {
michael@0 340 return true;
michael@0 341 }
michael@0 342
michael@0 343 // The load command is LC_SEGMENT or LC_SEGMENT_64, defining a segment
michael@0 344 // with the properties given in |segment|.
michael@0 345 virtual bool SegmentCommand(const Segment &segment) {
michael@0 346 return true;
michael@0 347 }
michael@0 348
michael@0 349 // The load command is LC_SYMTAB. |entries| holds the array of nlist
michael@0 350 // entries, and |names| holds the strings the entries refer to.
michael@0 351 virtual bool SymtabCommand(const ByteBuffer &entries,
michael@0 352 const ByteBuffer &names) {
michael@0 353 return true;
michael@0 354 }
michael@0 355
michael@0 356 // Add handler functions for more load commands here as needed.
michael@0 357 };
michael@0 358
michael@0 359 // Create a Mach-O file reader that reports problems to |reporter|.
michael@0 360 explicit Reader(Reporter *reporter)
michael@0 361 : reporter_(reporter) { }
michael@0 362
michael@0 363 // Read the given data as a Mach-O file. The reader retains pointers
michael@0 364 // into the data passed, so the data should live as long as the reader
michael@0 365 // does. On success, return true; on failure, return false.
michael@0 366 //
michael@0 367 // At most one of these functions should be invoked once on each Reader
michael@0 368 // instance.
michael@0 369 bool Read(const uint8_t *buffer,
michael@0 370 size_t size,
michael@0 371 cpu_type_t expected_cpu_type,
michael@0 372 cpu_subtype_t expected_cpu_subtype);
michael@0 373 bool Read(const ByteBuffer &buffer,
michael@0 374 cpu_type_t expected_cpu_type,
michael@0 375 cpu_subtype_t expected_cpu_subtype) {
michael@0 376 return Read(buffer.start,
michael@0 377 buffer.Size(),
michael@0 378 expected_cpu_type,
michael@0 379 expected_cpu_subtype);
michael@0 380 }
michael@0 381
michael@0 382 // Return this file's characteristics, as found in the Mach-O header.
michael@0 383 cpu_type_t cpu_type() const { return cpu_type_; }
michael@0 384 cpu_subtype_t cpu_subtype() const { return cpu_subtype_; }
michael@0 385 FileType file_type() const { return file_type_; }
michael@0 386 FileFlags flags() const { return flags_; }
michael@0 387
michael@0 388 // Return true if this is a 64-bit Mach-O file, false if it is a 32-bit
michael@0 389 // Mach-O file.
michael@0 390 bool bits_64() const { return bits_64_; }
michael@0 391
michael@0 392 // Return true if this is a big-endian Mach-O file, false if it is
michael@0 393 // little-endian.
michael@0 394 bool big_endian() const { return big_endian_; }
michael@0 395
michael@0 396 // Apply |handler| to each load command in this Mach-O file, stopping when
michael@0 397 // a handler function returns false. If we encounter a malformed load
michael@0 398 // command, report it via reporter_ and return false. Return true if all
michael@0 399 // load commands were parseable and all handlers returned true.
michael@0 400 bool WalkLoadCommands(LoadCommandHandler *handler) const;
michael@0 401
michael@0 402 // Set |segment| to describe the segment named |name|, if present. If
michael@0 403 // found, |segment|'s byte buffers refer to a subregion of the bytes
michael@0 404 // passed to Read. If we find the section, return true; otherwise,
michael@0 405 // return false.
michael@0 406 bool FindSegment(const string &name, Segment *segment) const;
michael@0 407
michael@0 408 // Apply |handler| to each section defined in |segment|. If |handler| returns
michael@0 409 // false, stop iterating and return false. If all calls to |handler| return
michael@0 410 // true and we reach the end of the section list, return true.
michael@0 411 bool WalkSegmentSections(const Segment &segment, SectionHandler *handler)
michael@0 412 const;
michael@0 413
michael@0 414 // Clear |section_map| and then populate it with a map of the sections
michael@0 415 // in |segment|, from section names to Section structures.
michael@0 416 // Each Section's contents refer to bytes in |segment|'s contents.
michael@0 417 // On success, return true; if a problem occurs, report it and return false.
michael@0 418 bool MapSegmentSections(const Segment &segment, SectionMap *section_map)
michael@0 419 const;
michael@0 420
michael@0 421 private:
michael@0 422 // Used internally.
michael@0 423 class SegmentFinder;
michael@0 424 class SectionMapper;
michael@0 425
michael@0 426 // We use this to report problems parsing the file's contents. (WEAK)
michael@0 427 Reporter *reporter_;
michael@0 428
michael@0 429 // The contents of the Mach-O file we're parsing. We do not own the
michael@0 430 // storage it refers to.
michael@0 431 ByteBuffer buffer_;
michael@0 432
michael@0 433 // True if this file is big-endian.
michael@0 434 bool big_endian_;
michael@0 435
michael@0 436 // True if this file is a 64-bit Mach-O file.
michael@0 437 bool bits_64_;
michael@0 438
michael@0 439 // This file's cpu type and subtype.
michael@0 440 cpu_type_t cpu_type_; // mach_header[_64].cputype
michael@0 441 cpu_subtype_t cpu_subtype_; // mach_header[_64].cpusubtype
michael@0 442
michael@0 443 // This file's type.
michael@0 444 FileType file_type_; // mach_header[_64].filetype
michael@0 445
michael@0 446 // The region of buffer_ occupied by load commands.
michael@0 447 ByteBuffer load_commands_;
michael@0 448
michael@0 449 // The number of load commands in load_commands_.
michael@0 450 uint32_t load_command_count_; // mach_header[_64].ncmds
michael@0 451
michael@0 452 // This file's header flags.
michael@0 453 FileFlags flags_;
michael@0 454 };
michael@0 455
michael@0 456 } // namespace mach_o
michael@0 457 } // namespace google_breakpad
michael@0 458
michael@0 459 #endif // BREAKPAD_COMMON_MAC_MACHO_READER_H_

mercurial