Skip to content

Conversation

sipa
Copy link
Member

@sipa sipa commented Feb 11, 2017

Notes:

  • The LevelDB patch here is simple, and makes it switch to c++11 atomics when available over their MemoryBarrier() based solutions. A slightly better version is submitted upstream as Prefer std::atomic over MemoryBarrier google/leveldb#449.
  • Several race fixes are not bug fixes (the numThreads one and the access to globals one both happen before any multithreaded access to those variables takes place; and the LevelDB one is due to tsan not recognizing the inline assembly used in MemoryBarrier), but in general improvements that result in less fragile code.

After these, I believe our unit and rpc tests are clean for asan/lsan/ubsan/tsan, but:

  • The BDB environment holds locks that cross call stacks, and trigger the tsan deadlock detection. To avoid, create a suppressions file with the line deadlock:__db_pthread_mutex_lock in it, and pass an environment variable TSAN_OPTIONS="suppressions=$file" to the binaries.
  • The zapwallettxn RPC test causes an assertion failure inside libtsan for me.
  • You need to compile with -DBOOST_SP_USE_STD_ATOMIC to avoid tsan incorrectly detecting signals2 calls as racy (boost uses inline assembly for atomic refcounting by default, unless this compile options is passed; it was introduced in 1.54 I think).

As tsan seems to become a usable option for race detection, I wonder if over time we could remove our own DEBUG_LOCKORDER features.

{
std::lock_guard<std::mutex> lock(cs);
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this one. How could the ctor be racy?

Copy link
Member

Choose a reason for hiding this comment

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

This is really confusing. I don't think we should be making this nonsensical change just to make a sanitizer tool happy.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd highly prefer we do - such tools are pretty critical to our ability to find issues - but should probably have a comment noting why we have this insanity.

Copy link
Member

Choose a reason for hiding this comment

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

The way forward would be to fix the tools, not contort our code around whatever happen to be the bugs in the analysis tools of the day.

@sipa
Copy link
Member Author

sipa commented Feb 11, 2017

It's not (see PR description). I think tsan is confused because it's accessed from another object.

@@ -667,11 +667,13 @@ static void MutateTx(CMutableTransaction& tx, const std::string& command,
MutateTxDelOutput(tx, commandVal);
else if (command == "outaddr")
MutateTxAddOutAddr(tx, commandVal);
else if (command == "outpubkey")
else if (command == "outpubkey") {
if (!ecc) { ecc.reset(new Secp256k1Init()); }
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... how can ecc be a nullptr at this point?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry,.. missed the fact that std::unique_ptr<Secp256k1Init> ecc; initialises ecc with nullptr

@sipa
Copy link
Member Author

sipa commented Feb 12, 2017

@jonasschnelli Where would it be set to something else?

Copy link
Contributor

@gmaxwell gmaxwell left a comment

Choose a reason for hiding this comment

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

ACK

@sipa
Copy link
Member Author

sipa commented Feb 20, 2017

I'll try to find something less insane that also doesn't trip it.

@sipa
Copy link
Member Author

sipa commented Mar 5, 2017

I'm unable to reproduce that tsan warning, so I just removed the commit.

@laanwj
Copy link
Member

laanwj commented Mar 5, 2017

I'm unable to reproduce that tsan warning, so I just removed the commit.

I think you forgot to push, it's still there: a10eff9

@sipa
Copy link
Member Author

sipa commented Mar 29, 2017

Rebased, and removed the LevelDB and the patches for spurious warning. The LevelDB patch should go through the leveldb repo instead.

src/sync.cpp Outdated
@@ -71,7 +71,7 @@ struct LockData {
boost::mutex dd_mutex;
} static lockdata;

boost::thread_specific_ptr<LockStack> lockstack;
boost::thread_specific_ptr<LockStack> lockstack([](LockStack* ptr) { delete ptr; });
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused. The boost docs appear to indicate this should be identical.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree, and tests seem to agree that they are indeed invoked. I'll remove it.

@@ -25,7 +28,7 @@ int64_t GetTime()

void SetMockTime(int64_t nMockTimeIn)
{
nMockTime = nMockTimeIn;
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
Copy link
Contributor

Choose a reason for hiding this comment

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

relaxed here just seems needlessly lossy - mocktime is for testing, it can be slow, and running a test with a race because we're using relaxed everyhwere just seems needlessly confusing.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's read by a relaxed read anyway, using a stronger serializing write won't matter (if I understand the semantics correctly).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, my suggestion was to change the read as well :)

Copy link
Member Author

Choose a reason for hiding this comment

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

GetTime is called all over the place, no? I don't want to introduce extra synchronization there.

@jtimon
Copy link
Contributor

jtimon commented Apr 24, 2017

utACK 1d31093

@laanwj laanwj merged commit 1d31093 into bitcoin:master Apr 26, 2017
laanwj added a commit that referenced this pull request Apr 26, 2017
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 12, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 14, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 14, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 15, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 19, 2019
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
barrystyle pushed a commit to PACGlobalOfficial/PAC that referenced this pull request Jan 22, 2020
1d31093 fix tsan: utiltime race on nMockTime (Pieter Wuille)
321bbc2 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)

Tree-SHA512: 39ea83c6122f06339cd425deb236357694e84ce2e4e9c61c10b90a8909b6e42e8c7b76396175cdc4723ababd2fa4f935d48f8a469baf853c5a06d7b962a5c8dc
furszy added a commit to PIVX-Project/PIVX that referenced this pull request Apr 5, 2021
267ee79 fix tsan: utiltime race on nMockTime (Pieter Wuille)
be2b6b5 fix ubsan: bitcoin-tx: not initialize context before IsFullyValid (Pieter Wuille)
bb65b9e [Refactoring] Additions/updates to pivx-tx.cpp (random-zebra)
2c944c7 [Refactoring] Add SEQUENCE_FINAL constant in CTxIn (random-zebra)
4501198 [Refactoring] Move non-throwing ParseHashStr from rest to core_read (random-zebra)
29e23bf [Refactoring] Add constant for max pubkeys per multisig (random-zebra)

Pull request description:

  Simple refactoring + backport bitcoin#9743

ACKs for top commit:
  Fuzzbawls:
    ACK 267ee79
  furszy:
    looking good, utACK 267ee79 and merging..

Tree-SHA512: a46c46ea3b9b3e625b13978448787b0afbd9233a1893dea0a400abb3aa7c73524968453d5955ceca991f587a431daed0f4a44744b4bfdd8360d591355f40c8cf
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
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.

8 participants