From 00913ac19c46039e921552c1953537f55b48e2ad Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 21 Jun 2022 15:17:30 -0700 Subject: [PATCH 1/6] Change SString::GetUTF8NoConvert to GetUTF8 that converts the SString This enables SString to get out of the UTF16 state and helps move us away from "SString's natural encoding is UTF16" --- src/coreclr/debug/daccess/daccess.cpp | 2 +- src/coreclr/inc/sstring.h | 6 ++- src/coreclr/inc/sstring.inl | 19 ++++++++++ src/coreclr/utilcode/clrconfig.cpp | 4 +- src/coreclr/utilcode/debug.cpp | 8 ++-- src/coreclr/utilcode/sstring.cpp | 53 +++++++++++++++++---------- src/coreclr/vm/assemblynative.cpp | 9 ++--- src/coreclr/vm/class.cpp | 12 +++--- src/coreclr/vm/comcallablewrapper.cpp | 2 +- src/coreclr/vm/coreassemblyspec.cpp | 9 ++--- src/coreclr/vm/dllimport.cpp | 2 +- src/coreclr/vm/gdbjit.cpp | 4 +- src/coreclr/vm/jitinterface.cpp | 2 +- src/coreclr/vm/mlinfo.cpp | 2 +- src/coreclr/vm/multicorejitplayer.cpp | 2 +- src/coreclr/vm/pgo.cpp | 2 +- src/coreclr/vm/profilinghelper.cpp | 6 +-- src/coreclr/vm/stubgen.cpp | 10 ++--- 18 files changed, 93 insertions(+), 61 deletions(-) diff --git a/src/coreclr/debug/daccess/daccess.cpp b/src/coreclr/debug/daccess/daccess.cpp index 564aa84cd687e..55c7a4d0829f9 100644 --- a/src/coreclr/debug/daccess/daccess.cpp +++ b/src/coreclr/debug/daccess/daccess.cpp @@ -2501,7 +2501,7 @@ namespace serialization { namespace bin { return ErrOverflow; } - memcpy_s(dest, destSize, s.GetUTF8NoConvert(), cnt); + memcpy_s(dest, destSize, s.GetUTF8(), cnt); return cnt; } diff --git a/src/coreclr/inc/sstring.h b/src/coreclr/inc/sstring.h index 5f5eeb1078e00..56eee4bb1a4a6 100644 --- a/src/coreclr/inc/sstring.h +++ b/src/coreclr/inc/sstring.h @@ -553,8 +553,9 @@ class EMPTY_BASES_DECL SString : private SBuffer const UTF8 *GetUTF8(AbstractScratchBuffer &scratch, COUNT_T *pcbUtf8) const; const ANSI *GetANSI(AbstractScratchBuffer &scratch) const; - // Used when the representation is known, throws if the representation doesn't match - const UTF8 *GetUTF8NoConvert() const; + // You can always get a UTF8 string. This will force a conversion + // if necessary. + const UTF8 *GetUTF8() const; // Converts/copies into the given output string void ConvertToUnicode(SString &dest) const; @@ -779,6 +780,7 @@ class EMPTY_BASES_DECL SString : private SBuffer void ConvertASCIIToUnicode(SString &dest) const; void ConvertToUnicode() const; void ConvertToUnicode(const CIterator &i) const; + void ConvertToUTF8() const; const SString &GetCompatibleString(const SString &s, SString &scratch) const; const SString &GetCompatibleString(const SString &s, SString &scratch, const CIterator &i) const; diff --git a/src/coreclr/inc/sstring.inl b/src/coreclr/inc/sstring.inl index 03fc26fe9666c..1a18c4573dd74 100644 --- a/src/coreclr/inc/sstring.inl +++ b/src/coreclr/inc/sstring.inl @@ -651,6 +651,25 @@ inline const WCHAR *SString::GetUnicode() const SS_RETURN GetRawUnicode(); } +// Get a const pointer to the internal buffer as a UTF8 string. +inline const UTF8 *SString::GetUTF8() const +{ + SS_CONTRACT(const UTF8 *) + { + GC_NOTRIGGER; + PRECONDITION(CheckPointer(this)); + SS_POSTCONDITION(CheckPointer(RETVAL)); + if (IsRepresentation(REPRESENTATION_UTF8)) NOTHROW; else THROWS; + GC_NOTRIGGER; + SUPPORTS_DAC; + } + SS_CONTRACT_END; + + ConvertToUTF8(); + + SS_RETURN GetRawUTF8(); +} + // Normalize the string to unicode. This will make many operations nonfailing. inline void SString::Normalize() const { diff --git a/src/coreclr/utilcode/clrconfig.cpp b/src/coreclr/utilcode/clrconfig.cpp index aaa33a6e4b03f..d52679b9e078b 100644 --- a/src/coreclr/utilcode/clrconfig.cpp +++ b/src/coreclr/utilcode/clrconfig.cpp @@ -205,10 +205,10 @@ namespace SString valueAsUTF8; temp.ConvertToUTF8(valueAsUTF8); - CLRConfigNoCache nonCache = CLRConfigNoCache::Get(nameAsUTF8.GetUTF8NoConvert(), noPrefix); + CLRConfigNoCache nonCache = CLRConfigNoCache::Get(nameAsUTF8.GetUTF8(), noPrefix); LPCSTR valueNoCache = nonCache.AsString(); - _ASSERTE(SString::_stricmp(valueNoCache, valueAsUTF8.GetUTF8NoConvert()) == 0); + _ASSERTE(SString::_stricmp(valueNoCache, valueAsUTF8.GetUTF8()) == 0); #endif // defined(DEBUG) && !defined(SELF_NO_HOST) } } diff --git a/src/coreclr/utilcode/debug.cpp b/src/coreclr/utilcode/debug.cpp index 3f4c74d420f9d..e78de404115e5 100644 --- a/src/coreclr/utilcode/debug.cpp +++ b/src/coreclr/utilcode/debug.cpp @@ -364,7 +364,7 @@ bool _DbgBreakCheck( " Image: %s\n\n", GetCurrentProcessId(), GetCurrentProcessId(), GetCurrentThreadId(), GetCurrentThreadId(), - szExpr, szFile, iLine, modulePath.GetUTF8NoConvert()); + szExpr, szFile, iLine, modulePath.GetUTF8()); formattedMessages = TRUE; } @@ -680,11 +680,11 @@ void DECLSPEC_NORETURN __FreeBuildAssertFail(const char *szFile, int iLine, cons " File: %s, Line: %d Image:\n%s\n", GetCurrentProcessId(), GetCurrentProcessId(), GetCurrentThreadId(), GetCurrentThreadId(), - szExpr, szFile, iLine, modulePath.GetUTF8NoConvert()); - OutputDebugStringUtf8(buffer.GetUTF8NoConvert()); + szExpr, szFile, iLine, modulePath.GetUTF8()); + OutputDebugStringUtf8(buffer.GetUTF8()); // Write out the error to the console - printf(buffer.GetUTF8NoConvert()); + printf(buffer.GetUTF8()); // Log to the stress log. Note that we can't include the szExpr b/c that // may not be a string literal (particularly for formatt-able asserts). diff --git a/src/coreclr/utilcode/sstring.cpp b/src/coreclr/utilcode/sstring.cpp index ec3c340ff23d3..fcdcc4166ff78 100644 --- a/src/coreclr/utilcode/sstring.cpp +++ b/src/coreclr/utilcode/sstring.cpp @@ -777,6 +777,39 @@ void SString::ConvertToUnicode(const CIterator &i) const RETURN; } +//----------------------------------------------------------------------------- +// Convert the internal representation for this String to Unicode. +//----------------------------------------------------------------------------- +void SString::ConvertToUTF8() const +{ + CONTRACT_VOID + { + POSTCONDITION(IsRepresentation(REPRESENTATION_UTF8)); + if (IsRepresentation(REPRESENTATION_UTF8)) NOTHROW; else THROWS; + GC_NOTRIGGER; + SUPPORTS_DAC_HOST_ONLY; + } + CONTRACT_END; + + if (!IsRepresentation(REPRESENTATION_UTF8)) + { + if (IsRepresentation(REPRESENTATION_ASCII)) + { + // ASCII is a subset of UTF8, so we can just set the representation. + (const_cast(this))->SetRepresentation(REPRESENTATION_UTF8); + } + else + { + StackSString s; + ConvertToUTF8(s); + PREFIX_ASSUME(!s.IsImmutable()); + (const_cast(this))->Set(s); + } + } + + RETURN; +} + //----------------------------------------------------------------------------- // Set s to be a copy of this string's contents, but in the unicode format. //----------------------------------------------------------------------------- @@ -1827,26 +1860,6 @@ const UTF8 *SString::GetUTF8(AbstractScratchBuffer &scratch, COUNT_T *pcbUtf8) c RETURN ((SString&)scratch).GetRawUTF8(); } -//----------------------------------------------------------------------------- -// Get a const pointer to the internal buffer which must already be a UTF8 string. -// This avoids the need to create a scratch buffer we know will never be used. -//----------------------------------------------------------------------------- -const UTF8 *SString::GetUTF8NoConvert() const -{ - CONTRACT(const UTF8 *) - { - INSTANCE_CHECK_NULL; - THROWS; - GC_NOTRIGGER; - } - CONTRACT_END; - - if (IsRepresentation(REPRESENTATION_UTF8)) - RETURN GetRawUTF8(); - - ThrowHR(E_INVALIDARG); -} - //----------------------------------------------------------------------------- // Safe version of sprintf. // Prints formatted ansi text w/ var args to this buffer. diff --git a/src/coreclr/vm/assemblynative.cpp b/src/coreclr/vm/assemblynative.cpp index 09b9932af764d..9c34f5d5d03f3 100644 --- a/src/coreclr/vm/assemblynative.cpp +++ b/src/coreclr/vm/assemblynative.cpp @@ -75,8 +75,7 @@ extern "C" void QCALLTYPE AssemblyNative_InternalLoad(NativeAssemblyNameParts* p if (pAssemblyNameParts->_pName == NULL) COMPlusThrow(kArgumentException, W("Format_StringZeroLength")); - StackSString ssName; - SString(SString::Literal, pAssemblyNameParts->_pName).ConvertToUTF8(ssName); + StackSString ssName(SString::Literal, pAssemblyNameParts->_pName); AssemblyMetaDataInternal asmInfo; @@ -87,11 +86,11 @@ extern "C" void QCALLTYPE AssemblyNative_InternalLoad(NativeAssemblyNameParts* p SmallStackSString ssLocale; if (pAssemblyNameParts->_pCultureName != NULL) - SString(SString::Literal, pAssemblyNameParts->_pCultureName).ConvertToUTF8(ssLocale); - asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8NoConvert() : NULL; + ssLocale.SetLiteral(pAssemblyNameParts->_pCultureName); + asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8() : NULL; // Initialize spec - spec.Init(ssName.GetUTF8NoConvert(), &asmInfo, + spec.Init(ssName.GetUTF8(), &asmInfo, pAssemblyNameParts->_pPublicKeyOrToken, pAssemblyNameParts->_cbPublicKeyOrToken, pAssemblyNameParts->_flags); if (pParentAssembly != NULL) diff --git a/src/coreclr/vm/class.cpp b/src/coreclr/vm/class.cpp index 118dc8c6e846e..a82a80f372902 100644 --- a/src/coreclr/vm/class.cpp +++ b/src/coreclr/vm/class.cpp @@ -2236,7 +2236,7 @@ void MethodTable::DebugRecursivelyDumpInstanceFields(LPCUTF8 pszClassName, BOOL // Display them if(debug) { ssBuff.Printf("%s:\n", pszClassName); - OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert()); + OutputDebugStringUtf8(ssBuff.GetUTF8()); } else { LOG((LF_CLASSLOADER, LL_ALWAYS, "%s:\n", pszClassName)); @@ -2250,7 +2250,7 @@ void MethodTable::DebugRecursivelyDumpInstanceFields(LPCUTF8 pszClassName, BOOL #endif if(debug) { ssBuff.Printf("offset %3d %s\n", pFD->GetOffset_NoLogging(), pFD->GetName()); - OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert()); + OutputDebugStringUtf8(ssBuff.GetUTF8()); } else { LOG((LF_CLASSLOADER, LL_ALWAYS, "offset %3d %s\n", pFD->GetOffset_NoLogging(), pFD->GetName())); @@ -2299,7 +2299,7 @@ void MethodTable::DebugDumpFieldLayout(LPCUTF8 pszClassName, BOOL debug) if (debug) { ssBuff.Printf("Field layout for '%s':\n\n", pszClassName); - OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert()); + OutputDebugStringUtf8(ssBuff.GetUTF8()); } else { @@ -2326,7 +2326,7 @@ void MethodTable::DebugDumpFieldLayout(LPCUTF8 pszClassName, BOOL debug) FieldDesc *pFD = GetClass()->GetFieldDescList() + ((GetNumInstanceFields()-cParentInstanceFields) + i); if(debug) { ssBuff.Printf("offset %3d %s\n", pFD->GetOffset_NoLogging(), pFD->GetName()); - OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert()); + OutputDebugStringUtf8(ssBuff.GetUTF8()); } else { @@ -2404,7 +2404,7 @@ MethodTable::DebugDumpGCDesc( if (fDebug) { ssBuff.Printf("GC description for '%s':\n\n", pszClassName); - OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert()); + OutputDebugStringUtf8(ssBuff.GetUTF8()); } else { @@ -2438,7 +2438,7 @@ MethodTable::DebugDumpGCDesc( pSeries->GetSeriesOffset() - OBJECT_SIZE, pSeries->GetSeriesSize(), pSeries->GetSeriesSize() + GetBaseSize() ); - OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert()); + OutputDebugStringUtf8(ssBuff.GetUTF8()); } else { diff --git a/src/coreclr/vm/comcallablewrapper.cpp b/src/coreclr/vm/comcallablewrapper.cpp index bd3c48ed443fe..330858a4fbadf 100644 --- a/src/coreclr/vm/comcallablewrapper.cpp +++ b/src/coreclr/vm/comcallablewrapper.cpp @@ -734,7 +734,7 @@ void SimpleComCallWrapper::LogRefCount(ComCallWrapper *pWrap, StackSString &ssMe EX_TRY { ssMessage.AppendPrintf(", RefCount=%u\n", dwRefCountToLog); - OutputDebugStringUtf8(ssMessage.GetUTF8NoConvert()); + OutputDebugStringUtf8(ssMessage.GetUTF8()); } EX_CATCH { } diff --git a/src/coreclr/vm/coreassemblyspec.cpp b/src/coreclr/vm/coreassemblyspec.cpp index 3b8d0c0e8e533..92fc2295aa989 100644 --- a/src/coreclr/vm/coreassemblyspec.cpp +++ b/src/coreclr/vm/coreassemblyspec.cpp @@ -185,8 +185,7 @@ extern "C" void QCALLTYPE AssemblyName_InitializeAssemblySpec(NativeAssemblyName BEGIN_QCALL; - StackSString ssName; - SString(SString::Literal, pAssemblyNameParts->_pName).ConvertToUTF8(ssName); + StackSString ssName(SString::Literal, pAssemblyNameParts->_pName); AssemblyMetaDataInternal asmInfo; @@ -197,11 +196,11 @@ extern "C" void QCALLTYPE AssemblyName_InitializeAssemblySpec(NativeAssemblyName SmallStackSString ssLocale; if (pAssemblyNameParts->_pCultureName != NULL) - SString(SString::Literal, pAssemblyNameParts->_pCultureName).ConvertToUTF8(ssLocale); - asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8NoConvert() : NULL; + ssLocale.SetLiteral(pAssemblyNameParts->_pCultureName); + asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8() : NULL; // Initialize spec - pAssemblySpec->Init(ssName.GetUTF8NoConvert(), &asmInfo, + pAssemblySpec->Init(ssName.GetUTF8(), &asmInfo, pAssemblyNameParts->_pPublicKeyOrToken, pAssemblyNameParts->_cbPublicKeyOrToken, pAssemblyNameParts->_flags); // Copy and own any fields we do not own diff --git a/src/coreclr/vm/dllimport.cpp b/src/coreclr/vm/dllimport.cpp index dceb6f6b89f7f..50e3f513331fc 100644 --- a/src/coreclr/vm/dllimport.cpp +++ b/src/coreclr/vm/dllimport.cpp @@ -1010,7 +1010,7 @@ class ILStubState : public StubState strILStubCode.AppendPrintf("// Code size\t%d (0x%04x)\n", cbCode, cbCode); strILStubCode.AppendPrintf(".maxstack %d \n", maxStack); - strILStubCode.AppendPrintf(".locals %s\n", strLocalSig.GetUTF8NoConvert()); + strILStubCode.AppendPrintf(".locals %s\n", strLocalSig.GetUTF8()); m_slIL.LogILStub(jitFlags, &strILStubCode); diff --git a/src/coreclr/vm/gdbjit.cpp b/src/coreclr/vm/gdbjit.cpp index 525ca50a28c2b..8447c024de380 100644 --- a/src/coreclr/vm/gdbjit.cpp +++ b/src/coreclr/vm/gdbjit.cpp @@ -1301,8 +1301,8 @@ void FunctionMember::DumpLinkageName(char* ptr, int& offset) namespaceOrClassName.ConvertToUTF8(utf8namespaceOrClassName); methodName.ConvertToUTF8(utf8methodName); - const char *nspace = utf8namespaceOrClassName.GetUTF8NoConvert(); - const char *mname = utf8methodName.GetUTF8NoConvert(); + const char *nspace = utf8namespaceOrClassName.GetUTF8(); + const char *mname = utf8methodName.GetUTF8(); if (!nspace || !mname) { diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index cee3f0d26248a..c6d91f521170b 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -12937,7 +12937,7 @@ PCODE UnsafeJitFunction(PrepareCodeConfig* config, (unsigned)(methodJitTimeStop.QuadPart - methodJitTimeStart.QuadPart), //cycle count methodInfo.ILCodeSize //il size ); - OutputDebugStringUtf8(codeBase.GetUTF8NoConvert()); + OutputDebugStringUtf8(codeBase.GetUTF8()); } #endif // PERF_TRACK_METHOD_JITTIMES diff --git a/src/coreclr/vm/mlinfo.cpp b/src/coreclr/vm/mlinfo.cpp index 471cbb1165aaf..e9ac852c8191f 100644 --- a/src/coreclr/vm/mlinfo.cpp +++ b/src/coreclr/vm/mlinfo.cpp @@ -916,7 +916,7 @@ CustomMarshalerHelper *EEMarshalingData::GetCustomMarshalerHelper(Assembly *pAss // Load the custom marshaler class. BOOL fNameIsAsmQualified = FALSE; - hndCustomMarshalerType = TypeName::GetTypeUsingCASearchRules(strCMMarshalerTypeName.GetUTF8NoConvert(), pAssembly, &fNameIsAsmQualified); + hndCustomMarshalerType = TypeName::GetTypeUsingCASearchRules(strCMMarshalerTypeName.GetUTF8(), pAssembly, &fNameIsAsmQualified); if (hndCustomMarshalerType.IsGenericTypeDefinition()) { diff --git a/src/coreclr/vm/multicorejitplayer.cpp b/src/coreclr/vm/multicorejitplayer.cpp index dd48014046446..46e2c6f721211 100644 --- a/src/coreclr/vm/multicorejitplayer.cpp +++ b/src/coreclr/vm/multicorejitplayer.cpp @@ -318,7 +318,7 @@ void PlayerModuleInfo::Dump(const CHAR * prefix, int index) i ++; } - MulticoreJitTrace(("%s", ssBuff.GetUTF8NoConvert())); + MulticoreJitTrace(("%s", ssBuff.GetUTF8())); } #endif diff --git a/src/coreclr/vm/pgo.cpp b/src/coreclr/vm/pgo.cpp index a4bad1f5c6dff..d1e2f91954ef4 100644 --- a/src/coreclr/vm/pgo.cpp +++ b/src/coreclr/vm/pgo.cpp @@ -871,7 +871,7 @@ HRESULT PgoManager::getPgoInstrumentationResults(MethodDesc* pMD, BYTE** pAlloca TypeHandle th = TypeName::GetTypeManaged(typeString.GetUnicode(), NULL, FALSE, FALSE, FALSE, NULL, NULL); if (!th.IsNull()) { - MethodDesc* pMD = MemberLoader::FindMethodByName(th.GetMethodTable(), methodString.GetUTF8NoConvert()); + MethodDesc* pMD = MemberLoader::FindMethodByName(th.GetMethodTable(), methodString.GetUTF8()); newPtr = (INT_PTR)pMD; } } diff --git a/src/coreclr/vm/profilinghelper.cpp b/src/coreclr/vm/profilinghelper.cpp index 285bcaab9887c..20d3cbf175f24 100644 --- a/src/coreclr/vm/profilinghelper.cpp +++ b/src/coreclr/vm/profilinghelper.cpp @@ -260,7 +260,7 @@ void ProfilingAPIUtility::AppendSupplementaryInformation(int iStringResource, SS pString->AppendUTF8(" "); pString->AppendPrintf( - supplementaryInformationUtf8.GetUTF8NoConvert(), + supplementaryInformationUtf8.GetUTF8(), GetCurrentProcessId(), iStringResource); } @@ -311,7 +311,7 @@ void ProfilingAPIUtility::LogProfEventVA( messageFromResource.ConvertToUTF8(messageFromResourceUtf8); StackSString messageToLog; - messageToLog.VPrintf(messageFromResourceUtf8.GetUTF8NoConvert(), insertionArgs); + messageToLog.VPrintf(messageFromResourceUtf8.GetUTF8(), insertionArgs); AppendSupplementaryInformation(iStringResourceID, &messageToLog); @@ -325,7 +325,7 @@ void ProfilingAPIUtility::LogProfEventVA( } // Ouput debug strings for diagnostic messages. - OutputDebugStringUtf8(messageToLog.GetUTF8NoConvert()); + OutputDebugStringUtf8(messageToLog.GetUTF8()); } // See code:ProfilingAPIUtility.LogProfEventVA for description of arguments. diff --git a/src/coreclr/vm/stubgen.cpp b/src/coreclr/vm/stubgen.cpp index a1dae94859cc0..69da400c09cbf 100644 --- a/src/coreclr/vm/stubgen.cpp +++ b/src/coreclr/vm/stubgen.cpp @@ -109,7 +109,7 @@ void ILStubLinker::DumpIL_FormatToken(mdToken token, SString &strTokenFormatting SString typeNameUtf8; typeName.ConvertToUTF8(typeNameUtf8); SString strFieldName(SString::Utf8, pFD->GetName()); - strTokenFormatting.Printf("%s::%s", typeNameUtf8.GetUTF8NoConvert(), strFieldName.GetUTF8NoConvert()); + strTokenFormatting.Printf("%s::%s", typeNameUtf8.GetUTF8(), strFieldName.GetUTF8()); } else if (TypeFromToken(token) == mdtModule) { @@ -550,13 +550,13 @@ ILStubLinker::LogILInstruction( // if (pDumpILStubCode) { - pDumpILStubCode->AppendPrintf("%s /*(%2d)*/ %s %s %s\n", strLabel.GetUTF8NoConvert(), iCurStack, strOpcode.GetUTF8NoConvert(), - strArgument.GetUTF8NoConvert(), strTokenName.GetUTF8NoConvert()); + pDumpILStubCode->AppendPrintf("%s /*(%2d)*/ %s %s %s\n", strLabel.GetUTF8(), iCurStack, strOpcode.GetUTF8(), + strArgument.GetUTF8(), strTokenName.GetUTF8()); } else { - LOG((LF_STUBS, LL_INFO1000, "%s (%2d) %s %s %s\n", strLabel.GetUTF8NoConvert(), iCurStack, \ - strOpcode.GetUTF8NoConvert(), strArgument.GetUTF8NoConvert(), strTokenName.GetUTF8NoConvert())); + LOG((LF_STUBS, LL_INFO1000, "%s (%2d) %s %s %s\n", strLabel.GetUTF8(), iCurStack, \ + strOpcode.GetUTF8(), strArgument.GetUTF8(), strTokenName.GetUTF8())); } } // ILStubLinker::LogILInstruction From 3d8d5103be45fed18dcaea5f37a8473a558f91d6 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 21 Jun 2022 15:50:42 -0700 Subject: [PATCH 2/6] Remove some stack scratch buffers that are unneeded now (as we can now convert the SString itself and in some cases it was already in UTF8) and implement PR feedback. --- src/coreclr/vm/array.cpp | 9 +++------ src/coreclr/vm/assembly.cpp | 3 +-- src/coreclr/vm/assemblynative.cpp | 4 ++-- src/coreclr/vm/classhash.cpp | 6 ++---- src/coreclr/vm/clsload.cpp | 6 ++---- src/coreclr/vm/codepitchingmanager.cpp | 5 ++--- src/coreclr/vm/comdelegate.cpp | 3 +-- src/coreclr/vm/coreassemblyspec.cpp | 4 ++-- src/coreclr/vm/corhost.cpp | 6 ++---- src/coreclr/vm/customattribute.cpp | 3 +-- src/coreclr/vm/gdbjit.cpp | 11 +++-------- src/coreclr/vm/generics.cpp | 8 +++----- src/coreclr/vm/jitinterface.cpp | 3 +-- src/coreclr/vm/method.cpp | 8 +++----- src/coreclr/vm/methodtablebuilder.cpp | 3 +-- src/coreclr/vm/mlinfo.cpp | 3 +-- src/coreclr/vm/multicorejit.cpp | 3 +-- src/coreclr/vm/nativelibrary.cpp | 3 +-- src/coreclr/vm/pgo.cpp | 13 ++++--------- src/coreclr/vm/stubgen.cpp | 5 +---- src/coreclr/vm/typeparse.cpp | 3 +-- 21 files changed, 38 insertions(+), 74 deletions(-) diff --git a/src/coreclr/vm/array.cpp b/src/coreclr/vm/array.cpp index 69617911e0359..aae2b2fb6c325 100644 --- a/src/coreclr/vm/array.cpp +++ b/src/coreclr/vm/array.cpp @@ -402,8 +402,7 @@ MethodTable* Module::CreateArrayMethodTable(TypeHandle elemTypeHnd, CorElementTy StackSString ssElemName; elemTypeHnd.GetName(ssElemName); - StackScratchBuffer scratch; - elemTypeHnd.GetAssembly()->ThrowTypeLoadException(ssElemName.GetUTF8(scratch), IDS_CLASSLOAD_VALUECLASSTOOLARGE); + elemTypeHnd.GetAssembly()->ThrowTypeLoadException(ssElemName.GetUTF8(), IDS_CLASSLOAD_VALUECLASSTOOLARGE); } } @@ -510,8 +509,7 @@ MethodTable* Module::CreateArrayMethodTable(TypeHandle elemTypeHnd, CorElementTy #ifdef _DEBUG StackSString debugName; TypeString::AppendType(debugName, TypeHandle(pMT)); - StackScratchBuffer buff; - const char* pDebugNameUTF8 = debugName.GetUTF8(buff); + const char* pDebugNameUTF8 = debugName.GetUTF8(); S_SIZE_T safeLen = S_SIZE_T(strlen(pDebugNameUTF8))+S_SIZE_T(1); if(safeLen.IsOverflow()) COMPlusThrowHR(COR_E_OVERFLOW); size_t len = safeLen.Value(); @@ -657,8 +655,7 @@ MethodTable* Module::CreateArrayMethodTable(TypeHandle elemTypeHnd, CorElementTy StackSString ssElemName; elemTypeHnd.GetName(ssElemName); - StackScratchBuffer scratch; - elemTypeHnd.GetAssembly()->ThrowTypeLoadException(ssElemName.GetUTF8(scratch), + elemTypeHnd.GetAssembly()->ThrowTypeLoadException(ssElemName.GetUTF8(), IDS_CLASSLOAD_VALUECLASSTOOLARGE); } diff --git a/src/coreclr/vm/assembly.cpp b/src/coreclr/vm/assembly.cpp index 83944d9f9db76..7d3c27de2c26b 100644 --- a/src/coreclr/vm/assembly.cpp +++ b/src/coreclr/vm/assembly.cpp @@ -1004,8 +1004,7 @@ Module *Assembly::FindModuleByName(LPCSTR pszModuleName) SString moduleName(SString::Utf8, pszModuleName); moduleName.LowerCase(); - StackScratchBuffer buffer; - pszModuleName = moduleName.GetUTF8(buffer); + pszModuleName = moduleName.GetUTF8(); mdFile kFile = GetManifestFileToken(pszModuleName); if (kFile == mdTokenNil) diff --git a/src/coreclr/vm/assemblynative.cpp b/src/coreclr/vm/assemblynative.cpp index 9c34f5d5d03f3..a50ac0415dea7 100644 --- a/src/coreclr/vm/assemblynative.cpp +++ b/src/coreclr/vm/assemblynative.cpp @@ -75,7 +75,7 @@ extern "C" void QCALLTYPE AssemblyNative_InternalLoad(NativeAssemblyNameParts* p if (pAssemblyNameParts->_pName == NULL) COMPlusThrow(kArgumentException, W("Format_StringZeroLength")); - StackSString ssName(SString::Literal, pAssemblyNameParts->_pName); + StackSString ssName(pAssemblyNameParts->_pName); AssemblyMetaDataInternal asmInfo; @@ -86,7 +86,7 @@ extern "C" void QCALLTYPE AssemblyNative_InternalLoad(NativeAssemblyNameParts* p SmallStackSString ssLocale; if (pAssemblyNameParts->_pCultureName != NULL) - ssLocale.SetLiteral(pAssemblyNameParts->_pCultureName); + ssLocale.Set(pAssemblyNameParts->_pCultureName); asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8() : NULL; // Initialize spec diff --git a/src/coreclr/vm/classhash.cpp b/src/coreclr/vm/classhash.cpp index 005a392565731..0731843d1d422 100644 --- a/src/coreclr/vm/classhash.cpp +++ b/src/coreclr/vm/classhash.cpp @@ -205,14 +205,12 @@ static void ConstructKeyFromDataCaseInsensitive(EEClassHashTable::ConstructKeyCa StackSString nameSpace(SString::Utf8, pszNameSpace); nameSpace.LowerCase(); - StackScratchBuffer nameSpaceBuffer; - Key[0] = (LPUTF8)nameSpace.GetUTF8(nameSpaceBuffer); + Key[0] = (LPUTF8)nameSpace.GetUTF8(); StackSString name(SString::Utf8, pszName); name.LowerCase(); - StackScratchBuffer nameBuffer; - Key[1] = (LPUTF8)name.GetUTF8(nameBuffer); + Key[1] = (LPUTF8)name.GetUTF8(); pCallback->UseKeys(Key); } diff --git a/src/coreclr/vm/clsload.cpp b/src/coreclr/vm/clsload.cpp index 427f73f46a9f1..c3e08ccf1061a 100644 --- a/src/coreclr/vm/clsload.cpp +++ b/src/coreclr/vm/clsload.cpp @@ -1796,15 +1796,13 @@ VOID ClassLoader::CreateCanonicallyCasedKey(LPCUTF8 pszNameSpace, LPCUTF8 pszNam StackSString nameSpace(SString::Utf8, pszNameSpace); nameSpace.LowerCase(); - StackScratchBuffer nameSpaceBuffer; - pszNameSpace = nameSpace.GetUTF8(nameSpaceBuffer); + pszNameSpace = nameSpace.GetUTF8(); StackSString name(SString::Utf8, pszName); name.LowerCase(); - StackScratchBuffer nameBuffer; - pszName = name.GetUTF8(nameBuffer); + pszName = name.GetUTF8(); size_t iNSLength = strlen(pszNameSpace); diff --git a/src/coreclr/vm/codepitchingmanager.cpp b/src/coreclr/vm/codepitchingmanager.cpp index 45bac532a317c..f4e8cdcac1cac 100644 --- a/src/coreclr/vm/codepitchingmanager.cpp +++ b/src/coreclr/vm/codepitchingmanager.cpp @@ -232,9 +232,8 @@ static void LookupOrCreateInPitchingCandidate(MethodDesc* pMD, ULONG sizeOfCode) SString className, methodName, methodSig; pMD->GetMethodInfo(className, methodName, methodSig); - StackScratchBuffer scratch; - const char* szClassName = className.GetUTF8(scratch); - const char* szMethodSig = methodSig.GetUTF8(scratch); + const char* szClassName = className.GetUTF8(); + const char* szMethodSig = methodSig.GetUTF8(); printf("Candidate %lu %s :: %s %s\n", sizeOfCode, szClassName, pMD->GetName(), szMethodSig); diff --git a/src/coreclr/vm/comdelegate.cpp b/src/coreclr/vm/comdelegate.cpp index 235e79ad58efa..1a3f636e7b0ba 100644 --- a/src/coreclr/vm/comdelegate.cpp +++ b/src/coreclr/vm/comdelegate.cpp @@ -925,8 +925,7 @@ FCIMPL5(FC_BOOL_RET, COMDelegate::BindToMethodName, // get the name in UTF8 format SString wszName(SString::Literal, gc.methodName->GetBuffer()); - StackScratchBuffer utf8Name; - LPCUTF8 szNameStr = wszName.GetUTF8(utf8Name); + LPCUTF8 szNameStr = wszName.GetUTF8(); // pick a proper compare function typedef int (__cdecl *UTF8StringCompareFuncPtr)(const char *, const char *); diff --git a/src/coreclr/vm/coreassemblyspec.cpp b/src/coreclr/vm/coreassemblyspec.cpp index 92fc2295aa989..cd58c5a2c8a21 100644 --- a/src/coreclr/vm/coreassemblyspec.cpp +++ b/src/coreclr/vm/coreassemblyspec.cpp @@ -185,7 +185,7 @@ extern "C" void QCALLTYPE AssemblyName_InitializeAssemblySpec(NativeAssemblyName BEGIN_QCALL; - StackSString ssName(SString::Literal, pAssemblyNameParts->_pName); + StackSString ssName(pAssemblyNameParts->_pName); AssemblyMetaDataInternal asmInfo; @@ -196,7 +196,7 @@ extern "C" void QCALLTYPE AssemblyName_InitializeAssemblySpec(NativeAssemblyName SmallStackSString ssLocale; if (pAssemblyNameParts->_pCultureName != NULL) - ssLocale.SetLiteral(pAssemblyNameParts->_pCultureName); + ssLocale.Set(pAssemblyNameParts->_pCultureName); asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8() : NULL; // Initialize spec diff --git a/src/coreclr/vm/corhost.cpp b/src/coreclr/vm/corhost.cpp index cc9fc82db7200..e1babc7f6228e 100644 --- a/src/coreclr/vm/corhost.cpp +++ b/src/coreclr/vm/corhost.cpp @@ -426,15 +426,13 @@ HRESULT CorHost2::ExecuteInDefaultAppDomain(LPCWSTR pwzAssemblyPath, Assembly *pAssembly = AssemblySpec::LoadAssembly(pwzAssemblyPath); SString szTypeName(pwzTypeName); - StackScratchBuffer buff1; - const char* szTypeNameUTF8 = szTypeName.GetUTF8(buff1); + const char* szTypeNameUTF8 = szTypeName.GetUTF8(); MethodTable *pMT = ClassLoader::LoadTypeByNameThrowing(pAssembly, NULL, szTypeNameUTF8).AsMethodTable(); SString szMethodName(pwzMethodName); - StackScratchBuffer buff; - const char* szMethodNameUTF8 = szMethodName.GetUTF8(buff); + const char* szMethodNameUTF8 = szMethodName.GetUTF8(); MethodDesc *pMethodMD = MemberLoader::FindMethod(pMT, szMethodNameUTF8, &gsig_SM_Str_RetInt); if (!pMethodMD) diff --git a/src/coreclr/vm/customattribute.cpp b/src/coreclr/vm/customattribute.cpp index 00c9150cddd71..00d27e8ea0e91 100644 --- a/src/coreclr/vm/customattribute.cpp +++ b/src/coreclr/vm/customattribute.cpp @@ -33,9 +33,8 @@ TypeHandle Attribute::GetTypeForEnum(LPCUTF8 szEnumName, COUNT_T cbEnumName, Dom } CONTRACTL_END; - StackScratchBuffer buff; StackSString sszEnumName(SString::Utf8, szEnumName, cbEnumName); - return TypeName::GetTypeUsingCASearchRules(sszEnumName.GetUTF8(buff), pDomainAssembly->GetAssembly()); + return TypeName::GetTypeUsingCASearchRules(sszEnumName.GetUTF8(), pDomainAssembly->GetAssembly()); } /*static*/ diff --git a/src/coreclr/vm/gdbjit.cpp b/src/coreclr/vm/gdbjit.cpp index 8447c024de380..eac4f9429e0b7 100644 --- a/src/coreclr/vm/gdbjit.cpp +++ b/src/coreclr/vm/gdbjit.cpp @@ -512,8 +512,7 @@ GetDebugInfoFromPDB(MethodDesc* methodDescPtr, if (modName.IsEmpty()) return E_FAIL; - StackScratchBuffer scratch; - const char* szModName = modName.GetUTF8(scratch); + const char* szModName = modName.GetUTF8(); MethodDebugInfo methodDebugInfo(numMap, locals.countVars); @@ -956,8 +955,7 @@ void TypeInfoBase::CalculateName() TypeString::AppendType(sName, typeHandle, formatFlags); - StackScratchBuffer buffer; - const UTF8 *utf8 = sName.GetUTF8(buffer); + const UTF8 *utf8 = sName.GetUTF8(); if (typeHandle.IsValueType()) { m_type_name = new char[strlen(utf8) + 1]; @@ -1298,8 +1296,6 @@ void FunctionMember::DumpLinkageName(char* ptr, int& offset) md->GetMethodInfoNoSig(namespaceOrClassName, methodName); SString utf8namespaceOrClassName; SString utf8methodName; - namespaceOrClassName.ConvertToUTF8(utf8namespaceOrClassName); - methodName.ConvertToUTF8(utf8methodName); const char *nspace = utf8namespaceOrClassName.GetUTF8(); const char *mname = utf8methodName.GetUTF8(); @@ -2538,8 +2534,7 @@ void NotifyGdb::OnMethodPrepared(MethodDesc* methodDescPtr) /* Get module name */ const Module* mod = methodDescPtr->GetMethodTable()->GetModule(); SString modName = mod->GetFile()->GetPath(); - StackScratchBuffer scratch; - const char* szModName = modName.GetUTF8(scratch); + const char* szModName = modName.GetUTF8(); const char* szModuleFile = SplitFilename(szModName); int length = MultiByteToWideChar(CP_UTF8, 0, szModuleFile, -1, NULL, 0); diff --git a/src/coreclr/vm/generics.cpp b/src/coreclr/vm/generics.cpp index fea2ce22645fd..55f1d274990c5 100644 --- a/src/coreclr/vm/generics.cpp +++ b/src/coreclr/vm/generics.cpp @@ -187,9 +187,8 @@ ClassLoader::CreateTypeHandleForNonCanonicalGenericInstantiation( TypeString::AppendTypeKeyDebug(debugTypeKeyName, pTypeKey); LOG((LF_CLASSLOADER, LL_INFO1000, "GENERICS: New instantiation requested: %S\n", debugTypeKeyName.GetUnicode())); - StackScratchBuffer buf; - if (g_pConfig->ShouldBreakOnInstantiation(debugTypeKeyName.GetUTF8(buf))) - CONSISTENCY_CHECK_MSGF(false, ("BreakOnInstantiation: typename '%s' ", debugTypeKeyName.GetUTF8(buf))); + if (g_pConfig->ShouldBreakOnInstantiation(debugTypeKeyName.GetUTF8())) + CONSISTENCY_CHECK_MSGF(false, ("BreakOnInstantiation: typename '%s' ", debugTypeKeyName.GetUTF8())); } #endif // _DEBUG @@ -474,8 +473,7 @@ ClassLoader::CreateTypeHandleForNonCanonicalGenericInstantiation( // Name for debugging StackSString debug_ClassNameString; TypeString::AppendTypeKey(debug_ClassNameString, pTypeKey, TypeString::FormatNamespace | TypeString::FormatAngleBrackets | TypeString::FormatFullInst); - StackScratchBuffer debug_ClassNameBuffer; - const char *debug_szClassNameBuffer = debug_ClassNameString.GetUTF8(debug_ClassNameBuffer); + const char *debug_szClassNameBuffer = debug_ClassNameString.GetUTF8(); S_SIZE_T safeLen = S_SIZE_T(strlen(debug_szClassNameBuffer)) + S_SIZE_T(1); if (safeLen.IsOverflow()) COMPlusThrowHR(COR_E_OVERFLOW); diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index c6d91f521170b..a4f53f19f70fd 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -12928,11 +12928,10 @@ PCODE UnsafeJitFunction(PrepareCodeConfig* config, SString moduleName; ftn->GetModule()->GetDomainAssembly()->GetPEAssembly()->GetPathOrCodeBase(moduleName); - MAKE_UTF8PTR_FROMWIDE(moduleNameUtf8, moduleName.GetUnicode()); SString codeBase; codeBase.AppendPrintf("%s,0x%x,%d,%d\n", - moduleNameUtf8, //module name + moduleName.GetUTF8(), //module name ftn->GetMemberDef(), //method token (unsigned)(methodJitTimeStop.QuadPart - methodJitTimeStart.QuadPart), //cycle count methodInfo.ILCodeSize //il size diff --git a/src/coreclr/vm/method.cpp b/src/coreclr/vm/method.cpp index 221be23cd4877..fef201a83cd99 100644 --- a/src/coreclr/vm/method.cpp +++ b/src/coreclr/vm/method.cpp @@ -353,17 +353,15 @@ VOID MethodDesc::GetFullMethodInfo(SString& fullMethodSigName) PCCOR_SIGNATURE pSig; SString methodFullName; - StackScratchBuffer namespaceNameBuffer, methodNameBuffer; methodFullName.AppendPrintf( (LPCUTF8)"[%s] %s::%s", GetModule()->GetAssembly()->GetSimpleName(), - namespaceOrClassName.GetUTF8(namespaceNameBuffer), - methodName.GetUTF8(methodNameBuffer)); + namespaceOrClassName.GetUTF8(), + methodName.GetUTF8()); GetSig(&pSig, &cSig); - StackScratchBuffer buffer; - PrettyPrintSig(pSig, (DWORD)cSig, methodFullName.GetUTF8(buffer), &qbOut, GetMDImport(), NULL); + PrettyPrintSig(pSig, (DWORD)cSig, methodFullName.GetUTF8(), &qbOut, GetMDImport(), NULL); fullMethodSigName.AppendUTF8((char *)qbOut.Ptr()); } diff --git a/src/coreclr/vm/methodtablebuilder.cpp b/src/coreclr/vm/methodtablebuilder.cpp index ffb90a935cacf..86f2cbfa199ff 100644 --- a/src/coreclr/vm/methodtablebuilder.cpp +++ b/src/coreclr/vm/methodtablebuilder.cpp @@ -1349,8 +1349,7 @@ MethodTableBuilder::BuildMethodTableThrowing( { StackSString debugName(SString::Utf8, GetDebugClassName()); TypeString::AppendInst(debugName, bmtGenerics->GetInstantiation(), TypeString::FormatBasic); - StackScratchBuffer buff; - const char* pDebugNameUTF8 = debugName.GetUTF8(buff); + const char* pDebugNameUTF8 = debugName.GetUTF8(); S_SIZE_T safeLen = S_SIZE_T(strlen(pDebugNameUTF8)) + S_SIZE_T(1); if(safeLen.IsOverflow()) COMPlusThrowHR(COR_E_OVERFLOW); diff --git a/src/coreclr/vm/mlinfo.cpp b/src/coreclr/vm/mlinfo.cpp index e9ac852c8191f..8984b8e4e9ed8 100644 --- a/src/coreclr/vm/mlinfo.cpp +++ b/src/coreclr/vm/mlinfo.cpp @@ -2067,9 +2067,8 @@ MarshalInfo::MarshalInfo(Module* pModule, { // Load the type. Use an SString for the string since we need to NULL terminate the string // that comes from the metadata. - StackScratchBuffer utf8Name; SString safeArrayUserDefTypeName(SString::Utf8, ParamInfo.m_strSafeArrayUserDefTypeName, ParamInfo.m_cSafeArrayUserDefTypeNameBytes); - thElement = TypeName::GetTypeUsingCASearchRules(safeArrayUserDefTypeName.GetUTF8(utf8Name), pAssembly); + thElement = TypeName::GetTypeUsingCASearchRules(safeArrayUserDefTypeName.GetUTF8(), pAssembly); } } else diff --git a/src/coreclr/vm/multicorejit.cpp b/src/coreclr/vm/multicorejit.cpp index 1a697a283bdda..bd940acb3b875 100644 --- a/src/coreclr/vm/multicorejit.cpp +++ b/src/coreclr/vm/multicorejit.cpp @@ -309,10 +309,9 @@ bool RecorderModuleInfo::SetModule(Module * pMod) simpleName.Set((const BYTE *) pModuleName, lenModuleName); // SBuffer::Set copies over name SString sAssemblyName; - StackScratchBuffer scratch; pMod->GetAssembly()->GetPEAssembly()->GetDisplayName(sAssemblyName); - LPCUTF8 pAssemblyName = sAssemblyName.GetUTF8(scratch); + LPCUTF8 pAssemblyName = sAssemblyName.GetUTF8(); unsigned lenAssemblyName = sAssemblyName.GetCount(); assemblyName.Set((const BYTE *) pAssemblyName, lenAssemblyName); diff --git a/src/coreclr/vm/nativelibrary.cpp b/src/coreclr/vm/nativelibrary.cpp index 891ce48635293..f2b43561c90b7 100644 --- a/src/coreclr/vm/nativelibrary.cpp +++ b/src/coreclr/vm/nativelibrary.cpp @@ -707,8 +707,7 @@ namespace SString moduleName(SString::Utf8, szLibName); moduleName.LowerCase(); - StackScratchBuffer buffer; - szLibName = (LPSTR)moduleName.GetUTF8(buffer); + szLibName = (LPSTR)moduleName.GetUTF8(); Assembly *pAssembly = spec.LoadAssembly(FILE_LOADED); Module *pModule = pAssembly->FindModuleByName(szLibName); diff --git a/src/coreclr/vm/pgo.cpp b/src/coreclr/vm/pgo.cpp index d1e2f91954ef4..b6b1a155eb948 100644 --- a/src/coreclr/vm/pgo.cpp +++ b/src/coreclr/vm/pgo.cpp @@ -220,10 +220,8 @@ void PgoManager::WritePgoData() SString tClass, tMethodName, tMethodSignature; pgoData->header.method->GetMethodInfo(tClass, tMethodName, tMethodSignature); - StackScratchBuffer nameBuffer; - StackScratchBuffer nameBuffer2; - fprintf(pgoDataFile, "MethodName: %s.%s\n", tClass.GetUTF8(nameBuffer), tMethodName.GetUTF8(nameBuffer2)); - fprintf(pgoDataFile, "Signature: %s\n", tMethodSignature.GetUTF8(nameBuffer)); + fprintf(pgoDataFile, "MethodName: %s.%s\n", tClass.GetUTF8(), tMethodName.GetUTF8()); + fprintf(pgoDataFile, "Signature: %s\n", tMethodSignature.GetUTF8()); uint8_t* data = pgoData->header.GetData(); @@ -262,7 +260,6 @@ void PgoManager::WritePgoData() else { StackSString ss; - StackScratchBuffer nameBuffer; TypeString::AppendType(ss, th, TypeString::FormatNamespace | TypeString::FormatFullInst | TypeString::FormatAssembly); if (ss.GetCount() > 8192) { @@ -270,7 +267,7 @@ void PgoManager::WritePgoData() } else { - fprintf(pgoDataFile, s_TypeHandle, ss.GetUTF8(nameBuffer)); + fprintf(pgoDataFile, s_TypeHandle, ss.GetUTF8()); } } break; @@ -301,9 +298,7 @@ void PgoManager::WritePgoData() } else { - StackScratchBuffer methodNameBuffer; - StackScratchBuffer typeBuffer; - fprintf(pgoDataFile, "MethodHandle: %s|@|%s\n", tMethodName.GetUTF8(methodNameBuffer), tTypeName.GetUTF8(typeBuffer)); + fprintf(pgoDataFile, "MethodHandle: %s|@|%s\n", tMethodName.GetUTF8(), tTypeName.GetUTF8()); } } break; diff --git a/src/coreclr/vm/stubgen.cpp b/src/coreclr/vm/stubgen.cpp index 69da400c09cbf..74f7428928dff 100644 --- a/src/coreclr/vm/stubgen.cpp +++ b/src/coreclr/vm/stubgen.cpp @@ -106,10 +106,7 @@ void ILStubLinker::DumpIL_FormatToken(mdToken token, SString &strTokenFormatting SString typeName; TypeString::AppendType(typeName, TypeHandle(pFD->GetApproxEnclosingMethodTable())); - SString typeNameUtf8; - typeName.ConvertToUTF8(typeNameUtf8); - SString strFieldName(SString::Utf8, pFD->GetName()); - strTokenFormatting.Printf("%s::%s", typeNameUtf8.GetUTF8(), strFieldName.GetUTF8()); + strTokenFormatting.Printf("%s::%s", typeName.GetUTF8(), pFD->GetName()); } else if (TypeFromToken(token) == mdtModule) { diff --git a/src/coreclr/vm/typeparse.cpp b/src/coreclr/vm/typeparse.cpp index 309f08e90ce0f..253a4151a8f7b 100644 --- a/src/coreclr/vm/typeparse.cpp +++ b/src/coreclr/vm/typeparse.cpp @@ -1303,8 +1303,7 @@ TypeName::GetTypeHaveAssemblyHelper( if (bIgnoreCase) name.LowerCase(); - StackScratchBuffer buffer; - typeName.SetName(name.GetUTF8(buffer)); + typeName.SetName(name.GetUTF8()); // typeName.m_pBucket gets set here if the type is found // it will be used in the next iteration to look up the nested type From 9e0754f40eea521bb158eec3d37dbe5463a55ca2 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 21 Jun 2022 18:58:37 -0700 Subject: [PATCH 3/6] Update src/coreclr/utilcode/sstring.cpp Co-authored-by: Aaron Robinson --- src/coreclr/utilcode/sstring.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/utilcode/sstring.cpp b/src/coreclr/utilcode/sstring.cpp index fcdcc4166ff78..7917bfe8b07be 100644 --- a/src/coreclr/utilcode/sstring.cpp +++ b/src/coreclr/utilcode/sstring.cpp @@ -778,7 +778,7 @@ void SString::ConvertToUnicode(const CIterator &i) const } //----------------------------------------------------------------------------- -// Convert the internal representation for this String to Unicode. +// Convert the internal representation for this String to UTF8. //----------------------------------------------------------------------------- void SString::ConvertToUTF8() const { From 36731bcad82e13127e90f6e3416aa7f8dbc17dd9 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 22 Jun 2022 10:47:26 -0700 Subject: [PATCH 4/6] Add SetAndConvertToUTF8 method and remove the GetUTF8 method variants with a scratch buffer. --- src/coreclr/inc/sstring.h | 8 +-- src/coreclr/utilcode/sstring.cpp | 63 +++++++------------ src/coreclr/vm/assemblynative.cpp | 13 ++-- src/coreclr/vm/assemblyspec.cpp | 18 ++++-- src/coreclr/vm/bundle.cpp | 5 +- src/coreclr/vm/class.h | 8 +-- src/coreclr/vm/codepitchingmanager.cpp | 5 +- src/coreclr/vm/comdelegate.cpp | 5 +- src/coreclr/vm/customattribute.cpp | 23 ++----- src/coreclr/vm/customattribute.h | 1 - src/coreclr/vm/dllimport.cpp | 5 +- .../vm/eventing/eventpipe/ep-rt-coreclr.h | 3 +- src/coreclr/vm/genmeth.cpp | 3 +- src/coreclr/vm/jitinterface.cpp | 26 +++++--- src/coreclr/vm/jitinterface.h | 2 +- src/coreclr/vm/methodtablebuilder.cpp | 6 +- 16 files changed, 88 insertions(+), 106 deletions(-) diff --git a/src/coreclr/inc/sstring.h b/src/coreclr/inc/sstring.h index 56eee4bb1a4a6..73185cc53a82a 100644 --- a/src/coreclr/inc/sstring.h +++ b/src/coreclr/inc/sstring.h @@ -173,6 +173,7 @@ class EMPTY_BASES_DECL SString : private SBuffer void SetASCII(const ASCII *string); void SetUTF8(const UTF8 *string); void SetANSI(const ANSI *string); + void SetAndConvertToUTF8(const WCHAR* string); // Set this string to a copy of the first count chars of the given string void Set(const WCHAR *string, COUNT_T count); @@ -544,13 +545,10 @@ class EMPTY_BASES_DECL SString : private SBuffer // SString *s = ...; // { // StackScratchBuffer buffer; - // const UTF8 *utf8 = s->GetUTF8(buffer); - // CallFoo(utf8); + // const ANSI *ansi = s->GetANSI(buffer); + // CallFoo(ansi); // } // // No more pointers to returned buffer allowed. - - const UTF8 *GetUTF8(AbstractScratchBuffer &scratch) const; - const UTF8 *GetUTF8(AbstractScratchBuffer &scratch, COUNT_T *pcbUtf8) const; const ANSI *GetANSI(AbstractScratchBuffer &scratch) const; // You can always get a UTF8 string. This will force a conversion diff --git a/src/coreclr/utilcode/sstring.cpp b/src/coreclr/utilcode/sstring.cpp index fcdcc4166ff78..ec5b6600af3a6 100644 --- a/src/coreclr/utilcode/sstring.cpp +++ b/src/coreclr/utilcode/sstring.cpp @@ -469,6 +469,29 @@ void SString::SetANSI(const ANSI *string, COUNT_T count) SS_RETURN; } +//----------------------------------------------------------------------------- +// Set this string to a copy of the given UTF16 string transcoded to UTF8 +//----------------------------------------------------------------------------- +void SString::SetAndConvertToUTF8(const WCHAR *string) +{ + SS_CONTRACT_VOID + { + // !!! Check for illegal UTF8 encoding? + INSTANCE_CHECK; + PRECONDITION(CheckPointer(string, NULL_OK)); + THROWS; + GC_NOTRIGGER; + SUPPORTS_DAC_HOST_ONLY; + } + SS_CONTRACT_END; + + SString utf16Str(Literal, string); + + utf16Str.ConvertToUTF8(*this); + + SS_RETURN; +} + //----------------------------------------------------------------------------- // Set this string to the given unicode character //----------------------------------------------------------------------------- @@ -1820,46 +1843,6 @@ const CHAR *SString::GetANSI(AbstractScratchBuffer &scratch) const SS_RETURN ((SString&)scratch).GetRawANSI(); } -//----------------------------------------------------------------------------- -// Get a const pointer to the internal buffer as a UTF8 string. -//----------------------------------------------------------------------------- -const UTF8 *SString::GetUTF8(AbstractScratchBuffer &scratch) const -{ - CONTRACT(const UTF8 *) - { - INSTANCE_CHECK_NULL; - THROWS; - GC_NOTRIGGER; - } - CONTRACT_END; - - if (IsRepresentation(REPRESENTATION_UTF8)) - RETURN GetRawUTF8(); - - ConvertToUTF8((SString&)scratch); - RETURN ((SString&)scratch).GetRawUTF8(); -} - -const UTF8 *SString::GetUTF8(AbstractScratchBuffer &scratch, COUNT_T *pcbUtf8) const -{ - CONTRACT(const UTF8 *) - { - INSTANCE_CHECK_NULL; - THROWS; - GC_NOTRIGGER; - } - CONTRACT_END; - - if (IsRepresentation(REPRESENTATION_UTF8)) - { - *pcbUtf8 = GetRawCount() + 1; - RETURN GetRawUTF8(); - } - - *pcbUtf8 = ConvertToUTF8((SString&)scratch); - RETURN ((SString&)scratch).GetRawUTF8(); -} - //----------------------------------------------------------------------------- // Safe version of sprintf. // Prints formatted ansi text w/ var args to this buffer. diff --git a/src/coreclr/vm/assemblynative.cpp b/src/coreclr/vm/assemblynative.cpp index a50ac0415dea7..d964660a7a341 100644 --- a/src/coreclr/vm/assemblynative.cpp +++ b/src/coreclr/vm/assemblynative.cpp @@ -540,10 +540,10 @@ extern "C" BYTE * QCALLTYPE AssemblyNative_GetResource(QCall::AssemblyHandle pAs COMPlusThrow(kArgumentNullException, W("ArgumentNull_String")); // Get the name in UTF8 - SString name(SString::Literal, wszName); + StackSString name; + name.SetAndConvertToUTF8(wszName); - StackScratchBuffer scratch; - LPCUTF8 pNameUTF8 = name.GetUTF8(scratch); + LPCUTF8 pNameUTF8 = name.GetUTF8(); if (*pNameUTF8 == '\0') COMPlusThrow(kArgumentException, W("Format_StringZeroLength")); @@ -570,10 +570,9 @@ extern "C" INT32 QCALLTYPE AssemblyNative_GetManifestResourceInfo(QCall::Assembl COMPlusThrow(kArgumentNullException, W("ArgumentNull_String")); // Get the name in UTF8 - SString name(SString::Literal, wszName); - - StackScratchBuffer scratch; - LPCUTF8 pNameUTF8 = name.GetUTF8(scratch); + SString name; + name.SetAndConvertToUTF8(wszName); + LPCUTF8 pNameUTF8 = name.GetUTF8(); if (*pNameUTF8 == '\0') COMPlusThrow(kArgumentException, W("Format_StringZeroLength")); diff --git a/src/coreclr/vm/assemblyspec.cpp b/src/coreclr/vm/assemblyspec.cpp index 2270d62c80392..55e3fd80c41a4 100644 --- a/src/coreclr/vm/assemblyspec.cpp +++ b/src/coreclr/vm/assemblyspec.cpp @@ -275,13 +275,23 @@ void AssemblySpec::InitializeAssemblyNameRef(_In_ BINDER_SPACE::AssemblyName* as AssemblySpec spec; spec.InitializeWithAssemblyIdentity(assemblyName); - StackScratchBuffer nameBuffer; - spec.SetName(assemblyName->GetSimpleName().GetUTF8(nameBuffer)); + StackSString nameBuffer; + nameBuffer.SetAndConvertToUTF8(assemblyName->GetSimpleName().GetUnicode()); + spec.SetName(nameBuffer.GetUTF8()); - StackScratchBuffer cultureBuffer; + StackSString cultureBuffer; if (assemblyName->Have(BINDER_SPACE::AssemblyIdentity::IDENTITY_FLAG_CULTURE)) { - LPCSTR culture = assemblyName->IsNeutralCulture() ? "" : assemblyName->GetCulture().GetUTF8(cultureBuffer); + LPCSTR culture; + if (assemblyName->IsNeutralCulture()) + { + culture = ""; + } + else + { + cultureBuffer.SetAndConvertToUTF8(assemblyName->GetCulture().GetUnicode()); + culture = cultureBuffer.GetUTF8(); + } spec.SetCulture(culture); } diff --git a/src/coreclr/vm/bundle.cpp b/src/coreclr/vm/bundle.cpp index a51ed906b710d..5b66a331dd48d 100644 --- a/src/coreclr/vm/bundle.cpp +++ b/src/coreclr/vm/bundle.cpp @@ -60,8 +60,9 @@ BundleFileLocation Bundle::Probe(const SString& path, bool pathIsBundleRelative) // Bundle.Probe("path/to/exe/lib.dll") => m_probe("lib.dll") // Bundle.Probe("path/to/exe/and/some/more/lib.dll") => m_probe("and/some/more/lib.dll") - StackScratchBuffer scratchBuffer; - LPCSTR utf8Path(path.GetUTF8(scratchBuffer)); + StackSString pathBuffer; + pathBuffer.SetAndConvertToUTF8(path.GetUnicode()); + LPCSTR utf8Path(pathBuffer.GetUTF8()); if (!pathIsBundleRelative) { diff --git a/src/coreclr/vm/class.h b/src/coreclr/vm/class.h index 7b9f77bca449c..0bbd15542ef2d 100644 --- a/src/coreclr/vm/class.h +++ b/src/coreclr/vm/class.h @@ -507,11 +507,11 @@ typedef struct #define DEFAULT_NONSTACK_CLASSNAME_SIZE (MAX_CLASSNAME_LENGTH/4) #define DefineFullyQualifiedNameForClass() \ - ScratchBuffer _scratchbuffer_; \ + InlineSString _ssclsname8_; \ InlineSString _ssclsname_; #define DefineFullyQualifiedNameForClassOnStack() \ - ScratchBuffer _scratchbuffer_; \ + InlineSString _ssclsname8_; \ InlineSString _ssclsname_; #define DefineFullyQualifiedNameForClassW() \ @@ -521,13 +521,13 @@ typedef struct InlineSString _ssclsname_w_; #define GetFullyQualifiedNameForClassNestedAware(pClass) \ - pClass->_GetFullyQualifiedNameForClassNestedAware(_ssclsname_).GetUTF8(_scratchbuffer_) + (pClass->_GetFullyQualifiedNameForClassNestedAware(_ssclsname_), _ssclsname8_.SetAndConvertToUTF8(_ssclsname_), _ssclsname8_.GetUTF8()) #define GetFullyQualifiedNameForClassNestedAwareW(pClass) \ pClass->_GetFullyQualifiedNameForClassNestedAware(_ssclsname_w_).GetUnicode() #define GetFullyQualifiedNameForClass(pClass) \ - pClass->_GetFullyQualifiedNameForClass(_ssclsname_).GetUTF8(_scratchbuffer_) + (pClass->_GetFullyQualifiedNameForClass(_ssclsname_), _ssclsname8_.SetAndConvertToUTF8(_ssclsname_), _ssclsname8_.GetUTF8()) #define GetFullyQualifiedNameForClassW(pClass) \ pClass->_GetFullyQualifiedNameForClass(_ssclsname_w_).GetUnicode() diff --git a/src/coreclr/vm/codepitchingmanager.cpp b/src/coreclr/vm/codepitchingmanager.cpp index f4e8cdcac1cac..0e7429a295906 100644 --- a/src/coreclr/vm/codepitchingmanager.cpp +++ b/src/coreclr/vm/codepitchingmanager.cpp @@ -417,9 +417,8 @@ void MethodDesc::PitchNativeCode() SString className, methodName, methodSig; GetMethodInfo(className, methodName, methodSig); - StackScratchBuffer scratch; - const char* szClassName = className.GetUTF8(scratch); - const char* szMethodSig = methodSig.GetUTF8(scratch); + const char* szClassName = className.GetUTF8(); + const char* szMethodSig = methodSig.GetUTF8(); printf("Pitched %lu %lu %s :: %s %s\n", s_PitchedMethodCounter, pitchedBytes, szClassName, GetName(), szMethodSig); diff --git a/src/coreclr/vm/comdelegate.cpp b/src/coreclr/vm/comdelegate.cpp index 1a3f636e7b0ba..4dde8873b2f04 100644 --- a/src/coreclr/vm/comdelegate.cpp +++ b/src/coreclr/vm/comdelegate.cpp @@ -924,8 +924,9 @@ FCIMPL5(FC_BOOL_RET, COMDelegate::BindToMethodName, // // get the name in UTF8 format - SString wszName(SString::Literal, gc.methodName->GetBuffer()); - LPCUTF8 szNameStr = wszName.GetUTF8(); + StackSString szName; + szName.SetAndConvertToUTF8(gc.methodName->GetBuffer()); + LPCUTF8 szNameStr = szName.GetUTF8(); // pick a proper compare function typedef int (__cdecl *UTF8StringCompareFuncPtr)(const char *, const char *); diff --git a/src/coreclr/vm/customattribute.cpp b/src/coreclr/vm/customattribute.cpp index 00d27e8ea0e91..ea18d499577a8 100644 --- a/src/coreclr/vm/customattribute.cpp +++ b/src/coreclr/vm/customattribute.cpp @@ -388,12 +388,11 @@ HRESULT Attribute::ParseCaNamedArgs( IfFailGo(Attribute::ParseCaType(ca, pNamedArgType, pDomainAssembly, &ss)); LPCSTR szLoadedEnumName = NULL; - StackScratchBuffer buff; if (pNamedArgType->tag == SERIALIZATION_TYPE_ENUM || (pNamedArgType->tag == SERIALIZATION_TYPE_SZARRAY && pNamedArgType->arrayType == SERIALIZATION_TYPE_ENUM )) { - szLoadedEnumName = ss.GetUTF8(buff); + szLoadedEnumName = ss.GetUTF8(); } // Get name of Arg. @@ -470,23 +469,20 @@ HRESULT Attribute::ParseCaNamedArgs( } /*static*/ -HRESULT Attribute::InitCaType(CustomAttributeType* pType, Factory* pSstringFactory, Factory* pStackScratchBufferFactory, CaType* pCaType) +HRESULT Attribute::InitCaType(CustomAttributeType* pType, Factory* pSstringFactory, CaType* pCaType) { CONTRACTL { THROWS; PRECONDITION(CheckPointer(pType)); PRECONDITION(CheckPointer(pSstringFactory)); - PRECONDITION(CheckPointer(pStackScratchBufferFactory)); PRECONDITION(CheckPointer(pCaType)); } CONTRACTL_END; HRESULT hr = S_OK; SString* psszName = NULL; - StackScratchBuffer* scratchBuffer = NULL; IfNullGo(psszName = pSstringFactory->Create()); - IfNullGo(scratchBuffer = pStackScratchBufferFactory->Create()); psszName->Set(pType->m_enumName == NULL ? NULL : pType->m_enumName->GetBuffer()); @@ -494,7 +490,7 @@ HRESULT Attribute::InitCaType(CustomAttributeType* pType, Factory* pSst pType->m_tag, pType->m_arrayType, pType->m_enumType, - psszName->GetUTF8(*scratchBuffer), + psszName->GetUTF8(), (ULONG)psszName->GetCount()); ErrExit: @@ -538,7 +534,6 @@ FCIMPL5(VOID, Attribute::ParseAttributeArguments, void* pCa, INT32 cCa, // on what we can allocate inline here. Leave the Windows versions alone to retain the perf benefits // since we don't have the same constraints. NewHolder pCaValueArrayFactory = new InlineFactory, 4>(); - InlineFactory stackScratchBufferFactory; InlineFactory sstringFactory; #else // __GNUC__ @@ -551,9 +546,6 @@ FCIMPL5(VOID, Attribute::ParseAttributeArguments, void* pCa, INT32 cCa, InlineFactory, 4> caValueArrayFactory; InlineFactory, 4> *pCaValueArrayFactory = &caValueArrayFactory; - // Need one StackScratchBuffer per ctor arg and two per named arg - InlineFactory stackScratchBufferFactory; - // Need one SString per ctor arg and two per named arg InlineFactory sstringFactory; #endif // __GNUC__ @@ -572,7 +564,7 @@ FCIMPL5(VOID, Attribute::ParseAttributeArguments, void* pCa, INT32 cCa, for (COUNT_T i = 0; i < cArgs; i ++) { CaType caType; - IfFailGo(Attribute::InitCaType(&gc.pArgs[i].m_type, &sstringFactory, &stackScratchBufferFactory, &caType)); + IfFailGo(Attribute::InitCaType(&gc.pArgs[i].m_type, &sstringFactory, &caType)); pCaArgs[i].Init(caType); } @@ -594,18 +586,15 @@ FCIMPL5(VOID, Attribute::ParseAttributeArguments, void* pCa, INT32 cCa, CustomAttributeNamedArgument* pNamedArg = &gc.pNamedArgs[i]; CaType caType; - IfFailGo(Attribute::InitCaType(&pNamedArg->m_type, &sstringFactory, &stackScratchBufferFactory, &caType)); + IfFailGo(Attribute::InitCaType(&pNamedArg->m_type, &sstringFactory, &caType)); SString* psszName = NULL; IfNullGo(psszName = sstringFactory.Create()); psszName->Set(pNamedArg->m_argumentName->GetBuffer()); - StackScratchBuffer* scratchBuffer = NULL; - IfNullGo(scratchBuffer = stackScratchBufferFactory.Create()); - pCaNamedArgs[i].Init( - psszName->GetUTF8(*scratchBuffer), + psszName->GetUTF8(), pNamedArg->m_propertyOrField, caType); } diff --git a/src/coreclr/vm/customattribute.h b/src/coreclr/vm/customattribute.h index ac95fa80af995..6d4282d8f52ab 100644 --- a/src/coreclr/vm/customattribute.h +++ b/src/coreclr/vm/customattribute.h @@ -139,7 +139,6 @@ class Attribute static HRESULT InitCaType( CustomAttributeType* pType, Factory* pSstringFactory, - Factory* pStackScratchBufferFactory, CaType* pCaType); static HRESULT ParseCaType( diff --git a/src/coreclr/vm/dllimport.cpp b/src/coreclr/vm/dllimport.cpp index 50e3f513331fc..5fc6d3c15b9f4 100644 --- a/src/coreclr/vm/dllimport.cpp +++ b/src/coreclr/vm/dllimport.cpp @@ -4628,8 +4628,9 @@ HRESULT FindPredefinedILStubMethod(MethodDesc *pTargetMD, DWORD dwStubFlags, Met // // Find method using name + signature // - StackScratchBuffer buffer; - LPCUTF8 szMethodNameUTF8 = methodName.GetUTF8(buffer); + StackSString buffer; + buffer.SetAndConvertToUTF8(methodName); + LPCUTF8 szMethodNameUTF8 = buffer.GetUTF8(); pStubMD = MemberLoader::FindMethod(stubClassType.GetMethodTable(), szMethodNameUTF8, pStubSig, diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index 6c35c8fa50198..c4246df418a64 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -1369,10 +1369,9 @@ ep_rt_method_get_full_name ( EX_TRY { SString method_name; - StackScratchBuffer conversion; TypeString::AppendMethodInternal (method_name, method, TypeString::FormatNamespace | TypeString::FormatSignature); - const ep_char8_t *method_name_utf8 = method_name.GetUTF8 (conversion); + const ep_char8_t *method_name_utf8 = method_name.GetUTF8 (); if (method_name_utf8) { size_t method_name_utf8_len = strlen (method_name_utf8) + 1; size_t to_copy = method_name_utf8_len < name_len ? method_name_utf8_len : name_len; diff --git a/src/coreclr/vm/genmeth.cpp b/src/coreclr/vm/genmeth.cpp index 24b47038c296f..a53119385ef2e 100644 --- a/src/coreclr/vm/genmeth.cpp +++ b/src/coreclr/vm/genmeth.cpp @@ -475,8 +475,7 @@ InstantiatedMethodDesc::NewInstantiatedMethodDesc(MethodTable *pExactMT, #ifdef _DEBUG SString name; TypeString::AppendMethodDebug(name, pNewMD); - StackScratchBuffer buff; - const char* pDebugNameUTF8 = name.GetUTF8(buff); + const char* pDebugNameUTF8 = name.GetUTF8(); const char* verb = "Created"; if (pWrappedMD) LOG((LF_CLASSLOADER, LL_INFO1000, diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index a4f53f19f70fd..6f33f718ad0c3 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -6199,7 +6199,8 @@ const char* CEEInfo::getMethodName (CORINFO_METHOD_HANDLE ftnHnd, const char** s if (!inst.IsEmpty()) TypeString::AppendInst(ssClsNameBuff, inst); - *scopeName = ssClsNameBuff.GetUTF8(ssClsNameBuffScratch); + ssClsNameBuffUTF8.SetAndConvertToUTF8(ssClsNameBuff.GetUnicode()); + *scopeName = ssClsNameBuffUTF8.GetUTF8(); #else // !_DEBUG // since this is for diagnostic purposes only, // give up on the namespace, as we don't have a buffer to concat it @@ -9129,7 +9130,8 @@ const char* CEEInfo::getFieldName (CORINFO_FIELD_HANDLE fieldHnd, const char** s { #ifdef _DEBUG t.GetName(ssClsNameBuff); - *scopeName = ssClsNameBuff.GetUTF8(ssClsNameBuffScratch); + ssClsNameBuffUTF8.SetAndConvertToUTF8(ssClsNameBuff.GetUnicode()); + *scopeName = ssClsNameBuffUTF8.GetUTF8(); #else // !_DEBUG // since this is for diagnostic purposes only, // give up on the namespace, as we don't have a buffer to concat it @@ -13706,8 +13708,9 @@ BOOL LoadDynamicInfoEntry(Module *currentModule, #ifdef _DEBUG { - StackScratchBuffer buf; - _ASSERTE_MSG(false, fatalErrorString.GetUTF8(buf)); + StackSString buf; + buf.SetAndConvertToUTF8(fatalErrorString.GetUnicode()); + _ASSERTE_MSG(false, buf.GetUTF8()); // Run through the type layout logic again, after the assert, makes debugging easy TypeLayoutCheck(pMT, pBlob, /* printDiff */ TRUE); } @@ -13786,8 +13789,9 @@ BOOL LoadDynamicInfoEntry(Module *currentModule, #ifdef _DEBUG { - StackScratchBuffer buf; - _ASSERTE_MSG(false, fatalErrorString.GetUTF8(buf)); + StackSString buf; + buf.SetAndConvertToUTF8(fatalErrorString.GetUnicode()); + _ASSERTE_MSG(false, buf.GetUTF8()); } #endif @@ -13907,8 +13911,9 @@ BOOL LoadDynamicInfoEntry(Module *currentModule, #ifdef _DEBUG { - StackScratchBuffer buf; - _ASSERTE_MSG(false, fatalErrorString.GetUTF8(buf)); + StackSString buf; + buf.SetAndConvertToUTF8(fatalErrorString.GetUnicode()); + _ASSERTE_MSG(false, buf.GetUTF8()); } #endif _ASSERTE(!IsDebuggerPresent() && "Stop on assert here instead of fatal error for ease of live debugging"); @@ -14059,8 +14064,9 @@ BOOL LoadDynamicInfoEntry(Module *currentModule, #ifdef _DEBUG { - StackScratchBuffer buf; - _ASSERTE_MSG(false, fatalErrorString.GetUTF8(buf)); + StackSString buf; + buf.SetAndConvertToUTF8(fatalErrorString.GetUnicode()); + _ASSERTE_MSG(false, buf.GetUTF8()); } #endif _ASSERTE(!IsDebuggerPresent() && "Stop on assert here instead of fatal error for ease of live debugging"); diff --git a/src/coreclr/vm/jitinterface.h b/src/coreclr/vm/jitinterface.h index 492b2fbe5fe12..9355becf83057 100644 --- a/src/coreclr/vm/jitinterface.h +++ b/src/coreclr/vm/jitinterface.h @@ -520,7 +520,7 @@ class CEEInfo : public ICorJitInfo #ifdef _DEBUG InlineSString ssClsNameBuff; - ScratchBuffer ssClsNameBuffScratch; + InlineSString ssClsNameBuffUTF8; #endif public: diff --git a/src/coreclr/vm/methodtablebuilder.cpp b/src/coreclr/vm/methodtablebuilder.cpp index 86f2cbfa199ff..57089d293765c 100644 --- a/src/coreclr/vm/methodtablebuilder.cpp +++ b/src/coreclr/vm/methodtablebuilder.cpp @@ -1893,8 +1893,7 @@ MethodTableBuilder::BuildMethodTableThrowing( MethodDesc *pMD = methIt->GetUnboxedMethodDesc(); StackSString name; TypeString::AppendMethodDebug(name, pMD); - StackScratchBuffer buff; - const char* pDebugNameUTF8 = name.GetUTF8(buff); + const char* pDebugNameUTF8 = name.GetUTF8(); S_SIZE_T safeLen = S_SIZE_T(strlen(pDebugNameUTF8)) + S_SIZE_T(1); if(safeLen.IsOverflow()) COMPlusThrowHR(COR_E_OVERFLOW); size_t len = safeLen.Value(); @@ -1908,8 +1907,7 @@ MethodTableBuilder::BuildMethodTableThrowing( StackSString name; TypeString::AppendMethodDebug(name, pMD); - StackScratchBuffer buff; - const char* pDebugNameUTF8 = name.GetUTF8(buff); + const char* pDebugNameUTF8 = name.GetUTF8(); S_SIZE_T safeLen = S_SIZE_T(strlen(pDebugNameUTF8))+S_SIZE_T(1); if(safeLen.IsOverflow()) COMPlusThrowHR(COR_E_OVERFLOW); size_t len = safeLen.Value(); From ada35f931507e25cbeb529ea5a9da5ef2b845b3b Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 22 Jun 2022 10:49:02 -0700 Subject: [PATCH 5/6] Remove unneeded variables. --- src/coreclr/utilcode/clrconfig.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/coreclr/utilcode/clrconfig.cpp b/src/coreclr/utilcode/clrconfig.cpp index d52679b9e078b..5df6748739ccb 100644 --- a/src/coreclr/utilcode/clrconfig.cpp +++ b/src/coreclr/utilcode/clrconfig.cpp @@ -200,15 +200,11 @@ namespace #if defined(DEBUG) && !defined(SELF_NO_HOST) // Validate the cache and no-cache logic result in the same answer SString nameToConvert(name); - SString nameAsUTF8; - nameToConvert.ConvertToUTF8(nameAsUTF8); - SString valueAsUTF8; - temp.ConvertToUTF8(valueAsUTF8); - CLRConfigNoCache nonCache = CLRConfigNoCache::Get(nameAsUTF8.GetUTF8(), noPrefix); + CLRConfigNoCache nonCache = CLRConfigNoCache::Get(nameToConvert.GetUTF8(), noPrefix); LPCSTR valueNoCache = nonCache.AsString(); - _ASSERTE(SString::_stricmp(valueNoCache, valueAsUTF8.GetUTF8()) == 0); + _ASSERTE(SString::_stricmp(valueNoCache, temp.GetUTF8()) == 0); #endif // defined(DEBUG) && !defined(SELF_NO_HOST) } } From cfd06d1c30428df90c86dbc8d03df35e27fbe0a4 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 22 Jun 2022 13:31:14 -0700 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: Jan Kotas --- src/coreclr/vm/assemblynative.cpp | 7 ++++--- src/coreclr/vm/coreassemblyspec.cpp | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/coreclr/vm/assemblynative.cpp b/src/coreclr/vm/assemblynative.cpp index 1405ccdaad0e1..393b31c77b70d 100644 --- a/src/coreclr/vm/assemblynative.cpp +++ b/src/coreclr/vm/assemblynative.cpp @@ -75,7 +75,8 @@ extern "C" void QCALLTYPE AssemblyNative_InternalLoad(NativeAssemblyNameParts* p if (pAssemblyNameParts->_pName == NULL) COMPlusThrow(kArgumentException, W("Format_StringZeroLength")); - StackSString ssName(pAssemblyNameParts->_pName); + StackSString ssName; + ssName.SetAndConvertToUTF8(pAssemblyNameParts->_pName); AssemblyMetaDataInternal asmInfo; @@ -86,7 +87,7 @@ extern "C" void QCALLTYPE AssemblyNative_InternalLoad(NativeAssemblyNameParts* p SmallStackSString ssLocale; if (pAssemblyNameParts->_pCultureName != NULL) - ssLocale.Set(pAssemblyNameParts->_pCultureName); + ssLocale.SetAndConvertToUTF8(pAssemblyNameParts->_pCultureName); asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8() : NULL; // Initialize spec @@ -570,7 +571,7 @@ extern "C" INT32 QCALLTYPE AssemblyNative_GetManifestResourceInfo(QCall::Assembl COMPlusThrow(kArgumentNullException, W("ArgumentNull_String")); // Get the name in UTF8 - SString name; + StackSString name; name.SetAndConvertToUTF8(wszName); LPCUTF8 pNameUTF8 = name.GetUTF8(); diff --git a/src/coreclr/vm/coreassemblyspec.cpp b/src/coreclr/vm/coreassemblyspec.cpp index cd58c5a2c8a21..5688d9a4cebfb 100644 --- a/src/coreclr/vm/coreassemblyspec.cpp +++ b/src/coreclr/vm/coreassemblyspec.cpp @@ -185,7 +185,8 @@ extern "C" void QCALLTYPE AssemblyName_InitializeAssemblySpec(NativeAssemblyName BEGIN_QCALL; - StackSString ssName(pAssemblyNameParts->_pName); + StackSString ssName; + ssName.SetAndConvertToUTF8(pAssemblyNameParts->_pName); AssemblyMetaDataInternal asmInfo; @@ -196,7 +197,7 @@ extern "C" void QCALLTYPE AssemblyName_InitializeAssemblySpec(NativeAssemblyName SmallStackSString ssLocale; if (pAssemblyNameParts->_pCultureName != NULL) - ssLocale.Set(pAssemblyNameParts->_pCultureName); + ssLocale.SetAndConvertToUTF8(pAssemblyNameParts->_pCultureName); asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8() : NULL; // Initialize spec