Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A Bug in pthread_cond_destroy?



I think you are assuming behavior that isn't specified.  Specifically, the definition for pthread_cond_destroy() says:

The pthread_cond_destroy() function shall destroy the given condition variable specified by cond; the object becomes, in effect, uninitialized. An implementation may cause pthread_cond_destroy() to set the object referenced by cond to an invalid value. A destroyed condition variable object can be reinitialized using pthread_cond_init(); the results of otherwise referencing the object after it has been destroyed are undefined.

So, trying to call pthread_cond_signal() from another thread after the condition is destroyed (or even "during" it being destroyed) can result is undefined behavior.  You really need synchronization of your own to avoid issues there.  Using the same mutex would have been good enough.  Since that isn't in your model, you need something else (maybe a second condition variable that indicates whether the first has been destroyed? - just thinking out loud).

--Jim

On Jan 9, 2008, at 2:25 AM, Kazuho Oku wrote:

Hi,

If this is not the right place to report bugs of libc, please tell me
the right mailing list.  Thank you in advance, and sorry, if it is the
case.

I think I have found a bug in pthread_cond_destroy of Libc (with Mac
OS X 10.4.10).

Current implementation of pthread_cond_destroy does not check if
sigspending==0.  Thus if pthread_cond_destroy is called from a thread
other than that called pthread_cond_signal, the destructor may return
0 even if pthread_cond_signal is still in work (i.e. sigspending !=
0), leading to memory corruption or an infinite loop in
pthread_cond_signal.

For example, in the following code, thread B would sometimes read from
and/or write to freed memory.

Thread A:
   pthread_cond_wait(cond, &mutex);
   while ((err = pthread_cond_destroy(cond)) != 0) {
       assert(err == EBUSY);
       usleep(1);
   }
   free(cond);

Thread B:
   pthread_cond_signal(global_cond);

Note that the problem only arises when thread B does not lock the same
mutex as thread A when calling thread A (this is not a requirement in
POSIX threads).

Attached to this mail is a code that would reproduce this bug.  It
sends and receives pthread_cond_signals and increment a counter.
It compiled without any compile time defines, the program prints the
counter incrementing infinitely.
But if compiled with -DCLEAR_MEM -DSIGNAL_WO_LOCK it would suddenly
stop due to memory corruption.
Or if compiled with -DAPPLE_TEST -DSIGNAL_WO_LOCK it would check the
value of sispending and print an assertion failure.

To reproduce the bug with the attached code, a dual processor system
might be a requirement.

--
Kazuho Oku
<thtest.c> _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden

References: 
 >A Bug in pthread_cond_destroy? (From: "Kazuho Oku" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.