Bug Summary

File:lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
Warning:line 379, column 11
Called C++ object pointer is null

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 ClangExpressionSourceCode.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/ExpressionParser/Clang -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/lldb/source/Plugins/ExpressionParser/Clang -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/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
1//===-- ClangExpressionSourceCode.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 "ClangExpressionSourceCode.h"
10
11#include "clang/Basic/CharInfo.h"
12#include "clang/Basic/FileManager.h"
13#include "clang/Basic/SourceManager.h"
14#include "clang/Lex/Lexer.h"
15#include "llvm/ADT/StringRef.h"
16
17#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
18#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
19#include "lldb/Symbol/Block.h"
20#include "lldb/Symbol/CompileUnit.h"
21#include "lldb/Symbol/DebugMacros.h"
22#include "lldb/Symbol/TypeSystem.h"
23#include "lldb/Symbol/VariableList.h"
24#include "lldb/Target/ExecutionContext.h"
25#include "lldb/Target/Language.h"
26#include "lldb/Target/Platform.h"
27#include "lldb/Target/StackFrame.h"
28#include "lldb/Target/Target.h"
29#include "lldb/Utility/StreamString.h"
30
31using namespace lldb_private;
32
33#define PREFIX_NAME"<lldb wrapper prefix>" "<lldb wrapper prefix>"
34#define SUFFIX_NAME"<lldb wrapper suffix>" "<lldb wrapper suffix>"
35
36const llvm::StringRef ClangExpressionSourceCode::g_prefix_file_name = PREFIX_NAME"<lldb wrapper prefix>";
37
38const char *ClangExpressionSourceCode::g_expression_prefix =
39"#line 1 \"" PREFIX_NAME"<lldb wrapper prefix>" R"("
40#ifndef offsetof
41#define offsetof(t, d) __builtin_offsetof(t, d)
42#endif
43#ifndef NULL
44#define NULL (__null)
45#endif
46#ifndef Nil
47#define Nil (__null)
48#endif
49#ifndef nil
50#define nil (__null)
51#endif
52#ifndef YES
53#define YES ((BOOL)1)
54#endif
55#ifndef NO
56#define NO ((BOOL)0)
57#endif
58typedef __INT8_TYPE__ int8_t;
59typedef __UINT8_TYPE__ uint8_t;
60typedef __INT16_TYPE__ int16_t;
61typedef __UINT16_TYPE__ uint16_t;
62typedef __INT32_TYPE__ int32_t;
63typedef __UINT32_TYPE__ uint32_t;
64typedef __INT64_TYPE__ int64_t;
65typedef __UINT64_TYPE__ uint64_t;
66typedef __INTPTR_TYPE__ intptr_t;
67typedef __UINTPTR_TYPE__ uintptr_t;
68typedef __SIZE_TYPE__ size_t;
69typedef __PTRDIFF_TYPE__ ptrdiff_t;
70typedef unsigned short unichar;
71extern "C"
72{
73 int printf(const char * __restrict, ...);
74}
75)";
76
77const char *ClangExpressionSourceCode::g_expression_suffix =
78 "\n;\n#line 1 \"" SUFFIX_NAME"<lldb wrapper suffix>" "\"\n";
79
80namespace {
81
82class AddMacroState {
83 enum State {
84 CURRENT_FILE_NOT_YET_PUSHED,
85 CURRENT_FILE_PUSHED,
86 CURRENT_FILE_POPPED
87 };
88
89public:
90 AddMacroState(const FileSpec &current_file, const uint32_t current_file_line)
91 : m_state(CURRENT_FILE_NOT_YET_PUSHED), m_current_file(current_file),
92 m_current_file_line(current_file_line) {}
93
94 void StartFile(const FileSpec &file) {
95 m_file_stack.push_back(file);
96 if (file == m_current_file)
97 m_state = CURRENT_FILE_PUSHED;
98 }
99
100 void EndFile() {
101 if (m_file_stack.size() == 0)
102 return;
103
104 FileSpec old_top = m_file_stack.back();
105 m_file_stack.pop_back();
106 if (old_top == m_current_file)
107 m_state = CURRENT_FILE_POPPED;
108 }
109
110 // An entry is valid if it occurs before the current line in the current
111 // file.
112 bool IsValidEntry(uint32_t line) {
113 switch (m_state) {
114 case CURRENT_FILE_NOT_YET_PUSHED:
115 return true;
116 case CURRENT_FILE_PUSHED:
117 // If we are in file included in the current file, the entry should be
118 // added.
119 if (m_file_stack.back() != m_current_file)
120 return true;
121
122 return line < m_current_file_line;
123 default:
124 return false;
125 }
126 }
127
128private:
129 std::vector<FileSpec> m_file_stack;
130 State m_state;
131 FileSpec m_current_file;
132 uint32_t m_current_file_line;
133};
134
135} // anonymous namespace
136
137static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
138 AddMacroState &state, StreamString &stream) {
139 if (dm == nullptr)
140 return;
141
142 for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
143 const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
144 uint32_t line;
145
146 switch (entry.GetType()) {
147 case DebugMacroEntry::DEFINE:
148 if (state.IsValidEntry(entry.GetLineNumber()))
149 stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
150 else
151 return;
152 break;
153 case DebugMacroEntry::UNDEF:
154 if (state.IsValidEntry(entry.GetLineNumber()))
155 stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
156 else
157 return;
158 break;
159 case DebugMacroEntry::START_FILE:
160 line = entry.GetLineNumber();
161 if (state.IsValidEntry(line))
162 state.StartFile(entry.GetFileSpec(comp_unit));
163 else
164 return;
165 break;
166 case DebugMacroEntry::END_FILE:
167 state.EndFile();
168 break;
169 case DebugMacroEntry::INDIRECT:
170 AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
171 break;
172 default:
173 // This is an unknown/invalid entry. Ignore.
174 break;
175 }
176 }
177}
178
179lldb_private::ClangExpressionSourceCode::ClangExpressionSourceCode(
180 llvm::StringRef filename, llvm::StringRef name, llvm::StringRef prefix,
181 llvm::StringRef body, Wrapping wrap, WrapKind wrap_kind)
182 : ExpressionSourceCode(name, prefix, body, wrap), m_wrap_kind(wrap_kind) {
183 // Use #line markers to pretend that we have a single-line source file
184 // containing only the user expression. This will hide our wrapper code
185 // from the user when we render diagnostics with Clang.
186 m_start_marker = "#line 1 \"" + filename.str() + "\"\n";
187 m_end_marker = g_expression_suffix;
188}
189
190namespace {
191/// Allows checking if a token is contained in a given expression.
192class TokenVerifier {
193 /// The tokens we found in the expression.
194 llvm::StringSet<> m_tokens;
195
196public:
197 TokenVerifier(std::string body);
198 /// Returns true iff the given expression body contained a token with the
199 /// given content.
200 bool hasToken(llvm::StringRef token) const {
201 return m_tokens.find(token) != m_tokens.end();
202 }
203};
204} // namespace
205
206TokenVerifier::TokenVerifier(std::string body) {
207 using namespace clang;
208
209 // We only care about tokens and not their original source locations. If we
210 // move the whole expression to only be in one line we can simplify the
211 // following code that extracts the token contents.
212 std::replace(body.begin(), body.end(), '\n', ' ');
213 std::replace(body.begin(), body.end(), '\r', ' ');
214
215 FileSystemOptions file_opts;
216 FileManager file_mgr(file_opts,
217 FileSystem::Instance().GetVirtualFileSystem());
218
219 // Let's build the actual source code Clang needs and setup some utility
220 // objects.
221 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_ids(new DiagnosticIDs());
222 llvm::IntrusiveRefCntPtr<DiagnosticOptions> diags_opts(
223 new DiagnosticOptions());
224 DiagnosticsEngine diags(diag_ids, diags_opts);
225 clang::SourceManager SM(diags, file_mgr);
226 auto buf = llvm::MemoryBuffer::getMemBuffer(body);
227
228 FileID FID = SM.createFileID(buf->getMemBufferRef());
229
230 // Let's just enable the latest ObjC and C++ which should get most tokens
231 // right.
232 LangOptions Opts;
233 Opts.ObjC = true;
234 Opts.DollarIdents = true;
235 Opts.CPlusPlus17 = true;
236 Opts.LineComment = true;
237
238 Lexer lex(FID, buf->getMemBufferRef(), SM, Opts);
239
240 Token token;
241 bool exit = false;
242 while (!exit) {
243 // Returns true if this is the last token we get from the lexer.
244 exit = lex.LexFromRawLexer(token);
245
246 // Extract the column number which we need to extract the token content.
247 // Our expression is just one line, so we don't need to handle any line
248 // numbers here.
249 bool invalid = false;
250 unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid);
251 if (invalid)
252 continue;
253 // Column numbers start at 1, but indexes in our string start at 0.
254 --start;
255
256 // Annotations don't have a length, so let's skip them.
257 if (token.isAnnotation())
258 continue;
259
260 // Extract the token string from our source code and store it.
261 std::string token_str = body.substr(start, token.getLength());
262 if (token_str.empty())
263 continue;
264 m_tokens.insert(token_str);
265 }
266}
267
268void ClangExpressionSourceCode::AddLocalVariableDecls(
269 const lldb::VariableListSP &var_list_sp, StreamString &stream,
270 const std::string &expr) const {
271 TokenVerifier tokens(expr);
272
273 for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
274 lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
275
276 ConstString var_name = var_sp->GetName();
277
278
279 // We can check for .block_descriptor w/o checking for langauge since this
280 // is not a valid identifier in either C or C++.
281 if (!var_name || var_name == ".block_descriptor")
282 continue;
283
284 if (!expr.empty() && !tokens.hasToken(var_name.GetStringRef()))
285 continue;
286
287 const bool is_objc = m_wrap_kind == WrapKind::ObjCInstanceMethod ||
288 m_wrap_kind == WrapKind::ObjCStaticMethod;
289 if ((var_name == "self" || var_name == "_cmd") && is_objc)
290 continue;
291
292 if (var_name == "this" && m_wrap_kind == WrapKind::CppMemberFunction)
293 continue;
294
295 stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
296 }
297}
298
299bool ClangExpressionSourceCode::GetText(
300 std::string &text, ExecutionContext &exe_ctx, bool add_locals,
301 bool force_add_all_locals, llvm::ArrayRef<std::string> modules) const {
302 const char *target_specific_defines = "typedef signed char BOOL;\n";
303 std::string module_macros;
304 llvm::raw_string_ostream module_macros_stream(module_macros);
305
306 Target *target = exe_ctx.GetTargetPtr();
1
'target' initialized here
307 if (target) {
2
Assuming 'target' is null
3
Taking false branch
308 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64 ||
309 target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_32) {
310 target_specific_defines = "typedef bool BOOL;\n";
311 }
312 if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
313 if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
314 if (platform_sp->GetPluginName() == "ios-simulator") {
315 target_specific_defines = "typedef bool BOOL;\n";
316 }
317 }
318 }
319
320 auto *persistent_vars = llvm::cast<ClangPersistentVariables>(
321 target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
322 std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
323 persistent_vars->GetClangModulesDeclVendor();
324 if (decl_vendor) {
325 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
326 persistent_vars->GetHandLoadedClangModules();
327 ClangModulesDeclVendor::ModuleVector modules_for_macros;
328
329 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
330 modules_for_macros.push_back(module);
331 }
332
333 if (target->GetEnableAutoImportClangModules()) {
334 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
335 if (Block *block = frame->GetFrameBlock()) {
336 SymbolContext sc;
337
338 block->CalculateSymbolContext(&sc);
339
340 if (sc.comp_unit) {
341 StreamString error_stream;
342
343 decl_vendor->AddModulesForCompileUnit(
344 *sc.comp_unit, modules_for_macros, error_stream);
345 }
346 }
347 }
348 }
349
350 decl_vendor->ForEachMacro(
351 modules_for_macros,
352 [&module_macros_stream](llvm::StringRef token,
353 llvm::StringRef expansion) -> bool {
354 // Check if the macro hasn't already been defined in the
355 // g_expression_prefix (which defines a few builtin macros).
356 module_macros_stream << "#ifndef " << token << "\n";
357 module_macros_stream << expansion << "\n";
358 module_macros_stream << "#endif\n";
359 return false;
360 });
361 }
362 }
363
364 StreamString debug_macros_stream;
365 StreamString lldb_local_var_decls;
366 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
4
Assuming 'frame' is non-null
5
Taking true branch
367 const SymbolContext &sc = frame->GetSymbolContext(
368 lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
369
370 if (sc.comp_unit && sc.line_entry.IsValid()) {
6
Assuming field 'comp_unit' is null
371 DebugMacros *dm = sc.comp_unit->GetDebugMacros();
372 if (dm) {
373 AddMacroState state(sc.line_entry.file, sc.line_entry.line);
374 AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
375 }
376 }
377
378 if (add_locals)
7
Assuming 'add_locals' is true
8
Taking true branch
379 if (target->GetInjectLocalVariables(&exe_ctx)) {
9
Called C++ object pointer is null
380 lldb::VariableListSP var_list_sp =
381 frame->GetInScopeVariableList(false, true);
382 AddLocalVariableDecls(var_list_sp, lldb_local_var_decls,
383 force_add_all_locals ? "" : m_body);
384 }
385 }
386
387 if (m_wrap) {
388 // Generate a list of @import statements that will import the specified
389 // module into our expression.
390 std::string module_imports;
391 for (const std::string &module : modules) {
392 module_imports.append("@import ");
393 module_imports.append(module);
394 module_imports.append(";\n");
395 }
396
397 StreamString wrap_stream;
398
399 wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", g_expression_prefix,
400 module_macros.c_str(), debug_macros_stream.GetData(),
401 target_specific_defines, m_prefix.c_str());
402
403 // First construct a tagged form of the user expression so we can find it
404 // later:
405 std::string tagged_body;
406 tagged_body.append(m_start_marker);
407 tagged_body.append(m_body);
408 tagged_body.append(m_end_marker);
409
410 switch (m_wrap_kind) {
411 case WrapKind::Function:
412 wrap_stream.Printf("%s"
413 "void \n"
414 "%s(void *$__lldb_arg) \n"
415 "{ \n"
416 " %s; \n"
417 "%s"
418 "} \n",
419 module_imports.c_str(), m_name.c_str(),
420 lldb_local_var_decls.GetData(), tagged_body.c_str());
421 break;
422 case WrapKind::CppMemberFunction:
423 wrap_stream.Printf("%s"
424 "void \n"
425 "$__lldb_class::%s(void *$__lldb_arg) \n"
426 "{ \n"
427 " %s; \n"
428 "%s"
429 "} \n",
430 module_imports.c_str(), m_name.c_str(),
431 lldb_local_var_decls.GetData(), tagged_body.c_str());
432 break;
433 case WrapKind::ObjCInstanceMethod:
434 wrap_stream.Printf(
435 "%s"
436 "@interface $__lldb_objc_class ($__lldb_category) \n"
437 "-(void)%s:(void *)$__lldb_arg; \n"
438 "@end \n"
439 "@implementation $__lldb_objc_class ($__lldb_category) \n"
440 "-(void)%s:(void *)$__lldb_arg \n"
441 "{ \n"
442 " %s; \n"
443 "%s"
444 "} \n"
445 "@end \n",
446 module_imports.c_str(), m_name.c_str(), m_name.c_str(),
447 lldb_local_var_decls.GetData(), tagged_body.c_str());
448 break;
449
450 case WrapKind::ObjCStaticMethod:
451 wrap_stream.Printf(
452 "%s"
453 "@interface $__lldb_objc_class ($__lldb_category) \n"
454 "+(void)%s:(void *)$__lldb_arg; \n"
455 "@end \n"
456 "@implementation $__lldb_objc_class ($__lldb_category) \n"
457 "+(void)%s:(void *)$__lldb_arg \n"
458 "{ \n"
459 " %s; \n"
460 "%s"
461 "} \n"
462 "@end \n",
463 module_imports.c_str(), m_name.c_str(), m_name.c_str(),
464 lldb_local_var_decls.GetData(), tagged_body.c_str());
465 break;
466 }
467
468 text = std::string(wrap_stream.GetString());
469 } else {
470 text.append(m_body);
471 }
472
473 return true;
474}
475
476bool ClangExpressionSourceCode::GetOriginalBodyBounds(
477 std::string transformed_text, size_t &start_loc, size_t &end_loc) {
478 start_loc = transformed_text.find(m_start_marker);
479 if (start_loc == std::string::npos)
480 return false;
481 start_loc += m_start_marker.size();
482 end_loc = transformed_text.find(m_end_marker);
483 return end_loc != std::string::npos;
484}