mirror of
https://github.com/siemens-mobile-hacks/llvm-c166.git
synced 2026-11-25 21:45:57 +00:00
`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>
128 lines
4.3 KiB
C++
128 lines
4.3 KiB
C++
//===-- Policy.cpp --------------------------------------------------------===//
|
|
//
|
|
// 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/Utility/Policy.h"
|
|
#include "lldb/Utility/LLDBLog.h"
|
|
#include "lldb/Utility/Log.h"
|
|
#include "lldb/Utility/Stream.h"
|
|
#include "lldb/Utility/StreamString.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
using namespace lldb_private;
|
|
|
|
// Declared out-of-line so every shared library resolves to the same
|
|
// instance: a function-local static in an inline function is not
|
|
// shared across shared library boundaries under hidden visibility.
|
|
PolicyStack &PolicyStack::Get() {
|
|
static thread_local PolicyStack s_stack;
|
|
return s_stack;
|
|
}
|
|
|
|
Policy PolicyStack::Current() const {
|
|
Policy p = m_stack.back();
|
|
// `Current()` is called on every read of the current policy (e.g. every
|
|
// `Process::GetState()`, itself called on every prompt redraw), so log
|
|
// only when verbose is set to avoid drowning out the process log.
|
|
if (Log *log = GetLog(LLDBLog::Process)) {
|
|
if (log->GetVerbose()) {
|
|
StreamString s;
|
|
p.Dump(s);
|
|
LLDB_LOG(log, "{0}", s.GetData());
|
|
}
|
|
}
|
|
return p;
|
|
}
|
|
|
|
// CreatePublicState is the baseline, not a transition. The stack returns to
|
|
// public state by popping the private-state guards, not by pushing a
|
|
// "public" policy on top. This factory exists only as a reference value
|
|
// (tests, dump comparisons); it never reads the current stack.
|
|
Policy Policy::CreatePublicState() { return {}; }
|
|
|
|
Policy Policy::CreatePrivateState(PrivateStatePurpose purpose) {
|
|
Policy p = PolicyStack::Get().Current();
|
|
p.view = View::Private;
|
|
switch (purpose) {
|
|
case PrivateStatePurpose::Default:
|
|
break;
|
|
case PrivateStatePurpose::RunningExpression:
|
|
p.capabilities.can_load_frame_providers = false;
|
|
p.capabilities.can_run_frame_recognizers = false;
|
|
break;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
Policy Policy::CreatePublicStateRunningExpression() {
|
|
Policy p = PolicyStack::Get().Current();
|
|
p.capabilities.can_run_breakpoint_actions = false;
|
|
return p;
|
|
}
|
|
|
|
Policy Policy::CreateScriptedExtensionCall() {
|
|
Policy p = PolicyStack::Get().Current();
|
|
p.capabilities.can_bypass_target_api_mutex = true;
|
|
return p;
|
|
}
|
|
|
|
PolicyStack::Guard::~Guard() {
|
|
if (!m_active)
|
|
return;
|
|
if (m_thread_id != std::this_thread::get_id())
|
|
llvm::report_fatal_error(
|
|
"PolicyStack::Guard destroyed on a different thread than the one "
|
|
"that created it");
|
|
Get().Pop();
|
|
}
|
|
|
|
PolicyStack::Guard::Guard(Guard &&other)
|
|
: m_thread_id(other.m_thread_id), m_active(other.m_active) {
|
|
if (m_active && m_thread_id != std::this_thread::get_id())
|
|
llvm::report_fatal_error("PolicyStack::Guard moved across threads");
|
|
other.m_active = false;
|
|
}
|
|
|
|
PolicyStack::Guard &PolicyStack::Guard::operator=(Guard &&other) {
|
|
if (this != &other) {
|
|
if (other.m_active && other.m_thread_id != std::this_thread::get_id())
|
|
llvm::report_fatal_error("PolicyStack::Guard moved across threads");
|
|
if (m_active) {
|
|
if (m_thread_id != std::this_thread::get_id())
|
|
llvm::report_fatal_error(
|
|
"PolicyStack::Guard destroyed on a different thread than the "
|
|
"one that created it");
|
|
Get().Pop();
|
|
}
|
|
m_thread_id = other.m_thread_id;
|
|
m_active = other.m_active;
|
|
other.m_active = false;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
void Policy::Dump(Stream &s) const {
|
|
s << "policy: view=" << (view == View::Public ? "public" : "private");
|
|
s << ", capabilities={";
|
|
s << "eval_expr=" << capabilities.can_evaluate_expressions;
|
|
s << " run_all=" << capabilities.can_run_all_threads;
|
|
s << " try_all=" << capabilities.can_try_all_threads;
|
|
s << " bp_actions=" << capabilities.can_run_breakpoint_actions;
|
|
s << " frame_providers=" << capabilities.can_load_frame_providers;
|
|
s << " frame_recognizers=" << capabilities.can_run_frame_recognizers;
|
|
s << " bypass_api_mutex=" << capabilities.can_bypass_target_api_mutex;
|
|
s << '}';
|
|
}
|
|
|
|
void PolicyStack::Dump(Stream &s) const {
|
|
s.Printf("PolicyStack depth=%zu\n", m_stack.size());
|
|
for (size_t i = 0; i < m_stack.size(); i++) {
|
|
s.Printf(" [%zu] ", i);
|
|
m_stack[i].Dump(s);
|
|
s << '\n';
|
|
}
|
|
}
|