Initial Vexta Core v0.1.0 release
This commit is contained in:
commit
25c454329d
2481 changed files with 689289 additions and 0 deletions
63
src/support/allocators/secure.h
Normal file
63
src/support/allocators/secure.h
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
||||
// Copyright (c) 2014-2019 The DigiByte Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef DIGIBYTE_SUPPORT_ALLOCATORS_SECURE_H
|
||||
#define DIGIBYTE_SUPPORT_ALLOCATORS_SECURE_H
|
||||
|
||||
#include <support/lockedpool.h>
|
||||
#include <support/cleanse.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
//
|
||||
// Allocator that locks its contents from being paged
|
||||
// out of memory and clears its contents before deletion.
|
||||
//
|
||||
template <typename T>
|
||||
struct secure_allocator : public std::allocator<T> {
|
||||
// MSVC8 default copy constructor is broken
|
||||
typedef std::allocator<T> base;
|
||||
typedef typename base::size_type size_type;
|
||||
typedef typename base::difference_type difference_type;
|
||||
typedef typename base::pointer pointer;
|
||||
typedef typename base::const_pointer const_pointer;
|
||||
typedef typename base::reference reference;
|
||||
typedef typename base::const_reference const_reference;
|
||||
typedef typename base::value_type value_type;
|
||||
secure_allocator() noexcept {}
|
||||
secure_allocator(const secure_allocator& a) noexcept : base(a) {}
|
||||
template <typename U>
|
||||
secure_allocator(const secure_allocator<U>& a) noexcept : base(a)
|
||||
{
|
||||
}
|
||||
~secure_allocator() noexcept {}
|
||||
template <typename _Other>
|
||||
struct rebind {
|
||||
typedef secure_allocator<_Other> other;
|
||||
};
|
||||
|
||||
T* allocate(std::size_t n, const void* hint = 0)
|
||||
{
|
||||
T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
|
||||
if (!allocation) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
return allocation;
|
||||
}
|
||||
|
||||
void deallocate(T* p, std::size_t n)
|
||||
{
|
||||
if (p != nullptr) {
|
||||
memory_cleanse(p, sizeof(T) * n);
|
||||
}
|
||||
LockedPoolManager::Instance().free(p);
|
||||
}
|
||||
};
|
||||
|
||||
// This is exactly like std::string, but with a custom allocator.
|
||||
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
|
||||
|
||||
#endif // DIGIBYTE_SUPPORT_ALLOCATORS_SECURE_H
|
||||
49
src/support/allocators/zeroafterfree.h
Normal file
49
src/support/allocators/zeroafterfree.h
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
||||
// Copyright (c) 2014-2019 The DigiByte Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef DIGIBYTE_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
|
||||
#define DIGIBYTE_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
|
||||
|
||||
#include <support/cleanse.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
template <typename T>
|
||||
struct zero_after_free_allocator : public std::allocator<T> {
|
||||
// MSVC8 default copy constructor is broken
|
||||
typedef std::allocator<T> base;
|
||||
typedef typename base::size_type size_type;
|
||||
typedef typename base::difference_type difference_type;
|
||||
typedef typename base::pointer pointer;
|
||||
typedef typename base::const_pointer const_pointer;
|
||||
typedef typename base::reference reference;
|
||||
typedef typename base::const_reference const_reference;
|
||||
typedef typename base::value_type value_type;
|
||||
zero_after_free_allocator() noexcept {}
|
||||
zero_after_free_allocator(const zero_after_free_allocator& a) noexcept : base(a) {}
|
||||
template <typename U>
|
||||
zero_after_free_allocator(const zero_after_free_allocator<U>& a) noexcept : base(a)
|
||||
{
|
||||
}
|
||||
~zero_after_free_allocator() noexcept {}
|
||||
template <typename _Other>
|
||||
struct rebind {
|
||||
typedef zero_after_free_allocator<_Other> other;
|
||||
};
|
||||
|
||||
void deallocate(T* p, std::size_t n)
|
||||
{
|
||||
if (p != nullptr)
|
||||
memory_cleanse(p, sizeof(T) * n);
|
||||
std::allocator<T>::deallocate(p, n);
|
||||
}
|
||||
};
|
||||
|
||||
/** Byte-vector that clears its contents before deletion. */
|
||||
using SerializeData = std::vector<uint8_t, zero_after_free_allocator<uint8_t>>;
|
||||
|
||||
#endif // DIGIBYTE_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
|
||||
36
src/support/cleanse.cpp
Normal file
36
src/support/cleanse.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
||||
// Copyright (c) 2014-2020 The DigiByte Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <support/cleanse.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <Windows.h> // For SecureZeroMemory.
|
||||
#endif
|
||||
|
||||
void memory_cleanse(void *ptr, size_t len)
|
||||
{
|
||||
#if defined(_MSC_VER)
|
||||
/* SecureZeroMemory is guaranteed not to be optimized out by MSVC. */
|
||||
SecureZeroMemory(ptr, len);
|
||||
#else
|
||||
std::memset(ptr, 0, len);
|
||||
|
||||
/* Memory barrier that scares the compiler away from optimizing out the memset.
|
||||
*
|
||||
* Quoting Adam Langley <agl@google.com> in commit ad1907fe73334d6c696c8539646c21b11178f20f
|
||||
* in BoringSSL (ISC License):
|
||||
* As best as we can tell, this is sufficient to break any optimisations that
|
||||
* might try to eliminate "superfluous" memsets.
|
||||
* This method is used in memzero_explicit() the Linux kernel, too. Its advantage is that it
|
||||
* is pretty efficient because the compiler can still implement the memset() efficiently,
|
||||
* just not remove it entirely. See "Dead Store Elimination (Still) Considered Harmful" by
|
||||
* Yang et al. (USENIX Security 2017) for more background.
|
||||
*/
|
||||
__asm__ __volatile__("" : : "r"(ptr) : "memory");
|
||||
#endif
|
||||
}
|
||||
16
src/support/cleanse.h
Normal file
16
src/support/cleanse.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
||||
// Copyright (c) 2014-2020 The DigiByte Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef DIGIBYTE_SUPPORT_CLEANSE_H
|
||||
#define DIGIBYTE_SUPPORT_CLEANSE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/** Secure overwrite a buffer (possibly containing secret data) with zero-bytes. The write
|
||||
* operation will not be optimized out by the compiler. */
|
||||
void memory_cleanse(void *ptr, size_t len);
|
||||
|
||||
#endif // DIGIBYTE_SUPPORT_CLEANSE_H
|
||||
57
src/support/events.h
Normal file
57
src/support/events.h
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
||||
// Copyright (c) 2014-2020 The DigiByte Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef DIGIBYTE_SUPPORT_EVENTS_H
|
||||
#define DIGIBYTE_SUPPORT_EVENTS_H
|
||||
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
|
||||
#include <event2/event.h>
|
||||
#include <event2/http.h>
|
||||
|
||||
#define MAKE_RAII(type) \
|
||||
/* deleter */\
|
||||
struct type##_deleter {\
|
||||
void operator()(struct type* ob) {\
|
||||
type##_free(ob);\
|
||||
}\
|
||||
};\
|
||||
/* unique ptr typedef */\
|
||||
typedef std::unique_ptr<struct type, type##_deleter> raii_##type
|
||||
|
||||
MAKE_RAII(event_base);
|
||||
MAKE_RAII(event);
|
||||
MAKE_RAII(evhttp);
|
||||
MAKE_RAII(evhttp_request);
|
||||
MAKE_RAII(evhttp_connection);
|
||||
|
||||
inline raii_event_base obtain_event_base() {
|
||||
auto result = raii_event_base(event_base_new());
|
||||
if (!result.get())
|
||||
throw std::runtime_error("cannot create event_base");
|
||||
return result;
|
||||
}
|
||||
|
||||
inline raii_event obtain_event(struct event_base* base, evutil_socket_t s, short events, event_callback_fn cb, void* arg) {
|
||||
return raii_event(event_new(base, s, events, cb, arg));
|
||||
}
|
||||
|
||||
inline raii_evhttp obtain_evhttp(struct event_base* base) {
|
||||
return raii_evhttp(evhttp_new(base));
|
||||
}
|
||||
|
||||
inline raii_evhttp_request obtain_evhttp_request(void(*cb)(struct evhttp_request *, void *), void *arg) {
|
||||
return raii_evhttp_request(evhttp_request_new(cb, arg));
|
||||
}
|
||||
|
||||
inline raii_evhttp_connection obtain_evhttp_connection_base(struct event_base* base, std::string host, uint16_t port) {
|
||||
auto result = raii_evhttp_connection(evhttp_connection_base_new(base, nullptr, host.c_str(), port));
|
||||
if (!result.get())
|
||||
throw std::runtime_error("create connection failed");
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // DIGIBYTE_SUPPORT_EVENTS_H
|
||||
413
src/support/lockedpool.cpp
Normal file
413
src/support/lockedpool.cpp
Normal file
|
|
@ -0,0 +1,413 @@
|
|||
// Copyright (c) 2016-2020 The Bitcoin Core developers
|
||||
// Copyright (c) 2016-2020 The DigiByte Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <support/lockedpool.h>
|
||||
#include <support/cleanse.h>
|
||||
|
||||
#if defined(HAVE_CONFIG_H)
|
||||
#include <config/vexta-config.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/mman.h> // for mmap
|
||||
#include <sys/resource.h> // for getrlimit
|
||||
#include <limits.h> // for PAGESIZE
|
||||
#include <unistd.h> // for sysconf
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#ifdef ARENA_DEBUG
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
LockedPoolManager* LockedPoolManager::_instance = nullptr;
|
||||
|
||||
/*******************************************************************************/
|
||||
// Utilities
|
||||
//
|
||||
/** Align up to power of 2 */
|
||||
static inline size_t align_up(size_t x, size_t align)
|
||||
{
|
||||
return (x + align - 1) & ~(align - 1);
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
// Implementation: Arena
|
||||
|
||||
Arena::Arena(void *base_in, size_t size_in, size_t alignment_in):
|
||||
base(static_cast<char*>(base_in)), end(static_cast<char*>(base_in) + size_in), alignment(alignment_in)
|
||||
{
|
||||
// Start with one free chunk that covers the entire arena
|
||||
auto it = size_to_free_chunk.emplace(size_in, base);
|
||||
chunks_free.emplace(base, it);
|
||||
chunks_free_end.emplace(base + size_in, it);
|
||||
}
|
||||
|
||||
Arena::~Arena()
|
||||
{
|
||||
}
|
||||
|
||||
void* Arena::alloc(size_t size)
|
||||
{
|
||||
// Round to next multiple of alignment
|
||||
size = align_up(size, alignment);
|
||||
|
||||
// Don't handle zero-sized chunks
|
||||
if (size == 0)
|
||||
return nullptr;
|
||||
|
||||
// Pick a large enough free-chunk. Returns an iterator pointing to the first element that is not less than key.
|
||||
// This allocation strategy is best-fit. According to "Dynamic Storage Allocation: A Survey and Critical Review",
|
||||
// Wilson et. al. 1995, https://www.scs.stanford.edu/14wi-cs140/sched/readings/wilson.pdf, best-fit and first-fit
|
||||
// policies seem to work well in practice.
|
||||
auto size_ptr_it = size_to_free_chunk.lower_bound(size);
|
||||
if (size_ptr_it == size_to_free_chunk.end())
|
||||
return nullptr;
|
||||
|
||||
// Create the used-chunk, taking its space from the end of the free-chunk
|
||||
const size_t size_remaining = size_ptr_it->first - size;
|
||||
auto allocated = chunks_used.emplace(size_ptr_it->second + size_remaining, size).first;
|
||||
chunks_free_end.erase(size_ptr_it->second + size_ptr_it->first);
|
||||
if (size_ptr_it->first == size) {
|
||||
// whole chunk is used up
|
||||
chunks_free.erase(size_ptr_it->second);
|
||||
} else {
|
||||
// still some memory left in the chunk
|
||||
auto it_remaining = size_to_free_chunk.emplace(size_remaining, size_ptr_it->second);
|
||||
chunks_free[size_ptr_it->second] = it_remaining;
|
||||
chunks_free_end.emplace(size_ptr_it->second + size_remaining, it_remaining);
|
||||
}
|
||||
size_to_free_chunk.erase(size_ptr_it);
|
||||
|
||||
return reinterpret_cast<void*>(allocated->first);
|
||||
}
|
||||
|
||||
void Arena::free(void *ptr)
|
||||
{
|
||||
// Freeing the nullptr pointer is OK.
|
||||
if (ptr == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove chunk from used map
|
||||
auto i = chunks_used.find(static_cast<char*>(ptr));
|
||||
if (i == chunks_used.end()) {
|
||||
throw std::runtime_error("Arena: invalid or double free");
|
||||
}
|
||||
std::pair<char*, size_t> freed = *i;
|
||||
chunks_used.erase(i);
|
||||
|
||||
// coalesce freed with previous chunk
|
||||
auto prev = chunks_free_end.find(freed.first);
|
||||
if (prev != chunks_free_end.end()) {
|
||||
freed.first -= prev->second->first;
|
||||
freed.second += prev->second->first;
|
||||
size_to_free_chunk.erase(prev->second);
|
||||
chunks_free_end.erase(prev);
|
||||
}
|
||||
|
||||
// coalesce freed with chunk after freed
|
||||
auto next = chunks_free.find(freed.first + freed.second);
|
||||
if (next != chunks_free.end()) {
|
||||
freed.second += next->second->first;
|
||||
size_to_free_chunk.erase(next->second);
|
||||
chunks_free.erase(next);
|
||||
}
|
||||
|
||||
// Add/set space with coalesced free chunk
|
||||
auto it = size_to_free_chunk.emplace(freed.second, freed.first);
|
||||
chunks_free[freed.first] = it;
|
||||
chunks_free_end[freed.first + freed.second] = it;
|
||||
}
|
||||
|
||||
Arena::Stats Arena::stats() const
|
||||
{
|
||||
Arena::Stats r{ 0, 0, 0, chunks_used.size(), chunks_free.size() };
|
||||
for (const auto& chunk: chunks_used)
|
||||
r.used += chunk.second;
|
||||
for (const auto& chunk: chunks_free)
|
||||
r.free += chunk.second->first;
|
||||
r.total = r.used + r.free;
|
||||
return r;
|
||||
}
|
||||
|
||||
#ifdef ARENA_DEBUG
|
||||
static void printchunk(void* base, size_t sz, bool used) {
|
||||
std::cout <<
|
||||
"0x" << std::hex << std::setw(16) << std::setfill('0') << base <<
|
||||
" 0x" << std::hex << std::setw(16) << std::setfill('0') << sz <<
|
||||
" 0x" << used << std::endl;
|
||||
}
|
||||
void Arena::walk() const
|
||||
{
|
||||
for (const auto& chunk: chunks_used)
|
||||
printchunk(chunk.first, chunk.second, true);
|
||||
std::cout << std::endl;
|
||||
for (const auto& chunk: chunks_free)
|
||||
printchunk(chunk.first, chunk.second->first, false);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*******************************************************************************/
|
||||
// Implementation: Win32LockedPageAllocator
|
||||
|
||||
#ifdef WIN32
|
||||
/** LockedPageAllocator specialized for Windows.
|
||||
*/
|
||||
class Win32LockedPageAllocator: public LockedPageAllocator
|
||||
{
|
||||
public:
|
||||
Win32LockedPageAllocator();
|
||||
void* AllocateLocked(size_t len, bool *lockingSuccess) override;
|
||||
void FreeLocked(void* addr, size_t len) override;
|
||||
size_t GetLimit() override;
|
||||
private:
|
||||
size_t page_size;
|
||||
};
|
||||
|
||||
Win32LockedPageAllocator::Win32LockedPageAllocator()
|
||||
{
|
||||
// Determine system page size in bytes
|
||||
SYSTEM_INFO sSysInfo;
|
||||
GetSystemInfo(&sSysInfo);
|
||||
page_size = sSysInfo.dwPageSize;
|
||||
}
|
||||
void *Win32LockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess)
|
||||
{
|
||||
len = align_up(len, page_size);
|
||||
void *addr = VirtualAlloc(nullptr, len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
||||
if (addr) {
|
||||
// VirtualLock is used to attempt to keep keying material out of swap. Note
|
||||
// that it does not provide this as a guarantee, but, in practice, memory
|
||||
// that has been VirtualLock'd almost never gets written to the pagefile
|
||||
// except in rare circumstances where memory is extremely low.
|
||||
*lockingSuccess = VirtualLock(const_cast<void*>(addr), len) != 0;
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
void Win32LockedPageAllocator::FreeLocked(void* addr, size_t len)
|
||||
{
|
||||
len = align_up(len, page_size);
|
||||
memory_cleanse(addr, len);
|
||||
VirtualUnlock(const_cast<void*>(addr), len);
|
||||
}
|
||||
|
||||
size_t Win32LockedPageAllocator::GetLimit()
|
||||
{
|
||||
// TODO is there a limit on Windows, how to get it?
|
||||
return std::numeric_limits<size_t>::max();
|
||||
}
|
||||
#endif
|
||||
|
||||
/*******************************************************************************/
|
||||
// Implementation: PosixLockedPageAllocator
|
||||
|
||||
#ifndef WIN32
|
||||
/** LockedPageAllocator specialized for OSes that don't try to be
|
||||
* special snowflakes.
|
||||
*/
|
||||
class PosixLockedPageAllocator: public LockedPageAllocator
|
||||
{
|
||||
public:
|
||||
PosixLockedPageAllocator();
|
||||
void* AllocateLocked(size_t len, bool *lockingSuccess) override;
|
||||
void FreeLocked(void* addr, size_t len) override;
|
||||
size_t GetLimit() override;
|
||||
private:
|
||||
size_t page_size;
|
||||
};
|
||||
|
||||
PosixLockedPageAllocator::PosixLockedPageAllocator()
|
||||
{
|
||||
// Determine system page size in bytes
|
||||
#if defined(PAGESIZE) // defined in limits.h
|
||||
page_size = PAGESIZE;
|
||||
#else // assume some POSIX OS
|
||||
page_size = sysconf(_SC_PAGESIZE);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Some systems (at least OS X) do not define MAP_ANONYMOUS yet and define
|
||||
// MAP_ANON which is deprecated
|
||||
#ifndef MAP_ANONYMOUS
|
||||
#define MAP_ANONYMOUS MAP_ANON
|
||||
#endif
|
||||
|
||||
void *PosixLockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess)
|
||||
{
|
||||
void *addr;
|
||||
len = align_up(len, page_size);
|
||||
addr = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
|
||||
if (addr == MAP_FAILED) {
|
||||
return nullptr;
|
||||
}
|
||||
if (addr) {
|
||||
*lockingSuccess = mlock(addr, len) == 0;
|
||||
#if defined(MADV_DONTDUMP) // Linux
|
||||
madvise(addr, len, MADV_DONTDUMP);
|
||||
#elif defined(MADV_NOCORE) // FreeBSD
|
||||
madvise(addr, len, MADV_NOCORE);
|
||||
#endif
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
void PosixLockedPageAllocator::FreeLocked(void* addr, size_t len)
|
||||
{
|
||||
len = align_up(len, page_size);
|
||||
memory_cleanse(addr, len);
|
||||
munlock(addr, len);
|
||||
munmap(addr, len);
|
||||
}
|
||||
size_t PosixLockedPageAllocator::GetLimit()
|
||||
{
|
||||
#ifdef RLIMIT_MEMLOCK
|
||||
struct rlimit rlim;
|
||||
if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) {
|
||||
if (rlim.rlim_cur != RLIM_INFINITY) {
|
||||
return rlim.rlim_cur;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return std::numeric_limits<size_t>::max();
|
||||
}
|
||||
#endif
|
||||
|
||||
/*******************************************************************************/
|
||||
// Implementation: LockedPool
|
||||
|
||||
LockedPool::LockedPool(std::unique_ptr<LockedPageAllocator> allocator_in, LockingFailed_Callback lf_cb_in):
|
||||
allocator(std::move(allocator_in)), lf_cb(lf_cb_in), cumulative_bytes_locked(0)
|
||||
{
|
||||
}
|
||||
|
||||
LockedPool::~LockedPool()
|
||||
{
|
||||
}
|
||||
void* LockedPool::alloc(size_t size)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
// Don't handle impossible sizes
|
||||
if (size == 0 || size > ARENA_SIZE)
|
||||
return nullptr;
|
||||
|
||||
// Try allocating from each current arena
|
||||
for (auto &arena: arenas) {
|
||||
void *addr = arena.alloc(size);
|
||||
if (addr) {
|
||||
return addr;
|
||||
}
|
||||
}
|
||||
// If that fails, create a new one
|
||||
if (new_arena(ARENA_SIZE, ARENA_ALIGN)) {
|
||||
return arenas.back().alloc(size);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void LockedPool::free(void *ptr)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
// TODO we can do better than this linear search by keeping a map of arena
|
||||
// extents to arena, and looking up the address.
|
||||
for (auto &arena: arenas) {
|
||||
if (arena.addressInArena(ptr)) {
|
||||
arena.free(ptr);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw std::runtime_error("LockedPool: invalid address not pointing to any arena");
|
||||
}
|
||||
|
||||
LockedPool::Stats LockedPool::stats() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
LockedPool::Stats r{0, 0, 0, cumulative_bytes_locked, 0, 0};
|
||||
for (const auto &arena: arenas) {
|
||||
Arena::Stats i = arena.stats();
|
||||
r.used += i.used;
|
||||
r.free += i.free;
|
||||
r.total += i.total;
|
||||
r.chunks_used += i.chunks_used;
|
||||
r.chunks_free += i.chunks_free;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
bool LockedPool::new_arena(size_t size, size_t align)
|
||||
{
|
||||
bool locked;
|
||||
// If this is the first arena, handle this specially: Cap the upper size
|
||||
// by the process limit. This makes sure that the first arena will at least
|
||||
// be locked. An exception to this is if the process limit is 0:
|
||||
// in this case no memory can be locked at all so we'll skip past this logic.
|
||||
if (arenas.empty()) {
|
||||
size_t limit = allocator->GetLimit();
|
||||
if (limit > 0) {
|
||||
size = std::min(size, limit);
|
||||
}
|
||||
}
|
||||
void *addr = allocator->AllocateLocked(size, &locked);
|
||||
if (!addr) {
|
||||
return false;
|
||||
}
|
||||
if (locked) {
|
||||
cumulative_bytes_locked += size;
|
||||
} else if (lf_cb) { // Call the locking-failed callback if locking failed
|
||||
if (!lf_cb()) { // If the callback returns false, free the memory and fail, otherwise consider the user warned and proceed.
|
||||
allocator->FreeLocked(addr, size);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
arenas.emplace_back(allocator.get(), addr, size, align);
|
||||
return true;
|
||||
}
|
||||
|
||||
LockedPool::LockedPageArena::LockedPageArena(LockedPageAllocator *allocator_in, void *base_in, size_t size_in, size_t align_in):
|
||||
Arena(base_in, size_in, align_in), base(base_in), size(size_in), allocator(allocator_in)
|
||||
{
|
||||
}
|
||||
LockedPool::LockedPageArena::~LockedPageArena()
|
||||
{
|
||||
allocator->FreeLocked(base, size);
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
// Implementation: LockedPoolManager
|
||||
//
|
||||
LockedPoolManager::LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator_in):
|
||||
LockedPool(std::move(allocator_in), &LockedPoolManager::LockingFailed)
|
||||
{
|
||||
}
|
||||
|
||||
bool LockedPoolManager::LockingFailed()
|
||||
{
|
||||
// TODO: log something but how? without including util.h
|
||||
return true;
|
||||
}
|
||||
|
||||
void LockedPoolManager::CreateInstance()
|
||||
{
|
||||
// Using a local static instance guarantees that the object is initialized
|
||||
// when it's first needed and also deinitialized after all objects that use
|
||||
// it are done with it. I can think of one unlikely scenario where we may
|
||||
// have a static deinitialization order/problem, but the check in
|
||||
// LockedPoolManagerBase's destructor helps us detect if that ever happens.
|
||||
#ifdef WIN32
|
||||
std::unique_ptr<LockedPageAllocator> allocator(new Win32LockedPageAllocator());
|
||||
#else
|
||||
std::unique_ptr<LockedPageAllocator> allocator(new PosixLockedPageAllocator());
|
||||
#endif
|
||||
static LockedPoolManager instance(std::move(allocator));
|
||||
LockedPoolManager::_instance = &instance;
|
||||
}
|
||||
241
src/support/lockedpool.h
Normal file
241
src/support/lockedpool.h
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
||||
// Copyright (c) 2014-2020 The DigiByte Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef DIGIBYTE_SUPPORT_LOCKEDPOOL_H
|
||||
#define DIGIBYTE_SUPPORT_LOCKEDPOOL_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
/**
|
||||
* OS-dependent allocation and deallocation of locked/pinned memory pages.
|
||||
* Abstract base class.
|
||||
*/
|
||||
class LockedPageAllocator
|
||||
{
|
||||
public:
|
||||
virtual ~LockedPageAllocator() {}
|
||||
/** Allocate and lock memory pages.
|
||||
* If len is not a multiple of the system page size, it is rounded up.
|
||||
* Returns nullptr in case of allocation failure.
|
||||
*
|
||||
* If locking the memory pages could not be accomplished it will still
|
||||
* return the memory, however the lockingSuccess flag will be false.
|
||||
* lockingSuccess is undefined if the allocation fails.
|
||||
*/
|
||||
virtual void* AllocateLocked(size_t len, bool *lockingSuccess) = 0;
|
||||
|
||||
/** Unlock and free memory pages.
|
||||
* Clear the memory before unlocking.
|
||||
*/
|
||||
virtual void FreeLocked(void* addr, size_t len) = 0;
|
||||
|
||||
/** Get the total limit on the amount of memory that may be locked by this
|
||||
* process, in bytes. Return size_t max if there is no limit or the limit
|
||||
* is unknown. Return 0 if no memory can be locked at all.
|
||||
*/
|
||||
virtual size_t GetLimit() = 0;
|
||||
};
|
||||
|
||||
/* An arena manages a contiguous region of memory by dividing it into
|
||||
* chunks.
|
||||
*/
|
||||
class Arena
|
||||
{
|
||||
public:
|
||||
Arena(void *base, size_t size, size_t alignment);
|
||||
virtual ~Arena();
|
||||
|
||||
Arena(const Arena& other) = delete; // non construction-copyable
|
||||
Arena& operator=(const Arena&) = delete; // non copyable
|
||||
|
||||
/** Memory statistics. */
|
||||
struct Stats
|
||||
{
|
||||
size_t used;
|
||||
size_t free;
|
||||
size_t total;
|
||||
size_t chunks_used;
|
||||
size_t chunks_free;
|
||||
};
|
||||
|
||||
/** Allocate size bytes from this arena.
|
||||
* Returns pointer on success, or 0 if memory is full or
|
||||
* the application tried to allocate 0 bytes.
|
||||
*/
|
||||
void* alloc(size_t size);
|
||||
|
||||
/** Free a previously allocated chunk of memory.
|
||||
* Freeing the zero pointer has no effect.
|
||||
* Raises std::runtime_error in case of error.
|
||||
*/
|
||||
void free(void *ptr);
|
||||
|
||||
/** Get arena usage statistics */
|
||||
Stats stats() const;
|
||||
|
||||
#ifdef ARENA_DEBUG
|
||||
void walk() const;
|
||||
#endif
|
||||
|
||||
/** Return whether a pointer points inside this arena.
|
||||
* This returns base <= ptr < (base+size) so only use it for (inclusive)
|
||||
* chunk starting addresses.
|
||||
*/
|
||||
bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
|
||||
private:
|
||||
typedef std::multimap<size_t, char*> SizeToChunkSortedMap;
|
||||
/** Map to enable O(log(n)) best-fit allocation, as it's sorted by size */
|
||||
SizeToChunkSortedMap size_to_free_chunk;
|
||||
|
||||
typedef std::unordered_map<char*, SizeToChunkSortedMap::const_iterator> ChunkToSizeMap;
|
||||
/** Map from begin of free chunk to its node in size_to_free_chunk */
|
||||
ChunkToSizeMap chunks_free;
|
||||
/** Map from end of free chunk to its node in size_to_free_chunk */
|
||||
ChunkToSizeMap chunks_free_end;
|
||||
|
||||
/** Map from begin of used chunk to its size */
|
||||
std::unordered_map<char*, size_t> chunks_used;
|
||||
|
||||
/** Base address of arena */
|
||||
char* base;
|
||||
/** End address of arena */
|
||||
char* end;
|
||||
/** Minimum chunk alignment */
|
||||
size_t alignment;
|
||||
};
|
||||
|
||||
/** Pool for locked memory chunks.
|
||||
*
|
||||
* To avoid sensitive key data from being swapped to disk, the memory in this pool
|
||||
* is locked/pinned.
|
||||
*
|
||||
* An arena manages a contiguous region of memory. The pool starts out with one arena
|
||||
* but can grow to multiple arenas if the need arises.
|
||||
*
|
||||
* Unlike a normal C heap, the administrative structures are separate from the managed
|
||||
* memory. This has been done as the sizes and bases of objects are not in themselves sensitive
|
||||
* information, as to conserve precious locked memory. In some operating systems
|
||||
* the amount of memory that can be locked is small.
|
||||
*/
|
||||
class LockedPool
|
||||
{
|
||||
public:
|
||||
/** Size of one arena of locked memory. This is a compromise.
|
||||
* Do not set this too low, as managing many arenas will increase
|
||||
* allocation and deallocation overhead. Setting it too high allocates
|
||||
* more locked memory from the OS than strictly necessary.
|
||||
*/
|
||||
static const size_t ARENA_SIZE = 256*1024;
|
||||
/** Chunk alignment. Another compromise. Setting this too high will waste
|
||||
* memory, setting it too low will facilitate fragmentation.
|
||||
*/
|
||||
static const size_t ARENA_ALIGN = 16;
|
||||
|
||||
/** Callback when allocation succeeds but locking fails.
|
||||
*/
|
||||
typedef bool (*LockingFailed_Callback)();
|
||||
|
||||
/** Memory statistics. */
|
||||
struct Stats
|
||||
{
|
||||
size_t used;
|
||||
size_t free;
|
||||
size_t total;
|
||||
size_t locked;
|
||||
size_t chunks_used;
|
||||
size_t chunks_free;
|
||||
};
|
||||
|
||||
/** Create a new LockedPool. This takes ownership of the MemoryPageLocker,
|
||||
* you can only instantiate this with LockedPool(std::move(...)).
|
||||
*
|
||||
* The second argument is an optional callback when locking a newly allocated arena failed.
|
||||
* If this callback is provided and returns false, the allocation fails (hard fail), if
|
||||
* it returns true the allocation proceeds, but it could warn.
|
||||
*/
|
||||
explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = nullptr);
|
||||
~LockedPool();
|
||||
|
||||
LockedPool(const LockedPool& other) = delete; // non construction-copyable
|
||||
LockedPool& operator=(const LockedPool&) = delete; // non copyable
|
||||
|
||||
/** Allocate size bytes from this arena.
|
||||
* Returns pointer on success, or 0 if memory is full or
|
||||
* the application tried to allocate 0 bytes.
|
||||
*/
|
||||
void* alloc(size_t size);
|
||||
|
||||
/** Free a previously allocated chunk of memory.
|
||||
* Freeing the zero pointer has no effect.
|
||||
* Raises std::runtime_error in case of error.
|
||||
*/
|
||||
void free(void *ptr);
|
||||
|
||||
/** Get pool usage statistics */
|
||||
Stats stats() const;
|
||||
private:
|
||||
std::unique_ptr<LockedPageAllocator> allocator;
|
||||
|
||||
/** Create an arena from locked pages */
|
||||
class LockedPageArena: public Arena
|
||||
{
|
||||
public:
|
||||
LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, size_t size, size_t align);
|
||||
~LockedPageArena();
|
||||
private:
|
||||
void *base;
|
||||
size_t size;
|
||||
LockedPageAllocator *allocator;
|
||||
};
|
||||
|
||||
bool new_arena(size_t size, size_t align);
|
||||
|
||||
std::list<LockedPageArena> arenas;
|
||||
LockingFailed_Callback lf_cb;
|
||||
size_t cumulative_bytes_locked;
|
||||
/** Mutex protects access to this pool's data structures, including arenas.
|
||||
*/
|
||||
mutable std::mutex mutex;
|
||||
};
|
||||
|
||||
/**
|
||||
* Singleton class to keep track of locked (ie, non-swappable) memory, for use in
|
||||
* std::allocator templates.
|
||||
*
|
||||
* Some implementations of the STL allocate memory in some constructors (i.e., see
|
||||
* MSVC's vector<T> implementation where it allocates 1 byte of memory in the allocator.)
|
||||
* Due to the unpredictable order of static initializers, we have to make sure the
|
||||
* LockedPoolManager instance exists before any other STL-based objects that use
|
||||
* secure_allocator are created. So instead of having LockedPoolManager also be
|
||||
* static-initialized, it is created on demand.
|
||||
*/
|
||||
class LockedPoolManager : public LockedPool
|
||||
{
|
||||
public:
|
||||
/** Return the current instance, or create it once */
|
||||
static LockedPoolManager& Instance()
|
||||
{
|
||||
static std::once_flag init_flag;
|
||||
std::call_once(init_flag, LockedPoolManager::CreateInstance);
|
||||
return *LockedPoolManager::_instance;
|
||||
}
|
||||
|
||||
private:
|
||||
explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
|
||||
|
||||
/** Create a new LockedPoolManager specialized to the OS */
|
||||
static void CreateInstance();
|
||||
/** Called when locking fails, warn the user here */
|
||||
static bool LockingFailed();
|
||||
|
||||
static LockedPoolManager* _instance;
|
||||
};
|
||||
|
||||
#endif // DIGIBYTE_SUPPORT_LOCKEDPOOL_H
|
||||
Loading…
Reference in a new issue