[mono-threads] Fix lifetime of MonoThreadInfo->handle (#3539)
authorLudovic Henry <ludovic@xamarin.com>
Fri, 9 Sep 2016 09:38:30 +0000 (11:38 +0200)
committerGitHub <noreply@github.com>
Fri, 9 Sep 2016 09:38:30 +0000 (11:38 +0200)
commit299b257efbcb9c90bfdd7cbee3518d6cd39a00a6
tree8e9acd2afc92bd710a7f3ef2d4c9da28ebda61ce
parent2acdc49ff6b86bcd6179aa5bd6d861d37709dcf7
[mono-threads] Fix lifetime of MonoThreadInfo->handle (#3539)

This handle should have a total of maximum 3 copies:
 - in MonoThreadInfo->handle
 - in the return value of mono_threads_create_thread
 - in MonoInternalThread->handle (if it's a managed thread)

Before, there would always be 2 references (in MonoThreadInfo->handle and another another for MonoInternalThread->handle), even if it would be referenced at one place only (MonoThreadInfo->handle), which would lead to a handle leak.

We also need to duplicate the returned handle, to avoid having a use-after-free of the handle (in case the thread exits before we access that handle)
mono/metadata/appdomain.c
mono/metadata/threads.c
mono/mini/aot-compiler.c
mono/utils/mono-threads-posix.c
mono/utils/mono-threads-windows.c
mono/utils/mono-threads.c
mono/utils/mono-threads.h