Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/lldb/source/Plugins/Process/elf-core/ThreadElfCore.cpp
Warning:line 179, column 27
Potential leak of memory pointed to by 'reg_interface'

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 ThreadElfCore.cpp -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-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-16/lib/clang/16.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/Process/elf-core -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/lldb/source/Plugins/Process/elf-core -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/lldb/include -I tools/lldb/include -I include -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/include -I /usr/include/python3.9 -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/clang/include -I tools/lldb/../clang/include -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/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-16/lib/clang/16.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-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -O2 -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-misleading-indentation -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-stringop-truncation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -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-09-04-125545-48738-1 -x c++ /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/lldb/source/Plugins/Process/elf-core/ThreadElfCore.cpp
1//===-- ThreadElfCore.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 "lldb/Target/RegisterContext.h"
10#include "lldb/Target/StopInfo.h"
11#include "lldb/Target/Target.h"
12#include "lldb/Target/Unwind.h"
13#include "lldb/Utility/DataExtractor.h"
14#include "lldb/Utility/LLDBLog.h"
15#include "lldb/Utility/Log.h"
16
17#include "Plugins/Process/Utility/RegisterContextFreeBSD_i386.h"
18#include "Plugins/Process/Utility/RegisterContextFreeBSD_mips64.h"
19#include "Plugins/Process/Utility/RegisterContextFreeBSD_powerpc.h"
20#include "Plugins/Process/Utility/RegisterContextFreeBSD_x86_64.h"
21#include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
22#include "Plugins/Process/Utility/RegisterContextLinux_s390x.h"
23#include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
24#include "Plugins/Process/Utility/RegisterContextNetBSD_i386.h"
25#include "Plugins/Process/Utility/RegisterContextNetBSD_x86_64.h"
26#include "Plugins/Process/Utility/RegisterContextOpenBSD_i386.h"
27#include "Plugins/Process/Utility/RegisterContextOpenBSD_x86_64.h"
28#include "Plugins/Process/Utility/RegisterInfoPOSIX_arm.h"
29#include "Plugins/Process/Utility/RegisterInfoPOSIX_arm64.h"
30#include "Plugins/Process/Utility/RegisterInfoPOSIX_ppc64le.h"
31#include "ProcessElfCore.h"
32#include "RegisterContextPOSIXCore_arm.h"
33#include "RegisterContextPOSIXCore_arm64.h"
34#include "RegisterContextPOSIXCore_mips64.h"
35#include "RegisterContextPOSIXCore_powerpc.h"
36#include "RegisterContextPOSIXCore_ppc64le.h"
37#include "RegisterContextPOSIXCore_s390x.h"
38#include "RegisterContextPOSIXCore_x86_64.h"
39#include "ThreadElfCore.h"
40
41#include <memory>
42
43using namespace lldb;
44using namespace lldb_private;
45
46// Construct a Thread object with given data
47ThreadElfCore::ThreadElfCore(Process &process, const ThreadData &td)
48 : Thread(process, td.tid), m_thread_name(td.name), m_thread_reg_ctx_sp(),
49 m_signo(td.signo), m_gpregset_data(td.gpregset), m_notes(td.notes) {}
50
51ThreadElfCore::~ThreadElfCore() { DestroyThread(); }
52
53void ThreadElfCore::RefreshStateAfterStop() {
54 GetRegisterContext()->InvalidateIfNeeded(false);
1
Calling 'ThreadElfCore::GetRegisterContext'
55}
56
57RegisterContextSP ThreadElfCore::GetRegisterContext() {
58 if (!m_reg_context_sp) {
2
Taking true branch
59 m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
3
Calling 'ThreadElfCore::CreateRegisterContextForFrame'
60 }
61 return m_reg_context_sp;
62}
63
64RegisterContextSP
65ThreadElfCore::CreateRegisterContextForFrame(StackFrame *frame) {
66 RegisterContextSP reg_ctx_sp;
67 uint32_t concrete_frame_idx = 0;
68 Log *log = GetLog(LLDBLog::Thread);
69
70 if (frame
3.1
'frame' is null
)
4
Taking false branch
71 concrete_frame_idx = frame->GetConcreteFrameIndex();
72
73 if (concrete_frame_idx
4.1
'concrete_frame_idx' is equal to 0
== 0) {
5
Taking true branch
74 if (m_thread_reg_ctx_sp)
6
Taking false branch
75 return m_thread_reg_ctx_sp;
76
77 ProcessElfCore *process = static_cast<ProcessElfCore *>(GetProcess().get());
78 ArchSpec arch = process->GetArchitecture();
79 RegisterInfoInterface *reg_interface = nullptr;
80
81 switch (arch.GetTriple().getOS()) {
7
Control jumps to 'case FreeBSD:' at line 82
82 case llvm::Triple::FreeBSD: {
83 switch (arch.GetMachine()) {
8
Control jumps to 'case mips64:' at line 93
84 case llvm::Triple::aarch64:
85 case llvm::Triple::arm:
86 break;
87 case llvm::Triple::ppc:
88 reg_interface = new RegisterContextFreeBSD_powerpc32(arch);
89 break;
90 case llvm::Triple::ppc64:
91 reg_interface = new RegisterContextFreeBSD_powerpc64(arch);
92 break;
93 case llvm::Triple::mips64:
94 reg_interface = new RegisterContextFreeBSD_mips64(arch);
9
Memory is allocated
95 break;
10
Execution continues on line 105
96 case llvm::Triple::x86:
97 reg_interface = new RegisterContextFreeBSD_i386(arch);
98 break;
99 case llvm::Triple::x86_64:
100 reg_interface = new RegisterContextFreeBSD_x86_64(arch);
101 break;
102 default:
103 break;
104 }
105 break;
106 }
107
108 case llvm::Triple::NetBSD: {
109 switch (arch.GetMachine()) {
110 case llvm::Triple::aarch64:
111 break;
112 case llvm::Triple::x86:
113 reg_interface = new RegisterContextNetBSD_i386(arch);
114 break;
115 case llvm::Triple::x86_64:
116 reg_interface = new RegisterContextNetBSD_x86_64(arch);
117 break;
118 default:
119 break;
120 }
121 break;
122 }
123
124 case llvm::Triple::Linux: {
125 switch (arch.GetMachine()) {
126 case llvm::Triple::aarch64:
127 break;
128 case llvm::Triple::ppc64le:
129 reg_interface = new RegisterInfoPOSIX_ppc64le(arch);
130 break;
131 case llvm::Triple::systemz:
132 reg_interface = new RegisterContextLinux_s390x(arch);
133 break;
134 case llvm::Triple::x86:
135 reg_interface = new RegisterContextLinux_i386(arch);
136 break;
137 case llvm::Triple::x86_64:
138 reg_interface = new RegisterContextLinux_x86_64(arch);
139 break;
140 default:
141 break;
142 }
143 break;
144 }
145
146 case llvm::Triple::OpenBSD: {
147 switch (arch.GetMachine()) {
148 case llvm::Triple::aarch64:
149 break;
150 case llvm::Triple::x86:
151 reg_interface = new RegisterContextOpenBSD_i386(arch);
152 break;
153 case llvm::Triple::x86_64:
154 reg_interface = new RegisterContextOpenBSD_x86_64(arch);
155 break;
156 default:
157 break;
158 }
159 break;
160 }
161
162 default:
163 break;
164 }
165
166 if (!reg_interface
10.1
'reg_interface' is non-null
&& arch.GetMachine() != llvm::Triple::aarch64 &&
167 arch.GetMachine() != llvm::Triple::arm) {
168 LLDB_LOGF(log, "elf-core::%s:: Architecture(%d) or OS(%d) not supported",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("elf-core::%s:: Architecture(%d) or OS(%d) not supported"
, __FUNCTION__, arch.GetMachine(), arch.GetTriple().getOS());
} while (0)
169 __FUNCTION__, arch.GetMachine(), arch.GetTriple().getOS())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("elf-core::%s:: Architecture(%d) or OS(%d) not supported"
, __FUNCTION__, arch.GetMachine(), arch.GetTriple().getOS());
} while (0)
;
170 assert(false && "Architecture or OS not supported")(static_cast <bool> (false && "Architecture or OS not supported"
) ? void (0) : __assert_fail ("false && \"Architecture or OS not supported\""
, "lldb/source/Plugins/Process/elf-core/ThreadElfCore.cpp", 170
, __extension__ __PRETTY_FUNCTION__))
;
171 }
172
173 switch (arch.GetMachine()) {
11
Control jumps to 'case arm:' at line 178
174 case llvm::Triple::aarch64:
175 m_thread_reg_ctx_sp = RegisterContextCorePOSIX_arm64::Create(
176 *this, arch, m_gpregset_data, m_notes);
177 break;
178 case llvm::Triple::arm:
179 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_arm>(
12
Potential leak of memory pointed to by 'reg_interface'
180 *this, std::make_unique<RegisterInfoPOSIX_arm>(arch), m_gpregset_data,
181 m_notes);
182 break;
183 case llvm::Triple::mipsel:
184 case llvm::Triple::mips:
185 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_mips64>(
186 *this, reg_interface, m_gpregset_data, m_notes);
187 break;
188 case llvm::Triple::mips64:
189 case llvm::Triple::mips64el:
190 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_mips64>(
191 *this, reg_interface, m_gpregset_data, m_notes);
192 break;
193 case llvm::Triple::ppc:
194 case llvm::Triple::ppc64:
195 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_powerpc>(
196 *this, reg_interface, m_gpregset_data, m_notes);
197 break;
198 case llvm::Triple::ppc64le:
199 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_ppc64le>(
200 *this, reg_interface, m_gpregset_data, m_notes);
201 break;
202 case llvm::Triple::systemz:
203 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_s390x>(
204 *this, reg_interface, m_gpregset_data, m_notes);
205 break;
206 case llvm::Triple::x86:
207 case llvm::Triple::x86_64:
208 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_x86_64>(
209 *this, reg_interface, m_gpregset_data, m_notes);
210 break;
211 default:
212 break;
213 }
214
215 reg_ctx_sp = m_thread_reg_ctx_sp;
216 } else {
217 reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
218 }
219 return reg_ctx_sp;
220}
221
222bool ThreadElfCore::CalculateStopInfo() {
223 ProcessSP process_sp(GetProcess());
224 if (process_sp) {
225 SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, m_signo));
226 return true;
227 }
228 return false;
229}
230
231// Parse PRSTATUS from NOTE entry
232ELFLinuxPrStatus::ELFLinuxPrStatus() {
233 memset(this, 0, sizeof(ELFLinuxPrStatus));
234}
235
236size_t ELFLinuxPrStatus::GetSize(const lldb_private::ArchSpec &arch) {
237 constexpr size_t mips_linux_pr_status_size_o32 = 96;
238 constexpr size_t mips_linux_pr_status_size_n32 = 72;
239 constexpr size_t num_ptr_size_members = 10;
240 if (arch.IsMIPS()) {
241 std::string abi = arch.GetTargetABI();
242 assert(!abi.empty() && "ABI is not set")(static_cast <bool> (!abi.empty() && "ABI is not set"
) ? void (0) : __assert_fail ("!abi.empty() && \"ABI is not set\""
, "lldb/source/Plugins/Process/elf-core/ThreadElfCore.cpp", 242
, __extension__ __PRETTY_FUNCTION__))
;
243 if (!abi.compare("n64"))
244 return sizeof(ELFLinuxPrStatus);
245 else if (!abi.compare("o32"))
246 return mips_linux_pr_status_size_o32;
247 // N32 ABI
248 return mips_linux_pr_status_size_n32;
249 }
250 switch (arch.GetCore()) {
251 case lldb_private::ArchSpec::eCore_x86_32_i386:
252 case lldb_private::ArchSpec::eCore_x86_32_i486:
253 return 72;
254 default:
255 if (arch.GetAddressByteSize() == 8)
256 return sizeof(ELFLinuxPrStatus);
257 else
258 return sizeof(ELFLinuxPrStatus) - num_ptr_size_members * 4;
259 }
260}
261
262Status ELFLinuxPrStatus::Parse(const DataExtractor &data,
263 const ArchSpec &arch) {
264 Status error;
265 if (GetSize(arch) > data.GetByteSize()) {
266 error.SetErrorStringWithFormat(
267 "NT_PRSTATUS size should be %zu, but the remaining bytes are: %" PRIu64"l" "u",
268 GetSize(arch), data.GetByteSize());
269 return error;
270 }
271
272 // Read field by field to correctly account for endianess of both the core
273 // dump and the platform running lldb.
274 offset_t offset = 0;
275 si_signo = data.GetU32(&offset);
276 si_code = data.GetU32(&offset);
277 si_errno = data.GetU32(&offset);
278
279 pr_cursig = data.GetU16(&offset);
280 offset += 2; // pad
281
282 pr_sigpend = data.GetAddress(&offset);
283 pr_sighold = data.GetAddress(&offset);
284
285 pr_pid = data.GetU32(&offset);
286 pr_ppid = data.GetU32(&offset);
287 pr_pgrp = data.GetU32(&offset);
288 pr_sid = data.GetU32(&offset);
289
290 pr_utime.tv_sec = data.GetAddress(&offset);
291 pr_utime.tv_usec = data.GetAddress(&offset);
292
293 pr_stime.tv_sec = data.GetAddress(&offset);
294 pr_stime.tv_usec = data.GetAddress(&offset);
295
296 pr_cutime.tv_sec = data.GetAddress(&offset);
297 pr_cutime.tv_usec = data.GetAddress(&offset);
298
299 pr_cstime.tv_sec = data.GetAddress(&offset);
300 pr_cstime.tv_usec = data.GetAddress(&offset);
301
302 return error;
303}
304
305// Parse PRPSINFO from NOTE entry
306ELFLinuxPrPsInfo::ELFLinuxPrPsInfo() {
307 memset(this, 0, sizeof(ELFLinuxPrPsInfo));
308}
309
310size_t ELFLinuxPrPsInfo::GetSize(const lldb_private::ArchSpec &arch) {
311 constexpr size_t mips_linux_pr_psinfo_size_o32_n32 = 128;
312 if (arch.IsMIPS()) {
313 uint8_t address_byte_size = arch.GetAddressByteSize();
314 if (address_byte_size == 8)
315 return sizeof(ELFLinuxPrPsInfo);
316 return mips_linux_pr_psinfo_size_o32_n32;
317 }
318
319 switch (arch.GetCore()) {
320 case lldb_private::ArchSpec::eCore_s390x_generic:
321 case lldb_private::ArchSpec::eCore_x86_64_x86_64:
322 return sizeof(ELFLinuxPrPsInfo);
323 case lldb_private::ArchSpec::eCore_x86_32_i386:
324 case lldb_private::ArchSpec::eCore_x86_32_i486:
325 return 124;
326 default:
327 return 0;
328 }
329}
330
331Status ELFLinuxPrPsInfo::Parse(const DataExtractor &data,
332 const ArchSpec &arch) {
333 Status error;
334 ByteOrder byteorder = data.GetByteOrder();
335 if (GetSize(arch) > data.GetByteSize()) {
336 error.SetErrorStringWithFormat(
337 "NT_PRPSINFO size should be %zu, but the remaining bytes are: %" PRIu64"l" "u",
338 GetSize(arch), data.GetByteSize());
339 return error;
340 }
341 size_t size = 0;
342 offset_t offset = 0;
343
344 pr_state = data.GetU8(&offset);
345 pr_sname = data.GetU8(&offset);
346 pr_zomb = data.GetU8(&offset);
347 pr_nice = data.GetU8(&offset);
348 if (data.GetAddressByteSize() == 8) {
349 // Word align the next field on 64 bit.
350 offset += 4;
351 }
352
353 pr_flag = data.GetAddress(&offset);
354
355 if (arch.IsMIPS()) {
356 // The pr_uid and pr_gid is always 32 bit irrespective of platforms
357 pr_uid = data.GetU32(&offset);
358 pr_gid = data.GetU32(&offset);
359 } else {
360 // 16 bit on 32 bit platforms, 32 bit on 64 bit platforms
361 pr_uid = data.GetMaxU64(&offset, data.GetAddressByteSize() >> 1);
362 pr_gid = data.GetMaxU64(&offset, data.GetAddressByteSize() >> 1);
363 }
364
365 pr_pid = data.GetU32(&offset);
366 pr_ppid = data.GetU32(&offset);
367 pr_pgrp = data.GetU32(&offset);
368 pr_sid = data.GetU32(&offset);
369
370 size = 16;
371 data.ExtractBytes(offset, size, byteorder, pr_fname);
372 offset += size;
373
374 size = 80;
375 data.ExtractBytes(offset, size, byteorder, pr_psargs);
376 offset += size;
377
378 return error;
379}
380
381// Parse SIGINFO from NOTE entry
382ELFLinuxSigInfo::ELFLinuxSigInfo() { memset(this, 0, sizeof(ELFLinuxSigInfo)); }
383
384size_t ELFLinuxSigInfo::GetSize(const lldb_private::ArchSpec &arch) {
385 if (arch.IsMIPS())
386 return sizeof(ELFLinuxSigInfo);
387 switch (arch.GetCore()) {
388 case lldb_private::ArchSpec::eCore_x86_64_x86_64:
389 return sizeof(ELFLinuxSigInfo);
390 case lldb_private::ArchSpec::eCore_s390x_generic:
391 case lldb_private::ArchSpec::eCore_x86_32_i386:
392 case lldb_private::ArchSpec::eCore_x86_32_i486:
393 return 12;
394 default:
395 return 0;
396 }
397}
398
399Status ELFLinuxSigInfo::Parse(const DataExtractor &data, const ArchSpec &arch) {
400 Status error;
401 if (GetSize(arch) > data.GetByteSize()) {
402 error.SetErrorStringWithFormat(
403 "NT_SIGINFO size should be %zu, but the remaining bytes are: %" PRIu64"l" "u",
404 GetSize(arch), data.GetByteSize());
405 return error;
406 }
407
408 // Parsing from a 32 bit ELF core file, and populating/reusing the structure
409 // properly, because the struct is for the 64 bit version
410 offset_t offset = 0;
411 si_signo = data.GetU32(&offset);
412 si_code = data.GetU32(&offset);
413 si_errno = data.GetU32(&offset);
414
415 return error;
416}