tools/profiler/LulElfInt.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
     4 // Copyright (c) 2006, 2012, Google Inc.
     5 // All rights reserved.
     6 //
     7 // Redistribution and use in source and binary forms, with or without
     8 // modification, are permitted provided that the following conditions are
     9 // met:
    10 //
    11 //     * Redistributions of source code must retain the above copyright
    12 // notice, this list of conditions and the following disclaimer.
    13 //     * Redistributions in binary form must reproduce the above
    14 // copyright notice, this list of conditions and the following disclaimer
    15 // in the documentation and/or other materials provided with the
    16 // distribution.
    17 //     * Neither the name of Google Inc. nor the names of its
    18 // contributors may be used to endorse or promote products derived from
    19 // this software without specific prior written permission.
    20 //
    21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    33 // This file is derived from the following files in
    34 // toolkit/crashreporter/google-breakpad:
    35 //   src/common/android/include/elf.h
    36 //   src/common/linux/elfutils.h
    37 //   src/common/linux/file_id.h
    38 //   src/common/linux/elfutils-inl.h
    40 #ifndef LulElfInt_h
    41 #define LulElfInt_h
    43 // This header defines functions etc internal to the ELF reader.  It
    44 // should not be included outside of LulElf.cpp.
    46 #include <elf.h>
    47 #include <stdlib.h>
    49 #include "mozilla/Assertions.h"
    51 #include "LulPlatformMacros.h"
    54 // (derived from)
    55 // elfutils.h: Utilities for dealing with ELF files.
    56 //
    58 #if defined(LUL_OS_android)
    60 // From toolkit/crashreporter/google-breakpad/src/common/android/include/elf.h
    61 // The Android headers don't always define this constant.
    62 #ifndef EM_X86_64
    63 #define EM_X86_64  62
    64 #endif
    66 #ifndef EM_PPC64
    67 #define EM_PPC64   21
    68 #endif
    70 #ifndef EM_S390
    71 #define EM_S390    22
    72 #endif
    74 #ifndef NT_GNU_BUILD_ID
    75 #define NT_GNU_BUILD_ID 3
    76 #endif
    78 #define ElfW(type)      _ElfW (Elf, ELFSIZE, type)
    79 #define _ElfW(e,w,t)    _ElfW_1 (e, w, _##t)
    80 #define _ElfW_1(e,w,t)  e##w##t
    82 //FIXME
    83 extern "C" {
    84   extern char*  basename(const char*  path);
    85 };
    86 #else
    88 # include <link.h>
    89 #endif
    92 namespace lul {
    94 // Traits classes so consumers can write templatized code to deal
    95 // with specific ELF bits.
    96 struct ElfClass32 {
    97   typedef Elf32_Addr Addr;
    98   typedef Elf32_Ehdr Ehdr;
    99   typedef Elf32_Nhdr Nhdr;
   100   typedef Elf32_Phdr Phdr;
   101   typedef Elf32_Shdr Shdr;
   102   typedef Elf32_Half Half;
   103   typedef Elf32_Off Off;
   104   typedef Elf32_Word Word;
   105   static const int kClass = ELFCLASS32;
   106   static const size_t kAddrSize = sizeof(Elf32_Addr);
   107 };
   109 struct ElfClass64 {
   110   typedef Elf64_Addr Addr;
   111   typedef Elf64_Ehdr Ehdr;
   112   typedef Elf64_Nhdr Nhdr;
   113   typedef Elf64_Phdr Phdr;
   114   typedef Elf64_Shdr Shdr;
   115   typedef Elf64_Half Half;
   116   typedef Elf64_Off Off;
   117   typedef Elf64_Word Word;
   118   static const int kClass = ELFCLASS64;
   119   static const size_t kAddrSize = sizeof(Elf64_Addr);
   120 };
   122 bool IsValidElf(const void* elf_header);
   123 int ElfClass(const void* elf_base);
   125 // Attempt to find a section named |section_name| of type |section_type|
   126 // in the ELF binary data at |elf_mapped_base|. On success, returns true
   127 // and sets |*section_start| to point to the start of the section data,
   128 // and |*section_size| to the size of the section's data. If |elfclass|
   129 // is not NULL, set |*elfclass| to the ELF file class.
   130 bool FindElfSection(const void *elf_mapped_base,
   131                     const char *section_name,
   132                     uint32_t section_type,
   133                     const void **section_start,
   134                     int *section_size,
   135                     int *elfclass);
   137 // Internal helper method, exposed for convenience for callers
   138 // that already have more info.
   139 template<typename ElfClass>
   140 const typename ElfClass::Shdr*
   141 FindElfSectionByName(const char* name,
   142                      typename ElfClass::Word section_type,
   143                      const typename ElfClass::Shdr* sections,
   144                      const char* section_names,
   145                      const char* names_end,
   146                      int nsection);
   148 // Attempt to find the first segment of type |segment_type| in the ELF
   149 // binary data at |elf_mapped_base|. On success, returns true and sets
   150 // |*segment_start| to point to the start of the segment data, and
   151 // and |*segment_size| to the size of the segment's data. If |elfclass|
   152 // is not NULL, set |*elfclass| to the ELF file class.
   153 bool FindElfSegment(const void *elf_mapped_base,
   154                     uint32_t segment_type,
   155                     const void **segment_start,
   156                     int *segment_size,
   157                     int *elfclass);
   159 // Convert an offset from an Elf header into a pointer to the mapped
   160 // address in the current process. Takes an extra template parameter
   161 // to specify the return type to avoid having to dynamic_cast the
   162 // result.
   163 template<typename ElfClass, typename T>
   164 const T*
   165 GetOffset(const typename ElfClass::Ehdr* elf_header,
   166           typename ElfClass::Off offset);
   169 // (derived from)
   170 // file_id.h: Return a unique identifier for a file
   171 //
   173 static const size_t kMDGUIDSize = sizeof(MDGUID);
   175 class FileID {
   176  public:
   178   // Load the identifier for the elf file mapped into memory at |base| into
   179   // |identifier|.  Return false if the identifier could not be created for the
   180   // file.
   181   static bool ElfFileIdentifierFromMappedFile(const void* base,
   182                                               uint8_t identifier[kMDGUIDSize]);
   184   // Convert the |identifier| data to a NULL terminated string.  The string will
   185   // be formatted as a UUID (e.g., 22F065BB-FC9C-49F7-80FE-26A7CEBD7BCE).
   186   // The |buffer| should be at least 37 bytes long to receive all of the data
   187   // and termination.  Shorter buffers will contain truncated data.
   188   static void ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize],
   189                                         char* buffer, int buffer_length);
   190 };
   194 template<typename ElfClass, typename T>
   195 const T* GetOffset(const typename ElfClass::Ehdr* elf_header,
   196                    typename ElfClass::Off offset) {
   197   return reinterpret_cast<const T*>(reinterpret_cast<uintptr_t>(elf_header) +
   198                                     offset);
   199 }
   201 template<typename ElfClass>
   202 const typename ElfClass::Shdr* FindElfSectionByName(
   203     const char* name,
   204     typename ElfClass::Word section_type,
   205     const typename ElfClass::Shdr* sections,
   206     const char* section_names,
   207     const char* names_end,
   208     int nsection) {
   209   MOZ_ASSERT(name != NULL);
   210   MOZ_ASSERT(sections != NULL);
   211   MOZ_ASSERT(nsection > 0);
   213   int name_len = strlen(name);
   214   if (name_len == 0)
   215     return NULL;
   217   for (int i = 0; i < nsection; ++i) {
   218     const char* section_name = section_names + sections[i].sh_name;
   219     if (sections[i].sh_type == section_type &&
   220         names_end - section_name >= name_len + 1 &&
   221         strcmp(name, section_name) == 0) {
   222       return sections + i;
   223     }
   224   }
   225   return NULL;
   226 }
   228 } // namespace lul
   231 // And finally, the external interface, offered to LulMain.cpp
   232 #include "LulElfExt.h"
   234 #endif // LulElfInt_h

mercurial