← Back to Pull requests

ValueImpl::GetSP was calling GetError before acquiring the StopLocker. #225976

Open
jimingham opened this pull request with 29 lines diff across 2 files
View on GitHub
jimingham jimingham commented on Sep 24, 2026 00:06 UTC
Author

GetError calls UpdateValueIfNeeded, and if you do that without keeping the process stopped, Resume could invalidate structures needed by UpdateValueIfNeeded, leading to crashes.

This was the cause of a small trickle of crashes we've been seeing when lldb is used under a GUI (in this case Xcode) that has more concurrency that a command-line lldb session. The typical example showed the internal-state-thread and the gdb-remote async thread either resuming the target or waiting for it to stop again, and then another thread was servicing an SBValue::IsInScope() or SBValue::GetType() or some other such API. That called ValueImpl::GetSP->ValueObject::UpdateValueIfNeeded and then it crashed fetching registers from the ValueObject's StackFrame or some other part of the stopped process state.

So in this patch, I added a PeekError that only fetches the current error but doesn't update, and use that in the one case where we're returning the ValueObjectSP held by the SBValue but can't get the stop locker.

I couldn't figure out how to craft a test case that failed the way our reported crashes did. You have to be pretty unlucky to hit this crash.

Diff: 29 lines (+25 / -4) across 2 files Reviewer: JDevlieghere Review duration: 101624m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.32) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.32 · 29 lines diff across 2 files
0.32
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.95 · 5 substantive comment(s)
0.95
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
87%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.203
Real PR Conversation Timeline
12 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Sep 24, 2026 00:07 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: jimingham

<details>
<summary>Changes</summary>

GetError calls UpdateValueIfNeeded, and if you do that without keeping the process stopped, Resume could invalidate structures needed by UpdateValueIfNeeded, leading to crashes.

This was the cause of a small trickle of crashes we've been seeing when lldb is used under a GUI (in this case Xcode) that has more concurrency that a command-line lldb session. The typical example showed the internal-state-thread and the gdb-remote async thread either resuming the target or waiting for it to stop again, and then another thread was servicing an SBValue::IsInScope() or SBValue::GetType() or some other such API. That called ValueImpl::GetSP-&gt;ValueObject::UpdateValueIfNeeded and then it crashed fetching registers from the ValueObject's StackFrame or some other part of the stopped process state.

So in this patch, I added a PeekError that only fetches the current error but doesn't update, and use that in the one case where we're returning the ValueObjectSP held by the SBValue but can't get the stop locker.

I couldn't figure out how to craft a test case that failed the way our reported crashes did. You have to be pretty unlucky to hit this crash.

---
Full diff: https://github.com/llvm/llvm-project/pull/225976.diff

2 Files Affected:

  • (modified) lldb/include/lldb/ValueObject/ValueObject.h (+7)

  • (modified) lldb/source/ValueObject/ValueObject.cpp (+21-4)
  • ``````

    diff --git a/lldb/include/lldb/ValueObject/ValueObject.h b/lldb/include/lldb/ValueObject/ValueObject.h
    index c336213c84e25..d8aefcb658aa5 100644
    --- a/lldb/include/lldb/ValueObject/ValueObject.h
    +++ b/lldb/include/lldb/ValueObject/ValueObject.h
    @@ -480,8 +480,15 @@ class ValueObject {
       virtual bool GetDeclaration(Declaration &decl);
     
       // The functions below should NOT be modified by subclasses
    +  /// Updates the ValueObject's value if needed, and then return the
    +  /// current error state.  This should only be called if the stop locker is
    +  /// held so the process can't resume while this is in flight.
       const Status &GetError();
     
    +  /// Return the current error without updating.  Only use this when you need
    +  /// the error but it is not safe to update the value.
    +  const Status &PeekError();
    +
       ConstString GetName() const { return m_name; }
     
       /// Returns a unique id for this ValueObject.
    diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp
    index e7d3b7ce5be6a..430d7639b1c23 100644
    --- a/lldb/source/ValueObject/ValueObject.cpp
    +++ b/lldb/source/ValueObject/ValueObject.cpp
    @@ -291,6 +291,10 @@ const Status &ValueObject::GetError() {
       return m_error;
     }
     
    +const Status &ValueObject::PeekError() {
    +  return m_error;
    +}
    +
     const char *ValueObject::GetLocationAsCStringImpl(const Value &value,
                                                       const DataExtractor &data) {
       if (UpdateValueIfNeeded(false)) {
    @@ -3893,12 +3897,13 @@ lldb::ValueObjectSP ValueImpl::GetSP(Process::StopLocker &stop_locker,
       lldb::ValueObjectSP value_sp = m_valobj_sp;
     
       Target *target = value_sp->GetTargetSP().get();
    -  // If this ValueObject holds an error, then it is valuable for that.
    -  if (value_sp->GetError().Fail())
    -    return value_sp;
     
    -  if (!target)
    +  if (!target) {
    +  // If this ValueObject holds an error, then it is valuable for that.
    +    if (value_sp->GetError().Fail())
    +      return value_sp;
         return ValueObjectSP();
    +  }
     
       api_mutex = target->GetAPIMutex();
       lock = std::unique_lock<TargetAPIMutex>(api_mutex);
    @@ -3909,9 +3914,21 @@ lldb::ValueObjectSP ValueImpl::GetSP(Process::StopLocker &stop_locker,
         // is running. If you want to look at values, pause the process, then
         // look.
         error = Status::FromErrorString("process must be stopped.");
    +    // We still want to return a value object if it was in an error state, but
    +    // we can't call GetError here, since that would call UpdateValueIfNeeded
    +    // which isn't safe to do without holding the stop locker.
    +    if (value_sp->PeekError().Fail())
    +      return value_sp;
    +
         return ValueObjectSP();
       }
     
    +  // Now we can safely get the ValueObject to update itself and if that results
    +  // in an error, return this ValueObject since it holds the error:
    +  // If this ValueObject holds an error, then it is valuable for that.
    +  if (value_sp->GetError().Fail())
    +    return value_sp;
    +
       if (m_use_dynamic != eNoDynamicValues) {
         ValueObjectSP dynamic_sp = value_sp->GetDynamicValue(m_use_dynamic);
         if (dynamic_sp)
    
    
    ``````

    </details>

    github-actions[bot] github-actions[bot] commented on Sep 24, 2026 00:11 UTC
    Bot

    <!--LLVM CODE FORMAT COMMENT: {clang-format}-->

    :white_check_mark: With the latest revision this PR passed the C/C++ code formatter.

    JDevlieghere JDevlieghere commented on code on Sep 25, 2026 19:00 UTC
    Code Review
    lldb/source/ValueObject/ValueObject.cpp:1
    @@ -3893,12 +3895,13 @@ lldb::ValueObjectSP ValueImpl::GetSP(Process::StopLocker &stop_locker,
       lldb::ValueObjectSP value_sp = m_valobj_sp;
     
       Target *target = value_sp->GetTargetSP().get();
    -  // If this ValueObject holds an error, then it is valuable for that.
    -  if (value_sp->GetError().Fail())
    -    return value_sp;
     
    -  if (!target)
    +  if (!target) {

    ValueImpl::GetSP seems to repeats the same 'return value_sp if its cached error failed' idiom three times (lines ~3901, ~3918, ~3926) with three slightly different preconditions and getters (GetError/PeekError/GetError). Could this be collapsed into a single check driven by a 'safe_to_update' boolean?

    JDevlieghere JDevlieghere commented on code on Sep 25, 2026 19:01 UTC
    Code Review
    lldb/source/ValueObject/ValueObject.cpp:1
    @@ -3909,9 +3912,20 @@ lldb::ValueObjectSP ValueImpl::GetSP(Process::StopLocker &stop_locker,
         // is running. If you want to look at values, pause the process, then
         // look.
         error = Status::FromErrorString("process must be stopped.");
    +    // We still want to return a value object if it was in an error state, but
    +    // we can't call GetError here, since that would call UpdateValueIfNeeded
    +    // which isn't safe to do without holding the stop locker.
    +    if (value_sp->PeekError().Fail())
    +      return value_sp;
    +
         return ValueObjectSP();
       }
     
    +  // Now we can safely get the ValueObject to update itself and if that results

    When PeekError().Fail() is true, GetSP returns the errored value_sp but leaves the separate error out-parameter set to the generic "process must be stopped." string. WE should clear it or reflect the real cached error.

    jimingham jimingham commented on code on Sep 25, 2026 22:05 UTC
    Code Review
    lldb/source/ValueObject/ValueObject.cpp:1
    @@ -3909,9 +3912,20 @@ lldb::ValueObjectSP ValueImpl::GetSP(Process::StopLocker &stop_locker,
         // is running. If you want to look at values, pause the process, then
         // look.
         error = Status::FromErrorString("process must be stopped.");
    +    // We still want to return a value object if it was in an error state, but
    +    // we can't call GetError here, since that would call UpdateValueIfNeeded
    +    // which isn't safe to do without holding the stop locker.
    +    if (value_sp->PeekError().Fail())
    +      return value_sp;
    +
         return ValueObjectSP();
       }
     
    +  // Now we can safely get the ValueObject to update itself and if that results

    I think we need to return some error here, but it isn't the ValueObject's error.

    Because we couldn't actually update the value, the error is likely stale. But we actually have no way of telling how stale it is. If you had continued 5 times w/o ever checking this ValueObject, then it would be 5 continues stale, for instance. OTOH it might be a ValueObjectConstResult in which case updates aren't relevant and the error is always accurate.

    So the side-channel error we need to return says "we couldn't update the VO because the process is running returning cached error value instead". I can make that a separate error from "returning an empty VO since we couldn't update the value" which we emit in the case where the previous error state was "Successful".

    hawkinsw hawkinsw commented on code on Sep 26, 2026 07:24 UTC
    Code Review
    lldb/include/lldb/ValueObject/ValueObject.h:1
    @@ -480,8 +480,15 @@ class ValueObject {
       virtual bool GetDeclaration(Declaration &decl);
     
       // The functions below should NOT be modified by subclasses
    +  /// Updates the ValueObject's value if needed, and then returns the
    +  /// current error state.  This should only be called if the stop locker is

      /// current error state.  This function should only be called if the stop locker is
    

    hawkinsw hawkinsw commented on code on Sep 26, 2026 07:25 UTC
    Code Review
    lldb/include/lldb/ValueObject/ValueObject.h:1
    @@ -480,8 +480,15 @@ class ValueObject {
       virtual bool GetDeclaration(Declaration &decl);
     
       // The functions below should NOT be modified by subclasses
    +  /// Updates the ValueObject's value if needed, and then returns the
    +  /// current error state.  This should only be called if the stop locker is
    +  /// held so the process can't resume while this is in flight.

      /// held so the process can't resume while this update/access is in flight.
    

    hawkinsw hawkinsw commented on code on Sep 26, 2026 07:26 UTC
    Code Review
    lldb/include/lldb/ValueObject/ValueObject.h:1
    @@ -480,8 +480,15 @@ class ValueObject {
       virtual bool GetDeclaration(Declaration &decl);
     
       // The functions below should NOT be modified by subclasses
    +  /// Updates the ValueObject's value if needed, and then returns the
    +  /// current error state.  This should only be called if the stop locker is
    +  /// held so the process can't resume while this is in flight.
       const Status &GetError();
     
    +  /// Return the current error without updating.  Only use this when you need

      /// Return the current error without updating.  Only use this function when you need
    

    hawkinsw hawkinsw commented on code on Sep 26, 2026 07:28 UTC
    Code Review
    lldb/source/ValueObject/ValueObject.cpp:1
    @@ -3909,9 +3912,20 @@ lldb::ValueObjectSP ValueImpl::GetSP(Process::StopLocker &stop_locker,
         // is running. If you want to look at values, pause the process, then
         // look.
         error = Status::FromErrorString("process must be stopped.");
    +    // We still want to return a value object if it was in an error state, but
    +    // we can't call GetError here, since that would call UpdateValueIfNeeded
    +    // which isn't safe to do without holding the stop locker.
    +    if (value_sp->PeekError().Fail())
    +      return value_sp;
    +
         return ValueObjectSP();
       }
     
    +  // Now we can safely get the ValueObject to update itself and if that results
    +  // in an error, return this ValueObject since it holds the error:

      // in an error, return this ValueObject because it holds the error:
    

    hawkinsw hawkinsw commented on Sep 26, 2026 07:32 UTC
    Reviewer

    I wish that I could be more helpful, but suggesting some minor changes to the comments is really all that I can do. I hope that they are useful.

    jimingham jimingham commented on code on Sep 30, 2026 18:50 UTC
    Code Review
    lldb/source/ValueObject/ValueObject.cpp:1
    @@ -3893,12 +3895,13 @@ lldb::ValueObjectSP ValueImpl::GetSP(Process::StopLocker &stop_locker,
       lldb::ValueObjectSP value_sp = m_valobj_sp;
     
       Target *target = value_sp->GetTargetSP().get();
    -  // If this ValueObject holds an error, then it is valuable for that.
    -  if (value_sp->GetError().Fail())
    -    return value_sp;
     
    -  if (!target)
    +  if (!target) {

    I don't think that makes anything clearer. If it's not safe to update, we want to figure out the error we can return (with PeekError()) and then not do any more work. If we instead set the safe_to_update boolean, we'd then have to use it to decide whether to call PeekError or GetError, and then we'd have to check it again and return an error if it was not safe to update.

    That makes it less clear that we need to not do any more work here if we can't get the locker.

    Reviewers
    Assignees
    jimingham jimingham
    Labels
    risk: low confidence: 87.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 31.8% (LOW) · Higher = larger blast radius
    31.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 87.4% · Lower = rushed or shallow review
    87.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4% · Safe to merge
    4%