From 550c59a96baa6714a32dd649795eb4e5294df18b Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Fri, 7 Apr 2017 13:25:42 +0300 Subject: [PATCH 01/32] Extract PEImage::CreateLayoutMapped and PEImage::CreateLayoutFlat from PEImage::GetLayoutInternal. --- src/vm/peimage.cpp | 138 +++++++++++++++++++++++++++++++++-------------------- src/vm/peimage.h | 6 +++ 2 files changed, 92 insertions(+), 52 deletions(-) diff --git a/src/vm/peimage.cpp b/src/vm/peimage.cpp index 3367ef9..1462c94 100644 --- a/src/vm/peimage.cpp +++ b/src/vm/peimage.cpp @@ -911,68 +911,102 @@ PTR_PEImageLayout PEImage::GetLayoutInternal(DWORD imageLayoutMask,DWORD flags) if (imageLayoutMask&PEImageLayout::LAYOUT_MAPPED) { - PEImageLayout * pLoadLayout = NULL; + pRetVal = PEImage::CreateLayoutMapped(); + } + else + { + pRetVal = PEImage::CreateLayoutFlat(); + } + } - if (m_bIsTrustedNativeImage || IsFile()) - { - // For CoreCLR, try to load all files via LoadLibrary first. If LoadLibrary did not work, retry using - // regular mapping - but not for native images. - pLoadLayout = PEImageLayout::Load(this, TRUE /* bNTSafeLoad */, m_bIsTrustedNativeImage /* bThrowOnError */); - } + if (pRetVal != NULL) + { + pRetVal->AddRef(); + } - if (pLoadLayout != NULL) - { - SetLayout(IMAGE_MAPPED,pLoadLayout); - pLoadLayout->AddRef(); - SetLayout(IMAGE_LOADED,pLoadLayout); - pRetVal=pLoadLayout; - } - else - if (IsFile()) - { - PEImageLayoutHolder pLayout(PEImageLayout::Map(GetFileHandle(),this)); - - bool fMarkAnyCpuImageAsLoaded = false; - // Avoid mapping another image if we can. We can only do this for IL-ONLY images - // since LoadLibrary is needed if we are to actually load code. - if (pLayout->HasCorHeader() && pLayout->IsILOnly()) - { - // For CoreCLR, IL only images will always be mapped. We also dont bother doing the conversion of PE header on 64bit, - // as done below for the desktop case, as there is no appcompat burden for CoreCLR on 64bit to have that conversion done. - fMarkAnyCpuImageAsLoaded = true; - } + return pRetVal; +} - pLayout.SuppressRelease(); +PTR_PEImageLayout PEImage::CreateLayoutMapped() +{ + CONTRACTL + { + THROWS; + GC_TRIGGERS; + MODE_ANY; + PRECONDITION(m_pLayoutLock->IsWriterLock()); + } + CONTRACTL_END; - SetLayout(IMAGE_MAPPED,pLayout); - if (fMarkAnyCpuImageAsLoaded) - { - pLayout->AddRef(); - SetLayout(IMAGE_LOADED, pLayout); - } - pRetVal=pLayout; - } - else - { - PEImageLayoutHolder flatPE(GetLayoutInternal(PEImageLayout::LAYOUT_FLAT,LAYOUT_CREATEIFNEEDED)); - if (!flatPE->CheckFormat()) - ThrowFormat(COR_E_BADIMAGEFORMAT); - pRetVal=PEImageLayout::LoadFromFlat(flatPE); - SetLayout(IMAGE_MAPPED,pRetVal); - } + PTR_PEImageLayout pRetVal; + + PEImageLayout * pLoadLayout = NULL; + + if (m_bIsTrustedNativeImage || IsFile()) + { + // For CoreCLR, try to load all files via LoadLibrary first. If LoadLibrary did not work, retry using + // regular mapping - but not for native images. + pLoadLayout = PEImageLayout::Load(this, TRUE /* bNTSafeLoad */, m_bIsTrustedNativeImage /* bThrowOnError */); + } + + if (pLoadLayout != NULL) + { + SetLayout(IMAGE_MAPPED,pLoadLayout); + pLoadLayout->AddRef(); + SetLayout(IMAGE_LOADED,pLoadLayout); + pRetVal=pLoadLayout; + } + else if (IsFile()) + { + PEImageLayoutHolder pLayout(PEImageLayout::Map(GetFileHandle(),this)); + + bool fMarkAnyCpuImageAsLoaded = false; + // Avoid mapping another image if we can. We can only do this for IL-ONLY images + // since LoadLibrary is needed if we are to actually load code + if (pLayout->HasCorHeader() && pLayout->IsILOnly()) + { + // For CoreCLR, IL only images will always be mapped. We also dont bother doing the conversion of PE header on 64bit, + // as done below for the desktop case, as there is no appcompat burden for CoreCLR on 64bit to have that conversion done. + fMarkAnyCpuImageAsLoaded = true; } - else - if (imageLayoutMask&PEImageLayout::LAYOUT_FLAT) + + pLayout.SuppressRelease(); + + SetLayout(IMAGE_MAPPED,pLayout); + if (fMarkAnyCpuImageAsLoaded) { - pRetVal=PEImageLayout::LoadFlat(GetFileHandle(),this); - m_pLayouts[IMAGE_FLAT]=pRetVal; + pLayout->AddRef(); + SetLayout(IMAGE_LOADED, pLayout); } - + pRetVal=pLayout; } - if (pRetVal) + else { - pRetVal->AddRef(); + PEImageLayoutHolder flatPE(GetLayoutInternal(PEImageLayout::LAYOUT_FLAT,LAYOUT_CREATEIFNEEDED)); + if (!flatPE->CheckFormat()) + ThrowFormat(COR_E_BADIMAGEFORMAT); + pRetVal=PEImageLayout::LoadFromFlat(flatPE); + SetLayout(IMAGE_MAPPED,pRetVal); } + + return pRetVal; +} + +PTR_PEImageLayout PEImage::CreateLayoutFlat() +{ + CONTRACTL + { + GC_TRIGGERS; + MODE_ANY; + PRECONDITION(m_pLayoutLock->IsWriterLock()); + } + CONTRACTL_END; + + PTR_PEImageLayout pRetVal; + + pRetVal = PEImageLayout::LoadFlat(GetFileHandle(),this); + m_pLayouts[IMAGE_FLAT] = pRetVal; + return pRetVal; } diff --git a/src/vm/peimage.h b/src/vm/peimage.h index 3245621..f61e185 100644 --- a/src/vm/peimage.h +++ b/src/vm/peimage.h @@ -257,6 +257,12 @@ private: #ifndef DACCESS_COMPILE // Get or create the layout corresponding to the mask, with an AddRef PTR_PEImageLayout GetLayoutInternal(DWORD imageLayoutMask, DWORD flags); + + // Create the mapped layout + PTR_PEImageLayout CreateLayoutMapped(); + + // Create the flat layout + PTR_PEImageLayout CreateLayoutFlat(); #endif // Get an existing layout corresponding to the mask, no AddRef PTR_PEImageLayout GetExistingLayoutInternal(DWORD imageLayoutMask); -- 2.7.4