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