ramvasanth / thread-sanitizer

Automatically exported from code.google.com/p/thread-sanitizer
0 stars 0 forks source link

tsan does not detect race conditions on global values #54

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Compile and run the test program below.

What is the expected output? What do you see instead?

Tsan should print a warning regarding access to "data". This works like 
expected as long as "data" is a member variable, but not for the global value.

What version of the product are you using? On what operating system?

llvm-3.4 on centos-6.5 x86_64

Please provide any additional information below.

    #include <QtConcurrentRun>
    #include <QMutex>
    #include <iostream>
    #include <boost/thread/mutex.hpp>

    int data;

    class TestClass
    {
    public:
     void fun()
     {
      data = 1;
     }
    private:
     //int data;
    };

    int main()
    {
     TestClass *tc = new TestClass;

     QFuture<void> t1 = QtConcurrent::run(tc, &TestClass::fun);
     QFuture<void> t2 = QtConcurrent::run(tc, &TestClass::fun);

     t1.waitForFinished();
     t2.waitForFinished();

     return 0;
    }

Original issue reported on code.google.com by benjamin...@wincor-nixdorf.com on 10 Mar 2014 at 1:14

GoogleCodeExporter commented 9 years ago
FTR, repro instructions:
$ sudo apt-get install libqt4-dev libqt4-core
$ clang++ -fsanitize=thread -I/usr/include/qt4 -I/usr/include/qt4/QtCore src.cc 
-lQtCore

Original comment by dvyu...@google.com on 11 Mar 2014 at 6:22

GoogleCodeExporter commented 9 years ago
Works for me:

==================
WARNING: ThreadSanitizer: data race (pid=27479)
  Write of size 4 at 0x7f8bd6b13c08 by thread T2:
    #0 TestClass::fun() /tmp/qt.cc:13 (a.out+0x00000008b5a6)
    #1 QtConcurrent::VoidStoredMemberFunctionPointerCall0<void, TestClass>::runFunctor() /usr/include/qt4/QtCore/qtconcurrentstoredfunctioncall.h:209 (a.out+0x00000008c024)
    #2 QtConcurrent::RunFunctionTask<void>::run() /usr/include/qt4/QtCore/qtconcurrentrunbase.h:134 (a.out+0x00000008bd48)
    #3 non-virtual thunk to QtConcurrent::RunFunctionTask<void>::run() /tmp/qt.cc:143 (a.out+0x00000008c079)
    #4 QThreadPoolThread::run() /build/buildd/qt4-x11-4.8.1/src/corelib/concurrent/qthreadpool.cpp:107 (libQtCore.so.4+0x00000006f4f1)

  Previous write of size 4 at 0x7f8bd6b13c08 by thread T1:
    #0 TestClass::fun() /tmp/qt.cc:13 (a.out+0x00000008b5a6)
    #1 QtConcurrent::VoidStoredMemberFunctionPointerCall0<void, TestClass>::runFunctor() /usr/include/qt4/QtCore/qtconcurrentstoredfunctioncall.h:209 (a.out+0x00000008c024)
    #2 QtConcurrent::RunFunctionTask<void>::run() /usr/include/qt4/QtCore/qtconcurrentrunbase.h:134 (a.out+0x00000008bd48)
    #3 non-virtual thunk to QtConcurrent::RunFunctionTask<void>::run() /tmp/qt.cc:143 (a.out+0x00000008c079)
    #4 QThreadPoolThread::run() /build/buildd/qt4-x11-4.8.1/src/corelib/concurrent/qthreadpool.cpp:107 (libQtCore.so.4+0x00000006f4f1)

  Location is global 'data' of size 4 at 0x7f8bd6b13c08 (a.out+0x000000ee0c08)

  Thread T2 (tid=27482, running) created by main thread at:
    #0 pthread_create tsan_interceptors.cc:851 (a.out+0x000000043893)
    #1 QThread::start(QThread::Priority) /build/buildd/qt4-x11-4.8.1/src/corelib/thread/qthread_unix.cpp:600 (libQtCore.so.4+0x00000007b7cd)
    #2 QFuture<void> QtConcurrent::run<void, TestClass>(TestClass*, void (TestClass::*)()) /usr/include/qt4/QtCore/qtconcurrentrun.h:280 (a.out+0x00000008b4d5)
    #3 main /tmp/qt.cc:24 (a.out+0x00000008b1cf)

  Thread T1 (tid=27481, running) created by main thread at:
    #0 pthread_create tsan_interceptors.cc:851 (a.out+0x000000043893)
    #1 QThread::start(QThread::Priority) /build/buildd/qt4-x11-4.8.1/src/corelib/thread/qthread_unix.cpp:600 (libQtCore.so.4+0x00000007b7cd)
    #2 QFuture<void> QtConcurrent::run<void, TestClass>(TestClass*, void (TestClass::*)()) /usr/include/qt4/QtCore/qtconcurrentrun.h:280 (a.out+0x00000008b4d5)
    #3 main /tmp/qt.cc:23 (a.out+0x00000008b149)

SUMMARY: ThreadSanitizer: data race /tmp/qt.cc:13 TestClass::fun()
==================

What am I doing differently?

Original comment by dvyu...@google.com on 11 Mar 2014 at 6:25

GoogleCodeExporter commented 9 years ago
---------- Forwarded message ----------
From:  <benjamin.firl>
Date: Tue, Mar 11, 2014 at 12:40 PM
Subject: Re: Issue 54 in thread-sanitizer: tsan does not detect race conditions 
on global values

Thanks for your response.

> Works for me:
>
You're right, it seems to work here to, my mistake.
I did not realize this, because the output seems very random. Regardless of 
global or member data, I need to run the example a couple of times to see the 
warning. Is this normal behavior?
Also there are a lot of (false) warnings you don't seem to have, like this:

==================
WARNING: ThreadSanitizer: data race (pid=4287)
  Write of size 1 at 0x7d180000eb20 by main thread:
    #0 pthread_mutex_destroy /home/hama/llvm-3.4/llvm-3.4/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:941 (exe+0x00000005017c)
    #1 <null> <null>:0 (libQtCore.so.4+0x00000007189c)

  Previous atomic read of size 1 at 0x7d180000eb20 by thread T2:
    #0 pthread_mutex_lock /home/hama/llvm-3.4/llvm-3.4/projects/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc:2185 (exe+0x00000006d01c)
    #1 <null> <null>:0 (libQtCore.so.4+0x00000007178b)

  Location is heap block of size 96 at 0x7d180000eb20 allocated by main thread:
    #0 operator new /home/hama/llvm-3.4/llvm-3.4/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:560 (exe+0x00000004bc09)
    #1 <null> <null>:0 (libQtCore.so.4+0x0000000718cd)
    #2 QFuture<void> QtConcurrent::run<void, TestClass>(TestClass*, void (TestClass::*)()) /usr/include/QtCore/qtconcurrentrun.h:230 (exe+0x0000000aa41b)
    #3 main /home/hama/svn/rvs/br_llvm/misc/ThreadTest/ThreadTestSanitizer/ThreadTestSanitizer.cpp:28 (exe+0x0000000aa2a1)

  Thread T2 (tid=4290, finished) created by main thread at:
    #0 pthread_create /home/hama/llvm-3.4/llvm-3.4/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:877 (exe+0x00000004f8bb)
    #1 <null> <null>:0 (libQtCore.so.4+0x000000071038)
    #2 QFuture<void> QtConcurrent::run<void, TestClass>(TestClass*, void (TestClass::*)()) /usr/include/QtCore/qtconcurrentrun.h:230 (exe+0x0000000aa41b)
    #3 main /home/hama/svn/rvs/br_llvm/misc/ThreadTest/ThreadTestSanitizer/ThreadTestSanitizer.cpp:28 (exe+0x0000000aa2a1)

SUMMARY: ThreadSanitizer: data race ??:0 ??
==================

> What am I doing differently?
>

Which Qt version do you use? I'm only able to use an relative old here: 4.6.
The example is build with cmake and clang is called with a lot of flags. But 
none of them should cause problems:

Building CXX object 
misc/ThreadTest/ThreadTestSanitizer/CMakeFiles/ThreadTestSanitizer.dir/ThreadTes
tSanitizer.cpp.o
cd /home/hama/svn/rvs/br_llvm_build/misc/ThreadTest/ThreadTestSanitizer && 
/opt/llvm-3.3/bin/clang++   -DQT_GUI_LIB -DQT_XML_LIB -DQT_CORE_LIB -DQT_DEBUG 
-fsanitize=thread -ggdb -I/home/hama/svn/rvs/br_llvm_build -I/usr/include/QtGui 
-I/usr/include/QtXml -I/usr/include/QtCore 
-I/home/hama/svn/rvs/br_llvm_build/misc/ThreadTest/ThreadTestSanitizer    
-isystem /opt/llvm-3.3/include/override -isystem 
/opt/llvm-3.3/include/override/crypto++ -isystem 
/opt/llvm-3.3/include/override/opencv -isystem 
/opt/llvm-3.3/include/override/c++/4.4.4 -D_DEBUG -DDEBUG 
-DBOOST_DISABLE_CB_DEBUG  -fmessage-length=0  -ggdb -isystem /usr/include 
-isystem /usr/include/QtGui -isystem /usr/include/opencv -isystem 
/usr/include/libusb-1.0 -pipe -Wall -W -Wextra -Wcomment -Wmissing-braces 
-Wparentheses -Wreturn-type -Wswitch -Wtrigraphs -Wunused-function 
-Wunused-label -Wunused-parameter -Wunused-variable -Wunknown-pragmas 
-Wendif-labels -Wpointer-arith -Wcast-align -Wpacked -Wdisabled-optimization 
-Wchar-subscripts -Wfloat-equal -Wimplicit -Wno-variadic-macros -Wno-long-long 
-Wno-invalid-source-encoding -Woverloaded-virtual -Wredundant-decls      -O1 
-fno-omit-frame-pointer -DLLVM_COMPAT -DGCC4_COMPAT -Du_int64_t=uint64_t 
-isystem /opt/intel/ipp/current/include  -o 
CMakeFiles/ThreadTestSanitizer.dir/ThreadTestSanitizer.cpp.o -c 
/home/hama/svn/rvs/br_llvm/misc/ThreadTest/ThreadTestSanitizer/ThreadTestSanitiz
er.cpp

Linking CXX executable ../../../bin/ThreadTestSanitizer
cd /home/hama/svn/rvs/br_llvm_build/misc/ThreadTest/ThreadTestSanitizer && 
/usr/bin/cmake -E cmake_link_script CMakeFiles/ThreadTestSanitizer.dir/link.txt 
--verbose=1
/opt/llvm-3.3/bin/clang++   -fsanitize=thread -ggdb    
-Wl,-rpath,/0_RVSROOT/,-rpath,/0_RVSROOT/BIN  -Wl,--add-needed 
-L/opt/intel/ipp/current/sharedlib -L/usr/lib/qt4/lib 
CMakeFiles/ThreadTestSanitizer.dir/ThreadTestSanitizer.cpp.o  -o 
../../../bin/ThreadTestSanitizer -rdynamic -lQtCore

Original comment by dvyu...@google.com on 11 Mar 2014 at 2:11

GoogleCodeExporter commented 9 years ago
> I need to run the example a couple of times to see the warning. Is this 
normal behavior?

This is more or less normal, in some runs the race can be masked by other 
synchronization in Qt.

> Also there are a lot of (false) warnings you don't seem to have, like this:

I also see them, Tsan does not understand what happens inside of Qt.

> Which Qt version do you use?

I don't know. I've just installed default qt4-dev/core packages on Ubuntu.
I don't think it matters a lot, Tsan does not understand all versions of Qt.

Original comment by dvyu...@google.com on 11 Mar 2014 at 2:13

GoogleCodeExporter commented 9 years ago
FTR, there is an issue for tsan support in Qt bug tracker:
https://bugreports.qt-project.org/browse/QTBUG-37402

Original comment by dvyu...@google.com on 12 Mar 2014 at 9:31

GoogleCodeExporter commented 9 years ago
Adding Project:ThreadSanitizer as part of GitHub migration.

Original comment by gli...@google.com on 30 Jul 2015 at 9:21