]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
The vthreadBase library was not designed to be unloaded and simply leaks
authorOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:01 +0000 (11:23 -0700)
committerOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:01 +0000 (11:23 -0700)
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.

open-vm-tools/lib/include/vthreadBase.h
open-vm-tools/lib/misc/vthreadBase.c

index 7161bd56262bb1379d1c18205997e0e2047886d2..5f6ece0d1888c6dec1edd9ebd028756dc6810f45 100644 (file)
@@ -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)
index 03b26a72f6c8ea09433a537abcbe20db1eb52517..c4cb70d8ea2173a5e4fc661b63d0634f28a4a903 100644 (file)
@@ -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);
+}
+
+
 /*
  *-----------------------------------------------------------------------------
  *