python / cpython

The Python programming language
https://www.python.org
Other
63.34k stars 30.32k forks source link

Add support for thread sanitizer (TSAN) via `--with-thread-sanitizer` #112536

Closed colesbury closed 7 months ago

colesbury commented 11 months ago

Feature or enhancement

GCC and Clang provide ThreadSanitizer, a tool that detects data races. We should add support for thread sanitizer to CPython. Note that Python already supports the memory and address sanitizers.

Eventually, it would be helpful to have a continuous build for the combination of --disable-gil --with-thread-sanitizer. Note that we probably won't want to run all the tests. ThreadSanitizer is slow and also not very useful for single-threaded tests. We should collect a subset of our tests that use threading for the ThreadSanitizer continuous build.

Linked PRs

samety commented 11 months ago

Hey, I would like to pick this up. As a first step tried copying the struct for ASAN to TSAN Here is a draft PR: https://github.com/python/cpython/pull/112648

Happy to get some initial feedback on it and then add remaining things like _Py_THREAD_SANITIZER etc.

samety commented 10 months ago

Added --with-thread-sanitizer as a configure option in pr 112648.

Still looking for a good way to enable TSAN for the continuous build, I will make another PR when I find one.

pitrou commented 7 months ago

Do we want to backport these at some point?

corona10 commented 7 months ago

~@pitrou We can go for the default build. I will submit the PR soon~

corona10 commented 7 months ago

Ah but the problem is —tsan flag..

corona10 commented 7 months ago

If we want to backport the TSAN test, the following PRs should be backported, but this is not a bug or security fix. We may need some discussion.

pitrou commented 7 months ago

Right. Perhaps ask on Discourse?

corona10 commented 7 months ago

Right. Perhaps ask on Discourse?

Good, do you want to write it? (I can write it but, since you proposed the suggestion first, I am asking)

pitrou commented 7 months ago

Ok, I posted https://discuss.python.org/t/backport-thread-sanitizer-infrastructure/48704

corona10 commented 7 months ago

@pitrou cc @colesbury By the way, once the backport patches are completed, let's close this issue. Expanding TSAN test can be tracked from the new issue. (I prefer to cut off the issues as the checkpoint)

pitrou commented 7 months ago

Ok, closing as done now.

comicfans commented 2 months ago

hello, possible to backport this to 3.10 too ?

picnixz commented 1 month ago

Unfortunately, 3.8 to 3.11 are security-only branches. As such, they would not accept such backports. The 3.11 backport was not merged since it was close to the last bugfix deadline.