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@winehq.org>
This commit is contained in:
Alexandre Julliard 2017-11-29 10:44:21 +01:00
parent 93eceba03e
commit f8e0bd1b0d
4 changed files with 35 additions and 7 deletions

View File

@ -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 );

View File

@ -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 */

View File

@ -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 ))

View File

@ -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;