Bug Summary

File:lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp
Warning:line 182, column 34
Potential leak of memory pointed to by 'synthetic_children'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name BlockPointer.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -isystem /usr/include/libxml2 -D HAVE_ROUND -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/lldb/source/Plugins/Language/CPlusPlus -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/lldb/source/Plugins/Language/CPlusPlus -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/lldb/include -I tools/lldb/include -I include -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/llvm/include -I /usr/include/python3.9 -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/clang/include -I tools/lldb/../clang/include -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/lldb/source -I tools/lldb/source -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-deprecated-register -Wno-vla-extension -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-19-134126-35450-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp
1//===-- BlockPointer.cpp --------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "BlockPointer.h"
10
11#include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
12#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
13#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
14#include "lldb/Core/ValueObject.h"
15#include "lldb/DataFormatters/FormattersHelpers.h"
16#include "lldb/Symbol/CompilerType.h"
17#include "lldb/Symbol/TypeSystem.h"
18#include "lldb/Target/Target.h"
19#include "lldb/Utility/LLDBAssert.h"
20#include "lldb/Utility/Log.h"
21
22using namespace lldb;
23using namespace lldb_private;
24using namespace lldb_private::formatters;
25
26namespace lldb_private {
27namespace formatters {
28
29class BlockPointerSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
30public:
31 BlockPointerSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
32 : SyntheticChildrenFrontEnd(*valobj_sp), m_block_struct_type() {
33 CompilerType block_pointer_type(m_backend.GetCompilerType());
34 CompilerType function_pointer_type;
35 block_pointer_type.IsBlockPointerType(&function_pointer_type);
36
37 TargetSP target_sp(m_backend.GetTargetSP());
38
39 if (!target_sp) {
40 return;
41 }
42
43 auto type_system_or_err = target_sp->GetScratchTypeSystemForLanguage(
44 lldb::eLanguageTypeC_plus_plus);
45 if (auto err = type_system_or_err.takeError()) {
46 LLDB_LOG_ERROR(do { ::lldb_private::Log *log_private = (lldb_private::GetLogIfAnyCategoriesSet
((1u << 29))); ::llvm::Error error_private = (std::move
(err)); if (log_private && error_private) { log_private
->FormatError(::std::move(error_private), "lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp"
, __func__, "Failed to get scratch TypeSystemClang"); } else ::
llvm::consumeError(::std::move(error_private)); } while (0)
47 lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS),do { ::lldb_private::Log *log_private = (lldb_private::GetLogIfAnyCategoriesSet
((1u << 29))); ::llvm::Error error_private = (std::move
(err)); if (log_private && error_private) { log_private
->FormatError(::std::move(error_private), "lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp"
, __func__, "Failed to get scratch TypeSystemClang"); } else ::
llvm::consumeError(::std::move(error_private)); } while (0)
48 std::move(err), "Failed to get scratch TypeSystemClang")do { ::lldb_private::Log *log_private = (lldb_private::GetLogIfAnyCategoriesSet
((1u << 29))); ::llvm::Error error_private = (std::move
(err)); if (log_private && error_private) { log_private
->FormatError(::std::move(error_private), "lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp"
, __func__, "Failed to get scratch TypeSystemClang"); } else ::
llvm::consumeError(::std::move(error_private)); } while (0)
;
49 return;
50 }
51
52 TypeSystemClang *clang_ast_context =
53 llvm::cast<TypeSystemClang>(block_pointer_type.GetTypeSystem());
54
55 std::shared_ptr<ClangASTImporter> clang_ast_importer;
56 auto *state = target_sp->GetPersistentExpressionStateForLanguage(
57 lldb::eLanguageTypeC_plus_plus);
58 if (state) {
59 auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);
60 clang_ast_importer = persistent_vars->GetClangASTImporter();
61 }
62
63 if (!clang_ast_importer) {
64 return;
65 }
66
67 const char *const isa_name("__isa");
68 const CompilerType isa_type =
69 clang_ast_context->GetBasicType(lldb::eBasicTypeObjCClass);
70 const char *const flags_name("__flags");
71 const CompilerType flags_type =
72 clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
73 const char *const reserved_name("__reserved");
74 const CompilerType reserved_type =
75 clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
76 const char *const FuncPtr_name("__FuncPtr");
77
78 m_block_struct_type = clang_ast_context->CreateStructForIdentifier(
79 ConstString(), {{isa_name, isa_type},
80 {flags_name, flags_type},
81 {reserved_name, reserved_type},
82 {FuncPtr_name, function_pointer_type}});
83 }
84
85 ~BlockPointerSyntheticFrontEnd() override = default;
86
87 size_t CalculateNumChildren() override {
88 const bool omit_empty_base_classes = false;
89 return m_block_struct_type.GetNumChildren(omit_empty_base_classes, nullptr);
90 }
91
92 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
93 if (!m_block_struct_type.IsValid()) {
94 return lldb::ValueObjectSP();
95 }
96
97 if (idx >= CalculateNumChildren()) {
98 return lldb::ValueObjectSP();
99 }
100
101 const bool thread_and_frame_only_if_stopped = true;
102 ExecutionContext exe_ctx = m_backend.GetExecutionContextRef().Lock(
103 thread_and_frame_only_if_stopped);
104 const bool transparent_pointers = false;
105 const bool omit_empty_base_classes = false;
106 const bool ignore_array_bounds = false;
107 ValueObject *value_object = nullptr;
108
109 std::string child_name;
110 uint32_t child_byte_size = 0;
111 int32_t child_byte_offset = 0;
112 uint32_t child_bitfield_bit_size = 0;
113 uint32_t child_bitfield_bit_offset = 0;
114 bool child_is_base_class = false;
115 bool child_is_deref_of_parent = false;
116 uint64_t language_flags = 0;
117
118 const CompilerType child_type =
119 m_block_struct_type.GetChildCompilerTypeAtIndex(
120 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
121 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
122 child_bitfield_bit_size, child_bitfield_bit_offset,
123 child_is_base_class, child_is_deref_of_parent, value_object,
124 language_flags);
125
126 ValueObjectSP struct_pointer_sp =
127 m_backend.Cast(m_block_struct_type.GetPointerType());
128
129 if (!struct_pointer_sp) {
130 return lldb::ValueObjectSP();
131 }
132
133 Status err;
134 ValueObjectSP struct_sp = struct_pointer_sp->Dereference(err);
135
136 if (!struct_sp || !err.Success()) {
137 return lldb::ValueObjectSP();
138 }
139
140 ValueObjectSP child_sp(struct_sp->GetSyntheticChildAtOffset(
141 child_byte_offset, child_type, true,
142 ConstString(child_name.c_str(), child_name.size())));
143
144 return child_sp;
145 }
146
147 // return true if this object is now safe to use forever without ever
148 // updating again; the typical (and tested) answer here is 'false'
149 bool Update() override { return false; }
150
151 // maybe return false if the block pointer is, say, null
152 bool MightHaveChildren() override { return true; }
153
154 size_t GetIndexOfChildWithName(ConstString name) override {
155 if (!m_block_struct_type.IsValid())
156 return UINT32_MAX(4294967295U);
157
158 const bool omit_empty_base_classes = false;
159 return m_block_struct_type.GetIndexOfChildWithName(name.AsCString(),
160 omit_empty_base_classes);
161 }
162
163private:
164 CompilerType m_block_struct_type;
165};
166
167} // namespace formatters
168} // namespace lldb_private
169
170bool lldb_private::formatters::BlockPointerSummaryProvider(
171 ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
172 lldb_private::SyntheticChildrenFrontEnd *synthetic_children =
173 BlockPointerSyntheticFrontEndCreator(nullptr, valobj.GetSP());
1
Calling 'BlockPointerSyntheticFrontEndCreator'
4
Returned allocated memory
174 if (!synthetic_children
4.1
'synthetic_children' is non-null
) {
5
Taking false branch
175 return false;
176 }
177
178 synthetic_children->Update();
179
180 static const ConstString s_FuncPtr_name("__FuncPtr");
181
182 lldb::ValueObjectSP child_sp = synthetic_children->GetChildAtIndex(
6
Potential leak of memory pointed to by 'synthetic_children'
183 synthetic_children->GetIndexOfChildWithName(s_FuncPtr_name));
184
185 if (!child_sp) {
186 return false;
187 }
188
189 lldb::ValueObjectSP qualified_child_representation_sp =
190 child_sp->GetQualifiedRepresentationIfAvailable(
191 lldb::eDynamicDontRunTarget, true);
192
193 const char *child_value =
194 qualified_child_representation_sp->GetValueAsCString();
195
196 s.Printf("%s", child_value);
197
198 return true;
199}
200
201lldb_private::SyntheticChildrenFrontEnd *
202lldb_private::formatters::BlockPointerSyntheticFrontEndCreator(
203 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
204 if (!valobj_sp)
2
Taking false branch
205 return nullptr;
206 return new BlockPointerSyntheticFrontEnd(valobj_sp);
3
Memory is allocated
207}