LCOV - code coverage report
Current view: top level - src/support/allocators - secure.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 10 11 90.9 %
Date: 2023-09-26 12:08:55 Functions: 4 6 66.7 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2021 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 <memory>
      13             : #include <string>
      14             : 
      15             : //
      16             : // Allocator that locks its contents from being paged
      17             : // out of memory and clears its contents before deletion.
      18             : //
      19             : template <typename T>
      20             : struct secure_allocator {
      21             :     using value_type = T;
      22             : 
      23             :     secure_allocator() = default;
      24             :     template <typename U>
      25             :     secure_allocator(const secure_allocator<U>&) noexcept {}
      26             : 
      27           3 :     T* allocate(std::size_t n)
      28             :     {
      29           3 :         T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
      30           3 :         if (!allocation) {
      31           0 :             throw std::bad_alloc();
      32             :         }
      33           3 :         return allocation;
      34             :     }
      35             : 
      36       19093 :     void deallocate(T* p, std::size_t n)
      37             :     {
      38       19093 :         if (p != nullptr) {
      39       19093 :             memory_cleanse(p, sizeof(T) * n);
      40       19093 :         }
      41       19093 :         LockedPoolManager::Instance().free(p);
      42       19093 :     }
      43             : 
      44             :     template <typename U>
      45             :     friend bool operator==(const secure_allocator&, const secure_allocator<U>&) noexcept
      46             :     {
      47             :         return true;
      48             :     }
      49             :     template <typename U>
      50             :     friend bool operator!=(const secure_allocator&, const secure_allocator<U>&) noexcept
      51             :     {
      52             :         return false;
      53             :     }
      54             : };
      55             : 
      56             : // This is exactly like std::string, but with a custom allocator.
      57             : // TODO: Consider finding a way to make incoming RPC request.params[i] mlock()ed as well
      58             : typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
      59             : 
      60             : #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H

Generated by: LCOV version 1.14