ReactOS  0.4.15-dev-1397-g19779b3
NtMapViewOfSection.c File Reference
#include "precomp.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 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 1166 of file NtMapViewOfSection.c.

◆ BYTES128

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

Definition at line 1163 of file NtMapViewOfSection.c.

◆ BYTES16

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

Definition at line 1160 of file NtMapViewOfSection.c.

◆ BYTES256

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

Definition at line 1164 of file NtMapViewOfSection.c.

◆ BYTES32

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

Definition at line 1161 of file NtMapViewOfSection.c.

◆ BYTES4

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

Definition at line 1158 of file NtMapViewOfSection.c.

◆ BYTES512

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

Definition at line 1165 of file NtMapViewOfSection.c.

◆ BYTES64

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

Definition at line 1162 of file NtMapViewOfSection.c.

◆ BYTES8

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

Definition at line 1159 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);
_In_ UINT Bytes
Definition: mmcopy.h:9
GLdouble n
Definition: glext.h:7729
#define StartSeh()
Definition: _sntprintf.h:16
const GLdouble * v
Definition: gl.h:2040
#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 _SECTION_CONTENTS_IMAGE_FILE, text_data)  = =0x400)

◆ C_ASSERT() [2/7]

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

◆ C_ASSERT() [3/7]

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

◆ 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 _RAW_SIZE_IMAGE_FILE, text_data)  = =0x400)

◆ C_ASSERT() [6/7]

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

◆ C_ASSERT() [7/7]

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

◆ START_TEST()

START_TEST ( NtMapViewOfSection  )

Definition at line 1993 of file NtMapViewOfSection.c.

1994 {
2000  Test_RawSize(0);
2001  Test_RawSize(1);
2002  Test_RawSize(2);
2003  Test_EmptyFile();
2004  Test_Truncate();
2005 }
static void Test_Truncate(VOID)
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
static void Test_RawSize(ULONG TestNumber)
void Test_PageFileSection(void)
void Test_ImageSection(void)
void Test_BasedSection(void)
static void Test_EmptyFile(VOID)
static void Test_SectionContents(BOOL Relocate)

◆ Test_BasedSection()

void Test_BasedSection ( void  )

Definition at line 1083 of file NtMapViewOfSection.c.

1084 {
1085  NTSTATUS Status;
1086  HANDLE SectionHandle1, SectionHandle2;
1088  PVOID BaseAddress1, BaseAddress2;
1089  SIZE_T ViewSize;
1090 
1091  /* Create a based section with SEC_COMMIT */
1092  MaximumSize.QuadPart = 0x1000;
1093  Status = NtCreateSection(&SectionHandle1,
1095  NULL,
1096  &MaximumSize,
1099  NULL);
1101 
1102  /* Map the 1st section */
1103  BaseAddress1 = NULL;
1104  SectionOffset.QuadPart = 0;
1105  ViewSize = 0;
1106  Status = NtMapViewOfSection(SectionHandle1,
1107  NtCurrentProcess(),
1108  &BaseAddress1,
1109  0,
1110  0,
1111  &SectionOffset,
1112  &ViewSize,
1113  ViewShare,
1114  0,
1115  PAGE_READWRITE);
1116 #if 0 // WOW64?
1118 #else
1120 #endif
1121 
1122  /* Create a 2nd based section with SEC_COMMIT */
1123  MaximumSize.QuadPart = 0x1000;
1124  Status = NtCreateSection(&SectionHandle2,
1126  NULL,
1127  &MaximumSize,
1130  NULL);
1132 
1133  /* Map the 2nd section */
1134  BaseAddress2 = NULL;
1135  SectionOffset.QuadPart = 0;
1136  ViewSize = 0;
1137  Status = NtMapViewOfSection(SectionHandle2,
1138  NtCurrentProcess(),
1139  &BaseAddress2,
1140  0,
1141  0,
1142  &SectionOffset,
1143  &ViewSize,
1144  ViewShare,
1145  0,
1146  PAGE_READWRITE);
1147 #if 0 // WOW64?
1149 #else
1151  ok((ULONG_PTR)BaseAddress2 < (ULONG_PTR)BaseAddress1,
1152  "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
1153  ok(((ULONG_PTR)BaseAddress1 - (ULONG_PTR)BaseAddress2) == 0x10000,
1154  "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
1155 #endif
1156 }
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:3373
LONG NTSTATUS
Definition: precomp.h:26
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define SEC_BASED
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define SEC_COMMIT
Definition: mmtypes.h:99
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:3554
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:404
#define NtCurrentProcess()
Definition: nt_native.h:1657
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:360
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:261
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by START_TEST().

◆ Test_EmptyFile()

static void Test_EmptyFile ( VOID  )
static

Definition at line 1852 of file NtMapViewOfSection.c.

1853 {
1854  NTSTATUS Status;
1855  WCHAR TempPath[MAX_PATH];
1857  HANDLE Handle;
1858  HANDLE SectionHandle;
1859  ULONG Length;
1860 
1861  Length = GetTempPathW(MAX_PATH, TempPath);
1862  ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1863  Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1864  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1867  0,
1868  NULL,
1869  CREATE_ALWAYS,
1870  0,
1871  NULL);
1873  {
1874  skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1875  return;
1876  }
1877 
1878  Status = NtCreateSection(&SectionHandle,
1880  0, 0, PAGE_READONLY, SEC_COMMIT, Handle);
1882 
1883  if (NT_SUCCESS(Status))
1884  NtClose(SectionHandle);
1885 
1886  Status = NtCreateSection(&SectionHandle,
1888  0, 0, PAGE_READONLY, SEC_IMAGE, Handle);
1890 
1891  if (NT_SUCCESS(Status))
1892  NtClose(SectionHandle);
1893 
1896 }
#define CloseHandle
Definition: compat.h:598
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
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:3373
#define FILE_ALL_ACCESS
Definition: nt_native.h:651
#define SECTION_QUERY
Definition: nt_native.h:1287
LONG NTSTATUS
Definition: precomp.h:26
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
#define SEC_COMMIT
Definition: mmtypes.h:99
DWORD WINAPI GetTempPathW(IN DWORD count, OUT LPWSTR path)
Definition: path.c:2079
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
Status
Definition: gdiplustypes.h:24
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:34
UINT WINAPI GetTempFileNameW(IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, IN UINT uUnique, OUT LPWSTR lpTempFileName)
Definition: filename.c:84
#define STATUS_INVALID_FILE_FOR_SECTION
Definition: ntstatus.h:269
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
static const WCHAR L[]
Definition: oid.c:1250
#define SECTION_MAP_READ
Definition: compat.h:139
#define CREATE_ALWAYS
Definition: disk.h:72
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
#define PAGE_READONLY
Definition: compat.h:138
#define CreateFileW
Definition: compat.h:600
#define skip(...)
Definition: atltest.h:64
_In_ HANDLE Handle
Definition: extypes.h:390
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
#define STANDARD_RIGHTS_REQUIRED
Definition: nt_native.h:63
unsigned int ULONG
Definition: retypes.h:1
#define SEC_IMAGE
Definition: mmtypes.h:96
#define STATUS_MAPPED_FILE_SIZE_ZERO
Definition: ntstatus.h:522

Referenced by START_TEST().

◆ Test_ImageSection()

void Test_ImageSection ( void  )

Definition at line 731 of file NtMapViewOfSection.c.

732 {
737  WCHAR TestDllPath[MAX_PATH];
738  HANDLE FileHandle, DataSectionHandle, ImageSectionHandle;
739  PVOID DataBase, ImageBase;
741 
742  GetModuleFileNameW(NULL, TestDllPath, RTL_NUMBER_OF(TestDllPath));
743  wcsrchr(TestDllPath, L'\\')[1] = UNICODE_NULL;
744  StringCbCatW(TestDllPath, sizeof(TestDllPath), L"testdata\\test.dll");
745  if (!RtlDosPathNameToNtPathName_U(TestDllPath,
746  &FileName,
747  NULL,
748  NULL))
749  {
750  ok(0, "RtlDosPathNameToNtPathName_U failed\n");
751  return;
752  }
753 
755  &FileName,
756  0,
757  NULL,
758  NULL);
759 
763  &IoStatusBlock,
767  if (!NT_SUCCESS(Status))
768  {
769  skip("Failed to open file %s\n", wine_dbgstr_wn(FileName.Buffer, FileName.Length / sizeof(WCHAR)));
770  return;
771  }
772 
773  /* Create a data section with write access */
774  Status = NtCreateSection(&DataSectionHandle,
775  SECTION_ALL_ACCESS, // DesiredAccess
776  NULL, // ObjectAttributes
777  NULL, // MaximumSize
778  PAGE_READWRITE, // SectionPageProtection
779  SEC_COMMIT, // AllocationAttributes
780  FileHandle);
782  if (!NT_SUCCESS(Status))
783  {
784  skip("Failed to create data section\n");
786  return;
787  }
788 
789  /* Map the data section as flat mapping */
790  DataBase = NULL;
791  ViewSize = 0;
792  Status = NtMapViewOfSection(DataSectionHandle,
794  &DataBase,
795  0,
796  0,
797  NULL,
798  &ViewSize,
799  ViewShare,
800  0,
803  //ok(ViewSize == 0x3f95cc48, "ViewSize wrong: 0x%lx\n");
804  if (!NT_SUCCESS(Status))
805  {
806  skip("Failed to map view of data section\n");
807  NtClose(DataSectionHandle);
809  return;
810  }
811 
812  /* Check the original data */
813  ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
814 
815  /* Modify the PE header (but do not flush!) */
816  *(ULONG*)DataBase = 0xdeadbabe;
817  ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
818 
819  /* Modify data in the .data section (but do not flush!) */
820  ok(*(ULONG*)((PUCHAR)DataBase + 0x800) == 0x12345678,
821  "Data in .data section invalid: 0x%lx!\n", *(ULONG*)((PUCHAR)DataBase + 0x800));
822  *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x87654321;
823 
824  /* Now try to create an image section (should fail) */
825  Status = NtCreateSection(&ImageSectionHandle,
826  SECTION_ALL_ACCESS, // DesiredAccess
827  NULL, // ObjectAttributes
828  NULL, // MaximumSize
829  PAGE_READWRITE, // SectionPageProtection
830  SEC_IMAGE, // AllocationAttributes
831  FileHandle);
833  if (NT_SUCCESS(Status)) NtClose(ImageSectionHandle);
834 
835  /* Restore the original header */
836  *(ULONG*)DataBase = 0x00905a4d;
837 
838  /* Modify data in the .data section (but do not flush!) */
839  ok_hex(*(ULONG*)((PUCHAR)DataBase + 0x800), 0x87654321);
840  *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xdeadbabe;
841 
842  /* Try to create an image section again */
843  Status = NtCreateSection(&ImageSectionHandle,
844  SECTION_ALL_ACCESS, // DesiredAccess
845  NULL, // ObjectAttributes
846  NULL, // MaximumSize
847  PAGE_READWRITE, // SectionPageProtection
848  SEC_IMAGE, // AllocationAttributes
849  FileHandle);
851  if (!NT_SUCCESS(Status))
852  {
853  skip("Failed to create image section\n");
854  NtClose(DataSectionHandle);
856  return;
857  }
858 
859  /* Map the image section */
860  ImageBase = NULL;
861  ViewSize = 0;
862  Status = NtMapViewOfSection(ImageSectionHandle,
864  &ImageBase,
865  0, // ZeroBits
866  0, // CommitSize
867  NULL, // SectionOffset
868  &ViewSize,
869  ViewShare,
870  0, // AllocationType
871  PAGE_READONLY);
872 #ifdef _M_IX86
874 #else
876 #endif
877  if (!NT_SUCCESS(Status))
878  {
879  skip("Failed to map view of image section\n");
880  NtClose(ImageSectionHandle);
881  NtClose(DataSectionHandle);
883  return;
884  }
885 
886  /* Check the header */
887  ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
888  ok(*(ULONG*)ImageBase == 0x00905a4d, "Header not ok\n");
889 
890  /* Check the data section. Either of these can be present! */
891  ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
892  (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
893  "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
894 
895  /* Now modify the data again */
896  *(ULONG*)DataBase = 0xdeadbabe;
897  *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xf00dada;
898 
899  /* Check the data */
900  ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
901  ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
902  ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
903  (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
904  "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
905 
906  /* Flush the view */
907  ViewSize = 0x1000;
909  &DataBase,
910  &ViewSize,
911  &IoStatusBlock);
913 
914  /* Check the data again */
915  ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
916  ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
917  (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
918  "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
919 
920  /* Restore the original header */
921  *(ULONG*)DataBase = 0x00905a4d;
922  ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
923 
924  /* Close the image mapping */
926  NtClose(ImageSectionHandle);
927 
928  /* Create an image section again */
929  Status = NtCreateSection(&ImageSectionHandle,
930  SECTION_ALL_ACCESS, // DesiredAccess
931  NULL, // ObjectAttributes
932  NULL, // MaximumSize
933  PAGE_READWRITE, // SectionPageProtection
934  SEC_IMAGE, // AllocationAttributes
935  FileHandle);
937  if (!NT_SUCCESS(Status))
938  {
939  skip("Failed to create image section\n");
940  NtClose(DataSectionHandle);
942  return;
943  }
944 
945  /* Map the image section again */
946  ImageBase = NULL;
947  ViewSize = 0;
948  Status = NtMapViewOfSection(ImageSectionHandle,
950  &ImageBase,
951  0,
952  0,
953  NULL,
954  &ViewSize,
955  ViewShare,
956  0,
957  PAGE_READONLY);
958 #ifdef _M_IX86
960 #else
962 #endif
963  if (!NT_SUCCESS(Status))
964  {
965  skip("Failed to map view of image section\n");
966  NtClose(ImageSectionHandle);
967  NtClose(DataSectionHandle);
969  return;
970  }
971 
972  // This one doesn't always work, needs investigation
973  /* Check the .data section again */
974  //ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada,
975  // "Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
976 
977  /* Restore the original data */
978  *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x12345678;
979 
980  /* Close the data mapping */
982 
983  NtClose(DataSectionHandle);
984 
985  /* Try to allocate memory inside the image mapping */
986  DataBase = (PUCHAR)ImageBase + 0x20000;
987  ViewSize = 0x1000;
990 
991  /* Cleanup */
993  NtClose(ImageSectionHandle);
995 }
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3782
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:3373
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
#define ok_hex(expression, result)
Definition: atltest.h:94
STRSAFEAPI StringCbCatW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:342
#define FILE_SHARE_READ
Definition: compat.h:136
_In_ PWDFDEVICE_INIT _In_ PWDF_FILEOBJECT_CONFIG _In_opt_ PWDF_OBJECT_ATTRIBUTES FileObjectAttributes
Definition: wdfdevice.h:3395
HANDLE FileHandle
Definition: stats.c:38
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define SEC_COMMIT
Definition: mmtypes.h:99
#define UNICODE_NULL
#define GENERIC_WRITE
Definition: nt_native.h:90
#define MEM_RESERVE
Definition: nt_native.h:1314
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:3554
NTSTATUS NTAPI NtFlushVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToFlush, OUT PIO_STATUS_BLOCK IoStatusBlock)
Definition: virtual.c:3950
#define PAGE_NOACCESS
Definition: nt_native.h:1302
#define NtCurrentProcess()
Definition: nt_native.h:1657
Status
Definition: gdiplustypes.h:24
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 STATUS_INVALID_IMAGE_NOT_MZ
Definition: ntstatus.h:539
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:34
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define STATUS_IMAGE_MACHINE_TYPE_MISMATCH
Definition: ntstatus.h:128
static const WCHAR L[]
Definition: oid.c:1250
const char * wine_dbgstr_wn(const WCHAR *str, int n)
Definition: compat.c:367
#define GENERIC_READ
Definition: compat.h:135
#define wcsrchr
Definition: compat.h:16
#define SYNCHRONIZE
Definition: nt_native.h:61
ULONG_PTR SIZE_T
Definition: typedefs.h:80
struct _FileName FileName
Definition: fatprocs.h:893
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:4407
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define ok(value,...)
Definition: atltest.h:57
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define NULL
Definition: types.h:112
#define PAGE_READONLY
Definition: compat.h:138
#define skip(...)
Definition: atltest.h:64
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
unsigned int ULONG
Definition: retypes.h:1
#define SEC_IMAGE
Definition: mmtypes.h:96
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:261
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)
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by START_TEST().

◆ Test_ImageSection2()

void Test_ImageSection2 ( void  )

Definition at line 998 of file NtMapViewOfSection.c.

999 {
1001  NTSTATUS Status;
1004  HANDLE FileHandle, ImageSectionHandle;
1005  PVOID ImageBase, BaseAddress;
1006  SIZE_T ViewSize;
1008 
1009  if (!RtlDosPathNameToNtPathName_U(L"testdata\\nvoglv32.dll",
1010  &FileName,
1011  NULL,
1012  NULL))
1013  {
1014  ok(0, "RtlDosPathNameToNtPathName_U failed\n");
1015  return;
1016  }
1017 
1019  &FileName,
1020  0,
1021  NULL,
1022  NULL);
1023 
1027  &IoStatusBlock,
1031  printf("Opened file with handle %p\n", FileHandle);
1032 
1033  /* Create a data section with write access */
1034  MaximumSize.QuadPart = 0x20000;
1035  Status = NtCreateSection(&ImageSectionHandle,
1036  SECTION_ALL_ACCESS, // DesiredAccess
1037  NULL, // ObjectAttributes
1038  &MaximumSize, // MaximumSize
1039  PAGE_READWRITE, // SectionPageProtection
1040  SEC_IMAGE, // AllocationAttributes
1041  FileHandle);
1043 
1044  printf("Created image section with handle %p\n", ImageSectionHandle);
1045  //system("PAUSE");
1046 
1047  /* Map the image section */
1048  ImageBase = NULL;
1049  ViewSize = 0x0000;
1050  SectionOffset.QuadPart = 0x00000;
1051  Status = NtMapViewOfSection(ImageSectionHandle,
1052  NtCurrentProcess(),
1053  &ImageBase,
1054  0,
1055  0,
1056  &SectionOffset,
1057  &ViewSize,
1058  ViewShare,
1059  0,
1060  PAGE_READWRITE);
1062 
1063  printf("Mapped image section at %p, value in text section: %lx\n",
1064  ImageBase, *((ULONG*)((PCHAR)ImageBase + 0x1196)));
1065  system("PAUSE");
1066 
1067  /* Try to allocate a page after the section */
1068  BaseAddress = (PUCHAR)ImageBase + 0x10000;
1069  ViewSize = 0x1000;
1071  &BaseAddress,
1072  0,
1073  &ViewSize,
1075  PAGE_READWRITE);
1076  printf("allocation status: %lx\n", Status);
1077  system("PAUSE");
1078 
1079 }
signed char * PCHAR
Definition: retypes.h:7
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:3373
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
#define MEM_COMMIT
Definition: nt_native.h:1313
#define FILE_SHARE_READ
Definition: compat.h:136
_In_ PWDFDEVICE_INIT _In_ PWDF_FILEOBJECT_CONFIG _In_opt_ PWDF_OBJECT_ATTRIBUTES FileObjectAttributes
Definition: wdfdevice.h:3395
HANDLE FileHandle
Definition: stats.c:38
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define GENERIC_WRITE
Definition: nt_native.h:90
#define MEM_RESERVE
Definition: nt_native.h:1314
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:3554
int __cdecl system(_In_opt_z_ const char *_Command)
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:404
#define NtCurrentProcess()
Definition: nt_native.h:1657
Status
Definition: gdiplustypes.h:24
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
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
static const WCHAR L[]
Definition: oid.c:1250
#define GENERIC_READ
Definition: compat.h:135
#define SYNCHRONIZE
Definition: nt_native.h:61
ULONG_PTR SIZE_T
Definition: typedefs.h:80
struct _FileName FileName
Definition: fatprocs.h:893
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:4407
#define ok(value,...)
Definition: atltest.h:57
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define NULL
Definition: types.h:112
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:360
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
unsigned int ULONG
Definition: retypes.h:1
#define SEC_IMAGE
Definition: mmtypes.h:96
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define STATUS_SUCCESS
Definition: shellext.h:65
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)
#define printf
Definition: config.h:203
#define PAGE_READWRITE
Definition: nt_native.h:1304

◆ Test_PageFileSection()

void Test_PageFileSection ( void  )

Definition at line 12 of file NtMapViewOfSection.c.

13 {
15  HANDLE SectionHandle;
17  PVOID BaseAddress, BaseAddress2;
19  ULONG OldProtect;
20 
21  /* Create a page file backed section with SEC_COMMIT */
22  MaximumSize.QuadPart = 0x20000;
23  Status = NtCreateSection(&SectionHandle,
25  NULL,
26  &MaximumSize,
28  SEC_COMMIT,
29  NULL);
31  if (!NT_SUCCESS(Status))
32  return;
33 
34  /* Try to map a page at an address that is not 64k aligned */
35  BaseAddress = (PVOID)0x30001000;
36  SectionOffset.QuadPart = 0;
37  ViewSize = 0x1000;
38  Status = NtMapViewOfSection(SectionHandle,
40  &BaseAddress,
41  0,
42  0,
44  &ViewSize,
45  ViewShare,
46  0,
49 
50  /* Try to map a page with execute rights */
51  BaseAddress = (PVOID)0x30000000;
52  SectionOffset.QuadPart = 0;
53  ViewSize = 0x1000;
54  Status = NtMapViewOfSection(SectionHandle,
56  &BaseAddress,
57  0,
58  0,
60  &ViewSize,
61  ViewShare,
62  0,
65 
66  /* Try to map 2 pages with MEM_COMMIT */
67  BaseAddress = (PVOID)0x30000000;
68  SectionOffset.QuadPart = 0;
69  ViewSize = 0x2000;
70  Status = NtMapViewOfSection(SectionHandle,
72  &BaseAddress,
73  0,
74  PAGE_SIZE,
76  &ViewSize,
77  ViewShare,
78  MEM_COMMIT,
81 
82  /* Try to map 1 page, with free base address and zero bits compatible with 64k granularity */
83  BaseAddress = NULL;
84  SectionOffset.QuadPart = 0;
85  ViewSize = 0x1000;
86  Status = NtMapViewOfSection(SectionHandle,
88  &BaseAddress,
89  10,
90  0,
92  &ViewSize,
93  ViewShare,
94  0,
99 
100 {
101  ULONG_PTR gran = 64 * 1024;
102  ULONG_PTR ZeroBits = 11;
103 
104  ok_hex(gran, 0x10000);
105  gran <<= ZeroBits;
106  ok_hex(gran, 0x8000000);
107  gran >>= ZeroBits;
108  ok_hex(gran, 0x10000);
109 
110  ok_hex((gran << ZeroBits) >> ZeroBits, gran);
111 
112 }
113 
114  /* Try to map 1 page, with free base address and zero bits incompatible with 64k granularity */
115  BaseAddress = NULL;
116  SectionOffset.QuadPart = 0;
117  ViewSize = 0x1000;
118  Status = NtMapViewOfSection(SectionHandle,
120  &BaseAddress,
121  11,
122  0,
123  &SectionOffset,
124  &ViewSize,
125  ViewShare,
126  0,
129 
130  /* Try to map 1 page, with base address and zero bits being compatible */
131  BaseAddress = (PVOID)0x30000000;
132  SectionOffset.QuadPart = 0;
133  ViewSize = 0x1000;
134  Status = NtMapViewOfSection(SectionHandle,
136  &BaseAddress,
137  2,
138  0,
139  &SectionOffset,
140  &ViewSize,
141  ViewShare,
142  0,
147 
148  /* Try to map 1 page, with base address and zero bits being incompatible */
149  BaseAddress = (PVOID)0x30000000;
150  SectionOffset.QuadPart = 0;
151  ViewSize = 0x1000;
152  Status = NtMapViewOfSection(SectionHandle,
154  &BaseAddress,
155  3,
156  0,
157  &SectionOffset,
158  &ViewSize,
159  ViewShare,
160  0,
163 
164  /* Map 2 pages, without MEM_COMMIT */
165  BaseAddress = (PVOID)0x30000000;
166  SectionOffset.QuadPart = 0;
167  ViewSize = 0x2000;
168  Status = NtMapViewOfSection(SectionHandle,
170  &BaseAddress,
171  0,
172  0,
173  &SectionOffset,
174  &ViewSize,
175  ViewShare,
176  0,
179 
180  /* We must be able to access the memory */
181  _SEH2_TRY
182  {
183  *(PULONG)BaseAddress = 1;
184  }
185  _SEH2_EXCEPT(1)
186  {
187  ok(FALSE, "Got an exception\n");
188  }
189  _SEH2_END;
190 
191  /* Commit a page in the section */
192  BaseAddress = (PVOID)0x30000000;
193  ViewSize = 0x1000;
195  &BaseAddress,
196  0,
197  &ViewSize,
198  MEM_COMMIT,
201 
202  /* Try to decommit a page in the section */
204  &BaseAddress,
205  &ViewSize,
206  MEM_DECOMMIT);
208 
209  /* Try to commit a range larger than the section */
210  BaseAddress = (PVOID)0x30000000;
211  ViewSize = 0x3000;
213  &BaseAddress,
214  0,
215  &ViewSize,
216  MEM_COMMIT,
219 
220  /* Try to commit a page after the section */
221  BaseAddress = (PVOID)0x30002000;
222  ViewSize = 0x1000;
224  &BaseAddress,
225  0,
226  &ViewSize,
227  MEM_COMMIT,
230 
231  /* Try to allocate a page after the section */
232  BaseAddress = (PVOID)0x30002000;
233  ViewSize = 0x1000;
235  &BaseAddress,
236  0,
237  &ViewSize,
241 
242  /* Need to go to next 64k boundary */
243  BaseAddress = (PVOID)0x30010000;
244  ViewSize = 0x1000;
246  &BaseAddress,
247  0,
248  &ViewSize,
252  if (!NT_SUCCESS(Status))
253  return;
254 
255  /* Free the allocation */
256  BaseAddress = (PVOID)0x30010000;
257  ViewSize = 0x1000;
259  &BaseAddress,
260  &ViewSize,
261  MEM_RELEASE);
262  ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed with Status %lx\n", Status);
263 
264  /* Try to release the section mapping with NtFreeVirtualMemory */
265  BaseAddress = (PVOID)0x30000000;
266  ViewSize = 0x1000;
268  &BaseAddress,
269  &ViewSize,
270  MEM_RELEASE);
272 
273  /* Commit a page in the section */
274  BaseAddress = (PVOID)0x30001000;
275  ViewSize = 0x1000;
277  &BaseAddress,
278  0,
279  &ViewSize,
280  MEM_COMMIT,
283 
284  /* Try to decommit the page */
285  BaseAddress = (PVOID)0x30001000;
286  ViewSize = 0x1000;
288  &BaseAddress,
289  &ViewSize,
290  MEM_DECOMMIT);
292 
293  BaseAddress = UlongToPtr(0x40000000);
294  SectionOffset.QuadPart = 0;
295  ViewSize = 0x1000;
296  Status = NtMapViewOfSection(SectionHandle,
298  &BaseAddress,
299  0,
300  0,
301  &SectionOffset,
302  &ViewSize,
303  ViewShare,
304  0,
307  if (!NT_SUCCESS(Status))
308  return;
309 
310  ok(BaseAddress == UlongToPtr(0x40000000), "Invalid BaseAddress: %p\n", BaseAddress);
311 
312  BaseAddress = (PVOID)0x40080000;
313  SectionOffset.QuadPart = 0x10000;
314  ViewSize = 0x1000;
315  Status = NtMapViewOfSection(SectionHandle,
317  &BaseAddress,
318  0,
319  0,
320  &SectionOffset,
321  &ViewSize,
322  ViewShare,
323  0,
326 
327  ok(BaseAddress == (PVOID)0x40080000, "Invalid BaseAddress: %p\n", BaseAddress);
328 
329  /* Commit a page in the section */
330  BaseAddress = (PVOID)0x40000000;
332  &BaseAddress,
333  0,
334  &ViewSize,
335  MEM_COMMIT,
338 
339  /* Close the mapping */
342  BaseAddress = (PVOID)0x30000000;
345  Status = NtClose(SectionHandle);
347 
348  /* Create a page file backed section, but only reserved */
349  MaximumSize.QuadPart = 0x20000;
350  Status = NtCreateSection(&SectionHandle,
352  NULL,
353  &MaximumSize,
355  SEC_RESERVE,
356  NULL);
358 
359  /* Try to map 1 page, passing MEM_RESERVE */
360  BaseAddress = NULL;
361  SectionOffset.QuadPart = 0;
363  Status = NtMapViewOfSection(SectionHandle,
365  &BaseAddress,
366  0,
367  PAGE_SIZE,
368  &SectionOffset,
369  &ViewSize,
370  ViewShare,
371  MEM_RESERVE,
374 
375  /* Try to map 1 page using MEM_COMMIT */
376  BaseAddress = NULL;
377  SectionOffset.QuadPart = 0;
379  Status = NtMapViewOfSection(SectionHandle,
381  &BaseAddress,
382  0,
383  PAGE_SIZE,
384  &SectionOffset,
385  &ViewSize,
386  ViewShare,
387  MEM_COMMIT,
390 
391  /* Map 2 pages, but commit 1 */
392  BaseAddress = NULL;
393  SectionOffset.QuadPart = 0;
394  ViewSize = 2 * PAGE_SIZE;
395  Status = NtMapViewOfSection(SectionHandle,
397  &BaseAddress,
398  0,
399  PAGE_SIZE,
400  &SectionOffset,
401  &ViewSize,
402  ViewShare,
403  0,
406 
407  /* We must be able to access the 1st page */
409  _SEH2_TRY
410  {
411  *(PUCHAR)BaseAddress = 1;
412  }
413  _SEH2_EXCEPT(1)
414  {
416  }
417  _SEH2_END;
419 
420  /* We must not be able to access the 2nd page */
422  _SEH2_TRY
423  {
424  *((PUCHAR)BaseAddress + PAGE_SIZE) = 1;
425  }
426  _SEH2_EXCEPT(1)
427  {
429  }
430  _SEH2_END;
432 
433  /* Map the 2 pages again into a different memory location */
434  BaseAddress2 = NULL;
435  Status = NtMapViewOfSection(SectionHandle,
437  &BaseAddress2,
438  0,
439  0,
440  &SectionOffset,
441  &ViewSize,
442  ViewShare,
443  0,
446 
447  /* Commit a the 2nd page in the 2nd memory location */
448  BaseAddress2 = (PUCHAR)BaseAddress2 + PAGE_SIZE;
451  &BaseAddress2,
452  0,
453  &ViewSize,
454  MEM_COMMIT,
455  PAGE_READONLY);
457 
458  /* Try to commit again (the already committed page) */
460  &BaseAddress2,
461  0,
462  &ViewSize,
463  MEM_COMMIT,
464  PAGE_READONLY);
466 
467  /* We must be able to access the memory in the 2nd page of the 1st memory location */
469  _SEH2_TRY
470  {
471  *((PUCHAR)BaseAddress + PAGE_SIZE) = 2;
472  }
473  _SEH2_EXCEPT(1)
474  {
476  }
477  _SEH2_END;
479 
480  ok(*(PULONG)BaseAddress2 == 2, "Value in memory was wrong\n");
481 
482  /* Close the mapping */
487  Status = NtClose(SectionHandle);
489 
490  /* Try to create a 512 GB page file backed section with committed pages */
491  MaximumSize.QuadPart = 0x8000000000;
492  Status = NtCreateSection(&SectionHandle,
494  NULL,
495  &MaximumSize,
497  SEC_COMMIT,
498  NULL);
500 
501  /* Try to create a huge page file backed section with PAGE_NOACCESS protection */
502  MaximumSize.QuadPart = 0x8000000000;
503  Status = NtCreateSection(&SectionHandle,
505  NULL,
506  &MaximumSize,
508  SEC_COMMIT,
509  NULL);
511 
512  /* Try to create a very huge page file backed section, but only reserved */
513  MaximumSize.QuadPart = 0x80000000000;
514  Status = NtCreateSection(&SectionHandle,
516  NULL,
517  &MaximumSize,
519  SEC_RESERVE,
520  NULL);
521 #ifdef _WIN64
523 #else
524  /* WoW64 returns STATUS_INSUFFICIENT_RESOURCES */
526  "got wrong Status: 0x%lx\n", Status);
527 #endif
528 
529  /* Try to create a even huger page file backed section, but only reserved */
530  MaximumSize.QuadPart = 0x800000000000;
531  Status = NtCreateSection(&SectionHandle,
533  NULL,
534  &MaximumSize,
536  SEC_RESERVE,
537  NULL);
539 
540  /* Create a 8 GB page file backed section, but only reserved */
541  MaximumSize.QuadPart = 0x200000000;
542  Status = NtCreateSection(&SectionHandle,
544  NULL,
545  &MaximumSize,
547  SEC_RESERVE,
548  NULL);
550 
551  /* Pass a too large region size */
552  BaseAddress = NULL;
553  SectionOffset.QuadPart = 0;
555  Status = NtMapViewOfSection(SectionHandle,
557  &BaseAddress,
558  0,
559  0,
560  &SectionOffset,
561  &ViewSize,
562  ViewShare,
563  0,
565 #ifdef _WIN64
567 #else
568  /* WoW64 returns STATUS_INVALID_PARAMETER_4 */
570  "got wrong Status: 0x%lx\n", Status);
571 #endif
572 
573  /* Pass 0 region size */
574  BaseAddress = NULL;
575  SectionOffset.QuadPart = 0;
576  ViewSize = 0;
577  Status = NtMapViewOfSection(SectionHandle,
579  &BaseAddress,
580  0,
581  0,
582  &SectionOffset,
583  &ViewSize,
584  ViewShare,
585  0,
587 #ifdef _WIN64
589  ok(ViewSize == 0x200000000, "wrong ViewSize: 0x%Ix\n", ViewSize);
590 #else
591  /* WoW64 returns STATUS_NO_MEMORY */
593  "got wrong Status: 0x%lx\n", Status);
594  ok(ViewSize == 0, "wrong ViewSize: 0x%Ix\n", ViewSize);
595 #endif
596 
597  /* Map with PAGE_NOACCESS */
598  BaseAddress = NULL;
599  SectionOffset.QuadPart = 0;
600  ViewSize = 0x20000000;
601  Status = NtMapViewOfSection(SectionHandle,
603  &BaseAddress,
604  0,
605  0,
606  &SectionOffset,
607  &ViewSize,
608  ViewShare,
609  0,
610  PAGE_NOACCESS);
612 
613  /* Try to change protection to read/write */
614  ViewSize = 0x1000;
615  OldProtect = -1;
616  BaseAddress2 = BaseAddress;
617  Status = NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2, &ViewSize, PAGE_READWRITE, &OldProtect);
619  // Windows 2003 returns bogus
620  //ok(OldProtect == PAGE_READWRITE, "Wrong protection returned: %u\n", OldProtect);
621 
622  /* Test read access */
624  _SEH2_TRY
625  {
626  (void)(*(volatile char*)BaseAddress2);
627  }
629  {
631  }
632  _SEH2_END;
634 
635  /* Try to change protection to read/write */
636  ViewSize = 0x1000;
637  OldProtect = -1;
638  BaseAddress2 = BaseAddress;
639  Status = NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2, &ViewSize, PAGE_READWRITE, &OldProtect);
641 #ifdef _WIN64
642  ok(OldProtect == 0, "Wrong protection returned: 0x%lx\n", OldProtect);
643 #else
644  // Windows 2003 returns bogus
645 #endif
646 
647  /* Try to change protection to readonly */
648  ViewSize = 0x1000;
649  OldProtect = -1;
650  BaseAddress2 = BaseAddress;
651  Status = NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2, &ViewSize, PAGE_READONLY, &OldProtect);
653 #ifdef _WIN64
654  //ok(OldProtect == 0, "Wrong protection returned: 0x%lx\n", OldProtect);
655 #else
656  // Windows 2003 returns bogus
657 #endif
658 
659  /* Commit a page */
660  ViewSize = 0x1000;
661  BaseAddress2 = BaseAddress;
664  ok(BaseAddress2 == BaseAddress, "Invalid base address: %p\n", BaseAddress2);
665 
666  /* Commit the page again */
667  ViewSize = 0x1000;
668  BaseAddress2 = BaseAddress;
671  ok(BaseAddress2 == BaseAddress, "Invalid base address: %p\n", BaseAddress2);
672 
673  /* Test read access */
675  _SEH2_TRY
676  {
677  (void)(*(volatile char*)BaseAddress2);
678  }
680  {
682  }
683  _SEH2_END;
685 
686  /* Test write access */
688  _SEH2_TRY
689  {
690  *(char*)BaseAddress2 = 1;
691  }
693  {
695  }
696  _SEH2_END;
698 
699  /* Update protection to PAGE_READWRITE */
700  ViewSize = 0x1000;
701  BaseAddress2 = BaseAddress;
704 
705  /* Test write access */
707  _SEH2_TRY
708  {
709  *(char*)BaseAddress2 = 1;
710  }
712  {
714  }
715  _SEH2_END;
717 
718  /* Update protection to PAGE_EXECUTE_READWRITE (1 page) */
719  ViewSize = 0x1000;
720  BaseAddress2 = BaseAddress;
723 
726  Status = NtClose(SectionHandle);
728 }
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR ZeroBits
Definition: mmfuncs.h:404
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3782
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
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:3373
unsigned char * PUCHAR
Definition: retypes.h:3
NTSTATUS NTAPI NtProtectVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UnsafeBaseAddress, IN OUT SIZE_T *UnsafeNumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG UnsafeOldAccessProtection)
Definition: virtual.c:3026
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_INVALID_PARAMETER_9
Definition: ntstatus.h:483
#define MAXULONG_PTR
Definition: basetsd.h:103
#define MEM_COMMIT
Definition: nt_native.h:1313
#define ok_hex(expression, result)
Definition: atltest.h:94
_SEH2_TRY
Definition: create.c:4226
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define MEM_DECOMMIT
Definition: nt_native.h:1315
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define STATUS_INVALID_PARAMETER_3
Definition: ntstatus.h:477
#define SEC_COMMIT
Definition: mmtypes.h:99
#define FALSE
Definition: types.h:117
#define SEC_RESERVE
Definition: nt_native.h:1323
#define MEM_RESERVE
Definition: nt_native.h:1314
#define STATUS_MAPPED_ALIGNMENT
Definition: ntstatus.h:676
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:3554
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define PAGE_NOACCESS
Definition: nt_native.h:1302
#define STATUS_SECTION_TOO_BIG
Definition: ntstatus.h:300
void * PVOID
Definition: retypes.h:9
#define UlongToPtr(u)
Definition: config.h:106
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:404
#define NtCurrentProcess()
Definition: nt_native.h:1657
Status
Definition: gdiplustypes.h:24
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_SECTION_PROTECTION
Definition: ntstatus.h:314
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define STATUS_COMMITMENT_LIMIT
Definition: ntstatus.h:537
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:305
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define STATUS_INVALID_VIEW_SIZE
Definition: ntstatus.h:268
_SEH2_END
Definition: create.c:4400
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:4407
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define ok(value,...)
Definition: atltest.h:57
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
unsigned int * PULONG
Definition: retypes.h:1
#define NULL
Definition: types.h:112
#define PAGE_READONLY
Definition: compat.h:138
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:360
#define MEM_RELEASE
Definition: nt_native.h:1316
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
unsigned int ULONG
Definition: retypes.h:1
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define STATUS_SUCCESS
Definition: shellext.h:65
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
#define STATUS_INVALID_PARAMETER_4
Definition: ntstatus.h:478
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:261
#define STATUS_UNABLE_TO_DELETE_SECTION
Definition: ntstatus.h:264
#define PAGE_READWRITE
Definition: nt_native.h:1304
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5130

Referenced by START_TEST().

◆ Test_RawSize()

static void Test_RawSize ( ULONG  TestNumber)
static

Definition at line 1698 of file NtMapViewOfSection.c.

1699 {
1700  NTSTATUS Status;
1701  WCHAR TempPath[MAX_PATH];
1703  HANDLE Handle;
1704  HANDLE SectionHandle;
1707  SIZE_T ViewSize;
1708  ULONG Written;
1709  ULONG Length;
1710  BOOL Success;
1711 
1712  Length = GetTempPathW(MAX_PATH, TempPath);
1713  ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1714  Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1715  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1718  0,
1719  NULL,
1720  CREATE_ALWAYS,
1721  0,
1722  NULL);
1724  {
1725  skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1726  return;
1727  }
1728  RawSizeImageFile.nthdrs.OptionalHeader.ImageBase = 0xe400000;
1729  if (TestNumber == 1)
1730  {
1731  /* Just for fun, show that these flags don't matter. */
1736  }
1737  else if (TestNumber == 2)
1738  {
1739  /* SizeOfRawData is too large and will overflow.
1740  * This should cause failure to load the file */
1741  RawSizeImageFile.rsrc_header.SizeOfRawData = (ULONG)-0x200;
1742  }
1743 
1746  sizeof(RawSizeImageFile),
1747  &Written,
1748  NULL);
1749  ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1750  ok(Written == sizeof(RawSizeImageFile), "WriteFile wrote %lu bytes\n", Written);
1751 
1752  Status = NtCreateSection(&SectionHandle,
1754  NULL,
1755  NULL,
1757  SEC_IMAGE,
1758  Handle);
1759  if (TestNumber == 2)
1760  {
1761  /* overflow in SizeOfRawData */
1763  }
1764  else
1765  {
1767  }
1768 
1769  if (NT_SUCCESS(Status))
1770  {
1771  /* Map the section with */
1772  BaseAddress = NULL;
1773  SectionOffset.QuadPart = 0;
1774  ViewSize = 0;
1775  Status = NtMapViewOfSection(SectionHandle,
1776  NtCurrentProcess(),
1777  &BaseAddress,
1778  0,
1779  0,
1780  &SectionOffset,
1781  &ViewSize,
1782  ViewShare,
1783  0,
1784  PAGE_READWRITE);
1786  if (NT_SUCCESS(Status))
1787  {
1790 
1791  /* .text section header is unmodified */
1792  ok_hex(ImageFile->text_header.Misc.VirtualSize, RawSizeImageFile.text_header.Misc.VirtualSize);
1793  ok_hex(ImageFile->text_header.VirtualAddress, RawSizeImageFile.text_header.VirtualAddress);
1794  ok_hex(ImageFile->text_header.SizeOfRawData, RawSizeImageFile.text_header.SizeOfRawData);
1795  ok_hex(ImageFile->text_header.PointerToRawData, RawSizeImageFile.text_header.PointerToRawData);
1796 
1797  /* SizeOfRawData = 0 resets PointerToRawData to 0 */
1798  ok_hex(ImageFile->zdata_header.Misc.VirtualSize, RawSizeImageFile.zdata_header.Misc.VirtualSize);
1799  ok_hex(ImageFile->zdata_header.VirtualAddress, RawSizeImageFile.zdata_header.VirtualAddress);
1800  ok_hex(ImageFile->zdata_header.SizeOfRawData, RawSizeImageFile.zdata_header.SizeOfRawData);
1801  ok_hex(ImageFile->zdata_header.PointerToRawData, 0);
1802 
1803 #define TEST_BYTE(n, v) \
1804  StartSeh() \
1805  ok(Bytes[n] == v, "[%lu] Bytes[%u] = 0x%x, expected 0x%x\n", \
1806  TestNumber, n, Bytes[n], v); \
1807  EndSeh(STATUS_SUCCESS);
1808  /* .text section data matches file up to 0x1000 */
1809  TEST_BYTE(0x1000, 0xc3);
1810  TEST_BYTE(0x1001, 0x00);
1811  TEST_BYTE(0x1008, 0x01);
1812  TEST_BYTE(0x1010, 0x02);
1813  TEST_BYTE(0x1fff, 0x0a);
1814 
1815  /* .data section data matches file up to 0x1000 */
1816  TEST_BYTE(0x2000, 0x01);
1817  TEST_BYTE(0x2001, 0x23);
1818  TEST_BYTE(0x20ff, 0xee);
1819  TEST_BYTE(0x2100, 0xff);
1820  TEST_BYTE(0x2fff, 0xbb);
1821 
1822  /* .zdata section data is all zeroes */
1823  TEST_BYTE(0x3000, 0x00);
1824  TEST_BYTE(0x3001, 0x00);
1825  TEST_BYTE(0x3800, 0x00);
1826  TEST_BYTE(0x3fff, 0x00);
1827 
1828  /* .rsrc section data matches file up to VirtualSize 0x200 */
1829  TEST_BYTE(0x4000, 0xff);
1830  TEST_BYTE(0x4100, 0xee);
1831  TEST_BYTE(0x4180, 0xdd);
1832  TEST_BYTE(0x41c0, 0xcc);
1833  TEST_BYTE(0x41e0, 0xbb);
1834  TEST_BYTE(0x41f0, 0xaa);
1835  TEST_BYTE(0x41fe, 0x23);
1836  TEST_BYTE(0x41ff, 0x01);
1837  TEST_BYTE(0x4200, 0x00);
1838  TEST_BYTE(0x4fff, 0x00);
1839 #undef TEST_BYTE
1842  }
1843  Status = NtClose(SectionHandle);
1845  }
1846 
1849 }
static PWSTR ImageFile
Definition: imagefile.c:10
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3782
#define CloseHandle
Definition: compat.h:598
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
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:3373
#define FILE_ALL_ACCESS
Definition: nt_native.h:651
#define TRUE
Definition: types.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
_In_ UINT Bytes
Definition: mmcopy.h:9
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
#define ok_hex(expression, result)
Definition: atltest.h:94
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
DWORD WINAPI GetTempPathW(IN DWORD count, OUT LPWSTR path)
Definition: path.c:2079
unsigned int BOOL
Definition: ntddk_ex.h:94
static struct _RAW_SIZE_IMAGE_FILE RawSizeImageFile
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
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:3554
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:404
#define NtCurrentProcess()
Definition: nt_native.h:1657
Status
Definition: gdiplustypes.h:24
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define IMAGE_SCN_CNT_INITIALIZED_DATA
Definition: ntimage.h:231
#define STATUS_INVALID_IMAGE_FORMAT
Definition: ntstatus.h:359
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:34
UINT WINAPI GetTempFileNameW(IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, IN UINT uUnique, OUT LPWSTR lpTempFileName)
Definition: filename.c:84
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
static const WCHAR L[]
Definition: oid.c:1250
#define IMAGE_SCN_CNT_CODE
Definition: ntimage.h:230
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA
Definition: ntimage.h:232
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define CREATE_ALWAYS
Definition: disk.h:72
#define TEST_BYTE(n, v)
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
#define CreateFileW
Definition: compat.h:600
#define skip(...)
Definition: atltest.h:64
_In_ HANDLE Handle
Definition: extypes.h:390
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
unsigned int ULONG
Definition: retypes.h:1
#define SEC_IMAGE
Definition: mmtypes.h:96
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define STATUS_SUCCESS
Definition: shellext.h:65
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by START_TEST().

◆ Test_SectionContents()

static void Test_SectionContents ( BOOL  Relocate)
static

Definition at line 1368 of file NtMapViewOfSection.c.

1369 {
1370  NTSTATUS Status;
1371  WCHAR TempPath[MAX_PATH];
1373  HANDLE Handle;
1374  HANDLE SectionHandle;
1377  SIZE_T ViewSize;
1378  ULONG Written;
1379  ULONG Length;
1380  BOOL Success;
1381 
1382  Length = GetTempPathW(MAX_PATH, TempPath);
1383  ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1384  Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1385  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1388  0,
1389  NULL,
1390  CREATE_ALWAYS,
1391  0,
1392  NULL);
1394  {
1395  skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1396  return;
1397  }
1398  if (Relocate)
1399  {
1400  ok((ULONG_PTR)GetModuleHandle(NULL) <= 0x80000000, "Module at %p\n", GetModuleHandle(NULL));
1401  SectionContentsImageFile.nthdrs.OptionalHeader.ImageBase = (ULONG)(ULONG_PTR)GetModuleHandle(NULL);
1402  }
1403  else
1404  {
1405  SectionContentsImageFile.nthdrs.OptionalHeader.ImageBase = 0xe400000;
1406  }
1407 
1410  sizeof(SectionContentsImageFile),
1411  &Written,
1412  NULL);
1413  ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1414  ok(Written == sizeof(SectionContentsImageFile), "WriteFile wrote %lu bytes\n", Written);
1415 
1416  Status = NtCreateSection(&SectionHandle,
1418  NULL,
1419  NULL,
1421  SEC_IMAGE,
1422  Handle);
1424 
1425  if (NT_SUCCESS(Status))
1426  {
1427  /* Map the section with */
1428  BaseAddress = NULL;
1429  SectionOffset.QuadPart = 0;
1430  ViewSize = 0;
1431  Status = NtMapViewOfSection(SectionHandle,
1432  NtCurrentProcess(),
1433  &BaseAddress,
1434  0,
1435  0,
1436  &SectionOffset,
1437  &ViewSize,
1438  ViewShare,
1439  0,
1440  PAGE_READWRITE);
1441  if (Relocate)
1443  else
1445  if (NT_SUCCESS(Status))
1446  {
1448 #define TEST_BYTE(n, v) StartSeh() ok_hex(Bytes[n], v); EndSeh(STATUS_SUCCESS);
1449 #define TEST_WRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_SUCCESS);
1450 #define TEST_NOWRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_ACCESS_VIOLATION);
1451  TEST_NOWRITE(0x2000);
1452  TEST_BYTE(0x2000, 0xc3);
1453  TEST_BYTE(0x2001, 0x00);
1454  TEST_NOWRITE(0x4000);
1455  TEST_BYTE(0x4000, 0x01);
1456  TEST_BYTE(0x4001, 0x23);
1457  TEST_BYTE(0x4007, 0xef);
1458  TEST_BYTE(0x4008, 0xaa);
1459  TEST_BYTE(0x4010, 0xbb);
1460  TEST_BYTE(0x4020, 0xcc);
1461  TEST_BYTE(0x4040, 0xdd);
1462  TEST_BYTE(0x4080, 0xee);
1463  TEST_BYTE(0x40c0, 0xff);
1464  TEST_BYTE(0x40ff, 0xff);
1465  TEST_BYTE(0x4100, 0x00);
1466  TEST_BYTE(0x41ff, 0x00);
1467  TEST_NOWRITE(0x6000);
1468  TEST_BYTE(0x6000, 0xee);
1469  TEST_BYTE(0x60ff, 0x55);
1470  TEST_BYTE(0x6100, 0xee);
1471  TEST_BYTE(0x63ff, 0xdd);
1472  TEST_BYTE(0x6400, 0x00);
1473  TEST_WRITE(0x8000);
1474  TEST_BYTE(0x8000, 0x11);
1475  TEST_BYTE(0x8400, 0x33);
1476 #undef TEST_BYTE
1477 #undef TEST_WRITE
1478 #undef TEST_NOWRITE
1481  }
1482  Status = NtClose(SectionHandle);
1484  }
1485 
1488 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3782
#define CloseHandle
Definition: compat.h:598
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
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:3373
#define FILE_ALL_ACCESS
Definition: nt_native.h:651
#define TRUE
Definition: types.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
static struct _SECTION_CONTENTS_IMAGE_FILE SectionContentsImageFile
_In_ UINT Bytes
Definition: mmcopy.h:9
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define TEST_WRITE(n)
DWORD WINAPI GetTempPathW(IN DWORD count, OUT LPWSTR path)
Definition: path.c:2079
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
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:3554
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:404
#define NtCurrentProcess()
Definition: nt_native.h:1657
Status
Definition: gdiplustypes.h:24
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:34
UINT WINAPI GetTempFileNameW(IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, IN UINT uUnique, OUT LPWSTR lpTempFileName)
Definition: filename.c:84
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
static const WCHAR L[]
Definition: oid.c:1250
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define STATUS_IMAGE_NOT_AT_BASE
Definition: ntstatus.h:117
#define CREATE_ALWAYS
Definition: disk.h:72
#define GetModuleHandle
Definition: winbase.h:3667
#define TEST_BYTE(n, v)
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
#define CreateFileW
Definition: compat.h:600
#define skip(...)
Definition: atltest.h:64
_In_ HANDLE Handle
Definition: extypes.h:390
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
unsigned int ULONG
Definition: retypes.h:1
#define SEC_IMAGE
Definition: mmtypes.h:96
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define STATUS_SUCCESS
Definition: shellext.h:65
#define TEST_NOWRITE(n)
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by START_TEST().

◆ Test_Truncate()

static void Test_Truncate ( VOID  )
static

Definition at line 1900 of file NtMapViewOfSection.c.

1901 {
1902  WCHAR TempPath[MAX_PATH];
1904  NTSTATUS Status;
1905  SIZE_T ViewSize = 0;
1906  HANDLE Handle;
1907  HANDLE SectionHandle;
1908 
1909  SIZE_T Length;
1910  BOOL Success;
1911  DWORD Written, Error;
1912  VOID* BaseAddress;
1913 
1914  Length = GetTempPathW(MAX_PATH, TempPath);
1915  ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1916  Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1917  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1919 
1920  Success = WriteFile(Handle, "TESTDATA", 8, &Written, NULL);
1921  ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1922  ok(Written == 8, "WriteFile wrote %lu bytes\n", Written);
1923 
1924  Written = SetFilePointer(Handle, 6, NULL, FILE_BEGIN);
1925  ok(Written == 6, "SetFilePointer returned %lu bytes\n", Written);
1927  ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
1928 
1929  Status = NtCreateSection(&SectionHandle,
1931  0, 0, PAGE_READONLY, SEC_COMMIT, Handle);
1933  BaseAddress = NULL;
1934  ViewSize = 0;
1935  Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
1936  0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
1938 
1939  if (BaseAddress)
1940  {
1941  // First we test data that was truncated even before the file mapping was opened
1942  Length = strlen((char*)BaseAddress);
1943  ok(Length == 6, "Old data was not properly erased! (Length=%lu)\n", Length);
1944  }
1945 
1946  // Now we truncate the file on disk some more
1947  Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
1948  ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
1950  Error = GetLastError();
1951  ok(Success == FALSE, "SetEndOfFile succeeded\n");
1952  ok(Error == ERROR_USER_MAPPED_FILE, "SetEndOfFile did not set error to ERROR_USER_MAPPED_FILE (%lu)\n", Error);
1953 
1954  if (BaseAddress)
1955  {
1956  Length = strlen((char*)BaseAddress);
1957  ok(Length == 6, "Length should not have changed! (Length=%lu)\n", Length);
1958  }
1959 
1960  // Unmap and set the end shorter.
1963  Success = CloseHandle(SectionHandle);
1964  ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
1965 
1966  Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
1967  ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
1969  ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
1970 
1971  Status = NtCreateSection(&SectionHandle,
1973  0, 0, PAGE_READONLY, SEC_COMMIT, Handle);
1975  BaseAddress = NULL;
1976  ViewSize = 0;
1977  Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
1978  0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
1980 
1981  // CLEANUP
1984  Success = CloseHandle(SectionHandle);
1985  ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
1987  ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
1988 
1990  ok(Success == TRUE, "DeleteFileW failed with %lu\n", GetLastError());
1991 }
#define ERROR_USER_MAPPED_FILE
Definition: winerror.h:727
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3782
#define CloseHandle
Definition: compat.h:598
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
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:3373
#define FILE_ALL_ACCESS
Definition: nt_native.h:651
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define TRUE
Definition: types.h:120
#define SECTION_QUERY
Definition: nt_native.h:1287
BOOL WINAPI SetEndOfFile(HANDLE hFile)
Definition: fileinfo.c:1004
LONG NTSTATUS
Definition: precomp.h:26
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
#define FILE_BEGIN
Definition: compat.h:620
#define SEC_COMMIT
Definition: mmtypes.h:99
DWORD WINAPI GetTempPathW(IN DWORD count, OUT LPWSTR path)
Definition: path.c:2079
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
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:3554
#define NtCurrentProcess()
Definition: nt_native.h:1657
Status
Definition: gdiplustypes.h:24
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MAX_PATH
Definition: compat.h:34
UINT WINAPI GetTempFileNameW(IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, IN UINT uUnique, OUT LPWSTR lpTempFileName)
Definition: filename.c:84
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
static const WCHAR L[]
Definition: oid.c:1250
#define SECTION_MAP_READ
Definition: compat.h:139
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define CREATE_ALWAYS
Definition: disk.h:72
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
#define PAGE_READONLY
Definition: compat.h:138
#define CreateFileW
Definition: compat.h:600
_In_ HANDLE Handle
Definition: extypes.h:390
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
#define STANDARD_RIGHTS_REQUIRED
Definition: nt_native.h:63
#define STATUS_SUCCESS
Definition: shellext.h:65
#define SetFilePointer
Definition: compat.h:602

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().