LCOV - code coverage report
Current view: top level - src/support/allocators - secure.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 12 13 92.3 %
Date: 2021-06-20 09:29:20 Functions: 8 8 100.0 %
Branches: 2 4 50.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2019 The Bitcoin Core developers
       3                 :            : // Distributed under the MIT software license, see the accompanying
       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :            : 
       6                 :            : #ifndef BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
       7                 :            : #define BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
       8                 :            : 
       9                 :            : #include <support/lockedpool.h>
      10                 :            : #include <support/cleanse.h>
      11                 :            : 
      12                 :            : #include <string>
      13                 :            : 
      14                 :            : //
      15                 :            : // Allocator that locks its contents from being paged
      16                 :            : // out of memory and clears its contents before deletion.
      17                 :            : //
      18                 :            : template <typename T>
      19                 :            : struct secure_allocator : public std::allocator<T> {
      20                 :            :     // MSVC8 default copy constructor is broken
      21                 :            :     typedef std::allocator<T> base;
      22                 :            :     typedef typename base::size_type size_type;
      23                 :            :     typedef typename base::difference_type difference_type;
      24                 :            :     typedef typename base::pointer pointer;
      25                 :            :     typedef typename base::const_pointer const_pointer;
      26                 :            :     typedef typename base::reference reference;
      27                 :            :     typedef typename base::const_reference const_reference;
      28                 :            :     typedef typename base::value_type value_type;
      29                 :      71044 :     secure_allocator() noexcept {}
      30                 :      20842 :     secure_allocator(const secure_allocator& a) noexcept : base(a) {}
      31                 :            :     template <typename U>
      32                 :            :     secure_allocator(const secure_allocator<U>& a) noexcept : base(a)
      33                 :            :     {
      34                 :            :     }
      35                 :            :     ~secure_allocator() noexcept {}
      36                 :            :     template <typename _Other>
      37                 :            :     struct rebind {
      38                 :            :         typedef secure_allocator<_Other> other;
      39                 :            :     };
      40                 :            : 
      41                 :      79745 :     T* allocate(std::size_t n, const void* hint = 0)
      42                 :            :     {
      43                 :      79745 :         T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
      44         [ +  - ]:      79745 :         if (!allocation) {
      45                 :          0 :             throw std::bad_alloc();
      46                 :            :         }
      47                 :      79745 :         return allocation;
      48                 :            :     }
      49                 :            : 
      50                 :      79745 :     void deallocate(T* p, std::size_t n)
      51                 :            :     {
      52         [ +  - ]:      79745 :         if (p != nullptr) {
      53                 :      79745 :             memory_cleanse(p, sizeof(T) * n);
      54                 :      79745 :         }
      55                 :      79745 :         LockedPoolManager::Instance().free(p);
      56                 :      79745 :     }
      57                 :            : };
      58                 :            : 
      59                 :            : // This is exactly like std::string, but with a custom allocator.
      60                 :            : typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
      61                 :            : 
      62                 :            : #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H

Generated by: LCOV version 1.14