Bug Summary

File:tools/lldb/source/Plugins/Process/elf-core/ThreadElfCore.cpp
Warning:line 229, column 16
Potential leak of memory pointed to by 'reg_interface'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ThreadElfCore.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -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 -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D HAVE_ROUND -D LLDB_CONFIGURATION_RELEASE -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn337957/build-llvm/tools/lldb/source/Plugins/Process/elf-core -I /build/llvm-toolchain-snapshot-7~svn337957/tools/lldb/source/Plugins/Process/elf-core -I /build/llvm-toolchain-snapshot-7~svn337957/build-llvm/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn337957/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn337957/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn337957/include -I /usr/include/python2.7 -I /build/llvm-toolchain-snapshot-7~svn337957/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn337957/build-llvm/tools/lldb/../clang/include -I /build/llvm-toolchain-snapshot-7~svn337957/tools/lldb/source/. -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/8/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-comment -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-deprecated-register -Wno-vla-extension -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn337957/build-llvm/tools/lldb/source/Plugins/Process/elf-core -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-07-26-043338-586-1 -x c++ /build/llvm-toolchain-snapshot-7~svn337957/tools/lldb/source/Plugins/Process/elf-core/ThreadElfCore.cpp -faddrsig
1//===-- ThreadElfCore.cpp --------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/RegisterContext.h"
11#include "lldb/Target/StopInfo.h"
12#include "lldb/Target/Target.h"
13#include "lldb/Target/Unwind.h"
14#include "lldb/Utility/DataExtractor.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_mips.h"
23#include "Plugins/Process/Utility/RegisterContextLinux_mips64.h"
24#include "Plugins/Process/Utility/RegisterContextLinux_s390x.h"
25#include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
26#include "Plugins/Process/Utility/RegisterContextNetBSD_x86_64.h"
27#include "Plugins/Process/Utility/RegisterContextOpenBSD_i386.h"
28#include "Plugins/Process/Utility/RegisterContextOpenBSD_x86_64.h"
29#include "Plugins/Process/Utility/RegisterInfoPOSIX_arm.h"
30#include "Plugins/Process/Utility/RegisterInfoPOSIX_arm64.h"
31#include "Plugins/Process/Utility/RegisterInfoPOSIX_ppc64le.h"
32#include "ProcessElfCore.h"
33#include "RegisterContextPOSIXCore_arm.h"
34#include "RegisterContextPOSIXCore_arm64.h"
35#include "RegisterContextPOSIXCore_mips64.h"
36#include "RegisterContextPOSIXCore_powerpc.h"
37#include "RegisterContextPOSIXCore_ppc64le.h"
38#include "RegisterContextPOSIXCore_s390x.h"
39#include "RegisterContextPOSIXCore_x86_64.h"
40#include "ThreadElfCore.h"
41
42using namespace lldb;
43using namespace lldb_private;
44
45//----------------------------------------------------------------------
46// Construct a Thread object with given data
47//----------------------------------------------------------------------
48ThreadElfCore::ThreadElfCore(Process &process, const ThreadData &td)
49 : Thread(process, td.tid), m_thread_name(td.name), m_thread_reg_ctx_sp(),
50 m_signo(td.signo), m_gpregset_data(td.gpregset), m_notes(td.notes) {}
51
52ThreadElfCore::~ThreadElfCore() { DestroyThread(); }
53
54void ThreadElfCore::RefreshStateAfterStop() {
55 GetRegisterContext()->InvalidateIfNeeded(false);
1
Calling 'ThreadElfCore::GetRegisterContext'
56}
57
58RegisterContextSP ThreadElfCore::GetRegisterContext() {
59 if (!m_reg_context_sp) {
2
Taking true branch
60 m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
3
Calling 'ThreadElfCore::CreateRegisterContextForFrame'
61 }
62 return m_reg_context_sp;
63}
64
65RegisterContextSP
66ThreadElfCore::CreateRegisterContextForFrame(StackFrame *frame) {
67 RegisterContextSP reg_ctx_sp;
68 uint32_t concrete_frame_idx = 0;
69 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD(1u << 2)));
70
71 if (frame)
4
Taking false branch
72 concrete_frame_idx = frame->GetConcreteFrameIndex();
73
74 if (concrete_frame_idx == 0) {
5
Taking true branch
75 if (m_thread_reg_ctx_sp)
6
Taking false branch
76 return m_thread_reg_ctx_sp;
77
78 ProcessElfCore *process = static_cast<ProcessElfCore *>(GetProcess().get());
79 ArchSpec arch = process->GetArchitecture();
80 RegisterInfoInterface *reg_interface = nullptr;
81
82 switch (arch.GetTriple().getOS()) {
7
Control jumps to 'case FreeBSD:' at line 83
83 case llvm::Triple::FreeBSD: {
84 switch (arch.GetMachine()) {
8
Control jumps to 'case x86_64:' at line 103
85 case llvm::Triple::aarch64:
86 reg_interface = new RegisterInfoPOSIX_arm64(arch);
87 break;
88 case llvm::Triple::arm:
89 reg_interface = new RegisterInfoPOSIX_arm(arch);
90 break;
91 case llvm::Triple::ppc:
92 reg_interface = new RegisterContextFreeBSD_powerpc32(arch);
93 break;
94 case llvm::Triple::ppc64:
95 reg_interface = new RegisterContextFreeBSD_powerpc64(arch);
96 break;
97 case llvm::Triple::mips64:
98 reg_interface = new RegisterContextFreeBSD_mips64(arch);
99 break;
100 case llvm::Triple::x86:
101 reg_interface = new RegisterContextFreeBSD_i386(arch);
102 break;
103 case llvm::Triple::x86_64:
104 reg_interface = new RegisterContextFreeBSD_x86_64(arch);
9
Memory is allocated
105 break;
10
Execution continues on line 109
106 default:
107 break;
108 }
109 break;
11
Execution continues on line 181
110 }
111
112 case llvm::Triple::NetBSD: {
113 switch (arch.GetMachine()) {
114 case llvm::Triple::x86_64:
115 reg_interface = new RegisterContextNetBSD_x86_64(arch);
116 break;
117 default:
118 break;
119 }
120 break;
121 }
122
123 case llvm::Triple::Linux: {
124 switch (arch.GetMachine()) {
125 case llvm::Triple::arm:
126 reg_interface = new RegisterInfoPOSIX_arm(arch);
127 break;
128 case llvm::Triple::aarch64:
129 reg_interface = new RegisterInfoPOSIX_arm64(arch);
130 break;
131 case llvm::Triple::mipsel:
132 case llvm::Triple::mips:
133 reg_interface = new RegisterContextLinux_mips(arch);
134 break;
135 case llvm::Triple::mips64el:
136 case llvm::Triple::mips64:
137 reg_interface = new RegisterContextLinux_mips64(arch);
138 break;
139 case llvm::Triple::ppc64le:
140 reg_interface = new RegisterInfoPOSIX_ppc64le(arch);
141 break;
142 case llvm::Triple::systemz:
143 reg_interface = new RegisterContextLinux_s390x(arch);
144 break;
145 case llvm::Triple::x86:
146 reg_interface = new RegisterContextLinux_i386(arch);
147 break;
148 case llvm::Triple::x86_64:
149 reg_interface = new RegisterContextLinux_x86_64(arch);
150 break;
151 default:
152 break;
153 }
154 break;
155 }
156
157 case llvm::Triple::OpenBSD: {
158 switch (arch.GetMachine()) {
159 case llvm::Triple::aarch64:
160 reg_interface = new RegisterInfoPOSIX_arm64(arch);
161 break;
162 case llvm::Triple::arm:
163 reg_interface = new RegisterInfoPOSIX_arm(arch);
164 break;
165 case llvm::Triple::x86:
166 reg_interface = new RegisterContextOpenBSD_i386(arch);
167 break;
168 case llvm::Triple::x86_64:
169 reg_interface = new RegisterContextOpenBSD_x86_64(arch);
170 break;
171 default:
172 break;
173 }
174 break;
175 }
176
177 default:
178 break;
179 }
180
181 if (!reg_interface) {
12
Taking false branch
182 if (log)
183 log->Printf("elf-core::%s:: Architecture(%d) or OS(%d) not supported",
184 __FUNCTION__, arch.GetMachine(), arch.GetTriple().getOS());
185 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\""
, "/build/llvm-toolchain-snapshot-7~svn337957/tools/lldb/source/Plugins/Process/elf-core/ThreadElfCore.cpp"
, 185, __extension__ __PRETTY_FUNCTION__))
;
186 }
187
188 switch (arch.GetMachine()) {
13
Control jumps to the 'default' case at line 225
189 case llvm::Triple::aarch64:
190 m_thread_reg_ctx_sp.reset(new RegisterContextCorePOSIX_arm64(
191 *this, reg_interface, m_gpregset_data, m_notes));
192 break;
193 case llvm::Triple::arm:
194 m_thread_reg_ctx_sp.reset(new RegisterContextCorePOSIX_arm(
195 *this, reg_interface, m_gpregset_data, m_notes));
196 break;
197 case llvm::Triple::mipsel:
198 case llvm::Triple::mips:
199 m_thread_reg_ctx_sp.reset(new RegisterContextCorePOSIX_mips64(
200 *this, reg_interface, m_gpregset_data, m_notes));
201 break;
202 case llvm::Triple::mips64:
203 case llvm::Triple::mips64el:
204 m_thread_reg_ctx_sp.reset(new RegisterContextCorePOSIX_mips64(
205 *this, reg_interface, m_gpregset_data, m_notes));
206 break;
207 case llvm::Triple::ppc:
208 case llvm::Triple::ppc64:
209 m_thread_reg_ctx_sp.reset(new RegisterContextCorePOSIX_powerpc(
210 *this, reg_interface, m_gpregset_data, m_notes));
211 break;
212 case llvm::Triple::ppc64le:
213 m_thread_reg_ctx_sp.reset(new RegisterContextCorePOSIX_ppc64le(
214 *this, reg_interface, m_gpregset_data, m_notes));
215 break;
216 case llvm::Triple::systemz:
217 m_thread_reg_ctx_sp.reset(new RegisterContextCorePOSIX_s390x(
218 *this, reg_interface, m_gpregset_data, m_notes));
219 break;
220 case llvm::Triple::x86:
221 case llvm::Triple::x86_64:
222 m_thread_reg_ctx_sp.reset(new RegisterContextCorePOSIX_x86_64(
223 *this, reg_interface, m_gpregset_data, m_notes));
224 break;
225 default:
226 break;
14
Execution continues on line 229
227 }
228
229 reg_ctx_sp = m_thread_reg_ctx_sp;
15
Potential leak of memory pointed to by 'reg_interface'
230 } else {
231 Unwind *unwinder = GetUnwinder();
232 if (unwinder != nullptr)
233 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
234 }
235 return reg_ctx_sp;
236}
237
238bool ThreadElfCore::CalculateStopInfo() {
239 ProcessSP process_sp(GetProcess());
240 if (process_sp) {
241 SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, m_signo));
242 return true;
243 }
244 return false;
245}
246
247//----------------------------------------------------------------
248// Parse PRSTATUS from NOTE entry
249//----------------------------------------------------------------
250ELFLinuxPrStatus::ELFLinuxPrStatus() {
251 memset(this, 0, sizeof(ELFLinuxPrStatus));
252}
253
254size_t ELFLinuxPrStatus::GetSize(const lldb_private::ArchSpec &arch) {
255 constexpr size_t mips_linux_pr_status_size_o32 = 96;
256 constexpr size_t mips_linux_pr_status_size_n32 = 72;
257 if (arch.IsMIPS()) {
258 std::string abi = arch.GetTargetABI();
259 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\""
, "/build/llvm-toolchain-snapshot-7~svn337957/tools/lldb/source/Plugins/Process/elf-core/ThreadElfCore.cpp"
, 259, __extension__ __PRETTY_FUNCTION__))
;
260 if (!abi.compare("n64"))
261 return sizeof(ELFLinuxPrStatus);
262 else if (!abi.compare("o32"))
263 return mips_linux_pr_status_size_o32;
264 // N32 ABI
265 return mips_linux_pr_status_size_n32;
266 }
267 switch (arch.GetCore()) {
268 case lldb_private::ArchSpec::eCore_s390x_generic:
269 case lldb_private::ArchSpec::eCore_x86_64_x86_64:
270 case lldb_private::ArchSpec::eCore_ppc64le_generic:
271 return sizeof(ELFLinuxPrStatus);
272 case lldb_private::ArchSpec::eCore_x86_32_i386:
273 case lldb_private::ArchSpec::eCore_x86_32_i486:
274 return 72;
275 default:
276 return 0;
277 }
278}
279
280Status ELFLinuxPrStatus::Parse(const DataExtractor &data,
281 const ArchSpec &arch) {
282 Status error;
283 if (GetSize(arch) > data.GetByteSize()) {
284 error.SetErrorStringWithFormat(
285 "NT_PRSTATUS size should be %zu, but the remaining bytes are: %" PRIu64"l" "u",
286 GetSize(arch), data.GetByteSize());
287 return error;
288 }
289
290 // Read field by field to correctly account for endianess of both the core
291 // dump and the platform running lldb.
292 offset_t offset = 0;
293 si_signo = data.GetU32(&offset);
294 si_code = data.GetU32(&offset);
295 si_errno = data.GetU32(&offset);
296
297 pr_cursig = data.GetU16(&offset);
298 offset += 2; // pad
299
300 pr_sigpend = data.GetPointer(&offset);
301 pr_sighold = data.GetPointer(&offset);
302
303 pr_pid = data.GetU32(&offset);
304 pr_ppid = data.GetU32(&offset);
305 pr_pgrp = data.GetU32(&offset);
306 pr_sid = data.GetU32(&offset);
307
308 pr_utime.tv_sec = data.GetPointer(&offset);
309 pr_utime.tv_usec = data.GetPointer(&offset);
310
311 pr_stime.tv_sec = data.GetPointer(&offset);
312 pr_stime.tv_usec = data.GetPointer(&offset);
313
314 pr_cutime.tv_sec = data.GetPointer(&offset);
315 pr_cutime.tv_usec = data.GetPointer(&offset);
316
317 pr_cstime.tv_sec = data.GetPointer(&offset);
318 pr_cstime.tv_usec = data.GetPointer(&offset);
319
320 return error;
321}
322
323//----------------------------------------------------------------
324// Parse PRPSINFO from NOTE entry
325//----------------------------------------------------------------
326ELFLinuxPrPsInfo::ELFLinuxPrPsInfo() {
327 memset(this, 0, sizeof(ELFLinuxPrPsInfo));
328}
329
330size_t ELFLinuxPrPsInfo::GetSize(const lldb_private::ArchSpec &arch) {
331 constexpr size_t mips_linux_pr_psinfo_size_o32_n32 = 128;
332 if (arch.IsMIPS()) {
333 uint8_t address_byte_size = arch.GetAddressByteSize();
334 if (address_byte_size == 8)
335 return sizeof(ELFLinuxPrPsInfo);
336 return mips_linux_pr_psinfo_size_o32_n32;
337 }
338
339 switch (arch.GetCore()) {
340 case lldb_private::ArchSpec::eCore_s390x_generic:
341 case lldb_private::ArchSpec::eCore_x86_64_x86_64:
342 return sizeof(ELFLinuxPrPsInfo);
343 case lldb_private::ArchSpec::eCore_x86_32_i386:
344 case lldb_private::ArchSpec::eCore_x86_32_i486:
345 return 124;
346 default:
347 return 0;
348 }
349}
350
351Status ELFLinuxPrPsInfo::Parse(const DataExtractor &data,
352 const ArchSpec &arch) {
353 Status error;
354 ByteOrder byteorder = data.GetByteOrder();
355 if (GetSize(arch) > data.GetByteSize()) {
356 error.SetErrorStringWithFormat(
357 "NT_PRPSINFO size should be %zu, but the remaining bytes are: %" PRIu64"l" "u",
358 GetSize(arch), data.GetByteSize());
359 return error;
360 }
361 size_t size = 0;
362 offset_t offset = 0;
363
364 pr_state = data.GetU8(&offset);
365 pr_sname = data.GetU8(&offset);
366 pr_zomb = data.GetU8(&offset);
367 pr_nice = data.GetU8(&offset);
368 if (data.GetAddressByteSize() == 8) {
369 // Word align the next field on 64 bit.
370 offset += 4;
371 }
372
373 pr_flag = data.GetPointer(&offset);
374
375 if (arch.IsMIPS()) {
376 // The pr_uid and pr_gid is always 32 bit irrespective of platforms
377 pr_uid = data.GetU32(&offset);
378 pr_gid = data.GetU32(&offset);
379 } else {
380 // 16 bit on 32 bit platforms, 32 bit on 64 bit platforms
381 pr_uid = data.GetMaxU64(&offset, data.GetAddressByteSize() >> 1);
382 pr_gid = data.GetMaxU64(&offset, data.GetAddressByteSize() >> 1);
383 }
384
385 pr_pid = data.GetU32(&offset);
386 pr_ppid = data.GetU32(&offset);
387 pr_pgrp = data.GetU32(&offset);
388 pr_sid = data.GetU32(&offset);
389
390 size = 16;
391 data.ExtractBytes(offset, size, byteorder, pr_fname);
392 offset += size;
393
394 size = 80;
395 data.ExtractBytes(offset, size, byteorder, pr_psargs);
396 offset += size;
397
398 return error;
399}
400
401//----------------------------------------------------------------
402// Parse SIGINFO from NOTE entry
403//----------------------------------------------------------------
404ELFLinuxSigInfo::ELFLinuxSigInfo() { memset(this, 0, sizeof(ELFLinuxSigInfo)); }
405
406size_t ELFLinuxSigInfo::GetSize(const lldb_private::ArchSpec &arch) {
407 if (arch.IsMIPS())
408 return sizeof(ELFLinuxSigInfo);
409 switch (arch.GetCore()) {
410 case lldb_private::ArchSpec::eCore_x86_64_x86_64:
411 return sizeof(ELFLinuxSigInfo);
412 case lldb_private::ArchSpec::eCore_s390x_generic:
413 case lldb_private::ArchSpec::eCore_x86_32_i386:
414 case lldb_private::ArchSpec::eCore_x86_32_i486:
415 return 12;
416 default:
417 return 0;
418 }
419}
420
421Status ELFLinuxSigInfo::Parse(const DataExtractor &data, const ArchSpec &arch) {
422 Status error;
423 if (GetSize(arch) > data.GetByteSize()) {
424 error.SetErrorStringWithFormat(
425 "NT_SIGINFO size should be %zu, but the remaining bytes are: %" PRIu64"l" "u",
426 GetSize(arch), data.GetByteSize());
427 return error;
428 }
429
430 // Parsing from a 32 bit ELF core file, and populating/reusing the structure
431 // properly, because the struct is for the 64 bit version
432 offset_t offset = 0;
433 si_signo = data.GetU32(&offset);
434 si_code = data.GetU32(&offset);
435 si_errno = data.GetU32(&offset);
436
437 return error;
438}