From: Oliver Kurth Date: Fri, 15 Sep 2017 18:23:01 +0000 (-0700) Subject: The vthreadBase library was not designed to be unloaded and simply leaks X-Git-Tag: stable-10.2.0~559 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f969faf20c307a9da02f2a3ba200ecb138d07b47;p=thirdparty%2Fopen-vm-tools.git The vthreadBase library was not designed to be unloaded and simply leaks the TLS keys it creates. Since plugins statically link it, loading and unloading may hit the OS limit. This fix adds a hook into vthreadBase to delete the TLS keys at plugin unload time. --- diff --git a/open-vm-tools/lib/include/vthreadBase.h b/open-vm-tools/lib/include/vthreadBase.h index 7161bd562..5f6ece0d1 100644 --- a/open-vm-tools/lib/include/vthreadBase.h +++ b/open-vm-tools/lib/include/vthreadBase.h @@ -121,6 +121,11 @@ void VThreadBase_ForgetSelf(void); void VThreadBase_SetNoIDFunc(void (*func)(void), void (*destr)(void *)); +/* + * See PR 1626963 and function documentation before thinking about using this. + */ +void VThreadBase_DeInitialize(void); + /* Match up historical VThread_ names with VThreadBase_ names */ static INLINE const char * VThread_CurName(void) diff --git a/open-vm-tools/lib/misc/vthreadBase.c b/open-vm-tools/lib/misc/vthreadBase.c index 03b26a72f..c4cb70d8e 100644 --- a/open-vm-tools/lib/misc/vthreadBase.c +++ b/open-vm-tools/lib/misc/vthreadBase.c @@ -251,6 +251,68 @@ VThreadBaseInitKeyWork(Atomic_Int *key, void (*deletePtr)(void*)) } } +/* + *----------------------------------------------------------------------------- + * + * VThreadBaseRemoveKey -- + * + * Abstracts TLS key deletion. + * + * Results: + * None + * + * Side effects: + * None + * + *----------------------------------------------------------------------------- + */ + +void +VThreadBaseRemoveKey(Atomic_Int *key) +{ +#if defined _WIN32 +#if defined VM_WIN_UWP + FlsFree(Atomic_Read(key)); +#else + TlsFree(Atomic_Read(key)); +#endif +#else + pthread_key_delete(Atomic_Read(key)); +#endif +} + + +/* + *----------------------------------------------------------------------------- + * + * VThreadBase_DeInitialize -- + * + * Deletes allocated TLS keys. Notice how this is only used for a very + * special case as the vthread library is not designed to be unloaded + * and normally just leaks these keys. The initialization code assumes + * that once set, these keys never revert to invalid. For the case of + * a copy of the vthread being statically linked into a dlopened library, + * we are using this to avoid leaking the keys immediately before unload. + * + * See PR 1626963 + * + * Results: + * None + * + * Side effects: + * None + * + *----------------------------------------------------------------------------- + */ + +void +VThreadBase_DeInitialize(void) +{ + VThreadBaseRemoveKey(&vthreadBaseGlobals.baseKey); + VThreadBaseRemoveKey(&vthreadBaseGlobals.threadIDKey); +} + + /* *----------------------------------------------------------------------------- *