Skip to content

Fix UB in benchmark#96

Open
hebasto wants to merge 2 commits intobitcoin-core:masterfrom
hebasto:250602-bench-ub
Open

Fix UB in benchmark#96
hebasto wants to merge 2 commits intobitcoin-core:masterfrom
hebasto:250602-bench-ub

Conversation

@hebasto
Copy link
Copy Markdown
Member

@hebasto hebasto commented Jun 2, 2025

From https://en.cppreference.com/w/c/language/operator_arithmetic.html:

The behavior is undefined if rhs is negative or is greater or equal the number of bits in the promoted lhs.

Steps to reproduce on Ubuntu 25.04 using the master branch @ ea8f66b:

$ ./autogen.sh
$ ./configure --enable-benchmark CXX=clang++-20 CXXFLAGS="-O0"
$ make bench -j $(nproc) bench
$ ./bench
<snip>
recover[ms]	 63	 260.72278	         -	  62.21269	
create[ns]	 63	  79.66483	         -	  17.74962	
/usr/lib/gcc/x86_64-linux-gnu/15/../../../../include/c++/15/bits/uniform_int_dist.h:108: std::uniform_int_distribution<unsigned long>::param_type::param_type(_IntType, _IntType) [_IntType = unsigned long]: Assertion '_M_a <= _M_b' failed.
Aborted (core dumped)

@hebasto
Copy link
Copy Markdown
Member Author

hebasto commented Jul 2, 2025

Rebased.

Added a commit that re-enables the now-fixed benchmarks in CI.

Comment thread src/bench.cpp Outdated
std::vector<uint64_t> roots(2 * syndromes);
std::random_device rng;
std::uniform_int_distribution<uint64_t> dist(1, (uint64_t(1) << bits) - 1);
uint64_t upper_bound;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There is a simpler way:

auto upper_bound = std::numeric_limits<uint64_t>::max() >> (64 - bits);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks! Taken.

hebasto and others added 2 commits October 21, 2025 10:40
From https://en.cppreference.com/w/c/language/operator_arithmetic.html:
> The behavior is undefined if rhs is negative or is greater or equal
> the number of bits in the promoted lhs.

Co-authored-by: Pieter Wuille <[email protected]>
Copy link
Copy Markdown

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants