LLDB mainline
IRInterpreter.cpp
Go to the documentation of this file.
1//===-- IRInterpreter.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
10#include "lldb/Core/Debugger.h"
11#include "lldb/Core/Module.h"
18#include "lldb/Utility/Endian.h"
20#include "lldb/Utility/Log.h"
21#include "lldb/Utility/Scalar.h"
22#include "lldb/Utility/Status.h"
25
26#include "lldb/Target/ABI.h"
28#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.h"
32
33#include "llvm/IR/Constants.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/Intrinsics.h"
38#include "llvm/IR/LLVMContext.h"
39#include "llvm/IR/Module.h"
40#include "llvm/IR/Operator.h"
41#include "llvm/Support/raw_ostream.h"
42
43#include <map>
44
45using namespace llvm;
47
48static std::string PrintValue(const Value *value, bool truncate = false) {
49 std::string s;
50 raw_string_ostream rso(s);
51 value->print(rso);
52 if (truncate)
53 s.resize(s.length() - 1);
54
55 size_t offset;
56 while ((offset = s.find('\n')) != s.npos)
57 s.erase(offset, 1);
58 while (s[0] == ' ' || s[0] == '\t')
59 s.erase(0, 1);
60
61 return s;
62}
63
64static std::string PrintType(const Type *type, bool truncate = false) {
65 std::string s;
66 raw_string_ostream rso(s);
67 type->print(rso);
68 if (truncate)
69 s.resize(s.length() - 1);
70 return s;
71}
72
73static bool CanIgnoreCall(const CallInst *call) {
74 const llvm::Function *called_function = call->getCalledFunction();
75
76 if (!called_function)
77 return false;
78
79 if (called_function->isIntrinsic()) {
80 switch (called_function->getIntrinsicID()) {
81 default:
82 break;
83 case llvm::Intrinsic::dbg_declare:
84 case llvm::Intrinsic::dbg_value:
85 return true;
86 }
87 }
88
89 return false;
90}
91
93public:
94 typedef std::map<const Value *, lldb::addr_t> ValueMap;
95
97 const DataLayout &m_target_data;
99 const BasicBlock *m_bb = nullptr;
100 const BasicBlock *m_prev_bb = nullptr;
101 BasicBlock::const_iterator m_ii;
102 BasicBlock::const_iterator m_ie;
103
107
110
111 InterpreterStackFrame(const DataLayout &target_data,
112 lldb_private::IRExecutionUnit &execution_unit,
113 lldb::addr_t stack_frame_bottom,
114 lldb::addr_t stack_frame_top)
115 : m_target_data(target_data), m_execution_unit(execution_unit) {
116 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle
118 m_addr_byte_size = (target_data.getPointerSize(0));
119
120 m_frame_process_address = stack_frame_bottom;
121 m_frame_size = stack_frame_top - stack_frame_bottom;
122 m_stack_pointer = stack_frame_top;
123 }
124
126
127 void Jump(const BasicBlock *bb) {
128 m_prev_bb = m_bb;
129 m_bb = bb;
130 m_ii = m_bb->begin();
131 m_ie = m_bb->end();
132 }
133
134 std::string SummarizeValue(const Value *value) {
136
137 ss.Printf("%s", PrintValue(value).c_str());
138
139 ValueMap::iterator i = m_values.find(value);
140
141 if (i != m_values.end()) {
142 lldb::addr_t addr = i->second;
143
144 ss.Printf(" 0x%llx", (unsigned long long)addr);
145 }
146
147 return std::string(ss.GetString());
148 }
149
150 bool AssignToMatchType(lldb_private::Scalar &scalar, llvm::APInt value,
151 Type *type) {
152 size_t type_size = m_target_data.getTypeStoreSize(type);
153
154 if (type_size > 8)
155 return false;
156
157 if (type_size != 1)
158 type_size = PowerOf2Ceil(type_size);
159
160 scalar = value.zextOrTrunc(type_size * 8);
161 return true;
162 }
163
164 bool EvaluateValue(lldb_private::Scalar &scalar, const Value *value,
165 Module &module) {
166 const Constant *constant = dyn_cast<Constant>(value);
167
168 if (constant) {
169 if (constant->getValueID() == Value::ConstantFPVal) {
170 if (auto *cfp = dyn_cast<ConstantFP>(constant)) {
171 if (cfp->getType()->isDoubleTy())
172 scalar = cfp->getValueAPF().convertToDouble();
173 else if (cfp->getType()->isFloatTy())
174 scalar = cfp->getValueAPF().convertToFloat();
175 else
176 return false;
177 return true;
178 }
179 return false;
180 }
181 APInt value_apint;
182
183 if (!ResolveConstantValue(value_apint, constant))
184 return false;
185
186 return AssignToMatchType(scalar, value_apint, value->getType());
187 }
188
189 lldb::addr_t process_address = ResolveValue(value, module);
190 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
191
192 lldb_private::DataExtractor value_extractor;
193 lldb_private::Status extract_error;
194
195 m_execution_unit.GetMemoryData(value_extractor, process_address,
196 value_size, extract_error);
197
198 if (!extract_error.Success())
199 return false;
200
201 lldb::offset_t offset = 0;
202 if (value_size <= 8) {
203 Type *ty = value->getType();
204 if (ty->isDoubleTy()) {
205 scalar = value_extractor.GetDouble(&offset);
206 return true;
207 } else if (ty->isFloatTy()) {
208 scalar = value_extractor.GetFloat(&offset);
209 return true;
210 } else {
211 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
212 return AssignToMatchType(scalar, llvm::APInt(64, u64value),
213 value->getType());
214 }
215 }
216
217 return false;
218 }
219
220 bool AssignValue(const Value *value, lldb_private::Scalar scalar,
221 Module &module) {
222 lldb::addr_t process_address = ResolveValue(value, module);
223
224 if (process_address == LLDB_INVALID_ADDRESS)
225 return false;
226
227 lldb_private::Scalar cast_scalar;
228 Type *vty = value->getType();
229 if (vty->isFloatTy() || vty->isDoubleTy()) {
230 cast_scalar = scalar;
231 } else {
232 scalar.MakeUnsigned();
233 if (!AssignToMatchType(cast_scalar, scalar.UInt128(llvm::APInt()),
234 value->getType()))
235 return false;
236 }
237
238 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
239
240 lldb_private::DataBufferHeap buf(value_byte_size, 0);
241
242 lldb_private::Status get_data_error;
243
244 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(),
245 m_byte_order, get_data_error))
246 return false;
247
248 lldb_private::Status write_error;
249
250 m_execution_unit.WriteMemory(process_address, buf.GetBytes(),
251 buf.GetByteSize(), write_error);
252
253 return write_error.Success();
254 }
255
256 bool ResolveConstantValue(APInt &value, const Constant *constant) {
257 switch (constant->getValueID()) {
258 default:
259 break;
260 case Value::FunctionVal:
261 if (const Function *constant_func = dyn_cast<Function>(constant)) {
263 llvm::GlobalValue::dropLLVMManglingEscape(
264 constant_func->getName()));
265 bool missing_weak = false;
266 lldb::addr_t addr = m_execution_unit.FindSymbol(name, missing_weak);
267 if (addr == LLDB_INVALID_ADDRESS)
268 return false;
269 value = APInt(m_target_data.getPointerSizeInBits(), addr);
270 return true;
271 }
272 break;
273 case Value::ConstantIntVal:
274 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) {
275 value = constant_int->getValue();
276 return true;
277 }
278 break;
279 case Value::ConstantFPVal:
280 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant)) {
281 value = constant_fp->getValueAPF().bitcastToAPInt();
282 return true;
283 }
284 break;
285 case Value::ConstantExprVal:
286 if (const ConstantExpr *constant_expr =
287 dyn_cast<ConstantExpr>(constant)) {
288 switch (constant_expr->getOpcode()) {
289 default:
290 return false;
291 case Instruction::IntToPtr:
292 case Instruction::PtrToInt:
293 case Instruction::BitCast:
294 return ResolveConstantValue(value, constant_expr->getOperand(0));
295 case Instruction::GetElementPtr: {
296 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
297 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
298
299 Constant *base = dyn_cast<Constant>(*op_cursor);
300
301 if (!base)
302 return false;
303
304 if (!ResolveConstantValue(value, base))
305 return false;
306
307 op_cursor++;
308
309 if (op_cursor == op_end)
310 return true; // no offset to apply!
311
312 SmallVector<Value *, 8> indices(op_cursor, op_end);
313 Type *src_elem_ty =
314 cast<GEPOperator>(constant_expr)->getSourceElementType();
315
316 // DataLayout::getIndexedOffsetInType assumes the indices are
317 // instances of ConstantInt.
318 uint64_t offset =
319 m_target_data.getIndexedOffsetInType(src_elem_ty, indices);
320
321 const bool is_signed = true;
322 value += APInt(value.getBitWidth(), offset, is_signed);
323
324 return true;
325 }
326 }
327 }
328 break;
329 case Value::ConstantPointerNullVal:
330 if (isa<ConstantPointerNull>(constant)) {
331 value = APInt(m_target_data.getPointerSizeInBits(), 0);
332 return true;
333 }
334 break;
335 }
336 return false;
337 }
338
339 bool MakeArgument(const Argument *value, uint64_t address) {
340 lldb::addr_t data_address = Malloc(value->getType());
341
342 if (data_address == LLDB_INVALID_ADDRESS)
343 return false;
344
345 lldb_private::Status write_error;
346
347 m_execution_unit.WritePointerToMemory(data_address, address, write_error);
348
349 if (!write_error.Success()) {
350 lldb_private::Status free_error;
351 m_execution_unit.Free(data_address, free_error);
352 return false;
353 }
354
355 m_values[value] = data_address;
356
357 lldb_private::Log *log(GetLog(LLDBLog::Expressions));
358
359 if (log) {
360 LLDB_LOGF(log, "Made an allocation for argument %s",
361 PrintValue(value).c_str());
362 LLDB_LOGF(log, " Data region : %llx", (unsigned long long)address);
363 LLDB_LOGF(log, " Ref region : %llx",
364 (unsigned long long)data_address);
365 }
366
367 return true;
368 }
369
370 bool ResolveConstant(lldb::addr_t process_address, const Constant *constant) {
371 APInt resolved_value;
372
373 if (!ResolveConstantValue(resolved_value, constant))
374 return false;
375
376 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
377 lldb_private::DataBufferHeap buf(constant_size, 0);
378
379 lldb_private::Status get_data_error;
380
381 lldb_private::Scalar resolved_scalar(
382 resolved_value.zextOrTrunc(llvm::NextPowerOf2(constant_size) * 8));
383 if (!resolved_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(),
384 m_byte_order, get_data_error))
385 return false;
386
387 lldb_private::Status write_error;
388
389 m_execution_unit.WriteMemory(process_address, buf.GetBytes(),
390 buf.GetByteSize(), write_error);
391
392 return write_error.Success();
393 }
394
395 lldb::addr_t Malloc(size_t size, uint8_t byte_alignment) {
397
398 ret -= size;
399 ret -= (ret % byte_alignment);
400
401 if (ret < m_frame_process_address)
403
404 m_stack_pointer = ret;
405 return ret;
406 }
407
408 lldb::addr_t Malloc(llvm::Type *type) {
409 lldb_private::Status alloc_error;
410
411 return Malloc(m_target_data.getTypeAllocSize(type),
412 m_target_data.getPrefTypeAlign(type).value());
413 }
414
415 std::string PrintData(lldb::addr_t addr, llvm::Type *type) {
416 size_t length = m_target_data.getTypeStoreSize(type);
417
418 lldb_private::DataBufferHeap buf(length, 0);
419
420 lldb_private::Status read_error;
421
422 m_execution_unit.ReadMemory(buf.GetBytes(), addr, length, read_error);
423
424 if (!read_error.Success())
425 return std::string("<couldn't read data>");
426
428
429 for (size_t i = 0; i < length; i++) {
430 if ((!(i & 0xf)) && i)
431 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
432 else
433 ss.Printf("%02hhx ", buf.GetBytes()[i]);
434 }
435
436 return std::string(ss.GetString());
437 }
438
439 lldb::addr_t ResolveValue(const Value *value, Module &module) {
440 ValueMap::iterator i = m_values.find(value);
441
442 if (i != m_values.end())
443 return i->second;
444
445 // Fall back and allocate space [allocation type Alloca]
446
447 lldb::addr_t data_address = Malloc(value->getType());
448
449 if (const Constant *constant = dyn_cast<Constant>(value)) {
450 if (!ResolveConstant(data_address, constant)) {
451 lldb_private::Status free_error;
452 m_execution_unit.Free(data_address, free_error);
454 }
455 }
456
457 m_values[value] = data_address;
458 return data_address;
459 }
460};
461
462static const char *unsupported_opcode_error =
463 "Interpreter doesn't handle one of the expression's opcodes";
464static const char *unsupported_operand_error =
465 "Interpreter doesn't handle one of the expression's operands";
466static const char *interpreter_internal_error =
467 "Interpreter encountered an internal error";
468static const char *interrupt_error =
469 "Interrupted while interpreting expression";
470static const char *bad_value_error =
471 "Interpreter couldn't resolve a value during execution";
472static const char *memory_allocation_error =
473 "Interpreter couldn't allocate memory";
474static const char *memory_write_error = "Interpreter couldn't write to memory";
475static const char *memory_read_error = "Interpreter couldn't read from memory";
476static const char *timeout_error =
477 "Reached timeout while interpreting expression";
478static const char *too_many_functions_error =
479 "Interpreter doesn't handle modules with multiple function bodies.";
480
481static bool CanResolveConstant(llvm::Constant *constant) {
482 switch (constant->getValueID()) {
483 default:
484 return false;
485 case Value::ConstantIntVal:
486 case Value::ConstantFPVal:
487 case Value::FunctionVal:
488 return true;
489 case Value::ConstantExprVal:
490 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) {
491 switch (constant_expr->getOpcode()) {
492 default:
493 return false;
494 case Instruction::IntToPtr:
495 case Instruction::PtrToInt:
496 case Instruction::BitCast:
497 return CanResolveConstant(constant_expr->getOperand(0));
498 case Instruction::GetElementPtr: {
499 // Check that the base can be constant-resolved.
500 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
501 Constant *base = dyn_cast<Constant>(*op_cursor);
502 if (!base || !CanResolveConstant(base))
503 return false;
504
505 // Check that all other operands are just ConstantInt.
506 for (Value *op : make_range(constant_expr->op_begin() + 1,
507 constant_expr->op_end())) {
508 ConstantInt *constant_int = dyn_cast<ConstantInt>(op);
509 if (!constant_int)
510 return false;
511 }
512 return true;
513 }
514 }
515 } else {
516 return false;
517 }
518 case Value::ConstantPointerNullVal:
519 return true;
520 }
521}
522
523bool IRInterpreter::CanInterpret(llvm::Module &module, llvm::Function &function,
525 const bool support_function_calls) {
526 lldb_private::Log *log(GetLog(LLDBLog::Expressions));
527
528 bool saw_function_with_body = false;
529 for (Function &f : module) {
530 if (f.begin() != f.end()) {
531 if (saw_function_with_body) {
532 LLDB_LOGF(log, "More than one function in the module has a body");
534 return false;
535 }
536 saw_function_with_body = true;
537 LLDB_LOGF(log, "Saw function with body: %s", f.getName().str().c_str());
538 }
539 }
540
541 for (BasicBlock &bb : function) {
542 for (Instruction &ii : bb) {
543 switch (ii.getOpcode()) {
544 default: {
545 LLDB_LOGF(log, "Unsupported instruction: %s", PrintValue(&ii).c_str());
547 return false;
548 }
549 case Instruction::Add:
550 case Instruction::Alloca:
551 case Instruction::BitCast:
552 case Instruction::UncondBr:
553 case Instruction::CondBr:
554 case Instruction::PHI:
555 break;
556 case Instruction::Call: {
557 CallInst *call_inst = dyn_cast<CallInst>(&ii);
558
559 if (!call_inst) {
560 error =
562 return false;
563 }
564
565 if (!CanIgnoreCall(call_inst) && !support_function_calls) {
566 LLDB_LOGF(log, "Unsupported instruction: %s",
567 PrintValue(&ii).c_str());
568 error =
570 return false;
571 }
572 } break;
573 case Instruction::GetElementPtr:
574 break;
575 case Instruction::FCmp:
576 case Instruction::ICmp: {
577 CmpInst *cmp_inst = dyn_cast<CmpInst>(&ii);
578
579 if (!cmp_inst) {
580 error =
582 return false;
583 }
584
585 switch (cmp_inst->getPredicate()) {
586 default: {
587 LLDB_LOGF(log, "Unsupported ICmp predicate: %s",
588 PrintValue(&ii).c_str());
589
590 error =
592 return false;
593 }
594 case CmpInst::FCMP_OEQ:
595 case CmpInst::ICMP_EQ:
596 case CmpInst::FCMP_UNE:
597 case CmpInst::ICMP_NE:
598 case CmpInst::FCMP_OGT:
599 case CmpInst::ICMP_UGT:
600 case CmpInst::FCMP_OGE:
601 case CmpInst::ICMP_UGE:
602 case CmpInst::FCMP_OLT:
603 case CmpInst::ICMP_ULT:
604 case CmpInst::FCMP_OLE:
605 case CmpInst::ICMP_ULE:
606 case CmpInst::ICMP_SGT:
607 case CmpInst::ICMP_SGE:
608 case CmpInst::ICMP_SLT:
609 case CmpInst::ICMP_SLE:
610 break;
611 }
612 } break;
613 case Instruction::And:
614 case Instruction::AShr:
615 case Instruction::FPToUI:
616 case Instruction::FPToSI:
617 case Instruction::IntToPtr:
618 case Instruction::PtrToInt:
619 case Instruction::Load:
620 case Instruction::LShr:
621 case Instruction::Mul:
622 case Instruction::Or:
623 case Instruction::Ret:
624 case Instruction::SDiv:
625 case Instruction::SExt:
626 case Instruction::Shl:
627 case Instruction::SRem:
628 case Instruction::Store:
629 case Instruction::Sub:
630 case Instruction::Trunc:
631 case Instruction::UDiv:
632 case Instruction::URem:
633 case Instruction::Xor:
634 case Instruction::ZExt:
635 break;
636 case Instruction::FAdd:
637 case Instruction::FSub:
638 case Instruction::FMul:
639 case Instruction::FDiv:
640 break;
641 case Instruction::UIToFP:
642 case Instruction::SIToFP:
643 case Instruction::FPTrunc:
644 case Instruction::FPExt:
645 if (!ii.getType()->isFloatTy() && !ii.getType()->isDoubleTy()) {
646 LLDB_LOGF(log, "Unsupported instruction: %s",
647 PrintValue(&ii).c_str());
648 error =
650 return false;
651 }
652 break;
653 }
654
655 for (unsigned oi = 0, oe = ii.getNumOperands(); oi != oe; ++oi) {
656 Value *operand = ii.getOperand(oi);
657 Type *operand_type = operand->getType();
658
659 switch (operand_type->getTypeID()) {
660 default:
661 break;
662 case Type::FixedVectorTyID:
663 case Type::ScalableVectorTyID: {
664 LLDB_LOGF(log, "Unsupported operand type: %s",
665 PrintType(operand_type).c_str());
666 error =
668 return false;
669 }
670 }
671
672 // The IR interpreter currently doesn't know about
673 // 128-bit integers. As they're not that frequent,
674 // we can just fall back to the JIT rather than
675 // choking.
676 if (operand_type->getPrimitiveSizeInBits() > 64) {
677 LLDB_LOGF(log, "Unsupported operand type: %s",
678 PrintType(operand_type).c_str());
679 error =
681 return false;
682 }
683
684 if (Constant *constant = llvm::dyn_cast<Constant>(operand)) {
685 if (!CanResolveConstant(constant)) {
686 LLDB_LOGF(log, "Unsupported constant: %s",
687 PrintValue(constant).c_str());
690 return false;
691 }
692 }
693 }
694 }
695 }
696
697 return true;
698}
699
700bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
701 llvm::ArrayRef<lldb::addr_t> args,
702 lldb_private::IRExecutionUnit &execution_unit,
704 lldb::addr_t stack_frame_bottom,
705 lldb::addr_t stack_frame_top,
708 lldb_private::Log *log(GetLog(LLDBLog::Expressions));
709
710 if (log) {
711 std::string s;
712 raw_string_ostream oss(s);
713
714 module.print(oss, nullptr);
715
716 LLDB_LOGF(log, "Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
717 s.c_str());
718 }
719
720 const DataLayout &data_layout = module.getDataLayout();
721
722 InterpreterStackFrame frame(data_layout, execution_unit, stack_frame_bottom,
723 stack_frame_top);
724
726 error =
727 lldb_private::Status::FromErrorString("Couldn't allocate stack frame");
728 }
729
730 int arg_index = 0;
731
732 for (llvm::Function::arg_iterator ai = function.arg_begin(),
733 ae = function.arg_end();
734 ai != ae; ++ai, ++arg_index) {
735 if (args.size() <= static_cast<size_t>(arg_index)) {
737 "Not enough arguments passed in to function");
738 return false;
739 }
740
741 lldb::addr_t ptr = args[arg_index];
742
743 frame.MakeArgument(&*ai, ptr);
744 }
745
746 frame.Jump(&function.front());
747
748 lldb_private::Process *process = exe_ctx.GetProcessPtr();
749 lldb_private::Target *target = exe_ctx.GetTargetPtr();
750
751 using clock = std::chrono::steady_clock;
752
753 // Compute the time at which the timeout has been exceeded.
754 std::optional<clock::time_point> end_time;
755 if (timeout && timeout->count() > 0)
756 end_time = clock::now() + *timeout;
757
758 while (frame.m_ii != frame.m_ie) {
759 // Timeout reached: stop interpreting.
760 if (end_time && clock::now() >= *end_time) {
762 return false;
763 }
764
765 // If we have access to the debugger we can honor an interrupt request.
766 if (target) {
767 if (INTERRUPT_REQUESTED(target->GetDebugger(),
768 "Interrupted in IR interpreting.")) {
770 return false;
771 }
772 }
773
774 const Instruction *inst = &*frame.m_ii;
775
776 LLDB_LOGF(log, "Interpreting %s", PrintValue(inst).c_str());
777
778 switch (inst->getOpcode()) {
779 default:
780 break;
781
782 case Instruction::Add:
783 case Instruction::Sub:
784 case Instruction::Mul:
785 case Instruction::SDiv:
786 case Instruction::UDiv:
787 case Instruction::SRem:
788 case Instruction::URem:
789 case Instruction::Shl:
790 case Instruction::LShr:
791 case Instruction::AShr:
792 case Instruction::And:
793 case Instruction::Or:
794 case Instruction::Xor:
795 case Instruction::FAdd:
796 case Instruction::FSub:
797 case Instruction::FMul:
798 case Instruction::FDiv: {
799 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
800
801 if (!bin_op) {
802 LLDB_LOGF(
803 log,
804 "getOpcode() returns %s, but instruction is not a BinaryOperator",
805 inst->getOpcodeName());
806 error =
808 return false;
809 }
810
811 Value *lhs = inst->getOperand(0);
812 Value *rhs = inst->getOperand(1);
813
816
817 if (!frame.EvaluateValue(L, lhs, module)) {
818 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(lhs).c_str());
820 return false;
821 }
822
823 if (!frame.EvaluateValue(R, rhs, module)) {
824 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(rhs).c_str());
826 return false;
827 }
828
830
831 switch (inst->getOpcode()) {
832 default:
833 break;
834 case Instruction::Add:
835 case Instruction::FAdd:
836 result = L + R;
837 break;
838 case Instruction::Mul:
839 case Instruction::FMul:
840 result = L * R;
841 break;
842 case Instruction::Sub:
843 case Instruction::FSub:
844 result = L - R;
845 break;
846 case Instruction::SDiv:
847 L.MakeSigned();
848 R.MakeSigned();
849 result = L / R;
850 break;
851 case Instruction::UDiv:
852 L.MakeUnsigned();
853 R.MakeUnsigned();
854 result = L / R;
855 break;
856 case Instruction::FDiv:
857 result = L / R;
858 break;
859 case Instruction::SRem:
860 L.MakeSigned();
861 R.MakeSigned();
862 result = L % R;
863 break;
864 case Instruction::URem:
865 L.MakeUnsigned();
866 R.MakeUnsigned();
867 result = L % R;
868 break;
869 case Instruction::Shl:
870 result = L << R;
871 break;
872 case Instruction::AShr:
873 result = L >> R;
874 break;
875 case Instruction::LShr:
876 result = L;
877 result.ShiftRightLogical(R);
878 break;
879 case Instruction::And:
880 result = L & R;
881 break;
882 case Instruction::Or:
883 result = L | R;
884 break;
885 case Instruction::Xor:
886 result = L ^ R;
887 break;
888 }
889
890 frame.AssignValue(inst, result, module);
891
892 if (log) {
893 LLDB_LOGF(log, "Interpreted a %s", inst->getOpcodeName());
894 LLDB_LOGF(log, " L : %s", frame.SummarizeValue(lhs).c_str());
895 LLDB_LOGF(log, " R : %s", frame.SummarizeValue(rhs).c_str());
896 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
897 }
898 } break;
899 case Instruction::Alloca: {
900 const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
901
902 std::optional<TypeSize> alloca_size =
903 alloca_inst->getAllocationSize(frame.m_target_data);
904 if (!alloca_size || alloca_size->isScalable()) {
905 LLDB_LOGF(log, "AllocaInsts are not handled if size is not computable");
907 return false;
908 }
909
910 // The semantics of Alloca are:
911 // Create a region R of virtual memory of type T, backed by a data
912 // buffer
913 // Create a region P of virtual memory of type T*, backed by a data
914 // buffer
915 // Write the virtual address of R into P
916
917 Type *Tptr = alloca_inst->getType();
918
919 lldb::addr_t R = frame.Malloc(alloca_size->getFixedValue(),
920 alloca_inst->getAlign().value());
921
922 if (R == LLDB_INVALID_ADDRESS) {
923 LLDB_LOGF(log, "Couldn't allocate memory for an AllocaInst");
925 return false;
926 }
927
928 lldb::addr_t P = frame.Malloc(Tptr);
929
930 if (P == LLDB_INVALID_ADDRESS) {
931 LLDB_LOGF(log,
932 "Couldn't allocate the result pointer for an AllocaInst");
934 return false;
935 }
936
937 lldb_private::Status write_error;
938
939 execution_unit.WritePointerToMemory(P, R, write_error);
940
941 if (!write_error.Success()) {
942 LLDB_LOGF(log, "Couldn't write the result pointer for an AllocaInst");
944 lldb_private::Status free_error;
945 execution_unit.Free(P, free_error);
946 execution_unit.Free(R, free_error);
947 return false;
948 }
949
950 frame.m_values[alloca_inst] = P;
951
952 if (log) {
953 LLDB_LOGF(log, "Interpreted an AllocaInst");
954 LLDB_LOGF(log, " R : 0x%" PRIx64, R);
955 LLDB_LOGF(log, " P : 0x%" PRIx64, P);
956 }
957 } break;
958 case Instruction::BitCast:
959 case Instruction::ZExt: {
960 const CastInst *cast_inst = cast<CastInst>(inst);
961
962 Value *source = cast_inst->getOperand(0);
963
965
966 if (!frame.EvaluateValue(S, source, module)) {
967 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(source).c_str());
969 return false;
970 }
971
972 frame.AssignValue(inst, S, module);
973 } break;
974 case Instruction::SExt: {
975 const CastInst *cast_inst = cast<CastInst>(inst);
976
977 Value *source = cast_inst->getOperand(0);
978
980
981 if (!frame.EvaluateValue(S, source, module)) {
982 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(source).c_str());
984 return false;
985 }
986
987 S.MakeSigned();
988
989 lldb_private::Scalar S_signextend(S.SLongLong());
990
991 frame.AssignValue(inst, S_signextend, module);
992 } break;
993 case Instruction::UncondBr:
994 frame.Jump(cast<UncondBrInst>(inst)->getSuccessor());
995 if (log) {
996 LLDB_LOGF(log, "Interpreted an UncondBrInst");
997 }
998 continue;
999 case Instruction::CondBr: {
1000 const CondBrInst *br_inst = cast<CondBrInst>(inst);
1001
1002 Value *condition = br_inst->getCondition();
1003
1005
1006 if (!frame.EvaluateValue(C, condition, module)) {
1007 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(condition).c_str());
1009 return false;
1010 }
1011
1012 if (!C.IsZero())
1013 frame.Jump(br_inst->getSuccessor(0));
1014 else
1015 frame.Jump(br_inst->getSuccessor(1));
1016
1017 if (log) {
1018 LLDB_LOGF(log, "Interpreted a CondBrInst");
1019 LLDB_LOGF(log, " cond : %s", frame.SummarizeValue(condition).c_str());
1020 }
1021 }
1022 continue;
1023 case Instruction::PHI: {
1024 const PHINode *phi_inst = cast<PHINode>(inst);
1025 if (!frame.m_prev_bb) {
1026 LLDB_LOGF(log,
1027 "Encountered PHI node without having jumped from another "
1028 "basic block");
1029 error =
1031 return false;
1032 }
1033
1034 Value *value = phi_inst->getIncomingValueForBlock(frame.m_prev_bb);
1035 lldb_private::Scalar result;
1036 if (!frame.EvaluateValue(result, value, module)) {
1037 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(value).c_str());
1039 return false;
1040 }
1041 frame.AssignValue(inst, result, module);
1042
1043 if (log) {
1044 LLDB_LOGF(log, "Interpreted a %s", inst->getOpcodeName());
1045 LLDB_LOGF(log, " Incoming value : %s",
1046 frame.SummarizeValue(value).c_str());
1047 }
1048 } break;
1049 case Instruction::GetElementPtr: {
1050 const GetElementPtrInst *gep_inst = cast<GetElementPtrInst>(inst);
1051
1052 const Value *pointer_operand = gep_inst->getPointerOperand();
1053 Type *src_elem_ty = gep_inst->getSourceElementType();
1054
1056
1057 if (!frame.EvaluateValue(P, pointer_operand, module)) {
1058 LLDB_LOGF(log, "Couldn't evaluate %s",
1059 PrintValue(pointer_operand).c_str());
1061 return false;
1062 }
1063
1064 typedef SmallVector<Value *, 8> IndexVector;
1065 typedef IndexVector::iterator IndexIterator;
1066
1067 SmallVector<Value *, 8> indices(gep_inst->idx_begin(),
1068 gep_inst->idx_end());
1069
1070 SmallVector<Value *, 8> const_indices;
1071
1072 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie;
1073 ++ii) {
1074 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1075
1076 if (!constant_index) {
1078
1079 if (!frame.EvaluateValue(I, *ii, module)) {
1080 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(*ii).c_str());
1082 return false;
1083 }
1084
1085 LLDB_LOGF(log, "Evaluated constant index %s as %llu",
1086 PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
1087
1088 constant_index = cast<ConstantInt>(ConstantInt::get(
1089 (*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1090 }
1091
1092 const_indices.push_back(constant_index);
1093 }
1094
1095 uint64_t offset =
1096 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
1097
1098 lldb_private::Scalar Poffset = P + offset;
1099
1100 frame.AssignValue(inst, Poffset, module);
1101
1102 if (log) {
1103 LLDB_LOGF(log, "Interpreted a GetElementPtrInst");
1104 LLDB_LOGF(log, " P : %s",
1105 frame.SummarizeValue(pointer_operand).c_str());
1106 LLDB_LOGF(log, " Poffset : %s", frame.SummarizeValue(inst).c_str());
1107 }
1108 } break;
1109 case Instruction::FCmp:
1110 case Instruction::ICmp: {
1111 const CmpInst *icmp_inst = cast<CmpInst>(inst);
1112
1113 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1114
1115 Value *lhs = inst->getOperand(0);
1116 Value *rhs = inst->getOperand(1);
1117
1120
1121 if (!frame.EvaluateValue(L, lhs, module)) {
1122 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(lhs).c_str());
1124 return false;
1125 }
1126
1127 if (!frame.EvaluateValue(R, rhs, module)) {
1128 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(rhs).c_str());
1130 return false;
1131 }
1132
1133 lldb_private::Scalar result;
1134
1135 switch (predicate) {
1136 default:
1137 return false;
1138 case CmpInst::ICMP_EQ:
1139 case CmpInst::FCMP_OEQ:
1140 result = (L == R);
1141 break;
1142 case CmpInst::ICMP_NE:
1143 case CmpInst::FCMP_UNE:
1144 result = (L != R);
1145 break;
1146 case CmpInst::ICMP_UGT:
1147 L.MakeUnsigned();
1148 R.MakeUnsigned();
1149 result = (L > R);
1150 break;
1151 case CmpInst::ICMP_UGE:
1152 L.MakeUnsigned();
1153 R.MakeUnsigned();
1154 result = (L >= R);
1155 break;
1156 case CmpInst::FCMP_OGE:
1157 result = (L >= R);
1158 break;
1159 case CmpInst::FCMP_OGT:
1160 result = (L > R);
1161 break;
1162 case CmpInst::ICMP_ULT:
1163 L.MakeUnsigned();
1164 R.MakeUnsigned();
1165 result = (L < R);
1166 break;
1167 case CmpInst::FCMP_OLT:
1168 result = (L < R);
1169 break;
1170 case CmpInst::ICMP_ULE:
1171 L.MakeUnsigned();
1172 R.MakeUnsigned();
1173 result = (L <= R);
1174 break;
1175 case CmpInst::FCMP_OLE:
1176 result = (L <= R);
1177 break;
1178 case CmpInst::ICMP_SGT:
1179 L.MakeSigned();
1180 R.MakeSigned();
1181 result = (L > R);
1182 break;
1183 case CmpInst::ICMP_SGE:
1184 L.MakeSigned();
1185 R.MakeSigned();
1186 result = (L >= R);
1187 break;
1188 case CmpInst::ICMP_SLT:
1189 L.MakeSigned();
1190 R.MakeSigned();
1191 result = (L < R);
1192 break;
1193 case CmpInst::ICMP_SLE:
1194 L.MakeSigned();
1195 R.MakeSigned();
1196 result = (L <= R);
1197 break;
1198 }
1199
1200 frame.AssignValue(inst, result, module);
1201
1202 if (log) {
1203 LLDB_LOGF(log, "Interpreted an ICmpInst");
1204 LLDB_LOGF(log, " L : %s", frame.SummarizeValue(lhs).c_str());
1205 LLDB_LOGF(log, " R : %s", frame.SummarizeValue(rhs).c_str());
1206 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1207 }
1208 } break;
1209 case Instruction::IntToPtr: {
1210 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1211
1212 Value *src_operand = int_to_ptr_inst->getOperand(0);
1213
1215
1216 if (!frame.EvaluateValue(I, src_operand, module)) {
1217 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
1219 return false;
1220 }
1221
1222 frame.AssignValue(inst, I, module);
1223
1224 if (log) {
1225 LLDB_LOGF(log, "Interpreted an IntToPtr");
1226 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1227 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1228 }
1229 } break;
1230 case Instruction::PtrToInt: {
1231 const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
1232
1233 Value *src_operand = ptr_to_int_inst->getOperand(0);
1234
1236
1237 if (!frame.EvaluateValue(I, src_operand, module)) {
1238 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
1240 return false;
1241 }
1242
1243 frame.AssignValue(inst, I, module);
1244
1245 if (log) {
1246 LLDB_LOGF(log, "Interpreted a PtrToInt");
1247 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1248 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1249 }
1250 } break;
1251 case Instruction::Trunc: {
1252 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1253
1254 Value *src_operand = trunc_inst->getOperand(0);
1255
1257
1258 if (!frame.EvaluateValue(I, src_operand, module)) {
1259 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
1261 return false;
1262 }
1263
1264 frame.AssignValue(inst, I, module);
1265
1266 if (log) {
1267 LLDB_LOGF(log, "Interpreted a Trunc");
1268 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1269 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1270 }
1271 } break;
1272 case Instruction::FPToUI:
1273 case Instruction::FPToSI: {
1274 Value *src_operand = inst->getOperand(0);
1275
1277 if (!frame.EvaluateValue(S, src_operand, module)) {
1278 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
1280 return false;
1281 }
1282
1283 assert(inst->getType()->isIntegerTy() && "Unexpected target type");
1284 llvm::APSInt result(inst->getType()->getIntegerBitWidth(),
1285 /*isUnsigned=*/inst->getOpcode() ==
1286 Instruction::FPToUI);
1287 assert(S.GetType() == lldb_private::Scalar::e_float &&
1288 "Unexpected source type");
1289 bool isExact;
1290 llvm::APFloatBase::opStatus status = S.GetAPFloat().convertToInteger(
1291 result, llvm::APFloat::rmTowardZero, &isExact);
1292 // Casting floating point values that are out of bounds of the target type
1293 // is undefined behaviour.
1294 if (status & llvm::APFloatBase::opInvalidOp) {
1295 std::string s;
1296 raw_string_ostream rso(s);
1297 rso << "Conversion error: " << S << " cannot be converted to ";
1298 if (inst->getOpcode() == Instruction::FPToUI)
1299 rso << "unsigned ";
1300 rso << *inst->getType();
1301 LLDB_LOGF(log, "%s", s.c_str());
1303 return false;
1304 }
1305 lldb_private::Scalar R(result);
1306
1307 frame.AssignValue(inst, R, module);
1308 if (log) {
1309 LLDB_LOGF(log, "Interpreted a %s", inst->getOpcodeName());
1310 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1311 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1312 }
1313 } break;
1314 case Instruction::UIToFP:
1315 case Instruction::SIToFP:
1316 case Instruction::FPTrunc:
1317 case Instruction::FPExt: {
1318 Value *src_operand = inst->getOperand(0);
1319
1321 if (!frame.EvaluateValue(S, src_operand, module)) {
1322 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
1324 return false;
1325 }
1327
1328 Type *result_type = inst->getType();
1329 assert(
1330 (result_type->isFloatTy() || result_type->isDoubleTy()) &&
1331 "Unsupported result type; CanInterpret() should have checked that");
1332 if (result_type->isFloatTy())
1333 R = S.Float();
1334 else
1335 R = S.Double();
1336
1337 frame.AssignValue(inst, R, module);
1338 if (log) {
1339 LLDB_LOGF(log, "Interpreted a %s", inst->getOpcodeName());
1340 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1341 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1342 }
1343 } break;
1344 case Instruction::Load: {
1345 const LoadInst *load_inst = cast<LoadInst>(inst);
1346
1347 // The semantics of Load are:
1348 // Create a region D that will contain the loaded data
1349 // Resolve the region P containing a pointer
1350 // Dereference P to get the region R that the data should be loaded from
1351 // Transfer a unit of type type(D) from R to D
1352
1353 const Value *pointer_operand = load_inst->getPointerOperand();
1354
1355 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1356 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1357
1358 if (D == LLDB_INVALID_ADDRESS) {
1359 LLDB_LOGF(log, "LoadInst's value doesn't resolve to anything");
1361 return false;
1362 }
1363
1364 if (P == LLDB_INVALID_ADDRESS) {
1365 LLDB_LOGF(log, "LoadInst's pointer doesn't resolve to anything");
1367 return false;
1368 }
1369
1370 lldb::addr_t R;
1371 lldb_private::Status read_error;
1372 execution_unit.ReadPointerFromMemory(&R, P, read_error);
1373
1374 if (!read_error.Success()) {
1375 LLDB_LOGF(log, "Couldn't read the address to be loaded for a LoadInst");
1377 return false;
1378 }
1379
1380 Type *target_ty = load_inst->getType();
1381 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1382 lldb_private::DataBufferHeap buffer(target_size, 0);
1383
1384 read_error.Clear();
1385 execution_unit.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(),
1386 read_error);
1387 if (!read_error.Success()) {
1388 LLDB_LOGF(log, "Couldn't read from a region on behalf of a LoadInst");
1390 return false;
1391 }
1392
1393 lldb_private::Status write_error;
1394 execution_unit.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(),
1395 write_error);
1396 if (!write_error.Success()) {
1397 LLDB_LOGF(log, "Couldn't write to a region on behalf of a LoadInst");
1399 return false;
1400 }
1401
1402 if (log) {
1403 LLDB_LOGF(log, "Interpreted a LoadInst");
1404 LLDB_LOGF(log, " P : 0x%" PRIx64, P);
1405 LLDB_LOGF(log, " R : 0x%" PRIx64, R);
1406 LLDB_LOGF(log, " D : 0x%" PRIx64, D);
1407 }
1408 } break;
1409 case Instruction::Ret: {
1410 return true;
1411 }
1412 case Instruction::Store: {
1413 const StoreInst *store_inst = cast<StoreInst>(inst);
1414
1415 // The semantics of Store are:
1416 // Resolve the region D containing the data to be stored
1417 // Resolve the region P containing a pointer
1418 // Dereference P to get the region R that the data should be stored in
1419 // Transfer a unit of type type(D) from D to R
1420
1421 const Value *value_operand = store_inst->getValueOperand();
1422 const Value *pointer_operand = store_inst->getPointerOperand();
1423
1424 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1425 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1426
1427 if (D == LLDB_INVALID_ADDRESS) {
1428 LLDB_LOGF(log, "StoreInst's value doesn't resolve to anything");
1430 return false;
1431 }
1432
1433 if (P == LLDB_INVALID_ADDRESS) {
1434 LLDB_LOGF(log, "StoreInst's pointer doesn't resolve to anything");
1436 return false;
1437 }
1438
1439 lldb::addr_t R;
1440 lldb_private::Status read_error;
1441 execution_unit.ReadPointerFromMemory(&R, P, read_error);
1442
1443 if (!read_error.Success()) {
1444 LLDB_LOGF(log, "Couldn't read the address to be loaded for a LoadInst");
1446 return false;
1447 }
1448
1449 Type *target_ty = value_operand->getType();
1450 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1451 lldb_private::DataBufferHeap buffer(target_size, 0);
1452
1453 read_error.Clear();
1454 execution_unit.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(),
1455 read_error);
1456 if (!read_error.Success()) {
1457 LLDB_LOGF(log, "Couldn't read from a region on behalf of a StoreInst");
1459 return false;
1460 }
1461
1462 lldb_private::Status write_error;
1463 execution_unit.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(),
1464 write_error);
1465 if (!write_error.Success()) {
1466 LLDB_LOGF(log, "Couldn't write to a region on behalf of a StoreInst");
1468 return false;
1469 }
1470
1471 if (log) {
1472 LLDB_LOGF(log, "Interpreted a StoreInst");
1473 LLDB_LOGF(log, " D : 0x%" PRIx64, D);
1474 LLDB_LOGF(log, " P : 0x%" PRIx64, P);
1475 LLDB_LOGF(log, " R : 0x%" PRIx64, R);
1476 }
1477 } break;
1478 case Instruction::Call: {
1479 const CallInst *call_inst = cast<CallInst>(inst);
1480
1481 if (CanIgnoreCall(call_inst))
1482 break;
1483
1484 // Get the return type
1485 llvm::Type *returnType = call_inst->getType();
1486 if (returnType == nullptr) {
1488 "unable to access return type");
1489 return false;
1490 }
1491
1492 // Work with void, integer and pointer return types
1493 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1494 !returnType->isPointerTy()) {
1496 "return type is not supported");
1497 return false;
1498 }
1499
1500 // Check we can actually get a thread
1501 if (exe_ctx.GetThreadPtr() == nullptr) {
1502 error =
1503 lldb_private::Status::FromErrorString("unable to acquire thread");
1504 return false;
1505 }
1506
1507 // Make sure we have a valid process
1508 if (!process) {
1509 error =
1510 lldb_private::Status::FromErrorString("unable to get the process");
1511 return false;
1512 }
1513
1514 // Find the address of the callee function
1516 const llvm::Value *val = call_inst->getCalledOperand();
1517
1518 if (!frame.EvaluateValue(I, val, module)) {
1520 "unable to get address of function");
1521 return false;
1522 }
1524
1527
1528 llvm::FunctionType *prototype = call_inst->getFunctionType();
1529
1530 // Find number of arguments
1531 const int numArgs = call_inst->arg_size();
1532
1533 // We work with a fixed array of 16 arguments which is our upper limit
1534 static lldb_private::ABI::CallArgument rawArgs[16];
1535 if (numArgs >= 16) {
1537 "function takes too many arguments");
1538 return false;
1539 }
1540
1541 // Push all function arguments to the argument list that will be passed
1542 // to the call function thread plan
1543 for (int i = 0; i < numArgs; i++) {
1544 // Get details of this argument
1545 llvm::Value *arg_op = call_inst->getArgOperand(i);
1546 llvm::Type *arg_ty = arg_op->getType();
1547
1548 // Ensure that this argument is an supported type
1549 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1551 "argument %d must be integer type", i);
1552 return false;
1553 }
1554
1555 // Extract the arguments value
1556 lldb_private::Scalar tmp_op = 0;
1557 if (!frame.EvaluateValue(tmp_op, arg_op, module)) {
1559 "unable to evaluate argument %d", i);
1560 return false;
1561 }
1562
1563 // Check if this is a string literal or constant string pointer
1564 if (arg_ty->isPointerTy()) {
1565 lldb::addr_t addr = tmp_op.ULongLong();
1566 size_t dataSize = 0;
1567
1568 bool Success = execution_unit.GetAllocSize(addr, dataSize);
1570 assert(Success &&
1571 "unable to locate host data for transfer to device");
1572 // Create the required buffer
1573 rawArgs[i].size = dataSize;
1574 rawArgs[i].data_up.reset(new uint8_t[dataSize + 1]);
1575
1576 // Read string from host memory
1577 execution_unit.ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1578 error);
1579 assert(!error.Fail() &&
1580 "we have failed to read the string from memory");
1581
1582 // Add null terminator
1583 rawArgs[i].data_up[dataSize] = '\0';
1585 } else /* if ( arg_ty->isPointerTy() ) */
1586 {
1588 // Get argument size in bytes
1589 rawArgs[i].size = arg_ty->getIntegerBitWidth() / 8;
1590 // Push value into argument list for thread plan
1591 rawArgs[i].value = tmp_op.ULongLong();
1592 }
1593 }
1594
1595 // Pack the arguments into an llvm::array
1596 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1597
1598 // Setup a thread plan to call the target function
1599 lldb::ThreadPlanSP call_plan_sp(
1601 exe_ctx.GetThreadRef(), funcAddr, *prototype, *returnType, args,
1602 options));
1603
1604 // Check if the plan is valid
1606 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1608 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1609 I.ULongLong());
1610 return false;
1611 }
1612
1613 process->SetRunningUserExpression(true);
1614
1615 // Execute the actual function call thread plan
1617 process->RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
1618
1619 // Check that the thread plan completed successfully
1622 "ThreadPlanCallFunctionUsingABI failed");
1623 return false;
1624 }
1625
1626 process->SetRunningUserExpression(false);
1627
1628 // Void return type
1629 if (returnType->isVoidTy()) {
1630 // Cant assign to void types, so we leave the frame untouched
1631 } else
1632 // Integer or pointer return type
1633 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1634 // Get the encapsulated return value
1635 lldb::ValueObjectSP retVal = call_plan_sp.get()->GetReturnValueObject();
1636
1637 lldb_private::Scalar returnVal = -1;
1638 lldb_private::ValueObject *vobj = retVal.get();
1639
1640 // Check if the return value is valid
1641 if (vobj == nullptr || !retVal) {
1643 "unable to get the return value");
1644 return false;
1645 }
1646
1647 // Extract the return value as a integer
1648 lldb_private::Value &value = vobj->GetValue();
1649 returnVal = value.GetScalar();
1650
1651 // Push the return value as the result
1652 frame.AssignValue(inst, returnVal, module);
1653 }
1654 } break;
1655 }
1656
1657 ++frame.m_ii;
1658 }
1659
1660 return false;
1661}
static llvm::raw_ostream & error(Stream &strm)
#define INTERRUPT_REQUESTED(debugger,...)
This handy define will keep you from having to generate a report for the interruption by hand.
Definition Debugger.h:474
static bool CanResolveConstant(llvm::Constant *constant)
static const char * memory_allocation_error
static const char * memory_read_error
static std::string PrintValue(const Value *value, bool truncate=false)
static const char * interpreter_internal_error
static std::string PrintType(const Type *type, bool truncate=false)
static const char * interrupt_error
static const char * unsupported_operand_error
static const char * timeout_error
static bool CanIgnoreCall(const CallInst *call)
static const char * memory_write_error
static const char * unsupported_opcode_error
static const char * bad_value_error
static const char * too_many_functions_error
#define LLDB_LOGF(log,...)
Definition Log.h:376
static bool CanInterpret(llvm::Module &module, llvm::Function &function, lldb_private::Status &error, const bool support_function_calls)
static bool Interpret(llvm::Module &module, llvm::Function &function, llvm::ArrayRef< lldb::addr_t > args, lldb_private::IRExecutionUnit &execution_unit, lldb_private::Status &error, lldb::addr_t stack_frame_bottom, lldb::addr_t stack_frame_top, lldb_private::ExecutionContext &exe_ctx, lldb_private::Timeout< std::micro > timeout)
lldb::addr_t ResolveValue(const Value *value, Module &module)
const BasicBlock * m_bb
std::string SummarizeValue(const Value *value)
bool ResolveConstantValue(APInt &value, const Constant *constant)
bool ResolveConstant(lldb::addr_t process_address, const Constant *constant)
const DataLayout & m_target_data
lldb_private::IRExecutionUnit & m_execution_unit
bool MakeArgument(const Argument *value, uint64_t address)
lldb::addr_t Malloc(size_t size, uint8_t byte_alignment)
const BasicBlock * m_prev_bb
BasicBlock::const_iterator m_ie
lldb::addr_t Malloc(llvm::Type *type)
bool AssignToMatchType(lldb_private::Scalar &scalar, llvm::APInt value, Type *type)
~InterpreterStackFrame()=default
InterpreterStackFrame(const DataLayout &target_data, lldb_private::IRExecutionUnit &execution_unit, lldb::addr_t stack_frame_bottom, lldb::addr_t stack_frame_top)
void Jump(const BasicBlock *bb)
std::string PrintData(lldb::addr_t addr, llvm::Type *type)
std::map< const Value *, lldb::addr_t > ValueMap
BasicBlock::const_iterator m_ii
lldb::ByteOrder m_byte_order
bool EvaluateValue(lldb_private::Scalar &scalar, const Value *value, Module &module)
bool AssignValue(const Value *value, lldb_private::Scalar scalar, Module &module)
lldb::addr_t m_frame_process_address
A section + offset based address class.
Definition Address.h:62
A uniqued constant string class.
Definition ConstString.h:40
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
An data extractor class.
float GetFloat(lldb::offset_t *offset_ptr) const
Extract a float from *offset_ptr.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
double GetDouble(lldb::offset_t *offset_ptr) const
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
Thread & GetThreadRef() const
Returns a reference to the thread object.
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
"lldb/Expression/IRExecutionUnit.h" Contains the IR and, optionally, JIT- compiled code for a module.
void Free(lldb::addr_t process_address, Status &error)
void ReadPointerFromMemory(lldb::addr_t *address, lldb::addr_t process_address, Status &error)
void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t pointer, Status &error)
bool GetAllocSize(lldb::addr_t address, size_t &size)
void WriteMemory(lldb::addr_t process_address, const uint8_t *bytes, size_t size, Status &error)
void ReadMemory(uint8_t *bytes, lldb::addr_t process_address, size_t size, Status &error)
A plug-in interface definition class for debugging a process.
Definition Process.h:354
lldb::ExpressionResults RunThreadPlan(ExecutionContext &exe_ctx, lldb::ThreadPlanSP &thread_plan_sp, const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager)
Definition Process.cpp:5096
void SetRunningUserExpression(bool on)
Definition Process.cpp:1447
bool IsZero() const
Definition Scalar.cpp:174
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:365
size_t GetAsMemoryData(void *dst, size_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
Definition Scalar.cpp:788
float Float(float fail_value=0.0f) const
Definition Scalar.cpp:393
double Double(double fail_value=0.0) const
Definition Scalar.cpp:413
bool ShiftRightLogical(const Scalar &rhs)
Definition Scalar.cpp:464
llvm::APInt UInt128(const llvm::APInt &fail_value) const
Definition Scalar.cpp:381
An error handling class.
Definition Status.h:118
void Clear()
Clear the object state.
Definition Status.cpp:214
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Success() const
Test for success condition.
Definition Status.cpp:303
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
Debugger & GetDebugger() const
Definition Target.h:1224
const Value & GetValue() const
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:113
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define UNUSED_IF_ASSERT_DISABLED(x)
#define LLDB_INVALID_ADDRESS
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
uint64_t offset_t
Definition lldb-types.h:85
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
ByteOrder
Byte ordering definitions.
uint64_t addr_t
Definition lldb-types.h:80
std::unique_ptr< uint8_t[]> data_up
Definition ABI.h:39