ReactOS  0.4.15-dev-3163-gf17c2c0
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 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 1168 of file NtMapViewOfSection.c.

◆ BYTES128

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

Definition at line 1165 of file NtMapViewOfSection.c.

◆ BYTES16

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

Definition at line 1162 of file NtMapViewOfSection.c.

◆ BYTES256

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

Definition at line 1166 of file NtMapViewOfSection.c.

◆ BYTES32

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

Definition at line 1163 of file NtMapViewOfSection.c.

◆ BYTES4

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

Definition at line 1160 of file NtMapViewOfSection.c.

◆ BYTES512

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

Definition at line 1167 of file NtMapViewOfSection.c.

◆ BYTES64

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

Definition at line 1164 of file NtMapViewOfSection.c.

◆ BYTES8

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

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

1996 {
2002  Test_RawSize(0);
2003  Test_RawSize(1);
2004  Test_RawSize(2);
2005  Test_EmptyFile();
2006  Test_Truncate();
2007 }
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 1085 of file NtMapViewOfSection.c.

1086 {
1087  NTSTATUS Status;
1088  HANDLE SectionHandle1, SectionHandle2;
1090  PVOID BaseAddress1, BaseAddress2;
1091  SIZE_T ViewSize;
1092 
1093  /* Create a based section with SEC_COMMIT */
1094  MaximumSize.QuadPart = 0x1000;
1095  Status = NtCreateSection(&SectionHandle1,
1097  NULL,
1098  &MaximumSize,
1101  NULL);
1103 
1104  /* Map the 1st section */
1105  BaseAddress1 = NULL;
1106  SectionOffset.QuadPart = 0;
1107  ViewSize = 0;
1108  Status = NtMapViewOfSection(SectionHandle1,
1109  NtCurrentProcess(),
1110  &BaseAddress1,
1111  0,
1112  0,
1113  &SectionOffset,
1114  &ViewSize,
1115  ViewShare,
1116  0,
1117  PAGE_READWRITE);
1118 #if 0 // WOW64?
1120 #else
1122 #endif
1123 
1124  /* Create a 2nd based section with SEC_COMMIT */
1125  MaximumSize.QuadPart = 0x1000;
1126  Status = NtCreateSection(&SectionHandle2,
1128  NULL,
1129  &MaximumSize,
1132  NULL);
1134 
1135  /* Map the 2nd section */
1136  BaseAddress2 = NULL;
1137  SectionOffset.QuadPart = 0;
1138  ViewSize = 0;
1139  Status = NtMapViewOfSection(SectionHandle2,
1140  NtCurrentProcess(),
1141  &BaseAddress2,
1142  0,
1143  0,
1144  &SectionOffset,
1145  &ViewSize,
1146  ViewShare,
1147  0,
1148  PAGE_READWRITE);
1149 #if 0 // WOW64?
1151 #else
1153  ok((ULONG_PTR)BaseAddress2 < (ULONG_PTR)BaseAddress1,
1154  "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
1155  ok(((ULONG_PTR)BaseAddress1 - (ULONG_PTR)BaseAddress2) == 0x10000,
1156  "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1, BaseAddress2);
1157 #endif
1158 }
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:3433
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:3614
_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 1854 of file NtMapViewOfSection.c.

1855 {
1856  NTSTATUS Status;
1857  WCHAR TempPath[MAX_PATH];
1859  HANDLE Handle;
1860  HANDLE SectionHandle;
1861  ULONG Length;
1862 
1863  Length = GetTempPathW(MAX_PATH, TempPath);
1864  ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1865  Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1866  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1869  0,
1870  NULL,
1871  CREATE_ALWAYS,
1872  0,
1873  NULL);
1875  {
1876  skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1877  return;
1878  }
1879 
1880  Status = NtCreateSection(&SectionHandle,
1882  0, 0, PAGE_READONLY, SEC_COMMIT, Handle);
1884 
1885  if (NT_SUCCESS(Status))
1886  NtClose(SectionHandle);
1887 
1888  Status = NtCreateSection(&SectionHandle,
1890  0, 0, PAGE_READONLY, SEC_IMAGE, Handle);
1892 
1893  if (NT_SUCCESS(Status))
1894  NtClose(SectionHandle);
1895 
1898 }
#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:3433
#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:1040
#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:3398
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 733 of file NtMapViewOfSection.c.

734 {
739  WCHAR TestDllPath[MAX_PATH];
740  HANDLE FileHandle, DataSectionHandle, ImageSectionHandle;
741  PVOID DataBase, ImageBase;
743 
744  GetModuleFileNameW(NULL, TestDllPath, RTL_NUMBER_OF(TestDllPath));
745  wcsrchr(TestDllPath, L'\\')[1] = UNICODE_NULL;
746  StringCbCatW(TestDllPath, sizeof(TestDllPath), L"testdata\\test.dll");
747  if (!RtlDosPathNameToNtPathName_U(TestDllPath,
748  &FileName,
749  NULL,
750  NULL))
751  {
752  ok(0, "RtlDosPathNameToNtPathName_U failed\n");
753  return;
754  }
755 
757  &FileName,
758  0,
759  NULL,
760  NULL);
761 
765  &IoStatusBlock,
769  if (!NT_SUCCESS(Status))
770  {
771  skip("Failed to open file %s\n", wine_dbgstr_wn(FileName.Buffer, FileName.Length / sizeof(WCHAR)));
772  return;
773  }
774 
775  /* Create a data section with write access */
776  Status = NtCreateSection(&DataSectionHandle,
777  SECTION_ALL_ACCESS, // DesiredAccess
778  NULL, // ObjectAttributes
779  NULL, // MaximumSize
780  PAGE_READWRITE, // SectionPageProtection
781  SEC_COMMIT, // AllocationAttributes
782  FileHandle);
784  if (!NT_SUCCESS(Status))
785  {
786  skip("Failed to create data section\n");
788  return;
789  }
790 
791  /* Map the data section as flat mapping */
792  DataBase = NULL;
793  ViewSize = 0;
794  Status = NtMapViewOfSection(DataSectionHandle,
796  &DataBase,
797  0,
798  0,
799  NULL,
800  &ViewSize,
801  ViewShare,
802  0,
805  //ok(ViewSize == 0x3f95cc48, "ViewSize wrong: 0x%lx\n");
806  if (!NT_SUCCESS(Status))
807  {
808  skip("Failed to map view of data section\n");
809  NtClose(DataSectionHandle);
811  return;
812  }
813 
814  /* Check the original data */
815  ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
816 
817  /* Modify the PE header (but do not flush!) */
818  *(ULONG*)DataBase = 0xdeadbabe;
819  ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
820 
821  /* Modify data in the .data section (but do not flush!) */
822  ok(*(ULONG*)((PUCHAR)DataBase + 0x800) == 0x12345678,
823  "Data in .data section invalid: 0x%lx!\n", *(ULONG*)((PUCHAR)DataBase + 0x800));
824  *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x87654321;
825 
826  /* Now try to create an image section (should fail) */
827  Status = NtCreateSection(&ImageSectionHandle,
828  SECTION_ALL_ACCESS, // DesiredAccess
829  NULL, // ObjectAttributes
830  NULL, // MaximumSize
831  PAGE_READWRITE, // SectionPageProtection
832  SEC_IMAGE, // AllocationAttributes
833  FileHandle);
835  if (NT_SUCCESS(Status)) NtClose(ImageSectionHandle);
836 
837  /* Restore the original header */
838  *(ULONG*)DataBase = 0x00905a4d;
839 
840  /* Modify data in the .data section (but do not flush!) */
841  ok_hex(*(ULONG*)((PUCHAR)DataBase + 0x800), 0x87654321);
842  *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xdeadbabe;
843 
844  /* Try to create an image section again */
845  Status = NtCreateSection(&ImageSectionHandle,
846  SECTION_ALL_ACCESS, // DesiredAccess
847  NULL, // ObjectAttributes
848  NULL, // MaximumSize
849  PAGE_READWRITE, // SectionPageProtection
850  SEC_IMAGE, // AllocationAttributes
851  FileHandle);
853  if (!NT_SUCCESS(Status))
854  {
855  skip("Failed to create image section\n");
856  NtClose(DataSectionHandle);
858  return;
859  }
860 
861  /* Map the image section */
862  ImageBase = NULL;
863  ViewSize = 0;
864  Status = NtMapViewOfSection(ImageSectionHandle,
866  &ImageBase,
867  0, // ZeroBits
868  0, // CommitSize
869  NULL, // SectionOffset
870  &ViewSize,
871  ViewShare,
872  0, // AllocationType
873  PAGE_READONLY);
874 #ifdef _M_IX86
876 #else
878 #endif
879  if (!NT_SUCCESS(Status))
880  {
881  skip("Failed to map view of image section\n");
882  NtClose(ImageSectionHandle);
883  NtClose(DataSectionHandle);
885  return;
886  }
887 
888  /* Check the header */
889  ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
890  ok(*(ULONG*)ImageBase == 0x00905a4d, "Header not ok\n");
891 
892  /* Check the data section. Either of these can be present! */
893  ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
894  (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
895  "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
896 
897  /* Now modify the data again */
898  *(ULONG*)DataBase = 0xdeadbabe;
899  *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xf00dada;
900 
901  /* Check the data */
902  ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
903  ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
904  ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
905  (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
906  "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
907 
908  /* Flush the view */
909  ViewSize = 0x1000;
911  &DataBase,
912  &ViewSize,
913  &IoStatusBlock);
915 
916  /* Check the data again */
917  ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
918  ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
919  (*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
920  "Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
921 
922  /* Restore the original header */
923  *(ULONG*)DataBase = 0x00905a4d;
924  ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
925 
926  /* Close the image mapping */
928  NtClose(ImageSectionHandle);
929 
930  /* Create an image section again */
931  Status = NtCreateSection(&ImageSectionHandle,
932  SECTION_ALL_ACCESS, // DesiredAccess
933  NULL, // ObjectAttributes
934  NULL, // MaximumSize
935  PAGE_READWRITE, // SectionPageProtection
936  SEC_IMAGE, // AllocationAttributes
937  FileHandle);
939  if (!NT_SUCCESS(Status))
940  {
941  skip("Failed to create image section\n");
942  NtClose(DataSectionHandle);
944  return;
945  }
946 
947  /* Map the image section again */
948  ImageBase = NULL;
949  ViewSize = 0;
950  Status = NtMapViewOfSection(ImageSectionHandle,
952  &ImageBase,
953  0,
954  0,
955  NULL,
956  &ViewSize,
957  ViewShare,
958  0,
959  PAGE_READONLY);
960 #ifdef _M_IX86
962 #else
964 #endif
965  if (!NT_SUCCESS(Status))
966  {
967  skip("Failed to map view of image section\n");
968  NtClose(ImageSectionHandle);
969  NtClose(DataSectionHandle);
971  return;
972  }
973 
974  // This one doesn't always work, needs investigation
975  /* Check the .data section again */
976  //ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada,
977  // "Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
978 
979  /* Restore the original data */
980  *(ULONG*)((PUCHAR)DataBase + 0x800) = 0x12345678;
981 
982  /* Close the data mapping */
984 
985  NtClose(DataSectionHandle);
986 
987  /* Try to allocate memory inside the image mapping */
988  DataBase = (PUCHAR)ImageBase + 0x20000;
989  ViewSize = 0x1000;
992 
993  /* Cleanup */
995  NtClose(ImageSectionHandle);
997 }
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:3840
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:3433
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:3614
NTSTATUS NTAPI NtFlushVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToFlush, OUT PIO_STATUS_BLOCK IoStatusBlock)
Definition: virtual.c:4024
#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:3398
#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:4481
#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 1000 of file NtMapViewOfSection.c.

1001 {
1003  NTSTATUS Status;
1006  HANDLE FileHandle, ImageSectionHandle;
1007  PVOID ImageBase, BaseAddress;
1008  SIZE_T ViewSize;
1010 
1011  if (!RtlDosPathNameToNtPathName_U(L"testdata\\nvoglv32.dll",
1012  &FileName,
1013  NULL,
1014  NULL))
1015  {
1016  ok(0, "RtlDosPathNameToNtPathName_U failed\n");
1017  return;
1018  }
1019 
1021  &FileName,
1022  0,
1023  NULL,
1024  NULL);
1025 
1029  &IoStatusBlock,
1033  printf("Opened file with handle %p\n", FileHandle);
1034 
1035  /* Create a data section with write access */
1036  MaximumSize.QuadPart = 0x20000;
1037  Status = NtCreateSection(&ImageSectionHandle,
1038  SECTION_ALL_ACCESS, // DesiredAccess
1039  NULL, // ObjectAttributes
1040  &MaximumSize, // MaximumSize
1041  PAGE_READWRITE, // SectionPageProtection
1042  SEC_IMAGE, // AllocationAttributes
1043  FileHandle);
1045 
1046  printf("Created image section with handle %p\n", ImageSectionHandle);
1047  //system("PAUSE");
1048 
1049  /* Map the image section */
1050  ImageBase = NULL;
1051  ViewSize = 0x0000;
1052  SectionOffset.QuadPart = 0x00000;
1053  Status = NtMapViewOfSection(ImageSectionHandle,
1054  NtCurrentProcess(),
1055  &ImageBase,
1056  0,
1057  0,
1058  &SectionOffset,
1059  &ViewSize,
1060  ViewShare,
1061  0,
1062  PAGE_READWRITE);
1064 
1065  printf("Mapped image section at %p, value in text section: %lx\n",
1066  ImageBase, *((ULONG*)((PCHAR)ImageBase + 0x1196)));
1067  system("PAUSE");
1068 
1069  /* Try to allocate a page after the section */
1070  BaseAddress = (PUCHAR)ImageBase + 0x10000;
1071  ViewSize = 0x1000;
1073  &BaseAddress,
1074  0,
1075  &ViewSize,
1077  PAGE_READWRITE);
1078  printf("allocation status: %lx\n", Status);
1079  system("PAUSE");
1080 
1081 }
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:3433
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:3614
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:4481
#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 14 of file NtMapViewOfSection.c.

15 {
17  HANDLE SectionHandle;
19  PVOID BaseAddress, BaseAddress2;
21  ULONG OldProtect;
22 
23  /* Create a page file backed section with SEC_COMMIT */
24  MaximumSize.QuadPart = 0x20000;
25  Status = NtCreateSection(&SectionHandle,
27  NULL,
28  &MaximumSize,
30  SEC_COMMIT,
31  NULL);
33  if (!NT_SUCCESS(Status))
34  return;
35 
36  /* Try to map a page at an address that is not 64k aligned */
37  BaseAddress = (PVOID)0x30001000;
38  SectionOffset.QuadPart = 0;
39  ViewSize = 0x1000;
40  Status = NtMapViewOfSection(SectionHandle,
42  &BaseAddress,
43  0,
44  0,
46  &ViewSize,
47  ViewShare,
48  0,
51 
52  /* Try to map a page with execute rights */
53  BaseAddress = (PVOID)0x30000000;
54  SectionOffset.QuadPart = 0;
55  ViewSize = 0x1000;
56  Status = NtMapViewOfSection(SectionHandle,
58  &BaseAddress,
59  0,
60  0,
62  &ViewSize,
63  ViewShare,
64  0,
67 
68  /* Try to map 2 pages with MEM_COMMIT */
69  BaseAddress = (PVOID)0x30000000;
70  SectionOffset.QuadPart = 0;
71  ViewSize = 0x2000;
72  Status = NtMapViewOfSection(SectionHandle,
74  &BaseAddress,
75  0,
76  PAGE_SIZE,
78  &ViewSize,
79  ViewShare,
80  MEM_COMMIT,
83 
84  /* Try to map 1 page, with free base address and zero bits compatible with 64k granularity */
85  BaseAddress = NULL;
86  SectionOffset.QuadPart = 0;
87  ViewSize = 0x1000;
88  Status = NtMapViewOfSection(SectionHandle,
90  &BaseAddress,
91  10,
92  0,
94  &ViewSize,
95  ViewShare,
96  0,
101 
102 {
103  ULONG_PTR gran = 64 * 1024;
104  ULONG_PTR ZeroBits = 11;
105 
106  ok_hex(gran, 0x10000);
107  gran <<= ZeroBits;
108  ok_hex(gran, 0x8000000);
109  gran >>= ZeroBits;
110  ok_hex(gran, 0x10000);
111 
112  ok_hex((gran << ZeroBits) >> ZeroBits, gran);
113 
114 }
115 
116  /* Try to map 1 page, with free base address and zero bits incompatible with 64k granularity */
117  BaseAddress = NULL;
118  SectionOffset.QuadPart = 0;
119  ViewSize = 0x1000;
120  Status = NtMapViewOfSection(SectionHandle,
122  &BaseAddress,
123  11,
124  0,
125  &SectionOffset,
126  &ViewSize,
127  ViewShare,
128  0,
131 
132  /* Try to map 1 page, with base address and zero bits being compatible */
133  BaseAddress = (PVOID)0x30000000;
134  SectionOffset.QuadPart = 0;
135  ViewSize = 0x1000;
136  Status = NtMapViewOfSection(SectionHandle,
138  &BaseAddress,
139  2,
140  0,
141  &SectionOffset,
142  &ViewSize,
143  ViewShare,
144  0,
149 
150  /* Try to map 1 page, with base address and zero bits being incompatible */
151  BaseAddress = (PVOID)0x30000000;
152  SectionOffset.QuadPart = 0;
153  ViewSize = 0x1000;
154  Status = NtMapViewOfSection(SectionHandle,
156  &BaseAddress,
157  3,
158  0,
159  &SectionOffset,
160  &ViewSize,
161  ViewShare,
162  0,
165 
166  /* Map 2 pages, without MEM_COMMIT */
167  BaseAddress = (PVOID)0x30000000;
168  SectionOffset.QuadPart = 0;
169  ViewSize = 0x2000;
170  Status = NtMapViewOfSection(SectionHandle,
172  &BaseAddress,
173  0,
174  0,
175  &SectionOffset,
176  &ViewSize,
177  ViewShare,
178  0,
181 
182  /* We must be able to access the memory */
183  _SEH2_TRY
184  {
185  *(PULONG)BaseAddress = 1;
186  }
187  _SEH2_EXCEPT(1)
188  {
189  ok(FALSE, "Got an exception\n");
190  }
191  _SEH2_END;
192 
193  /* Commit a page in the section */
194  BaseAddress = (PVOID)0x30000000;
195  ViewSize = 0x1000;
197  &BaseAddress,
198  0,
199  &ViewSize,
200  MEM_COMMIT,
203 
204  /* Try to decommit a page in the section */
206  &BaseAddress,
207  &ViewSize,
208  MEM_DECOMMIT);
210 
211  /* Try to commit a range larger than the section */
212  BaseAddress = (PVOID)0x30000000;
213  ViewSize = 0x3000;
215  &BaseAddress,
216  0,
217  &ViewSize,
218  MEM_COMMIT,
221 
222  /* Try to commit a page after the section */
223  BaseAddress = (PVOID)0x30002000;
224  ViewSize = 0x1000;
226  &BaseAddress,
227  0,
228  &ViewSize,
229  MEM_COMMIT,
232 
233  /* Try to allocate a page after the section */
234  BaseAddress = (PVOID)0x30002000;
235  ViewSize = 0x1000;
237  &BaseAddress,
238  0,
239  &ViewSize,
243 
244  /* Need to go to next 64k boundary */
245  BaseAddress = (PVOID)0x30010000;
246  ViewSize = 0x1000;
248  &BaseAddress,
249  0,
250  &ViewSize,
254  if (!NT_SUCCESS(Status))
255  return;
256 
257  /* Free the allocation */
258  BaseAddress = (PVOID)0x30010000;
259  ViewSize = 0x1000;
261  &BaseAddress,
262  &ViewSize,
263  MEM_RELEASE);
264  ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed with Status %lx\n", Status);
265 
266  /* Try to release the section mapping with NtFreeVirtualMemory */
267  BaseAddress = (PVOID)0x30000000;
268  ViewSize = 0x1000;
270  &BaseAddress,
271  &ViewSize,
272  MEM_RELEASE);
274 
275  /* Commit a page in the section */
276  BaseAddress = (PVOID)0x30001000;
277  ViewSize = 0x1000;
279  &BaseAddress,
280  0,
281  &ViewSize,
282  MEM_COMMIT,
285 
286  /* Try to decommit the page */
287  BaseAddress = (PVOID)0x30001000;
288  ViewSize = 0x1000;
290  &BaseAddress,
291  &ViewSize,
292  MEM_DECOMMIT);
294 
295  BaseAddress = UlongToPtr(0x40000000);
296  SectionOffset.QuadPart = 0;
297  ViewSize = 0x1000;
298  Status = NtMapViewOfSection(SectionHandle,
300  &BaseAddress,
301  0,
302  0,
303  &SectionOffset,
304  &ViewSize,
305  ViewShare,
306  0,
309  if (!NT_SUCCESS(Status))
310  return;
311 
312  ok(BaseAddress == UlongToPtr(0x40000000), "Invalid BaseAddress: %p\n", BaseAddress);
313 
314  BaseAddress = (PVOID)0x40080000;
315  SectionOffset.QuadPart = 0x10000;
316  ViewSize = 0x1000;
317  Status = NtMapViewOfSection(SectionHandle,
319  &BaseAddress,
320  0,
321  0,
322  &SectionOffset,
323  &ViewSize,
324  ViewShare,
325  0,
328 
329  ok(BaseAddress == (PVOID)0x40080000, "Invalid BaseAddress: %p\n", BaseAddress);
330 
331  /* Commit a page in the section */
332  BaseAddress = (PVOID)0x40000000;
334  &BaseAddress,
335  0,
336  &ViewSize,
337  MEM_COMMIT,
340 
341  /* Close the mapping */
344  BaseAddress = (PVOID)0x30000000;
347  Status = NtClose(SectionHandle);
349 
350  /* Create a page file backed section, but only reserved */
351  MaximumSize.QuadPart = 0x20000;
352  Status = NtCreateSection(&SectionHandle,
354  NULL,
355  &MaximumSize,
357  SEC_RESERVE,
358  NULL);
360 
361  /* Try to map 1 page, passing MEM_RESERVE */
362  BaseAddress = NULL;
363  SectionOffset.QuadPart = 0;
365  Status = NtMapViewOfSection(SectionHandle,
367  &BaseAddress,
368  0,
369  PAGE_SIZE,
370  &SectionOffset,
371  &ViewSize,
372  ViewShare,
373  MEM_RESERVE,
376 
377  /* Try to map 1 page using MEM_COMMIT */
378  BaseAddress = NULL;
379  SectionOffset.QuadPart = 0;
381  Status = NtMapViewOfSection(SectionHandle,
383  &BaseAddress,
384  0,
385  PAGE_SIZE,
386  &SectionOffset,
387  &ViewSize,
388  ViewShare,
389  MEM_COMMIT,
392 
393  /* Map 2 pages, but commit 1 */
394  BaseAddress = NULL;
395  SectionOffset.QuadPart = 0;
396  ViewSize = 2 * PAGE_SIZE;
397  Status = NtMapViewOfSection(SectionHandle,
399  &BaseAddress,
400  0,
401  PAGE_SIZE,
402  &SectionOffset,
403  &ViewSize,
404  ViewShare,
405  0,
408 
409  /* We must be able to access the 1st page */
411  _SEH2_TRY
412  {
413  *(PUCHAR)BaseAddress = 1;
414  }
415  _SEH2_EXCEPT(1)
416  {
418  }
419  _SEH2_END;
421 
422  /* We must not be able to access the 2nd page */
424  _SEH2_TRY
425  {
426  *((PUCHAR)BaseAddress + PAGE_SIZE) = 1;
427  }
428  _SEH2_EXCEPT(1)
429  {
431  }
432  _SEH2_END;
434 
435  /* Map the 2 pages again into a different memory location */
436  BaseAddress2 = NULL;
437  Status = NtMapViewOfSection(SectionHandle,
439  &BaseAddress2,
440  0,
441  0,
442  &SectionOffset,
443  &ViewSize,
444  ViewShare,
445  0,
448 
449  /* Commit a the 2nd page in the 2nd memory location */
450  BaseAddress2 = (PUCHAR)BaseAddress2 + PAGE_SIZE;
453  &BaseAddress2,
454  0,
455  &ViewSize,
456  MEM_COMMIT,
457  PAGE_READONLY);
459 
460  /* Try to commit again (the already committed page) */
462  &BaseAddress2,
463  0,
464  &ViewSize,
465  MEM_COMMIT,
466  PAGE_READONLY);
468 
469  /* We must be able to access the memory in the 2nd page of the 1st memory location */
471  _SEH2_TRY
472  {
473  *((PUCHAR)BaseAddress + PAGE_SIZE) = 2;
474  }
475  _SEH2_EXCEPT(1)
476  {
478  }
479  _SEH2_END;
481 
482  ok(*(PULONG)BaseAddress2 == 2, "Value in memory was wrong\n");
483 
484  /* Close the mapping */
489  Status = NtClose(SectionHandle);
491 
492  /* Try to create a 512 GB page file backed section with committed pages */
493  MaximumSize.QuadPart = 0x8000000000;
494  Status = NtCreateSection(&SectionHandle,
496  NULL,
497  &MaximumSize,
499  SEC_COMMIT,
500  NULL);
502 
503  /* Try to create a huge page file backed section with PAGE_NOACCESS protection */
504  MaximumSize.QuadPart = 0x8000000000;
505  Status = NtCreateSection(&SectionHandle,
507  NULL,
508  &MaximumSize,
510  SEC_COMMIT,
511  NULL);
513 
514  /* Try to create a very huge page file backed section, but only reserved */
515  MaximumSize.QuadPart = 0x80000000000;
516  Status = NtCreateSection(&SectionHandle,
518  NULL,
519  &MaximumSize,
521  SEC_RESERVE,
522  NULL);
523 #ifdef _WIN64
525 #else
526  /* WoW64 returns STATUS_INSUFFICIENT_RESOURCES */
528  "got wrong Status: 0x%lx\n", Status);
529 #endif
530 
531  /* Try to create a even huger page file backed section, but only reserved */
532  MaximumSize.QuadPart = 0x800000000000;
533  Status = NtCreateSection(&SectionHandle,
535  NULL,
536  &MaximumSize,
538  SEC_RESERVE,
539  NULL);
541 
542  /* Create a 8 GB page file backed section, but only reserved */
543  MaximumSize.QuadPart = 0x200000000;
544  Status = NtCreateSection(&SectionHandle,
546  NULL,
547  &MaximumSize,
549  SEC_RESERVE,
550  NULL);
552 
553  /* Pass a too large region size */
554  BaseAddress = NULL;
555  SectionOffset.QuadPart = 0;
557  Status = NtMapViewOfSection(SectionHandle,
559  &BaseAddress,
560  0,
561  0,
562  &SectionOffset,
563  &ViewSize,
564  ViewShare,
565  0,
567 #ifdef _WIN64
569 #else
570  /* WoW64 returns STATUS_INVALID_PARAMETER_4 */
572  "got wrong Status: 0x%lx\n", Status);
573 #endif
574 
575  /* Pass 0 region size */
576  BaseAddress = NULL;
577  SectionOffset.QuadPart = 0;
578  ViewSize = 0;
579  Status = NtMapViewOfSection(SectionHandle,
581  &BaseAddress,
582  0,
583  0,
584  &SectionOffset,
585  &ViewSize,
586  ViewShare,
587  0,
589 #ifdef _WIN64
591  ok(ViewSize == 0x200000000, "wrong ViewSize: 0x%Ix\n", ViewSize);
592 #else
593  /* WoW64 returns STATUS_NO_MEMORY */
595  "got wrong Status: 0x%lx\n", Status);
596  ok(ViewSize == 0, "wrong ViewSize: 0x%Ix\n", ViewSize);
597 #endif
598 
599  /* Map with PAGE_NOACCESS */
600  BaseAddress = NULL;
601  SectionOffset.QuadPart = 0;
602  ViewSize = 0x20000000;
603  Status = NtMapViewOfSection(SectionHandle,
605  &BaseAddress,
606  0,
607  0,
608  &SectionOffset,
609  &ViewSize,
610  ViewShare,
611  0,
612  PAGE_NOACCESS);
614 
615  /* Try to change protection to read/write */
616  ViewSize = 0x1000;
617  OldProtect = -1;
618  BaseAddress2 = BaseAddress;
619  Status = NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2, &ViewSize, PAGE_READWRITE, &OldProtect);
621  // Windows 2003 returns bogus
622  //ok(OldProtect == PAGE_READWRITE, "Wrong protection returned: %u\n", OldProtect);
623 
624  /* Test read access */
626  _SEH2_TRY
627  {
628  (void)(*(volatile char*)BaseAddress2);
629  }
631  {
633  }
634  _SEH2_END;
636 
637  /* Try to change protection to read/write */
638  ViewSize = 0x1000;
639  OldProtect = -1;
640  BaseAddress2 = BaseAddress;
641  Status = NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2, &ViewSize, PAGE_READWRITE, &OldProtect);
643 #ifdef _WIN64
644  ok(OldProtect == 0, "Wrong protection returned: 0x%lx\n", OldProtect);
645 #else
646  // Windows 2003 returns bogus
647 #endif
648 
649  /* Try to change protection to readonly */
650  ViewSize = 0x1000;
651  OldProtect = -1;
652  BaseAddress2 = BaseAddress;
653  Status = NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2, &ViewSize, PAGE_READONLY, &OldProtect);
655 #ifdef _WIN64
656  //ok(OldProtect == 0, "Wrong protection returned: 0x%lx\n", OldProtect);
657 #else
658  // Windows 2003 returns bogus
659 #endif
660 
661  /* Commit a page */
662  ViewSize = 0x1000;
663  BaseAddress2 = BaseAddress;
666  ok(BaseAddress2 == BaseAddress, "Invalid base address: %p\n", BaseAddress2);
667 
668  /* Commit the page again */
669  ViewSize = 0x1000;
670  BaseAddress2 = BaseAddress;
673  ok(BaseAddress2 == BaseAddress, "Invalid base address: %p\n", BaseAddress2);
674 
675  /* Test read access */
677  _SEH2_TRY
678  {
679  (void)(*(volatile char*)BaseAddress2);
680  }
682  {
684  }
685  _SEH2_END;
687 
688  /* Test write access */
690  _SEH2_TRY
691  {
692  *(char*)BaseAddress2 = 1;
693  }
695  {
697  }
698  _SEH2_END;
700 
701  /* Update protection to PAGE_READWRITE */
702  ViewSize = 0x1000;
703  BaseAddress2 = BaseAddress;
706 
707  /* Test write access */
709  _SEH2_TRY
710  {
711  *(char*)BaseAddress2 = 1;
712  }
714  {
716  }
717  _SEH2_END;
719 
720  /* Update protection to PAGE_EXECUTE_READWRITE (1 page) */
721  ViewSize = 0x1000;
722  BaseAddress2 = BaseAddress;
725 
728  Status = NtClose(SectionHandle);
730 }
_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:3840
#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:3433
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:3100
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:3614
#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 EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define STATUS_SECTION_PROTECTION
Definition: ntstatus.h:314
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3398
#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:4481
#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:40
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define STATUS_SUCCESS
Definition: shellext.h:65
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#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:5204

Referenced by START_TEST().

◆ Test_RawSize()

static void Test_RawSize ( ULONG  TestNumber)
static

Definition at line 1700 of file NtMapViewOfSection.c.

1701 {
1702  NTSTATUS Status;
1703  WCHAR TempPath[MAX_PATH];
1705  HANDLE Handle;
1706  HANDLE SectionHandle;
1709  SIZE_T ViewSize;
1710  ULONG Written;
1711  ULONG Length;
1712  BOOL Success;
1713 
1714  Length = GetTempPathW(MAX_PATH, TempPath);
1715  ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1716  Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1717  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1720  0,
1721  NULL,
1722  CREATE_ALWAYS,
1723  0,
1724  NULL);
1726  {
1727  skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1728  return;
1729  }
1730  RawSizeImageFile.nthdrs.OptionalHeader.ImageBase = 0xe400000;
1731  if (TestNumber == 1)
1732  {
1733  /* Just for fun, show that these flags don't matter. */
1738  }
1739  else if (TestNumber == 2)
1740  {
1741  /* SizeOfRawData is too large and will overflow.
1742  * This should cause failure to load the file */
1743  RawSizeImageFile.rsrc_header.SizeOfRawData = (ULONG)-0x200;
1744  }
1745 
1748  sizeof(RawSizeImageFile),
1749  &Written,
1750  NULL);
1751  ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1752  ok(Written == sizeof(RawSizeImageFile), "WriteFile wrote %lu bytes\n", Written);
1753 
1754  Status = NtCreateSection(&SectionHandle,
1756  NULL,
1757  NULL,
1759  SEC_IMAGE,
1760  Handle);
1761  if (TestNumber == 2)
1762  {
1763  /* overflow in SizeOfRawData */
1765  }
1766  else
1767  {
1769  }
1770 
1771  if (NT_SUCCESS(Status))
1772  {
1773  /* Map the section with */
1774  BaseAddress = NULL;
1775  SectionOffset.QuadPart = 0;
1776  ViewSize = 0;
1777  Status = NtMapViewOfSection(SectionHandle,
1778  NtCurrentProcess(),
1779  &BaseAddress,
1780  0,
1781  0,
1782  &SectionOffset,
1783  &ViewSize,
1784  ViewShare,
1785  0,
1786  PAGE_READWRITE);
1788  if (NT_SUCCESS(Status))
1789  {
1792 
1793  /* .text section header is unmodified */
1794  ok_hex(ImageFile->text_header.Misc.VirtualSize, RawSizeImageFile.text_header.Misc.VirtualSize);
1795  ok_hex(ImageFile->text_header.VirtualAddress, RawSizeImageFile.text_header.VirtualAddress);
1796  ok_hex(ImageFile->text_header.SizeOfRawData, RawSizeImageFile.text_header.SizeOfRawData);
1797  ok_hex(ImageFile->text_header.PointerToRawData, RawSizeImageFile.text_header.PointerToRawData);
1798 
1799  /* SizeOfRawData = 0 resets PointerToRawData to 0 */
1800  ok_hex(ImageFile->zdata_header.Misc.VirtualSize, RawSizeImageFile.zdata_header.Misc.VirtualSize);
1801  ok_hex(ImageFile->zdata_header.VirtualAddress, RawSizeImageFile.zdata_header.VirtualAddress);
1802  ok_hex(ImageFile->zdata_header.SizeOfRawData, RawSizeImageFile.zdata_header.SizeOfRawData);
1803  ok_hex(ImageFile->zdata_header.PointerToRawData, 0);
1804 
1805 #define TEST_BYTE(n, v) \
1806  StartSeh() \
1807  ok(Bytes[n] == v, "[%lu] Bytes[%u] = 0x%x, expected 0x%x\n", \
1808  TestNumber, n, Bytes[n], v); \
1809  EndSeh(STATUS_SUCCESS);
1810  /* .text section data matches file up to 0x1000 */
1811  TEST_BYTE(0x1000, 0xc3);
1812  TEST_BYTE(0x1001, 0x00);
1813  TEST_BYTE(0x1008, 0x01);
1814  TEST_BYTE(0x1010, 0x02);
1815  TEST_BYTE(0x1fff, 0x0a);
1816 
1817  /* .data section data matches file up to 0x1000 */
1818  TEST_BYTE(0x2000, 0x01);
1819  TEST_BYTE(0x2001, 0x23);
1820  TEST_BYTE(0x20ff, 0xee);
1821  TEST_BYTE(0x2100, 0xff);
1822  TEST_BYTE(0x2fff, 0xbb);
1823 
1824  /* .zdata section data is all zeroes */
1825  TEST_BYTE(0x3000, 0x00);
1826  TEST_BYTE(0x3001, 0x00);
1827  TEST_BYTE(0x3800, 0x00);
1828  TEST_BYTE(0x3fff, 0x00);
1829 
1830  /* .rsrc section data matches file up to VirtualSize 0x200 */
1831  TEST_BYTE(0x4000, 0xff);
1832  TEST_BYTE(0x4100, 0xee);
1833  TEST_BYTE(0x4180, 0xdd);
1834  TEST_BYTE(0x41c0, 0xcc);
1835  TEST_BYTE(0x41e0, 0xbb);
1836  TEST_BYTE(0x41f0, 0xaa);
1837  TEST_BYTE(0x41fe, 0x23);
1838  TEST_BYTE(0x41ff, 0x01);
1839  TEST_BYTE(0x4200, 0x00);
1840  TEST_BYTE(0x4fff, 0x00);
1841 #undef TEST_BYTE
1844  }
1845  Status = NtClose(SectionHandle);
1847  }
1848 
1851 }
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:3840
#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:3433
#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:1040
#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:3614
_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:3398
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 1370 of file NtMapViewOfSection.c.

1371 {
1372  NTSTATUS Status;
1373  WCHAR TempPath[MAX_PATH];
1375  HANDLE Handle;
1376  HANDLE SectionHandle;
1379  SIZE_T ViewSize;
1380  ULONG Written;
1381  ULONG Length;
1382  BOOL Success;
1383 
1384  Length = GetTempPathW(MAX_PATH, TempPath);
1385  ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1386  Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1387  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1390  0,
1391  NULL,
1392  CREATE_ALWAYS,
1393  0,
1394  NULL);
1396  {
1397  skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1398  return;
1399  }
1400  if (Relocate)
1401  {
1402  ok((ULONG_PTR)GetModuleHandle(NULL) <= 0x80000000, "Module at %p\n", GetModuleHandle(NULL));
1403  SectionContentsImageFile.nthdrs.OptionalHeader.ImageBase = (ULONG)(ULONG_PTR)GetModuleHandle(NULL);
1404  }
1405  else
1406  {
1407  SectionContentsImageFile.nthdrs.OptionalHeader.ImageBase = 0xe400000;
1408  }
1409 
1412  sizeof(SectionContentsImageFile),
1413  &Written,
1414  NULL);
1415  ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1416  ok(Written == sizeof(SectionContentsImageFile), "WriteFile wrote %lu bytes\n", Written);
1417 
1418  Status = NtCreateSection(&SectionHandle,
1420  NULL,
1421  NULL,
1423  SEC_IMAGE,
1424  Handle);
1426 
1427  if (NT_SUCCESS(Status))
1428  {
1429  /* Map the section with */
1430  BaseAddress = NULL;
1431  SectionOffset.QuadPart = 0;
1432  ViewSize = 0;
1433  Status = NtMapViewOfSection(SectionHandle,
1434  NtCurrentProcess(),
1435  &BaseAddress,
1436  0,
1437  0,
1438  &SectionOffset,
1439  &ViewSize,
1440  ViewShare,
1441  0,
1442  PAGE_READWRITE);
1443  if (Relocate)
1445  else
1447  if (NT_SUCCESS(Status))
1448  {
1450 #define TEST_BYTE(n, v) StartSeh() ok_hex(Bytes[n], v); EndSeh(STATUS_SUCCESS);
1451 #define TEST_WRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_SUCCESS);
1452 #define TEST_NOWRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_ACCESS_VIOLATION);
1453  TEST_NOWRITE(0x2000);
1454  TEST_BYTE(0x2000, 0xc3);
1455  TEST_BYTE(0x2001, 0x00);
1456  TEST_NOWRITE(0x4000);
1457  TEST_BYTE(0x4000, 0x01);
1458  TEST_BYTE(0x4001, 0x23);
1459  TEST_BYTE(0x4007, 0xef);
1460  TEST_BYTE(0x4008, 0xaa);
1461  TEST_BYTE(0x4010, 0xbb);
1462  TEST_BYTE(0x4020, 0xcc);
1463  TEST_BYTE(0x4040, 0xdd);
1464  TEST_BYTE(0x4080, 0xee);
1465  TEST_BYTE(0x40c0, 0xff);
1466  TEST_BYTE(0x40ff, 0xff);
1467  TEST_BYTE(0x4100, 0x00);
1468  TEST_BYTE(0x41ff, 0x00);
1469  TEST_NOWRITE(0x6000);
1470  TEST_BYTE(0x6000, 0xee);
1471  TEST_BYTE(0x60ff, 0x55);
1472  TEST_BYTE(0x6100, 0xee);
1473  TEST_BYTE(0x63ff, 0xdd);
1474  TEST_BYTE(0x6400, 0x00);
1475  TEST_WRITE(0x8000);
1476  TEST_BYTE(0x8000, 0x11);
1477  TEST_BYTE(0x8400, 0x33);
1478 #undef TEST_BYTE
1479 #undef TEST_WRITE
1480 #undef TEST_NOWRITE
1483  }
1484  Status = NtClose(SectionHandle);
1486  }
1487 
1490 }
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:3840
#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:3433
#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:1040
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:3614
_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:3398
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:3683
#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 1902 of file NtMapViewOfSection.c.

1903 {
1904  WCHAR TempPath[MAX_PATH];
1906  NTSTATUS Status;
1907  SIZE_T ViewSize = 0;
1908  HANDLE Handle;
1909  HANDLE SectionHandle;
1910 
1911  SIZE_T Length;
1912  BOOL Success;
1913  DWORD Written, Error;
1914  VOID* BaseAddress;
1915 
1916  Length = GetTempPathW(MAX_PATH, TempPath);
1917  ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1918  Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1919  ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1921 
1922  Success = WriteFile(Handle, "TESTDATA", 8, &Written, NULL);
1923  ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1924  ok(Written == 8, "WriteFile wrote %lu bytes\n", Written);
1925 
1926  Written = SetFilePointer(Handle, 6, NULL, FILE_BEGIN);
1927  ok(Written == 6, "SetFilePointer returned %lu bytes\n", Written);
1929  ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
1930 
1931  Status = NtCreateSection(&SectionHandle,
1933  0, 0, PAGE_READONLY, SEC_COMMIT, Handle);
1935  BaseAddress = NULL;
1936  ViewSize = 0;
1937  Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
1938  0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
1940 
1941  if (BaseAddress)
1942  {
1943  // First we test data that was truncated even before the file mapping was opened
1944  Length = strlen((char*)BaseAddress);
1945  ok(Length == 6, "Old data was not properly erased! (Length=%lu)\n", Length);
1946  }
1947 
1948  // Now we truncate the file on disk some more
1949  Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
1950  ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
1952  Error = GetLastError();
1953  ok(Success == FALSE, "SetEndOfFile succeeded\n");
1954  ok(Error == ERROR_USER_MAPPED_FILE, "SetEndOfFile did not set error to ERROR_USER_MAPPED_FILE (%lu)\n", Error);
1955 
1956  if (BaseAddress)
1957  {
1958  Length = strlen((char*)BaseAddress);
1959  ok(Length == 6, "Length should not have changed! (Length=%lu)\n", Length);
1960  }
1961 
1962  // Unmap and set the end shorter.
1965  Success = CloseHandle(SectionHandle);
1966  ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
1967 
1968  Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
1969  ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
1971  ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
1972 
1973  Status = NtCreateSection(&SectionHandle,
1975  0, 0, PAGE_READONLY, SEC_COMMIT, Handle);
1977  BaseAddress = NULL;
1978  ViewSize = 0;
1979  Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
1980  0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
1982 
1983  // CLEANUP
1986  Success = CloseHandle(SectionHandle);
1987  ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
1989  ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
1990 
1992  ok(Success == TRUE, "DeleteFileW failed with %lu\n", GetLastError());
1993 }
#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:3840
#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:3433
#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:1040
#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:3614
#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().