Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Fix unloading of images mapped by PAL (#17053)
Browse files Browse the repository at this point in the history
* Fix unloading of images mapped by PAL

The m_FileView member of MappedImageLayout is a CLRMapViewHolder,
which runs CLRUnmapViewOfFile on release. This is fine on Windows,
where the MappedImageLayout constructor calls
CLRMapViewOfFile. However, with FEATURE_PAL, the constructor calls
PAL_LOADLoadPEFile, which simulates LoadLibrary and records multiple
mapping entries. Each entry increases the refcount of the underlying
file handle PAL object. PAL_LOADUnloadPEFile should be called in this
case (instead of CLRUnmapViewOfFile), to decrease the refcount for
each mapping entry.

Fixes https://github.com/dotnet/coreclr/issues/15189.

* Fix build failure
  • Loading branch information
sbomer authored and jkotas committed Mar 20, 2018
1 parent 77d4c11 commit 0bec577
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/vm/peimagelayout.cpp
Expand Up @@ -469,9 +469,9 @@ MappedImageLayout::MappedImageLayout(HANDLE hFile, PEImage* pOwner)
#else //!FEATURE_PAL

#ifndef CROSSGEN_COMPILE
m_FileView = PAL_LOADLoadPEFile(hFile);
m_LoadedFile = PAL_LOADLoadPEFile(hFile);

if (m_FileView == NULL)
if (m_LoadedFile == NULL)
{
// For CoreCLR, try to load all files via LoadLibrary first. If LoadLibrary did not work, retry using
// regular mapping - but not for native images.
Expand All @@ -481,10 +481,10 @@ MappedImageLayout::MappedImageLayout(HANDLE hFile, PEImage* pOwner)
}

LOG((LF_LOADER, LL_INFO1000, "PEImage: image %S (hFile %p) mapped @ %p\n",
(LPCWSTR) GetPath(), hFile, (void*)m_FileView));
(LPCWSTR) GetPath(), hFile, (void*)m_LoadedFile));

TESTHOOKCALL(ImageMapped(GetPath(),m_FileView,IM_IMAGEMAP));
IfFailThrow(Init((void *) m_FileView));
TESTHOOKCALL(ImageMapped(GetPath(),m_LoadedFile,IM_IMAGEMAP));
IfFailThrow(Init((void *) m_LoadedFile));

if (!HasCorHeader())
ThrowHR(COR_E_BADIMAGEFORMAT);
Expand All @@ -500,7 +500,7 @@ MappedImageLayout::MappedImageLayout(HANDLE hFile, PEImage* pOwner)
}

#else // !CROSSGEN_COMPILE
m_FileView = NULL;
m_LoadedFile = NULL;
#endif // !CROSSGEN_COMPILE

#endif // !FEATURE_PAL
Expand Down
4 changes: 4 additions & 0 deletions src/vm/peimagelayout.h
Expand Up @@ -120,8 +120,12 @@ class MappedImageLayout: public PEImageLayout
VPTR_VTABLE_CLASS(MappedImageLayout,PEImageLayout)
VPTR_UNIQUE(0x15)
protected:
#ifndef FEATURE_PAL
HandleHolder m_FileMap;
CLRMapViewHolder m_FileView;
#else
PALPEFileHolder m_LoadedFile;
#endif
public:
#ifndef DACCESS_COMPILE
MappedImageLayout(HANDLE hFile, PEImage* pOwner);
Expand Down
9 changes: 9 additions & 0 deletions src/vm/util.hpp
Expand Up @@ -728,6 +728,15 @@ typedef Wrapper<void *, DoNothing, VoidCLRUnmapViewOfFile> CLRMapViewHolder;
typedef Wrapper<void *, DoNothing, DoNothing> CLRMapViewHolder;
#endif

#ifdef FEATURE_PAL
#ifndef DACCESS_COMPILE
FORCEINLINE void VoidPALUnloadPEFile(void *ptr) { PAL_LOADUnloadPEFile(ptr); }
typedef Wrapper<void *, DoNothing, VoidPALUnloadPEFile> PALPEFileHolder;
#else
typedef Wrapper<void *, DoNothing, DoNothing> PALPEFileHolder;
#endif
#endif // FEATURE_PAL

void GetProcessMemoryLoad(LPMEMORYSTATUSEX pMSEX);

void ProcessEventForHost(EClrEvent event, void *data);
Expand Down

0 comments on commit 0bec577

Please sign in to comment.