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