1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/linux/file_id.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,195 @@ 1.4 +// Copyright (c) 2006, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// file_id.cc: Return a unique identifier for a file 1.34 +// 1.35 +// See file_id.h for documentation 1.36 +// 1.37 + 1.38 +#include "common/linux/file_id.h" 1.39 + 1.40 +#include <arpa/inet.h> 1.41 +#include <assert.h> 1.42 +#include <string.h> 1.43 + 1.44 +#include <algorithm> 1.45 + 1.46 +#include "common/linux/elfutils.h" 1.47 +#include "common/linux/linux_libc_support.h" 1.48 +#include "common/linux/memory_mapped_file.h" 1.49 +#include "third_party/lss/linux_syscall_support.h" 1.50 + 1.51 +namespace google_breakpad { 1.52 + 1.53 +#ifndef NT_GNU_BUILD_ID 1.54 +#define NT_GNU_BUILD_ID 3 1.55 +#endif 1.56 + 1.57 +FileID::FileID(const char* path) { 1.58 + strncpy(path_, path, sizeof(path_)); 1.59 +} 1.60 + 1.61 +// ELF note name and desc are 32-bits word padded. 1.62 +#define NOTE_PADDING(a) ((a + 3) & ~3) 1.63 + 1.64 +// These functions are also used inside the crashed process, so be safe 1.65 +// and use the syscall/libc wrappers instead of direct syscalls or libc. 1.66 + 1.67 +template<typename ElfClass> 1.68 +static bool ElfClassBuildIDNoteIdentifier(const void *section, int length, 1.69 + uint8_t identifier[kMDGUIDSize]) { 1.70 + typedef typename ElfClass::Nhdr Nhdr; 1.71 + 1.72 + const void* section_end = reinterpret_cast<const char*>(section) + length; 1.73 + const Nhdr* note_header = reinterpret_cast<const Nhdr*>(section); 1.74 + while (reinterpret_cast<const void *>(note_header) < section_end) { 1.75 + if (note_header->n_type == NT_GNU_BUILD_ID) 1.76 + break; 1.77 + note_header = reinterpret_cast<const Nhdr*>( 1.78 + reinterpret_cast<const char*>(note_header) + sizeof(Nhdr) + 1.79 + NOTE_PADDING(note_header->n_namesz) + 1.80 + NOTE_PADDING(note_header->n_descsz)); 1.81 + } 1.82 + if (reinterpret_cast<const void *>(note_header) >= section_end || 1.83 + note_header->n_descsz == 0) { 1.84 + return false; 1.85 + } 1.86 + 1.87 + const char* build_id = reinterpret_cast<const char*>(note_header) + 1.88 + sizeof(Nhdr) + NOTE_PADDING(note_header->n_namesz); 1.89 + // Copy as many bits of the build ID as will fit 1.90 + // into the GUID space. 1.91 + my_memset(identifier, 0, kMDGUIDSize); 1.92 + memcpy(identifier, build_id, 1.93 + std::min(kMDGUIDSize, (size_t)note_header->n_descsz)); 1.94 + 1.95 + return true; 1.96 +} 1.97 + 1.98 +// Attempt to locate a .note.gnu.build-id section in an ELF binary 1.99 +// and copy as many bytes of it as will fit into |identifier|. 1.100 +static bool FindElfBuildIDNote(const void *elf_mapped_base, 1.101 + uint8_t identifier[kMDGUIDSize]) { 1.102 + void* note_section; 1.103 + int note_size, elfclass; 1.104 + if ((!FindElfSegment(elf_mapped_base, PT_NOTE, 1.105 + (const void**)¬e_section, ¬e_size, &elfclass) || 1.106 + note_size == 0) && 1.107 + (!FindElfSection(elf_mapped_base, ".note.gnu.build-id", SHT_NOTE, 1.108 + (const void**)¬e_section, ¬e_size, &elfclass) || 1.109 + note_size == 0)) { 1.110 + return false; 1.111 + } 1.112 + 1.113 + if (elfclass == ELFCLASS32) { 1.114 + return ElfClassBuildIDNoteIdentifier<ElfClass32>(note_section, note_size, 1.115 + identifier); 1.116 + } else if (elfclass == ELFCLASS64) { 1.117 + return ElfClassBuildIDNoteIdentifier<ElfClass64>(note_section, note_size, 1.118 + identifier); 1.119 + } 1.120 + 1.121 + return false; 1.122 +} 1.123 + 1.124 +// Attempt to locate the .text section of an ELF binary and generate 1.125 +// a simple hash by XORing the first page worth of bytes into |identifier|. 1.126 +static bool HashElfTextSection(const void *elf_mapped_base, 1.127 + uint8_t identifier[kMDGUIDSize]) { 1.128 + void* text_section; 1.129 + int text_size; 1.130 + if (!FindElfSection(elf_mapped_base, ".text", SHT_PROGBITS, 1.131 + (const void**)&text_section, &text_size, NULL) || 1.132 + text_size == 0) { 1.133 + return false; 1.134 + } 1.135 + 1.136 + my_memset(identifier, 0, kMDGUIDSize); 1.137 + const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section); 1.138 + const uint8_t* ptr_end = ptr + std::min(text_size, 4096); 1.139 + while (ptr < ptr_end) { 1.140 + for (unsigned i = 0; i < kMDGUIDSize; i++) 1.141 + identifier[i] ^= ptr[i]; 1.142 + ptr += kMDGUIDSize; 1.143 + } 1.144 + return true; 1.145 +} 1.146 + 1.147 +// static 1.148 +bool FileID::ElfFileIdentifierFromMappedFile(const void* base, 1.149 + uint8_t identifier[kMDGUIDSize]) { 1.150 + // Look for a build id note first. 1.151 + if (FindElfBuildIDNote(base, identifier)) 1.152 + return true; 1.153 + 1.154 + // Fall back on hashing the first page of the text section. 1.155 + return HashElfTextSection(base, identifier); 1.156 +} 1.157 + 1.158 +bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) { 1.159 + MemoryMappedFile mapped_file(path_); 1.160 + if (!mapped_file.data()) // Should probably check if size >= ElfW(Ehdr)? 1.161 + return false; 1.162 + 1.163 + return ElfFileIdentifierFromMappedFile(mapped_file.data(), identifier); 1.164 +} 1.165 + 1.166 +// static 1.167 +void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], 1.168 + char* buffer, int buffer_length) { 1.169 + uint8_t identifier_swapped[kMDGUIDSize]; 1.170 + 1.171 + // Endian-ness swap to match dump processor expectation. 1.172 + memcpy(identifier_swapped, identifier, kMDGUIDSize); 1.173 + uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped); 1.174 + *data1 = htonl(*data1); 1.175 + uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4); 1.176 + *data2 = htons(*data2); 1.177 + uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6); 1.178 + *data3 = htons(*data3); 1.179 + 1.180 + int buffer_idx = 0; 1.181 + for (unsigned int idx = 0; 1.182 + (buffer_idx < buffer_length) && (idx < kMDGUIDSize); 1.183 + ++idx) { 1.184 + int hi = (identifier_swapped[idx] >> 4) & 0x0F; 1.185 + int lo = (identifier_swapped[idx]) & 0x0F; 1.186 + 1.187 + if (idx == 4 || idx == 6 || idx == 8 || idx == 10) 1.188 + buffer[buffer_idx++] = '-'; 1.189 + 1.190 + buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi; 1.191 + buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo; 1.192 + } 1.193 + 1.194 + // NULL terminate 1.195 + buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0; 1.196 +} 1.197 + 1.198 +} // namespace google_breakpad