libcxx: data race in shared_ptr
| Bugzilla Link | 22836 |
| Resolution | FIXED |
| Resolved on | Jul 07, 2015 15:31 |
| Version | 3.6 |
| OS | All |
| CC | @asl,@BenPope,@hfinkel,@kcc,@mclow |
Extended Description
ThreadSanitizer says:
WARNING: ThreadSanitizer: data race (pid=19491) Read of size 8 at 0x7d4800017b88 by thread T4 (mutexes: write M188): #0 std::__1::__shared_weak_count::lock() /work/libc++/libcxx/src/memory.cpp:102 (libc++.so.1+0x000000086cc7) #1 std::__1::weak_ptrbase::Stream::lock() const /opt/clang36/bin/../include/c++/v1/memory:5197 (block_test+0x0000005ae3c9) #2 operator() // std::shared_ptr is passed through std::function (via the capture list) .....
Previous atomic write of size 8 at 0x7d4800017b88 by thread T3: #0 __tsan_atomic64_fetch_add /work/x/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interface_atomic.cc:618 (block_test+0x000000526d07) #1 __release_shared /work/libc++/libcxx/src/memory.cpp:61 (libc++.so.1+0x000000086bf6) #2 ~shared_ptr /opt/clang36/bin/../include/c++/v1/memory:4462 (block_test+0x00000063b21f) #3 operator() // std::shared_ptr is passed through std::function (via the capture list) ....
Both sites contain this pattern:
void f(std::shared_ptr
I don't have a full reproducer as it is reported by a ThreadSanitizer user. But it should be easy to reproduce.
Looking at the shared_ptr sources: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/memory.cpp?revision=224095&view=markup
99 __shared_weak_count* 100 __shared_weak_count::lock() _NOEXCEPT 101 { 102 long object_owners = _shared_owners; 103 while (object_owners != -1) 104 { 105 if (__sync_bool_compare_and_swap(&_shared_owners, 106 object_owners, 107 object_owners+1)) 108 return this; 109 object_owners = _shared_owners; 110 } 111 return 0; 112 }
There is non-atomic load of _shared_owners on line 102.
On a related note, why does libcxx use the rudimentary atomic operations interface instead of std::atomic or at least __atomic_load_n family of functions? The __sync builtins do not even allow to fix this data race in a reasonable way.
Fixed in r241532.
Yes of course. My choice of words was lead by the comments on Phabricator, but what I really meant is that TSan can see the __atomic_load and no longer complains about the code.
Just as a nitpicking note. This is not so about appeasing tsan, this is about fixing undefined behavior in libc++ code.
On Phabricator http://reviews.llvm.org/D8147 there was some discussion about using __atomic_load for object_owners. I've made that change locally and it does appease TSan.
The intuitive patch: http://reviews.llvm.org/D8147
OK, the essence of the original report is in running std::shared_ptr destructor from multiple threads. Reproducing the issue over std libs is possible... but I get bogged down in TSan warnings about the threading machinery...
#include
struct Thing { Thing() : a(0) { data.push_back("one"); data.push_back("two"); }
std::list<std::string> data;
int a;
};
std::shared_ptr
int main()
{
auto p = std::make_shared
auto f = std::async(std::launch::async,
[]() {
std::shared_ptr<Thing> p;
{
std::lock_guard<std::mutex> lock(_gm);
std::swap(p, _gptr);
}
});
p.reset();
return 0;
}
The original report is here: https://groups.google.com/forum/#!topic/thread-sanitizer/eHu4dE_z7Cc
That case comes down to pushing an instance of std::shared_ptr across thread boundary. The destructor of the wrapped object can therefore execute on either thread.
The additional case is about std::weak_ptr sharing. The wrapped object is consecutively accessed via the weak->lock() call.
I can probably code up minimal repros using std::thread/std::async machinery.