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