Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null pointer of pindexPrev crash. #4995

Closed
wants to merge 2 commits into from
Closed

null pointer of pindexPrev crash. #4995

wants to merge 2 commits into from

Conversation

imharrywu
Copy link
Contributor

i use bitcoind to mine, and if BitcoinMiner() is started before ConnectTip(), pindexPrev will be null pointer, and bitcoind will crash.
maybe this fix is not the optimized fix, although i have test it on my gentoobox and it seems working now.

here are logs and stack:

Starting program: /root/harrywu/build/tmp/native/run-0.9.99/bitcoind -datadir=/root/harrywu/build/tmp/native/run/data -conf=bitcoin.conf -server -gen -debug -checkpoints=0 -txindex=1 -reindex
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/libthread_db.so.1".
[New Thread 0xb7cc8b40 (LWP 25005)]
[New Thread 0xb72ffb40 (LWP 25006)]
[New Thread 0xb6afeb40 (LWP 25007)]
[New Thread 0xb60ffb40 (LWP 25008)]
[New Thread 0xb4ac0b40 (LWP 25009)]
[New Thread 0xb40d6b40 (LWP 25010)]
[New Thread 0xb38d5b40 (LWP 25011)]
[New Thread 0xb2effb40 (LWP 25012)]
[New Thread 0xb24ffb40 (LWP 25013)]
[New Thread 0xb1cfeb40 (LWP 25014)]
[New Thread 0xb14fdb40 (LWP 25015)]
[New Thread 0xb0cfcb40 (LWP 25016)]
[New Thread 0xb04fbb40 (LWP 25017)]
[New Thread 0xafcfab40 (LWP 25018)]
[New Thread 0xaf4f9b40 (LWP 25019)]
[New Thread 0xaecf8b40 (LWP 25020)]
[New Thread 0xae4f7b40 (LWP 25021)]
[New Thread 0xadcf6b40 (LWP 25022)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xae4f7b40 (LWP 25021)]
IncrementExtraNonce (pblock=0xb2501828, pindexPrev=0x0, nExtraNonce=@0xae4f6fc0: 1) at miner.cpp:338
338 unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
(gdb) bt
#0 IncrementExtraNonce (pblock=0xb2501828, pindexPrev=0x0, nExtraNonce=@0xae4f6fc0: 1) at miner.cpp:338
#1 0x80110240 in BitcoinMiner (pwallet=0x807c6a20) at miner.cpp:461
#2 0x80111c73 in operator()<void ()(CWallet), boost::_bi::list0> (this=0x80e18360, f=, a=...)

at /root/harrywu/build/3rd/boost-1.53/include/boost/bind/bind.hpp:253

#3 operator() (this=0x80e1835c) at /root/harrywu/build/3rd/boost-1.53/include/boost/bind/bind_template.hpp:20
#4 boost::detail::thread_data<boost::_bi::bind_t<void, void ()(CWallet), boost::_bi::list1<boost::_bi::value<CWallet*> > > >::run (this=0x80e18240)

at /root/harrywu/build/3rd/boost-1.53/include/boost/thread/detail/thread.hpp:117

#5 0x80324ebe in thread_proxy ()
#6 0xb7cd3d9f in start_thread () from /lib/libpthread.so.0
#7 0xb7dd4c8e in clone () from /lib/libc.so.6

(gdb)

2014-09-28 08:30:17

2014-09-28 08:30:17 Bitcoin version v0.9.99.0-gae9966e (Sun, 21 Sep 2014 21:25:25 +0200)
2014-09-28 08:30:17 Using OpenSSL version OpenSSL 1.0.1g 7 Apr 2014
2014-09-28 08:30:17 Using BerkeleyDB version Berkeley DB 4.8.30: (April 9, 2010)
2014-09-28 08:30:17 Default data directory /root/.bitcoin
2014-09-28 08:30:17 Using data directory /root/harrywu/build/tmp/native/run/data
2014-09-28 08:30:17 Using config file /root/harrywu/build/tmp/native/run/data/bitcoin.conf
2014-09-28 08:30:17 Using at most 125 connections (1024 file descriptors available)
2014-09-28 08:30:17 Using 4 threads for script verification
2014-09-28 08:30:17 Using wallet wallet.dat
2014-09-28 08:30:17 init message: Verifying wallet...
2014-09-28 08:30:17 CDBEnv::Open : LogDir=/root/harrywu/build/tmp/native/run/data/database ErrorFile=/root/harrywu/build/tmp/native/run/data/db.log
2014-09-28 08:30:17 Bound to [::]:7333
2014-09-28 08:30:17 Bound to 0.0.0.0:7333
2014-09-28 08:30:17 init message: Loading block index...
2014-09-28 08:30:17 Wiping LevelDB in /root/harrywu/build/tmp/native/run/data/blocks/index
2014-09-28 08:30:17 Opening LevelDB in /root/harrywu/build/tmp/native/run/data/blocks/index
2014-09-28 08:30:17 Opened LevelDB successfully
2014-09-28 08:30:17 Wiping LevelDB in /root/harrywu/build/tmp/native/run/data/chainstate
2014-09-28 08:30:17 Opening LevelDB in /root/harrywu/build/tmp/native/run/data/chainstate
2014-09-28 08:30:17 Opened LevelDB successfully
2014-09-28 08:30:17 Initializing databases...
2014-09-28 08:30:17 init message: Verifying blocks...
2014-09-28 08:30:17 block index 10ms
2014-09-28 08:30:17 init message: Loading wallet...
2014-09-28 08:30:17 nFileVersion = 99900
2014-09-28 08:30:17 Keys: 105 plaintext, 0 encrypted, 105 w/ metadata, 105 total
2014-09-28 08:30:17 wallet 90ms
2014-09-28 08:30:17 init message: Loading addresses...
2014-09-28 08:30:17 Reindexing block file blk00000.dat...
2014-09-28 08:30:17 Loaded 1 addresses from peers.dat 0ms
2014-09-28 08:30:17 mapBlockIndex.size() = 0
2014-09-28 08:30:17 nBestHeight = -1
2014-09-28 08:30:17 setKeyPool.size() = 102
2014-09-28 08:30:17 mapWallet.size() = 2
2014-09-28 08:30:17 mapAddressBook.size() = 1
2014-09-28 08:30:17 Added connection peer=0
2014-09-28 08:30:17 ext-ip thread start
2014-09-28 08:30:17 dnsseed thread start
2014-09-28 08:30:17 net thread start
2014-09-28 08:30:17 addcon thread start
2014-09-28 08:30:17 opencon thread start
2014-09-28 08:30:17 trying connection 10.12.23.47:7333 lastseen=364414.7hrs
2014-09-28 08:30:17 msghand thread start
2014-09-28 08:30:17 dumpaddr thread start
2014-09-28 08:30:17 Allowing RPC connections from: 127.0.0.0/255.0.0.0 ::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
2014-09-28 08:30:17 Added connection peer=1
2014-09-28 08:30:17 send version message: version 70002, blocks=-1, us=0.0.0.0:0, peer=1
2014-09-28 08:30:17 Binding RPC on address ::1 port 8332 (IPv4+IPv6 bind any: 0)
2014-09-28 08:30:17 sending: version (102 bytes) peer=1
2014-09-28 08:30:17 Binding RPC on address 127.0.0.1 port 8332 (IPv4+IPv6 bind any: 0)
2014-09-28 08:30:17 - Load block from disk: 0.00ms [0.00s]
2014-09-28 08:30:17 - Connect total: 0.00ms [0.00s]
2014-09-28 08:30:17 init message: Done loading
2014-09-28 08:30:17 BitcoinMiner started
2014-09-28 08:30:17 - Flush: 0.00ms [0.00s]
2014-09-28 08:30:17 keypool reserve 4
2014-09-28 08:30:17 - Writing chainstate: 0.00ms [0.00s]
2014-09-28 08:30:17 UpdateTip: new best=0000000061b1aca334b059920fed7bace2336ea4d23d63428c7aee04da49e942 height=0 log2_work=32.000022 tx=1 date=2014-09-25 17:32:11 progress=0.000000
2014-09-28 08:30:17 - Connect postprocess: 0.00ms [0.00s]
2014-09-28 08:30:17 - Connect block: 0.00ms [0.00s]
2014-09-28 08:30:17 - Load block from disk: 0.00ms [0.00s]
2014-09-28 08:30:17 - Connect 1 transactions: 0.00ms (0.000ms/tx, 0.000ms/txin) [0.00s]
2014-09-28 08:30:17 - Verify 0 txins: 0.00ms (0.000ms/txin) [0.00s]
2014-09-28 08:30:17 Pre-allocating up to position 0x100000 in rev00000.dat
2014-09-28 08:30:17 - Index writing: 0.00ms [0.00s]
2014-09-28 08:30:17 - Callbacks: 0.00ms [0.00s]
2014-09-28 08:30:17 - Connect total: 0.00ms [0.00s]
2014-09-28 08:30:17 - Flush: 0.00ms [0.00s]
2014-09-28 08:30:17 - Writing chainstate: 0.00ms [0.00s]
2014-09-28 08:30:17 UpdateTip: new best=00000000923c935006272f11b781421bb9c40670aec27ed716eedb48b3790e38 height=1 log2_work=33.000022 tx=2 date=2014-09-26 02:34:14 progress=0.000000
2014-09-28 08:30:17 - Connect postprocess: 0.00ms [0.00s]
2014-09-28 08:30:17 - Connect block: 0.00ms [0.00s]
2014-09-28 08:30:17 CreateNewBlock(): total size 1000
2014-09-28 08:30:17 - Connect 1 transactions: 0.00ms (0.000ms/tx, 0.000ms/txin) [0.00s]
2014-09-28 08:30:17 - Verify 0 txins: 0.00ms (0.000ms/txin) [0.00s]

i use bitcoind to mine, and if BitcoinMiner() is started before ConnectTip(), pindexPrev will be null pointer, and bitcoind will crash.
maybe this fix is the optimized fix, although i have test it on my gentoobox and i seems working now.

here are logs and stack:

Starting program: /root/harrywu/build/tmp/native/run-0.9.99/bitcoind -datadir=/root/harrywu/build/tmp/native/run/data -conf=bitcoin.conf -server -gen -debug -checkpoints=0 -txindex=1 -reindex
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/libthread_db.so.1".
[New Thread 0xb7cc8b40 (LWP 25005)]
[New Thread 0xb72ffb40 (LWP 25006)]
[New Thread 0xb6afeb40 (LWP 25007)]
[New Thread 0xb60ffb40 (LWP 25008)]
[New Thread 0xb4ac0b40 (LWP 25009)]
[New Thread 0xb40d6b40 (LWP 25010)]
[New Thread 0xb38d5b40 (LWP 25011)]
[New Thread 0xb2effb40 (LWP 25012)]
[New Thread 0xb24ffb40 (LWP 25013)]
[New Thread 0xb1cfeb40 (LWP 25014)]
[New Thread 0xb14fdb40 (LWP 25015)]
[New Thread 0xb0cfcb40 (LWP 25016)]
[New Thread 0xb04fbb40 (LWP 25017)]
[New Thread 0xafcfab40 (LWP 25018)]
[New Thread 0xaf4f9b40 (LWP 25019)]
[New Thread 0xaecf8b40 (LWP 25020)]
[New Thread 0xae4f7b40 (LWP 25021)]
[New Thread 0xadcf6b40 (LWP 25022)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xae4f7b40 (LWP 25021)]
IncrementExtraNonce (pblock=0xb2501828, pindexPrev=0x0, nExtraNonce=@0xae4f6fc0: 1) at miner.cpp:338
338	    unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
(gdb) bt
#0  IncrementExtraNonce (pblock=0xb2501828, pindexPrev=0x0, nExtraNonce=@0xae4f6fc0: 1) at miner.cpp:338
#1  0x80110240 in BitcoinMiner (pwallet=0x807c6a20) at miner.cpp:461
#2  0x80111c73 in operator()<void (*)(CWallet*), boost::_bi::list0> (this=0x80e18360, f=<optimized out>, a=...)
    at /root/harrywu/build/3rd/boost-1.53/include/boost/bind/bind.hpp:253
#3  operator() (this=0x80e1835c) at /root/harrywu/build/3rd/boost-1.53/include/boost/bind/bind_template.hpp:20
#4  boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(CWallet*), boost::_bi::list1<boost::_bi::value<CWallet*> > > >::run (this=0x80e18240)
    at /root/harrywu/build/3rd/boost-1.53/include/boost/thread/detail/thread.hpp:117
#5  0x80324ebe in thread_proxy ()
#6  0xb7cd3d9f in start_thread () from /lib/libpthread.so.0
#7  0xb7dd4c8e in clone () from /lib/libc.so.6
(gdb) 



2014-09-28 08:30:17 



















2014-09-28 08:30:17 Bitcoin version v0.9.99.0-gae9966e (Sun, 21 Sep 2014 21:25:25 +0200)
2014-09-28 08:30:17 Using OpenSSL version OpenSSL 1.0.1g 7 Apr 2014
2014-09-28 08:30:17 Using BerkeleyDB version Berkeley DB 4.8.30: (April  9, 2010)
2014-09-28 08:30:17 Default data directory /root/.bitcoin
2014-09-28 08:30:17 Using data directory /root/harrywu/build/tmp/native/run/data
2014-09-28 08:30:17 Using config file /root/harrywu/build/tmp/native/run/data/bitcoin.conf
2014-09-28 08:30:17 Using at most 125 connections (1024 file descriptors available)
2014-09-28 08:30:17 Using 4 threads for script verification
2014-09-28 08:30:17 Using wallet wallet.dat
2014-09-28 08:30:17 init message: Verifying wallet...
2014-09-28 08:30:17 CDBEnv::Open : LogDir=/root/harrywu/build/tmp/native/run/data/database ErrorFile=/root/harrywu/build/tmp/native/run/data/db.log
2014-09-28 08:30:17 Bound to [::]:7333
2014-09-28 08:30:17 Bound to 0.0.0.0:7333
2014-09-28 08:30:17 init message: Loading block index...
2014-09-28 08:30:17 Wiping LevelDB in /root/harrywu/build/tmp/native/run/data/blocks/index
2014-09-28 08:30:17 Opening LevelDB in /root/harrywu/build/tmp/native/run/data/blocks/index
2014-09-28 08:30:17 Opened LevelDB successfully
2014-09-28 08:30:17 Wiping LevelDB in /root/harrywu/build/tmp/native/run/data/chainstate
2014-09-28 08:30:17 Opening LevelDB in /root/harrywu/build/tmp/native/run/data/chainstate
2014-09-28 08:30:17 Opened LevelDB successfully
2014-09-28 08:30:17 Initializing databases...
2014-09-28 08:30:17 init message: Verifying blocks...
2014-09-28 08:30:17  block index              10ms
2014-09-28 08:30:17 init message: Loading wallet...
2014-09-28 08:30:17 nFileVersion = 99900
2014-09-28 08:30:17 Keys: 105 plaintext, 0 encrypted, 105 w/ metadata, 105 total
2014-09-28 08:30:17  wallet                   90ms
2014-09-28 08:30:17 init message: Loading addresses...
2014-09-28 08:30:17 Reindexing block file blk00000.dat...
2014-09-28 08:30:17 Loaded 1 addresses from peers.dat  0ms
2014-09-28 08:30:17 mapBlockIndex.size() = 0
2014-09-28 08:30:17 nBestHeight = -1
2014-09-28 08:30:17 setKeyPool.size() = 102
2014-09-28 08:30:17 mapWallet.size() = 2
2014-09-28 08:30:17 mapAddressBook.size() = 1
2014-09-28 08:30:17 Added connection peer=0
2014-09-28 08:30:17 ext-ip thread start
2014-09-28 08:30:17 dnsseed thread start
2014-09-28 08:30:17 net thread start
2014-09-28 08:30:17 addcon thread start
2014-09-28 08:30:17 opencon thread start
2014-09-28 08:30:17 trying connection 10.12.23.47:7333 lastseen=364414.7hrs
2014-09-28 08:30:17 msghand thread start
2014-09-28 08:30:17 dumpaddr thread start
2014-09-28 08:30:17 Allowing RPC connections from: 127.0.0.0/255.0.0.0 ::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 
2014-09-28 08:30:17 Added connection peer=1
2014-09-28 08:30:17 send version message: version 70002, blocks=-1, us=0.0.0.0:0, peer=1
2014-09-28 08:30:17 Binding RPC on address ::1 port 8332 (IPv4+IPv6 bind any: 0)
2014-09-28 08:30:17 sending: version (102 bytes) peer=1
2014-09-28 08:30:17 Binding RPC on address 127.0.0.1 port 8332 (IPv4+IPv6 bind any: 0)
2014-09-28 08:30:17   - Load block from disk: 0.00ms [0.00s]
2014-09-28 08:30:17   - Connect total: 0.00ms [0.00s]
2014-09-28 08:30:17 init message: Done loading
2014-09-28 08:30:17 BitcoinMiner started
2014-09-28 08:30:17   - Flush: 0.00ms [0.00s]
2014-09-28 08:30:17 keypool reserve 4
2014-09-28 08:30:17   - Writing chainstate: 0.00ms [0.00s]
2014-09-28 08:30:17 UpdateTip: new best=0000000061b1aca334b059920fed7bace2336ea4d23d63428c7aee04da49e942  height=0  log2_work=32.000022  tx=1  date=2014-09-25 17:32:11 progress=0.000000
2014-09-28 08:30:17   - Connect postprocess: 0.00ms [0.00s]
2014-09-28 08:30:17 - Connect block: 0.00ms [0.00s]
2014-09-28 08:30:17   - Load block from disk: 0.00ms [0.00s]
2014-09-28 08:30:17       - Connect 1 transactions: 0.00ms (0.000ms/tx, 0.000ms/txin) [0.00s]
2014-09-28 08:30:17     - Verify 0 txins: 0.00ms (0.000ms/txin) [0.00s]
2014-09-28 08:30:17 Pre-allocating up to position 0x100000 in rev00000.dat
2014-09-28 08:30:17     - Index writing: 0.00ms [0.00s]
2014-09-28 08:30:17     - Callbacks: 0.00ms [0.00s]
2014-09-28 08:30:17   - Connect total: 0.00ms [0.00s]
2014-09-28 08:30:17   - Flush: 0.00ms [0.00s]
2014-09-28 08:30:17   - Writing chainstate: 0.00ms [0.00s]
2014-09-28 08:30:17 UpdateTip: new best=00000000923c935006272f11b781421bb9c40670aec27ed716eedb48b3790e38  height=1  log2_work=33.000022  tx=2  date=2014-09-26 02:34:14 progress=0.000000
2014-09-28 08:30:17   - Connect postprocess: 0.00ms [0.00s]
2014-09-28 08:30:17 - Connect block: 0.00ms [0.00s]
2014-09-28 08:30:17 CreateNewBlock(): total size 1000
2014-09-28 08:30:17       - Connect 1 transactions: 0.00ms (0.000ms/tx, 0.000ms/txin) [0.00s]
2014-09-28 08:30:17     - Verify 0 txins: 0.00ms (0.000ms/txin) [0.00s]
@@ -450,6 +450,10 @@ void static BitcoinMiner(CWallet *pwallet)
//
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
CBlockIndex* pindexPrev = chainActive.Tip();
if (NULL == pindexPrev) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems more common to use pindexPrev == NULL or !pindexPrev?

@laanwj
Copy link
Member

laanwj commented Sep 29, 2014

As I understand the initialization sequence, the block database is loaded, chainActive and such is initialized. Then at the end of AppInit2() GenerateBitcoins is called which starts the miner thread.

How can this result in a race condition?

@imharrywu
Copy link
Contributor Author

i am testing the minging of an altcoin ( begining with a new genesis block), so, in which case, mapBlockIndex.size() will be ZERO. as i see this log:

2014-09-28 08:30:17 mapBlockIndex.size() = 0
.......
2014-09-28 08:30:17 BitcoinMiner started

And my latest code is as following (init.cpp: ~line: 1000+):

//// debug print
LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size());
LogPrintf("nBestHeight = %d\n", chainActive.Height());
......
#ifdef ENABLE_WALLET
// Generate coins in the background
if (pwalletMain)
GenerateBitcoins(GetBoolArg("-gen", false), pwalletMain, GetArg("-genproclimit", -1));
#endif

@laanwj
Copy link
Member

laanwj commented Sep 29, 2014

What will set chainActive.Tip in this case - ie, what is your proposed loop waiting for?

@imharrywu
Copy link
Contributor Author

Got it, -reindex, is the key. i run bitcoind with this parameter.

AppInit2() call InitBlockIndex() with -reindex=true.

bool InitBlockIndex() {
.......
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
if (!fReindex) {

and after InitBlockIndex() return, chainActive.tip() is NULL.

but then ProcessBlock() will call ActivateBestChain()
bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
{
......
if (!ActivateBestChain(state, pblock))
return error("ProcessBlock() : ActivateBestChain failed");

return true;

}

ActivateBestChain() will call ActivateBestChainStep(). ActivateBestChainStep() will call ConnectTip(), ConnectTip will call UpdateTip().

but i am not sure who will call ProcessBlock(), because i can not see the log: "received block %s peer=%d\n"

else if (strCommand == "block" && !fImporting && !fReindex) // Ignore blocks received while importing
{

......
LogPrint("net", "received block %s peer=%d\n", block.GetHash().ToString(), pfrom->id);
......
ProcessBlock(state, pfrom, &block);

@laanwj
Copy link
Member

laanwj commented Sep 29, 2014

OK that's unexpected to me. You'd say that chainActive.tip() would point to the genesis block, when starting reindexing, not NULL.

Anyhow, then it makes sense.

@BitcoinPullTester
Copy link

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/p4995_b4d1881ca4b939dd3cecb221fb4b3c65d8f23021/ for binaries and test log.
This test script verifies pulls every time they are updated. It, however, dies sometimes and fails to test properly. If you are waiting on a test, please check timestamps to verify that the test.log is moving at http://jenkins.bluematt.me/pull-tester/current/
Contact BlueMatt on freenode if something looks broken.

@laanwj
Copy link
Member

laanwj commented Oct 15, 2014

This is a legitimate problem, but we're going for an alternative solution. See discussion in #5078.

@laanwj laanwj closed this Oct 15, 2014
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants