Skip to content

Commit

Permalink
[release/8.0] PEImage should not permit m_path field mutation (#9…
Browse files Browse the repository at this point in the history
…1085)

* Remove cases where PEImage::m_path was mutated. Create m_pathHash field and remove function. Remove FEATURE_CASE_SENSITIVE_FILESYSTEM.

* Address contract violations

* Remove try/catch and replace with assert

---------

Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
  • Loading branch information
github-actions[bot] and AaronRobinsonMSFT authored Aug 24, 2023
1 parent 5d92883 commit f4120cc
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 62 deletions.
3 changes: 1 addition & 2 deletions src/coreclr/debug/daccess/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2648,8 +2648,7 @@ ClrDataAccess::GetAssemblyLocation(CLRDATA_ADDRESS assembly, int count, _Inout_u
// Turn from bytes to wide characters
if (!pAssembly->GetPEAssembly()->GetPath().IsEmpty())
{
if (!pAssembly->GetPEAssembly()->GetPath().
DacGetUnicode(count, location, pNeeded))
if (!pAssembly->GetPEAssembly()->GetPath().DacGetUnicode(count, location, pNeeded))
{
hr = E_FAIL;
}
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/inc/sstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,9 @@ class EMPTY_BASES_DECL SString : private SBuffer
BOOL IsASCIIScanned() const;
void SetASCIIScanned() const;
void SetNormalized() const;
public:
BOOL IsNormalized() const;
private:
void ClearNormalized() const;

void EnsureWritable() const;
Expand Down
8 changes: 0 additions & 8 deletions src/coreclr/md/compiler/mdutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,7 @@ HRESULT LOADEDMODULES::FindCachedReadOnlyEntry(
{
// If the name matches...
LPCWSTR pszName = pRegMeta->GetNameOfDBFile();
#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
if (u16_strcmp(szName, pszName) == 0)
#else
if (SString::_wcsicmp(szName, pszName) == 0)
#endif
{
ULONG cRefs;

Expand Down Expand Up @@ -299,11 +295,7 @@ HRESULT LOADEDMODULES::FindCachedReadOnlyEntry(
{
// If the name matches...
LPCWSTR pszName = pRegMeta->GetNameOfDBFile();
#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
if (u16_strcmp(szName, pszName) == 0)
#else
if (SString::_wcsicmp(szName, pszName) == 0)
#endif
{
ULONG cRefs;

Expand Down
8 changes: 3 additions & 5 deletions src/coreclr/vm/dwbucketmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,11 +960,9 @@ bool BaseBucketParamsManager::GetFileVersionInfoForModule(Module* pModule, USHOR
// if we failed to get the version info from the native image then fall back to the IL image.
if (!succeeded)
{
LPCWSTR modulePath = pPEAssembly->GetPath().GetUnicode();
if (modulePath != NULL && modulePath != SString::Empty() && SUCCEEDED(DwGetFileVersionInfo(modulePath, major, minor, build, revision)))
{
succeeded = true;
}
const SString& modulePath = pPEAssembly->GetPath();
_ASSERTE(modulePath.IsNormalized());
succeeded = !modulePath.IsEmpty() && SUCCEEDED(DwGetFileVersionInfo(modulePath.GetUnicode(), major, minor, build, revision));
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/vm/excep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11616,7 +11616,8 @@ VOID GetAssemblyDetailInfo(SString &sType,

SString sAlcName;
pPEAssembly->GetAssemblyBinder()->GetNameForDiagnostics(sAlcName);
if (pPEAssembly->GetPath().IsEmpty())
SString assemblyPath{ pPEAssembly->GetPath() };
if (assemblyPath.IsEmpty())
{
detailsUtf8.Printf("Type %s originates from '%s' in the context '%s' in a byte array",
sType.GetUTF8(),
Expand All @@ -11629,7 +11630,7 @@ VOID GetAssemblyDetailInfo(SString &sType,
sType.GetUTF8(),
sAssemblyDisplayName.GetUTF8(),
sAlcName.GetUTF8(),
pPEAssembly->GetPath().GetUTF8());
assemblyPath.GetUTF8());
}

sAssemblyDetailInfo.Append(detailsUtf8.GetUnicode());
Expand Down
10 changes: 3 additions & 7 deletions src/coreclr/vm/peimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ ULONG PEImage::Release()
result=InterlockedDecrement(&m_refCount);
if (result == 0 )
{
LOG((LF_LOADER, LL_INFO100, "PEImage: Closing Image %s\n", m_path.GetUTF8()));
LOG((LF_LOADER, LL_INFO100, "PEImage: Closing %p\n", this));
if(m_bInHashMap)
{
PEImageLocator locator(this);
PEImage* deleted = (PEImage *)s_Images->DeleteValue(GetPathHash(), &locator);
PEImage* deleted = (PEImage *)s_Images->DeleteValue(m_pathHash, &locator);
_ASSERTE(deleted == this);
}
}
Expand Down Expand Up @@ -249,12 +249,7 @@ BOOL PEImage::CompareImage(UPTR u1, UPTR u2)
EX_TRY
{
SString path(SString::Literal, pLocator->m_pPath);

#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
if (pImage->GetPath().Equals(path))
#else
if (pImage->GetPath().EqualsCaseInsensitive(path))
#endif
{
ret = TRUE;
}
Expand Down Expand Up @@ -623,6 +618,7 @@ void PEImage::EnumMemoryRegions(CLRDataEnumMemoryFlags flags)

PEImage::PEImage():
m_path(),
m_pathHash(0),
m_refCount(1),
m_bInHashMap(FALSE),
m_bundleFileLocation(),
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/vm/peimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ class PEImage final
PTR_PEImageLayout GetLoadedLayout();
PTR_PEImageLayout GetFlatLayout();

BOOL HasPath();
ULONG GetPathHash();
const SString& GetPath();
const SString& GetPathToLoad();
LPCWSTR GetPathForErrorMessages() { return GetPath(); }
Expand Down Expand Up @@ -288,6 +286,7 @@ class PEImage final
// ------------------------------------------------------------

SString m_path;
ULONG m_pathHash;
LONG m_refCount;

// means this is a unique (deduped) instance.
Expand Down
32 changes: 2 additions & 30 deletions src/coreclr/vm/peimage.inl
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ inline void PEImage::Init(LPCWSTR pPath, BundleFileLocation bundleFileLocation)

m_path = pPath;
m_path.Normalize();
m_pathHash = m_path.HashCaseInsensitive();
m_bundleFileLocation = bundleFileLocation;
SetModuleFileNameHintForDAC();
}
Expand All @@ -310,11 +311,7 @@ inline PTR_PEImage PEImage::FindByPath(LPCWSTR pPath, BOOL isInBundle /* = TRUE
int CaseHashHelper(const WCHAR *buffer, COUNT_T count);

PEImageLocator locator(pPath, isInBundle);
#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
DWORD dwHash=path.Hash();
#else
DWORD dwHash = CaseHashHelper(pPath, (COUNT_T) u16_strlen(pPath));
#endif
return (PEImage *) s_Images->LookupValue(dwHash, &locator);
}

Expand Down Expand Up @@ -366,7 +363,7 @@ inline void PEImage::AddToHashMap()
CONTRACTL_END;

_ASSERTE(s_hashLock.OwnedByCurrentThread());
s_Images->InsertValue(GetPathHash(),this);
s_Images->InsertValue(m_pathHash,this);
m_bInHashMap=TRUE;
}

Expand All @@ -378,31 +375,6 @@ inline BOOL PEImage::Has32BitNTHeaders()
return GetOrCreateLayout(PEImageLayout::LAYOUT_ANY)->Has32BitNTHeaders();
}

inline BOOL PEImage::HasPath()
{
LIMITED_METHOD_CONTRACT;

return !GetPath().IsEmpty();
}

inline ULONG PEImage::GetPathHash()
{
CONTRACT(ULONG)
{
PRECONDITION(HasPath());
MODE_ANY;
GC_NOTRIGGER;
THROWS;
}
CONTRACT_END;

#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
RETURN m_path.Hash();
#else
RETURN m_path.HashCaseInsensitive();
#endif
}

inline void PEImage::GetPEKindAndMachine(DWORD* pdwKind, DWORD* pdwMachine)
{
CONTRACTL
Expand Down
15 changes: 12 additions & 3 deletions src/coreclr/vm/peimagelayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ LoadedImageLayout::LoadedImageLayout(PEImage* pOwner, HRESULT* loadFailure)

IfFailThrow(Init(m_Module, true));

LOG((LF_LOADER, LL_INFO1000, "PEImage: Opened HMODULE %s\n", pOwner->GetPath().GetUTF8()));
#ifdef LOGGING
SString ownerPath{ pOwner->GetPath() };
LOG((LF_LOADER, LL_INFO1000, "PEImage: Opened HMODULE %s\n", ownerPath.GetUTF8()));
#endif // LOGGING

#else
HANDLE hFile = pOwner->GetFileHandle();
Expand All @@ -548,8 +551,11 @@ LoadedImageLayout::LoadedImageLayout(PEImage* pOwner, HRESULT* loadFailure)
return;
}

#ifdef LOGGING
SString ownerPath{ pOwner->GetPath() };
LOG((LF_LOADER, LL_INFO1000, "PEImage: image %s (hFile %p) mapped @ %p\n",
pOwner->GetPath().GetUTF8(), hFile, (void*)m_LoadedFile));
ownerPath.GetUTF8(), hFile, (void*)m_LoadedFile));
#endif // LOGGING

IfFailThrow(Init((void*)m_LoadedFile));

Expand Down Expand Up @@ -616,7 +622,10 @@ FlatImageLayout::FlatImageLayout(PEImage* pOwner)
INT64 offset = pOwner->GetOffset();
INT64 size = pOwner->GetSize();

LOG((LF_LOADER, LL_INFO100, "PEImage: Opening flat %s\n", pOwner->GetPath().GetUTF8()));
#ifdef LOGGING
SString ownerPath{ pOwner->GetPath() };
LOG((LF_LOADER, LL_INFO100, "PEImage: Opening flat %s\n", ownerPath.GetUTF8()));
#endif // LOGGING

// If a size is not specified, load the whole file
if (size == 0)
Expand Down
7 changes: 4 additions & 3 deletions src/coreclr/vm/readytoruninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ static void LogR2r(const char *msg, PEAssembly *pPEAssembly)
if (r2rLogFile == NULL)
return;

fprintf(r2rLogFile, "%s: \"%s\".\n", msg, pPEAssembly->GetPath().GetUTF8());
SString assemblyPath{ pPEAssembly->GetPath() };
fprintf(r2rLogFile, "%s: \"%s\".\n", msg, assemblyPath.GetUTF8());
fflush(r2rLogFile);
}

Expand Down Expand Up @@ -1904,7 +1905,7 @@ uint32_t ReadyToRun_TypeGenericInfoMap::GetGenericArgumentCount(mdTypeDef input,
uint32_t count = ((uint8_t)typeGenericInfo & (uint8_t)ReadyToRunTypeGenericInfo::GenericCountMask);
if (count > 2)
foundResult = false;

if (!foundResult)
{
HENUMInternalHolder hEnumTyPars(pImport);
Expand All @@ -1922,7 +1923,7 @@ HRESULT ReadyToRun_TypeGenericInfoMap::GetGenericArgumentCountNoThrow(mdTypeDef
uint32_t count = ((uint8_t)typeGenericInfo & (uint8_t)ReadyToRunTypeGenericInfo::GenericCountMask);
if (count > 2)
foundResult = false;

if (!foundResult)
{
HENUMInternalHolder hEnumTyPars(pImport);
Expand Down

0 comments on commit f4120cc

Please sign in to comment.