llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Multiple races reported in std::async (TSan)

Open llvmbot opened this issue 11 years ago • 16 comments

Bugzilla Link 23235
Resolution FIXED
Resolved on Apr 20, 2015 15:42
Version 3.6
OS Linux
Attachments TSan output
Reporter LLVM Bugzilla Contributor
CC @KernelAddress,@kcc,@mclow

Extended Description

I've just noticed TSan warnings when using std::async on Linux. A small example is below. Compiled with Clang 3.6 and linked against an instrumented version of libc++:

/opt/clang36/bin/clang++ -std=c++14 -g -fno-omit-frame-pointer -fsanitize=thread -stdlib=libc++ -fcolor-diagnostics future.cpp -Wl,-rpath /opt/clang36/lib-tsan -lc++abi -o future

#include #include #include #include

std::atomic _counter;

int main() { _counter = 1;

auto f = std::async(std::launch::async,
                    []{
                        _counter += 2;
                        return true;
                    });
std::cout << "Result: " << std::boolalpha << f.get() << "\n";
return 0;

}

llvmbot avatar Apr 15 '15 02:04 llvmbot

Oh, got it. Thanks!

llvmbot avatar Apr 20 '15 22:04 llvmbot

The patch went exactly to compiler-rt in llvm repo: http://llvm.org/viewvc/llvm-project?view=revision&revision=235293

KernelAddress avatar Apr 20 '15 19:04 KernelAddress

Thanks, Dmitry!

Could you clarify where the patch went please? I use Clang and it gets compiler-rc from here, I believe:

http://llvm.org/svn/llvm-project/cfe/trunk/runtime/compiler-rt

Did you patch the GCC variant? Or am I misunderstanding the structure?

llvmbot avatar Apr 20 '15 17:04 llvmbot

Filed https://llvm.org/bugs/show_bug.cgi?id=23293 for the racy use-after-free on cond var.

KernelAddress avatar Apr 20 '15 12:04 KernelAddress

The false reports fixed by: http://llvm.org/viewvc/llvm-project?view=revision&revision=235293

KernelAddress avatar Apr 20 '15 12:04 KernelAddress

Although, the condvar race that I described looks real. I will file a separate bug for it.

KernelAddress avatar Apr 20 '15 12:04 KernelAddress

I can reproduce this and it is a bug in tsan. Fix is coming.

KernelAddress avatar Apr 20 '15 12:04 KernelAddress

BTW, have you been able to repro the issue?

Have not tried yet.

KernelAddress avatar Apr 16 '15 09:04 KernelAddress

Thanks, Dmitry!

BTW, have you been able to repro the issue?

llvmbot avatar Apr 15 '15 17:04 llvmbot

OK, everything looks correct. I will take a close look some time later. I am curious whether the last report is a false positive, because it can a very bad racy condvar use-after-free... Future sets ready flag, unlocks the mutex and then broadcasts condvar. If there is nothing else that preserves the future alive, then the broadcast can be done on a destroyed condvar.

KernelAddress avatar Apr 15 '15 17:04 KernelAddress

It's in comment #​3 above:

$ ldd future linux-vdso.so.1 (0x00007fff573dd000) libc++abi.so.1 => /opt/clang36/lib-tsan/libc++abi.so.1 libc++.so.1 => /opt/clang36/lib-tsan/libc++.so.1

llvmbot avatar Apr 15 '15 17:04 llvmbot

What does 'ldd future' say?

KernelAddress avatar Apr 15 '15 17:04 KernelAddress

The test binary was built this way:

/opt/clang36/bin/clang++ -std=c++14 -g -fno-omit-frame-pointer -fsanitize=thread -stdlib=libc++ -fcolor-diagnostics future.cpp -Wl,-rpath /opt/clang36/lib-tsan -lc++abi -o future

The instrumented version of libc++ was built this way:

cmake .. -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_LIBCXXABI_INCLUDE_PATHS=$TOP/libcxxabi/include/ -DLIT_EXECUTABLE=$TOP/llvm/utils/lit/lit.py -DCMAKE_C_COMPILER=$DESTINATION/bin/clang -DCMAKE_CXX_COMPILER=$DESTINATION/bin/clang++ -DCMAKE_INSTALL_PREFIX=$DESTINATION -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_SANITIZER=Thread make mv -f lib/libc++* $DESTINATION/lib-tsan

Also, I applied my shared_ptr patch from bug 22836.

llvmbot avatar Apr 15 '15 17:04 llvmbot

OK, lets have a look at the test binary:

$ ldd future linux-vdso.so.1 (0x00007fff573dd000) libc++abi.so.1 => /opt/clang36/lib-tsan/libc++abi.so.1 libc++.so.1 => /opt/clang36/lib-tsan/libc++.so.1

The instrumented lib lives in /opt/clang36/lib-tsan/. Lets looks at the symbols:

$ objdump --syms /opt/clang36/lib-tsan/libc++.so.1 | grep __tsan 0000000000000000 UND 0000000000000000 __tsan_atomic8_load 0000000000000000 UND 0000000000000000 __tsan_vptr_read 0000000000000000 UND 0000000000000000 __tsan_init 0000000000000000 UND 0000000000000000 __tsan_read1 0000000000000000 UND 0000000000000000 __tsan_write16 0000000000000000 UND 0000000000000000 __tsan_write1 0000000000000000 UND 0000000000000000 __tsan_atomic64_compare_exchange_val 0000000000000000 UND 0000000000000000 __tsan_func_exit 0000000000000000 UND 0000000000000000 __tsan_atomic64_fetch_add 0000000000000000 UND 0000000000000000 __tsan_read16 0000000000000000 UND 0000000000000000 __tsan_vptr_update 0000000000000000 UND 0000000000000000 __tsan_func_entry 0000000000000000 UND 0000000000000000 __tsan_read4 0000000000000000 UND 0000000000000000 __tsan_read2 0000000000000000 UND 0000000000000000 __tsan_write8 0000000000000000 UND 0000000000000000 __tsan_atomic32_fetch_add 0000000000000000 UND 0000000000000000 __tsan_write2 0000000000000000 UND 0000000000000000 __tsan_write4 0000000000000000 UND 0000000000000000 __tsan_atomic64_load 0000000000000000 UND 0000000000000000 __tsan_read8

llvmbot avatar Apr 15 '15 17:04 llvmbot

Are you sure that libc++ is indeed instrumented? Run objdump on libc++.so.1 loaded by the program and grep for calls to __tsan_func_entry.

I don't see more than 1 subsequent libc++.so frames in any of the reports, which is an indirect indication that it is non-instrumented. Also in this stack, there seems to be missed frames around libc++.so frame, which is also an indirect indication that it is non-instrumented:

Previous read of size 8 at 0x7d200000bfc0 by thread T1: #​0 pthread_cond_broadcast /work/x/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:1063 (future+0x00000045ce70) #​1 std::__1::condition_variable::notify_all() /work/libc++/libcxx/src/condition_variable.cpp:35 (libc++.so.1+0x0000000d6409) #​2 void std::__1::__assoc_state::set_value(bool&&) /opt/clang36/bin/../include/c++/v1/future:655 (future+0x0000004c738c)

How exactly did you build it? Can you give a full reproduction sequence of commands? That would help.

KernelAddress avatar Apr 15 '15 09:04 KernelAddress

Could you guys confirm whether these are true bugs and I am not missing something silly please?

llvmbot avatar Apr 15 '15 02:04 llvmbot