ReactOS 0.4.16-dev-92-g0c2cdca
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);
#define StartSeh()
Definition: _sntprintf.h:16
const GLdouble * v
Definition: gl.h:2040
GLdouble n
Definition: glext.h:7729
_In_ UINT Bytes
Definition: mmcopy.h:9
#define STATUS_SUCCESS
Definition: shellext.h:65

◆ TEST_NOWRITE

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

◆ TEST_WRITE

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

Function Documentation

◆ C_ASSERT() [1/7]

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

◆ C_ASSERT() [2/7]

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

◆ C_ASSERT() [3/7]

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

◆ C_ASSERT() [4/7]

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

◆ C_ASSERT() [5/7]

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

◆ C_ASSERT() [6/7]

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

◆ C_ASSERT() [7/7]

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

◆ START_TEST()

START_TEST ( NtMapViewOfSection  )

Definition at line 2015 of file NtMapViewOfSection.c.

2016{
2022 Test_RawSize(0);
2023 Test_RawSize(1);
2024 Test_RawSize(2);
2026 Test_Truncate();
2027}
static void Test_SectionContents(BOOL Relocate)
void Test_PageFileSection(void)
void Test_ImageSection(void)
static void Test_EmptyFile(VOID)
void Test_BasedSection(void)
static void Test_RawSize(ULONG TestNumber)
static void Test_Truncate(VOID)
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117

◆ Test_BasedSection()

void Test_BasedSection ( void  )

Definition at line 1085 of file NtMapViewOfSection.c.

1086{
1088 HANDLE SectionHandle1, SectionHandle2;
1090 PVOID BaseAddress1, BaseAddress2;
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,
1110 &BaseAddress1,
1111 0,
1112 0,
1114 &ViewSize,
1115 ViewShare,
1116 0,
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,
1141 &BaseAddress2,
1142 0,
1143 0,
1145 &ViewSize,
1146 ViewShare,
1147 0,
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:3074
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:3255
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
#define ok(value,...)
Definition: atltest.h:57
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
Status
Definition: gdiplustypes.h:25
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:362
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:407
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:408
#define SEC_COMMIT
Definition: mmtypes.h:100
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define NtCurrentProcess()
Definition: nt_native.h:1657
@ ViewShare
Definition: nt_native.h:1278
#define SEC_BASED
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:261
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by START_TEST().

◆ Test_EmptyFile()

static void Test_EmptyFile ( VOID  )
static

Definition at line 1874 of file NtMapViewOfSection.c.

1875{
1877 WCHAR TempPath[MAX_PATH];
1879 HANDLE Handle;
1880 HANDLE SectionHandle;
1881 ULONG Length;
1882
1883 Length = GetTempPathW(MAX_PATH, TempPath);
1884 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1885 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1886 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1889 0,
1890 NULL,
1892 0,
1893 NULL);
1895 {
1896 skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1897 return;
1898 }
1899
1900 Status = NtCreateSection(&SectionHandle,
1904
1905 if (NT_SUCCESS(Status))
1906 NtClose(SectionHandle);
1907
1908 Status = NtCreateSection(&SectionHandle,
1912
1913 if (NT_SUCCESS(Status))
1914 NtClose(SectionHandle);
1915
1918}
#define skip(...)
Definition: atltest.h:64
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define CloseHandle
Definition: compat.h:739
#define PAGE_READONLY
Definition: compat.h:138
#define SECTION_MAP_READ
Definition: compat.h:139
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
#define CreateFileW
Definition: compat.h:741
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
DWORD WINAPI GetTempPathW(IN DWORD count, OUT LPWSTR path)
Definition: path.c:2080
UINT WINAPI GetTempFileNameW(IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, IN UINT uUnique, OUT LPWSTR lpTempFileName)
Definition: filename.c:84
ULONG Handle
Definition: gdb_input.c:15
#define CREATE_ALWAYS
Definition: disk.h:72
#define SEC_IMAGE
Definition: mmtypes.h:97
#define SECTION_QUERY
Definition: nt_native.h:1287
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define FILE_ALL_ACCESS
Definition: nt_native.h:651
#define STANDARD_RIGHTS_REQUIRED
Definition: nt_native.h:63
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define STATUS_INVALID_FILE_FOR_SECTION
Definition: ntstatus.h:269
#define STATUS_MAPPED_FILE_SIZE_ZERO
Definition: ntstatus.h:522
#define L(x)
Definition: ntvdm.h:50
uint32_t ULONG
Definition: typedefs.h:59
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by START_TEST().

◆ Test_ImageSection()

void Test_ImageSection ( void  )

Definition at line 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
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
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,
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,
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}
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3481
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define ok_hex(expression, result)
Definition: atltest.h:94
const char * wine_dbgstr_wn(const WCHAR *str, int n)
Definition: compat.c:367
#define wcsrchr
Definition: compat.h:16
#define GENERIC_READ
Definition: compat.h:135
#define FILE_SHARE_READ
Definition: compat.h:136
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
struct _FileName FileName
Definition: fatprocs.h:897
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
NTSYSAPI BOOLEAN NTAPI RtlDosPathNameToNtPathName_U(_In_opt_z_ PCWSTR DosPathName, _Out_ PUNICODE_STRING NtPathName, _Out_opt_ PCWSTR *NtFileNamePart, _Out_opt_ PRTL_RELATIVE_NAME_U DirectoryInfo)
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3952
#define SYNCHRONIZE
Definition: nt_native.h:61
#define MEM_RESERVE
Definition: nt_native.h:1314
#define GENERIC_WRITE
Definition: nt_native.h:90
#define PAGE_NOACCESS
Definition: nt_native.h:1302
#define UNICODE_NULL
NTSTATUS NTAPI NtFlushVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToFlush, OUT PIO_STATUS_BLOCK IoStatusBlock)
Definition: virtual.c:4035
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:4492
#define STATUS_IMAGE_MACHINE_TYPE_MISMATCH
Definition: ntstatus.h:128
#define STATUS_INVALID_IMAGE_NOT_MZ
Definition: ntstatus.h:539
STRSAFEAPI StringCbCatW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:342
unsigned char * PUCHAR
Definition: typedefs.h:53
_In_ PWDFDEVICE_INIT _In_ PWDF_FILEOBJECT_CONFIG _In_opt_ PWDF_OBJECT_ATTRIBUTES FileObjectAttributes
Definition: wdfdevice.h:3400

Referenced by START_TEST().

◆ Test_ImageSection2()

void Test_ImageSection2 ( void  )

Definition at line 1000 of file NtMapViewOfSection.c.

1001{
1006 HANDLE FileHandle, ImageSectionHandle;
1007 PVOID ImageBase, BaseAddress;
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
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,
1055 &ImageBase,
1056 0,
1057 0,
1059 &ViewSize,
1060 ViewShare,
1061 0,
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,
1078 printf("allocation status: %lx\n", Status);
1079 system("PAUSE");
1080
1081}
#define printf
Definition: freeldr.h:97
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define MEM_COMMIT
Definition: nt_native.h:1313
int __cdecl system(_In_opt_z_ const char *_Command)
char * PCHAR
Definition: typedefs.h:51

◆ 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,
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,
43 0,
44 0,
46 &ViewSize,
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,
59 0,
60 0,
62 &ViewSize,
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,
75 0,
78 &ViewSize,
83
84 /* Try to map 1 page, with free base address and zero bits compatible with 64k granularity */
86 SectionOffset.QuadPart = 0;
87 ViewSize = 0x1000;
88 Status = NtMapViewOfSection(SectionHandle,
91 10,
92 0,
94 &ViewSize,
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 */
118 SectionOffset.QuadPart = 0;
119 ViewSize = 0x1000;
120 Status = NtMapViewOfSection(SectionHandle,
123 11,
124 0,
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,
139 2,
140 0,
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,
157 3,
158 0,
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,
173 0,
174 0,
176 &ViewSize,
177 ViewShare,
178 0,
181
182 /* We must be able to access the memory */
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;
198 0,
199 &ViewSize,
203
204 /* Try to decommit a page in the section */
207 &ViewSize,
210
211 /* Try to commit a range larger than the section */
212 BaseAddress = (PVOID)0x30000000;
213 ViewSize = 0x3000;
216 0,
217 &ViewSize,
221
222 /* Try to commit a page after the section */
223 BaseAddress = (PVOID)0x30002000;
224 ViewSize = 0x1000;
227 0,
228 &ViewSize,
232
233 /* Try to allocate a page after the section */
234 BaseAddress = (PVOID)0x30002000;
235 ViewSize = 0x1000;
238 0,
239 &ViewSize,
243
244 /* Need to go to next 64k boundary */
245 BaseAddress = (PVOID)0x30010000;
246 ViewSize = 0x1000;
249 0,
250 &ViewSize,
254 if (!NT_SUCCESS(Status))
255 return;
256
257 /* Free the allocation */
258 BaseAddress = (PVOID)0x30010000;
259 ViewSize = 0x1000;
262 &ViewSize,
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;
271 &ViewSize,
274
275 /* Commit a page in the section */
276 BaseAddress = (PVOID)0x30001000;
277 ViewSize = 0x1000;
280 0,
281 &ViewSize,
285
286 /* Try to decommit the page */
287 BaseAddress = (PVOID)0x30001000;
288 ViewSize = 0x1000;
291 &ViewSize,
294
295 BaseAddress = UlongToPtr(0x40000000);
296 SectionOffset.QuadPart = 0;
297 ViewSize = 0x1000;
298 Status = NtMapViewOfSection(SectionHandle,
301 0,
302 0,
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,
320 0,
321 0,
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;
335 0,
336 &ViewSize,
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,
358 NULL);
360
361 /* Try to map 1 page, passing MEM_RESERVE */
363 SectionOffset.QuadPart = 0;
365 Status = NtMapViewOfSection(SectionHandle,
368 0,
369 PAGE_SIZE,
371 &ViewSize,
372 ViewShare,
376
377 /* Try to map 1 page using MEM_COMMIT */
379 SectionOffset.QuadPart = 0;
381 Status = NtMapViewOfSection(SectionHandle,
384 0,
385 PAGE_SIZE,
387 &ViewSize,
388 ViewShare,
392
393 /* Map 2 pages, but commit 1 */
395 SectionOffset.QuadPart = 0;
396 ViewSize = 2 * PAGE_SIZE;
397 Status = NtMapViewOfSection(SectionHandle,
400 0,
401 PAGE_SIZE,
403 &ViewSize,
404 ViewShare,
405 0,
408
409 /* We must be able to access the 1st page */
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 */
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,
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,
459
460 /* Try to commit again (the already committed page) */
462 &BaseAddress2,
463 0,
464 &ViewSize,
468
469 /* We must be able to access the memory in the 2nd page of the 1st memory location */
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,
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,
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,
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,
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,
550 NULL);
552
553 /* Pass a too large region size */
555 SectionOffset.QuadPart = 0;
557 Status = NtMapViewOfSection(SectionHandle,
560 0,
561 0,
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 */
577 SectionOffset.QuadPart = 0;
578 ViewSize = 0;
579 Status = NtMapViewOfSection(SectionHandle,
582 0,
583 0,
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 */
601 SectionOffset.QuadPart = 0;
602 ViewSize = 0x20000000;
603 Status = NtMapViewOfSection(SectionHandle,
606 0,
607 0,
609 &ViewSize,
610 ViewShare,
611 0,
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 */
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 */
678 {
679 (void)(*(volatile char*)BaseAddress2);
680 }
682 {
684 }
685 _SEH2_END;
687
688 /* Test write access */
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 */
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}
#define MAXULONG_PTR
Definition: basetsd.h:103
#define UlongToPtr(u)
Definition: config.h:106
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR ZeroBits
Definition: mmfuncs.h:405
#define MEM_DECOMMIT
Definition: nt_native.h:1315
#define SEC_RESERVE
Definition: nt_native.h:1323
#define MEM_RELEASE
Definition: nt_native.h:1316
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5230
NTSTATUS NTAPI NtProtectVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UnsafeBaseAddress, IN OUT SIZE_T *UnsafeNumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG UnsafeOldAccessProtection)
Definition: virtual.c:3111
#define STATUS_INVALID_VIEW_SIZE
Definition: ntstatus.h:268
#define STATUS_MAPPED_ALIGNMENT
Definition: ntstatus.h:676
#define STATUS_SECTION_PROTECTION
Definition: ntstatus.h:314
#define STATUS_UNABLE_TO_DELETE_SECTION
Definition: ntstatus.h:264
#define STATUS_INVALID_PARAMETER_9
Definition: ntstatus.h:483
#define STATUS_INVALID_PARAMETER_4
Definition: ntstatus.h:478
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:305
#define STATUS_SECTION_TOO_BIG
Definition: ntstatus.h:300
#define STATUS_INVALID_PARAMETER_3
Definition: ntstatus.h:477
#define STATUS_COMMITMENT_LIMIT
Definition: ntstatus.h:537
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:66
uint32_t * PULONG
Definition: typedefs.h:59
void * PVOID
Definition: typedefs.h:50
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158

Referenced by START_TEST().

◆ Test_RawSize()

static void Test_RawSize ( ULONG  TestNumber)
static

Definition at line 1716 of file NtMapViewOfSection.c.

1717{
1719 WCHAR TempPath[MAX_PATH];
1721 HANDLE Handle;
1722 HANDLE SectionHandle;
1726 ULONG Written;
1727 ULONG Length;
1728 BOOL Success;
1729
1730 Length = GetTempPathW(MAX_PATH, TempPath);
1731 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1732 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1733 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1736 0,
1737 NULL,
1739 0,
1740 NULL);
1742 {
1743 skip("Failed to create temp file %ls, error %lu\n", FileName, GetLastError());
1744 return;
1745 }
1746 RawSizeImageFile.nthdrs.OptionalHeader.ImageBase = 0xe400000;
1747 if (TestNumber == 1)
1748 {
1749 /* Just for fun, show that these flags don't matter. */
1754 }
1755 else if (TestNumber == 2)
1756 {
1757 /* SizeOfRawData is too large and will overflow.
1758 * This should cause failure to load the file */
1759 RawSizeImageFile.rsrc_header.SizeOfRawData = (ULONG)-0x200;
1760 }
1761
1764 sizeof(RawSizeImageFile),
1765 &Written,
1766 NULL);
1767 ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1768 ok(Written == sizeof(RawSizeImageFile), "WriteFile wrote %lu bytes\n", Written);
1769
1770 Status = NtCreateSection(&SectionHandle,
1772 NULL,
1773 NULL,
1775 SEC_IMAGE,
1776 Handle);
1777 if (TestNumber == 2)
1778 {
1779 /* overflow in SizeOfRawData */
1781 }
1782 else
1783 {
1785 }
1786
1787 if (NT_SUCCESS(Status))
1788 {
1789 /* Map the section with */
1790 BaseAddress = NULL;
1791 SectionOffset.QuadPart = 0;
1792 ViewSize = 0;
1793 Status = NtMapViewOfSection(SectionHandle,
1795 &BaseAddress,
1796 0,
1797 0,
1799 &ViewSize,
1800 ViewShare,
1801 0,
1804 if (NT_SUCCESS(Status))
1805 {
1808
1809 /* .text section header is unmodified */
1810 ok_hex(ImageFile->text_header.Misc.VirtualSize, RawSizeImageFile.text_header.Misc.VirtualSize);
1811 ok_hex(ImageFile->text_header.VirtualAddress, RawSizeImageFile.text_header.VirtualAddress);
1812 ok_hex(ImageFile->text_header.SizeOfRawData, RawSizeImageFile.text_header.SizeOfRawData);
1813 ok_hex(ImageFile->text_header.PointerToRawData, RawSizeImageFile.text_header.PointerToRawData);
1814
1815 /* SizeOfRawData = 0 resets PointerToRawData to 0 */
1816 ok_hex(ImageFile->zdata_header.Misc.VirtualSize, RawSizeImageFile.zdata_header.Misc.VirtualSize);
1817 ok_hex(ImageFile->zdata_header.VirtualAddress, RawSizeImageFile.zdata_header.VirtualAddress);
1818 ok_hex(ImageFile->zdata_header.SizeOfRawData, RawSizeImageFile.zdata_header.SizeOfRawData);
1819 ok_hex(ImageFile->zdata_header.PointerToRawData, 0);
1820
1821 /* .bss section is unmodified */
1822 ok_hex(ImageFile->bss_header.SizeOfRawData, 0x600);
1823 ok_hex(ImageFile->bss_header.PointerToRawData, 0);
1824
1825#define TEST_BYTE(n, v) \
1826 StartSeh() \
1827 ok(Bytes[n] == v, "[%lu] Bytes[%u] = 0x%x, expected 0x%x\n", \
1828 TestNumber, n, Bytes[n], v); \
1829 EndSeh(STATUS_SUCCESS);
1830 /* .text section data matches file up to 0x1000 */
1831 TEST_BYTE(0x1000, 0xc3);
1832 TEST_BYTE(0x1001, 0x00);
1833 TEST_BYTE(0x1008, 0x01);
1834 TEST_BYTE(0x1010, 0x02);
1835 TEST_BYTE(0x1fff, 0x0a);
1836
1837 /* .data section data matches file up to 0x1000 */
1838 TEST_BYTE(0x2000, 0x01);
1839 TEST_BYTE(0x2001, 0x23);
1840 TEST_BYTE(0x20ff, 0xee);
1841 TEST_BYTE(0x2100, 0xff);
1842 TEST_BYTE(0x2fff, 0xbb);
1843
1844 /* .zdata section data is all zeroes */
1845 TEST_BYTE(0x3000, 0x00);
1846 TEST_BYTE(0x3001, 0x00);
1847 TEST_BYTE(0x3800, 0x00);
1848 TEST_BYTE(0x3fff, 0x00);
1849
1850 /* .rsrc section data matches file up to VirtualSize 0x200 */
1851 TEST_BYTE(0x4000, 0xff);
1852 TEST_BYTE(0x4100, 0xee);
1853 TEST_BYTE(0x4180, 0xdd);
1854 TEST_BYTE(0x41c0, 0xcc);
1855 TEST_BYTE(0x41e0, 0xbb);
1856 TEST_BYTE(0x41f0, 0xaa);
1857 TEST_BYTE(0x41fe, 0x23);
1858 TEST_BYTE(0x41ff, 0x01);
1859 TEST_BYTE(0x4200, 0x00);
1860 TEST_BYTE(0x4fff, 0x00);
1861#undef TEST_BYTE
1864 }
1865 Status = NtClose(SectionHandle);
1867 }
1868
1871}
#define TEST_BYTE(n, v)
static struct _RAW_SIZE_IMAGE_FILE RawSizeImageFile
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
@ Success
Definition: eventcreate.c:712
unsigned int BOOL
Definition: ntddk_ex.h:94
static PWSTR ImageFile
Definition: imagefile.c:10
#define IMAGE_SCN_CNT_INITIALIZED_DATA
Definition: ntimage.h:231
#define IMAGE_SCN_CNT_CODE
Definition: ntimage.h:230
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA
Definition: ntimage.h:232
#define STATUS_INVALID_IMAGE_FORMAT
Definition: ntstatus.h:359

Referenced by START_TEST().

◆ Test_SectionContents()

static void Test_SectionContents ( BOOL  Relocate)
static

Definition at line 1370 of file NtMapViewOfSection.c.

1371{
1373 WCHAR TempPath[MAX_PATH];
1375 HANDLE Handle;
1376 HANDLE SectionHandle;
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,
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
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,
1435 &BaseAddress,
1436 0,
1437 0,
1439 &ViewSize,
1440 ViewShare,
1441 0,
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}
#define TEST_WRITE(n)
#define TEST_NOWRITE(n)
static struct _SECTION_CONTENTS_IMAGE_FILE SectionContentsImageFile
#define STATUS_IMAGE_NOT_AT_BASE
Definition: ntstatus.h:117
#define GetModuleHandle
Definition: winbase.h:3827

Referenced by START_TEST().

◆ Test_Truncate()

static void Test_Truncate ( VOID  )
static

Definition at line 1922 of file NtMapViewOfSection.c.

1923{
1924 WCHAR TempPath[MAX_PATH];
1927 SIZE_T ViewSize = 0;
1928 HANDLE Handle;
1929 HANDLE SectionHandle;
1930
1931 SIZE_T Length;
1932 BOOL Success;
1933 DWORD Written, Error;
1935
1936 Length = GetTempPathW(MAX_PATH, TempPath);
1937 ok(Length != 0, "GetTempPathW failed with %lu\n", GetLastError());
1938 Length = GetTempFileNameW(TempPath, L"nta", 0, FileName);
1939 ok(Length != 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1941
1942 Success = WriteFile(Handle, "TESTDATA", 8, &Written, NULL);
1943 ok(Success == TRUE, "WriteFile failed with %lu\n", GetLastError());
1944 ok(Written == 8, "WriteFile wrote %lu bytes\n", Written);
1945
1946 Written = SetFilePointer(Handle, 6, NULL, FILE_BEGIN);
1947 ok(Written == 6, "SetFilePointer returned %lu bytes\n", Written);
1949 ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
1950
1951 Status = NtCreateSection(&SectionHandle,
1955 BaseAddress = NULL;
1956 ViewSize = 0;
1957 Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
1958 0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
1960
1961 if (BaseAddress)
1962 {
1963 // First we test data that was truncated even before the file mapping was opened
1964 Length = strlen((char*)BaseAddress);
1965 ok(Length == 6, "Old data was not properly erased! (Length=%lu)\n", Length);
1966 }
1967
1968 // Now we truncate the file on disk some more
1969 Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
1970 ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
1972 Error = GetLastError();
1973 ok(Success == FALSE, "SetEndOfFile succeeded\n");
1974 ok(Error == ERROR_USER_MAPPED_FILE, "SetEndOfFile did not set error to ERROR_USER_MAPPED_FILE (%lu)\n", Error);
1975
1976 if (BaseAddress)
1977 {
1978 Length = strlen((char*)BaseAddress);
1979 ok(Length == 6, "Length should not have changed! (Length=%lu)\n", Length);
1980 }
1981
1982 // Unmap and set the end shorter.
1985 Success = CloseHandle(SectionHandle);
1986 ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
1987
1988 Written = SetFilePointer(Handle, 4, NULL, FILE_BEGIN);
1989 ok(Written == 4, "SetFilePointer returned %lu bytes\n", Written);
1991 ok(Success == TRUE, "SetEndOfFile failed with %lu\n", GetLastError());
1992
1993 Status = NtCreateSection(&SectionHandle,
1997 BaseAddress = NULL;
1998 ViewSize = 0;
1999 Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0,
2000 0, 0, &ViewSize, ViewShare, 0, PAGE_READONLY);
2002
2003 // CLEANUP
2006 Success = CloseHandle(SectionHandle);
2007 ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
2009 ok(Success == TRUE, "CloseHandle failed with %lu\n", GetLastError());
2010
2012 ok(Success == TRUE, "DeleteFileW failed with %lu\n", GetLastError());
2013}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
BOOL Error
Definition: chkdsk.c:66
#define FILE_BEGIN
Definition: compat.h:761
#define SetFilePointer
Definition: compat.h:743
BOOL WINAPI SetEndOfFile(HANDLE hFile)
Definition: fileinfo.c:1004
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERROR_USER_MAPPED_FILE
Definition: winerror.h:727

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