net: Add Clang thread safety annotations for guarded variables in the networking code #13123

pull practicalswift wants to merge 2 commits into bitcoin:master from practicalswift:net-guarded-by changing 3 files +28 −34
  1. practicalswift commented at 8:47 AM on April 30, 2018: contributor

    Add Clang thread safety annotations for variables guarded by:

    • cs_addrLocal
    • cs_addrName
    • cs_feeFilter
    • cs_filter
    • cs_hSocket
    • cs_inventory
    • cs_mapLocalHost
    • cs_most_recent_block
    • cs_proxyInfos
    • cs_sendProcessing
    • cs_setBanned
    • cs_SubVer
    • cs_vOneShots
    • cs_vProcessMsg
    • cs_vRecv
    • cs_vSend

    Changed files:

    • src/net.{cpp,h}
    • src/netbase.cpp
  2. practicalswift renamed this:
    Add Clang thread safety annotations for guarded variables in the networking code
    net: Add Clang thread safety annotations for guarded variables in the networking code
    on Apr 30, 2018
  3. fanquake added the label Refactoring on Apr 30, 2018
  4. fanquake added the label P2P on Apr 30, 2018
  5. practicalswift cross-referenced this on Apr 30, 2018 from issue Meta-issue: Add Clang thread safety analysis annotations by practicalswift
  6. DrahtBot cross-referenced this on Jun 7, 2018 from issue [net,mempool] Call AcceptToMemoryPool() asynchronously in p2p by skeees
  7. DrahtBot added the label Needs rebase on Jul 9, 2018
  8. practicalswift force-pushed on Jul 11, 2018
  9. practicalswift commented at 11:26 PM on July 11, 2018: contributor

    Rebased!

  10. DrahtBot removed the label Needs rebase on Jul 12, 2018
  11. DrahtBot cross-referenced this on Aug 12, 2018 from issue Dandelion transaction relay (BIP 156) by MarcoFalke
  12. MarcoFalke commented at 10:05 PM on August 12, 2018: member

    Imo could squash everything, since most of the commits only touch 2 lines.

  13. practicalswift force-pushed on Aug 13, 2018
  14. practicalswift commented at 7:39 AM on August 13, 2018: contributor

    @MarcoFalke Rebased and squashed! Please re-review :-)

  15. DrahtBot cross-referenced this on Aug 23, 2018 from issue Add p2p layer encryption with ECDH/ChaCha20Poly1305 by jonasschnelli
  16. DrahtBot cross-referenced this on Aug 24, 2018 from issue net: Refactor message parsing (CNetMessage), adds flexibility by jonasschnelli
  17. practicalswift force-pushed on Aug 30, 2018
  18. practicalswift commented at 1:37 PM on August 30, 2018: contributor

    Rebased and added missing lock:

    diff --git a/src/net.cpp b/src/net.cpp
    index 529a2f82f..7fcf6a3ef 100644
    --- a/src/net.cpp
    +++ b/src/net.cpp
    @@ -715,7 +715,10 @@ void CNode::copyStats(CNodeStats &stats)
             X(nRecvBytes);
         }
         X(fWhitelisted);
    -    X(minFeeFilter);
    +    {
    +        LOCK(cs_feeFilter);
    +        X(minFeeFilter);
    +    }
    
         // It is common for nodes with good ping times to suddenly become lagged,
         // due to a new block arriving or other large transfer.
    
  19. practicalswift cross-referenced this on Aug 31, 2018 from issue wallet: Remove unused local variable old_label by practicalswift
  20. in src/net.h:640 in 36ecc9ee38 outdated
     630 | -    std::deque<std::vector<unsigned char>> vSendMsg;
     631 |      CCriticalSection cs_vSend;
     632 | -    CCriticalSection cs_hSocket;
     633 | +    uint64_t nSendBytes GUARDED_BY(cs_vSend);
     634 | +    std::deque<std::vector<unsigned char>> vSendMsg GUARDED_BY(cs_vSend);
     635 |      CCriticalSection cs_vRecv;
    


    Empact commented at 7:11 AM on September 2, 2018:

    nit: move this down?

  21. in src/net.h:665 in 36ecc9ee38 outdated
     652 | @@ -653,8 +653,8 @@ class CNode
     653 |      // to be printed out, displayed to humans in various forms and so on. So we sanitize it and
     654 |      // store the sanitized version in cleanSubVer. The original should be used when dealing with
     655 |      // the network or wire types and the cleaned string used when displayed or logged.
     656 | -    std::string strSubVer, cleanSubVer;
     657 |      CCriticalSection cs_SubVer; // used for both cleanSubVer and strSubVer
     658 | +    std::string strSubVer GUARDED_BY(cs_SubVer), cleanSubVer GUARDED_BY(cs_SubVer);
    


    Empact commented at 7:14 AM on September 2, 2018:

    nit: separate lines?

  22. in src/net.h:675 in 36ecc9ee38 outdated
     673 | +    bool fRelayTxes GUARDED_BY(cs_filter);
     674 |      bool fSentAddr;
     675 |      CSemaphoreGrant grantOutbound;
     676 | -    CCriticalSection cs_filter;
     677 | -    std::unique_ptr<CBloomFilter> pfilter;
     678 | +    std::unique_ptr<CBloomFilter> pfilter GUARDED_BY(cs_filter);
    


    Empact commented at 7:16 AM on September 2, 2018:

    PT_GUARDED_BY?

  23. DrahtBot commented at 10:37 PM on September 20, 2018: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #14605 (Return of the Banman by dongcarl)
    • #14046 (net: Refactor message parsing (CNetMessage), adds flexibility by jonasschnelli)

    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.

  24. practicalswift force-pushed on Oct 8, 2018
  25. practicalswift commented at 1:30 PM on October 8, 2018: contributor

    @MarcoFalke @Empact Updated. Please re-review :-)

  26. practicalswift force-pushed on Oct 8, 2018
  27. practicalswift force-pushed on Oct 8, 2018
  28. practicalswift force-pushed on Oct 8, 2018
  29. practicalswift force-pushed on Oct 8, 2018
  30. practicalswift force-pushed on Oct 8, 2018
  31. practicalswift force-pushed on Oct 8, 2018
  32. practicalswift force-pushed on Oct 8, 2018
  33. DrahtBot cross-referenced this on Oct 30, 2018 from issue Return of the Banman by dongcarl
  34. practicalswift cross-referenced this on Nov 21, 2018 from issue refactor: Convert comments to thread safety annotations by MarcoFalke
  35. practicalswift cross-referenced this on Nov 27, 2018 from issue Add missing locks: validation.cpp + related by practicalswift
  36. DrahtBot added the label Needs rebase on Nov 27, 2018
  37. practicalswift force-pushed on Nov 27, 2018
  38. DrahtBot removed the label Needs rebase on Nov 27, 2018
  39. in src/net.h:677 in 559e910a4f outdated
     673 | @@ -673,15 +674,15 @@ class CNode
     674 |      const bool fInbound;
     675 |      std::atomic_bool fSuccessfullyConnected;
     676 |      std::atomic_bool fDisconnect;
     677 | +    mutable CCriticalSection cs_filter;
    


    MarcoFalke commented at 8:05 PM on November 27, 2018:

    Is moving those lines required? It makes automated review harder, so best to keep them where they were?


    practicalswift commented at 8:01 AM on November 28, 2018:

    Good point! Fixed. Please re-review :-)

  40. practicalswift force-pushed on Nov 28, 2018
  41. Add missing locking annotations b312cd7707
  42. Add missing lock in CNode::copyStats(...) 4894133dc5
  43. practicalswift force-pushed on Nov 28, 2018
  44. MarcoFalke commented at 4:05 PM on November 28, 2018: member

    utACK 4894133dc5095ed971bf98d695384e7cbb16e54c

    • Checked that where the lock is added is also a place where tsan would complain [1]
    • Checked that the lock annotations don't change the objdump of the bitcoind, compiled with gcc or clang on my system

    [1]

    WARNING: ThreadSanitizer: data race (pid=30947)
      Read of size 8 at 0x7d6c0005e688 by thread T12 (mutexes: write M131981):
        [#0](/github-metadata-backup-bitcoin-bitcoin/0/) CNode::copyStats(CNodeStats&) /bitcoin/src/net.cpp:718 (bitcoind+0x0000005224e5)
        [#1](/github-metadata-backup-bitcoin-bitcoin/1/) CConnman::GetNodeStats(std::vector<CNodeStats, std::allocator<CNodeStats> >&) /bitcoin/src/net.cpp:2567 (bitcoind+0x0000005346b7)
        [#2](/github-metadata-backup-bitcoin-bitcoin/2/) getpeerinfo(JSONRPCRequest const&) /bitcoin/src/rpc/net.cpp:131 (bitcoind+0x000000617137)
    
    ...
    
      Previous write of size 8 at 0x7d6c0005e688 by thread T19 (mutexes: write M134272):
        [#0](/github-metadata-backup-bitcoin-bitcoin/0/) ProcessMessage(CNode*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, CDataStream&, long, CChainParams const&, CConnman*, std::atomic<bool> const&, bool) /bitcoin/src/net_processing.cpp:2927 (bitcoind+0x000000562dc8)
    
  45. MarcoFalke referenced this in commit 0a452d02ce on Nov 28, 2018
  46. MarcoFalke merged this on Nov 28, 2018
  47. MarcoFalke closed this on Nov 28, 2018

  48. PastaPastaPasta referenced this in commit 75d5fec88b on Apr 12, 2020
  49. PastaPastaPasta referenced this in commit dd1e7e4ce0 on Apr 16, 2020
  50. PastaPastaPasta referenced this in commit a13e2f4355 on Apr 16, 2020
  51. jnewbery commented at 1:02 PM on June 22, 2020: member

    @practicalswift why are nNextAddrSend and nNextLocalAddrSend now guarded by cs_sendProcessing? I don't think those things should be connected.

  52. practicalswift commented at 1:10 PM on June 22, 2020: contributor

    @jnewbery I agree that sure looks like an error! Good catch!

  53. jnewbery commented at 1:19 PM on June 22, 2020: member

    Thanks for the quick reply. I have a PR moving some of those fields around, so I'll get rid of the lock annotations there.

  54. practicalswift commented at 2:18 PM on June 22, 2020: contributor

    @jnewbery Thanks a lot for fixing (and finding)! Much appreciated!

  55. jnewbery commented at 2:44 PM on June 22, 2020: member

    @practicalswift sorry I meant to say that I have a branch that moves the fields around that I haven't PR'ed yet. I'll ping you when the PR is open.

  56. jnewbery cross-referenced this on Oct 6, 2020 from issue Refactoring and minor improvement for self-advertisements by naumenkogs
  57. naumenkogs referenced this in commit ed5c6deacc on Oct 6, 2020
  58. naumenkogs referenced this in commit e8da279a38 on Oct 6, 2020
  59. naumenkogs referenced this in commit c3ad35e367 on Oct 7, 2020
  60. naumenkogs referenced this in commit 836d4fb5d0 on Oct 13, 2020
  61. jnewbery referenced this in commit 9dc6124e92 on Oct 13, 2020
  62. naumenkogs referenced this in commit 0d9322f9c2 on Dec 9, 2020
  63. naumenkogs referenced this in commit 6fe9af6f88 on Dec 9, 2020
  64. naumenkogs referenced this in commit 8c9b306986 on Dec 10, 2020
  65. naumenkogs referenced this in commit ba6dc3576d on Dec 18, 2020
  66. jnewbery cross-referenced this on Feb 28, 2021 from issue net processing: Extract `addr` send functionality into MaybeSendAddr() by jnewbery
  67. jnewbery referenced this in commit c001ba45f1 on Feb 28, 2021
  68. jnewbery commented at 11:53 AM on February 28, 2021: member

    @practicalswift sorry I meant to say that I have a branch that moves the fields around that I haven't PR'ed yet. I'll ping you when the PR is open.

    PR is open here: #21236. It's a pure refactor and should be easy to review.

  69. jnewbery referenced this in commit 6a956bafd4 on Mar 1, 2021
  70. ckti referenced this in commit 4120fb71f7 on Mar 28, 2021
  71. LarryRuane referenced this in commit 5ea5a52636 on Apr 5, 2021
  72. LarryRuane referenced this in commit 716f2c6984 on Apr 5, 2021
  73. LarryRuane cross-referenced this on Apr 5, 2021 from issue Bitcoin 0.18 locking PRs by LarryRuane
  74. practicalswift deleted the branch on Apr 10, 2021
  75. LarryRuane referenced this in commit a535339c1b on Apr 29, 2021
  76. LarryRuane referenced this in commit 98f7b77d65 on Apr 29, 2021
  77. LarryRuane referenced this in commit b4fcee0614 on Jun 1, 2021
  78. LarryRuane referenced this in commit bcf71e925c on Jun 1, 2021
  79. gades referenced this in commit 8d8d47fb36 on Jun 25, 2021
  80. jnewbery cross-referenced this on Aug 17, 2021 from issue net/net processing: Move tx inventory into net_processing by jnewbery
  81. gades referenced this in commit 9c05585999 on Feb 8, 2022
  82. bitcoin locked this on Aug 18, 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-05-20 06:55 UTC