mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 09:35:38 +00:00
util: Add and use AssertUnreachable #36087
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36087
Loading…
Reference in a new issue
No description provided.
Delete branch "2608-unreachable"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The current code uses assertions with falsy literal values in a few places. This is perfectly fine, but this comes with a bit of confusion sometimes:
assert(false)may cause confusion when it is unclear ifNDEBUGcan disable it. (The intention is to disallowNDEBUGcompilation). Also, it can lead to warnings when used with clang-cl.exe (GCC seems to understand it)Assert(false)hides the noreturn attribute behind a function call, which may cause GCC return-type warnings. (Clang seems to understand it)See https://godbolt.org/z/h7PT3jh3Y for the clang-cl/GCC warning reproducer.
std::unreachable. However, this is unsafe and will invoke UB.CHECK_NONFATALmacro exists, but no fatal equivalent.Fix all those issues by:
AssertUnreachable, which directly calls the noreturninternal_abort_helperhelper.assert_falsylinter to enforce code to use this macro.The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
src/test/fuzzand treat them as errors by hebasto)[[noreturn]]by fanquake)If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
🚧 At least one of the CI tasks failed.
Task
lint: https://github.com/bitcoin/bitcoin/actions/runs/32933915544/job/98071384264LLM reason (✨ experimental): CI failed because the
assert_falsylint check detected anassert(0 && ...)(insrc/tinyformat.h) that must be replaced withAssertUnreachable().Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
🐙 This pull request conflicts with the target branch and needs rebase.
🐙 This pull request conflicts with the target branch and needs rebase.
Left 3 comments.
AssertUnreachablehere and inSignatureHashSchnorrcallsassertion_failfromutil/check.cpp. This file is inlibbitcoin_consensus, which should only depend on crypto. Normal builds hide it,bitcoin_commonpulls util in anyway, but the riscv job links the lib standalone and breaks. Exclude consensus fromassert_falsyunder the same "not linking the util lib" reason, or moveassertion_fail?The message says any falsy literal, but the regex hardcodes the spacing, so
assert (false),assert(false )andassert(0&&"x")all slip through.\<(a|A)ssert\s*\(\s*(0|false)\s*(&&|\))catches them. Andstd_unreachablelets__builtin_unreachable()through. libstdc++ implementsstd::unreachable()as exactly that, and it builds under C++20 wherestd::unreachable()doesn't. Worth catching the builtin too?The scripted-diff that satisfies this linter is the next commit, so lint is red here, 135 lines. Swapping the two applies cleanly and gives the same tree. The linters already in master came in with their fix in one commit.
🐙 This pull request conflicts with the target branch and needs rebase.
Thx, used that.
I think the risk of someone using it should be low, because it is a compiler-specific built-in.
Thx, swapped commits
Thx, will fix this some time before taking out of draft.
🐙 This pull request conflicts with the target branch and needs rebase.
No verified findings are available from this review.
Review coverage was incomplete.
Reviewed:
fa9a4ddbaf16· Review report · Previous reportspermalink
🚧 At least one of the CI tasks failed.
Task
tidy: https://github.com/bitcoin/bitcoin/actions/runs/42874130721/job/130032819364LLM reason (✨ experimental): CI failed because
check-deps.shdetected an unexpected dependency:interpreter.cpp.odepends oncheck.cpp.oviainternal_abort_helper.Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
thx, excluded for now
Concept ACK.
Thank you for reviving this topic!
Approach ACK
fa0f314a5c..faf79448a2, forgot this one?github.com/bitcoin/bitcoin@fa0f314a5c/src/protocol.cpp (L95)Dead code left over?
fa3ec8d81dThis header does not belong in this group.
faf79448a2This makes
#include <cassert>unnecessary.nit: Why not use the more readable form (assuming we are okay with side effects in the asserted expression)?
fa47891c9bThe new message no longer mentions
assert, but the regex still catches it.🚧 At least one of the CI tasks failed.
Task
lint: https://github.com/bitcoin/bitcoin/actions/runs/43179667829/job/131052297731LLM reason (✨ experimental): CI failed on lint checks:
assert_falsyflagsassert(false)insrc/test/fuzz/util/wallet.h, and thescripted_diffcheck also failed.Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
rebased for silent conflict and fixed all nits
ACK
fa5c2d1440.nit:
Why is
src/script/interpreter.cppthe onlybitcoin_consensussource excluded here? The other sources of that library do not linkbitcoin_utileither.fixed include nit
self-nit: Could be NotNull?
There are other linters (linkers) that enforce the exclusion as well: https://github.com/bitcoin/bitcoin/pull/36087#issuecomment-6776675778. Happy to include the full list, but I think I'll leave as-is for now and this can be extended on a case-by-case basis, when needed. Hopefully this "fixes itself" some time in the future via the library cleanup efforts.
re-ACK
fa343ffd11.crACK
fa343ffd11Should reduce the issue with non-standard no-
[[no_return]]assert()s on Windows Clang-CL as noted in https://github.com/bitcoin/bitcoin/pull/31507#discussion_r3801801387.Makes me wonder if the scope of
-Wno-error=return-typefrom ci-windows.py could be reduced to only code that doesn't depend on check.h, but don't see any obvious way to do it.remark: It's not obvious to me whether this shouldn't be named and/or cased
FATAL_UNREACHABLE().Maybe the uppercasing of
NONFATAL_UNREACHABLE()andCHECK_NONFATAL()could be inferred to mean that they have different effects in debug builds and release builds. That would explain why you didn't go that way withAssertUnreachable(). OTOHAssume()has different effects depending on the build too.nanonit: Think it's healthier for the shared semantics of the project to imply that all asserts are fatal rather than suggesting there are two classes.
nit: This is not entirely true, asserting
0occurs in two places.ok, I may or may not reconsider this on the next push. I think the linter is fine either way for now.
Assertalready means fatal in this codebase and this is a bit like anAssertZero()(always fails), so I kept consistency with that. But naming is always hard and subjective, I guess.Which ones? Did you see
#define TINYFORMAT_ERROR(reasonString) throw tinyformat::format_error(reasonString)?That disables one of them, but there's also a
TINYFORMAT_ASSERT(0)(which admittedly doesn't match the regex).Ah, didn't see that. Though, at this point, it isn't really "externally maintained". At some point this will all be removed, so it doesn't matter, but if I touch this again, I can say:
// Avoid flagging dead branches in temporary std::format portConcept ACK!
Glad we're converging on the assert-cleanup-project™ - might as well simplify a few more of these while we're here, so we don't have to revisit the same lines later.
I don't insist on any of them, but wanted to present the inconsistencies.
This could just be a plain assert, the log seems superfluous:
Assert the addrman check directly
Might as well tidy this up while we're here:
Assert the remaining command
Maybe extend the existing scripted diff to cover these, too?
Use AssertUnreachable in both fallbacks
nit: same comment as in
to_btckfor consistency?Explain the omitted defaults
compat/assumptions.halready checks this (and even mentionsMallocUsage).Use the existing pointer-width check
We still get warnings for missing cases this way (and is less repetitive if we just break):
Share the final assertion
If this is indeed unreachable, we could collapse it to a single return with comment:
Return the comparison
nit: we can just fall through
Share the two case labels
As far as I can tell,
ZeroSockalready does this. Is the more specific error location a reason to keep these?Inherit the existing assignment guard
Same as in
InitWalletFlags: it's weird to doif not ... assert(false)...Assert the wallet results
Given the name suggests it's "unreachable", if we start handling it, it sounds like it's just "exceptional" ...
Writes can fail, so would an exception make more sense once the caller can restore the backup - wouldn't this get tangled up with https://github.com/bitcoin/bitcoin/pull/34193?
To avoid
assume/AssumeUnreachable/assertUnreachable, we could use--perl-regexp(with\binstead, which also avoids native macOS Git silently missing\<- seemslint_includes_build_configalready uses it) and list the methods instead of(ume|ert)hacksSpell out the assertion names
🐙 This pull request conflicts with the target branch and needs rebase.
Sure, happy to wait for the other pull first
this is part of the scripted-diff, so not sure
All of this code will be deleted , so no need to rewrite it
🚧 At least one of the CI tasks failed.
Task
lint: https://github.com/bitcoin/bitcoin/actions/runs/44072951784/job/134003532996LLM reason (✨ experimental): CI failed the
scripted_difflint check because the scripted replacement incorrectly removes#include <util/check.h>fromsrc/memusage.h.Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
not sure
I presume the log was intentional, so I'll leave as-is.
this is not possible to do in a scripted-diff. Also, I am sure your diff is wrong, because there is no compiler checks on stuff like
btck_LogLevel_ERROR.I guess so, but I think this was written intentionally? I think I'll leave as-is for now.
thx, done
hmm, did something else in the first commit
thx, pushed something
pushed my last suggestion
Not sure I understand what this means: if it's not 32 bits and not 64 bits, check that it's either 32 bits or 64 bits? We're already asserting that elsewhere, I don't think this is useful...
Heh, didn't know that git doesn't bundle regex. Switched to perl regex
thx, done
We could add the break in a previous commit.
Sure, done
btck_LogLevel_ERROR- ah, so the missing comments are sometimes deliberate. Isn't that another reason to add them when they would be true?Isn't the error clearer saying that
cmdis notgetchainparams? Isn't the meaning the same as before? I don't insist, but seems simpler to me.This will become
static_assert(0)in C++23, which doesn't work in C++20 due to a language defect.static_assert(0)is the compile-time equivalent ofassert(0)at runtime, so I think it is useful.Edit: Leaving this open, so I don't forget to follow-up with C++23 here.
The proper fix here would be the removal of the incomplete 32 bit support anyway - please resolve the comment.
This makes it harder to add new commands in the future, because the diff would be more verbose
Yeah, they should be added, but this pull is already a bit large, and there is no consistency enforcement anyway, so I am not sure if reviewers will be happy about such a commit.
Right, but I think an explicit marker is clearer than a break, so I think I'll leave this as-is for now.
ACK
fa9a4ddbafNit: PR description needs update,
AssertUnreachabledoesn't callassertion_failanymore.re-ACK
fa9a4ddbafChanges since previous review (https://github.com/bitcoin/bitcoin/pull/36087#pullrequestreview-5919415158):
switchAssertUnreachable()overabort()to make unreachability explicitAssert(blah)overif (!blah) { AssertUnreachable(); }tACK
fa9a4ddbafCode review ACK
fa9a4ddbaf. Nice cleanup making code more consistent, and I like the clever approach to updating includes in the scripted-diffIn commit "util: Add AssertUnreachable" (
fa81e4700a)This list of check helpers doesn't mention
AssertUnreachable()orNONFATAL_UNREACHABLE(). Since the PR description gives the missing fatal counterpart toNONFATAL_UNREACHABLE()as one of the motivations, it could be worth adding a bullet after this one, something like:re: https://github.com/bitcoin/bitcoin/pull/36087#discussion_r4599063726
Two things to know for the follow-up:
MallocUsageneeds to be made into a function template forstatic_assert(false)to work. In a non-template function, astatic_assertis checked in every branch, including discardedif constexprbranches, so it fires on every platform.-std=c++20on GCC 13 and our minimum Clang, so this can change as soon as we drop support for GCC 12.thx, done in https://github.com/bitcoin/bitcoin/pull/36952
Ok, I think I'll leave this as-is then. An alternative would be to append a short comment to this line: