Skip to content
Closed
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
29 changes: 19 additions & 10 deletions src/coreclr/src/utilcode/pedecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1770,20 +1770,29 @@ void PEDecoder::LayoutILOnly(void *base, BOOL allowFullPE) const
PAGE_READONLY, &oldProtection))
ThrowLastError();

// Finally, apply proper protection to copied sections
section = sectionStart;
while (section < sectionEnd)
// Finally, apply proper protection to copied sections
for (section = sectionStart; section < sectionEnd; section++)
{
// Add appropriate page protection.
if ((section->Characteristics & VAL32(IMAGE_SCN_MEM_WRITE)) == 0)
#if defined(CROSSGEN_COMPILE) || defined(TARGET_UNIX)
if (section->Characteristics & IMAGE_SCN_MEM_WRITE)
continue;

DWORD newProtection = PAGE_READONLY;
#else
DWORD newProtection = section->Characteristics & IMAGE_SCN_MEM_EXECUTE ?
PAGE_EXECUTE_READ :
section->Characteristics & IMAGE_SCN_MEM_WRITE ?
PAGE_READWRITE :
PAGE_READONLY;
#endif

if (!ClrVirtualProtect((void*)((BYTE*)base + VAL32(section->VirtualAddress)),
VAL32(section->Misc.VirtualSize),
newProtection, &oldProtection))
{
if (!ClrVirtualProtect((void *) ((BYTE *)base + VAL32(section->VirtualAddress)),
VAL32(section->Misc.VirtualSize),
PAGE_READONLY, &oldProtection))
ThrowLastError();
ThrowLastError();
}

section++;
}

RETURN;
Expand Down
73 changes: 67 additions & 6 deletions src/coreclr/src/vm/peimagelayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ PEImageLayout* PEImageLayout::CreateFromHMODULE(HMODULE hModule,PEImage* pOwner,
PEImageLayout* PEImageLayout::LoadFromFlat(PEImageLayout* pflatimage)
{
STANDARD_VM_CONTRACT;

_ASSERTE(!"how did we get here?");
return new ConvertedImageLayout(pflatimage);

throw "How did we get here?";
}

PEImageLayout* PEImageLayout::LoadConverted(PEImage* pOwner)
Expand Down Expand Up @@ -398,32 +402,89 @@ ConvertedImageLayout::ConvertedImageLayout(PEImageLayout* source)
m_pOwner=source->m_pOwner;
_ASSERTE(!source->IsMapped());

m_pExceptionDir = NULL;

if (!source->HasNTHeaders())
EEFileLoadException::Throw(GetPath(), COR_E_BADIMAGEFORMAT);
LOG((LF_LOADER, LL_INFO100, "PEImage: Opening manually mapped stream\n"));

#if !defined(CROSSGEN_COMPILE) && !defined(TARGET_UNIX)
// on Windows we may want to enable execution if the image contains R2R sections
// so must ensure the mapping is compatible with that
m_FileMap.Assign(WszCreateFileMapping(INVALID_HANDLE_VALUE, NULL,
PAGE_EXECUTE_READWRITE, 0,
source->GetVirtualSize(), NULL));

DWORD allAccess = FILE_MAP_EXECUTE | FILE_MAP_WRITE;
#else
m_FileMap.Assign(WszCreateFileMapping(INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE, 0,
source->GetVirtualSize(), NULL));
PAGE_READWRITE, 0,
source->GetVirtualSize(), NULL));

DWORD allAccess = FILE_MAP_ALL_ACCESS;
#endif

if (m_FileMap == NULL)
ThrowLastError();


m_FileView.Assign(CLRMapViewOfFile(m_FileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0,
m_FileView.Assign(CLRMapViewOfFile(m_FileMap, allAccess, 0, 0, 0,
(void *) source->GetPreferredBase()));
if (m_FileView == NULL)
m_FileView.Assign(CLRMapViewOfFile(m_FileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0));
m_FileView.Assign(CLRMapViewOfFile(m_FileMap, allAccess, 0, 0, 0));

if (m_FileView == NULL)
ThrowLastError();

source->LayoutILOnly(m_FileView, TRUE); //@TODO should be false for streams
IfFailThrow(Init(m_FileView));

#ifdef CROSSGEN_COMPILE
#if defined(CROSSGEN_COMPILE)
if (HasNativeHeader())
{
ApplyBaseRelocations();
}
#elif !defined(TARGET_UNIX)
if (HasCorHeader() && (HasNativeHeader() || HasReadyToRunHeader()) && g_fAllowNativeImages)
{
//Do base relocation for PE, if necessary.
if (!IsNativeMachineFormat())
ThrowHR(COR_E_BADIMAGEFORMAT);

ApplyBaseRelocations();

// Check if there is a static function table and install it. (except x86)
#if !defined(TARGET_X86)
COUNT_T cbSize = 0;
PT_RUNTIME_FUNCTION pExceptionDir = (PT_RUNTIME_FUNCTION)GetDirectoryEntryData(IMAGE_DIRECTORY_ENTRY_EXCEPTION, &cbSize);
DWORD tableSize = cbSize / sizeof(T_RUNTIME_FUNCTION);

if (pExceptionDir != NULL)
{
if (!RtlAddFunctionTable(pExceptionDir, tableSize, (DWORD64)this->GetBase()))
ThrowLastError();

m_pExceptionDir = pExceptionDir;
}
#endif //TARGET_X86
}
#endif
}

ConvertedImageLayout::~ConvertedImageLayout()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;

#if !defined(CROSSGEN_COMPILE) && !defined(TARGET_UNIX) && !defined(TARGET_X86)
if (m_pExceptionDir)
{
RtlDeleteFunctionTable(m_pExceptionDir);
}
#endif
}

Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/src/vm/peimagelayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ class ConvertedImageLayout: public PEImageLayout
public:
#ifndef DACCESS_COMPILE
ConvertedImageLayout(PEImageLayout* source);
virtual ~ConvertedImageLayout();
#endif
private:
PT_RUNTIME_FUNCTION m_pExceptionDir;
};

class MappedImageLayout: public PEImageLayout
Expand Down