ReactOS 0.4.16-dev-2224-g3637fd8
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 1195 of file NtMapViewOfSection.c.

◆ BYTES128

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

Definition at line 1192 of file NtMapViewOfSection.c.

◆ BYTES16

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

Definition at line 1189 of file NtMapViewOfSection.c.

◆ BYTES256

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

Definition at line 1193 of file NtMapViewOfSection.c.

◆ BYTES32

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

Definition at line 1190 of file NtMapViewOfSection.c.

◆ BYTES4

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

Definition at line 1187 of file NtMapViewOfSection.c.

◆ BYTES512

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

Definition at line 1194 of file NtMapViewOfSection.c.

◆ BYTES64

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

Definition at line 1191 of file NtMapViewOfSection.c.

◆ BYTES8

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

Definition at line 1188 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 2060 of file NtMapViewOfSection.c.

2061{
2067 Test_RawSize(0);
2068 Test_RawSize(1);
2069 Test_RawSize(2);
2071 Test_Truncate();
2072}
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 1112 of file NtMapViewOfSection.c.

1113{
1115 HANDLE SectionHandle1, SectionHandle2;
1117 PVOID BaseAddress1, BaseAddress2;
1119
1120 /* Create a based section with SEC_COMMIT */
1121 MaximumSize.QuadPart = 0x1000;
1122 Status = NtCreateSection(&SectionHandle1,
1124 NULL,
1125 &MaximumSize,
1128 NULL);
1130
1131 /* Map the 1st section */
1132 BaseAddress1 = NULL;
1133 SectionOffset.QuadPart = 0;
1134 ViewSize = 0;
1135 Status = NtMapViewOfSection(SectionHandle1,
1137 &BaseAddress1,
1138 0,
1139 0,
1141 &ViewSize,
1142 ViewShare,
1143 0,
1145#if 0 // WOW64?
1147#else
1149#endif
1150
1151 /* Create a 2nd based section with SEC_COMMIT */
1152 MaximumSize.QuadPart = 0x1000;
1153 Status = NtCreateSection(&SectionHandle2,
1155 NULL,
1156 &MaximumSize,
1159 NULL);
1161
1162 /* Map the 2nd section */
1163 BaseAddress2 = NULL;
1164 SectionOffset.QuadPart = 0;
1165 ViewSize = 0;
1166 Status = NtMapViewOfSection(SectionHandle2,
1168 &BaseAddress2,
1169 0,
1170 0,
1172 &ViewSize,
1173 ViewShare,
1174 0,
1176#if 0 // WOW64?
1178#else
1180 ok((ULONG_PTR)BaseAddress2 < (ULONG_PTR)BaseAddress1,
1181 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
1182 ok(((ULONG_PTR)BaseAddress1 - (ULONG_PTR)BaseAddress2) == 0x10000,
1183 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
1184#endif
1185}
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:3076
NTSTATUS NTAPI NtMapViewOfSection(IN HANDLE SectionHandle, IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
Definition: section.c:3257
#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:25
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:362
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:407
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:408
#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 1919 of file NtMapViewOfSection.c.

1920{
1922 WCHAR TempPath[MAX_PATH];
1924 HANDLE Handle;
1925 HANDLE SectionHandle;
1926 ULONG Length;
1927
1928 Length = GetTempPathW(MAX_PATH, TempPath);
1929 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1930 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1931 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1934 0,
1935 NULL,
1937 0,
1938 NULL);
1940 {
1941 skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1942 return;
1943 }
1944
1945 Status = NtCreateSection(&SectionHandle,
1949
1950 if (NT_SUCCESS(Status))
1951 NtClose(SectionHandle);
1952
1953 Status = NtCreateSection(&SectionHandle,
1957
1958 if (NT_SUCCESS(Status))
1959 NtClose(SectionHandle);
1960
1963}
#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:2080
#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:3402
#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
uint32_t ULONG
Definition: typedefs.h:59
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
__wchar_t WCHAR
Definition: xmlstorage.h:180

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 return;
800 }
801
802 /* Create a data section with write access */
803 Status = NtCreateSection(&DataSectionHandle,
804 SECTION_ALL_ACCESS, // DesiredAccess
805 NULL, // ObjectAttributes
806 NULL, // MaximumSize
807 PAGE_READWRITE, // SectionPageProtection
808 SEC_COMMIT, // AllocationAttributes
809 FileHandle);
811 if (!NT_SUCCESS(Status))
812 {
813 skip("Failed to create data section\n");
815 return;
816 }
817
818 /* Map the data section as flat mapping */
819 DataBase = NULL;
820 ViewSize = 0;
821 Status = NtMapViewOfSection(DataSectionHandle,
823 &DataBase,
824 0,
825 0,
826 NULL,
827 &ViewSize,
828 ViewShare,
829 0,
832 //ok(ViewSize == 0x3f95cc48, "ViewSize wrong: 0x%lx\n");
833 if (!NT_SUCCESS(Status))
834 {
835 skip("Failed to map view of data section\n");
836 NtClose(DataSectionHandle);
838 return;
839 }
840
841 /* Check the original data */
842 ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
843
844 /* Modify the PE header (but do not flush!) */
845 *(ULONG*)DataBase = 0xdeadbabe;
846 ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
847
848 /* Modify data in the .data section (but do not flush!) */
849 ok(*(ULONG*)((PUCHAR)DataBase + 0x800) == 0x12345678,
850 "Data in .data section invalid: 0x%lx!\n", *(ULONG*)((PUCHAR)DataBase + 0x800));
851 *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x87654321;
852
853 /* Now try to create an image section (should fail) */
854 Status = NtCreateSection(&ImageSectionHandle,
855 SECTION_ALL_ACCESS, // DesiredAccess
856 NULL, // ObjectAttributes
857 NULL, // MaximumSize
858 PAGE_READWRITE, // SectionPageProtection
859 SEC_IMAGE, // AllocationAttributes
860 FileHandle);
862 if (NT_SUCCESS(Status)) NtClose(ImageSectionHandle);
863
864 /* Restore the original header */
865 *(ULONG*)DataBase = 0x00905a4d;
866
867 /* Modify data in the .data section (but do not flush!) */
868 ok_hex(*(ULONG*)((PUCHAR)DataBase + 0x800), 0x87654321);
869 *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xdeadbabe;
870
871 /* Try to create an image section again */
872 Status = NtCreateSection(&ImageSectionHandle,
873 SECTION_ALL_ACCESS, // DesiredAccess
874 NULL, // ObjectAttributes
875 NULL, // MaximumSize
876 PAGE_READWRITE, // SectionPageProtection
877 SEC_IMAGE, // AllocationAttributes
878 FileHandle);
880 if (!NT_SUCCESS(Status))
881 {
882 skip("Failed to create image section\n");
883 NtClose(DataSectionHandle);
885 return;
886 }
887
888 /* Map the image section */
889 ImageBase = NULL;
890 ViewSize = 0;
891 Status = NtMapViewOfSection(ImageSectionHandle,
893 &ImageBase,
894 0, // ZeroBits
895 0, // CommitSize
896 NULL, // SectionOffset
897 &ViewSize,
898 ViewShare,
899 0, // AllocationType
901#ifdef _M_IX86
903#else
905#endif
906 if (!NT_SUCCESS(Status))
907 {
908 skip("Failed to map view of image section\n");
909 NtClose(ImageSectionHandle);
910 NtClose(DataSectionHandle);
912 return;
913 }
914
915 /* Check the header */
916 ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
917 ok(*(ULONG*)ImageBase == 0x00905a4d, "Header not ok\n");
918
919 /* Check the data section. Either of these can be present! */
920 ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
921 (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
922 "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
923
924 /* Now modify the data again */
925 *(ULONG*)DataBase = 0xdeadbabe;
926 *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xf00dada;
927
928 /* Check the data */
929 ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
930 ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
931 ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
932 (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
933 "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
934
935 /* Flush the view */
936 ViewSize = 0x1000;
938 &DataBase,
939 &ViewSize,
942
943 /* Check the data again */
944 ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
945 ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
946 (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
947 "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
948
949 /* Restore the original header */
950 *(ULONG*)DataBase = 0x00905a4d;
951 ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
952
953 /* Close the image mapping */
955 NtClose(ImageSectionHandle);
956
957 /* Create an image section again */
958 Status = NtCreateSection(&ImageSectionHandle,
959 SECTION_ALL_ACCESS, // DesiredAccess
960 NULL, // ObjectAttributes
961 NULL, // MaximumSize
962 PAGE_READWRITE, // SectionPageProtection
963 SEC_IMAGE, // AllocationAttributes
964 FileHandle);
966 if (!NT_SUCCESS(Status))
967 {
968 skip("Failed to create image section\n");
969 NtClose(DataSectionHandle);
971 return;
972 }
973
974 /* Map the image section again */
975 ImageBase = NULL;
976 ViewSize = 0;
977 Status = NtMapViewOfSection(ImageSectionHandle,
979 &ImageBase,
980 0,
981 0,
982 NULL,
983 &ViewSize,
984 ViewShare,
985 0,
987#ifdef _M_IX86
989#else
991#endif
992 if (!NT_SUCCESS(Status))
993 {
994 skip("Failed to map view of image section\n");
995 NtClose(ImageSectionHandle);
996 NtClose(DataSectionHandle);
998 return;
999 }
1000
1001 // This one doesn't always work, needs investigation
1002 /* Check the .data section again */
1003 //ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada,
1004 // "Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
1005
1006 /* Restore the original data */
1007 *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x12345678;
1008
1009 /* Close the data mapping */
1011
1012 NtClose(DataSectionHandle);
1013
1014 /* Try to allocate memory inside the image mapping */
1015 DataBase = (PUCHAR)ImageBase + 0x20000;
1016 ViewSize = 0x1000;
1019
1020 /* Cleanup */
1022 NtClose(ImageSectionHandle);
1024}
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3483
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define ok_hex(expression, result)
Definition: atltest.h:94
#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:106
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:3953
#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:75
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 1027 of file NtMapViewOfSection.c.

1028{
1033 HANDLE FileHandle, ImageSectionHandle;
1034 PVOID ImageBase, BaseAddress;
1037
1038 if (!RtlDosPathNameToNtPathName_U(L"testdata\\nvoglv32.dll",
1039 &FileName,
1040 NULL,
1041 NULL))
1042 {
1043 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
1044 return;
1045 }
1046
1048 &FileName,
1049 0,
1050 NULL,
1051 NULL);
1052
1060 printf("Opened file with handle %p\n", FileHandle);
1061
1062 /* Create a data section with write access */
1063 MaximumSize.QuadPart = 0x20000;
1064 Status = NtCreateSection(&ImageSectionHandle,
1065 SECTION_ALL_ACCESS, // DesiredAccess
1066 NULL, // ObjectAttributes
1067 &MaximumSize, // MaximumSize
1068 PAGE_READWRITE, // SectionPageProtection
1069 SEC_IMAGE, // AllocationAttributes
1070 FileHandle);
1072
1073 printf("Created image section with handle %p\n", ImageSectionHandle);
1074 //system("PAUSE");
1075
1076 /* Map the image section */
1077 ImageBase = NULL;
1078 ViewSize = 0x0000;
1079 SectionOffset.QuadPart = 0x00000;
1080 Status = NtMapViewOfSection(ImageSectionHandle,
1082 &ImageBase,
1083 0,
1084 0,
1086 &ViewSize,
1087 ViewShare,
1088 0,
1091
1092 printf("Mapped image section at %p, value in text section: %lx\n",
1093 ImageBase, *((ULONG*)((PCHAR)ImageBase + 0x1196)));
1094 system("PAUSE");
1095
1096 /* Try to allocate a page after the section */
1097 BaseAddress = (PUCHAR)ImageBase + 0x10000;
1098 ViewSize = 0x1000;
1100 &BaseAddress,
1101 0,
1102 &ViewSize,
1105 printf("allocation status: %lx\n", Status);
1106 system("PAUSE");
1107
1108}
_ACRTIMP int __cdecl system(const char *)
Definition: process.c:1297
#define printf
Definition: freeldr.h:104
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#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:181
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:82
#define _SEH2_END
Definition: pseh2_64.h:171
#define _SEH2_TRY
Definition: pseh2_64.h:71
#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 1761 of file NtMapViewOfSection.c.

1762{
1764 WCHAR TempPath[MAX_PATH];
1766 HANDLE Handle;
1767 HANDLE SectionHandle;
1771 ULONG Written;
1772 ULONG Length;
1773 BOOL Success;
1774
1775 Length = GetTempPathW(MAX_PATH, TempPath);
1776 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1777 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1778 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1781 0,
1782 NULL,
1784 0,
1785 NULL);
1787 {
1788 skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1789 return;
1790 }
1791 RawSizeImageFile.nthdrs.OptionalHeader.ImageBase = 0xe400000;
1792 if (TestNumber == 1)
1793 {
1794 /* Just for fun, show that these flags don't matter. */
1799 }
1800 else if (TestNumber == 2)
1801 {
1802 /* SizeOfRawData is too large and will overflow.
1803 * This should cause failure to load the file */
1804 RawSizeImageFile.rsrc_header.SizeOfRawData = (ULONG)-0x200;
1805 }
1806
1809 sizeof(RawSizeImageFile),
1810 &Written,
1811 NULL);
1812 ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1813 ok(Written == sizeof(RawSizeImageFile), "WriteFile wrote %lu bytes\n", Written);
1814
1815 Status = NtCreateSection(&SectionHandle,
1817 NULL,
1818 NULL,
1820 SEC_IMAGE,
1821 Handle);
1822 if (TestNumber == 2)
1823 {
1824 /* overflow in SizeOfRawData */
1826 }
1827 else
1828 {
1830 }
1831
1832 if (NT_SUCCESS(Status))
1833 {
1834 /* Map the section with */
1835 BaseAddress = NULL;
1836 SectionOffset.QuadPart = 0;
1837 ViewSize = 0;
1838 Status = NtMapViewOfSection(SectionHandle,
1840 &BaseAddress,
1841 0,
1842 0,
1844 &ViewSize,
1845 ViewShare,
1846 0,
1849 if (NT_SUCCESS(Status))
1850 {
1853
1854 /* .text section header is unmodified */
1855 ok_hex(ImageFile->text_header.Misc.VirtualSize, RawSizeImageFile.text_header.Misc.VirtualSize);
1856 ok_hex(ImageFile->text_header.VirtualAddress, RawSizeImageFile.text_header.VirtualAddress);
1857 ok_hex(ImageFile->text_header.SizeOfRawData, RawSizeImageFile.text_header.SizeOfRawData);
1858 ok_hex(ImageFile->text_header.PointerToRawData, RawSizeImageFile.text_header.PointerToRawData);
1859
1860 /* SizeOfRawData = 0 resets PointerToRawData to 0 */
1861 ok_hex(ImageFile->zdata_header.Misc.VirtualSize, RawSizeImageFile.zdata_header.Misc.VirtualSize);
1862 ok_hex(ImageFile->zdata_header.VirtualAddress, RawSizeImageFile.zdata_header.VirtualAddress);
1863 ok_hex(ImageFile->zdata_header.SizeOfRawData, RawSizeImageFile.zdata_header.SizeOfRawData);
1864 ok_hex(ImageFile->zdata_header.PointerToRawData, 0);
1865
1866 /* .bss section is unmodified */
1867 ok_hex(ImageFile->bss_header.SizeOfRawData, 0x600);
1868 ok_hex(ImageFile->bss_header.PointerToRawData, 0);
1869
1870#define TEST_BYTE(n, v) \
1871 StartSeh() \
1872 ok(Bytes[n] == v, "[%lu] Bytes[%u] = 0x%x, expected 0x%x\n", \
1873 TestNumber, n, Bytes[n], v); \
1874 EndSeh(STATUS_SUCCESS);
1875 /* .text section data matches file up to 0x1000 */
1876 TEST_BYTE(0x1000, 0xc3);
1877 TEST_BYTE(0x1001, 0x00);
1878 TEST_BYTE(0x1008, 0x01);
1879 TEST_BYTE(0x1010, 0x02);
1880 TEST_BYTE(0x1fff, 0x0a);
1881
1882 /* .data section data matches file up to 0x1000 */
1883 TEST_BYTE(0x2000, 0x01);
1884 TEST_BYTE(0x2001, 0x23);
1885 TEST_BYTE(0x20ff, 0xee);
1886 TEST_BYTE(0x2100, 0xff);
1887 TEST_BYTE(0x2fff, 0xbb);
1888
1889 /* .zdata section data is all zeroes */
1890 TEST_BYTE(0x3000, 0x00);
1891 TEST_BYTE(0x3001, 0x00);
1892 TEST_BYTE(0x3800, 0x00);
1893 TEST_BYTE(0x3fff, 0x00);
1894
1895 /* .rsrc section data matches file up to VirtualSize 0x200 */
1896 TEST_BYTE(0x4000, 0xff);
1897 TEST_BYTE(0x4100, 0xee);
1898 TEST_BYTE(0x4180, 0xdd);
1899 TEST_BYTE(0x41c0, 0xcc);
1900 TEST_BYTE(0x41e0, 0xbb);
1901 TEST_BYTE(0x41f0, 0xaa);
1902 TEST_BYTE(0x41fe, 0x23);
1903 TEST_BYTE(0x41ff, 0x01);
1904 TEST_BYTE(0x4200, 0x00);
1905 TEST_BYTE(0x4fff, 0x00);
1906#undef TEST_BYTE
1909 }
1910 Status = NtClose(SectionHandle);
1912 }
1913
1916}
#define TEST_BYTE(n, v)
#define IsX86()
static struct _RAW_SIZE_IMAGE_FILE RawSizeImageFile
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
@ 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 1411 of file NtMapViewOfSection.c.

1412{
1414 WCHAR TempPath[MAX_PATH];
1416 HANDLE Handle;
1417 HANDLE SectionHandle;
1421 ULONG Written;
1422 ULONG Length;
1423 BOOL Success;
1424
1425 Length = GetTempPathW(MAX_PATH, TempPath);
1426 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1427 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1428 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1431 0,
1432 NULL,
1434 0,
1435 NULL);
1437 {
1438 skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1439 return;
1440 }
1441 if (Relocate)
1442 {
1443#ifdef _WIN64
1444 ok((ULONG_PTR)GetModuleHandle(NULL) <= 0x00007FFFFFFFFFFF, "Module at %p\n", GetModuleHandle(NULL));
1445#else
1446 ok((ULONG_PTR)GetModuleHandle(NULL) <= 0x80000000, "Module at %p\n", GetModuleHandle(NULL));
1447#endif
1448 SectionContentsImageFile.nthdrs.OptionalHeader.ImageBase = (ULONG_PTR)GetModuleHandle(NULL);
1449 }
1450 else
1451 {
1452 SectionContentsImageFile.nthdrs.OptionalHeader.ImageBase = 0xe400000;
1453 }
1454
1458 &Written,
1459 NULL);
1460 ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1461 ok(Written == sizeof(SectionContentsImageFile), "WriteFile wrote %lu bytes\n", Written);
1462
1463 Status = NtCreateSection(&SectionHandle,
1465 NULL,
1466 NULL,
1468 SEC_IMAGE,
1469 Handle);
1471
1472 if (NT_SUCCESS(Status))
1473 {
1474 /* Map the section with */
1475 BaseAddress = NULL;
1476 SectionOffset.QuadPart = 0;
1477 ViewSize = 0;
1478 Status = NtMapViewOfSection(SectionHandle,
1480 &BaseAddress,
1481 0,
1482 0,
1484 &ViewSize,
1485 ViewShare,
1486 0,
1488 if (Relocate)
1490 else
1492 if (NT_SUCCESS(Status))
1493 {
1495#define TEST_BYTE(n, v) StartSeh() ok_hex(Bytes[n], v); EndSeh(STATUS_SUCCESS);
1496#define TEST_WRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_SUCCESS);
1497#define TEST_NOWRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_ACCESS_VIOLATION);
1498 TEST_NOWRITE(0x2000);
1499 TEST_BYTE(0x2000, 0xc3);
1500 TEST_BYTE(0x2001, 0x00);
1501 TEST_NOWRITE(0x4000);
1502 TEST_BYTE(0x4000, 0x01);
1503 TEST_BYTE(0x4001, 0x23);
1504 TEST_BYTE(0x4007, 0xef);
1505 TEST_BYTE(0x4008, 0xaa);
1506 TEST_BYTE(0x4010, 0xbb);
1507 TEST_BYTE(0x4020, 0xcc);
1508 TEST_BYTE(0x4040, 0xdd);
1509 TEST_BYTE(0x4080, 0xee);
1510 TEST_BYTE(0x40c0, 0xff);
1511 TEST_BYTE(0x40ff, 0xff);
1512 TEST_BYTE(0x4100, 0x00);
1513 TEST_BYTE(0x41ff, 0x00);
1514 TEST_NOWRITE(0x6000);
1515 TEST_BYTE(0x6000, 0xee);
1516 TEST_BYTE(0x60ff, 0x55);
1517 TEST_BYTE(0x6100, 0xee);
1518 TEST_BYTE(0x63ff, 0xdd);
1519 TEST_BYTE(0x6400, 0x00);
1520 TEST_WRITE(0x8000);
1521 TEST_BYTE(0x8000, 0x11);
1522 TEST_BYTE(0x8400, 0x33);
1523#undef TEST_BYTE
1524#undef TEST_WRITE
1525#undef TEST_NOWRITE
1528 }
1529 Status = NtClose(SectionHandle);
1531 }
1532
1535}
#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:3576

Referenced by START_TEST().

◆ Test_Truncate()

static void Test_Truncate ( VOID  )
static

Definition at line 1967 of file NtMapViewOfSection.c.

1968{
1969 WCHAR TempPath[MAX_PATH];
1972 SIZE_T ViewSize = 0;
1973 HANDLE Handle;
1974 HANDLE SectionHandle;
1975
1976 SIZE_T Length;
1977 BOOL Success;
1978 DWORD Written, Error;
1980
1981 Length = GetTempPathW(MAX_PATH, TempPath);
1982 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1983 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1984 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1986
1987 Success = WriteFile(Handle, "TESTDATA", 8, &Written, NULL);
1988 ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1989 ok(Written == 8, "WriteFile wrote %lu bytes\n", Written);
1990
1991 Written = SetFilePointer(Handle, 6, NULL, FILE_BEGIN);
1992 ok(Written == 6, "SetFilePointer returned %lu bytes\n", Written);
1994 ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
1995
1996 Status = NtCreateSection(&SectionHandle,
2000 BaseAddress = NULL;
2001 ViewSize = 0;
2002 Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
2003 0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
2005
2006 if (BaseAddress)
2007 {
2008 // First we test data that was truncated even before the file mapping was opened
2009 Length = strlen((char*)BaseAddress);
2010 ok(Length == 6, "Old data was not properly erased! (Length=%lu)\n", Length);
2011 }
2012
2013 // Now we truncate the file on disk some more
2014 Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
2015 ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
2017 Error = GetLastError();
2018 ok(Success == FALSE, "SetEndOfFile succeeded\n");
2019 ok(Error == ERROR_USER_MAPPED_FILE, "SetEndOfFile did not set error to ERROR_USER_MAPPED_FILE (%lu)\n", Error);
2020
2021 if (BaseAddress)
2022 {
2023 Length = strlen((char*)BaseAddress);
2024 ok(Length == 6, "Length should not have changed! (Length=%lu)\n", Length);
2025 }
2026
2027 // Unmap and set the end shorter.
2030 Success = CloseHandle(SectionHandle);
2031 ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
2032
2033 Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
2034 ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
2036 ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
2037
2038 Status = NtCreateSection(&SectionHandle,
2042 BaseAddress = NULL;
2043 ViewSize = 0;
2044 Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
2045 0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
2047
2048 // CLEANUP
2051 Success = CloseHandle(SectionHandle);
2052 ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
2054 ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
2055
2057 ok(Success == TRUE, "DeleteFileW failed with %lu\n", GetLastError());
2058}
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:1004
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1592
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().