static tls memory leak

Ryan Arnold ryan.arnold@gmail.com
Thu Apr 21 18:51:00 GMT 2011


On Mon, Mar 14, 2011 at 11:54 PM, Alan Modra <amodra@gmail.com> wrote:
> This patch fixes some errors in static tls handling, TLS_DTV_AT_TP
> case.  tst-tls13 fails on powerpc64 if gcc happens to align the TLS
> segment, due to dlclose not freeing static tls space properly.  With
> one version of gcc, at -O3 I was seeing tst-tlsmod13a with
>  TLS 0x000e70 0x0000000000010e70 0x0000000000010e70 0x000000 0x000008 R 0x10
> At -O2
>  TLS 0x000e70 0x0000000000010e70 0x0000000000010e70 0x000000 0x000008 R 0x4
> The two object files are byte for byte identical except for this
> alignment difference, 16 vs 4, and the corresponding alignment
> difference in the .tbss section.  I'm sure it is quite permissible for
> gcc to request a larger alignment.
>
> So in the -O3 case glibc allocates 8 bytes for tlsmod13, pads 8 bytes,
> then allocates 8 bytes for tlsmod13a.  On dlclose, the 8 bytes of
> tlsmod13a space is freed, but the pad confuses dlclose code when
> trying to free the block for tlsmod13, so it isn't freed.  Net result
> is a leak of 16 bytes every dlopen/dlclose cycle.  glibc allocates
> TLS_STATIC_SURPLUS = 64 + 16 * 100 = 1664 bytes space for static TLS
> which explains why we bomb on the 104th iteration in the test loop.
>
> Note that the padding belongs to the second tls block allocated, not
> the first.  (It's the second block alignment that causes glibc to pad,
> not the first block alignment.)  Thus a proper fix means tracking the
> padding added at dlopen time and freeing it at dlclose, as I do in the
> following patch.  I've also fixed an error in dl-close.c "Extend the
> contiguous chunk being reclaimed" code, and better supported
> non-contiguous freeing as was done for TLS_TCB_AT_TP.
>
> This patch does not teach the TLS_TCB_AT_TP code to similarly free
> padding.  The _dl_debug_printf statements I used when developing this
> patch should help anyone who wants to fix x86.  If you'd like a patch
> without the debug print, please ask.
>
> 2011-03-15  Alan Modra  <amodra@gmail.com>
>
>        * elf/dl-reloc.c (_dl_try_allocate_static_tls <TLS_DTV_AT_TP>): Handle
>        l_tls_firstbyte_offset non-zero.  Save padding offset in
>        l_tls_firstbyte_offset for later use.  Add debug print.
>        * elf/dl-close.c (_dl_close_worker <TLS_DTV_AT_TP>): Correct code
>        freeing static tls block.  Add debug print.
>        * elf/fl-tls.c (_dl_determine_tlsoffset): Add debug print.
> ...
> --
> Alan Modra
> Australia Development Lab, IBM

Can the patch attached to Alan's email please be considered for committing?

Ryan S. Arnold
IBM Linux Technology Center



More information about the Libc-alpha mailing list