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