Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Fix use after free AV in EventPipe (#24924)
Browse files Browse the repository at this point in the history
* Reenable tests turned off in #24772

* Disable event pipe tests for investigation

* Fix logic for deletion

* In case of error - we need to take the lock because disabling the event pipe session

* Intentionally leaking the EventPipeSession to mitigate a known race condition for now

* Fix for EventListener lock order and throwing

* Fix purposeful leak to still close the session
  • Loading branch information
hoyosjs authored and John Salem committed Jun 3, 2019
1 parent 877efe9 commit 7ec87b0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/vm/eventpipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void EventPipe::DisableInternal(EventPipeSessionID id, EventPipeProviderCallback
EventPipeSession *EventPipe::GetSession(EventPipeSessionID id)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(IsLockOwnedByCurrentThread());
CrstHolder _crst(GetLock());

if (s_pSessions == nullptr)
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/eventpipebuffermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ EventPipeEventInstance* EventPipeBufferManager::GetNextEvent()
{
CONTRACTL
{
NOTHROW;
THROWS;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(!EventPipe::IsLockOwnedByCurrentThread());
Expand Down
4 changes: 3 additions & 1 deletion src/vm/eventpipeconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ void EventPipeConfiguration::DeleteSession(EventPipeSession *pSession)
{
// Reset the mask of active sessions.
m_activeSessions &= ~pSession->GetId();
delete pSession;
pSession->Close();
// TODO: Re-enable this after fixing the underlying race condition
// delete pSession;
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/vm/eventpipesession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ EventPipeSession::EventPipeSession(
QueryPerformanceCounter(&m_sessionStartTimeStamp);
}

void EventPipeSession::Close()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_PREEMPTIVE;
}
CONTRACTL_END;

// FIXME: **ONLY** closes the stream. This explicitly **LEAKS** the
// provider list and buffer manager.
delete m_pFile;
}

EventPipeSession::~EventPipeSession()
{
CONTRACTL
Expand Down Expand Up @@ -182,7 +197,9 @@ DWORD WINAPI EventPipeSession::ThreadProc(void *args)
pEventPipeSession->SetThreadShutdownEvent();

if (!fSuccess)
pEventPipeSession->Disable();
{
EventPipe::RunWithCallbackPostponed([pEventPipeSession](EventPipeProviderCallbackDataQueue *pEventPipeProviderCallbackDataQueue){pEventPipeSession->Disable();});
}
}
EX_CATCH
{
Expand Down
1 change: 1 addition & 0 deletions src/vm/eventpipesession.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class EventPipeSession
uint32_t numProviders,
bool rundownEnabled = false);
~EventPipeSession();
void Close();

EventPipeSessionID GetId() const
{
Expand Down
12 changes: 3 additions & 9 deletions src/vm/eventpipesessionprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,12 @@ void EventPipeSessionProviderList::Clear()
{
if (m_pProviders != NULL)
{
SListElem<EventPipeSessionProvider *> *pElem = m_pProviders->GetHead();
while (pElem != NULL)
while (!m_pProviders->IsEmpty())
{
SListElem<EventPipeSessionProvider*> *pElem = m_pProviders->RemoveHead();
EventPipeSessionProvider *pProvider = pElem->GetValue();
delete pProvider;

SListElem<EventPipeSessionProvider *> *pCurElem = pElem;
pElem = m_pProviders->GetNext(pElem);
delete pCurElem;

// Remove deleted node.
m_pProviders->RemoveHead();
delete pElem;
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/issues.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<Project DefaultTargets = "GetListOfTestCmds" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- All OS/Arch common excludes -->
<ItemGroup Condition="'$(XunitTestBinBase)' != ''">
<ExcludeList Include="$(XunitTestBinBase)/tracing/eventsource/**/*">
<Issue>24839</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/tracing/tracevalidation/**/*">
<Issue>24839</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/baseservices/threading/generics/threadstart/GThread23/*">
<Issue>19339</Issue>
</ExcludeList>
Expand Down Expand Up @@ -518,12 +524,6 @@
<ExcludeList Include="$(XunitTestBinBase)/JIT/Regression/JitBlue/GitHub_19601/Github_19601/*">
<Issue>Needs Triage</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/tracing/eventsource/**/*">
<Issue>Failing on alpine, being tracked by #24772</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/tracing/tracevalidation/**/*">
<Issue>Failing on alpine, being tracked by #24772</Issue>
</ExcludeList>
</ItemGroup>

<!-- Unix arm64 specific -->
Expand Down

0 comments on commit 7ec87b0

Please sign in to comment.