aboutsummaryrefslogtreecommitdiff
path: root/lldb/unittests/Language/CPlusPlus/LibStdcppTupleTest.cpp
blob: ccdd4a445b5764f489964483db657bf513dd4933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//===----------------------------------------------------------------------===//
//
// 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 "Plugins/Language/CPlusPlus/LibStdcpp.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "TestingSupport/SubsystemRAII.h"
#include "TestingSupport/Symbol/ClangTestUtils.h"
#include "lldb/DataFormatters/TypeSynthetic.h"
#include "lldb/ValueObject/ValueObject.h"

#include "gtest/gtest.h"

using namespace lldb;
using namespace lldb_private;

namespace {

/// A minimal ValueObject mock that returns null from GetChildAtIndex for a
/// specified index. This simulates the crash scenario where GetChildAtIndex()
/// returns null due to incomplete debug info or type system errors.
class ValueObjectWithNullChild : public ValueObject {
public:
  static lldb::ValueObjectSP Create(CompilerType type, ConstString name,
                                    size_t null_child_idx) {
    auto manager = ValueObjectManager::Create();
    auto *obj =
        new ValueObjectWithNullChild(*manager, type, name, null_child_idx);
    return obj->GetSP();
  }

  ~ValueObjectWithNullChild() override = default;

  llvm::Expected<uint64_t> GetByteSize() override { return 4; }

  lldb::ValueType GetValueType() const override {
    return lldb::eValueTypeConstResult;
  }

  llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override {
    auto num_or_err = m_type.GetNumChildren(true, nullptr);
    if (!num_or_err)
      return num_or_err.takeError();
    return *num_or_err;
  }

  ConstString GetTypeName() override { return m_type.GetTypeName(); }

  ConstString GetDisplayTypeName() override { return GetTypeName(); }

  bool IsInScope() override { return true; }

protected:
  bool UpdateValue() override {
    m_error.Clear();
    return true;
  }

  CompilerType GetCompilerTypeImpl() override { return m_type; }

  /// This is the key method - return null for the specified child index
  /// to simulate the crash scenario.
  ValueObject *CreateChildAtIndex(size_t idx) override {
    if (idx == m_null_child_idx) {
      return nullptr; // Return null to trigger the crash scenario!
    }
    return ValueObject::CreateChildAtIndex(idx);
  }

private:
  ValueObjectWithNullChild(ValueObjectManager &manager, CompilerType type,
                           ConstString name, size_t null_child_idx)
      : ValueObject(nullptr, manager), m_type(type),
        m_null_child_idx(null_child_idx) {
    SetName(name);
  }

  CompilerType m_type;
  size_t m_null_child_idx;
};

class LibStdcppTupleTest : public ::testing::Test {
public:
  SubsystemRAII<FileSystem, HostInfo> m_subsystems;

  void SetUp() override {
    m_holder = std::make_unique<clang_utils::TypeSystemClangHolder>("test");
    m_type_system = m_holder->GetAST();
  }

  /// Create a struct type with a child named "std::_Tuple_impl<0, int>"
  /// to trigger the tuple synthetic frontend's child iteration.
  CompilerType CreateTypeWithTupleImplChild() {
    // Create outer type
    CompilerType outer_type = m_type_system->CreateRecordType(
        m_type_system->getASTContext().getTranslationUnitDecl(),
        OptionalClangModuleID(), lldb::AccessType::eAccessPublic,
        "std::tuple<int>", 0, lldb::LanguageType::eLanguageTypeC_plus_plus);

    // Create inner _Tuple_impl type
    CompilerType inner_type = m_type_system->CreateRecordType(
        m_type_system->getASTContext().getTranslationUnitDecl(),
        OptionalClangModuleID(), lldb::AccessType::eAccessPublic,
        "std::_Tuple_impl<0, int>", 0,
        lldb::LanguageType::eLanguageTypeC_plus_plus);

    TypeSystemClang::StartTagDeclarationDefinition(inner_type);
    TypeSystemClang::CompleteTagDeclarationDefinition(inner_type);

    // Add the inner type as a field of the outer type
    TypeSystemClang::StartTagDeclarationDefinition(outer_type);
    m_type_system->AddFieldToRecordType(outer_type, "std::_Tuple_impl<0, int>",
                                        inner_type,
                                        lldb::AccessType::eAccessPublic, 0);
    TypeSystemClang::CompleteTagDeclarationDefinition(outer_type);

    return outer_type;
  }

  TypeSystemClang *m_type_system;

private:
  std::unique_ptr<clang_utils::TypeSystemClangHolder> m_holder;
};

} // anonymous namespace

TEST_F(LibStdcppTupleTest, CreatorHandlesNullValueObject) {
  auto *frontend = formatters::LibStdcppTupleSyntheticFrontEndCreator(
      nullptr, lldb::ValueObjectSP());
  EXPECT_EQ(frontend, nullptr);
}

/// This test verifies the null child handling fix.
/// It creates a ValueObject that returns null from GetChildAtIndex(0),
/// simulating the crash scenario from incomplete debug info.
/// WITHOUT the fix (null check), this test will crash with SIGSEGV.
/// WITH the fix, this test passes.
TEST_F(LibStdcppTupleTest, UpdateHandlesNullChild) {
  CompilerType type = CreateTypeWithTupleImplChild();

  // Create a ValueObject that returns null for child at index 0
  auto valobj_sp = ValueObjectWithNullChild::Create(
      type, ConstString("test_tuple"), 0 /* null_child_idx */);
  ASSERT_TRUE(valobj_sp);

  // Verify our mock returns null for child 0
  ASSERT_FALSE(valobj_sp->GetChildAtIndex(0));

  // Create the frontend - this calls Update() which iterates through children.
  // WITHOUT the null check fix, this crashes with SIGSEGV when trying to call
  // GetName() on a null child_sp.
  // WITH the fix, this succeeds because null children are skipped.
  auto *frontend =
      formatters::LibStdcppTupleSyntheticFrontEndCreator(nullptr, valobj_sp);
  ASSERT_NE(frontend, nullptr);

  // If we get here, the null check worked.
  auto num_children = frontend->CalculateNumChildren();
  ASSERT_TRUE(static_cast<bool>(num_children));

  delete frontend;
}