ReactOS 0.4.17-dev-684-ga6524ef
NtMapViewOfSection.c File Reference
#include "precomp.h"
#include <pseh/pseh2.h>
Include dependency graph for NtMapViewOfSection.c:

Go to the source code of this file.

Classes

struct  _SECTION_CONTENTS_IMAGE_FILE
 
struct  _RAW_SIZE_IMAGE_FILE
 

Macros

#define IsX86()   FALSE
 
#define BYTES4(x)   x, x, x, x
 
#define BYTES8(x)   BYTES4(x), BYTES4(x)
 
#define BYTES16(x)   BYTES8(x), BYTES8(x)
 
#define BYTES32(x)   BYTES16(x), BYTES16(x)
 
#define BYTES64(x)   BYTES32(x), BYTES32(x)
 
#define BYTES128(x)   BYTES64(x), BYTES64(x)
 
#define BYTES256(x)   BYTES128(x), BYTES128(x)
 
#define BYTES512(x)   BYTES256(x), BYTES256(x)
 
#define BYTES1024(x)   BYTES512(x), BYTES512(x)
 
#define TEST_BYTE(n, v)   StartSeh() ok_hex(Bytes[n], v); EndSeh(STATUS_SUCCESS);
 
#define TEST_WRITE(n)   StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_SUCCESS);
 
#define TEST_NOWRITE(n)   StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_ACCESS_VIOLATION);
 
#define TEST_BYTE(n, v)
 

Functions

void Test_PageFileSection (void)
 
void Test_ImageSection (void)
 
void Test_ImageSection2 (void)
 
void Test_BasedSection (void)
 
 C_ASSERT (FIELD_OFFSET(struct _SECTION_CONTENTS_IMAGE_FILE, text_data)==0x400)
 
 C_ASSERT (FIELD_OFFSET(struct _SECTION_CONTENTS_IMAGE_FILE, rossym_data)==0x800)
 
 C_ASSERT (FIELD_OFFSET(struct _SECTION_CONTENTS_IMAGE_FILE, rsrc_data)==0xc00)
 
 C_ASSERT (FIELD_OFFSET(struct _SECTION_CONTENTS_IMAGE_FILE, clc_data)==0x1000)
 
static void Test_SectionContents (BOOL Relocate)
 
 C_ASSERT (FIELD_OFFSET(struct _RAW_SIZE_IMAGE_FILE, text_data)==0x400)
 
 C_ASSERT (FIELD_OFFSET(struct _RAW_SIZE_IMAGE_FILE, data_data)==0x1600)
 
 C_ASSERT (FIELD_OFFSET(struct _RAW_SIZE_IMAGE_FILE, rsrc_data)==0x2800)
 
static void Test_RawSize (ULONG TestNumber)
 
static void Test_EmptyFile (VOID)
 
static void Test_Truncate (VOID)
 
 START_TEST (NtMapViewOfSection)
 

Variables

static struct _SECTION_CONTENTS_IMAGE_FILE SectionContentsImageFile
 
static struct _RAW_SIZE_IMAGE_FILE RawSizeImageFile
 

Macro Definition Documentation

◆ BYTES1024

#define BYTES1024 (   x)    BYTES512(x), BYTES512(x)

Definition at line 1198 of file NtMapViewOfSection.c.

◆ BYTES128

#define BYTES128 (   x)    BYTES64(x), BYTES64(x)

Definition at line 1195 of file NtMapViewOfSection.c.

◆ BYTES16

#define BYTES16 (   x)    BYTES8(x), BYTES8(x)

Definition at line 1192 of file NtMapViewOfSection.c.

◆ BYTES256

#define BYTES256 (   x)    BYTES128(x), BYTES128(x)

Definition at line 1196 of file NtMapViewOfSection.c.

◆ BYTES32

#define BYTES32 (   x)    BYTES16(x), BYTES16(x)

Definition at line 1193 of file NtMapViewOfSection.c.

◆ BYTES4

#define BYTES4 (   x)    x, x, x, x

Definition at line 1190 of file NtMapViewOfSection.c.

◆ BYTES512

#define BYTES512 (   x)    BYTES256(x), BYTES256(x)

Definition at line 1197 of file NtMapViewOfSection.c.

◆ BYTES64

#define BYTES64 (   x)    BYTES32(x), BYTES32(x)

Definition at line 1194 of file NtMapViewOfSection.c.

◆ BYTES8

#define BYTES8 (   x)    BYTES4(x), BYTES4(x)

Definition at line 1191 of file NtMapViewOfSection.c.

◆ IsX86

#define IsX86 ( )    FALSE

Definition at line 16 of file NtMapViewOfSection.c.

◆ TEST_BYTE [1/2]

#define TEST_BYTE (   n,
  v 
)    StartSeh() ok_hex(Bytes[n], v); EndSeh(STATUS_SUCCESS);

◆ TEST_BYTE [2/2]

#define TEST_BYTE (   n,
  v 
)
Value:
ok(Bytes[n] == v, "[%lu] Bytes[%u] = 0x%x, expected 0x%x\n", \
TestNumber, n, Bytes[n], v); \
EndSeh(STATUS_SUCCESS);
#define StartSeh()
Definition: apitest.h:93
const GLdouble * v
Definition: gl.h:2040
GLdouble n
Definition: glext.h:7729
_In_ UINT Bytes
Definition: mmcopy.h:9
#define STATUS_SUCCESS
Definition: shellext.h:65

◆ TEST_NOWRITE

#define TEST_NOWRITE (   n)    StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_ACCESS_VIOLATION);

◆ TEST_WRITE

#define TEST_WRITE (   n)    StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_SUCCESS);

Function Documentation

◆ C_ASSERT() [1/7]

C_ASSERT ( FIELD_OFFSET(struct _RAW_SIZE_IMAGE_FILE, data_data)  = =0x1600)

◆ C_ASSERT() [2/7]

C_ASSERT ( FIELD_OFFSET(struct _RAW_SIZE_IMAGE_FILE, rsrc_data)  = =0x2800)

◆ C_ASSERT() [3/7]

C_ASSERT ( FIELD_OFFSET(struct _RAW_SIZE_IMAGE_FILE, text_data)  = =0x400)

◆ C_ASSERT() [4/7]

C_ASSERT ( FIELD_OFFSET(struct _SECTION_CONTENTS_IMAGE_FILE, clc_data)  = =0x1000)

◆ C_ASSERT() [5/7]

C_ASSERT ( FIELD_OFFSET(struct _SECTION_CONTENTS_IMAGE_FILE, rossym_data)  = =0x800)

◆ C_ASSERT() [6/7]

C_ASSERT ( FIELD_OFFSET(struct _SECTION_CONTENTS_IMAGE_FILE, rsrc_data)  = =0xc00)

◆ C_ASSERT() [7/7]

C_ASSERT ( FIELD_OFFSET(struct _SECTION_CONTENTS_IMAGE_FILE, text_data)  = =0x400)

◆ START_TEST()

START_TEST ( NtMapViewOfSection  )

Definition at line 2063 of file NtMapViewOfSection.c.

2064{
2070 Test_RawSize(0);
2071 Test_RawSize(1);
2072 Test_RawSize(2);
2074 Test_Truncate();
2075}
static void Test_SectionContents(BOOL Relocate)
void Test_PageFileSection(void)
void Test_ImageSection(void)
static void Test_EmptyFile(VOID)
void Test_BasedSection(void)
static void Test_RawSize(ULONG TestNumber)
static void Test_Truncate(VOID)
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117

◆ Test_BasedSection()

void Test_BasedSection ( void  )

Definition at line 1115 of file NtMapViewOfSection.c.

1116{
1118 HANDLE SectionHandle1, SectionHandle2;
1120 PVOID BaseAddress1, BaseAddress2;
1122
1123 /* Create a based section with SEC_COMMIT */
1124 MaximumSize.QuadPart = 0x1000;
1125 Status = NtCreateSection(&SectionHandle1,
1127 NULL,
1128 &MaximumSize,
1131 NULL);
1133
1134 /* Map the 1st section */
1135 BaseAddress1 = NULL;
1136 SectionOffset.QuadPart = 0;
1137 ViewSize = 0;
1138 Status = NtMapViewOfSection(SectionHandle1,
1140 &BaseAddress1,
1141 0,
1142 0,
1144 &ViewSize,
1145 ViewShare,
1146 0,
1148#if 0 // WOW64?
1150#else
1152#endif
1153
1154 /* Create a 2nd based section with SEC_COMMIT */
1155 MaximumSize.QuadPart = 0x1000;
1156 Status = NtCreateSection(&SectionHandle2,
1158 NULL,
1159 &MaximumSize,
1162 NULL);
1164
1165 /* Map the 2nd section */
1166 BaseAddress2 = NULL;
1167 SectionOffset.QuadPart = 0;
1168 ViewSize = 0;
1169 Status = NtMapViewOfSection(SectionHandle2,
1171 &BaseAddress2,
1172 0,
1173 0,
1175 &ViewSize,
1176 ViewShare,
1177 0,
1179#if 0 // WOW64?
1181#else
1183 ok((ULONG_PTR)BaseAddress2 < (ULONG_PTR)BaseAddress1,
1184 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
1185 ok(((ULONG_PTR)BaseAddress1 - (ULONG_PTR)BaseAddress2) == 0x10000,
1186 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
1187#endif
1188}
NTSTATUS NTAPI NtCreateSection(OUT PHANDLE SectionHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN PLARGE_INTEGER MaximumSize OPTIONAL, IN ULONG SectionPageProtection OPTIONAL, IN ULONG AllocationAttributes, IN HANDLE FileHandle OPTIONAL)
Definition: section.c:3090
NTSTATUS NTAPI NtMapViewOfSection(_In_ HANDLE SectionHandle, _In_ HANDLE ProcessHandle, _Outptr_result_bytebuffer_(*ViewSize) _Pre_valid_ PVOID *BaseAddress, _In_ ULONG_PTR ZeroBits, _In_ SIZE_T CommitSize, _Inout_opt_ PLARGE_INTEGER SectionOffset, _Inout_ PSIZE_T ViewSize, _In_range_(ViewShare, ViewUnmap) SECTION_INHERIT InheritDisposition, _In_ ULONG AllocationType, _In_ ULONG Win32Protect)
Definition: section.c:3271
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
#define ok(value,...)
Definition: atltest.h:57
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
Status
Definition: gdiplustypes.h:24
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize _Pre_valid_ PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:411
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:366
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize _Pre_valid_ PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:412
#define SEC_COMMIT
Definition: mmtypes.h:100
#define PAGE_READWRITE
Definition: nt_native.h:1307
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1296
#define NtCurrentProcess()
Definition: nt_native.h:1660
@ ViewShare
Definition: nt_native.h:1281
#define SEC_BASED
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:354
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by START_TEST().

◆ Test_EmptyFile()

static void Test_EmptyFile ( VOID  )
static

Definition at line 1922 of file NtMapViewOfSection.c.

1923{
1925 WCHAR TempPath[MAX_PATH];
1927 HANDLE Handle;
1928 HANDLE SectionHandle;
1929 ULONG Length;
1930
1931 Length = GetTempPathW(MAX_PATH, TempPath);
1932 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1933 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1934 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1937 0,
1938 NULL,
1940 0,
1941 NULL);
1943 {
1944 skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1945 return;
1946 }
1947
1948 Status = NtCreateSection(&SectionHandle,
1952
1953 if (NT_SUCCESS(Status))
1954 NtClose(SectionHandle);
1955
1956 Status = NtCreateSection(&SectionHandle,
1960
1961 if (NT_SUCCESS(Status))
1962 NtClose(SectionHandle);
1963
1966}
#define skip(...)
Definition: atltest.h:64
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define CloseHandle
Definition: compat.h:739
#define PAGE_READONLY
Definition: compat.h:138
#define SECTION_MAP_READ
Definition: compat.h:139
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
#define CreateFileW
Definition: compat.h:741
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
DWORD WINAPI GetTempPathW(IN DWORD count, OUT LPWSTR path)
Definition: path.c:1999
#define L(x)
Definition: resources.c:13
UINT WINAPI GetTempFileNameW(IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, IN UINT uUnique, OUT LPWSTR lpTempFileName)
Definition: filename.c:84
ULONG Handle
Definition: gdb_input.c:15
#define CREATE_ALWAYS
Definition: disk.h:72
#define SEC_IMAGE
Definition: mmtypes.h:97
#define SECTION_QUERY
Definition: nt_native.h:1290
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3429
#define FILE_ALL_ACCESS
Definition: nt_native.h:651
#define STANDARD_RIGHTS_REQUIRED
Definition: nt_native.h:63
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define STATUS_INVALID_FILE_FOR_SECTION
Definition: ntstatus.h:362
#define STATUS_MAPPED_FILE_SIZE_ZERO
Definition: ntstatus.h:616
short WCHAR
Definition: pedump.c:58
uint32_t ULONG
Definition: typedefs.h:59
DWORD WINAPI GetLastError(void)
Definition: except.c:1042

Referenced by START_TEST().

◆ Test_ImageSection()

void Test_ImageSection ( void  )

Definition at line 760 of file NtMapViewOfSection.c.

761{
766 WCHAR TestDllPath[MAX_PATH];
767 HANDLE FileHandle, DataSectionHandle, ImageSectionHandle;
768 PVOID DataBase, ImageBase;
770
771 GetModuleFileNameW(NULL, TestDllPath, RTL_NUMBER_OF(TestDllPath));
772 wcsrchr(TestDllPath, L'\\')[1] = UNICODE_NULL;
773 StringCbCatW(TestDllPath, sizeof(TestDllPath), L"testdata\\test.dll");
774 if (!RtlDosPathNameToNtPathName_U(TestDllPath,
775 &FileName,
776 NULL,
777 NULL))
778 {
779 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
780 return;
781 }
782
784 &FileName,
785 0,
786 NULL,
787 NULL);
788
796 if (!NT_SUCCESS(Status))
797 {
798 skip("Failed to open file %s\n", wine_dbgstr_wn(FileName.Buffer, FileName.Length / sizeof(WCHAR)));
799 RtlFreeHeap(RtlGetProcessHeap(), 0, FileName.Buffer);
800 return;
801 }
802 RtlFreeHeap(RtlGetProcessHeap(), 0, FileName.Buffer);
803
804 /* Create a data section with write access */
805 Status = NtCreateSection(&DataSectionHandle,
806 SECTION_ALL_ACCESS, // DesiredAccess
807 NULL, // ObjectAttributes
808 NULL, // MaximumSize
809 PAGE_READWRITE, // SectionPageProtection
810 SEC_COMMIT, // AllocationAttributes
811 FileHandle);
813 if (!NT_SUCCESS(Status))
814 {
815 skip("Failed to create data section\n");
817 return;
818 }
819
820 /* Map the data section as flat mapping */
821 DataBase = NULL;
822 ViewSize = 0;
823 Status = NtMapViewOfSection(DataSectionHandle,
825 &DataBase,
826 0,
827 0,
828 NULL,
829 &ViewSize,
830 ViewShare,
831 0,
834 //ok(ViewSize == 0x3f95cc48, "ViewSize wrong: 0x%lx\n");
835 if (!NT_SUCCESS(Status))
836 {
837 skip("Failed to map view of data section\n");
838 NtClose(DataSectionHandle);
840 return;
841 }
842
843 /* Check the original data */
844 ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
845
846 /* Modify the PE header (but do not flush!) */
847 *(ULONG*)DataBase = 0xdeadbabe;
848 ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
849
850 /* Modify data in the .data section (but do not flush!) */
851 ok(*(ULONG*)((PUCHAR)DataBase + 0x800) == 0x12345678,
852 "Data in .data section invalid: 0x%lx!\n", *(ULONG*)((PUCHAR)DataBase + 0x800));
853 *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x87654321;
854
855 /* Now try to create an image section (should fail) */
856 Status = NtCreateSection(&ImageSectionHandle,
857 SECTION_ALL_ACCESS, // DesiredAccess
858 NULL, // ObjectAttributes
859 NULL, // MaximumSize
860 PAGE_READWRITE, // SectionPageProtection
861 SEC_IMAGE, // AllocationAttributes
862 FileHandle);
864 if (NT_SUCCESS(Status)) NtClose(ImageSectionHandle);
865
866 /* Restore the original header */
867 *(ULONG*)DataBase = 0x00905a4d;
868
869 /* Modify data in the .data section (but do not flush!) */
870 ok_hex(*(ULONG*)((PUCHAR)DataBase + 0x800), 0x87654321);
871 *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xdeadbabe;
872
873 /* Try to create an image section again */
874 Status = NtCreateSection(&ImageSectionHandle,
875 SECTION_ALL_ACCESS, // DesiredAccess
876 NULL, // ObjectAttributes
877 NULL, // MaximumSize
878 PAGE_READWRITE, // SectionPageProtection
879 SEC_IMAGE, // AllocationAttributes
880 FileHandle);
882 if (!NT_SUCCESS(Status))
883 {
884 skip("Failed to create image section\n");
885 NtClose(DataSectionHandle);
887 return;
888 }
889
890 /* Map the image section */
891 ImageBase = NULL;
892 ViewSize = 0;
893 Status = NtMapViewOfSection(ImageSectionHandle,
895 &ImageBase,
896 0, // ZeroBits
897 0, // CommitSize
898 NULL, // SectionOffset
899 &ViewSize,
900 ViewShare,
901 0, // AllocationType
903#ifdef _M_IX86
905#else
907#endif
908 if (!NT_SUCCESS(Status))
909 {
910 skip("Failed to map view of image section\n");
911 NtClose(ImageSectionHandle);
912 NtClose(DataSectionHandle);
914 return;
915 }
916
917 /* Check the header */
918 ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
919 ok(*(ULONG*)ImageBase == 0x00905a4d, "Header not ok\n");
920
921 /* Check the data section. Either of these can be present! */
922 ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
923 (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
924 "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
925
926 /* Now modify the data again */
927 *(ULONG*)DataBase = 0xdeadbabe;
928 *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xf00dada;
929
930 /* Check the data */
931 ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
932 ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
933 ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
934 (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
935 "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
936
937 /* Flush the view */
938 ViewSize = 0x1000;
940 &DataBase,
941 &ViewSize,
944
945 /* Check the data again */
946 ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
947 ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
948 (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
949 "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
950
951 /* Restore the original header */
952 *(ULONG*)DataBase = 0x00905a4d;
953 ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
954
955 /* Close the image mapping */
957 NtClose(ImageSectionHandle);
958
959 /* Create an image section again */
960 Status = NtCreateSection(&ImageSectionHandle,
961 SECTION_ALL_ACCESS, // DesiredAccess
962 NULL, // ObjectAttributes
963 NULL, // MaximumSize
964 PAGE_READWRITE, // SectionPageProtection
965 SEC_IMAGE, // AllocationAttributes
966 FileHandle);
968 if (!NT_SUCCESS(Status))
969 {
970 skip("Failed to create image section\n");
971 NtClose(DataSectionHandle);
973 return;
974 }
975
976 /* Map the image section again */
977 ImageBase = NULL;
978 ViewSize = 0;
979 Status = NtMapViewOfSection(ImageSectionHandle,
981 &ImageBase,
982 0,
983 0,
984 NULL,
985 &ViewSize,
986 ViewShare,
987 0,
989#ifdef _M_IX86
991#else
993#endif
994 if (!NT_SUCCESS(Status))
995 {
996 skip("Failed to map view of image section\n");
997 NtClose(ImageSectionHandle);
998 NtClose(DataSectionHandle);
1000 return;
1001 }
1002
1003 // This one doesn't always work, needs investigation
1004 /* Check the .data section again */
1005 //ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada,
1006 // "Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
1007
1008 /* Restore the original data */
1009 *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x12345678;
1010
1011 /* Close the data mapping */
1013
1014 NtClose(DataSectionHandle);
1015
1016 /* Try to allocate memory inside the image mapping */
1017 DataBase = (PUCHAR)ImageBase + 0x20000;
1018 ViewSize = 0x1000;
1021
1022 /* Cleanup */
1024 NtClose(ImageSectionHandle);
1026}
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3498
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define ok_hex(expression, result)
Definition: atltest.h:94
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:634
#define wcsrchr
Definition: compat.h:16
#define GENERIC_READ
Definition: compat.h:135
#define FILE_SHARE_READ
Definition: compat.h:136
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
struct _FileName FileName
Definition: fatprocs.h:897
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:115
NTSYSAPI BOOLEAN NTAPI RtlDosPathNameToNtPathName_U(_In_opt_z_ PCWSTR DosPathName, _Out_ PUNICODE_STRING NtPathName, _Out_opt_ PCWSTR *NtFileNamePart, _Out_opt_ PRTL_RELATIVE_NAME_U DirectoryInfo)
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3951
#define SYNCHRONIZE
Definition: nt_native.h:61
#define MEM_RESERVE
Definition: nt_native.h:1317
#define GENERIC_WRITE
Definition: nt_native.h:90
#define PAGE_NOACCESS
Definition: nt_native.h:1305
#define UNICODE_NULL
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:100
NTSTATUS NTAPI NtFlushVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToFlush, OUT PIO_STATUS_BLOCK IoStatusBlock)
Definition: virtual.c:4000
NTSTATUS NTAPI NtAllocateVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UBaseAddress, IN ULONG_PTR ZeroBits, IN OUT PSIZE_T URegionSize, IN ULONG AllocationType, IN ULONG Protect)
Definition: virtual.c:4457
#define STATUS_IMAGE_MACHINE_TYPE_MISMATCH
Definition: ntstatus.h:203
#define STATUS_INVALID_IMAGE_NOT_MZ
Definition: ntstatus.h:633
#define wine_dbgstr_wn
Definition: testlist.c:2
STRSAFEAPI StringCbCatW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:342
unsigned char * PUCHAR
Definition: typedefs.h:53
_In_ PWDFDEVICE_INIT _In_ PWDF_FILEOBJECT_CONFIG _In_opt_ PWDF_OBJECT_ATTRIBUTES FileObjectAttributes
Definition: wdfdevice.h:3406

Referenced by START_TEST().

◆ Test_ImageSection2()

void Test_ImageSection2 ( void  )

Definition at line 1029 of file NtMapViewOfSection.c.

1030{
1035 HANDLE FileHandle, ImageSectionHandle;
1036 PVOID ImageBase, BaseAddress;
1039
1040 if (!RtlDosPathNameToNtPathName_U(L"testdata\\nvoglv32.dll",
1041 &FileName,
1042 NULL,
1043 NULL))
1044 {
1045 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
1046 return;
1047 }
1048
1050 &FileName,
1051 0,
1052 NULL,
1053 NULL);
1054
1061 RtlFreeHeap(RtlGetProcessHeap(), 0, FileName.Buffer);
1063 printf("Opened file with handle %p\n", FileHandle);
1064
1065 /* Create a data section with write access */
1066 MaximumSize.QuadPart = 0x20000;
1067 Status = NtCreateSection(&ImageSectionHandle,
1068 SECTION_ALL_ACCESS, // DesiredAccess
1069 NULL, // ObjectAttributes
1070 &MaximumSize, // MaximumSize
1071 PAGE_READWRITE, // SectionPageProtection
1072 SEC_IMAGE, // AllocationAttributes
1073 FileHandle);
1075
1076 printf("Created image section with handle %p\n", ImageSectionHandle);
1077 //system("PAUSE");
1078
1079 /* Map the image section */
1080 ImageBase = NULL;
1081 ViewSize = 0x0000;
1082 SectionOffset.QuadPart = 0x00000;
1083 Status = NtMapViewOfSection(ImageSectionHandle,
1085 &ImageBase,
1086 0,
1087 0,
1089 &ViewSize,
1090 ViewShare,
1091 0,
1094
1095 printf("Mapped image section at %p, value in text section: %lx\n",
1096 ImageBase, *((ULONG*)((PCHAR)ImageBase + 0x1196)));
1097 system("PAUSE");
1098
1099 /* Try to allocate a page after the section */
1100 BaseAddress = (PUCHAR)ImageBase + 0x10000;
1101 ViewSize = 0x1000;
1103 &BaseAddress,
1104 0,
1105 &ViewSize,
1108 printf("allocation status: %lx\n", Status);
1109 system("PAUSE");
1110
1111}
_ACRTIMP int __cdecl system(const char *)
Definition: process.c:1297
#define printf
Definition: freeldr.h:103
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize _Pre_valid_ PVOID * BaseAddress
Definition: mmfuncs.h:408
#define MEM_COMMIT
Definition: nt_native.h:1316
char * PCHAR
Definition: typedefs.h:51

◆ Test_PageFileSection()

void Test_PageFileSection ( void  )

Definition at line 20 of file NtMapViewOfSection.c.

21{
23 HANDLE SectionHandle;
25 PVOID BaseAddress, BaseAddress2;
27 ULONG OldProtect;
28
29 /* Create a page file backed section with SEC_COMMIT */
30 MaximumSize.QuadPart = 0x20000;
31 Status = NtCreateSection(&SectionHandle,
33 NULL,
37 NULL);
39 if (!NT_SUCCESS(Status))
40 return;
41
42 /* Try to map a page at an address that is not 64k aligned */
43 BaseAddress = (PVOID)0x30001000;
44 SectionOffset.QuadPart = 0;
45 ViewSize = 0x1000;
46 Status = NtMapViewOfSection(SectionHandle,
49 0,
50 0,
52 &ViewSize,
54 0,
57
58 /* Try to map a page with execute rights */
59 BaseAddress = (PVOID)0x30000000;
60 SectionOffset.QuadPart = 0;
61 ViewSize = 0x1000;
62 Status = NtMapViewOfSection(SectionHandle,
65 0,
66 0,
68 &ViewSize,
70 0,
73
74 /* Try to map 2 pages with MEM_COMMIT */
75 BaseAddress = (PVOID)0x30000000;
76 SectionOffset.QuadPart = 0;
77 ViewSize = 0x2000;
78 Status = NtMapViewOfSection(SectionHandle,
81 0,
84 &ViewSize,
89
90 /* Try to map 1 page, with free base address and zero bits compatible with 64k granularity */
92 SectionOffset.QuadPart = 0;
93 ViewSize = 0x1000;
94 Status = NtMapViewOfSection(SectionHandle,
97 10,
98 0,
100 &ViewSize,
101 ViewShare,
102 0,
105 ok(((ULONG_PTR)BaseAddress & 0x0000FFFF) == 0, "BaseAddress not 64k aligned: %p\n", BaseAddress);
106 ok(((ULONG_PTR)BaseAddress & (ULONG_PTR)0xFFFFFFFFFFC00000ULL) == 0, "Upper bits are not all zero: %p\n", BaseAddress);
109
110 /* Try to map 1 page, with free base address and zero bits incompatible with 64k granularity.
111 * Note: On 64 bit Windows, ZeroBits != 0 means that the upper (32 + ZeroBits) bits must be zero.
112 * While the documented maximum for ZeroBits is "less than 21", to not break 64 bit alignment,
113 * we need to keep at least 17 bits (i.e., ZeroBits must be less than 16). */
115 SectionOffset.QuadPart = 0;
116 ViewSize = 0x1000;
117 Status = NtMapViewOfSection(SectionHandle,
120 (32 - 16),
121 0,
123 &ViewSize,
124 ViewShare,
125 0,
128
129 /* Try to map 1 page, with base address and zero bits being compatible */
130 BaseAddress = (PVOID)0x30000000;
131 SectionOffset.QuadPart = 0;
132 ViewSize = 0x1000;
133 Status = NtMapViewOfSection(SectionHandle,
136 2,
137 0,
139 &ViewSize,
140 ViewShare,
141 0,
146
147 /* Try to map 1 page, with base address and zero bits being incompatible */
148 BaseAddress = (PVOID)0x30000000;
149 SectionOffset.QuadPart = 0;
150 ViewSize = 0x1000;
151 Status = NtMapViewOfSection(SectionHandle,
154 3,
155 0,
157 &ViewSize,
158 ViewShare,
159 0,
162
163 /* Try using ZeroBits like a bitmask */
165 SectionOffset.QuadPart = 0;
166 ViewSize = 0x1000;
167 Status = NtMapViewOfSection(SectionHandle,
170 0x0FFFFFFF,
171 0,
173 &ViewSize,
174 ViewShare,
175 0,
178 {
180 ok(((ULONG_PTR)BaseAddress & 0x0000FFFF) == 0, "BaseAddress not 64k aligned: %p\n", BaseAddress);
181 ok(((ULONG_PTR)BaseAddress & (ULONG_PTR)0xFFFFFFFFF0000000ULL) == 0, "Upper bits are not all zero: %p\n", BaseAddress);
182 }
183 else
184 {
186 if (!NT_SUCCESS(Status))
187 return;
188 }
189
190 /* Map 2 pages, without MEM_COMMIT */
191 BaseAddress = (PVOID)0x30000000;
192 SectionOffset.QuadPart = 0;
193 ViewSize = 0x2000;
194 Status = NtMapViewOfSection(SectionHandle,
197 0,
198 0,
200 &ViewSize,
201 ViewShare,
202 0,
205
206 /* We must be able to access the memory */
208 {
209 *(PULONG)BaseAddress = 1;
210 }
211 _SEH2_EXCEPT(1)
212 {
213 ok(FALSE, "Got an exception\n");
214 }
215 _SEH2_END;
216
217 /* Commit a page in the section */
218 BaseAddress = (PVOID)0x30000000;
219 ViewSize = 0x1000;
222 0,
223 &ViewSize,
227
228 /* Try to decommit a page in the section */
231 &ViewSize,
234
235 /* Try to commit a range larger than the section */
236 BaseAddress = (PVOID)0x30000000;
237 ViewSize = 0x3000;
240 0,
241 &ViewSize,
245
246 /* Try to commit a page after the section */
247 BaseAddress = (PVOID)0x30002000;
248 ViewSize = 0x1000;
251 0,
252 &ViewSize,
256
257 /* Try to allocate a page after the section */
258 BaseAddress = (PVOID)0x30002000;
259 ViewSize = 0x1000;
262 0,
263 &ViewSize,
267
268 /* Need to go to next 64k boundary */
269 BaseAddress = (PVOID)0x30010000;
270 ViewSize = 0x1000;
273 0,
274 &ViewSize,
278 if (!NT_SUCCESS(Status))
279 return;
280
281 /* Free the allocation */
282 BaseAddress = (PVOID)0x30010000;
283 ViewSize = 0x1000;
286 &ViewSize,
288 ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed with Status %lx\n", Status);
289
290 /* Try to release the section mapping with NtFreeVirtualMemory */
291 BaseAddress = (PVOID)0x30000000;
292 ViewSize = 0x1000;
295 &ViewSize,
298
299 /* Commit a page in the section */
300 BaseAddress = (PVOID)0x30001000;
301 ViewSize = 0x1000;
304 0,
305 &ViewSize,
309
310 /* Try to decommit the page */
311 BaseAddress = (PVOID)0x30001000;
312 ViewSize = 0x1000;
315 &ViewSize,
318
319 BaseAddress = UlongToPtr(0x40000000);
320 SectionOffset.QuadPart = 0;
321 ViewSize = 0x1000;
322 Status = NtMapViewOfSection(SectionHandle,
325 0,
326 0,
328 &ViewSize,
329 ViewShare,
330 0,
333 if (!NT_SUCCESS(Status))
334 return;
335
336 ok(BaseAddress == UlongToPtr(0x40000000), "Invalid BaseAddress: %p\n", BaseAddress);
337
338 BaseAddress = (PVOID)0x40080000;
339 SectionOffset.QuadPart = 0x10000;
340 ViewSize = 0x1000;
341 Status = NtMapViewOfSection(SectionHandle,
344 0,
345 0,
347 &ViewSize,
348 ViewShare,
349 0,
352
353 ok(BaseAddress == (PVOID)0x40080000, "Invalid BaseAddress: %p\n", BaseAddress);
354
355 /* Commit a page in the section */
356 BaseAddress = (PVOID)0x40000000;
359 0,
360 &ViewSize,
364
365 /* Close the mapping */
368 BaseAddress = (PVOID)0x30000000;
371 Status = NtClose(SectionHandle);
373
374 /* Create a page file backed section, but only reserved */
375 MaximumSize.QuadPart = 0x20000;
376 Status = NtCreateSection(&SectionHandle,
378 NULL,
382 NULL);
384
385 /* Try to map 1 page, passing MEM_RESERVE */
387 SectionOffset.QuadPart = 0;
389 Status = NtMapViewOfSection(SectionHandle,
392 0,
393 PAGE_SIZE,
395 &ViewSize,
396 ViewShare,
400
401 /* Try to map 1 page using MEM_COMMIT */
403 SectionOffset.QuadPart = 0;
405 Status = NtMapViewOfSection(SectionHandle,
408 0,
409 PAGE_SIZE,
411 &ViewSize,
412 ViewShare,
416
417 /* Map 2 pages, but commit 1 */
419 SectionOffset.QuadPart = 0;
420 ViewSize = 2 * PAGE_SIZE;
421 Status = NtMapViewOfSection(SectionHandle,
424 0,
425 PAGE_SIZE,
427 &ViewSize,
428 ViewShare,
429 0,
432
433 /* We must be able to access the 1st page */
436 {
437 *(PUCHAR)BaseAddress = 1;
438 }
439 _SEH2_EXCEPT(1)
440 {
442 }
443 _SEH2_END;
445
446 /* We must not be able to access the 2nd page */
449 {
450 *((PUCHAR)BaseAddress + PAGE_SIZE) = 1;
451 }
452 _SEH2_EXCEPT(1)
453 {
455 }
456 _SEH2_END;
458
459 /* Map the 2 pages again into a different memory location */
460 BaseAddress2 = NULL;
461 Status = NtMapViewOfSection(SectionHandle,
463 &BaseAddress2,
464 0,
465 0,
467 &ViewSize,
468 ViewShare,
469 0,
472
473 /* Commit a the 2nd page in the 2nd memory location */
474 BaseAddress2 = (PUCHAR)BaseAddress2 + PAGE_SIZE;
477 &BaseAddress2,
478 0,
479 &ViewSize,
483
484 /* Try to commit again (the already committed page) */
486 &BaseAddress2,
487 0,
488 &ViewSize,
492
493 /* We must be able to access the memory in the 2nd page of the 1st memory location */
496 {
497 *((PUCHAR)BaseAddress + PAGE_SIZE) = 2;
498 }
499 _SEH2_EXCEPT(1)
500 {
502 }
503 _SEH2_END;
505
506 ok(*(PULONG)BaseAddress2 == 2, "Value in memory was wrong\n");
507
508 /* Close the mapping */
513 Status = NtClose(SectionHandle);
515
516 /* Try to create a 512 GB page file backed section with committed pages */
517 MaximumSize.QuadPart = 0x8000000000;
518 Status = NtCreateSection(&SectionHandle,
520 NULL,
524 NULL);
526
527 /* Try to create a huge page file backed section with PAGE_NOACCESS protection */
528 MaximumSize.QuadPart = 0x8000000000;
529 Status = NtCreateSection(&SectionHandle,
531 NULL,
535 NULL);
537
538 /* Try to create a very huge page file backed section, but only reserved */
539 MaximumSize.QuadPart = 0x80000000000;
540 Status = NtCreateSection(&SectionHandle,
542 NULL,
546 NULL);
547#ifdef _WIN64
549#else
550 /* WoW64 returns STATUS_INSUFFICIENT_RESOURCES */
552 "got wrong Status: 0x%lx\n", Status);
553#endif
554
555 /* Try to create a even huger page file backed section, but only reserved */
556 MaximumSize.QuadPart = 0x800000000000;
557 Status = NtCreateSection(&SectionHandle,
559 NULL,
563 NULL);
565
566 /* Create a 8 GB page file backed section, but only reserved */
567 MaximumSize.QuadPart = 0x200000000;
568 Status = NtCreateSection(&SectionHandle,
570 NULL,
574 NULL);
576
577 /* Pass a too large region size */
579 SectionOffset.QuadPart = 0;
581 Status = NtMapViewOfSection(SectionHandle,
584 0,
585 0,
587 &ViewSize,
588 ViewShare,
589 0,
591#ifdef _WIN64
593#else
594 /* WoW64 returns STATUS_INVALID_PARAMETER_4 */
596 ok(Status == STATUS_INVALID_PARAMETER, "got wrong Status: 0x%lx\n", Status);
597 else
599 "got wrong Status: 0x%lx\n", Status);
600#endif
601
602 /* Pass 0 region size */
604 SectionOffset.QuadPart = 0;
605 ViewSize = 0;
606 Status = NtMapViewOfSection(SectionHandle,
609 0,
610 0,
612 &ViewSize,
613 ViewShare,
614 0,
616#ifdef _WIN64
618 ok(ViewSize == 0x200000000, "wrong ViewSize: 0x%Ix\n", ViewSize);
619#else
620 /* WoW64 returns STATUS_NO_MEMORY */
622 "got wrong Status: 0x%lx\n", Status);
623 ok(ViewSize == 0, "wrong ViewSize: 0x%Ix\n", ViewSize);
624#endif
625
626 /* Map with PAGE_NOACCESS */
628 SectionOffset.QuadPart = 0;
629 ViewSize = 0x20000000;
630 Status = NtMapViewOfSection(SectionHandle,
633 0,
634 0,
636 &ViewSize,
637 ViewShare,
638 0,
641
642 /* Try to change protection to read/write */
643 ViewSize = 0x1000;
644 OldProtect = -1;
645 BaseAddress2 = BaseAddress;
646 Status = NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2, &ViewSize, PAGE_READWRITE, &OldProtect);
648 // Windows 2003 returns bogus
649 //ok(OldProtect == PAGE_READWRITE, "Wrong protection returned: %u\n", OldProtect);
650
651 /* Test read access */
654 {
655 (void)(*(volatile char*)BaseAddress2);
656 }
658 {
660 }
661 _SEH2_END;
663
664 /* Try to change protection to read/write */
665 ViewSize = 0x1000;
666 OldProtect = -1;
667 BaseAddress2 = BaseAddress;
668 Status = NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2, &ViewSize, PAGE_READWRITE, &OldProtect);
670#ifdef _WIN64
671 ok(OldProtect == PAGE_NOACCESS, "Wrong protection returned: 0x%lx\n", OldProtect);
672#else
673 // Windows 2003 returns bogus
674#endif
675
676 /* Try to change protection to readonly */
677 ViewSize = 0x1000;
678 OldProtect = -1;
679 BaseAddress2 = BaseAddress;
680 Status = NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2, &ViewSize, PAGE_READONLY, &OldProtect);
682#ifdef _WIN64
683 //ok(OldProtect == 0, "Wrong protection returned: 0x%lx\n", OldProtect);
684#else
685 // Windows 2003 returns bogus
686#endif
687
688 /* Commit a page */
689 ViewSize = 0x1000;
690 BaseAddress2 = BaseAddress;
693 ok(BaseAddress2 == BaseAddress, "Invalid base address: %p\n", BaseAddress2);
694
695 /* Commit the page again */
696 ViewSize = 0x1000;
697 BaseAddress2 = BaseAddress;
700 ok(BaseAddress2 == BaseAddress, "Invalid base address: %p\n", BaseAddress2);
701
702 /* Test read access */
705 {
706 (void)(*(volatile char*)BaseAddress2);
707 }
709 {
711 }
712 _SEH2_END;
714
715 /* Test write access */
718 {
719 *(char*)BaseAddress2 = 1;
720 }
722 {
724 }
725 _SEH2_END;
727
728 /* Update protection to PAGE_READWRITE */
729 ViewSize = 0x1000;
730 BaseAddress2 = BaseAddress;
733
734 /* Test write access */
737 {
738 *(char*)BaseAddress2 = 1;
739 }
741 {
743 }
744 _SEH2_END;
746
747 /* Update protection to PAGE_EXECUTE_READWRITE (1 page) */
748 ViewSize = 0x1000;
749 BaseAddress2 = BaseAddress;
752
755 Status = NtClose(SectionHandle);
757}
#define GetNTVersion()
Definition: apitest.h:17
#define MAXULONG_PTR
Definition: basetsd.h:97
#define STATUS_NO_MEMORY
Definition: d3dkmdt.h:51
#define UlongToPtr(u)
Definition: config.h:106
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define STATUS_ACCESS_VIOLATION
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
#define MEM_DECOMMIT
Definition: nt_native.h:1318
#define SEC_RESERVE
Definition: nt_native.h:1326
#define MEM_RELEASE
Definition: nt_native.h:1319
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1311
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5192
NTSTATUS NTAPI NtProtectVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UnsafeBaseAddress, IN OUT SIZE_T *UnsafeNumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG UnsafeOldAccessProtection)
Definition: virtual.c:3076
#define STATUS_INVALID_VIEW_SIZE
Definition: ntstatus.h:361
#define STATUS_MAPPED_ALIGNMENT
Definition: ntstatus.h:798
#define STATUS_SECTION_PROTECTION
Definition: ntstatus.h:408
#define STATUS_UNABLE_TO_DELETE_SECTION
Definition: ntstatus.h:357
#define STATUS_INVALID_PARAMETER_9
Definition: ntstatus.h:577
#define STATUS_INVALID_PARAMETER_4
Definition: ntstatus.h:572
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:399
#define STATUS_SECTION_TOO_BIG
Definition: ntstatus.h:394
#define STATUS_INVALID_PARAMETER_3
Definition: ntstatus.h:571
#define STATUS_COMMITMENT_LIMIT
Definition: ntstatus.h:631
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:204
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:104
#define _SEH2_END
Definition: pseh2_64.h:194
#define _SEH2_TRY
Definition: pseh2_64.h:93
#define _WIN32_WINNT_WIN10
Definition: sdkddkver.h:32
#define _WIN32_WINNT_VISTA
Definition: sdkddkver.h:25
uint32_t * PULONG
Definition: typedefs.h:59
void * PVOID
Definition: typedefs.h:50
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158

Referenced by START_TEST().

◆ Test_RawSize()

static void Test_RawSize ( ULONG  TestNumber)
static

Definition at line 1764 of file NtMapViewOfSection.c.

1765{
1767 WCHAR TempPath[MAX_PATH];
1769 HANDLE Handle;
1770 HANDLE SectionHandle;
1774 ULONG Written;
1775 ULONG Length;
1776 BOOL Success;
1777
1778 Length = GetTempPathW(MAX_PATH, TempPath);
1779 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1780 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1781 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1784 0,
1785 NULL,
1787 0,
1788 NULL);
1790 {
1791 skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1792 return;
1793 }
1794 RawSizeImageFile.nthdrs.OptionalHeader.ImageBase = 0xe400000;
1795 if (TestNumber == 1)
1796 {
1797 /* Just for fun, show that these flags don't matter. */
1802 }
1803 else if (TestNumber == 2)
1804 {
1805 /* SizeOfRawData is too large and will overflow.
1806 * This should cause failure to load the file */
1807 RawSizeImageFile.rsrc_header.SizeOfRawData = (ULONG)-0x200;
1808 }
1809
1812 sizeof(RawSizeImageFile),
1813 &Written,
1814 NULL);
1815 ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1816 ok(Written == sizeof(RawSizeImageFile), "WriteFile wrote %lu bytes\n", Written);
1817
1818 Status = NtCreateSection(&SectionHandle,
1820 NULL,
1821 NULL,
1823 SEC_IMAGE,
1824 Handle);
1825 if (TestNumber == 2)
1826 {
1827 /* overflow in SizeOfRawData */
1829 }
1830 else
1831 {
1833 }
1834
1835 if (NT_SUCCESS(Status))
1836 {
1837 /* Map the section with */
1838 BaseAddress = NULL;
1839 SectionOffset.QuadPart = 0;
1840 ViewSize = 0;
1841 Status = NtMapViewOfSection(SectionHandle,
1843 &BaseAddress,
1844 0,
1845 0,
1847 &ViewSize,
1848 ViewShare,
1849 0,
1852 if (NT_SUCCESS(Status))
1853 {
1856
1857 /* .text section header is unmodified */
1858 ok_hex(ImageFile->text_header.Misc.VirtualSize, RawSizeImageFile.text_header.Misc.VirtualSize);
1859 ok_hex(ImageFile->text_header.VirtualAddress, RawSizeImageFile.text_header.VirtualAddress);
1860 ok_hex(ImageFile->text_header.SizeOfRawData, RawSizeImageFile.text_header.SizeOfRawData);
1861 ok_hex(ImageFile->text_header.PointerToRawData, RawSizeImageFile.text_header.PointerToRawData);
1862
1863 /* SizeOfRawData = 0 resets PointerToRawData to 0 */
1864 ok_hex(ImageFile->zdata_header.Misc.VirtualSize, RawSizeImageFile.zdata_header.Misc.VirtualSize);
1865 ok_hex(ImageFile->zdata_header.VirtualAddress, RawSizeImageFile.zdata_header.VirtualAddress);
1866 ok_hex(ImageFile->zdata_header.SizeOfRawData, RawSizeImageFile.zdata_header.SizeOfRawData);
1867 ok_hex(ImageFile->zdata_header.PointerToRawData, 0);
1868
1869 /* .bss section is unmodified */
1870 ok_hex(ImageFile->bss_header.SizeOfRawData, 0x600);
1871 ok_hex(ImageFile->bss_header.PointerToRawData, 0);
1872
1873#define TEST_BYTE(n, v) \
1874 StartSeh() \
1875 ok(Bytes[n] == v, "[%lu] Bytes[%u] = 0x%x, expected 0x%x\n", \
1876 TestNumber, n, Bytes[n], v); \
1877 EndSeh(STATUS_SUCCESS);
1878 /* .text section data matches file up to 0x1000 */
1879 TEST_BYTE(0x1000, 0xc3);
1880 TEST_BYTE(0x1001, 0x00);
1881 TEST_BYTE(0x1008, 0x01);
1882 TEST_BYTE(0x1010, 0x02);
1883 TEST_BYTE(0x1fff, 0x0a);
1884
1885 /* .data section data matches file up to 0x1000 */
1886 TEST_BYTE(0x2000, 0x01);
1887 TEST_BYTE(0x2001, 0x23);
1888 TEST_BYTE(0x20ff, 0xee);
1889 TEST_BYTE(0x2100, 0xff);
1890 TEST_BYTE(0x2fff, 0xbb);
1891
1892 /* .zdata section data is all zeroes */
1893 TEST_BYTE(0x3000, 0x00);
1894 TEST_BYTE(0x3001, 0x00);
1895 TEST_BYTE(0x3800, 0x00);
1896 TEST_BYTE(0x3fff, 0x00);
1897
1898 /* .rsrc section data matches file up to VirtualSize 0x200 */
1899 TEST_BYTE(0x4000, 0xff);
1900 TEST_BYTE(0x4100, 0xee);
1901 TEST_BYTE(0x4180, 0xdd);
1902 TEST_BYTE(0x41c0, 0xcc);
1903 TEST_BYTE(0x41e0, 0xbb);
1904 TEST_BYTE(0x41f0, 0xaa);
1905 TEST_BYTE(0x41fe, 0x23);
1906 TEST_BYTE(0x41ff, 0x01);
1907 TEST_BYTE(0x4200, 0x00);
1908 TEST_BYTE(0x4fff, 0x00);
1909#undef TEST_BYTE
1912 }
1913 Status = NtClose(SectionHandle);
1915 }
1916
1919}
#define TEST_BYTE(n, v)
#define IsX86()
static struct _RAW_SIZE_IMAGE_FILE RawSizeImageFile
BOOL WINAPI WriteFile(_In_ HANDLE hFile, _In_reads_bytes_opt_(nNumberOfBytesToWrite) LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped)
Definition: rw.c:25
@ Success
Definition: eventcreate.c:712
unsigned int BOOL
Definition: ntddk_ex.h:94
static PWSTR ImageFile
Definition: imagefile.c:10
#define IMAGE_SCN_CNT_INITIALIZED_DATA
Definition: ntimage.h:231
#define IMAGE_SCN_CNT_CODE
Definition: ntimage.h:230
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA
Definition: ntimage.h:232
#define STATUS_INVALID_IMAGE_FORMAT
Definition: ntstatus.h:453

Referenced by START_TEST().

◆ Test_SectionContents()

static void Test_SectionContents ( BOOL  Relocate)
static

Definition at line 1414 of file NtMapViewOfSection.c.

1415{
1417 WCHAR TempPath[MAX_PATH];
1419 HANDLE Handle;
1420 HANDLE SectionHandle;
1424 ULONG Written;
1425 ULONG Length;
1426 BOOL Success;
1427
1428 Length = GetTempPathW(MAX_PATH, TempPath);
1429 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1430 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1431 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1434 0,
1435 NULL,
1437 0,
1438 NULL);
1440 {
1441 skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1442 return;
1443 }
1444 if (Relocate)
1445 {
1446#ifdef _WIN64
1447 ok((ULONG_PTR)GetModuleHandle(NULL) <= 0x00007FFFFFFFFFFF, "Module at %p\n", GetModuleHandle(NULL));
1448#else
1449 ok((ULONG_PTR)GetModuleHandle(NULL) <= 0x80000000, "Module at %p\n", GetModuleHandle(NULL));
1450#endif
1451 SectionContentsImageFile.nthdrs.OptionalHeader.ImageBase = (ULONG_PTR)GetModuleHandle(NULL);
1452 }
1453 else
1454 {
1455 SectionContentsImageFile.nthdrs.OptionalHeader.ImageBase = 0xe400000;
1456 }
1457
1461 &Written,
1462 NULL);
1463 ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1464 ok(Written == sizeof(SectionContentsImageFile), "WriteFile wrote %lu bytes\n", Written);
1465
1466 Status = NtCreateSection(&SectionHandle,
1468 NULL,
1469 NULL,
1471 SEC_IMAGE,
1472 Handle);
1474
1475 if (NT_SUCCESS(Status))
1476 {
1477 /* Map the section with */
1478 BaseAddress = NULL;
1479 SectionOffset.QuadPart = 0;
1480 ViewSize = 0;
1481 Status = NtMapViewOfSection(SectionHandle,
1483 &BaseAddress,
1484 0,
1485 0,
1487 &ViewSize,
1488 ViewShare,
1489 0,
1491 if (Relocate)
1493 else
1495 if (NT_SUCCESS(Status))
1496 {
1498#define TEST_BYTE(n, v) StartSeh() ok_hex(Bytes[n], v); EndSeh(STATUS_SUCCESS);
1499#define TEST_WRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_SUCCESS);
1500#define TEST_NOWRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_ACCESS_VIOLATION);
1501 TEST_NOWRITE(0x2000);
1502 TEST_BYTE(0x2000, 0xc3);
1503 TEST_BYTE(0x2001, 0x00);
1504 TEST_NOWRITE(0x4000);
1505 TEST_BYTE(0x4000, 0x01);
1506 TEST_BYTE(0x4001, 0x23);
1507 TEST_BYTE(0x4007, 0xef);
1508 TEST_BYTE(0x4008, 0xaa);
1509 TEST_BYTE(0x4010, 0xbb);
1510 TEST_BYTE(0x4020, 0xcc);
1511 TEST_BYTE(0x4040, 0xdd);
1512 TEST_BYTE(0x4080, 0xee);
1513 TEST_BYTE(0x40c0, 0xff);
1514 TEST_BYTE(0x40ff, 0xff);
1515 TEST_BYTE(0x4100, 0x00);
1516 TEST_BYTE(0x41ff, 0x00);
1517 TEST_NOWRITE(0x6000);
1518 TEST_BYTE(0x6000, 0xee);
1519 TEST_BYTE(0x60ff, 0x55);
1520 TEST_BYTE(0x6100, 0xee);
1521 TEST_BYTE(0x63ff, 0xdd);
1522 TEST_BYTE(0x6400, 0x00);
1523 TEST_WRITE(0x8000);
1524 TEST_BYTE(0x8000, 0x11);
1525 TEST_BYTE(0x8400, 0x33);
1526#undef TEST_BYTE
1527#undef TEST_WRITE
1528#undef TEST_NOWRITE
1531 }
1532 Status = NtClose(SectionHandle);
1534 }
1535
1538}
#define TEST_WRITE(n)
#define TEST_NOWRITE(n)
static struct _SECTION_CONTENTS_IMAGE_FILE SectionContentsImageFile
#define ULONG_PTR
Definition: config.h:101
#define STATUS_IMAGE_NOT_AT_BASE
Definition: ntstatus.h:192
#define GetModuleHandle
Definition: winbase.h:3548

Referenced by START_TEST().

◆ Test_Truncate()

static void Test_Truncate ( VOID  )
static

Definition at line 1970 of file NtMapViewOfSection.c.

1971{
1972 WCHAR TempPath[MAX_PATH];
1975 SIZE_T ViewSize = 0;
1976 HANDLE Handle;
1977 HANDLE SectionHandle;
1978
1979 SIZE_T Length;
1980 BOOL Success;
1981 DWORD Written, Error;
1983
1984 Length = GetTempPathW(MAX_PATH, TempPath);
1985 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1986 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1987 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1989
1990 Success = WriteFile(Handle, "TESTDATA", 8, &Written, NULL);
1991 ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1992 ok(Written == 8, "WriteFile wrote %lu bytes\n", Written);
1993
1994 Written = SetFilePointer(Handle, 6, NULL, FILE_BEGIN);
1995 ok(Written == 6, "SetFilePointer returned %lu bytes\n", Written);
1997 ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
1998
1999 Status = NtCreateSection(&SectionHandle,
2003 BaseAddress = NULL;
2004 ViewSize = 0;
2005 Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
2006 0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
2008
2009 if (BaseAddress)
2010 {
2011 // First we test data that was truncated even before the file mapping was opened
2012 Length = strlen((char*)BaseAddress);
2013 ok(Length == 6, "Old data was not properly erased! (Length=%lu)\n", Length);
2014 }
2015
2016 // Now we truncate the file on disk some more
2017 Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
2018 ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
2020 Error = GetLastError();
2021 ok(Success == FALSE, "SetEndOfFile succeeded\n");
2022 ok(Error == ERROR_USER_MAPPED_FILE, "SetEndOfFile did not set error to ERROR_USER_MAPPED_FILE (%lu)\n", Error);
2023
2024 if (BaseAddress)
2025 {
2026 Length = strlen((char*)BaseAddress);
2027 ok(Length == 6, "Length should not have changed! (Length=%lu)\n", Length);
2028 }
2029
2030 // Unmap and set the end shorter.
2033 Success = CloseHandle(SectionHandle);
2034 ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
2035
2036 Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
2037 ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
2039 ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
2040
2041 Status = NtCreateSection(&SectionHandle,
2045 BaseAddress = NULL;
2046 ViewSize = 0;
2047 Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
2048 0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
2050
2051 // CLEANUP
2054 Success = CloseHandle(SectionHandle);
2055 ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
2057 ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
2058
2060 ok(Success == TRUE, "DeleteFileW failed with %lu\n", GetLastError());
2061}
BOOL Error
Definition: chkdsk.c:66
#define FILE_BEGIN
Definition: compat.h:761
#define SetFilePointer
Definition: compat.h:743
BOOL WINAPI SetEndOfFile(HANDLE hFile)
Definition: fileinfo.c:989
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1597
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERROR_USER_MAPPED_FILE
Definition: winerror.h:1056

Referenced by START_TEST().

Variable Documentation

◆ RawSizeImageFile

struct _RAW_SIZE_IMAGE_FILE RawSizeImageFile
static

Referenced by Test_RawSize().

◆ SectionContentsImageFile

struct _SECTION_CONTENTS_IMAGE_FILE SectionContentsImageFile
static

Referenced by Test_SectionContents().