clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ValueObjectVariable.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 -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/liblldbCore/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/liblldbCore/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/liblldbCore/../include -I /usr/src/gnu/usr.bin/clang/liblldbCore/obj -I /usr/src/gnu/usr.bin/clang/liblldbCore/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -I /usr/src/gnu/usr.bin/clang/liblldbCore/../../../llvm/lldb/include -I /usr/src/gnu/usr.bin/clang/liblldbCore/../../../llvm/lldb/source -I /usr/src/gnu/usr.bin/clang/liblldbCore/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/liblldbCore/obj/../include/lldb/Core -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/liblldbCore/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/liblldbCore/../../../llvm/lldb/source/Core/ValueObjectVariable.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include "lldb/Core/ValueObjectVariable.h" |
10 | |
11 | #include "lldb/Core/Address.h" |
12 | #include "lldb/Core/AddressRange.h" |
13 | #include "lldb/Core/Declaration.h" |
14 | #include "lldb/Core/Module.h" |
15 | #include "lldb/Core/Value.h" |
16 | #include "lldb/Expression/DWARFExpression.h" |
17 | #include "lldb/Symbol/Function.h" |
18 | #include "lldb/Symbol/ObjectFile.h" |
19 | #include "lldb/Symbol/SymbolContext.h" |
20 | #include "lldb/Symbol/SymbolContextScope.h" |
21 | #include "lldb/Symbol/Type.h" |
22 | #include "lldb/Symbol/Variable.h" |
23 | #include "lldb/Target/ExecutionContext.h" |
24 | #include "lldb/Target/Process.h" |
25 | #include "lldb/Target/RegisterContext.h" |
26 | #include "lldb/Target/Target.h" |
27 | #include "lldb/Utility/DataExtractor.h" |
28 | #include "lldb/Utility/RegisterValue.h" |
29 | #include "lldb/Utility/Scalar.h" |
30 | #include "lldb/Utility/Status.h" |
31 | #include "lldb/lldb-private-enumerations.h" |
32 | #include "lldb/lldb-types.h" |
33 | |
34 | #include "llvm/ADT/StringRef.h" |
35 | |
36 | #include <cassert> |
37 | #include <memory> |
38 | |
39 | namespace lldb_private { |
40 | class ExecutionContextScope; |
41 | } |
42 | namespace lldb_private { |
43 | class StackFrame; |
44 | } |
45 | namespace lldb_private { |
46 | struct RegisterInfo; |
47 | } |
48 | using namespace lldb_private; |
49 | |
50 | lldb::ValueObjectSP |
51 | ValueObjectVariable::Create(ExecutionContextScope *exe_scope, |
52 | const lldb::VariableSP &var_sp) { |
53 | auto manager_sp = ValueObjectManager::Create(); |
54 | return (new ValueObjectVariable(exe_scope, *manager_sp, var_sp))->GetSP(); |
| |
| |
55 | } |
56 | |
57 | ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope, |
58 | ValueObjectManager &manager, |
59 | const lldb::VariableSP &var_sp) |
60 | : ValueObject(exe_scope, manager), m_variable_sp(var_sp) { |
61 | |
62 | assert(m_variable_sp.get() != nullptr); |
63 | m_name = var_sp->GetName(); |
64 | } |
65 | |
66 | ValueObjectVariable::~ValueObjectVariable() = default; |
67 | |
68 | CompilerType ValueObjectVariable::GetCompilerTypeImpl() { |
69 | Type *var_type = m_variable_sp->GetType(); |
70 | if (var_type) |
71 | return var_type->GetForwardCompilerType(); |
72 | return CompilerType(); |
73 | } |
74 | |
75 | ConstString ValueObjectVariable::GetTypeName() { |
76 | Type *var_type = m_variable_sp->GetType(); |
77 | if (var_type) |
78 | return var_type->GetName(); |
79 | return ConstString(); |
80 | } |
81 | |
82 | ConstString ValueObjectVariable::GetDisplayTypeName() { |
83 | Type *var_type = m_variable_sp->GetType(); |
84 | if (var_type) |
85 | return var_type->GetForwardCompilerType().GetDisplayTypeName(); |
86 | return ConstString(); |
87 | } |
88 | |
89 | ConstString ValueObjectVariable::GetQualifiedTypeName() { |
90 | Type *var_type = m_variable_sp->GetType(); |
91 | if (var_type) |
92 | return var_type->GetQualifiedName(); |
93 | return ConstString(); |
94 | } |
95 | |
96 | size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) { |
97 | CompilerType type(GetCompilerType()); |
98 | |
99 | if (!type.IsValid()) |
100 | return 0; |
101 | |
102 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
103 | const bool omit_empty_base_classes = true; |
104 | auto child_count = type.GetNumChildren(omit_empty_base_classes, &exe_ctx); |
105 | return child_count <= max ? child_count : max; |
106 | } |
107 | |
108 | llvm::Optional<uint64_t> ValueObjectVariable::GetByteSize() { |
109 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
110 | |
111 | CompilerType type(GetCompilerType()); |
112 | |
113 | if (!type.IsValid()) |
114 | return {}; |
115 | |
116 | return type.GetByteSize(exe_ctx.GetBestExecutionContextScope()); |
117 | } |
118 | |
119 | lldb::ValueType ValueObjectVariable::GetValueType() const { |
120 | if (m_variable_sp) |
121 | return m_variable_sp->GetScope(); |
122 | return lldb::eValueTypeInvalid; |
123 | } |
124 | |
125 | bool ValueObjectVariable::UpdateValue() { |
126 | SetValueIsValid(false); |
127 | m_error.Clear(); |
128 | |
129 | Variable *variable = m_variable_sp.get(); |
130 | DWARFExpression &expr = variable->LocationExpression(); |
131 | |
132 | if (variable->GetLocationIsConstantValueData()) { |
133 | |
134 | |
135 | if (expr.GetExpressionData(m_data)) { |
136 | if (m_data.GetDataStart() && m_data.GetByteSize()) |
137 | m_value.SetBytes(m_data.GetDataStart(), m_data.GetByteSize()); |
138 | m_value.SetContext(Value::ContextType::Variable, variable); |
139 | } |
140 | else |
141 | m_error.SetErrorString("empty constant data"); |
142 | |
143 | m_resolved_value.SetContext(Value::ContextType::Invalid, nullptr); |
144 | } else { |
145 | lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS; |
146 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
147 | |
148 | Target *target = exe_ctx.GetTargetPtr(); |
149 | if (target) { |
150 | m_data.SetByteOrder(target->GetArchitecture().GetByteOrder()); |
151 | m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); |
152 | } |
153 | |
154 | if (expr.IsLocationList()) { |
155 | SymbolContext sc; |
156 | variable->CalculateSymbolContext(&sc); |
157 | if (sc.function) |
158 | loclist_base_load_addr = |
159 | sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress( |
160 | target); |
161 | } |
162 | Value old_value(m_value); |
163 | if (expr.Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, nullptr, |
164 | nullptr, m_value, &m_error)) { |
165 | m_resolved_value = m_value; |
166 | m_value.SetContext(Value::ContextType::Variable, variable); |
167 | |
168 | CompilerType compiler_type = GetCompilerType(); |
169 | if (compiler_type.IsValid()) |
170 | m_value.SetCompilerType(compiler_type); |
171 | |
172 | Value::ValueType value_type = m_value.GetValueType(); |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | if (value_type == Value::ValueType::HostAddress && |
187 | compiler_type.IsValid()) { |
188 | if (size_t value_buf_size = m_value.GetBuffer().GetByteSize()) { |
189 | size_t value_size = m_value.GetValueByteSize(&m_error, &exe_ctx); |
190 | if (m_error.Success() && value_buf_size < value_size) |
191 | m_value.ResizeData(value_size); |
192 | } |
193 | } |
194 | |
195 | Process *process = exe_ctx.GetProcessPtr(); |
196 | const bool process_is_alive = process && process->IsAlive(); |
197 | |
198 | switch (value_type) { |
199 | case Value::ValueType::Invalid: |
200 | m_error.SetErrorString("invalid value"); |
201 | break; |
202 | case Value::ValueType::Scalar: |
203 | |
204 | |
205 | m_error = |
206 | m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get()); |
207 | break; |
208 | |
209 | case Value::ValueType::FileAddress: |
210 | case Value::ValueType::LoadAddress: |
211 | case Value::ValueType::HostAddress: |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | if (value_type == Value::ValueType::FileAddress && process_is_alive) |
221 | m_value.ConvertToLoadAddress(GetModule().get(), target); |
222 | |
223 | if (!CanProvideValue()) { |
224 | |
225 | |
226 | |
227 | SetValueDidChange(value_type != old_value.GetValueType() || |
228 | m_value.GetScalar() != old_value.GetScalar()); |
229 | } else { |
230 | |
231 | |
232 | Value value(m_value); |
233 | value.SetContext(Value::ContextType::Variable, variable); |
234 | m_error = |
235 | value.GetValueAsData(&exe_ctx, m_data, GetModule().get()); |
236 | |
237 | SetValueDidChange(value_type != old_value.GetValueType() || |
238 | m_value.GetScalar() != old_value.GetScalar()); |
239 | } |
240 | break; |
241 | } |
242 | |
243 | SetValueIsValid(m_error.Success()); |
244 | } else { |
245 | |
246 | m_resolved_value.SetContext(Value::ContextType::Invalid, nullptr); |
247 | } |
248 | } |
249 | |
250 | return m_error.Success(); |
251 | } |
252 | |
253 | void ValueObjectVariable::DoUpdateChildrenAddressType(ValueObject &valobj) { |
254 | Value::ValueType value_type = valobj.GetValue().GetValueType(); |
255 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
256 | Process *process = exe_ctx.GetProcessPtr(); |
257 | const bool process_is_alive = process && process->IsAlive(); |
258 | const uint32_t type_info = valobj.GetCompilerType().GetTypeInfo(); |
259 | const bool is_pointer_or_ref = |
260 | (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0; |
261 | |
262 | switch (value_type) { |
263 | case Value::ValueType::Invalid: |
264 | break; |
265 | case Value::ValueType::FileAddress: |
266 | |
267 | |
268 | |
269 | |
270 | |
271 | |
272 | |
273 | |
274 | |
275 | |
276 | |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | |
283 | if (process_is_alive && is_pointer_or_ref) |
284 | valobj.SetAddressTypeOfChildren(eAddressTypeLoad); |
285 | else |
286 | valobj.SetAddressTypeOfChildren(eAddressTypeFile); |
287 | break; |
288 | case Value::ValueType::HostAddress: |
289 | |
290 | |
291 | |
292 | |
293 | |
294 | if (is_pointer_or_ref) |
295 | valobj.SetAddressTypeOfChildren(eAddressTypeLoad); |
296 | else |
297 | valobj.SetAddressTypeOfChildren(eAddressTypeHost); |
298 | break; |
299 | case Value::ValueType::LoadAddress: |
300 | case Value::ValueType::Scalar: |
301 | valobj.SetAddressTypeOfChildren(eAddressTypeLoad); |
302 | break; |
303 | } |
304 | } |
305 | |
306 | |
307 | |
308 | bool ValueObjectVariable::IsInScope() { |
309 | const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef(); |
310 | if (exe_ctx_ref.HasFrameRef()) { |
311 | ExecutionContext exe_ctx(exe_ctx_ref); |
312 | StackFrame *frame = exe_ctx.GetFramePtr(); |
313 | if (frame) { |
314 | return m_variable_sp->IsInScope(frame); |
315 | } else { |
316 | |
317 | |
318 | return false; |
319 | } |
320 | } |
321 | |
322 | |
323 | return true; |
324 | } |
325 | |
326 | lldb::ModuleSP ValueObjectVariable::GetModule() { |
327 | if (m_variable_sp) { |
328 | SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope(); |
329 | if (sc_scope) { |
330 | return sc_scope->CalculateSymbolContextModule(); |
331 | } |
332 | } |
333 | return lldb::ModuleSP(); |
334 | } |
335 | |
336 | SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() { |
337 | if (m_variable_sp) |
338 | return m_variable_sp->GetSymbolContextScope(); |
339 | return nullptr; |
340 | } |
341 | |
342 | bool ValueObjectVariable::GetDeclaration(Declaration &decl) { |
343 | if (m_variable_sp) { |
344 | decl = m_variable_sp->GetDeclaration(); |
345 | return true; |
346 | } |
347 | return false; |
348 | } |
349 | |
350 | const char *ValueObjectVariable::GetLocationAsCString() { |
351 | if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) |
352 | return GetLocationAsCStringImpl(m_resolved_value, m_data); |
353 | else |
354 | return ValueObject::GetLocationAsCString(); |
355 | } |
356 | |
357 | bool ValueObjectVariable::SetValueFromCString(const char *value_str, |
358 | Status &error) { |
359 | if (!UpdateValueIfNeeded()) { |
360 | error.SetErrorString("unable to update value before writing"); |
361 | return false; |
362 | } |
363 | |
364 | if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) { |
365 | RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); |
366 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
367 | RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); |
368 | RegisterValue reg_value; |
369 | if (!reg_info || !reg_ctx) { |
370 | error.SetErrorString("unable to retrieve register info"); |
371 | return false; |
372 | } |
373 | error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str)); |
374 | if (error.Fail()) |
375 | return false; |
376 | if (reg_ctx->WriteRegister(reg_info, reg_value)) { |
377 | SetNeedsUpdate(); |
378 | return true; |
379 | } else { |
380 | error.SetErrorString("unable to write back to register"); |
381 | return false; |
382 | } |
383 | } else |
384 | return ValueObject::SetValueFromCString(value_str, error); |
385 | } |
386 | |
387 | bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) { |
388 | if (!UpdateValueIfNeeded()) { |
389 | error.SetErrorString("unable to update value before writing"); |
390 | return false; |
391 | } |
392 | |
393 | if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) { |
394 | RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); |
395 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
396 | RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); |
397 | RegisterValue reg_value; |
398 | if (!reg_info || !reg_ctx) { |
399 | error.SetErrorString("unable to retrieve register info"); |
400 | return false; |
401 | } |
402 | error = reg_value.SetValueFromData(reg_info, data, 0, true); |
403 | if (error.Fail()) |
404 | return false; |
405 | if (reg_ctx->WriteRegister(reg_info, reg_value)) { |
406 | SetNeedsUpdate(); |
407 | return true; |
408 | } else { |
409 | error.SetErrorString("unable to write back to register"); |
410 | return false; |
411 | } |
412 | } else |
413 | return ValueObject::SetData(data, error); |
414 | } |