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