Skip to content

Commit

Permalink
Merge pull request dotnet#6464 from russellhadley/HardenJitStartup
Browse files Browse the repository at this point in the history
Harden stdout at startup
  • Loading branch information
russellhadley authored Jul 26, 2016
2 parents 16988a4 + 7f106c5 commit f09a1f3
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/jit/ee_il_dll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,35 @@ void __stdcall jitStartup(ICorJitHost* jitHost)
#else
if (jitstdout == nullptr)
{
int jitstdoutFd = _dup(_fileno(procstdout()));
_setmode(jitstdoutFd, _O_TEXT);
jitstdout = _fdopen(jitstdoutFd, "w");
assert(jitstdout != nullptr);

// Prevent the FILE* from buffering its output in order to avoid calls to
// `fflush()` throughout the code.
setvbuf(jitstdout, nullptr, _IONBF, 0);
int stdoutFd = _fileno(procstdout());
// Check fileno error output(s) -1 may overlap with errno result
// but is included for completness.
// We want to detect the case where the initial handle is null
// or bogus and avoid making further calls.
if ((stdoutFd != -1) && (stdoutFd != -2) && (errno != EINVAL))
{
int jitstdoutFd = _dup(_fileno(procstdout()));
// Check the error status returned by dup.
if (jitstdoutFd != -1)
{
_setmode(jitstdoutFd, _O_TEXT);
jitstdout = _fdopen(jitstdoutFd, "w");
assert(jitstdout != nullptr);

// Prevent the FILE* from buffering its output in order to avoid calls to
// `fflush()` throughout the code.
setvbuf(jitstdout, nullptr, _IONBF, 0);
}
}
}
#endif

// If jitstdout is still null, fallback to whatever procstdout() was
// initially set to.
if (jitstdout == nullptr)
{
jitstdout = procstdout();
}
#endif // PLATFORM_UNIX

#ifdef FEATURE_TRACELOGGING
JitTelemetry::NotifyDllProcessAttach();
Expand Down

0 comments on commit f09a1f3

Please sign in to comment.