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