Skip to content

Commit fc06054

Browse files
authored
Fail FuncEval if slot backpatching lock is held by any thread (dotnet#2380)
- In many cases cooperative GC mode is entered after acquiring the slot backpatching lock and the thread may block for debugger suspension while holding the lock. A FuncEval may time out on entering the lock if for example it calls a virtual or interface method for the first time. Failing the FuncEval when the lock is held enables the debugger to fall back to other options for expression evaluation. - Also added polls for debugger suspension before acquiring the slot backpatching lock on background threads that often operate in preemptive GC mode. A common case is when the debugger breaks while the tiering delay timer is active, the timer ticks shortly afterwards (after debugger suspension completes) and if a thread pool thread is already available, the background thread would block while holding the lock. The poll checks for debugger suspension and pulses the GC mode to block before acquiring the lock. - The fix is only a heuristic and lessens the problem when it is detected that the lock is held by some thread. Since the lock is acquired in preemptive GC mode, it is still possible that after the check at the start of a FuncEval, another thread acquires the lock and the FuncEval may time out. The polling makes it less likely for the lock to be taken by background tiering work, for example if a FuncEval starts while rejitting a method. - The expression evaluation experience may be worse when it is detected that the lock is held, and may still happen from unfortunate timing Fix for dotnet#1537
1 parent 9a8f52c commit fc06054

File tree

5 files changed

+83
-18
lines changed

5 files changed

+83
-18
lines changed

src/coreclr/src/debug/ee/debugger.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -15309,6 +15309,15 @@ HRESULT Debugger::FuncEvalSetup(DebuggerIPCE_FuncEvalInfo *pEvalInfo,
1530915309
return CORDBG_E_FUNC_EVAL_BAD_START_POINT;
1531015310
}
1531115311

15312+
if (MethodDescBackpatchInfoTracker::IsLockOwnedByAnyThread())
15313+
{
15314+
// A thread may have suspended for the debugger while holding the slot backpatching lock while trying to enter
15315+
// cooperative GC mode. If the FuncEval calls a method that is eligible for slot backpatching (virtual or interface
15316+
// methods that are eligible for tiering), the FuncEval may deadlock on trying to acquire the same lock. Fail the
15317+
// FuncEval to avoid the issue.
15318+
return CORDBG_E_FUNC_EVAL_BAD_START_POINT;
15319+
}
15320+
1531215321
// Create a DebuggerEval to hold info about this eval while its in progress. Constructor copies the thread's
1531315322
// CONTEXT.
1531415323
DebuggerEval *pDE = new (interopsafe, nothrow) DebuggerEval(filterContext, pEvalInfo, fInException);

src/coreclr/src/vm/callcounting.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ void CallCountingManager::CompleteCallCounting()
817817
{
818818
CodeVersionManager *codeVersionManager = appDomain->GetCodeVersionManager();
819819

820+
MethodDescBackpatchInfoTracker::PollForDebuggerSuspension();
820821
MethodDescBackpatchInfoTracker::ConditionalLockHolder slotBackpatchLockHolder;
821822

822823
// Backpatching entry point slots requires cooperative GC mode, see
@@ -993,6 +994,7 @@ void CallCountingManager::StopAndDeleteAllCallCountingStubs()
993994
TieredCompilationManager *tieredCompilationManager = GetAppDomain()->GetTieredCompilationManager();
994995
bool scheduleTieringBackgroundWork = false;
995996
{
997+
MethodDescBackpatchInfoTracker::PollForDebuggerSuspension();
996998
MethodDescBackpatchInfoTracker::ConditionalLockHolder slotBackpatchLockHolder;
997999

9981000
ThreadSuspend::SuspendEE(ThreadSuspend::SUSPEND_OTHER);

src/coreclr/src/vm/methoddescbackpatchinfo.cpp

+24-8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void EntryPointSlots::Backpatch_Locked(TADDR slot, SlotType slotType, PCODE entr
6666
// MethodDescBackpatchInfoTracker
6767

6868
CrstStatic MethodDescBackpatchInfoTracker::s_lock;
69+
bool MethodDescBackpatchInfoTracker::s_isLocked = false;
6970

7071
#ifndef DACCESS_COMPILE
7172

@@ -111,7 +112,6 @@ void MethodDescBackpatchInfoTracker::AddSlotAndPatch_Locked(MethodDesc *pMethodD
111112
#endif // DACCESS_COMPILE
112113

113114
#ifdef _DEBUG
114-
115115
bool MethodDescBackpatchInfoTracker::IsLockOwnedByCurrentThread()
116116
{
117117
WRAPPER_NO_CONTRACT;
@@ -122,16 +122,32 @@ bool MethodDescBackpatchInfoTracker::IsLockOwnedByCurrentThread()
122122
return true;
123123
#endif
124124
}
125+
#endif // _DEBUG
125126

126-
bool MethodDescBackpatchInfoTracker::MayHaveEntryPointSlotsToBackpatch(PTR_MethodDesc methodDesc)
127+
#ifndef DACCESS_COMPILE
128+
void MethodDescBackpatchInfoTracker::PollForDebuggerSuspension()
127129
{
128-
// The only purpose of this method is to allow asserts in inline functions defined in the .h file, by which time MethodDesc
129-
// is not fully defined
130+
CONTRACTL
131+
{
132+
NOTHROW;
133+
GC_TRIGGERS;
134+
MODE_PREEMPTIVE;
135+
}
136+
CONTRACTL_END;
130137

131-
WRAPPER_NO_CONTRACT;
132-
return methodDesc->MayHaveEntryPointSlotsToBackpatch();
133-
}
138+
_ASSERTE(!IsLockOwnedByCurrentThread());
134139

135-
#endif // _DEBUG
140+
// If suspension is pending for the debugger, pulse the GC mode to suspend the thread here. Following this call, typically
141+
// the lock is acquired and the GC mode is changed, and suspending there would cause FuncEvals to fail (see
142+
// Debugger::FuncEvalSetup() at the reference to IsLockOwnedByAnyThread()). Since this thread is in preemptive mode, the
143+
// debugger may think it's already suspended and it would be unfortunate to suspend the thread with the lock held.
144+
Thread *thread = GetThread();
145+
_ASSERTE(thread != nullptr);
146+
if (thread->HasThreadState(Thread::TS_DebugSuspendPending))
147+
{
148+
GCX_COOP();
149+
}
150+
}
151+
#endif
136152

137153
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

src/coreclr/src/vm/methoddescbackpatchinfo.h

+43-10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class MethodDescBackpatchInfoTracker
6666
{
6767
private:
6868
static CrstStatic s_lock;
69+
static bool s_isLocked;
6970

7071
class BackpatchInfoTrackerHashTraits : public NoRemoveDefaultCrossLoaderAllocatorHashTraits<MethodDesc *, UINT_PTR>
7172
{
@@ -97,9 +98,23 @@ class MethodDescBackpatchInfoTracker
9798
static bool IsLockOwnedByCurrentThread();
9899
#endif
99100

101+
#ifndef DACCESS_COMPILE
102+
public:
103+
static bool IsLockOwnedByAnyThread()
104+
{
105+
LIMITED_METHOD_CONTRACT;
106+
return VolatileLoadWithoutBarrier(&s_isLocked);
107+
}
108+
109+
static void PollForDebuggerSuspension();
110+
#endif
111+
100112
public:
101113
class ConditionalLockHolder : private CrstHolderWithState
102114
{
115+
private:
116+
bool m_isLocked;
117+
103118
public:
104119
ConditionalLockHolder(bool acquireLock = true)
105120
: CrstHolderWithState(
@@ -108,13 +123,37 @@ class MethodDescBackpatchInfoTracker
108123
#else
109124
nullptr
110125
#endif
111-
)
126+
),
127+
m_isLocked(false)
112128
{
113-
LIMITED_METHOD_CONTRACT;
129+
WRAPPER_NO_CONTRACT;
130+
131+
#ifndef DACCESS_COMPILE
132+
if (acquireLock)
133+
{
134+
_ASSERTE(IsLockOwnedByCurrentThread());
135+
_ASSERTE(!s_isLocked);
136+
m_isLocked = true;
137+
s_isLocked = true;
138+
}
139+
#endif
114140
}
115141

116-
ConditionalLockHolder(const ConditionalLockHolder &) = delete;
117-
ConditionalLockHolder &operator =(const ConditionalLockHolder &) = delete;
142+
~ConditionalLockHolder()
143+
{
144+
WRAPPER_NO_CONTRACT;
145+
146+
#ifndef DACCESS_COMPILE
147+
if (m_isLocked)
148+
{
149+
_ASSERTE(IsLockOwnedByCurrentThread());
150+
_ASSERTE(s_isLocked);
151+
s_isLocked = false;
152+
}
153+
#endif
154+
}
155+
156+
DISABLE_COPY(ConditionalLockHolder);
118157
};
119158

120159
public:
@@ -123,16 +162,10 @@ class MethodDescBackpatchInfoTracker
123162
LIMITED_METHOD_CONTRACT;
124163
}
125164

126-
#ifdef _DEBUG
127-
public:
128-
static bool MayHaveEntryPointSlotsToBackpatch(PTR_MethodDesc methodDesc);
129-
#endif
130-
131165
#ifndef DACCESS_COMPILE
132166
public:
133167
void Backpatch_Locked(MethodDesc *pMethodDesc, PCODE entryPoint);
134168
void AddSlotAndPatch_Locked(MethodDesc *pMethodDesc, LoaderAllocator *pLoaderAllocatorOfSlot, TADDR slot, EntryPointSlots::SlotType slotType, PCODE currentEntryPoint);
135-
public:
136169
#endif
137170

138171
DISABLE_COPY(MethodDescBackpatchInfoTracker);

src/coreclr/src/vm/tieredcompilation.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ void TieredCompilationManager::DeactivateTieringDelay()
450450
COUNT_T methodCount = methodsPendingCounting->GetCount();
451451
CodeVersionManager *codeVersionManager = GetAppDomain()->GetCodeVersionManager();
452452

453+
MethodDescBackpatchInfoTracker::PollForDebuggerSuspension();
453454
MethodDescBackpatchInfoTracker::ConditionalLockHolder slotBackpatchLockHolder;
454455

455456
// Backpatching entry point slots requires cooperative GC mode, see
@@ -815,6 +816,10 @@ void TieredCompilationManager::ActivateCodeVersion(NativeCodeVersion nativeCodeV
815816
HRESULT hr = S_OK;
816817
{
817818
bool mayHaveEntryPointSlotsToBackpatch = pMethod->MayHaveEntryPointSlotsToBackpatch();
819+
if (mayHaveEntryPointSlotsToBackpatch)
820+
{
821+
MethodDescBackpatchInfoTracker::PollForDebuggerSuspension();
822+
}
818823
MethodDescBackpatchInfoTracker::ConditionalLockHolder slotBackpatchLockHolder(mayHaveEntryPointSlotsToBackpatch);
819824

820825
// Backpatching entry point slots requires cooperative GC mode, see

0 commit comments

Comments
 (0)