LCOV - code coverage report
Current view: directory - toolkit/crashreporter/google-breakpad/src/common/linux - file_id.cc (source / functions) Found Hit Coverage
Test: app.info Lines: 85 67 78.8 %
Date: 2012-06-02 Functions: 7 5 71.4 %

       1                 : // Copyright (c) 2006, Google Inc.
       2                 : // All rights reserved.
       3                 : //
       4                 : // Redistribution and use in source and binary forms, with or without
       5                 : // modification, are permitted provided that the following conditions are
       6                 : // met:
       7                 : //
       8                 : //     * Redistributions of source code must retain the above copyright
       9                 : // notice, this list of conditions and the following disclaimer.
      10                 : //     * Redistributions in binary form must reproduce the above
      11                 : // copyright notice, this list of conditions and the following disclaimer
      12                 : // in the documentation and/or other materials provided with the
      13                 : // distribution.
      14                 : //     * Neither the name of Google Inc. nor the names of its
      15                 : // contributors may be used to endorse or promote products derived from
      16                 : // this software without specific prior written permission.
      17                 : //
      18                 : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      19                 : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      20                 : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      21                 : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      22                 : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      23                 : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      24                 : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      25                 : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      26                 : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      27                 : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      28                 : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      29                 : //
      30                 : // file_id.cc: Return a unique identifier for a file
      31                 : //
      32                 : // See file_id.h for documentation
      33                 : //
      34                 : 
      35                 : #include "common/linux/file_id.h"
      36                 : 
      37                 : #include <arpa/inet.h>
      38                 : #include <assert.h>
      39                 : #include <elf.h>
      40                 : #include <fcntl.h>
      41                 : #if defined(__ANDROID__)
      42                 : #include "client/linux/android_link.h"
      43                 : #else
      44                 : #include <link.h>
      45                 : #endif
      46                 : #include <stdio.h>
      47                 : #include <string.h>
      48                 : #include <sys/mman.h>
      49                 : #include <sys/stat.h>
      50                 : #include <unistd.h>
      51                 : 
      52                 : #include <algorithm>
      53                 : 
      54                 : #include "common/linux/linux_libc_support.h"
      55                 : #include "common/linux/linux_syscall_support.h"
      56                 : 
      57                 : namespace google_breakpad {
      58                 : 
      59             124 : FileID::FileID(const char* path) {
      60             124 :   strncpy(path_, path, sizeof(path_));
      61             124 : }
      62                 : 
      63                 : struct ElfClass32 {
      64                 :   typedef Elf32_Ehdr Ehdr;
      65                 :   typedef Elf32_Shdr Shdr;
      66                 :   static const int kClass = ELFCLASS32;
      67                 : };
      68                 : 
      69                 : struct ElfClass64 {
      70                 :   typedef Elf64_Ehdr Ehdr;
      71                 :   typedef Elf64_Shdr Shdr;
      72                 :   static const int kClass = ELFCLASS64;
      73                 : };
      74                 : 
      75                 : // These three functions are also used inside the crashed process, so be safe
      76                 : // and use the syscall/libc wrappers instead of direct syscalls or libc.
      77                 : template<typename ElfClass>
      78             124 : static void FindElfClassTextSection(const char *elf_base,
      79                 :                                     const void **text_start,
      80                 :                                     int *text_size) {
      81                 :   typedef typename ElfClass::Ehdr Ehdr;
      82                 :   typedef typename ElfClass::Shdr Shdr;
      83                 : 
      84             124 :   assert(elf_base);
      85             124 :   assert(text_start);
      86             124 :   assert(text_size);
      87                 : 
      88             124 :   assert(my_strncmp(elf_base, ELFMAG, SELFMAG) == 0);
      89                 : 
      90             124 :   const char* text_section_name = ".text";
      91             124 :   int name_len = my_strlen(text_section_name);
      92                 : 
      93             124 :   const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base);
      94             124 :   assert(elf_header->e_ident[EI_CLASS] == ElfClass::kClass);
      95                 : 
      96                 :   const Shdr* sections =
      97             124 :       reinterpret_cast<const Shdr*>(elf_base + elf_header->e_shoff);
      98             124 :   const Shdr* string_section = sections + elf_header->e_shstrndx;
      99                 : 
     100             124 :   const Shdr* text_section = NULL;
     101            1586 :   for (int i = 0; i < elf_header->e_shnum; ++i) {
     102            1586 :     if (sections[i].sh_type == SHT_PROGBITS) {
     103                 :       const char* section_name = (char*)(elf_base +
     104                 :                                          string_section->sh_offset +
     105             482 :                                          sections[i].sh_name);
     106             482 :       if (!my_strncmp(section_name, text_section_name, name_len)) {
     107             124 :         text_section = &sections[i];
     108             124 :         break;
     109                 :       }
     110                 :     }
     111                 :   }
     112             124 :   if (text_section != NULL && text_section->sh_size > 0) {
     113             124 :     *text_start = elf_base + text_section->sh_offset;
     114             124 :     *text_size = text_section->sh_size;
     115                 :   }
     116             124 : }
     117                 : 
     118             124 : static bool FindElfTextSection(const void *elf_mapped_base,
     119                 :                                const void **text_start,
     120                 :                                int *text_size) {
     121             124 :   assert(elf_mapped_base);
     122             124 :   assert(text_start);
     123             124 :   assert(text_size);
     124                 : 
     125                 :   const char* elf_base =
     126             124 :     static_cast<const char*>(elf_mapped_base);
     127                 :   const ElfW(Ehdr)* elf_header =
     128             124 :     reinterpret_cast<const ElfW(Ehdr)*>(elf_base);
     129             124 :   if (my_strncmp(elf_base, ELFMAG, SELFMAG) != 0)
     130               0 :     return false;
     131                 : 
     132             124 :   if (elf_header->e_ident[EI_CLASS] == ELFCLASS32) {
     133             124 :     FindElfClassTextSection<ElfClass32>(elf_base, text_start, text_size);
     134               0 :   } else if (elf_header->e_ident[EI_CLASS] == ELFCLASS64) {
     135               0 :     FindElfClassTextSection<ElfClass64>(elf_base, text_start, text_size);
     136                 :   } else {
     137               0 :     return false;
     138                 :   }
     139                 : 
     140             124 :   return true;
     141                 : }
     142                 : 
     143                 : // static
     144             124 : bool FileID::ElfFileIdentifierFromMappedFile(void* base,
     145                 :                                              uint8_t identifier[kMDGUIDSize])
     146                 : {
     147             124 :   const void* text_section = NULL;
     148             124 :   int text_size = 0;
     149             124 :   bool success = false;
     150             124 :   if (FindElfTextSection(base, &text_section, &text_size) && (text_size > 0)) {
     151             124 :     my_memset(identifier, 0, kMDGUIDSize);
     152             124 :     const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section);
     153             124 :     const uint8_t* ptr_end = ptr + std::min(text_size, 4096);
     154           31831 :     while (ptr < ptr_end) {
     155          536911 :       for (unsigned i = 0; i < kMDGUIDSize; i++)
     156          505328 :         identifier[i] ^= ptr[i];
     157           31583 :       ptr += kMDGUIDSize;
     158                 :     }
     159             124 :     success = true;
     160                 :   }
     161             124 :   return success;
     162                 : }
     163                 : 
     164               0 : bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) {
     165               0 :   int fd = open(path_, O_RDONLY);
     166               0 :   if (fd < 0)
     167               0 :     return false;
     168                 :   struct stat st;
     169               0 :   if (fstat(fd, &st) != 0) {
     170               0 :     close(fd);
     171               0 :     return false;
     172                 :   }
     173                 :   void* base = mmap(NULL, st.st_size,
     174               0 :                     PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
     175               0 :   close(fd);
     176               0 :   if (base == MAP_FAILED)
     177               0 :     return false;
     178                 : 
     179               0 :   bool success = ElfFileIdentifierFromMappedFile(base, identifier);
     180               0 :   munmap(base, st.st_size);
     181               0 :   return success;
     182                 : }
     183                 : 
     184                 : // static
     185             124 : void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize],
     186                 :                                        char* buffer, int buffer_length) {
     187                 :   uint8_t identifier_swapped[kMDGUIDSize];
     188                 : 
     189                 :   // Endian-ness swap to match dump processor expectation.
     190             124 :   memcpy(identifier_swapped, identifier, kMDGUIDSize);
     191             124 :   uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped);
     192             124 :   *data1 = htonl(*data1);
     193             124 :   uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4);
     194             124 :   *data2 = htons(*data2);
     195             124 :   uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6);
     196             124 :   *data3 = htons(*data3);
     197                 : 
     198             124 :   int buffer_idx = 0;
     199            2108 :   for (unsigned int idx = 0;
     200                 :        (buffer_idx < buffer_length) && (idx < kMDGUIDSize);
     201                 :        ++idx) {
     202            1984 :     int hi = (identifier_swapped[idx] >> 4) & 0x0F;
     203            1984 :     int lo = (identifier_swapped[idx]) & 0x0F;
     204                 : 
     205            1984 :     if (idx == 4 || idx == 6 || idx == 8 || idx == 10)
     206             496 :       buffer[buffer_idx++] = '-';
     207                 : 
     208            1984 :     buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi;
     209            1984 :     buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo;
     210                 :   }
     211                 : 
     212                 :   // NULL terminate
     213             124 :   buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0;
     214             124 : }
     215                 : 
     216                 : }  // namespace google_breakpad

Generated by: LCOV version 1.7