mono_native_tls_get_value that does not change LastError. (mono/mono#15568)
authorJay Krell <jaykrell@microsoft.com>
Mon, 8 Jul 2019 07:44:23 +0000 (00:44 -0700)
committerJohan Lorensson <lateralusx.github@gmail.com>
Mon, 8 Jul 2019 07:44:23 +0000 (09:44 +0200)
Commit migrated from https://github.com/mono/mono/commit/f19bc9634364185a0a3628c3e1a6fd3f06ed1a89

src/mono/mono/utils/mono-tls.h

index a171329..cf99376 100644 (file)
@@ -52,7 +52,24 @@ g_static_assert (TLS_KEY_DOMAIN == 0);
 #define mono_native_tls_alloc(key,destructor) ((*(key) = TlsAlloc ()) != TLS_OUT_OF_INDEXES && destructor == NULL)
 #define mono_native_tls_free TlsFree
 #define mono_native_tls_set_value TlsSetValue
-#define mono_native_tls_get_value TlsGetValue
+
+#include <winternl.h>
+
+// TlsGetValue always writes 0 to LastError. Which can cause problems. This never changes LastError.
+//
+static inline
+void*
+mono_native_tls_get_value (unsigned index)
+{
+       PTEB const teb = NtCurrentTeb ();
+
+       if (index < TLS_MINIMUM_AVAILABLE)
+               return teb->TlsSlots [index];
+
+       void** const p = (void**)teb->TlsExpansionSlots;
+
+       return p ? p [index - TLS_MINIMUM_AVAILABLE] : NULL;
+}
 
 #else