Bug #14807

2.6.0-preview2 segfaults on OpenBSD due to missing pthread_condattr_init call

Added by jeremyevans0 (Jeremy Evans) 4 months ago. Updated about 14 hours ago.

Target version:
ruby -v:
ruby 2.6.0dev (2018-06-01 trunk 63545) [x86_64-openbsd]


r63238 refactored thread_pthread.c, and where there was previously a pthread_condattr_init call to initialize the pthread_condattr_t value, it removed the call and passed the pthread_condattr_t* directly to pthread_condattr_setclock without initializing the value by calling pthread_condattr_init first. On some operating systems that works, but it's not required to work, and it segfaults on OpenBSD because the pthread_condattr_t is not initialized.

The attached patch should fix the problem.


#1 [ruby-core:87346] Updated by normalperson (Eric Wong) 4 months ago

Thanks, r63548

Btw, is PTHREAD_COND_INITIALIZER supported on OpenBSD?

Something like this:

 --- a/thread_pthread.c
 +++ b/thread_pthread.c
 @@ -55,7 +55,7 @@ static struct {
 defined(CLOCK_REALTIME) && defined(CLOCK_MONOTONIC) && \
 -static pthread_condattr_t condattr_mono;
 +static pthread_condattr_t condattr_mono = PTHREAD_COND_INITIALIZER;
 static pthread_condattr_t *condattr_monotonic = &condattr_mono;
 static const void *const condattr_monotonic = NULL;

...And reverting your patch. Should save a few instructions at
startup since it avoids linkage and function call at startup.

#2 [ruby-core:87347] Updated by jeremyevans0 (Jeremy Evans) 4 months ago

normalperson (Eric Wong) wrote:

Btw, is PTHREAD_COND_INITIALIZER supported on OpenBSD?

It's defined but I don't think it would be usable:

/usr/include/pthread.h:#define PTHREAD_COND_INITIALIZER NULL

#3 [ruby-core:89128] Updated by taca (Takahiro Kambe) 1 day ago


The similar problem occurs on NetBSD 8.0_STABLE. (And I belive it would be occur on 7.2.)

PTHREAD_COND_INITIALIZER is for pthread_cond_t not for pthread_condattr_t.
So, initializing condattr_mono (via condattr_monotonic) with pthread_condattr_init() is correct way to fix the problem as the attached patch.

Best regards.

Oh, trunk was already applied the patch!

#4 [ruby-core:89135] Updated by normalperson (Eric Wong) about 14 hours ago

Right, already in trunk at r63548

And back to Jeremy's earlier comment:

It's defined but I don't think it would be usable:

/usr/include/pthread.h:#define PTHREAD_COND_INITIALIZER NULL

Without reading the OpenBSD pthreads library source code;
I suspect that it would be usable.

The condvar implementation can safely perform lazy-initialization
because it can rely on the underlying mutex for protection.

Also available in: Atom PDF