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