Alexandre Julliard : ntdll: Put the initial pthread stack at the end of the Win32 stack.

Alexandre Julliard julliard at winehq.org
Wed Nov 29 15:12:30 CST 2017


Module: wine
Branch: master
Commit: f8e0bd1b0d189d5950dc39082f439cd1fc9569d5
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=f8e0bd1b0d189d5950dc39082f439cd1fc9569d5

Author: Alexandre Julliard <julliard at winehq.org>
Date:   Wed Nov 29 10:44:21 2017 +0100

ntdll: Put the initial pthread stack at the end of the Win32 stack.

Create a separate view for it so that the main stack can be freed
independently.

Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/ntdll/loader.c     |  2 +-
 dlls/ntdll/ntdll_misc.h |  4 +++-
 dlls/ntdll/thread.c     | 15 ++++++++++++---
 dlls/ntdll/virtual.c    | 21 +++++++++++++++++++--
 4 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c
index c5cfdce..f7fd8da 100644
--- a/dlls/ntdll/loader.c
+++ b/dlls/ntdll/loader.c
@@ -3144,7 +3144,7 @@ void WINAPI LdrInitializeThunk( void *kernel_start, ULONG_PTR unknown2,
     RemoveEntryList( &wm->ldr.InMemoryOrderModuleList );
     InsertHeadList( &peb->LdrData->InMemoryOrderModuleList, &wm->ldr.InMemoryOrderModuleList );
 
-    if ((status = virtual_alloc_thread_stack( NtCurrentTeb(), 0, 0 )) != STATUS_SUCCESS) goto error;
+    if ((status = virtual_alloc_thread_stack( NtCurrentTeb(), 0, 0, 0 )) != STATUS_SUCCESS) goto error;
     if ((status = server_init_process_done( &context )) != STATUS_SUCCESS) goto error;
 
     status = wine_call_on_stack( attach_dlls, (void *)1, (char *)NtCurrentTeb()->Tib.StackBase - page_size );
diff --git a/dlls/ntdll/ntdll_misc.h b/dlls/ntdll/ntdll_misc.h
index e469afd..c8a870c 100644
--- a/dlls/ntdll/ntdll_misc.h
+++ b/dlls/ntdll/ntdll_misc.h
@@ -165,7 +165,8 @@ extern NTSTATUS nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES *attr, ANSI_S
 /* virtual memory */
 extern void virtual_get_system_info( SYSTEM_BASIC_INFORMATION *info ) DECLSPEC_HIDDEN;
 extern NTSTATUS virtual_create_builtin_view( void *base ) DECLSPEC_HIDDEN;
-extern NTSTATUS virtual_alloc_thread_stack( TEB *teb, SIZE_T reserve_size, SIZE_T commit_size ) DECLSPEC_HIDDEN;
+extern NTSTATUS virtual_alloc_thread_stack( TEB *teb, SIZE_T reserve_size,
+                                            SIZE_T commit_size, SIZE_T extra_size ) DECLSPEC_HIDDEN;
 extern void virtual_clear_thread_stack(void) DECLSPEC_HIDDEN;
 extern BOOL virtual_handle_stack_fault( void *addr ) DECLSPEC_HIDDEN;
 extern BOOL virtual_is_valid_code_address( const void *addr, SIZE_T size ) DECLSPEC_HIDDEN;
@@ -224,6 +225,7 @@ struct ntdll_thread_data
     WINE_VM86_TEB_INFO __vm86;        /* FIXME: placeholder for vm86 data from struct x86_thread_data */
 #endif
     struct debug_info *debug_info;    /* info for debugstr functions */
+    void              *start_stack;   /* stack for thread startup */
     int                request_fd;    /* fd for sending server requests */
     int                reply_fd;      /* fd for receiving server replies */
     int                wait_fd[2];    /* fd for sleeping server requests */
diff --git a/dlls/ntdll/thread.c b/dlls/ntdll/thread.c
index 9c7cd13..80f4780 100644
--- a/dlls/ntdll/thread.c
+++ b/dlls/ntdll/thread.c
@@ -408,6 +408,7 @@ HANDLE thread_init(void)
  */
 static void free_thread_data( TEB *teb )
 {
+    struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
     SIZE_T size;
 
     if (teb->DeallocationStack)
@@ -415,6 +416,11 @@ static void free_thread_data( TEB *teb )
         size = 0;
         NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
     }
+    if (thread_data->start_stack)
+    {
+        size = 0;
+        NtFreeVirtualMemory( GetCurrentProcess(), &thread_data->start_stack, &size, MEM_RELEASE );
+    }
     signal_free_thread( teb );
 }
 
@@ -621,16 +627,19 @@ NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *
     info->entry_point = start;
     info->entry_arg   = param;
 
+    if ((status = virtual_alloc_thread_stack( teb, stack_reserve, stack_commit, PTHREAD_STACK_MIN )))
+        goto error;
+
     thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
     thread_data->request_fd  = request_pipe[1];
     thread_data->reply_fd    = -1;
     thread_data->wait_fd[0]  = -1;
     thread_data->wait_fd[1]  = -1;
-
-    if ((status = virtual_alloc_thread_stack( teb, stack_reserve, stack_commit ))) goto error;
+    thread_data->start_stack = (char *)teb->Tib.StackBase;
 
     pthread_attr_init( &attr );
-    pthread_attr_setstacksize( &attr, PTHREAD_STACK_MIN );
+    pthread_attr_setstack( &attr, teb->DeallocationStack,
+                         (char *)teb->Tib.StackBase + PTHREAD_STACK_MIN - (char *)teb->DeallocationStack );
     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 ))
diff --git a/dlls/ntdll/virtual.c b/dlls/ntdll/virtual.c
index ab27077..8325b28 100644
--- a/dlls/ntdll/virtual.c
+++ b/dlls/ntdll/virtual.c
@@ -1758,7 +1758,7 @@ NTSTATUS virtual_create_builtin_view( void *module )
 /***********************************************************************
  *           virtual_alloc_thread_stack
  */
-NTSTATUS virtual_alloc_thread_stack( TEB *teb, SIZE_T reserve_size, SIZE_T commit_size )
+NTSTATUS virtual_alloc_thread_stack( TEB *teb, SIZE_T reserve_size, SIZE_T commit_size, SIZE_T extra_size )
 {
     struct file_view *view;
     NTSTATUS status;
@@ -1778,7 +1778,7 @@ NTSTATUS virtual_alloc_thread_stack( TEB *teb, SIZE_T reserve_size, SIZE_T commi
 
     server_enter_uninterrupted_section( &csVirtual, &sigset );
 
-    if ((status = map_view( &view, NULL, size, 0xffff, 0,
+    if ((status = map_view( &view, NULL, size + extra_size, 0xffff, 0,
                             VPROT_READ | VPROT_WRITE | VPROT_COMMITTED )) != STATUS_SUCCESS)
         goto done;
 
@@ -1793,6 +1793,23 @@ NTSTATUS virtual_alloc_thread_stack( TEB *teb, SIZE_T reserve_size, SIZE_T commi
     mprotect_range( view->base, 2 * page_size, 0, 0 );
     VIRTUAL_DEBUG_DUMP_VIEW( view );
 
+    if (extra_size)
+    {
+        struct file_view *extra_view;
+
+        /* shrink the first view and create a second one for the extra size */
+        /* this allows the app to free the stack without freeing the thread start portion */
+        view->size -= extra_size;
+        status = create_view( &extra_view, (char *)view->base + view->size, extra_size,
+                              VPROT_READ | VPROT_WRITE | VPROT_COMMITTED );
+        if (status != STATUS_SUCCESS)
+        {
+            unmap_area( (char *)view->base + view->size, extra_size );
+            delete_view( view );
+            goto done;
+        }
+    }
+
     /* note: limit is lower than base since the stack grows down */
     teb->DeallocationStack = view->base;
     teb->Tib.StackBase     = (char *)view->base + view->size;




More information about the wine-cvs mailing list