Skip to content

Commit 3811a50

Browse files
committed
Merge #5810: MOVEONLY-ISH: allocators: split allocators and pagelocker
d7d187e allocators: split allocators and pagelocker (Cory Fields)
2 parents c7abfa5 + d7d187e commit 3811a50

File tree

12 files changed

+129
-102
lines changed

12 files changed

+129
-102
lines changed

src/Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ endif
7272
BITCOIN_CORE_H = \
7373
addrman.h \
7474
alert.h \
75-
allocators.h \
7675
amount.h \
7776
arith_uint256.h \
7877
base58.h \
@@ -123,7 +122,10 @@ BITCOIN_CORE_H = \
123122
script/standard.h \
124123
serialize.h \
125124
streams.h \
125+
support/allocators/secure.h \
126+
support/allocators/zeroafterfree.h \
126127
support/cleanse.h \
128+
support/pagelocker.h \
127129
sync.h \
128130
threadsafety.h \
129131
timedata.h \
@@ -233,7 +235,6 @@ univalue_libbitcoin_univalue_a_SOURCES = \
233235
# common: shared between bitcoind, and bitcoin-qt and non-server tools
234236
libbitcoin_common_a_CPPFLAGS = $(BITCOIN_INCLUDES)
235237
libbitcoin_common_a_SOURCES = \
236-
allocators.cpp \
237238
arith_uint256.cpp \
238239
amount.cpp \
239240
base58.cpp \
@@ -264,6 +265,7 @@ libbitcoin_common_a_SOURCES = \
264265
# backward-compatibility objects and their sanity checks are linked.
265266
libbitcoin_util_a_CPPFLAGS = $(BITCOIN_INCLUDES)
266267
libbitcoin_util_a_SOURCES = \
268+
support/pagelocker.cpp \
267269
chainparamsbase.cpp \
268270
clientversion.cpp \
269271
compat/glibc_sanity.cpp \

src/base58.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "pubkey.h"
2020
#include "script/script.h"
2121
#include "script/standard.h"
22+
#include "support/allocators/zeroafterfree.h"
2223

2324
#include <string>
2425
#include <vector>

src/crypter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#ifndef BITCOIN_CRYPTER_H
66
#define BITCOIN_CRYPTER_H
77

8-
#include "allocators.h"
98
#include "keystore.h"
109
#include "serialize.h"
10+
#include "support/allocators/secure.h"
1111

1212
class uint256;
1313

src/key.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#ifndef BITCOIN_KEY_H
77
#define BITCOIN_KEY_H
88

9-
#include "allocators.h"
109
#include "serialize.h"
10+
#include "support/allocators/secure.h"
1111
#include "uint256.h"
1212

1313
#include <stdexcept>

src/qt/askpassphrasedialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "guiconstants.h"
99
#include "walletmodel.h"
1010

11-
#include "allocators.h"
11+
#include "support/allocators/secure.h"
1212

1313
#include <QKeyEvent>
1414
#include <QMessageBox>

src/qt/walletmodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "paymentrequestplus.h"
99
#include "walletmodeltransaction.h"
1010

11-
#include "allocators.h" /* for SecureString */
11+
#include "support/allocators/secure.h"
1212

1313
#include <map>
1414
#include <vector>

src/streams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef BITCOIN_STREAMS_H
77
#define BITCOIN_STREAMS_H
88

9-
#include "allocators.h"
9+
#include "support/allocators/zeroafterfree.h"
1010
#include "serialize.h"
1111

1212
#include <algorithm>

src/support/allocators/secure.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2013 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_ALLOCATORS_SECURE_H
7+
#define BITCOIN_ALLOCATORS_SECURE_H
8+
9+
#include "support/pagelocker.h"
10+
11+
#include <string>
12+
13+
//
14+
// Allocator that locks its contents from being paged
15+
// out of memory and clears its contents before deletion.
16+
//
17+
template <typename T>
18+
struct secure_allocator : public std::allocator<T> {
19+
// MSVC8 default copy constructor is broken
20+
typedef std::allocator<T> base;
21+
typedef typename base::size_type size_type;
22+
typedef typename base::difference_type difference_type;
23+
typedef typename base::pointer pointer;
24+
typedef typename base::const_pointer const_pointer;
25+
typedef typename base::reference reference;
26+
typedef typename base::const_reference const_reference;
27+
typedef typename base::value_type value_type;
28+
secure_allocator() throw() {}
29+
secure_allocator(const secure_allocator& a) throw() : base(a) {}
30+
template <typename U>
31+
secure_allocator(const secure_allocator<U>& a) throw() : base(a)
32+
{
33+
}
34+
~secure_allocator() throw() {}
35+
template <typename _Other>
36+
struct rebind {
37+
typedef secure_allocator<_Other> other;
38+
};
39+
40+
T* allocate(std::size_t n, const void* hint = 0)
41+
{
42+
T* p;
43+
p = std::allocator<T>::allocate(n, hint);
44+
if (p != NULL)
45+
LockedPageManager::Instance().LockRange(p, sizeof(T) * n);
46+
return p;
47+
}
48+
49+
void deallocate(T* p, std::size_t n)
50+
{
51+
if (p != NULL) {
52+
memory_cleanse(p, sizeof(T) * n);
53+
LockedPageManager::Instance().UnlockRange(p, sizeof(T) * n);
54+
}
55+
std::allocator<T>::deallocate(p, n);
56+
}
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_ALLOCATORS_SECURE_H
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2013 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_ALLOCATORS_ZEROAFTERFREE_H
7+
#define BITCOIN_ALLOCATORS_ZEROAFTERFREE_H
8+
9+
#include "support/cleanse.h"
10+
11+
#include <memory>
12+
#include <vector>
13+
14+
template <typename T>
15+
struct zero_after_free_allocator : public std::allocator<T> {
16+
// MSVC8 default copy constructor is broken
17+
typedef std::allocator<T> base;
18+
typedef typename base::size_type size_type;
19+
typedef typename base::difference_type difference_type;
20+
typedef typename base::pointer pointer;
21+
typedef typename base::const_pointer const_pointer;
22+
typedef typename base::reference reference;
23+
typedef typename base::const_reference const_reference;
24+
typedef typename base::value_type value_type;
25+
zero_after_free_allocator() throw() {}
26+
zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {}
27+
template <typename U>
28+
zero_after_free_allocator(const zero_after_free_allocator<U>& a) throw() : base(a)
29+
{
30+
}
31+
~zero_after_free_allocator() throw() {}
32+
template <typename _Other>
33+
struct rebind {
34+
typedef zero_after_free_allocator<_Other> other;
35+
};
36+
37+
void deallocate(T* p, std::size_t n)
38+
{
39+
if (p != NULL)
40+
memory_cleanse(p, sizeof(T) * n);
41+
std::allocator<T>::deallocate(p, n);
42+
}
43+
};
44+
45+
// Byte-vector that clears its contents before deletion.
46+
typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;
47+
48+
#endif // BITCOIN_ALLOCATORS_ZEROAFTERFREE_H

src/allocators.cpp renamed to src/support/pagelocker.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include "allocators.h"
5+
#include "support/pagelocker.h"
6+
7+
#if defined(HAVE_CONFIG_H)
8+
#include "config/bitcoin-config.h"
9+
#endif
610

711
#ifdef WIN32
812
#ifdef _WIN32_WINNT

0 commit comments

Comments
 (0)