Skip to content

Conversation

fanquake
Copy link
Member

@fanquake fanquake commented Feb 20, 2022

Use of this macro enables precondition checks for iterators and functions of the library. It's use is recommended in debug builds. See https://www.boost.org/doc/libs/1_78_0/libs/multi_index/doc/tutorial/debug.html for more info.

There is also a BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING macro:

When this mode is in effect, all public functions of Boost.MultiIndex will perform post-execution tests aimed at ensuring that the basic internal invariants of the data structures managed are preserved.

@theStack
Copy link
Contributor

theStack commented Mar 3, 2022

Concept ACK

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Tested ACK fe565fb

FWIW, executing the unit tests for debug builds doesn't take significantly longer with this patch on my machine:

--with-debug build on master branch:
$ time ./src/test/test_bitcoin
15m05.76s real    12m11.82s user     6m03.60s system

--with-debug build on PR branch:
$ time ./src/test/test_bitcoin
15m11.04s real    12m05.47s user     6m43.91s system

(I didn't execute more than once each, i.e. the results have to be taken with a grain of salt)

@fanquake fanquake requested a review from mzumsande March 5, 2022 16:21
@JeremyRubin
Copy link
Contributor

is there a failing example test case that we can run to verify it is enabled in debug mode?

Or should these invariants be unreachable for extenral users?

@mzumsande
Copy link
Contributor

Partial Tested ACK fe565fb (can't say anything about the configure.ac flag details, which I don't know anything about )

is there a failing example test case that we can run to verify it is enabled in debug mode?

I tried to dereference an iterator to an entry of an empty mempool:

BOOST_AUTO_TEST_CASE(mempool_iterator_ub)
{
    CTxMemPool pool;
    LOCK2(cs_main, pool.cs);
    typename CTxMemPool::indexed_transaction_set::index<descendant_score>::type::iterator it = pool.mapTx.get<descendant_score>().begin();
    std::string txId{it->GetTx().GetHash().ToString()};
}

This fails for me on this branch with

(...)Assertion `safe_mode::check_dereferenceable_iterator(*this)' failed.

and passes on master. Triggering other codes from safe_mode::error_code (see link in OP) should be possible too.

FWIW, executing the unit tests for debug builds doesn't take significantly longer with this patch on my machine:

The benchmark ComplexMemPool is slower by a factor of roughly 3 compared to a --enable-debug build on master.
PR:

ns/op op/s err% total benchmark
3,003,964,479.00 0.33 0.6% 33.01 ComplexMemPool

Master:

ns/op op/s err% total benchmark
1,182,466,622.00 0.85 1.6% 13.03 ComplexMemPool

Copy link
Contributor

@mzumsande mzumsande left a comment

Choose a reason for hiding this comment

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

Would it make sense to enable this also for fuzzing, or do the existing sanitizers already cover these error conditions anyway?

@fanquake
Copy link
Member Author

Would it make sense to enable this also for fuzzing, or do the existing sanitizers already cover these error conditions anyway?

@MarcoFalke any thoughts?

@maflcko
Copy link
Member

maflcko commented Mar 17, 2022

I think google oss-fuzz has --enable-debug turned on? Also, there might be a CI task in the qa-assets repo that has it turned on.

@JeremyRubin
Copy link
Contributor

you should turn it on for fuzzing, i can think of some implementations that might not be caught in existing sanitizers (e.g., imagine as an optimization deleting a key puts the cleared object in a valid but not initialized 'orphanage' for a future allocation to use -- the iter might be invalid but not a memory fault to access afterwards).

i dont think the code actually does this, but the internal safe mode would catch these things.

@maflcko
Copy link
Member

maflcko commented Mar 17, 2022

Oh, I was wrong. OSS-Fuzz does not run with --enable-debug. https://github.com/google/oss-fuzz/blob/aa83381257bbf55d71c6a4b0f56f805409776047/projects/bitcoin-core/build.sh#L57

It only runs with DEBUG=1, but this is disabled due to bugs (:smiling_face_with_tear: ): https://github.com/google/oss-fuzz/blob/aa83381257bbf55d71c6a4b0f56f805409776047/projects/bitcoin-core/build.sh#L33

I am running --enable-debug somewhere else: https://github.com/MarcoFalke/btc_nightly/blob/0b191f4cb53134217f992908a0f5962dcc05b6f7/00_setup_env_native_fuzz_enable_debug.sh#L17

Though that also fails currently due to a boost bug (:smiling_face_with_tear: ): https://cirrus-ci.com/task/6626378031300608?logs=fuzz_run#L947

If you enable it for fuzzing in this repo, the mempool test might take three times as long. (https://cirrus-ci.com/task/4976712239808512?logs=ci#L4448) From 30 minutes to 90 minutes. Not sure if this will push us over the 120 minute timeout.

@fanquake fanquake force-pushed the boost_multiindex_safe_mode branch from fe565fb to 821e5b0 Compare April 4, 2022 10:09
@fanquake
Copy link
Member Author

fanquake commented Apr 4, 2022

Rebased this given #24558 has been merged.

@DrahtBot
Copy link
Contributor

Guix builds

File commit 346e780
(master)
commit 06f2e41
(master and this pull)
SHA256SUMS.part 791ceba7511820bd... f61dce7e030666d8...
*-aarch64-linux-gnu-debug.tar.gz c4394ec90823ac39... 1bb177acb40472ce...
*-aarch64-linux-gnu.tar.gz 49e745c299086c99... 9979bad174e583e9...
*-arm-linux-gnueabihf-debug.tar.gz 35633cce17d629eb... 2e84605bfd931a68...
*-arm-linux-gnueabihf.tar.gz 0761e21f2e62ed95... 0039844c8bb32acf...
*-arm64-apple-darwin-unsigned.dmg 5daa39fc518c86ae... 07fd8d65706ea357...
*-arm64-apple-darwin-unsigned.tar.gz cb9d396cfa8666db... f1e899eae70fcc59...
*-arm64-apple-darwin.tar.gz 60f8f27f4b93a533... 67863e0d7d63c9bf...
*-powerpc64-linux-gnu-debug.tar.gz ecc3060a762707f1... 5c6aea34ce95313f...
*-powerpc64-linux-gnu.tar.gz 18b2b694385817e4... 0597915aad4ff786...
*-powerpc64le-linux-gnu-debug.tar.gz e65825425df649a4... 3f7565e89f0dc54f...
*-powerpc64le-linux-gnu.tar.gz cf5d5221c17adf07... 66a624de33e97a69...
*-riscv64-linux-gnu-debug.tar.gz e724ba323541cfae... f922cb7fa75b2e16...
*-riscv64-linux-gnu.tar.gz f9454b4a737f3969... dcf1d060fd016f66...
*-win64-debug.zip 4c330fdd8159b833... fe5aa3d57bff3fff...
*-win64-setup-unsigned.exe 57ae88cdfecd4c3f... c26b38a47bb6b9c4...
*-win64-unsigned.tar.gz cf8abf09f23d3db1... cfbb14bb0c705629...
*-win64.zip 0eb863575ba5da10... 213c122146d867c3...
*-x86_64-apple-darwin-unsigned.dmg 27f2d9448832e6e8... 9b3cc97fdaca0b67...
*-x86_64-apple-darwin-unsigned.tar.gz ff3b4f667dcfdd1d... d66e1a78bcee83fe...
*-x86_64-apple-darwin.tar.gz 8eb9c9fc8611b7c7... 0e3e8316bf025f1d...
*-x86_64-linux-gnu-debug.tar.gz 040f6196974aa498... b9c0a8f3bfbbfda7...
*-x86_64-linux-gnu.tar.gz db199b6a60087d64... 7bbac0b51850fd6a...
*.tar.gz d3b56c48d0c9a3fd... c93ea571c752a9a4...
guix_build.log 91802c9a48e3e323... e3ad46266df9ee16...
guix_build.log.diff facdf995862eb025...

@fanquake
Copy link
Member Author

Rebased and simplified.

Use of this macro enables precondition checks for iterators and
functions of the library. It's use is recommended in debug builds.

See:
https://www.boost.org/doc/libs/1_78_0/libs/multi_index/doc/tutorial/debug.html.
@fanquake fanquake force-pushed the boost_multiindex_safe_mode branch from d581ee0 to 06e18e0 Compare May 19, 2022 08:44
@maflcko
Copy link
Member

maflcko commented May 20, 2022

Looks like this doesn't affect the fuzz runtime too much, so should be fine.

@fanquake fanquake requested a review from laanwj June 1, 2022 17:14
@laanwj
Copy link
Member

laanwj commented Jun 8, 2022

Concept and code review ACK 06e18e0

@maflcko maflcko merged commit 2e079c8 into bitcoin:master Jun 8, 2022
@fanquake fanquake deleted the boost_multiindex_safe_mode branch June 8, 2022 15:28
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jun 13, 2022
…when debugging

06e18e0 build: use BOOST_MULTI_INDEX_ENABLE_SAFE_MODE when debugging (fanquake)

Pull request description:

  Use of this macro enables precondition checks for iterators and functions of the library. It's use is recommended in debug builds. See https://www.boost.org/doc/libs/1_78_0/libs/multi_index/doc/tutorial/debug.html for more info.

  There is also a `BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING` macro:
  > When this mode is in effect, all public functions of Boost.MultiIndex will perform post-execution tests aimed at ensuring that the basic internal invariants of the data structures managed are preserved.

ACKs for top commit:
  laanwj:
    Concept and code review ACK 06e18e0

Tree-SHA512: 7ee489eccda81c7dbca9210af6d3007d5b2c704b645139d2714c077af157789dd9478c29d0d212e210e96686ea83713aaf3d458e879122b3cde64f3e3e3789d2
@bitcoin bitcoin locked and limited conversation to collaborators Jun 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants