Skip to content

Commit

Permalink
Obtain pagesize from OS in PAL tests (#64540)
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 authored Jan 31, 2022
1 parent 396886e commit ca6716a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
55 changes: 29 additions & 26 deletions src/coreclr/pal/tests/palsuite/file_io/ReadFile/test2/ReadFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
** WriteFile
** GetLastError
**
**
**===================================================================*/


#include <palsuite.h>


#define szStringTest "The quick fox jumped over the lazy dog's back.\0"
#define szEmptyString ""
#define szReadableFile "Readable.txt"
Expand All @@ -29,7 +27,6 @@
//Previously number of tests was 6, now 4 refer VSW 312690
#define NOOFTESTS 4

const int PAGESIZE = 4096;
char *readBuffer_ReadFile_test2;

BOOL validateResults_ReadFile_test2(const char* szString, // string read
Expand Down Expand Up @@ -66,9 +63,9 @@ BOOL readTest_ReadFile_test2(DWORD dwByteCount, char cResult)
HANDLE hFile = NULL;
DWORD dwBytesRead;
BOOL bRc = FALSE;
// open the test file
hFile = CreateFile(szReadableFile,

// open the test file
hFile = CreateFile(szReadableFile,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
Expand All @@ -77,12 +74,15 @@ BOOL readTest_ReadFile_test2(DWORD dwByteCount, char cResult)
NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
Trace("ReadFile: ERROR -> Unable to open file \"%s\".\n",
Trace("ReadFile: ERROR -> Unable to open file \"%s\".\n",
szReadableFile);
return FALSE;
}

memset(readBuffer_ReadFile_test2, 0, PAGESIZE);
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
long pageSize = sysInfo.dwPageSize;
memset(readBuffer_ReadFile_test2, 0, pageSize);

bRc = ReadFile(hFile, readBuffer_ReadFile_test2, dwByteCount, &dwBytesRead, NULL);

Expand All @@ -94,7 +94,7 @@ BOOL readTest_ReadFile_test2(DWORD dwByteCount, char cResult)
Trace("\nbRc = %d\n", bRc);
Trace("readBuffer = [%s] dwByteCount = %d dwBytesRead = %d\n", readBuffer_ReadFile_test2, dwByteCount, dwBytesRead);
Trace("cresult = 1\n");
Trace("getlasterror = %d\n", GetLastError());
Trace("getlasterror = %d\n", GetLastError());
CloseHandle(hFile);
return FALSE;
}
Expand All @@ -121,51 +121,55 @@ BOOL readTest_ReadFile_test2(DWORD dwByteCount, char cResult)
PALTEST(file_io_ReadFile_test2_paltest_readfile_test2, "file_io/ReadFile/test2/paltest_readfile_test2")
{
HANDLE hFile = NULL;
const int BUFFER_SIZE = 2 * PAGESIZE;

DWORD dwByteCount[] = { 0,
10,
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
long pageSize = sysInfo.dwPageSize;
long bufferSize = 2 * pageSize;

DWORD dwByteCount[] = { 0,
10,
strlen(szStringTest),
PAGESIZE
pageSize
// Commented out two negative test cases : Refer VSW 312690
// 2 * PAGESIZE,
// 2 * pageSize,
// -1
};

DWORD oldProt;
char szResults[] = "1111"; // Was "111100": Refer VSW 312690
char szResults[] = "1111"; // Was "111100": Refer VSW 312690
int i;
BOOL bRc = FALSE;
DWORD dwBytesWritten = 0;

if (0 != PAL_Initialize(argc,argv))
{
return FAIL;
}

/* allocate read-write memery for readBuffer */
if (!(readBuffer_ReadFile_test2 = (char*) VirtualAlloc(NULL, BUFFER_SIZE, MEM_COMMIT, PAGE_READWRITE)))
if (!(readBuffer_ReadFile_test2 = (char*) VirtualAlloc(NULL, bufferSize, MEM_COMMIT, PAGE_READWRITE)))
{
Fail("VirtualAlloc failed: GetLastError returns %d\n", GetLastError());
return FAIL;
}

/* write protect the second page of readBuffer */
if (!VirtualProtect(&readBuffer_ReadFile_test2[PAGESIZE], PAGESIZE, PAGE_NOACCESS, &oldProt))
if (!VirtualProtect(&readBuffer_ReadFile_test2[pageSize], pageSize, PAGE_NOACCESS, &oldProt))
{
Fail("VirtualProtect failed: GetLastError returns %d\n", GetLastError());
return FAIL;
}

// create the test file
hFile = CreateFile(szReadableFile,
// create the test file
hFile = CreateFile(szReadableFile,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);

if(hFile == INVALID_HANDLE_VALUE)
{
Fail("ReadFile: ERROR -> Unable to create file \"%s\" (%d).\n",
Expand All @@ -175,7 +179,7 @@ PALTEST(file_io_ReadFile_test2_paltest_readfile_test2, "file_io/ReadFile/test2/p
bRc = WriteFile(hFile, szStringTest, strlen(szStringTest), &dwBytesWritten, NULL);
CloseHandle(hFile);


for (i = 0; i< NOOFTESTS; i++)
{
bRc = readTest_ReadFile_test2(dwByteCount[i], szResults[i]);
Expand All @@ -184,9 +188,8 @@ PALTEST(file_io_ReadFile_test2_paltest_readfile_test2, "file_io/ReadFile/test2/p
Fail("ReadFile: ERROR -> Failed on test[%d]\n", i);
}
}
VirtualFree(readBuffer_ReadFile_test2, BUFFER_SIZE, MEM_RELEASE);

VirtualFree(readBuffer_ReadFile_test2, bufferSize, MEM_RELEASE);
PAL_Terminate();
return PASS;
}

4 changes: 0 additions & 4 deletions src/coreclr/pal/tests/palsuite/issues.targets
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetArchitecture)' == 'arm64' and '$(TargetOS)' == 'OSX'">
<ExcludeList Include="file_io/ReadFile/test2/paltest_readfile_test2">
<!-- Test hard codes PAGE_SIZE = 4096. Apple Silicon does not have 4K pages. -->
<Issue>https://github.com/dotnet/runtime/issues/48783</Issue>
</ExcludeList>
<ExcludeList Include="filemapping_memmgt/VirtualProtect/test4/paltest_virtualprotect_test4">
<!-- Test tries to set RWX on general memory page -->
<Issue>https://github.com/dotnet/runtime/issues/48783</Issue>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
#include <unistd.h>
#include <minipal/utils.h>

#undef PAGE_SIZE
#define PAGE_SIZE (4096)

auto test_strcpy = strcpy;
auto test_strcmp = strcmp;
auto test_strlen = strlen;
Expand Down Expand Up @@ -50,7 +47,7 @@ bool WriteHeaderInfo(const char *path, char sharedMemoryType, char version, int
if (fd == -1)
return false;
*fdRef = fd;
if (ftruncate(fd, PAGE_SIZE) != 0)
if (ftruncate(fd, getpagesize()) != 0)
return false;
if (lseek(fd, 0, SEEK_SET) != 0)
return false;
Expand Down

0 comments on commit ca6716a

Please sign in to comment.