llvm-c166/lldb/source/Target/TargetAPIMutex.cpp
Med Ismail Bennani b81f88c2ee
[lldb] Fix scripted frame provider cross-thread re-entrant deadlock (#208242)
`GetStoppedExecutionContext` unconditionally blocked acquiring the
target's API mutex. A thread already holding that mutex (for example a
`bt` command thread, through `CommandObjectParsed`'s
`eCommandTryTargetAPILock`) can end up waiting on a `StackFrameList`
lock held by another thread (for example the debugger's event-handler
thread) that is itself blocked re-acquiring the API mutex from inside a
scripted frame provider's Python code that touches the SB API. This is a
classic AB-BA deadlock.

This patch introduces
`Policy::Capabilities::can_bypass_target_api_mutex`, pushed around every
scripted-extension callback in `ScriptedPythonInterface`:
`CreatePluginObject`, `Dispatch` and `CallStaticMethod`. A thread
running one of these callbacks isn't servicing a client-facing SB API
entry point; it doesn't need the same locking guarantees a top-level SB
API call does for any of the calls it makes during that window, not just
the one that happens to deadlock.

`TargetAPIMutex::lock()`/`try_lock()` check this capability when
resolving which mutex to use: when the current thread's policy says it
can bypass, they leave the handle pointing at nothing, touching no
synchronization primitive at all, instead of the real mutex. Every
existing caller keeps its own `lock_guard`/`unique_lock` code unchanged
and becomes deadlock-safe automatically, since a no-op handle can be
`locked`/`unlocked` from any thread with no cross-thread hazard.

Extensions that run directly on the user's behalf opt out. A scripted
command that the user invoked directly, rather than a callback running
internally, needs to keep serializing on the API mutex. This is achieved
by checking `ScriptedInterface::UserCanRunDirectly` so the exemption
covers every scripting language rather than only Python.

This patch adds regression tests for both the original deadlock and for
a blocking `SBMutex.lock()` call made from inside a callback, including
`TestSBMutexReflectsTargetMutex`, which confirms `SBMutex` aliases the
real, shared target mutex rather than the bypass no-op.

Depends on #212872, which introduces TargetAPIMutex's per-call
resolve/replay behavior that makes this bypass safe to observe through a
deferred-lock SBMutex.

Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2026-08-19 06:13:04 +00:00

33 lines
933 B
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Target/TargetAPIMutex.h"
#include "lldb/Target/Target.h"
using namespace lldb_private;
void TargetAPIMutex::Resolve() {
if (!m_target_sp)
return;
std::recursive_mutex *real_mutex = m_target_sp->GetAPIMutexForCurrentPolicy();
m_mutex = real_mutex
? std::shared_ptr<std::recursive_mutex>(m_target_sp, real_mutex)
: nullptr;
}
void TargetAPIMutex::lock() {
Resolve();
if (m_mutex)
m_mutex->lock();
}
bool TargetAPIMutex::try_lock() {
Resolve();
return m_mutex ? m_mutex->try_lock() : true;
}