Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the case when pid replacement is not needed #73664

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/coreclr/inc/stresslog.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
#define STRESS_LOG_GC_STACK
#endif //_DEBUG

void AppendPid(LPCWSTR logFilename, LPWSTR fileName, size_t fileNameLength);
LPCWSTR ReplacePid(LPCWSTR logFilename, LPWSTR fileName, size_t fileNameLength);

class ThreadStressLog;

Expand Down
11 changes: 8 additions & 3 deletions src/coreclr/utilcode/stresslog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void StressLog::Leave(CRITSEC_COOKIE) {
DecCantAllocCount();
}

void AppendPid(LPCWSTR logFilename, LPWSTR fileName, size_t fileNameLength)
LPCWSTR ReplacePid(LPCWSTR logFilename, LPWSTR fileName, size_t fileNameLength)
{
// if the string "{pid}" occurs in the logFilename,
// replace it by the PID of our process
Expand All @@ -164,6 +164,11 @@ void AppendPid(LPCWSTR logFilename, LPWSTR fileName, size_t fileNameLength)

// append the rest of the filename
wcscat_s(fileName, fileNameLength, logFilename + pidInx + wcslen(pidLit));
return fileName;
}
else
{
return logFilename;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memory management rules of doing this feel error prone if sometimes we are returning a pointer to one buffer and other times we are returning a pointer to a different buffer. I'd suggest copying logFilename to fileName, make the return type void again, and the caller will always use fileName as before.

I don't think any of the callers that currently exist have an issue here, but I worry that it would be easy to create a new caller in the future that did have an issue.

}
}

Expand All @@ -176,9 +181,9 @@ static LPVOID CreateMemoryMappedFile(LPWSTR logFilename, size_t maxBytesTotal)
}

WCHAR fileName[MAX_PATH];
AppendPid(logFilename, fileName, MAX_PATH);
LPCWSTR logFilenameReplaced = ReplacePid(logFilename, fileName, MAX_PATH);

HandleHolder hFile = WszCreateFile(fileName,
HandleHolder hFile = WszCreateFile(logFilenameReplaced,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL, // default security descriptor
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/finalizerthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ VOID FinalizerThread::FinalizerThreadWorker(void *args)

// Writing an empty file to indicate completion
WCHAR outputPath[MAX_PATH];
AppendPid(GENAWARE_COMPLETION_FILE_NAME, outputPath, MAX_PATH);
fclose(_wfopen(outputPath, W("w+")));
LPCWSTR outputPathReplaced = ReplacePid(GENAWARE_COMPLETION_FILE_NAME, outputPath, MAX_PATH);
fclose(_wfopen(outputPathReplaced, W("w+")));
}

if (!bPriorityBoosted)
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/gcenv.ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1696,8 +1696,8 @@ void GCToEEInterface::AnalyzeSurvivorsFinished(size_t gcIndex, int condemnedGene
EX_TRY
{
WCHAR outputPath[MAX_PATH];
AppendPid(GENAWARE_DUMP_FILE_NAME, outputPath, MAX_PATH);
GenerateDump (outputPath, 2, GenerateDumpFlagsNone, nullptr, 0);
LPCWSTR outputPathReplaced = ReplacePid(GENAWARE_DUMP_FILE_NAME, outputPath, MAX_PATH);
GenerateDump (outputPathReplaced, 2, GenerateDumpFlagsNone, nullptr, 0);
}
EX_CATCH {}
EX_END_CATCH(SwallowAllExceptions);
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/genanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ bool gcGenAnalysisDump = false;
/* static */ void GenAnalysis::EnableGenerationalAwareSession()
{
WCHAR outputPath[MAX_PATH];
AppendPid(GENAWARE_TRACE_FILE_NAME, outputPath, MAX_PATH);
LPCWSTR outputPathReplaced = ReplacePid(GENAWARE_TRACE_FILE_NAME, outputPath, MAX_PATH);

NewArrayHolder<COR_PRF_EVENTPIPE_PROVIDER_CONFIG> pProviders;
int providerCnt = 1;
Expand All @@ -94,7 +94,7 @@ bool gcGenAnalysisDump = false;

EventPipeProviderConfigurationAdapter configAdapter(pProviders, providerCnt);
gcGenAnalysisEventPipeSessionId = EventPipeAdapter::Enable(
outputPath,
outputPathReplaced,
gcGenAnalysisBufferMB,
configAdapter,
EP_SESSION_TYPE_FILE,
Expand Down