Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TSAN] atomic will cause data race undetected. #62371

Open
JackyWoo opened this issue Apr 26, 2023 · 3 comments
Open

[TSAN] atomic will cause data race undetected. #62371

JackyWoo opened this issue Apr 26, 2023 · 3 comments
Labels
compiler-rt:tsan Thread sanitizer

Comments

@JackyWoo
Copy link

JackyWoo commented Apr 26, 2023

Env

$ clang -v
Ubuntu clang version 15.0.7
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Selected multilib: .;@m64

Test case

  1. Data race detected
// bad case: data race detected
void testDataRace()
{
    int32_t counter = 0;

    std::thread t1([&counter] {
        if (!counter)
            counter = 1;
    });
    std::thread t2([&counter] {
        if (!counter)
            counter = 2;
    });

    t1.join();
    t2.join();
}
  1. Not detected when use atomic
// For user it is a bad case, but not detected
void testDataRaceAtomic()
{
    std::atomic<int32_t> counter = 0;

    std::thread t1([&counter] {
        if (!counter)
            counter = 1;
    });
    std::thread t2([&counter] {
        if (!counter)
            counter = 2;
    });

    t1.join();
    t2.join();
}

As a user's perspective the behavior is unexpected.

@JackyWoo JackyWoo changed the title TSAN: atomic will cause data race undetected. [TSAN] atomic will cause data race undetected. Apr 26, 2023
@EugeneZelenko EugeneZelenko added compiler-rt:tsan Thread sanitizer and removed new issue labels Apr 26, 2023
@EugeneZelenko
Copy link
Contributor

Could you please try 16 or main branch?

@HolyBlackCat
Copy link

It still happens in trunk, but it doesn't look like a bug to me.

From what I understand, TSAN only detects data races (as in, the kind of UB), not race conditions in general (as in, logic errors in concurrent code that are not UB per se). (1, 2)

@JackyWoo
Copy link
Author

@HolyBlackCat Thanks for reply. I wonder whether TSAN will detect data conditions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt:tsan Thread sanitizer
Projects
None yet
Development

No branches or pull requests

3 participants