This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: static tls memory leak
- From: Ryan Arnold <ryan dot arnold at gmail dot com>
- To: libc-alpha at sourceware dot org
- Date: Thu, 21 Apr 2011 13:49:41 -0500
- Subject: Re: static tls memory leak
- References: <20110315045404.GK6275@bubble.grove.modra.org>
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