Boost logo

Boost :

Subject: [boost] [smart_ptr] [thread] (was Re: A summary of testing with sanitizers)
From: Ben Pope (benpope81_at_[hidden])
Date: 2015-04-24 15:17:40


On Tuesday, April 21, 2015 06:42 PM, Ben Pope wrote:
> 89 SUMMARY: ThreadSanitizer: data race
> /home/ben/development/boost/test/build/boost_root/status/../boost/smart_ptr/detail/shared_count.hpp:449:49
> in boost::detail::shared_count::shared_count(boost::detail::shared_count
> const&)

I've just been looking at this one, essentially it seems to boil down to:

#include <atomic>
#include <thread>
#include <cstdint>

typedef _Atomic( std::int_least32_t ) atomic_int_least32_t;

inline void atomic_increment( atomic_int_least32_t * pw )
{
     __c11_atomic_fetch_add( pw, 1, __ATOMIC_RELAXED );
}

int main()
{
    atomic_int_least32_t use_count_;
    std::thread t ([&](){ atomic_increment(&use_count_); });
    __c11_atomic_init( &use_count_, 1 );
    t.join();
}

clang -fsanitize=thread -std=c++14 -stdlib=libc++ -pthread -lc++
-lc++abi -g sp_counted_base.cpp -O0

./a.out
==================
WARNING: ThreadSanitizer: data race (pid=13176)
   Atomic write of size 4 at 0x7ffed833619c by thread T1:
     #0 __tsan_atomic32_fetch_add
/home/ben/development/llvm/trunk/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interface_atomic.cc:614:3
(a.out+0x000000469c06)
     #1 atomic_increment(int _Atomic*)
/home/ben/development/test/sp_counted_base.cpp:9:5 (a.out+0x0000004999c4)
     #2 main::$_0::operator()() const
/home/ben/development/test/sp_counted_base.cpp:15:26 (a.out+0x00000049983e)
     #3
decltype(std::__1::forward<main::$_0>(fp)(std::__1::forward<>(fp0)))
std::__1::__invoke<main::$_0>(main::$_0&&)
/home/ben/development/llvm/trunk/install/release/bin/../include/c++/v1/__functional_base:415:12
(a.out+0x000000499563)
     #4 void
std::__1::__thread_execute<main::$_0>(std::__1::tuple<main::$_0>&,
std::__1::__tuple_indices<>)
/home/ben/development/llvm/trunk/install/release/bin/../include/c++/v1/thread:337
(a.out+0x000000499563)
     #5 void* std::__1::__thread_proxy<std::__1::tuple<main::$_0>
>(void*)
/home/ben/development/llvm/trunk/install/release/bin/../include/c++/v1/thread:347
(a.out+0x000000499563)

   Previous write of size 4 at 0x7ffed833619c by main thread:
     #0 main /home/ben/development/test/sp_counted_base.cpp:16:4
(a.out+0x000000498c39)

   Location is stack of main thread.

   Thread T1 (tid=13178, running) created by main thread at:
     #0 pthread_create
/home/ben/development/llvm/trunk/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:951:3
(a.out+0x000000422a21)
     #1 std::__1::thread::thread<main::$_0, void>(main::$_0&&)
/home/ben/development/llvm/trunk/install/release/bin/../include/c++/v1/thread:359:16
(a.out+0x000000498f89)
     #2 main /home/ben/development/test/sp_counted_base.cpp:15:16
(a.out+0x000000498c30)

SUMMARY: ThreadSanitizer: data race
/home/ben/development/test/sp_counted_base.cpp:9:5 in
atomic_increment(int _Atomic*)
==================
ThreadSanitizer: reported 1 warnings

I'm not sure how add_ref_copy is called during construction, but in the
particular case I was looking at this seems to occur in the move
constructor of thread:
http://www.boost.org/development/tests/develop/developer/output/BenPope%20x86_64%20Ubuntu-boost-bin-v2-libs-thread-test-thread__join_until_p_lib-test-clang-linux-3-7~tsan~c14_libc++-debug-threading-multi.html

Ben


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk