Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 15 additions & 37 deletions src/coreclr/ilasm/asmparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define asmparse_h

#include <stdio.h> // for FILE
#include <winwrap.h>
#include <dn-memmap.h>

#include "assembler.h" // for ErrorReporter Labels
//class Assembler;
Expand Down Expand Up @@ -95,9 +97,8 @@ class MappedFileStream : public ReadStream {
public:
MappedFileStream(_In_ __nullterminated WCHAR* wFileName)
: m_fileNameUtf8(NULL)
, m_hFile(INVALID_HANDLE_VALUE)
, m_File(nullptr)
, m_FileSize(0)
, m_hMapFile(NULL)
{
m_pStart = open(wFileName);
m_pCurr = m_pStart;
Expand All @@ -112,20 +113,8 @@ class MappedFileStream : public ReadStream {
}
~MappedFileStream()
{
if (m_hFile != INVALID_HANDLE_VALUE)
{
if (m_pStart)
UnmapViewOfFile((void*)m_pStart);
if (m_hMapFile)
CloseHandle(m_hMapFile);
CloseHandle(m_hFile);

m_pStart = NULL;
m_hMapFile = NULL;
m_hFile = INVALID_HANDLE_VALUE;
m_FileSize = 0;
}

if (m_File)
delete m_File;
if (m_fileNameUtf8 != NULL)
delete [] m_fileNameUtf8;
}
Expand Down Expand Up @@ -171,37 +160,26 @@ class MappedFileStream : public ReadStream {
}

private:
char* map_file()
{
DWORD dwFileSizeLow;

dwFileSizeLow = GetFileSize( m_hFile, NULL);
if (dwFileSizeLow == INVALID_FILE_SIZE)
return NULL;
m_FileSize = dwFileSizeLow;

// No difference between A and W in this case: last param (LPCTSTR) is NULL
m_hMapFile = CreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (m_hMapFile == NULL)
return NULL;

return (char*)(HMODULE) MapViewOfFile(m_hMapFile, FILE_MAP_READ, 0, 0, 0);
}
char* open(const WCHAR* moduleName)
{
_ASSERTE(moduleName);
if (!moduleName)
return NULL;

m_hFile = WszCreateFile(moduleName, GENERIC_READ, FILE_SHARE_READ,
0, OPEN_EXISTING, 0, 0);
return (m_hFile == INVALID_HANDLE_VALUE) ? NULL : map_file();
m_File = CreateMappedFile(moduleName);
if (m_File->Size() > UINT_MAX)
{
delete m_File;
m_File = nullptr;
}

m_FileSize = (DWORD)m_File->Size();
return (char*)m_File->Address();
}

char* m_fileNameUtf8; // FileName (for error reporting)
HANDLE m_hFile; // File we are reading from
MemoryMappedFile* m_File; // File we are reading from
DWORD m_FileSize;
HANDLE m_hMapFile;
char* m_pStart;
char* m_pEnd;
char* m_pCurr;
Expand Down
75 changes: 7 additions & 68 deletions src/coreclr/ildasm/ceeload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
/*************************************************************************************/
PELoader::PELoader()
{
m_hFile = NULL;
m_File = nullptr;
m_hMod = NULL;
m_hMapFile = NULL;
m_pNT64 = NULL;
m_bIsPE32 = FALSE;
m_FileSize = m_FileSizeAligned = 0;
Expand All @@ -30,91 +29,31 @@ PELoader::~PELoader()

m_hMod = NULL;
m_pNT64 = NULL;
// If we have an hFile then we opened this file ourselves!
// If we do not then this file was loaded by the OS and the OS will
// close it for us.
if (m_hFile)
this->close();
}

/*************************************************************************************/
/*************************************************************************************/
void PELoader::close()
{

// _ASSERTE(m_hFile != NULL);
if (m_hFile)
{
if (m_hMod)
UnmapViewOfFile((void*)m_hMod);
if (m_hMapFile)
CloseHandle(m_hMapFile);
CloseHandle(m_hFile);

m_hMod = NULL;
m_hMapFile = NULL;
m_hFile = NULL;
m_FileSize = m_FileSizeAligned = 0;
}
}


BOOL PELoader::open(LPCSTR moduleName)
{
HMODULE newhMod = NULL;
DWORD dwFileSizeLow;

_ASSERTE(moduleName);
if (!moduleName)
return FALSE;


m_hFile = CreateFileA(moduleName, GENERIC_READ, FILE_SHARE_READ,
0, OPEN_EXISTING, 0, 0);
if (m_hFile == INVALID_HANDLE_VALUE)
return FALSE;

dwFileSizeLow = GetFileSize( m_hFile, NULL);
if (dwFileSizeLow == INVALID_FILE_SIZE)
return FALSE;
m_FileSize = dwFileSizeLow;

m_hMapFile = CreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (m_hMapFile == NULL)
return FALSE;

newhMod = (HMODULE) MapViewOfFile(m_hMapFile, FILE_MAP_READ, 0, 0, 0);
if (newhMod == NULL)
return FALSE;
return open(newhMod);
delete m_File;
m_File = nullptr;
}

BOOL PELoader::open(const WCHAR* moduleName)
{
HMODULE newhMod = NULL;
DWORD dwFileSizeLow;

_ASSERTE(moduleName);
if (!moduleName)
return FALSE;

m_hFile = WszCreateFile(moduleName, GENERIC_READ, FILE_SHARE_READ,
0, OPEN_EXISTING, 0, 0);
if (m_hFile == INVALID_HANDLE_VALUE)
m_File = CreateMappedFile(moduleName);
if (m_File == nullptr)
return FALSE;

dwFileSizeLow = GetFileSize( m_hFile, NULL);
if (dwFileSizeLow == INVALID_FILE_SIZE)
return FALSE;
m_FileSize = dwFileSizeLow;
m_FileSize = m_File->Size();

m_hMapFile = CreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (m_hMapFile == NULL)
return FALSE;

newhMod = (HMODULE) MapViewOfFile(m_hMapFile, FILE_MAP_READ, 0, 0, 0);
if (newhMod == NULL)
return FALSE;
newhMod = (HMODULE)m_File->Address();
return open(newhMod);
}

Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/ildasm/ceeload.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class PELoader {
protected:

HMODULE m_hMod;
HANDLE m_hFile;
HANDLE m_hMapFile;
MemoryMappedFile* m_File;
BOOL m_bIsPE32;
size_t m_FileSize;
size_t m_FileSizeAligned;
Expand All @@ -54,7 +53,6 @@ class PELoader {

PELoader();
~PELoader();
BOOL open(const char* moduleNameIn);
BOOL open(const WCHAR* moduleNameIn);
BOOL open(HMODULE hMod);
BOOL getCOMHeader(IMAGE_COR20_HEADER **ppCorHeader);
Expand All @@ -69,7 +67,7 @@ class PELoader {
inline DWORD Signature() { return m_pNT32->Signature; };
inline BYTE* base() { return (BYTE*) m_hMod; };
inline HMODULE getHModule() { return m_hMod; };
inline HANDLE getHFile() { return m_hFile; } ;
inline size_t getFileSize() { return m_FileSize; };
};

#endif // CEELoad_H
2 changes: 1 addition & 1 deletion src/coreclr/ildasm/dasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5837,7 +5837,7 @@ void DumpStatistics(IMAGE_COR20_HEADER *CORHeader, void* GUICookie)
TableSeenReset();
metaSize = 0;

sprintf_s(szString,SZSTRING_SIZE,"// File size : %d", fileSize = SafeGetFileSize(g_pPELoader->getHFile(), NULL));
sprintf_s(szString,SZSTRING_SIZE,"// File size : %d", fileSize = (int)g_pPELoader->getFileSize());
printLine(GUICookie,szStr);

if (g_pPELoader->IsPE32())
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/ildasm/ildasmpch.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <limits.h>
#include <algorithm>
#include "dn-stdio.h"
#include "dn-memmap.h"

using std::min;
using std::max;
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/inc/longfilepathwrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#define _WIN_PATH_APIS_WRAPPER_
class SString;

#include <dn-memmap.h>

#ifdef HOST_WINDOWS

HMODULE
Expand All @@ -27,6 +29,8 @@ CreateFileWrapper(

int u16_fopen_wrapper(FILE** stream, const WCHAR* filename, const WCHAR* mode);

MemoryMappedFile* CreateMappedFileWrapper(const WCHAR* filename);

BOOL
CopyFileExWrapper(
_In_ LPCWSTR lpExistingFileName,
Expand Down
6 changes: 1 addition & 5 deletions src/coreclr/inc/stresslog.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,9 @@ class StressLog {
static const size_t MAX_MODULES = 5;
ModuleDesc modules[MAX_MODULES]; // descriptor of the modules images

#if defined(HOST_64BIT)
#if defined(HOST_64BIT) && HOST_WINDOWS
#define MEMORY_MAPPED_STRESSLOG
#ifdef HOST_WINDOWS
#define MEMORY_MAPPED_STRESSLOG_BASE_ADDRESS (void*)0x400000000000
#else
#define MEMORY_MAPPED_STRESSLOG_BASE_ADDRESS nullptr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this removing support for memory mapped stresslog on non-Windows?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. NativeAOT copy of stresslog removes memory map support on all platforms. I haven't measured the impact.

#endif
#endif

#ifdef MEMORY_MAPPED_STRESSLOG
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/inc/winwrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
#define WszLoadLibrary LoadLibraryExWrapper
#define WszCreateFile CreateFileWrapper
#define fopen_lp u16_fopen_wrapper
#define CreateMappedFile CreateMappedFileWrapper
#else // HOST_WINDOWS
#define WszLoadLibrary LoadLibraryExW
#define WszCreateFile CreateFileW
#define fopen_lp u16_fopen_s
#define CreateMappedFile MemoryMappedFile::Open
#endif // HOST_WINDOWS

//APIS which have a buffer as an out parameter
Expand Down
Loading
Loading