[validation] Two small clang lock annotation improvements #21202

pull amitiuttarwar wants to merge 2 commits into bitcoin:master from amitiuttarwar:2021-02-locks changing 3 files +18 −3
  1. amitiuttarwar commented at 7:48 PM on February 16, 2021: contributor

    Based on reviewing #21188

    the first commit switches the lock annotations on CheckInputScripts to be on the function declaration instead of on the function definition. this ensures that all call sites are checked, not just ones that come after the definition.

    the second commit adds a note to the developer-notes section to clarify where the annotations should be applied.

  2. amitiuttarwar commented at 7:53 PM on February 16, 2021: contributor

    to test this out, reviewers can apply this diff locally

    - bool CheckInputScripts(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck>* pvChecks)
    + RecursiveMutex cs_new;
    + bool CheckInputScripts(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck>* pvChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_new)
    

    and see that the compiler warns about the call site from ConnectBlock:

    validation.cpp:2136:35: warning: calling function 'CheckInputScripts' requires holding mutex 'cs_new' exclusively [-Wthread-safety-analysis]
                if (fScriptChecks && !CheckInputScripts(tx, tx_state, view, flags, fCacheResults, fCacheResults, txsdata[i], g_parallel_script_checks ? &vChecks : nullptr)) {
    

    but there is no warning from the call site from CheckInputsFromMempoolAndCache or PolicyScriptChecks.

    then you can apply the annotation to the declaration instead of the definition, and see that all the call sites generate warnings.

  3. DrahtBot added the label Docs on Feb 16, 2021
  4. DrahtBot added the label Validation on Feb 16, 2021
  5. in src/validation.cpp:201 in a72bff2b4f outdated
     197 | @@ -198,7 +198,7 @@ CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlo
     198 |  
     199 |  std::unique_ptr<CBlockTreeDB> pblocktree;
     200 |  
     201 | -bool CheckInputScripts(const CTransaction& tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr);
     202 | +bool CheckInputScripts(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck>* pvChecks = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
    



    amitiuttarwar commented at 11:59 PM on February 17, 2021:

    good call, added annotation there too.

  6. in doc/developer-notes.md:788 in 9d0975c91d outdated
     784 | @@ -785,6 +785,11 @@ Threads and synchronization
     785 |    get compile-time warnings about potential race conditions in code. Combine annotations in function declarations with
     786 |    run-time asserts in function definitions:
     787 |  
     788 | +  - In functions that are declared seperately from where they are defined, the
    


    hebasto commented at 5:02 AM on February 17, 2021:

    https://cirrus-ci.com/task/6341367142023168?command=lint#L859:

    doc/developer-notes.md:788: seperately ==> separately
    

    amitiuttarwar commented at 12:00 AM on February 18, 2021:

    fixed

  7. hebasto commented at 5:06 AM on February 17, 2021: member

    If this pr a case to apply #20986?

  8. [validation] Move the lock annotation from function definition to declaration
    When the annotation is on the definition, it does not check call sites between
    the declaration and the definition.
    ad5f01b960
  9. [doc] Add a note about where lock annotations should go. 25c57d6409
  10. amitiuttarwar force-pushed on Feb 17, 2021
  11. amitiuttarwar commented at 12:00 AM on February 18, 2021: contributor

    thanks for the review @hebasto, took all your suggestions.

    If this pr a case to apply #20986?

    done

  12. MarcoFalke removed the label Docs on Feb 18, 2021
  13. MarcoFalke added the label Refactoring on Feb 18, 2021
  14. promag commented at 2:32 PM on February 21, 2021: member

    Code review ACK 25c57d640992255ed67964a44b17afbfd4bed0cf.

    How about adding a validation_p.h with these "private" declarations?

  15. MarcoFalke commented at 8:46 AM on February 22, 2021: member

    ACK 25c57d640992255ed67964a44b17afbfd4bed0cf 🥘

    <details><summary>Show signature and timestamp</summary>

    Signature:

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA512
    
    ACK 25c57d640992255ed67964a44b17afbfd4bed0cf 🥘
    -----BEGIN PGP SIGNATURE-----
    
    iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
    pUghXQwAxK2rZIu0qYdOFsfc6ncH58m6umvxB6lxhN7rpYnpz1AyiCe4+RbYe2Nx
    ty820uHzkkQSb7eu40NeO7OkqcjlXHeUDSzXzzReSbOi90OLJa5pX4vMiur4chlP
    AiGAQLuHhDtDOwOySpVBR7NxHkcaCUjD5CrGkbBieLaTJlmYcwqpI4hycCAR9A4h
    Yg2hfcpwosTyUA7NNrk8F50UK7Ez97slqvDd7CLcI70jvEGpMiGO/YW1w2HqgJkP
    s6x7l0g3Frd9q7VA/EucZoMZYbDnoWEGyP88UaT2G727WxuYhRrUDVVP7srND7Z1
    JSNSzm/4v9eJtfaD0D/9xcgN0YxF6DRJqyJK6fPJAMdoimvpFIqyg2NowRaRcvHz
    zTTTcygMRV6l/HMDNdNJ59VwWlvxE3MHu4yzSntRtgNaeW/a3bTnm4XZAcF5woQZ
    2QhiNmS8BmHj2URMXcrNJmUmnWPj4bHaZuOhqIhvr8UMPeBhzTAzyT6Pyn3xU7Hs
    4pT3enGx
    =pXvj
    -----END PGP SIGNATURE-----
    

    Timestamp of file with hash 29b3e3a94f59f0a1d3ed2364dbf2aa60e4373ca5a3c8c71576d43351cf60215b -

    </details>

  16. MarcoFalke merged this on Feb 22, 2021
  17. MarcoFalke closed this on Feb 22, 2021

  18. sidhujag referenced this in commit b657411889 on Feb 22, 2021
  19. amitiuttarwar deleted the branch on Feb 22, 2021
  20. amitiuttarwar commented at 7:33 PM on February 22, 2021: contributor

    @promag where are other examples of forward declaring functions from the tests? I see examples for other files eg. denialofservice_tests have declarations for orphan handling stuff from net_processing. But I'm not seeing other declarations for internal validation.cpp functions. I'm definitely open to the pattern of test-only header files, but in this case, seems like it might be a bit of overkill to have an additional header file for just one function? Unless I'm missing something?

  21. DrahtBot locked this on Aug 16, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-17 06:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me