Skip to content

Commit f4120cc

Browse files
[release/8.0] PEImage should not permit m_path field mutation (#91085)
* 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 <[email protected]>
1 parent 5d92883 commit f4120cc

File tree

10 files changed

+31
-62
lines changed

10 files changed

+31
-62
lines changed

src/coreclr/debug/daccess/request.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,8 +2648,7 @@ ClrDataAccess::GetAssemblyLocation(CLRDATA_ADDRESS assembly, int count, _Inout_u
26482648
// Turn from bytes to wide characters
26492649
if (!pAssembly->GetPEAssembly()->GetPath().IsEmpty())
26502650
{
2651-
if (!pAssembly->GetPEAssembly()->GetPath().
2652-
DacGetUnicode(count, location, pNeeded))
2651+
if (!pAssembly->GetPEAssembly()->GetPath().DacGetUnicode(count, location, pNeeded))
26532652
{
26542653
hr = E_FAIL;
26552654
}

src/coreclr/inc/sstring.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,9 @@ class EMPTY_BASES_DECL SString : private SBuffer
683683
BOOL IsASCIIScanned() const;
684684
void SetASCIIScanned() const;
685685
void SetNormalized() const;
686+
public:
686687
BOOL IsNormalized() const;
688+
private:
687689
void ClearNormalized() const;
688690

689691
void EnsureWritable() const;

src/coreclr/md/compiler/mdutil.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,7 @@ HRESULT LOADEDMODULES::FindCachedReadOnlyEntry(
265265
{
266266
// If the name matches...
267267
LPCWSTR pszName = pRegMeta->GetNameOfDBFile();
268-
#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
269-
if (u16_strcmp(szName, pszName) == 0)
270-
#else
271268
if (SString::_wcsicmp(szName, pszName) == 0)
272-
#endif
273269
{
274270
ULONG cRefs;
275271

@@ -299,11 +295,7 @@ HRESULT LOADEDMODULES::FindCachedReadOnlyEntry(
299295
{
300296
// If the name matches...
301297
LPCWSTR pszName = pRegMeta->GetNameOfDBFile();
302-
#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
303-
if (u16_strcmp(szName, pszName) == 0)
304-
#else
305298
if (SString::_wcsicmp(szName, pszName) == 0)
306-
#endif
307299
{
308300
ULONG cRefs;
309301

src/coreclr/vm/dwbucketmanager.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,9 @@ bool BaseBucketParamsManager::GetFileVersionInfoForModule(Module* pModule, USHOR
960960
// if we failed to get the version info from the native image then fall back to the IL image.
961961
if (!succeeded)
962962
{
963-
LPCWSTR modulePath = pPEAssembly->GetPath().GetUnicode();
964-
if (modulePath != NULL && modulePath != SString::Empty() && SUCCEEDED(DwGetFileVersionInfo(modulePath, major, minor, build, revision)))
965-
{
966-
succeeded = true;
967-
}
963+
const SString& modulePath = pPEAssembly->GetPath();
964+
_ASSERTE(modulePath.IsNormalized());
965+
succeeded = !modulePath.IsEmpty() && SUCCEEDED(DwGetFileVersionInfo(modulePath.GetUnicode(), major, minor, build, revision));
968966
}
969967
}
970968

src/coreclr/vm/excep.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11616,7 +11616,8 @@ VOID GetAssemblyDetailInfo(SString &sType,
1161611616

1161711617
SString sAlcName;
1161811618
pPEAssembly->GetAssemblyBinder()->GetNameForDiagnostics(sAlcName);
11619-
if (pPEAssembly->GetPath().IsEmpty())
11619+
SString assemblyPath{ pPEAssembly->GetPath() };
11620+
if (assemblyPath.IsEmpty())
1162011621
{
1162111622
detailsUtf8.Printf("Type %s originates from '%s' in the context '%s' in a byte array",
1162211623
sType.GetUTF8(),
@@ -11629,7 +11630,7 @@ VOID GetAssemblyDetailInfo(SString &sType,
1162911630
sType.GetUTF8(),
1163011631
sAssemblyDisplayName.GetUTF8(),
1163111632
sAlcName.GetUTF8(),
11632-
pPEAssembly->GetPath().GetUTF8());
11633+
assemblyPath.GetUTF8());
1163311634
}
1163411635

1163511636
sAssemblyDetailInfo.Append(detailsUtf8.GetUnicode());

src/coreclr/vm/peimage.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ ULONG PEImage::Release()
141141
result=InterlockedDecrement(&m_refCount);
142142
if (result == 0 )
143143
{
144-
LOG((LF_LOADER, LL_INFO100, "PEImage: Closing Image %s\n", m_path.GetUTF8()));
144+
LOG((LF_LOADER, LL_INFO100, "PEImage: Closing %p\n", this));
145145
if(m_bInHashMap)
146146
{
147147
PEImageLocator locator(this);
148-
PEImage* deleted = (PEImage *)s_Images->DeleteValue(GetPathHash(), &locator);
148+
PEImage* deleted = (PEImage *)s_Images->DeleteValue(m_pathHash, &locator);
149149
_ASSERTE(deleted == this);
150150
}
151151
}
@@ -249,12 +249,7 @@ BOOL PEImage::CompareImage(UPTR u1, UPTR u2)
249249
EX_TRY
250250
{
251251
SString path(SString::Literal, pLocator->m_pPath);
252-
253-
#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
254-
if (pImage->GetPath().Equals(path))
255-
#else
256252
if (pImage->GetPath().EqualsCaseInsensitive(path))
257-
#endif
258253
{
259254
ret = TRUE;
260255
}
@@ -623,6 +618,7 @@ void PEImage::EnumMemoryRegions(CLRDataEnumMemoryFlags flags)
623618

624619
PEImage::PEImage():
625620
m_path(),
621+
m_pathHash(0),
626622
m_refCount(1),
627623
m_bInHashMap(FALSE),
628624
m_bundleFileLocation(),

src/coreclr/vm/peimage.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ class PEImage final
132132
PTR_PEImageLayout GetLoadedLayout();
133133
PTR_PEImageLayout GetFlatLayout();
134134

135-
BOOL HasPath();
136-
ULONG GetPathHash();
137135
const SString& GetPath();
138136
const SString& GetPathToLoad();
139137
LPCWSTR GetPathForErrorMessages() { return GetPath(); }
@@ -288,6 +286,7 @@ class PEImage final
288286
// ------------------------------------------------------------
289287

290288
SString m_path;
289+
ULONG m_pathHash;
291290
LONG m_refCount;
292291

293292
// means this is a unique (deduped) instance.

src/coreclr/vm/peimage.inl

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ inline void PEImage::Init(LPCWSTR pPath, BundleFileLocation bundleFileLocation)
288288

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

312313
PEImageLocator locator(pPath, isInBundle);
313-
#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
314-
DWORD dwHash=path.Hash();
315-
#else
316314
DWORD dwHash = CaseHashHelper(pPath, (COUNT_T) u16_strlen(pPath));
317-
#endif
318315
return (PEImage *) s_Images->LookupValue(dwHash, &locator);
319316
}
320317

@@ -366,7 +363,7 @@ inline void PEImage::AddToHashMap()
366363
CONTRACTL_END;
367364

368365
_ASSERTE(s_hashLock.OwnedByCurrentThread());
369-
s_Images->InsertValue(GetPathHash(),this);
366+
s_Images->InsertValue(m_pathHash,this);
370367
m_bInHashMap=TRUE;
371368
}
372369

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

381-
inline BOOL PEImage::HasPath()
382-
{
383-
LIMITED_METHOD_CONTRACT;
384-
385-
return !GetPath().IsEmpty();
386-
}
387-
388-
inline ULONG PEImage::GetPathHash()
389-
{
390-
CONTRACT(ULONG)
391-
{
392-
PRECONDITION(HasPath());
393-
MODE_ANY;
394-
GC_NOTRIGGER;
395-
THROWS;
396-
}
397-
CONTRACT_END;
398-
399-
#ifdef FEATURE_CASE_SENSITIVE_FILESYSTEM
400-
RETURN m_path.Hash();
401-
#else
402-
RETURN m_path.HashCaseInsensitive();
403-
#endif
404-
}
405-
406378
inline void PEImage::GetPEKindAndMachine(DWORD* pdwKind, DWORD* pdwMachine)
407379
{
408380
CONTRACTL

src/coreclr/vm/peimagelayout.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,10 @@ LoadedImageLayout::LoadedImageLayout(PEImage* pOwner, HRESULT* loadFailure)
534534

535535
IfFailThrow(Init(m_Module, true));
536536

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

539542
#else
540543
HANDLE hFile = pOwner->GetFileHandle();
@@ -548,8 +551,11 @@ LoadedImageLayout::LoadedImageLayout(PEImage* pOwner, HRESULT* loadFailure)
548551
return;
549552
}
550553

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

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

@@ -616,7 +622,10 @@ FlatImageLayout::FlatImageLayout(PEImage* pOwner)
616622
INT64 offset = pOwner->GetOffset();
617623
INT64 size = pOwner->GetSize();
618624

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

621630
// If a size is not specified, load the whole file
622631
if (size == 0)

src/coreclr/vm/readytoruninfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ static void LogR2r(const char *msg, PEAssembly *pPEAssembly)
429429
if (r2rLogFile == NULL)
430430
return;
431431

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

@@ -1904,7 +1905,7 @@ uint32_t ReadyToRun_TypeGenericInfoMap::GetGenericArgumentCount(mdTypeDef input,
19041905
uint32_t count = ((uint8_t)typeGenericInfo & (uint8_t)ReadyToRunTypeGenericInfo::GenericCountMask);
19051906
if (count > 2)
19061907
foundResult = false;
1907-
1908+
19081909
if (!foundResult)
19091910
{
19101911
HENUMInternalHolder hEnumTyPars(pImport);
@@ -1922,7 +1923,7 @@ HRESULT ReadyToRun_TypeGenericInfoMap::GetGenericArgumentCountNoThrow(mdTypeDef
19221923
uint32_t count = ((uint8_t)typeGenericInfo & (uint8_t)ReadyToRunTypeGenericInfo::GenericCountMask);
19231924
if (count > 2)
19241925
foundResult = false;
1925-
1926+
19261927
if (!foundResult)
19271928
{
19281929
HENUMInternalHolder hEnumTyPars(pImport);

0 commit comments

Comments
 (0)