tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

pthreads vs. overriden malloc



I just killed quite some time debugging a weird lockup I had with a
third party program under NetBSD.

Long story short...  That program overrides malloc and returns memory
that is only 8 bytes aligned.  That creates problems for our pthreads,
as rwlock code assumes that pthread_self() is 16-byte aligned and uses
the lower bits for the flags (see RW_THREAD in pthread_int.h and code
in pthread_rwlock.c).

The two approaches I can think of are to use posix_memalign+memset
instead of calloc and to use calloc and manually re-align the result.

So either

    newthread = calloc(1, __pthread_st_size + ~RW_THREAD);
    if ((uintptr_t)newthread & ~RW_THREAD) {
        newthread = (pthread_t)(((uintptr_t)(newthread)
                                 + ~RW_THREAD) & RW_THREAD);
    }

or

    posix_memalign((void **)&newthread, -RW_THREAD, __pthread_st_size);
    memset(newthread, 0, __pthread_st_size);


The latter seems a bit prettier, but many older programs that override
malloc don't override posix_memalign, so we will still use our own
malloc for this.  Not sure if this is a problem.  Debugging mallocs
probably all properly override posix_memalign.

Ideas?

-uwe


Home | Main Index | Thread Index | Old Index