tech-userlevel archive

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

Re: pthreads vs. overriden malloc



On Wed, Sep 13, 2017 at 06:54:22 +0200, Martin Husemann wrote:

> On Wed, Sep 13, 2017 at 02:23:22AM +0300, Valery Ushakov wrote:
> > 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).
> 
> It may cause even more fallout, see 
> src/external/gpl3/gcc.old/dist/gcc/tree-ssa-ccp.c
> 
> 
> #if defined(__NetBSD__) && defined(NETBSD_NATIVE)
> /*
>  * This is a big, ugly, temporary hack:
>  *    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59958
>  * To make sure we have configured all our targets correctly, mimic the
>  * #ifdef cascade from src/lib/libc/stdlib/jemalloc.c here and compile
>  * time assert that the value matches gcc's MALLOC_ABI_ALIGNMENT here.
>  */
> 
> #if defined(__alpha__) || defined(__amd64__) || defined(__sparc64__)    \
>      || (defined(__arm__) && defined(__ARM_EABI__)) \
>      || defined(__ia64__) || defined(__powerpc__) \
>      || ((defined(__mips__) || defined(__riscv__)) && defined(_LP64))
> #define JEMALLOC_TINY_MIN_2POW  3
> #endif

As far as I understand the relevant define here is not TINY_MIN_2POW,
but QUANTUM_2POW_MIN.

C99 requires that

       The pointer returned if the allocation succeeds is suitably
       aligned so that it may be assigned to a pointer to any type of
       object and then used to access such an object or an array of
       such objects in the space allocated

On a typical cpu that would be the alignment of "long double" I guess.
(I haven't checked, but it looks like that's what TINY_MIN_2POW
ensures for "tiny" allocation).

So i'd day that out pthreads expects from malloc something that a
conforming malloc does not guarantee.  To express the extra alignment
requirements we need to explicitly arrange for it.  posix_memalign(3)
looks like just the right API then.

-uwe


Home | Main Index | Thread Index | Old Index