From ef57b2c7cadda78730cb57e117507c7a8a0ff6cb Mon Sep 17 00:00:00 2001
From: Sebastian Lackner <sebastian@fds-team.de>
Date: Wed, 13 Apr 2016 03:11:03 +0200
Subject: ntdll: Use a separate stack when starting new threads.

---
 dlls/ntdll/thread.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/dlls/ntdll/thread.c b/dlls/ntdll/thread.c
index 07dc285..db27447 100644
--- a/dlls/ntdll/thread.c
+++ b/dlls/ntdll/thread.c
@@ -25,6 +25,7 @@
 #include <stdarg.h>
 #include <string.h>
 #include <stdio.h>
+#include <limits.h>
 #include <sys/types.h>
 #ifdef HAVE_SYS_MMAN_H
 #include <sys/mman.h>
@@ -452,15 +453,8 @@ void exit_thread( int status )
 static void start_thread( struct startup_info *info )
 {
     TEB *teb = info->teb;
-    struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
     PRTL_THREAD_START_ROUTINE func = info->entry_point;
     void *arg = info->entry_arg;
-    struct debug_info debug_info;
-
-    debug_info.str_pos = debug_info.strings;
-    debug_info.out_pos = debug_info.output;
-    thread_data->debug_info = &debug_info;
-    thread_data->pthread_id = pthread_self();
 
     signal_init_thread( teb );
     server_init_thread( func );
@@ -476,6 +470,26 @@ static void start_thread( struct startup_info *info )
 
 
 /***********************************************************************
+ *           call_start_thread
+ *
+ * Setup debug_info struct and call start_thread on target stack.
+ */
+static void call_start_thread( struct startup_info *info )
+{
+    TEB *teb = info->teb;
+    struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SpareBytes1;
+    struct debug_info debug_info;
+
+    debug_info.str_pos = debug_info.strings;
+    debug_info.out_pos = debug_info.output;
+    thread_data->debug_info = &debug_info;
+    thread_data->pthread_id = pthread_self();
+
+    wine_switch_to_stack( (void (*)(void *))start_thread, info, teb->Tib.StackBase );
+}
+
+
+/***********************************************************************
  *              RtlCreateUserThread   (NTDLL.@)
  */
 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
@@ -581,11 +595,10 @@ NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *
     if ((status = virtual_alloc_thread_stack( teb, stack_reserve, stack_commit ))) goto error;
 
     pthread_attr_init( &attr );
-    pthread_attr_setstack( &attr, teb->DeallocationStack,
-                           (char *)teb->Tib.StackBase - (char *)teb->DeallocationStack );
+    pthread_attr_setstacksize( &attr, PTHREAD_STACK_MIN );
     pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread */
     interlocked_xchg_add( &nb_threads, 1 );
-    if (pthread_create( &pthread_id, &attr, (void * (*)(void *))start_thread, info ))
+    if (pthread_create( &pthread_id, &attr, (void * (*)(void *))call_start_thread, info ))
     {
         interlocked_xchg_add( &nb_threads, -1 );
         pthread_attr_destroy( &attr );
-- 
2.7.1

