Skip to content

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented May 9, 2023

The global function has issues:

  • It causes gcc-13 warnings, see GCC 13: -Wdangling-reference output #26926
  • There is no rationale for it being a global function, when it acts like a member function
  • performance-unnecessary-copy-initialization clang-tidy isn't run on it

Fix all issues by making it a member function.

@DrahtBot
Copy link
Contributor

DrahtBot commented May 9, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK hebasto, achow101
Concept ACK fanquake
Stale ACK stickies-v

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #27101 (Support JSON-RPC 2.0 when requested by client by pinheadmz)

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.

@fanquake
Copy link
Member

fanquake commented May 9, 2023

Concept ACK

@hebasto
Copy link
Member

hebasto commented May 9, 2023

Concept ACK.

@DrahtBot DrahtBot removed the CI failed label May 9, 2023
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

ACK fa0d180f48d4e5253f58aced32768369f963d1c7, tested on Ubuntu 23.04 using GCC 13.0.1.

@maflcko
Copy link
Member Author

maflcko commented May 9, 2023

Ubuntu 23.04 using GCC 13.0.1

I think it ships with 12.2? https://packages.ubuntu.com/lunar/gcc

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

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

ACK fa0d180f48d4e5253f58aced32768369f963d1c7

I can't see any behaviour change, and as far as I can tell using references in fa0d180f48d4e5253f58aced32768369f963d1c7 doesn't introduce any (potential) lifetime issues.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be LIFETIMEBOUND?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe in a follow-up for the whole univalue interface?

@hebasto
Copy link
Member

hebasto commented May 9, 2023

Ubuntu 23.04 using GCC 13.0.1

I think it ships with 12.2? packages.ubuntu.com/lunar/gcc

https://packages.ubuntu.com/lunar/g++-13 has been installed as sudo apt install g++-13.

@fanquake
Copy link
Member

fanquake commented May 9, 2023

With fa0d180f48d4e5253f58aced32768369f963d1c7 & gcc (GCC) 13.1.1 20230426 (Red Hat 13.1.1-1), fixes all issues from #26926 except for:

test/interfaces_tests.cpp: In member function ‘void interfaces_tests::findCommonAncestor::test_method()’:
test/interfaces_tests.cpp:101:19: warning: possibly dangling reference to a temporary [-Wdangling-reference]
  101 |     const CChain& active = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return Assert(m_node.chainman)->ActiveChain());
      |                   ^~~~~~
In file included from ./common/args.h:9,
                 from ./test/util/setup_common.h:9,
                 from test/interfaces_tests.cpp:9:
./sync.h:302:96: note: the temporary was destroyed at the end of the full expression ‘<lambda closure object>interfaces_tests::findCommonAncestor::test_method()::<lambda()>{((interfaces_tests::findCommonAncestor*)this)}.interfaces_tests::findCommonAncestor::test_method()::<lambda()>()’
  302 | #define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
      |                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
test/interfaces_tests.cpp:101:28: note: in expansion of macro ‘WITH_LOCK’
  101 |     const CChain& active = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return Assert(m_node.chainman)->ActiveChain());
      |                            ^~~~~~~~~
  CXX      test/test_bitcoin-netbase_tests.o

MarcoFalke added 4 commits May 9, 2023 18:47
-BEGIN VERIFY SCRIPT-
 sed --regexp-extended -i 's/find_value\(([^ ,]+), /\1.find_value(/g' $(git grep -l find_value)
-END VERIFY SCRIPT-
@maflcko maflcko force-pushed the 2305-univalue-no-dangling- branch from fa0d180 to fa28850 Compare May 9, 2023 16:49
This can be reverted once gcc excludes lambdas with decltype(auto)
return type from its -Wdangling-reference analysis.
@maflcko
Copy link
Member Author

maflcko commented May 9, 2023

except for ...

Might be good to ask the gcc devs if they want to fix this, as it seems a different false positive for decltype(auto) return type lambdas?

Still, added a temporary commit here to hack around it.

{
auto& chain = m_node.chain;
const CChain& active = WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return Assert(m_node.chainman)->ActiveChain());
const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be best to add the commit message as code documentation to make it easier to track when this can be reverted?

I'm also not sure this commit fits the scope of the PR but I can see how it's related, and it's a separate commit anyway so probably fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am thinking that this may not be fixed by upstream gcc, and anyway, gcc-13.1.1 is released and will stick around, so this can't be reverted any time soon, anyway. So it might be best to just make it permanent now?

Copy link
Member

Choose a reason for hiding this comment

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

GCC 13 introduced a broken behavior, and we usually do not modify our correct code to make a broken compiler happy. Considering that this is the only line in test code, I lean to agree with this change.

Copy link
Member

@hebasto hebasto May 10, 2023

Choose a reason for hiding this comment

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

As the test is executed in a single thread, is it a big deal to hold ::cs_main during the entire test case?

Why not to use the pattern as

LOCK(Assert(m_node.chainman)->GetMutex());
auto& chain = m_node.chain;
const CChain& active = Assert(m_node.chainman)->ActiveChain();
or
LOCK(::cs_main);
auto& chain = m_node.chain;
const CChain& active = Assert(m_node.chainman)->ActiveChain();

?

Copy link
Member Author

Choose a reason for hiding this comment

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

Likely due to a LOCKS_EXCLUDED?

Copy link
Member

Choose a reason for hiding this comment

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

Likely due to a LOCKS_EXCLUDED?

Right, in Chainstate::InvalidateBlock.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

re-ACK fa266c4

@DrahtBot DrahtBot requested a review from stickies-v May 10, 2023 11:23
@hebasto
Copy link
Member

hebasto commented May 10, 2023

Maybe notice the GCC-13 bug in the comments for #define WITH_LOCK?

@maflcko
Copy link
Member Author

maflcko commented May 10, 2023

Maybe notice the GCC-13 bug in the comments for #define WITH_LOCK?

I think #27605 (comment) applies and we should disable the warning instead if there are more false positives?

@achow101
Copy link
Member

ACK fa266c4

@achow101 achow101 merged commit e0a70c5 into bitcoin:master May 10, 2023
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 10, 2023
@maflcko maflcko deleted the 2305-univalue-no-dangling- branch May 11, 2023 07:56
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 5, 2024
…e method

Summary:
These warning are very noisy and annoying when working on large refactors, hiding the useful information from the developer.

Backport of [[bitcoin/bitcoin#27605 | core#27605]].

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Differential Revision: https://reviews.bitcoinabc.org/D15097
@bitcoin bitcoin locked and limited conversation to collaborators May 10, 2024
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.

6 participants