Skip to content

Commit 6ca6470

Browse files
committed
allocators: split allocators and pagelocker
>>> backports bitcoin/bitcoin@d7d187e Pagelocker is only needed for secure (usually wallet) operations, so don't make the zero-after-free allocator depend on it.
1 parent b993c56 commit 6ca6470

15 files changed

+135
-112
lines changed

CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ target_include_directories(ZEROCOIN_A PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src
374374
)
375375

376376
set(COMMON_SOURCES
377-
./src/allocators.cpp
378377
./src/base58.cpp
379378
./src/bip38.cpp
380379
./src/consensus/params.cpp
@@ -421,7 +420,6 @@ target_include_directories(COMMON_A PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src
421420
)
422421

423422
set(UTIL_SOURCES
424-
./src/allocators.cpp
425423
./src/compat/strnlen.cpp
426424
./src/compat/glibc_sanity.cpp
427425
./src/compat/glibcxx_sanity.cpp
@@ -441,6 +439,7 @@ set(UTIL_SOURCES
441439
./src/utilstrencodings.cpp
442440
./src/utilmoneystr.cpp
443441
./src/utiltime.cpp
442+
./src/support/pagelocker.cpp
444443
./src/support/cleanse.cpp
445444
)
446445
add_library(UTIL_A STATIC ${BitcoinHeaders} ${UTIL_SOURCES})

src/Makefile.am

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ BITCOIN_CORE_H = \
148148
activemasternode.h \
149149
addrdb.h \
150150
addrman.h \
151-
allocators.h \
152151
arith_uint256.h \
153152
amount.h \
154153
base58.h \
@@ -260,7 +259,10 @@ BITCOIN_CORE_H = \
260259
stakeinput.h \
261260
script/ismine.h \
262261
streams.h \
262+
support/allocators/secure.h \
263+
support/allocators/zeroafterfree.h \
263264
support/cleanse.h \
265+
support/pagelocker.h \
264266
sync.h \
265267
threadsafety.h \
266268
threadinterrupt.h \
@@ -485,7 +487,6 @@ endif
485487
libbitcoin_common_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
486488
libbitcoin_common_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
487489
libbitcoin_common_a_SOURCES = \
488-
allocators.cpp \
489490
base58.cpp \
490491
bip38.cpp \
491492
chainparams.cpp \
@@ -526,7 +527,6 @@ libbitcoin_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
526527
libbitcoin_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
527528
libbitcoin_util_a_SOURCES = \
528529
arith_uint256.cpp \
529-
allocators.cpp \
530530
chainparamsbase.cpp \
531531
clientversion.cpp \
532532
compat/glibc_sanity.cpp \
@@ -538,6 +538,7 @@ libbitcoin_util_a_SOURCES = \
538538
random.cpp \
539539
rpc/protocol.cpp \
540540
support/cleanse.cpp \
541+
support/pagelocker.cpp \
541542
sync.cpp \
542543
threadinterrupt.cpp \
543544
uint256.cpp \
@@ -670,7 +671,6 @@ if BUILD_BITCOIN_LIBS
670671
include_HEADERS = script/bitcoinconsensus.h
671672
libbitcoinconsensus_la_SOURCES = \
672673
arith_uint256.cpp \
673-
allocators.cpp \
674674
primitives/transaction.cpp \
675675
crypto/hmac_sha512.cpp \
676676
crypto/scrypt.cpp \

src/crypter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#ifndef BITCOIN_CRYPTER_H
77
#define BITCOIN_CRYPTER_H
88

9-
#include "allocators.h"
109
#include "keystore.h"
1110
#include "serialize.h"
1211
#include "streams.h"
12+
#include "support/allocators/zeroafterfree.h"
1313

1414
class uint256;
1515

src/key.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#ifndef PIVX_KEY_H
88
#define PIVX_KEY_H
99

10-
#include "allocators.h"
1110
#include "serialize.h"
11+
#include "support/allocators/secure.h"
1212
#include "uint256.h"
1313
#include "pubkey.h"
1414

@@ -20,7 +20,6 @@ class CPubKey;
2020
struct CExtPubKey;
2121

2222
/**
23-
* secure_allocator is defined in allocators.h
2423
* CPrivKey is a serialized private key, with all parameters included
2524
* (PRIVATE_KEY_SIZE bytes)
2625
*/

src/qt/askpassphrasedialog.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include <QDialog>
1010
#include "qt/pivx/prunnable.h"
11-
#include "allocators.h"
11+
#include "support/allocators/secure.h"
1212
#include <QCheckBox>
1313

1414
class WalletModel;

src/qt/walletmodel.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
#include "interfaces/wallet.h"
1515

16-
#include "allocators.h" /* for SecureString */
1716
#include "operationresult.h"
17+
#include "support/allocators/zeroafterfree.h"
1818
#include "wallet/wallet.h"
1919
#include "pairresult.h"
2020

src/sapling/zip32.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#ifndef PIVX_ZIP32_H
66
#define PIVX_ZIP32_H
77

8-
#include "serialize.h"
9-
#include "allocators.h"
108
#include "blob_uint256.h"
119
#include "key.h"
12-
#include "uint256.h"
1310
#include "sapling/address.h"
11+
#include "serialize.h"
12+
#include "support/allocators/zeroafterfree.h"
13+
#include "uint256.h"
1414

1515
#include <boost/optional.hpp>
1616

src/streams.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#ifndef BITCOIN_STREAMS_H
88
#define BITCOIN_STREAMS_H
99

10-
#include "allocators.h"
1110
#include "serialize.h"
11+
#include "support/allocators/zeroafterfree.h"
1212

1313
#include <algorithm>
1414
#include <assert.h>

src/support/allocators/secure.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2013 The Bitcoin developers
3+
// Distributed under the MIT/X11 software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
7+
#ifndef BITCOIN_ALLOCATORS_SECURE_H
8+
#define BITCOIN_ALLOCATORS_SECURE_H
9+
10+
#include "support/pagelocker.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+
secure_allocator() throw() {}
30+
secure_allocator(const secure_allocator& a) throw() : base(a) {}
31+
template <typename U>
32+
secure_allocator(const secure_allocator<U>& a) throw() : base(a)
33+
{
34+
}
35+
~secure_allocator() throw() {}
36+
template <typename _Other>
37+
struct rebind {
38+
typedef secure_allocator<_Other> other;
39+
};
40+
41+
T* allocate(std::size_t n, const void* hint = 0)
42+
{
43+
T* p;
44+
p = std::allocator<T>::allocate(n, hint);
45+
if (p != NULL)
46+
LockedPageManager::Instance().LockRange(p, sizeof(T) * n);
47+
return p;
48+
}
49+
50+
void deallocate(T* p, std::size_t n)
51+
{
52+
if (p != NULL) {
53+
memory_cleanse(p, sizeof(T) * n);
54+
LockedPageManager::Instance().UnlockRange(p, sizeof(T) * n);
55+
}
56+
std::allocator<T>::deallocate(p, n);
57+
}
58+
};
59+
60+
// This is exactly like std::string, but with a custom allocator.
61+
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
62+
63+
#endif // BITCOIN_ALLOCATORS_SECURE_H
+48
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 src/support/pagelocker.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// Distributed under the MIT/X11 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/pivx-config.h"
9+
#endif
610

711
#ifdef WIN32
812
#ifdef _WIN32_WINNT

src/allocators.h src/support/pagelocker.h

+3-93
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
// Distributed under the MIT/X11 software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
#ifndef BITCOIN_ALLOCATORS_H
7-
#define BITCOIN_ALLOCATORS_H
6+
#ifndef BITCOIN_ALLOCATORS_PAGELOCKER_H
7+
#define BITCOIN_ALLOCATORS_PAGELOCKER_H
88

99
#include "support/cleanse.h"
1010

1111
#include <map>
12-
#include <string.h>
13-
#include <string>
14-
#include <vector>
1512

1613
#include <boost/thread/mutex.hpp>
1714
#include <boost/thread/once.hpp>
@@ -160,91 +157,4 @@ class LockedPageManager : public LockedPageManagerBase<MemoryPageLocker>
160157
static boost::once_flag init_flag;
161158
};
162159

163-
//
164-
// Allocator that locks its contents from being paged
165-
// out of memory and clears its contents before deletion.
166-
//
167-
template <typename T>
168-
struct secure_allocator : public std::allocator<T> {
169-
// MSVC8 default copy constructor is broken
170-
typedef std::allocator<T> base;
171-
typedef typename base::size_type size_type;
172-
typedef typename base::difference_type difference_type;
173-
typedef typename base::pointer pointer;
174-
typedef typename base::const_pointer const_pointer;
175-
typedef typename base::reference reference;
176-
typedef typename base::const_reference const_reference;
177-
typedef typename base::value_type value_type;
178-
secure_allocator() throw() {}
179-
secure_allocator(const secure_allocator& a) throw() : base(a) {}
180-
template <typename U>
181-
secure_allocator(const secure_allocator<U>& a) throw() : base(a)
182-
{
183-
}
184-
~secure_allocator() throw() {}
185-
template <typename _Other>
186-
struct rebind {
187-
typedef secure_allocator<_Other> other;
188-
};
189-
190-
T* allocate(std::size_t n, const void* hint = 0)
191-
{
192-
T* p;
193-
p = std::allocator<T>::allocate(n, hint);
194-
if (p != NULL)
195-
LockedPageManager::Instance().LockRange(p, sizeof(T) * n);
196-
return p;
197-
}
198-
199-
void deallocate(T* p, std::size_t n)
200-
{
201-
if (p != NULL) {
202-
memory_cleanse(p, sizeof(T) * n);
203-
LockedPageManager::Instance().UnlockRange(p, sizeof(T) * n);
204-
}
205-
std::allocator<T>::deallocate(p, n);
206-
}
207-
};
208-
209-
210-
//
211-
// Allocator that clears its contents before deletion.
212-
//
213-
template <typename T>
214-
struct zero_after_free_allocator : public std::allocator<T> {
215-
// MSVC8 default copy constructor is broken
216-
typedef std::allocator<T> base;
217-
typedef typename base::size_type size_type;
218-
typedef typename base::difference_type difference_type;
219-
typedef typename base::pointer pointer;
220-
typedef typename base::const_pointer const_pointer;
221-
typedef typename base::reference reference;
222-
typedef typename base::const_reference const_reference;
223-
typedef typename base::value_type value_type;
224-
zero_after_free_allocator() throw() {}
225-
zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {}
226-
template <typename U>
227-
zero_after_free_allocator(const zero_after_free_allocator<U>& a) throw() : base(a)
228-
{
229-
}
230-
~zero_after_free_allocator() throw() {}
231-
template <typename _Other>
232-
struct rebind {
233-
typedef zero_after_free_allocator<_Other> other;
234-
};
235-
236-
void deallocate(T* p, std::size_t n)
237-
{
238-
if (p != NULL)
239-
memory_cleanse(p, sizeof(T) * n);
240-
std::allocator<T>::deallocate(p, n);
241-
}
242-
};
243-
244-
// This is exactly like std::string, but with a custom allocator.
245-
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
246-
247-
// Byte-vector that clears its contents before deletion.
248-
typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;
249-
250-
#endif // BITCOIN_ALLOCATORS_H
160+
#endif // BITCOIN_ALLOCATORS_PAGELOCKER_H

src/test/allocator_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "util.h"
66

7-
#include "allocators.h"
7+
#include "support/allocators/zeroafterfree.h"
88
#include "test/test_pivx.h"
99

1010
#include <boost/test/unit_test.hpp>

0 commit comments

Comments
 (0)