LCOV - code coverage report
Current view: top level - toolkit/crashreporter/google-breakpad/src/common - memory.h (source / functions) Hit Total Coverage
Test: output.info Lines: 0 70 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 52 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2009, 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             : #ifndef GOOGLE_BREAKPAD_COMMON_MEMORY_H_
      31             : #define GOOGLE_BREAKPAD_COMMON_MEMORY_H_
      32             : 
      33             : #include <stdint.h>
      34             : #include <stdlib.h>
      35             : #include <unistd.h>
      36             : #include <sys/mman.h>
      37             : 
      38             : #include <memory>
      39             : #include <vector>
      40             : 
      41             : #if defined(MEMORY_SANITIZER)
      42             : #include <sanitizer/msan_interface.h>
      43             : #endif
      44             : 
      45             : #ifdef __APPLE__
      46             : #define sys_mmap mmap
      47             : #define sys_munmap munmap
      48             : #define MAP_ANONYMOUS MAP_ANON
      49             : #else
      50             : #include "third_party/lss/linux_syscall_support.h"
      51             : #endif
      52             : 
      53             : namespace google_breakpad {
      54             : 
      55             : // This is very simple allocator which fetches pages from the kernel directly.
      56             : // Thus, it can be used even when the heap may be corrupted.
      57             : //
      58             : // There is no free operation. The pages are only freed when the object is
      59             : // destroyed.
      60             : class PageAllocator {
      61             :  public:
      62           0 :   PageAllocator()
      63           0 :       : page_size_(getpagesize()),
      64             :         last_(NULL),
      65             :         current_page_(NULL),
      66             :         page_offset_(0),
      67           0 :         pages_allocated_(0) {
      68           0 :   }
      69             : 
      70           0 :   ~PageAllocator() {
      71           0 :     FreeAll();
      72           0 :   }
      73             : 
      74           0 :   void *Alloc(size_t bytes) {
      75           0 :     if (!bytes)
      76           0 :       return NULL;
      77             : 
      78           0 :     if (current_page_ && page_size_ - page_offset_ >= bytes) {
      79           0 :       uint8_t *const ret = current_page_ + page_offset_;
      80           0 :       page_offset_ += bytes;
      81           0 :       if (page_offset_ == page_size_) {
      82           0 :         page_offset_ = 0;
      83           0 :         current_page_ = NULL;
      84             :       }
      85             : 
      86           0 :       return ret;
      87             :     }
      88             : 
      89             :     const size_t pages =
      90           0 :         (bytes + sizeof(PageHeader) + page_size_ - 1) / page_size_;
      91           0 :     uint8_t *const ret = GetNPages(pages);
      92           0 :     if (!ret)
      93           0 :       return NULL;
      94             : 
      95           0 :     page_offset_ =
      96           0 :         (page_size_ - (page_size_ * pages - (bytes + sizeof(PageHeader)))) %
      97           0 :         page_size_;
      98           0 :     current_page_ = page_offset_ ? ret + page_size_ * (pages - 1) : NULL;
      99             : 
     100           0 :     return ret + sizeof(PageHeader);
     101             :   }
     102             : 
     103             :   // Checks whether the page allocator owns the passed-in pointer.
     104             :   // This method exists for testing pursposes only.
     105             :   bool OwnsPointer(const void* p) {
     106             :     for (PageHeader* header = last_; header; header = header->next) {
     107             :       const char* current = reinterpret_cast<char*>(header);
     108             :       if ((p >= current) && (p < current + header->num_pages * page_size_))
     109             :         return true;
     110             :     }
     111             : 
     112             :     return false;
     113             :   }
     114             : 
     115             :   unsigned long pages_allocated() { return pages_allocated_; }
     116             : 
     117             :  private:
     118           0 :   uint8_t *GetNPages(size_t num_pages) {
     119           0 :     void *a = sys_mmap(NULL, page_size_ * num_pages, PROT_READ | PROT_WRITE,
     120           0 :                        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
     121           0 :     if (a == MAP_FAILED)
     122           0 :       return NULL;
     123             : 
     124             : #if defined(MEMORY_SANITIZER)
     125             :     // We need to indicate to MSan that memory allocated through sys_mmap is
     126             :     // initialized, since linux_syscall_support.h doesn't have MSan hooks.
     127             :     __msan_unpoison(a, page_size_ * num_pages);
     128             : #endif
     129             : 
     130           0 :     struct PageHeader *header = reinterpret_cast<PageHeader*>(a);
     131           0 :     header->next = last_;
     132           0 :     header->num_pages = num_pages;
     133           0 :     last_ = header;
     134             : 
     135           0 :     pages_allocated_ += num_pages;
     136             : 
     137           0 :     return reinterpret_cast<uint8_t*>(a);
     138             :   }
     139             : 
     140           0 :   void FreeAll() {
     141             :     PageHeader *next;
     142             : 
     143           0 :     for (PageHeader *cur = last_; cur; cur = next) {
     144           0 :       next = cur->next;
     145           0 :       sys_munmap(cur, cur->num_pages * page_size_);
     146             :     }
     147           0 :   }
     148             : 
     149             :   struct PageHeader {
     150             :     PageHeader *next;  // pointer to the start of the next set of pages.
     151             :     size_t num_pages;  // the number of pages in this set.
     152             :   };
     153             : 
     154             :   const size_t page_size_;
     155             :   PageHeader *last_;
     156             :   uint8_t *current_page_;
     157             :   size_t page_offset_;
     158             :   unsigned long pages_allocated_;
     159             : };
     160             : 
     161             : // Wrapper to use with STL containers
     162             : template <typename T>
     163           0 : struct PageStdAllocator : public std::allocator<T> {
     164             :   typedef typename std::allocator<T>::pointer pointer;
     165             :   typedef typename std::allocator<T>::size_type size_type;
     166             : 
     167           0 :   explicit PageStdAllocator(PageAllocator& allocator) : allocator_(allocator),
     168             :                                                         stackdata_(NULL),
     169           0 :                                                         stackdata_size_(0)
     170           0 :   {}
     171             : 
     172             :   template <class Other> PageStdAllocator(const PageStdAllocator<Other>& other)
     173             :       : allocator_(other.allocator_),
     174             :         stackdata_(nullptr),
     175             :         stackdata_size_(0)
     176             :   {}
     177             : 
     178           0 :   explicit PageStdAllocator(PageAllocator& allocator,
     179             :                             pointer stackdata,
     180             :                             size_type stackdata_size) : allocator_(allocator),
     181             :       stackdata_(stackdata),
     182           0 :       stackdata_size_(stackdata_size)
     183           0 :   {}
     184             : 
     185           0 :   inline pointer allocate(size_type n, const void* = 0) {
     186           0 :     const size_type size = sizeof(T) * n;
     187           0 :     if (size <= stackdata_size_) {
     188           0 :       return stackdata_;
     189             :     }
     190           0 :     return static_cast<pointer>(allocator_.Alloc(size));
     191             :   }
     192             : 
     193           0 :   inline void deallocate(pointer, size_type) {
     194             :     // The PageAllocator doesn't free.
     195           0 :   }
     196             : 
     197             :   template <typename U> struct rebind {
     198             :     typedef PageStdAllocator<U> other;
     199             :   };
     200             : 
     201             :  private:
     202             :   // Silly workaround for the gcc from Android's ndk (gcc 4.6), which will
     203             :   // otherwise complain that `other.allocator_` is private in the constructor
     204             :   // code.
     205             :   template<typename Other> friend struct PageStdAllocator;
     206             : 
     207             :   PageAllocator& allocator_;
     208             :   pointer stackdata_;
     209             :   size_type stackdata_size_;
     210             : };
     211             : 
     212             : // A wasteful vector is a std::vector, except that it allocates memory from a
     213             : // PageAllocator. It's wasteful because, when resizing, it always allocates a
     214             : // whole new array since the PageAllocator doesn't support realloc.
     215             : template<class T>
     216           0 : class wasteful_vector : public std::vector<T, PageStdAllocator<T> > {
     217             :  public:
     218           0 :   wasteful_vector(PageAllocator* allocator, unsigned size_hint = 16)
     219           0 :       : std::vector<T, PageStdAllocator<T> >(PageStdAllocator<T>(*allocator)) {
     220           0 :     std::vector<T, PageStdAllocator<T> >::reserve(size_hint);
     221           0 :   }
     222             :  protected:
     223           0 :   wasteful_vector(PageStdAllocator<T> allocator)
     224           0 :       : std::vector<T, PageStdAllocator<T> >(allocator) {}
     225             : };
     226             : 
     227             : // auto_wasteful_vector allocates space on the stack for N entries to avoid
     228             : // using the PageAllocator for small data, while still allowing for larger data.
     229             : template<class T, unsigned int N>
     230           0 : class auto_wasteful_vector : public wasteful_vector<T> {
     231             :  T stackdata_[N];
     232             :  public:
     233           0 :   auto_wasteful_vector(PageAllocator* allocator)
     234             :       : wasteful_vector<T>(
     235             :             PageStdAllocator<T>(*allocator,
     236             :                                 &stackdata_[0],
     237           0 :                                 sizeof(stackdata_))) {
     238           0 :     std::vector<T, PageStdAllocator<T> >::reserve(N);
     239           0 :   }
     240             : };
     241             : 
     242             : }  // namespace google_breakpad
     243             : 
     244           0 : inline void* operator new(size_t nbytes,
     245             :                           google_breakpad::PageAllocator& allocator) {
     246           0 :   return allocator.Alloc(nbytes);
     247             : }
     248             : 
     249             : #endif  // GOOGLE_BREAKPAD_COMMON_MEMORY_H_

Generated by: LCOV version 1.13