[llvm-branch-commits] [lldb] [LLDB] Keep InstrumentationRuntime plugins alive during breakpoint callbacks (PR #238465)
Dan Liew via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Nov 10 18:01:22 PST 2026
https://github.com/delcypher updated https://github.com/llvm/llvm-project/pull/238465
>From 38c2bf8c4bec1458409d0826dba2e5f69bcd1637 Mon Sep 17 00:00:00 2001
From: Dan Liew <dan at su-root.co.uk>
Date: Mon, 2 Nov 2026 15:31:57 -0800
Subject: [PATCH] [LLDB] Keep InstrumentationRuntime plugins alive during
breakpoint callbacks
This patch is the fifth part in a patch series that will allow
enabling/disabling InstrumentationRuntime plugins in a running debug
session.
InstrumentationRuntime plugins passed a raw `this` pointer as the baton
for their breakpoint callbacks. Now that disabling a plugin in a process
destroys the plugin instance, a plugin could be destroyed (e.g. by
`plugin disable` running on the command thread) while one of its
breakpoint callbacks was running on another thread (e.g. the thread
handling the stop event), causing a use-after-free.
Try to partially fix this by keeping a plugin alive while the baton is being
used. Now the plugins use a baton created by
`InstrumentationRuntime::CreateBreakpointBaton()` which only holds a weak
reference to the plugin. In a callback,
`InstrumentationRuntime::GetRuntimeFromBaton()` gets a strong reference to the
plugin which keeps the plugin alive until the callback finishes. If the plugin
has already been destroyed the callback doesn't stop.
Because a callback can now hold a reference to a plugin when the plugin
is disabled, the assert that the process holds the only reference to the
plugin is removed. In that case the plugin (and therefore its
breakpoint) is destroyed when the callback finishes.
This doesn't make `Process::m_instrumentation_runtimes` itself thread
safe. Accesses to it from different threads (e.g. disabling a plugin
while `Process::ModulesDidLoad()` iterates over the plugins) are still
unsynchronized. That will be addressed separately in a later patch.
Assisted-by: Claude Code
---
.../lldb/Target/InstrumentationRuntime.h | 12 ++++++++++++
.../ASan/InstrumentationRuntimeASan.cpp | 11 ++++++++---
.../InstrumentationRuntimeBoundsSafety.cpp | 10 ++++++++--
...nstrumentationRuntimeMainThreadChecker.cpp | 10 ++++++++--
.../TSan/InstrumentationRuntimeTSan.cpp | 11 ++++++++---
.../UBSan/InstrumentationRuntimeUBSan.cpp | 9 +++++++--
lldb/source/Target/InstrumentationRuntime.cpp | 14 ++++++++++++++
lldb/source/Target/Process.cpp | 19 ++++++-------------
8 files changed, 71 insertions(+), 25 deletions(-)
diff --git a/lldb/include/lldb/Target/InstrumentationRuntime.h b/lldb/include/lldb/Target/InstrumentationRuntime.h
index d2499528e97abc..838e8fa0d9b23b 100644
--- a/lldb/include/lldb/Target/InstrumentationRuntime.h
+++ b/lldb/include/lldb/Target/InstrumentationRuntime.h
@@ -61,6 +61,18 @@ class InstrumentationRuntime
void SetActive(bool IsActive) { m_is_active = IsActive; }
+ /// Create a baton for this plugin's breakpoint callbacks. The baton only
+ /// holds a weak reference to this plugin so the plugin can be destroyed
+ /// (e.g. when it is disabled) while one of its breakpoint callbacks is
+ /// running on another thread. Callbacks must use GetRuntimeFromBaton() to
+ /// access the plugin.
+ lldb::BatonSP CreateBreakpointBaton();
+
+ /// \return the plugin that \p baton (created by CreateBreakpointBaton())
+ /// refers to or nullptr if the plugin has been destroyed. The plugin is kept
+ /// alive while the returned pointer is held.
+ static lldb::InstrumentationRuntimeSP GetRuntimeFromBaton(void *baton);
+
/// Return a regular expression which can be used to identify a valid version
/// of the runtime library.
virtual const RegularExpression &GetPatternForRuntimeLibrary() = 0;
diff --git a/lldb/source/Plugins/InstrumentationRuntime/ASan/InstrumentationRuntimeASan.cpp b/lldb/source/Plugins/InstrumentationRuntime/ASan/InstrumentationRuntimeASan.cpp
index af30715294bd60..74870ff6c52f1d 100644
--- a/lldb/source/Plugins/InstrumentationRuntime/ASan/InstrumentationRuntimeASan.cpp
+++ b/lldb/source/Plugins/InstrumentationRuntime/ASan/InstrumentationRuntimeASan.cpp
@@ -251,8 +251,13 @@ bool InstrumentationRuntimeASan::NotifyBreakpointHit(
if (!baton)
return false;
+ // Hold a reference to the plugin while using it. The plugin might have been
+ // destroyed (e.g. because it was disabled) in which case don't stop.
+ InstrumentationRuntimeSP runtime_sp = GetRuntimeFromBaton(baton);
+ if (!runtime_sp)
+ return false;
InstrumentationRuntimeASan *const instance =
- static_cast<InstrumentationRuntimeASan *>(baton);
+ static_cast<InstrumentationRuntimeASan *>(runtime_sp.get());
ProcessSP process_sp = instance->GetProcessSP();
@@ -317,8 +322,8 @@ void InstrumentationRuntimeASan::Activate() {
const bool sync = false;
- breakpoint->SetCallback(InstrumentationRuntimeASan::NotifyBreakpointHit, this,
- sync);
+ breakpoint->SetCallback(InstrumentationRuntimeASan::NotifyBreakpointHit,
+ CreateBreakpointBaton(), sync);
breakpoint->SetBreakpointKind("address-sanitizer-report");
SetBreakpointID(breakpoint->GetID());
diff --git a/lldb/source/Plugins/InstrumentationRuntime/BoundsSafety/InstrumentationRuntimeBoundsSafety.cpp b/lldb/source/Plugins/InstrumentationRuntime/BoundsSafety/InstrumentationRuntimeBoundsSafety.cpp
index 506785ad15e1b4..02398b7607f7c1 100644
--- a/lldb/source/Plugins/InstrumentationRuntime/BoundsSafety/InstrumentationRuntimeBoundsSafety.cpp
+++ b/lldb/source/Plugins/InstrumentationRuntime/BoundsSafety/InstrumentationRuntimeBoundsSafety.cpp
@@ -410,8 +410,13 @@ bool InstrumentationRuntimeBoundsSafety::NotifyBreakpointHit(
if (!baton)
return false; ///< false => resume execution.
+ // Hold a reference to the plugin while using it. The plugin might have been
+ // destroyed (e.g. because it was disabled) in which case don't stop.
+ InstrumentationRuntimeSP runtime_sp = GetRuntimeFromBaton(baton);
+ if (!runtime_sp)
+ return false;
InstrumentationRuntimeBoundsSafety *const instance =
- static_cast<InstrumentationRuntimeBoundsSafety *>(baton);
+ static_cast<InstrumentationRuntimeBoundsSafety *>(runtime_sp.get());
ProcessSP process_sp = instance->GetProcessSP();
if (!process_sp)
@@ -474,7 +479,8 @@ void InstrumentationRuntimeBoundsSafety::Activate() {
// Note: When `sync=true` the suggested stackframe is completely ignored. So
// we use `sync=false`. Is that a bug?
breakpoint->SetCallback(
- InstrumentationRuntimeBoundsSafety::NotifyBreakpointHit, this,
+ InstrumentationRuntimeBoundsSafety::NotifyBreakpointHit,
+ CreateBreakpointBaton(),
/*sync=*/false);
breakpoint->SetBreakpointKind("bounds-safety-soft-trap");
SetBreakpointID(breakpoint->GetID());
diff --git a/lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp b/lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp
index 3b63ca21930282..8149c33dc84c43 100644
--- a/lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp
+++ b/lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp
@@ -155,8 +155,13 @@ bool InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit(
if (!baton)
return false; ///< false => resume execution.
+ // Hold a reference to the plugin while using it. The plugin might have been
+ // destroyed (e.g. because it was disabled) in which case don't stop.
+ InstrumentationRuntimeSP runtime_sp = GetRuntimeFromBaton(baton);
+ if (!runtime_sp)
+ return false;
InstrumentationRuntimeMainThreadChecker *const instance =
- static_cast<InstrumentationRuntimeMainThreadChecker *>(baton);
+ static_cast<InstrumentationRuntimeMainThreadChecker *>(runtime_sp.get());
ProcessSP process_sp = instance->GetProcessSP();
ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
@@ -217,7 +222,8 @@ void InstrumentationRuntimeMainThreadChecker::Activate() {
.get();
const bool sync = false;
breakpoint->SetCallback(
- InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit, this, sync);
+ InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit,
+ CreateBreakpointBaton(), sync);
breakpoint->SetBreakpointKind("main-thread-checker-report");
SetBreakpointID(breakpoint->GetID());
diff --git a/lldb/source/Plugins/InstrumentationRuntime/TSan/InstrumentationRuntimeTSan.cpp b/lldb/source/Plugins/InstrumentationRuntime/TSan/InstrumentationRuntimeTSan.cpp
index 74bd3ca25bc6f4..a1a8ea2d2808b6 100644
--- a/lldb/source/Plugins/InstrumentationRuntime/TSan/InstrumentationRuntimeTSan.cpp
+++ b/lldb/source/Plugins/InstrumentationRuntime/TSan/InstrumentationRuntimeTSan.cpp
@@ -802,8 +802,13 @@ bool InstrumentationRuntimeTSan::NotifyBreakpointHit(
if (!baton)
return false;
+ // Hold a reference to the plugin while using it. The plugin might have been
+ // destroyed (e.g. because it was disabled) in which case don't stop.
+ InstrumentationRuntimeSP runtime_sp = GetRuntimeFromBaton(baton);
+ if (!runtime_sp)
+ return false;
InstrumentationRuntimeTSan *const instance =
- static_cast<InstrumentationRuntimeTSan *>(baton);
+ static_cast<InstrumentationRuntimeTSan *>(runtime_sp.get());
ProcessSP process_sp = instance->GetProcessSP();
@@ -925,8 +930,8 @@ void InstrumentationRuntimeTSan::Activate() {
process_sp->GetTarget()
.CreateBreakpoint(symbol_address, internal, hardware)
.get();
- breakpoint->SetCallback(InstrumentationRuntimeTSan::NotifyBreakpointHit, this,
- sync);
+ breakpoint->SetCallback(InstrumentationRuntimeTSan::NotifyBreakpointHit,
+ CreateBreakpointBaton(), sync);
breakpoint->SetBreakpointKind("thread-sanitizer-report");
SetBreakpointID(breakpoint->GetID());
diff --git a/lldb/source/Plugins/InstrumentationRuntime/UBSan/InstrumentationRuntimeUBSan.cpp b/lldb/source/Plugins/InstrumentationRuntime/UBSan/InstrumentationRuntimeUBSan.cpp
index 1db85e68156362..dd57b2b6b0f9a7 100644
--- a/lldb/source/Plugins/InstrumentationRuntime/UBSan/InstrumentationRuntimeUBSan.cpp
+++ b/lldb/source/Plugins/InstrumentationRuntime/UBSan/InstrumentationRuntimeUBSan.cpp
@@ -202,8 +202,13 @@ bool InstrumentationRuntimeUBSan::NotifyBreakpointHit(
if (!baton)
return false; ///< false => resume execution.
+ // Hold a reference to the plugin while using it. The plugin might have been
+ // destroyed (e.g. because it was disabled) in which case don't stop.
+ InstrumentationRuntimeSP runtime_sp = GetRuntimeFromBaton(baton);
+ if (!runtime_sp)
+ return false;
InstrumentationRuntimeUBSan *const instance =
- static_cast<InstrumentationRuntimeUBSan *>(baton);
+ static_cast<InstrumentationRuntimeUBSan *>(runtime_sp.get());
ProcessSP process_sp = instance->GetProcessSP();
ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
@@ -275,7 +280,7 @@ void InstrumentationRuntimeUBSan::Activate() {
.get();
const bool sync = false;
breakpoint->SetCallback(InstrumentationRuntimeUBSan::NotifyBreakpointHit,
- this, sync);
+ CreateBreakpointBaton(), sync);
breakpoint->SetBreakpointKind("undefined-behavior-sanitizer-report");
SetBreakpointID(breakpoint->GetID());
diff --git a/lldb/source/Target/InstrumentationRuntime.cpp b/lldb/source/Target/InstrumentationRuntime.cpp
index 09b23802bf4712..0da25669839923 100644
--- a/lldb/source/Target/InstrumentationRuntime.cpp
+++ b/lldb/source/Target/InstrumentationRuntime.cpp
@@ -11,12 +11,26 @@
#include "lldb/Core/ModuleList.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Target/Process.h"
+#include "lldb/Utility/Baton.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/lldb-private.h"
using namespace lldb;
using namespace lldb_private;
+lldb::BatonSP InstrumentationRuntime::CreateBreakpointBaton() {
+ return std::make_shared<TypedBaton<std::weak_ptr<InstrumentationRuntime>>>(
+ std::make_unique<std::weak_ptr<InstrumentationRuntime>>(
+ weak_from_this()));
+}
+
+lldb::InstrumentationRuntimeSP
+InstrumentationRuntime::GetRuntimeFromBaton(void *baton) {
+ if (!baton)
+ return nullptr;
+ return static_cast<std::weak_ptr<InstrumentationRuntime> *>(baton)->lock();
+}
+
void InstrumentationRuntime::ModulesDidLoad(
lldb_private::ModuleList &module_list, lldb_private::Process *process,
InstrumentationRuntimeCollection &runtimes) {
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index 02318ab8d7ddae..3ec5131b3a9a85 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -6532,23 +6532,16 @@ Process::SetInstrumentationRuntimeEnabled(InstrumentationRuntimeType irt,
return llvm::createStringErrorV("process {} is not alive", GetID());
if (!enabled) {
- InstrumentationRuntimeSP &runtime_sp = m_instrumentation_runtimes[irt];
- // Destroy any instance of this plugin. We rely on the plugin's destructor
- // being implicitly called here to deactivate the plugin (e.g. remove its
- // breakpoints). That only happens if this map holds the only reference to
- // the plugin.
- assert((!runtime_sp || runtime_sp.use_count() == 1) &&
- "instrumentation runtime plugin unexpectedly still referenced");
- // FIXME: The plugins pass a raw `this` pointer as the baton for their
- // breakpoint callbacks. If one of this plugin's breakpoint callbacks is
- // running on another thread (e.g. the thread handling the stop event) when
- // the plugin is disabled, destroying the plugin here causes a
- // use-after-free.
+ // Destroy any instance of this plugin. The plugin's destructor deactivates
+ // the plugin (e.g. removes its breakpoints). If one of the plugin's
+ // breakpoint callbacks is running on another thread (e.g. the thread
+ // handling the stop event) then the callback holds a reference to the
+ // plugin so the plugin is only destroyed once the callback finishes.
//
// A null entry is stored rather than removing the entry so that
// `InstrumentationRuntime::ModulesDidLoad` doesn't create a new instance
// (e.g. after dlopen) if the plugin is still enabled globally.
- runtime_sp = nullptr;
+ m_instrumentation_runtimes[irt] = nullptr;
return llvm::Error::success();
}
More information about the llvm-branch-commits
mailing list