This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Could you kindly explain this modification about pthread_cond_destroy?



On 27/11/2017 10:03, Florian Weimer wrote:
> On 11/27/2017 08:52 AM, honan li wrote:
>> But back to our issue, on the Qualcomm platform we are working on,
>> there are many places invoking pthread_cond_destroy, but after each
>> pthread_cond_destroy, thread or even the process would exit, and at
>> least for now it seems working all right on glibc2.20. Could you
>> kindly explain what is the potential risk of the case?
> 
> I'm not sure if I understand this question.
> 
> It results in undefined behavior.  Your application could crash, hang, or even exhibit silent memory corruption, resulting in incorrect results.
> 
> Thanks,
> Florian

As Florian pointed out and in your example pthread_cond_destroy is racing 
with pthread_cond_wait and you can check it with TSAN.  You should not 
rely on undefined behaviour.

$ gcc -Wall test-cond.c -o test-cond -pthread -fsanitize=thread
$ ./test-cond 
==================
WARNING: ThreadSanitizer: unlock of an unlocked mutex (or by a wrong thread) (pid=30973)
    #0 pthread_cond_wait <null> (libtsan.so.0+0x0000000279de)
    #1 thread1 <null> (test-cond+0x0000004009ed)
    #2 <null> <null> (libtsan.so.0+0x0000000230d9)

  Location is global 'mutex' of size 40 at 0x0000004020a0 (test-cond+0x0000004020a0)

  Mutex M0 (0x0000004020a0) created at:
    #0 pthread_cond_wait <null> (libtsan.so.0+0x0000000279de)
    #1 thread1 <null> (test-cond+0x0000004009ed)
    #2 <null> <null> (libtsan.so.0+0x0000000230d9)

SUMMARY: ThreadSanitizer: unlock of an unlocked mutex (or by a wrong thread) ??:0 __interceptor_pthread_cond_wait
==================
==================
WARNING: ThreadSanitizer: data race (pid=30973)
  Write of size 8 at 0x0000004020e0 by main thread:
    #0 pthread_cond_destroy <null> (libtsan.so.0+0x000000027f97)
    #1 main <null> (test-cond+0x0000004009b2)

  Previous read of size 8 at 0x0000004020e0 by thread T1:
    #0 pthread_cond_wait <null> (libtsan.so.0+0x0000000279de)
    #1 thread1 <null> (test-cond+0x0000004009ed)
    #2 <null> <null> (libtsan.so.0+0x0000000230d9)

  As if synchronized via sleep:
    #0 sleep <null> (libtsan.so.0+0x000000043956)
    #1 main <null> (test-cond+0x0000004009a8)

  Location is global 'cond' of size 48 at 0x0000004020e0 (test-cond+0x0000004020e0)

  Thread T1 (tid=30975, running) created by main thread at:
    #0 pthread_create <null> (libtsan.so.0+0x000000027577)
    #1 main <null> (test-cond+0x000000400983)

SUMMARY: ThreadSanitizer: data race ??:0 __interceptor_pthread_cond_destroy
==================
==================
WARNING: ThreadSanitizer: thread leak (pid=30973)
  Thread T2 (tid=30976, finished) created by main thread at:
    #0 pthread_create <null> (libtsan.so.0+0x000000027577)
    #1 main <null> (test-cond+0x00000040099e)

SUMMARY: ThreadSanitizer: thread leak ??:0 __interceptor_pthread_create
==================
ThreadSanitizer: reported 3 warnings


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]