toolkit/crashreporter/google-breakpad/src/common/dwarf/bytereader.cc

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 // Copyright (c) 2010 Google Inc. All Rights Reserved.
michael@0 2 //
michael@0 3 // Redistribution and use in source and binary forms, with or without
michael@0 4 // modification, are permitted provided that the following conditions are
michael@0 5 // met:
michael@0 6 //
michael@0 7 // * Redistributions of source code must retain the above copyright
michael@0 8 // notice, this list of conditions and the following disclaimer.
michael@0 9 // * Redistributions in binary form must reproduce the above
michael@0 10 // copyright notice, this list of conditions and the following disclaimer
michael@0 11 // in the documentation and/or other materials provided with the
michael@0 12 // distribution.
michael@0 13 // * Neither the name of Google Inc. nor the names of its
michael@0 14 // contributors may be used to endorse or promote products derived from
michael@0 15 // this software without specific prior written permission.
michael@0 16 //
michael@0 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 28
michael@0 29 #include <assert.h>
michael@0 30 #include <stdlib.h>
michael@0 31
michael@0 32 #include "common/dwarf/bytereader-inl.h"
michael@0 33 #include "common/dwarf/bytereader.h"
michael@0 34
michael@0 35 namespace dwarf2reader {
michael@0 36
michael@0 37 ByteReader::ByteReader(enum Endianness endian)
michael@0 38 :offset_reader_(NULL), address_reader_(NULL), endian_(endian),
michael@0 39 address_size_(0), offset_size_(0),
michael@0 40 have_section_base_(), have_text_base_(), have_data_base_(),
michael@0 41 have_function_base_() { }
michael@0 42
michael@0 43 ByteReader::~ByteReader() { }
michael@0 44
michael@0 45 void ByteReader::SetOffsetSize(uint8 size) {
michael@0 46 offset_size_ = size;
michael@0 47 assert(size == 4 || size == 8);
michael@0 48 if (size == 4) {
michael@0 49 this->offset_reader_ = &ByteReader::ReadFourBytes;
michael@0 50 } else {
michael@0 51 this->offset_reader_ = &ByteReader::ReadEightBytes;
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 void ByteReader::SetAddressSize(uint8 size) {
michael@0 56 address_size_ = size;
michael@0 57 assert(size == 4 || size == 8);
michael@0 58 if (size == 4) {
michael@0 59 this->address_reader_ = &ByteReader::ReadFourBytes;
michael@0 60 } else {
michael@0 61 this->address_reader_ = &ByteReader::ReadEightBytes;
michael@0 62 }
michael@0 63 }
michael@0 64
michael@0 65 uint64 ByteReader::ReadInitialLength(const char* start, size_t* len) {
michael@0 66 const uint64 initial_length = ReadFourBytes(start);
michael@0 67 start += 4;
michael@0 68
michael@0 69 // In DWARF2/3, if the initial length is all 1 bits, then the offset
michael@0 70 // size is 8 and we need to read the next 8 bytes for the real length.
michael@0 71 if (initial_length == 0xffffffff) {
michael@0 72 SetOffsetSize(8);
michael@0 73 *len = 12;
michael@0 74 return ReadOffset(start);
michael@0 75 } else {
michael@0 76 SetOffsetSize(4);
michael@0 77 *len = 4;
michael@0 78 }
michael@0 79 return initial_length;
michael@0 80 }
michael@0 81
michael@0 82 bool ByteReader::ValidEncoding(DwarfPointerEncoding encoding) const {
michael@0 83 if (encoding == DW_EH_PE_omit) return true;
michael@0 84 if (encoding == DW_EH_PE_aligned) return true;
michael@0 85 if ((encoding & 0x7) > DW_EH_PE_udata8)
michael@0 86 return false;
michael@0 87 if ((encoding & 0x70) > DW_EH_PE_funcrel)
michael@0 88 return false;
michael@0 89 return true;
michael@0 90 }
michael@0 91
michael@0 92 bool ByteReader::UsableEncoding(DwarfPointerEncoding encoding) const {
michael@0 93 switch (encoding & 0x70) {
michael@0 94 case DW_EH_PE_absptr: return true;
michael@0 95 case DW_EH_PE_pcrel: return have_section_base_;
michael@0 96 case DW_EH_PE_textrel: return have_text_base_;
michael@0 97 case DW_EH_PE_datarel: return have_data_base_;
michael@0 98 case DW_EH_PE_funcrel: return have_function_base_;
michael@0 99 default: return false;
michael@0 100 }
michael@0 101 }
michael@0 102
michael@0 103 uint64 ByteReader::ReadEncodedPointer(const char *buffer,
michael@0 104 DwarfPointerEncoding encoding,
michael@0 105 size_t *len) const {
michael@0 106 // UsableEncoding doesn't approve of DW_EH_PE_omit, so we shouldn't
michael@0 107 // see it here.
michael@0 108 assert(encoding != DW_EH_PE_omit);
michael@0 109
michael@0 110 // The Linux Standards Base 4.0 does not make this clear, but the
michael@0 111 // GNU tools (gcc/unwind-pe.h; readelf/dwarf.c; gdb/dwarf2-frame.c)
michael@0 112 // agree that aligned pointers are always absolute, machine-sized,
michael@0 113 // machine-signed pointers.
michael@0 114 if (encoding == DW_EH_PE_aligned) {
michael@0 115 assert(have_section_base_);
michael@0 116
michael@0 117 // We don't need to align BUFFER in *our* address space. Rather, we
michael@0 118 // need to find the next position in our buffer that would be aligned
michael@0 119 // when the .eh_frame section the buffer contains is loaded into the
michael@0 120 // program's memory. So align assuming that buffer_base_ gets loaded at
michael@0 121 // address section_base_, where section_base_ itself may or may not be
michael@0 122 // aligned.
michael@0 123
michael@0 124 // First, find the offset to START from the closest prior aligned
michael@0 125 // address.
michael@0 126 uint64 skew = section_base_ & (AddressSize() - 1);
michael@0 127 // Now find the offset from that aligned address to buffer.
michael@0 128 uint64 offset = skew + (buffer - buffer_base_);
michael@0 129 // Round up to the next boundary.
michael@0 130 uint64 aligned = (offset + AddressSize() - 1) & -AddressSize();
michael@0 131 // Convert back to a pointer.
michael@0 132 const char *aligned_buffer = buffer_base_ + (aligned - skew);
michael@0 133 // Finally, store the length and actually fetch the pointer.
michael@0 134 *len = aligned_buffer - buffer + AddressSize();
michael@0 135 return ReadAddress(aligned_buffer);
michael@0 136 }
michael@0 137
michael@0 138 // Extract the value first, ignoring whether it's a pointer or an
michael@0 139 // offset relative to some base.
michael@0 140 uint64 offset;
michael@0 141 switch (encoding & 0x0f) {
michael@0 142 case DW_EH_PE_absptr:
michael@0 143 // DW_EH_PE_absptr is weird, as it is used as a meaningful value for
michael@0 144 // both the high and low nybble of encoding bytes. When it appears in
michael@0 145 // the high nybble, it means that the pointer is absolute, not an
michael@0 146 // offset from some base address. When it appears in the low nybble,
michael@0 147 // as here, it means that the pointer is stored as a normal
michael@0 148 // machine-sized and machine-signed address. A low nybble of
michael@0 149 // DW_EH_PE_absptr does not imply that the pointer is absolute; it is
michael@0 150 // correct for us to treat the value as an offset from a base address
michael@0 151 // if the upper nybble is not DW_EH_PE_absptr.
michael@0 152 offset = ReadAddress(buffer);
michael@0 153 *len = AddressSize();
michael@0 154 break;
michael@0 155
michael@0 156 case DW_EH_PE_uleb128:
michael@0 157 offset = ReadUnsignedLEB128(buffer, len);
michael@0 158 break;
michael@0 159
michael@0 160 case DW_EH_PE_udata2:
michael@0 161 offset = ReadTwoBytes(buffer);
michael@0 162 *len = 2;
michael@0 163 break;
michael@0 164
michael@0 165 case DW_EH_PE_udata4:
michael@0 166 offset = ReadFourBytes(buffer);
michael@0 167 *len = 4;
michael@0 168 break;
michael@0 169
michael@0 170 case DW_EH_PE_udata8:
michael@0 171 offset = ReadEightBytes(buffer);
michael@0 172 *len = 8;
michael@0 173 break;
michael@0 174
michael@0 175 case DW_EH_PE_sleb128:
michael@0 176 offset = ReadSignedLEB128(buffer, len);
michael@0 177 break;
michael@0 178
michael@0 179 case DW_EH_PE_sdata2:
michael@0 180 offset = ReadTwoBytes(buffer);
michael@0 181 // Sign-extend from 16 bits.
michael@0 182 offset = (offset ^ 0x8000) - 0x8000;
michael@0 183 *len = 2;
michael@0 184 break;
michael@0 185
michael@0 186 case DW_EH_PE_sdata4:
michael@0 187 offset = ReadFourBytes(buffer);
michael@0 188 // Sign-extend from 32 bits.
michael@0 189 offset = (offset ^ 0x80000000ULL) - 0x80000000ULL;
michael@0 190 *len = 4;
michael@0 191 break;
michael@0 192
michael@0 193 case DW_EH_PE_sdata8:
michael@0 194 // No need to sign-extend; this is the full width of our type.
michael@0 195 offset = ReadEightBytes(buffer);
michael@0 196 *len = 8;
michael@0 197 break;
michael@0 198
michael@0 199 default:
michael@0 200 abort();
michael@0 201 }
michael@0 202
michael@0 203 // Find the appropriate base address.
michael@0 204 uint64 base;
michael@0 205 switch (encoding & 0x70) {
michael@0 206 case DW_EH_PE_absptr:
michael@0 207 base = 0;
michael@0 208 break;
michael@0 209
michael@0 210 case DW_EH_PE_pcrel:
michael@0 211 assert(have_section_base_);
michael@0 212 base = section_base_ + (buffer - buffer_base_);
michael@0 213 break;
michael@0 214
michael@0 215 case DW_EH_PE_textrel:
michael@0 216 assert(have_text_base_);
michael@0 217 base = text_base_;
michael@0 218 break;
michael@0 219
michael@0 220 case DW_EH_PE_datarel:
michael@0 221 assert(have_data_base_);
michael@0 222 base = data_base_;
michael@0 223 break;
michael@0 224
michael@0 225 case DW_EH_PE_funcrel:
michael@0 226 assert(have_function_base_);
michael@0 227 base = function_base_;
michael@0 228 break;
michael@0 229
michael@0 230 default:
michael@0 231 abort();
michael@0 232 }
michael@0 233
michael@0 234 uint64 pointer = base + offset;
michael@0 235
michael@0 236 // Remove inappropriate upper bits.
michael@0 237 if (AddressSize() == 4)
michael@0 238 pointer = pointer & 0xffffffff;
michael@0 239 else
michael@0 240 assert(AddressSize() == sizeof(uint64));
michael@0 241
michael@0 242 return pointer;
michael@0 243 }
michael@0 244
michael@0 245 } // namespace dwarf2reader

mercurial