2002-09-17 20:54:42 +02:00
|
|
|
/*
|
|
|
|
* Win32 virtual memory functions
|
|
|
|
*
|
|
|
|
* Copyright 1997, 2002 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-09-17 20:54:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2005-06-06 22:13:08 +02:00
|
|
|
#include <stdarg.h>
|
2002-09-17 20:54:42 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "ntstatus.h"
|
2005-11-28 17:32:54 +01:00
|
|
|
#define WIN32_NO_STATUS
|
2015-03-14 03:38:17 +01:00
|
|
|
#define NONAMELESSUNION
|
2005-06-06 22:13:08 +02:00
|
|
|
#include "windef.h"
|
2002-09-17 20:54:42 +02:00
|
|
|
#include "winternl.h"
|
|
|
|
#include "wine/server.h"
|
|
|
|
#include "wine/debug.h"
|
2003-10-08 21:11:08 +02:00
|
|
|
#include "ntdll_misc.h"
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(virtual);
|
|
|
|
|
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
/**********************************************************************
|
|
|
|
* RtlCreateUserStack (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlCreateUserStack( SIZE_T commit, SIZE_T reserve, ULONG zero_bits,
|
|
|
|
SIZE_T commit_align, SIZE_T reserve_align, INITIAL_TEB *stack )
|
2009-06-25 14:18:53 +02:00
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
TRACE("commit %#lx, reserve %#lx, zero_bits %u, commit_align %#lx, reserve_align %#lx, stack %p\n",
|
|
|
|
commit, reserve, zero_bits, commit_align, reserve_align, stack);
|
2009-06-25 14:18:53 +02:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
if (!commit_align || !reserve_align)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
2009-06-25 14:18:53 +02:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
if (!commit || !reserve)
|
2009-06-25 14:18:53 +02:00
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
|
|
|
|
if (!reserve) reserve = nt->OptionalHeader.SizeOfStackReserve;
|
|
|
|
if (!commit) commit = nt->OptionalHeader.SizeOfStackCommit;
|
2009-06-25 14:18:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
reserve = (reserve + reserve_align - 1) & ~(reserve_align - 1);
|
|
|
|
commit = (commit + commit_align - 1) & ~(commit_align - 1);
|
2010-04-09 16:25:25 +02:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->virtual_alloc_thread_stack( stack, reserve, commit, NULL );
|
|
|
|
}
|
2009-06-25 14:18:53 +02:00
|
|
|
|
2013-04-04 13:00:47 +02:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
/**********************************************************************
|
|
|
|
* RtlFreeUserStack (NTDLL.@)
|
|
|
|
*/
|
|
|
|
void WINAPI RtlFreeUserStack( void *stack )
|
|
|
|
{
|
|
|
|
SIZE_T size = 0;
|
2018-12-03 17:46:51 +01:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
TRACE("stack %p\n", stack);
|
2009-08-05 11:23:02 +02:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
NtFreeVirtualMemory( NtCurrentProcess(), &stack, &size, MEM_RELEASE );
|
2004-06-18 02:26:57 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* __wine_locked_recvmsg
|
|
|
|
*/
|
|
|
|
ssize_t CDECL __wine_locked_recvmsg( int fd, struct msghdr *hdr, int flags )
|
|
|
|
{
|
|
|
|
return unix_funcs->virtual_locked_recvmsg( fd, hdr, flags );
|
2013-04-04 13:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* NtAllocateVirtualMemory (NTDLL.@)
|
|
|
|
* ZwAllocateVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2019-08-01 10:07:40 +02:00
|
|
|
NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, ULONG_PTR zero_bits,
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T *size_ptr, ULONG type, ULONG protect )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtAllocateVirtualMemory( process, ret, zero_bits, size_ptr, type, protect );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtFreeVirtualMemory (NTDLL.@)
|
|
|
|
* ZwFreeVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
NTSTATUS WINAPI NtFreeVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr, ULONG type )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtFreeVirtualMemory( process, addr_ptr, size_ptr, type );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtProtectVirtualMemory (NTDLL.@)
|
|
|
|
* ZwProtectVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2019-12-04 12:02:12 +01:00
|
|
|
NTSTATUS WINAPI DECLSPEC_HOTPATCH NtProtectVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr,
|
|
|
|
ULONG new_prot, ULONG *old_prot )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2020-06-09 12:32:41 +02:00
|
|
|
return unix_funcs->NtProtectVirtualMemory( process, addr_ptr, size_ptr, new_prot, old_prot );
|
2020-01-24 21:19:38 +01:00
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2020-01-24 21:19:37 +01:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtQueryVirtualMemory (NTDLL.@)
|
|
|
|
* ZwQueryVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtQueryVirtualMemory( HANDLE process, LPCVOID addr,
|
|
|
|
MEMORY_INFORMATION_CLASS info_class,
|
|
|
|
PVOID buffer, SIZE_T len, SIZE_T *res_len )
|
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtQueryVirtualMemory( process, addr, info_class, buffer, len, res_len );
|
2020-01-24 21:19:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* NtLockVirtualMemory (NTDLL.@)
|
|
|
|
* ZwLockVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
NTSTATUS WINAPI NtLockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtLockVirtualMemory( process, addr, size, unknown );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtUnlockVirtualMemory (NTDLL.@)
|
|
|
|
* ZwUnlockVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
NTSTATUS WINAPI NtUnlockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtUnlockVirtualMemory( process, addr, size, unknown );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtCreateSection (NTDLL.@)
|
|
|
|
* ZwCreateSection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtCreateSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
|
|
|
|
const LARGE_INTEGER *size, ULONG protect,
|
|
|
|
ULONG sec_flags, HANDLE file )
|
|
|
|
{
|
2020-06-04 21:34:28 +02:00
|
|
|
return unix_funcs->NtCreateSection( handle, access, attr, size, protect, sec_flags, file );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtOpenSection (NTDLL.@)
|
|
|
|
* ZwOpenSection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtOpenSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
|
|
|
|
{
|
2020-06-04 21:34:28 +02:00
|
|
|
return unix_funcs->NtOpenSection( handle, access, attr );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtMapViewOfSection (NTDLL.@)
|
|
|
|
* ZwMapViewOfSection (NTDLL.@)
|
|
|
|
*/
|
2019-08-01 10:07:40 +02:00
|
|
|
NTSTATUS WINAPI NtMapViewOfSection( HANDLE handle, HANDLE process, PVOID *addr_ptr, ULONG_PTR zero_bits,
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T commit_size, const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
|
2002-09-17 20:54:42 +02:00
|
|
|
SECTION_INHERIT inherit, ULONG alloc_type, ULONG protect )
|
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtMapViewOfSection( handle, process, addr_ptr, zero_bits, commit_size, offset_ptr,
|
|
|
|
size_ptr, inherit, alloc_type, protect );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtUnmapViewOfSection (NTDLL.@)
|
|
|
|
* ZwUnmapViewOfSection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtUnmapViewOfSection( HANDLE process, PVOID addr )
|
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtUnmapViewOfSection( process, addr );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-24 12:32:30 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* virtual_fill_image_information
|
|
|
|
*
|
|
|
|
* Helper for NtQuerySection.
|
|
|
|
*/
|
|
|
|
void virtual_fill_image_information( const pe_image_info_t *pe_info, SECTION_IMAGE_INFORMATION *info )
|
|
|
|
{
|
|
|
|
info->TransferAddress = wine_server_get_ptr( pe_info->entry_point );
|
|
|
|
info->ZeroBits = pe_info->zerobits;
|
|
|
|
info->MaximumStackSize = pe_info->stack_size;
|
|
|
|
info->CommittedStackSize = pe_info->stack_commit;
|
|
|
|
info->SubSystemType = pe_info->subsystem;
|
|
|
|
info->SubsystemVersionLow = pe_info->subsystem_low;
|
|
|
|
info->SubsystemVersionHigh = pe_info->subsystem_high;
|
|
|
|
info->GpValue = pe_info->gp;
|
|
|
|
info->ImageCharacteristics = pe_info->image_charact;
|
|
|
|
info->DllCharacteristics = pe_info->dll_charact;
|
|
|
|
info->Machine = pe_info->machine;
|
|
|
|
info->ImageContainsCode = pe_info->contains_code;
|
2019-04-22 11:34:25 +02:00
|
|
|
info->u.ImageFlags = pe_info->image_flags & ~(IMAGE_FLAGS_WineBuiltin|IMAGE_FLAGS_WineFakeDll);
|
2018-10-24 12:32:30 +02:00
|
|
|
info->LoaderFlags = pe_info->loader_flags;
|
|
|
|
info->ImageFileSize = pe_info->file_size;
|
|
|
|
info->CheckSum = pe_info->checksum;
|
|
|
|
#ifndef _WIN64 /* don't return 64-bit values to 32-bit processes */
|
|
|
|
if (pe_info->machine == IMAGE_FILE_MACHINE_AMD64 || pe_info->machine == IMAGE_FILE_MACHINE_ARM64)
|
|
|
|
{
|
|
|
|
info->TransferAddress = (void *)0x81231234; /* sic */
|
|
|
|
info->MaximumStackSize = 0x100000;
|
|
|
|
info->CommittedStackSize = 0x10000;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-26 07:43:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* NtQuerySection (NTDLL.@)
|
|
|
|
* ZwQuerySection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtQuerySection( HANDLE handle, SECTION_INFORMATION_CLASS class, void *ptr,
|
2017-12-28 13:04:20 +01:00
|
|
|
SIZE_T size, SIZE_T *ret_size )
|
2016-07-26 07:43:02 +02:00
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtQuerySection( handle, class, ptr, size, ret_size );
|
2016-07-26 07:43:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* NtFlushVirtualMemory (NTDLL.@)
|
|
|
|
* ZwFlushVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtFlushVirtualMemory( HANDLE process, LPCVOID *addr_ptr,
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T *size_ptr, ULONG unknown )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtFlushVirtualMemory( process, addr_ptr, size_ptr, unknown );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2003-07-08 23:18:45 +02:00
|
|
|
|
|
|
|
|
2008-11-14 10:49:02 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* NtGetWriteWatch (NTDLL.@)
|
|
|
|
* ZwGetWriteWatch (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtGetWriteWatch( HANDLE process, ULONG flags, PVOID base, SIZE_T size, PVOID *addresses,
|
|
|
|
ULONG_PTR *count, ULONG *granularity )
|
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtGetWriteWatch( process, flags, base, size, addresses, count, granularity );
|
2008-11-14 10:49:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtResetWriteWatch (NTDLL.@)
|
|
|
|
* ZwResetWriteWatch (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtResetWriteWatch( HANDLE process, PVOID base, SIZE_T size )
|
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtResetWriteWatch( process, base, size );
|
2008-11-14 10:49:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-08 23:18:45 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* NtReadVirtualMemory (NTDLL.@)
|
|
|
|
* ZwReadVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtReadVirtualMemory( HANDLE process, const void *addr, void *buffer,
|
|
|
|
SIZE_T size, SIZE_T *bytes_read )
|
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtReadVirtualMemory( process, addr, buffer, size, bytes_read );
|
2003-07-08 23:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtWriteVirtualMemory (NTDLL.@)
|
|
|
|
* ZwWriteVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtWriteVirtualMemory( HANDLE process, void *addr, const void *buffer,
|
|
|
|
SIZE_T size, SIZE_T *bytes_written )
|
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtWriteVirtualMemory( process, addr, buffer, size, bytes_written );
|
2003-07-08 23:18:45 +02:00
|
|
|
}
|
2007-09-26 20:50:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtAreMappedFilesTheSame (NTDLL.@)
|
|
|
|
* ZwAreMappedFilesTheSame (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtAreMappedFilesTheSame(PVOID addr1, PVOID addr2)
|
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
return unix_funcs->NtAreMappedFilesTheSame( addr1, addr2 );
|
2007-09-26 20:50:28 +02:00
|
|
|
}
|