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