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/DataEncoder.h" 11 #include "lldb/Core/Log.h" 12 #include "lldb/Core/ValueObjectConstResult.h" 13 #include "lldb/Expression/ClangExpressionDeclMap.h" 14 #include "lldb/Expression/ClangExpressionVariable.h" 15 #include "lldb/Expression/IRForTarget.h" 16 #include "lldb/Expression/IRInterpreter.h" 17 18 #include "llvm/Constants.h" 19 #include "llvm/Function.h" 20 #include "llvm/Instructions.h" 21 #include "llvm/Module.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include "llvm/DataLayout.h" 24 25 #include <map> 26 27 using namespace llvm; 28 29 IRInterpreter::IRInterpreter(lldb_private::ClangExpressionDeclMap &decl_map, 30 lldb_private::Stream *error_stream) : 31 m_decl_map(decl_map) 32 { 33 34 } 35 36 IRInterpreter::~IRInterpreter() 37 { 38 39 } 40 41 static std::string 42 PrintValue(const Value *value, bool truncate = false) 43 { 44 std::string s; 45 raw_string_ostream rso(s); 46 value->print(rso); 47 rso.flush(); 48 if (truncate) 49 s.resize(s.length() - 1); 50 51 size_t offset; 52 while ((offset = s.find('\n')) != s.npos) 53 s.erase(offset, 1); 54 while (s[0] == ' ' || s[0] == '\t') 55 s.erase(0, 1); 56 57 return s; 58 } 59 60 static std::string 61 PrintType(const Type *type, bool truncate = false) 62 { 63 std::string s; 64 raw_string_ostream rso(s); 65 type->print(rso); 66 rso.flush(); 67 if (truncate) 68 s.resize(s.length() - 1); 69 return s; 70 } 71 72 typedef STD_SHARED_PTR(lldb_private::DataEncoder) DataEncoderSP; 73 typedef STD_SHARED_PTR(lldb_private::DataExtractor) DataExtractorSP; 74 75 class Memory 76 { 77 public: 78 typedef uint32_t index_t; 79 80 struct Allocation 81 { 82 // m_virtual_address is always the address of the variable in the virtual memory 83 // space provided by Memory. 84 // 85 // m_origin is always non-NULL and describes the source of the data (possibly 86 // m_data if this allocation is the authoritative source). 87 // 88 // Possible value configurations: 89 // 90 // Allocation type getValueType() getContextType() m_origin->GetScalar() m_data 91 // ========================================================================================================================= 92 // FileAddress eValueTypeFileAddress eContextTypeInvalid A location in a binary NULL 93 // image 94 // 95 // LoadAddress eValueTypeLoadAddress eContextTypeInvalid A location in the target's NULL 96 // virtual memory 97 // 98 // Alloca eValueTypeHostAddress eContextTypeInvalid == m_data->GetBytes() Deleted at end of 99 // execution 100 // 101 // PersistentVar eValueTypeHostAddress eContextTypeClangType A persistent variable's NULL 102 // location in LLDB's memory 103 // 104 // Register [ignored] eContextTypeRegister [ignored] Flushed to the register 105 // at the end of execution 106 107 lldb::addr_t m_virtual_address; 108 size_t m_extent; 109 lldb_private::Value m_origin; 110 lldb::DataBufferSP m_data; 111 112 Allocation (lldb::addr_t virtual_address, 113 size_t extent, 114 lldb::DataBufferSP data) : 115 m_virtual_address(virtual_address), 116 m_extent(extent), 117 m_data(data) 118 { 119 } 120 121 Allocation (const Allocation &allocation) : 122 m_virtual_address(allocation.m_virtual_address), 123 m_extent(allocation.m_extent), 124 m_origin(allocation.m_origin), 125 m_data(allocation.m_data) 126 { 127 } 128 }; 129 130 typedef STD_SHARED_PTR(Allocation) AllocationSP; 131 132 struct Region 133 { 134 AllocationSP m_allocation; 135 uint64_t m_base; 136 uint64_t m_extent; 137 138 Region () : 139 m_allocation(), 140 m_base(0), 141 m_extent(0) 142 { 143 } 144 145 Region (AllocationSP allocation, uint64_t base, uint64_t extent) : 146 m_allocation(allocation), 147 m_base(base), 148 m_extent(extent) 149 { 150 } 151 152 Region (const Region ®ion) : 153 m_allocation(region.m_allocation), 154 m_base(region.m_base), 155 m_extent(region.m_extent) 156 { 157 } 158 159 bool IsValid () 160 { 161 return (bool) m_allocation; 162 } 163 164 bool IsInvalid () 165 { 166 return !m_allocation; 167 } 168 }; 169 170 typedef std::vector <AllocationSP> MemoryMap; 171 172 private: 173 lldb::addr_t m_addr_base; 174 lldb::addr_t m_addr_max; 175 MemoryMap m_memory; 176 lldb::ByteOrder m_byte_order; 177 lldb::addr_t m_addr_byte_size; 178 DataLayout &m_target_data; 179 180 lldb_private::ClangExpressionDeclMap &m_decl_map; 181 182 MemoryMap::iterator LookupInternal (lldb::addr_t addr) 183 { 184 for (MemoryMap::iterator i = m_memory.begin(), e = m_memory.end(); 185 i != e; 186 ++i) 187 { 188 if ((*i)->m_virtual_address <= addr && 189 (*i)->m_virtual_address + (*i)->m_extent > addr) 190 return i; 191 } 192 193 return m_memory.end(); 194 } 195 196 public: 197 Memory (DataLayout &target_data, 198 lldb_private::ClangExpressionDeclMap &decl_map, 199 lldb::addr_t alloc_start, 200 lldb::addr_t alloc_max) : 201 m_addr_base(alloc_start), 202 m_addr_max(alloc_max), 203 m_target_data(target_data), 204 m_decl_map(decl_map) 205 { 206 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig); 207 m_addr_byte_size = (target_data.getPointerSize(0)); 208 } 209 210 Region Malloc (size_t size, size_t align) 211 { 212 lldb::DataBufferSP data(new lldb_private::DataBufferHeap(size, 0)); 213 214 if (data) 215 { 216 index_t index = m_memory.size(); 217 218 const size_t mask = (align - 1); 219 220 m_addr_base += mask; 221 m_addr_base &= ~mask; 222 223 if (m_addr_base + size < m_addr_base || 224 m_addr_base + size > m_addr_max) 225 return Region(); 226 227 uint64_t base = m_addr_base; 228 229 m_memory.push_back(AllocationSP(new Allocation(base, size, data))); 230 231 m_addr_base += size; 232 233 AllocationSP alloc = m_memory[index]; 234 235 alloc->m_origin.GetScalar() = (unsigned long long)data->GetBytes(); 236 alloc->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 237 alloc->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress); 238 239 return Region(alloc, base, size); 240 } 241 242 return Region(); 243 } 244 245 Region Malloc (Type *type) 246 { 247 return Malloc (m_target_data.getTypeAllocSize(type), 248 m_target_data.getPrefTypeAlignment(type)); 249 } 250 251 Region Place (Type *type, lldb::addr_t base, lldb_private::Value &value) 252 { 253 index_t index = m_memory.size(); 254 size_t size = m_target_data.getTypeAllocSize(type); 255 256 m_memory.push_back(AllocationSP(new Allocation(base, size, lldb::DataBufferSP()))); 257 258 AllocationSP alloc = m_memory[index]; 259 260 alloc->m_origin = value; 261 262 return Region(alloc, base, size); 263 } 264 265 void Free (lldb::addr_t addr) 266 { 267 MemoryMap::iterator i = LookupInternal (addr); 268 269 if (i != m_memory.end()) 270 m_memory.erase(i); 271 } 272 273 Region Lookup (lldb::addr_t addr, Type *type) 274 { 275 MemoryMap::iterator i = LookupInternal(addr); 276 277 if (i == m_memory.end() || !type->isSized()) 278 return Region(); 279 280 size_t size = m_target_data.getTypeStoreSize(type); 281 282 return Region(*i, addr, size); 283 } 284 285 DataEncoderSP GetEncoder (Region region) 286 { 287 if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress) 288 return DataEncoderSP(); 289 290 lldb::DataBufferSP buffer = region.m_allocation->m_data; 291 292 if (!buffer) 293 return DataEncoderSP(); 294 295 size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address); 296 297 return DataEncoderSP(new lldb_private::DataEncoder(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size)); 298 } 299 300 DataExtractorSP GetExtractor (Region region) 301 { 302 if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress) 303 return DataExtractorSP(); 304 305 lldb::DataBufferSP buffer = region.m_allocation->m_data; 306 size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address); 307 308 if (buffer) 309 return DataExtractorSP(new lldb_private::DataExtractor(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size)); 310 else 311 return DataExtractorSP(new lldb_private::DataExtractor((uint8_t*)region.m_allocation->m_origin.GetScalar().ULongLong() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size)); 312 } 313 314 lldb_private::Value GetAccessTarget(lldb::addr_t addr) 315 { 316 MemoryMap::iterator i = LookupInternal(addr); 317 318 if (i == m_memory.end()) 319 return lldb_private::Value(); 320 321 lldb_private::Value target = (*i)->m_origin; 322 323 if (target.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo) 324 { 325 target.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 326 target.SetValueType(lldb_private::Value::eValueTypeHostAddress); 327 target.GetScalar() = (unsigned long long)(*i)->m_data->GetBytes(); 328 } 329 330 target.GetScalar() += (addr - (*i)->m_virtual_address); 331 332 return target; 333 } 334 335 bool Write (lldb::addr_t addr, const uint8_t *data, size_t length) 336 { 337 lldb_private::Value target = GetAccessTarget(addr); 338 339 return m_decl_map.WriteTarget(target, data, length); 340 } 341 342 bool Read (uint8_t *data, lldb::addr_t addr, size_t length) 343 { 344 lldb_private::Value source = GetAccessTarget(addr); 345 346 return m_decl_map.ReadTarget(data, source, length); 347 } 348 349 bool WriteToRawPtr (lldb::addr_t addr, const uint8_t *data, size_t length) 350 { 351 lldb_private::Value target = m_decl_map.WrapBareAddress(addr); 352 353 return m_decl_map.WriteTarget(target, data, length); 354 } 355 356 bool ReadFromRawPtr (uint8_t *data, lldb::addr_t addr, size_t length) 357 { 358 lldb_private::Value source = m_decl_map.WrapBareAddress(addr); 359 360 return m_decl_map.ReadTarget(data, source, length); 361 } 362 363 std::string PrintData (lldb::addr_t addr, size_t length) 364 { 365 lldb_private::Value target = GetAccessTarget(addr); 366 367 lldb_private::DataBufferHeap buf(length, 0); 368 369 if (!m_decl_map.ReadTarget(buf.GetBytes(), target, length)) 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 std::string SummarizeRegion (Region ®ion) 386 { 387 lldb_private::StreamString ss; 388 389 lldb_private::Value base = GetAccessTarget(region.m_base); 390 391 ss.Printf("%" PRIx64 " [%s - %s %llx]", 392 region.m_base, 393 lldb_private::Value::GetValueTypeAsCString(base.GetValueType()), 394 lldb_private::Value::GetContextTypeAsCString(base.GetContextType()), 395 base.GetScalar().ULongLong()); 396 397 ss.Printf(" %s", PrintData(region.m_base, region.m_extent).c_str()); 398 399 return ss.GetString(); 400 } 401 }; 402 403 class InterpreterStackFrame 404 { 405 public: 406 typedef std::map <const Value*, Memory::Region> ValueMap; 407 408 ValueMap m_values; 409 Memory &m_memory; 410 DataLayout &m_target_data; 411 lldb_private::ClangExpressionDeclMap &m_decl_map; 412 const BasicBlock *m_bb; 413 BasicBlock::const_iterator m_ii; 414 BasicBlock::const_iterator m_ie; 415 416 lldb::ByteOrder m_byte_order; 417 size_t m_addr_byte_size; 418 419 InterpreterStackFrame (DataLayout &target_data, 420 Memory &memory, 421 lldb_private::ClangExpressionDeclMap &decl_map) : 422 m_memory (memory), 423 m_target_data (target_data), 424 m_decl_map (decl_map) 425 { 426 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig); 427 m_addr_byte_size = (target_data.getPointerSize(0)); 428 } 429 430 void Jump (const BasicBlock *bb) 431 { 432 m_bb = bb; 433 m_ii = m_bb->begin(); 434 m_ie = m_bb->end(); 435 } 436 437 bool Cache (Memory::AllocationSP allocation, Type *type) 438 { 439 if (allocation->m_origin.GetContextType() != lldb_private::Value::eContextTypeRegisterInfo) 440 return false; 441 442 return m_decl_map.ReadTarget(allocation->m_data->GetBytes(), allocation->m_origin, allocation->m_data->GetByteSize()); 443 } 444 445 std::string SummarizeValue (const Value *value) 446 { 447 lldb_private::StreamString ss; 448 449 ss.Printf("%s", PrintValue(value).c_str()); 450 451 ValueMap::iterator i = m_values.find(value); 452 453 if (i != m_values.end()) 454 { 455 Memory::Region region = i->second; 456 457 ss.Printf(" %s", m_memory.SummarizeRegion(region).c_str()); 458 } 459 460 return ss.GetString(); 461 } 462 463 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type) 464 { 465 size_t type_size = m_target_data.getTypeStoreSize(type); 466 467 switch (type_size) 468 { 469 case 1: 470 scalar = (uint8_t)u64value; 471 break; 472 case 2: 473 scalar = (uint16_t)u64value; 474 break; 475 case 4: 476 scalar = (uint32_t)u64value; 477 break; 478 case 8: 479 scalar = (uint64_t)u64value; 480 break; 481 default: 482 return false; 483 } 484 485 return true; 486 } 487 488 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module) 489 { 490 const Constant *constant = dyn_cast<Constant>(value); 491 492 if (constant) 493 { 494 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) 495 { 496 return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType()); 497 } 498 } 499 else 500 { 501 Memory::Region region = ResolveValue(value, module); 502 DataExtractorSP value_extractor = m_memory.GetExtractor(region); 503 504 if (!value_extractor) 505 return false; 506 507 size_t value_size = m_target_data.getTypeStoreSize(value->getType()); 508 509 uint32_t offset = 0; 510 uint64_t u64value = value_extractor->GetMaxU64(&offset, value_size); 511 512 return AssignToMatchType(scalar, u64value, value->getType()); 513 } 514 515 return false; 516 } 517 518 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module) 519 { 520 Memory::Region region = ResolveValue (value, module); 521 522 lldb_private::Scalar cast_scalar; 523 524 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType())) 525 return false; 526 527 lldb_private::DataBufferHeap buf(cast_scalar.GetByteSize(), 0); 528 529 lldb_private::Error err; 530 531 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, err)) 532 return false; 533 534 DataEncoderSP region_encoder = m_memory.GetEncoder(region); 535 536 memcpy(region_encoder->GetDataStart(), buf.GetBytes(), buf.GetByteSize()); 537 538 return true; 539 } 540 541 bool ResolveConstantValue (APInt &value, const Constant *constant) 542 { 543 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) 544 { 545 value = constant_int->getValue(); 546 return true; 547 } 548 else if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant)) 549 { 550 value = constant_fp->getValueAPF().bitcastToAPInt(); 551 return true; 552 } 553 else if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) 554 { 555 switch (constant_expr->getOpcode()) 556 { 557 default: 558 return false; 559 case Instruction::IntToPtr: 560 case Instruction::PtrToInt: 561 case Instruction::BitCast: 562 return ResolveConstantValue(value, constant_expr->getOperand(0)); 563 case Instruction::GetElementPtr: 564 { 565 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin(); 566 ConstantExpr::const_op_iterator op_end = constant_expr->op_end(); 567 568 Constant *base = dyn_cast<Constant>(*op_cursor); 569 570 if (!base) 571 return false; 572 573 if (!ResolveConstantValue(value, base)) 574 return false; 575 576 op_cursor++; 577 578 if (op_cursor == op_end) 579 return true; // no offset to apply! 580 581 SmallVector <Value *, 8> indices (op_cursor, op_end); 582 583 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices); 584 585 const bool is_signed = true; 586 value += APInt(value.getBitWidth(), offset, is_signed); 587 588 return true; 589 } 590 } 591 } 592 593 return false; 594 } 595 596 bool ResolveConstant (Memory::Region ®ion, const Constant *constant) 597 { 598 APInt resolved_value; 599 600 if (!ResolveConstantValue(resolved_value, constant)) 601 return false; 602 603 const uint64_t *raw_data = resolved_value.getRawData(); 604 605 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType()); 606 return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size); 607 } 608 609 Memory::Region ResolveValue (const Value *value, Module &module) 610 { 611 ValueMap::iterator i = m_values.find(value); 612 613 if (i != m_values.end()) 614 return i->second; 615 616 const GlobalValue *global_value = dyn_cast<GlobalValue>(value); 617 618 // If the variable is indirected through the argument 619 // array then we need to build an extra level of indirection 620 // for it. This is the default; only magic arguments like 621 // "this", "self", and "_cmd" are direct. 622 bool indirect_variable = true; 623 624 // Attempt to resolve the value using the program's data. 625 // If it is, the values to be created are: 626 // 627 // data_region - a region of memory in which the variable's data resides. 628 // ref_region - a region of memory in which its address (i.e., &var) resides. 629 // In the JIT case, this region would be a member of the struct passed in. 630 // pointer_region - a region of memory in which the address of the pointer 631 // resides. This is an IR-level variable. 632 do 633 { 634 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 635 636 lldb_private::Value resolved_value; 637 lldb_private::ClangExpressionVariable::FlagType flags = 0; 638 639 if (global_value) 640 { 641 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module); 642 643 if (!decl) 644 break; 645 646 if (isa<clang::FunctionDecl>(decl)) 647 { 648 if (log) 649 log->Printf("The interpreter does not handle function pointers at the moment"); 650 651 return Memory::Region(); 652 } 653 654 resolved_value = m_decl_map.LookupDecl(decl, flags); 655 } 656 else 657 { 658 // Special-case "this", "self", and "_cmd" 659 660 std::string name_str = value->getName().str(); 661 662 if (name_str == "this" || 663 name_str == "self" || 664 name_str == "_cmd") 665 resolved_value = m_decl_map.GetSpecialValue(lldb_private::ConstString(name_str.c_str())); 666 667 indirect_variable = false; 668 } 669 670 if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void) 671 { 672 if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo) 673 { 674 bool bare_register = (flags & lldb_private::ClangExpressionVariable::EVBareRegister); 675 676 if (bare_register) 677 indirect_variable = false; 678 679 lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo(); 680 Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ? 681 m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) : 682 m_memory.Malloc(value->getType()); 683 684 data_region.m_allocation->m_origin = resolved_value; 685 Memory::Region ref_region = m_memory.Malloc(value->getType()); 686 Memory::Region pointer_region; 687 688 if (indirect_variable) 689 pointer_region = m_memory.Malloc(value->getType()); 690 691 if (!Cache(data_region.m_allocation, value->getType())) 692 return Memory::Region(); 693 694 if (ref_region.IsInvalid()) 695 return Memory::Region(); 696 697 if (pointer_region.IsInvalid() && indirect_variable) 698 return Memory::Region(); 699 700 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region); 701 702 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX) 703 return Memory::Region(); 704 705 if (log) 706 { 707 log->Printf("Made an allocation for register variable %s", PrintValue(value).c_str()); 708 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str()); 709 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base); 710 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base); 711 if (indirect_variable) 712 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base); 713 } 714 715 if (indirect_variable) 716 { 717 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region); 718 719 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX) 720 return Memory::Region(); 721 722 m_values[value] = pointer_region; 723 return pointer_region; 724 } 725 else 726 { 727 m_values[value] = ref_region; 728 return ref_region; 729 } 730 } 731 else 732 { 733 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value); 734 Memory::Region ref_region = m_memory.Malloc(value->getType()); 735 Memory::Region pointer_region; 736 737 if (indirect_variable) 738 pointer_region = m_memory.Malloc(value->getType()); 739 740 if (ref_region.IsInvalid()) 741 return Memory::Region(); 742 743 if (pointer_region.IsInvalid() && indirect_variable) 744 return Memory::Region(); 745 746 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region); 747 748 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX) 749 return Memory::Region(); 750 751 if (indirect_variable) 752 { 753 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region); 754 755 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX) 756 return Memory::Region(); 757 758 m_values[value] = pointer_region; 759 } 760 761 if (log) 762 { 763 log->Printf("Made an allocation for %s", PrintValue(value).c_str()); 764 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str()); 765 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base); 766 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base); 767 if (indirect_variable) 768 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base); 769 } 770 771 if (indirect_variable) 772 return pointer_region; 773 else 774 return ref_region; 775 } 776 } 777 } 778 while(0); 779 780 // Fall back and allocate space [allocation type Alloca] 781 782 Type *type = value->getType(); 783 784 lldb::ValueSP backing_value(new lldb_private::Value); 785 786 Memory::Region data_region = m_memory.Malloc(type); 787 data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes(); 788 data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 789 data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress); 790 791 const Constant *constant = dyn_cast<Constant>(value); 792 793 do 794 { 795 if (!constant) 796 break; 797 798 if (!ResolveConstant (data_region, constant)) 799 return Memory::Region(); 800 } 801 while(0); 802 803 m_values[value] = data_region; 804 return data_region; 805 } 806 807 bool ConstructResult (lldb::ClangExpressionVariableSP &result, 808 const GlobalValue *result_value, 809 const lldb_private::ConstString &result_name, 810 lldb_private::TypeFromParser result_type, 811 Module &module) 812 { 813 // The result_value resolves to P, a pointer to a region R containing the result data. 814 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process. 815 816 if (!result_value) 817 return true; // There was no slot for a result – the expression doesn't return one. 818 819 ValueMap::iterator i = m_values.find(result_value); 820 821 if (i == m_values.end()) 822 return false; // There was a slot for the result, but we didn't write into it. 823 824 Memory::Region P = i->second; 825 DataExtractorSP P_extractor = m_memory.GetExtractor(P); 826 827 if (!P_extractor) 828 return false; 829 830 Type *pointer_ty = result_value->getType(); 831 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 832 if (!pointer_ptr_ty) 833 return false; 834 Type *R_ty = pointer_ptr_ty->getElementType(); 835 836 uint32_t offset = 0; 837 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 838 839 Memory::Region R = m_memory.Lookup(pointer, R_ty); 840 841 if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress || 842 !R.m_allocation->m_data) 843 return false; 844 845 lldb_private::Value base; 846 847 bool transient = false; 848 bool maybe_make_load = false; 849 850 if (m_decl_map.ResultIsReference(result_name)) 851 { 852 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty); 853 if (!R_ptr_ty) 854 return false; 855 Type *R_final_ty = R_ptr_ty->getElementType(); 856 857 DataExtractorSP R_extractor = m_memory.GetExtractor(R); 858 859 if (!R_extractor) 860 return false; 861 862 offset = 0; 863 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset); 864 865 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty); 866 867 if (R_final.m_allocation) 868 { 869 if (R_final.m_allocation->m_data) 870 transient = true; // this is a stack allocation 871 872 base = R_final.m_allocation->m_origin; 873 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address); 874 } 875 else 876 { 877 // We got a bare pointer. We are going to treat it as a load address 878 // or a file address, letting decl_map make the choice based on whether 879 // or not a process exists. 880 881 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 882 base.SetValueType(lldb_private::Value::eValueTypeFileAddress); 883 base.GetScalar() = (unsigned long long)R_pointer; 884 maybe_make_load = true; 885 } 886 } 887 else 888 { 889 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 890 base.SetValueType(lldb_private::Value::eValueTypeHostAddress); 891 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address); 892 } 893 894 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient, maybe_make_load); 895 } 896 }; 897 898 bool 899 IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result, 900 const lldb_private::ConstString &result_name, 901 lldb_private::TypeFromParser result_type, 902 Function &llvm_function, 903 Module &llvm_module, 904 lldb_private::Error &err) 905 { 906 if (supportsFunction (llvm_function, err)) 907 return runOnFunction(result, 908 result_name, 909 result_type, 910 llvm_function, 911 llvm_module, 912 err); 913 else 914 return false; 915 } 916 917 static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes"; 918 static const char *interpreter_initialization_error = "Interpreter couldn't be initialized"; 919 static const char *interpreter_internal_error = "Interpreter encountered an internal error"; 920 static const char *bad_value_error = "Interpreter couldn't resolve a value during execution"; 921 static const char *memory_allocation_error = "Interpreter couldn't allocate memory"; 922 static const char *memory_write_error = "Interpreter couldn't write to memory"; 923 static const char *memory_read_error = "Interpreter couldn't read from memory"; 924 static const char *infinite_loop_error = "Interpreter ran for too many cycles"; 925 static const char *bad_result_error = "Result of expression is in bad memory"; 926 927 bool 928 IRInterpreter::supportsFunction (Function &llvm_function, 929 lldb_private::Error &err) 930 { 931 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 932 933 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end(); 934 bbi != bbe; 935 ++bbi) 936 { 937 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end(); 938 ii != ie; 939 ++ii) 940 { 941 switch (ii->getOpcode()) 942 { 943 default: 944 { 945 if (log) 946 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str()); 947 err.SetErrorToGenericError(); 948 err.SetErrorString(unsupported_opcode_error); 949 return false; 950 } 951 case Instruction::Add: 952 case Instruction::Alloca: 953 case Instruction::BitCast: 954 case Instruction::Br: 955 case Instruction::GetElementPtr: 956 break; 957 case Instruction::ICmp: 958 { 959 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii); 960 961 if (!icmp_inst) 962 { 963 err.SetErrorToGenericError(); 964 err.SetErrorString(interpreter_internal_error); 965 return false; 966 } 967 968 switch (icmp_inst->getPredicate()) 969 { 970 default: 971 { 972 if (log) 973 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str()); 974 975 err.SetErrorToGenericError(); 976 err.SetErrorString(unsupported_opcode_error); 977 return false; 978 } 979 case CmpInst::ICMP_EQ: 980 case CmpInst::ICMP_NE: 981 case CmpInst::ICMP_UGT: 982 case CmpInst::ICMP_UGE: 983 case CmpInst::ICMP_ULT: 984 case CmpInst::ICMP_ULE: 985 case CmpInst::ICMP_SGT: 986 case CmpInst::ICMP_SGE: 987 case CmpInst::ICMP_SLT: 988 case CmpInst::ICMP_SLE: 989 break; 990 } 991 } 992 break; 993 case Instruction::IntToPtr: 994 case Instruction::PtrToInt: 995 case Instruction::Load: 996 case Instruction::Mul: 997 case Instruction::Ret: 998 case Instruction::SDiv: 999 case Instruction::Store: 1000 case Instruction::Sub: 1001 case Instruction::UDiv: 1002 case Instruction::ZExt: 1003 break; 1004 } 1005 } 1006 } 1007 1008 return true; 1009 } 1010 1011 bool 1012 IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result, 1013 const lldb_private::ConstString &result_name, 1014 lldb_private::TypeFromParser result_type, 1015 Function &llvm_function, 1016 Module &llvm_module, 1017 lldb_private::Error &err) 1018 { 1019 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1020 1021 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo(); 1022 1023 if (!target_info.IsValid()) 1024 { 1025 err.SetErrorToGenericError(); 1026 err.SetErrorString(interpreter_initialization_error); 1027 return false; 1028 } 1029 1030 lldb::addr_t alloc_min; 1031 lldb::addr_t alloc_max; 1032 1033 switch (target_info.address_byte_size) 1034 { 1035 default: 1036 err.SetErrorToGenericError(); 1037 err.SetErrorString(interpreter_initialization_error); 1038 return false; 1039 case 4: 1040 alloc_min = 0x00001000llu; 1041 alloc_max = 0x0000ffffllu; 1042 break; 1043 case 8: 1044 alloc_min = 0x0000000000001000llu; 1045 alloc_max = 0x000000000000ffffllu; 1046 break; 1047 } 1048 1049 DataLayout target_data(&llvm_module); 1050 if (target_data.getPointerSize(0) != target_info.address_byte_size) 1051 { 1052 err.SetErrorToGenericError(); 1053 err.SetErrorString(interpreter_initialization_error); 1054 return false; 1055 } 1056 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle)) 1057 { 1058 err.SetErrorToGenericError(); 1059 err.SetErrorString(interpreter_initialization_error); 1060 return false; 1061 } 1062 1063 Memory memory(target_data, m_decl_map, alloc_min, alloc_max); 1064 InterpreterStackFrame frame(target_data, memory, m_decl_map); 1065 1066 uint32_t num_insts = 0; 1067 1068 frame.Jump(llvm_function.begin()); 1069 1070 while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) 1071 { 1072 const Instruction *inst = frame.m_ii; 1073 1074 if (log) 1075 log->Printf("Interpreting %s", PrintValue(inst).c_str()); 1076 1077 switch (inst->getOpcode()) 1078 { 1079 default: 1080 break; 1081 case Instruction::Add: 1082 case Instruction::Sub: 1083 case Instruction::Mul: 1084 case Instruction::SDiv: 1085 case Instruction::UDiv: 1086 { 1087 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst); 1088 1089 if (!bin_op) 1090 { 1091 if (log) 1092 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName()); 1093 err.SetErrorToGenericError(); 1094 err.SetErrorString(interpreter_internal_error); 1095 return false; 1096 } 1097 1098 Value *lhs = inst->getOperand(0); 1099 Value *rhs = inst->getOperand(1); 1100 1101 lldb_private::Scalar L; 1102 lldb_private::Scalar R; 1103 1104 if (!frame.EvaluateValue(L, lhs, llvm_module)) 1105 { 1106 if (log) 1107 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1108 err.SetErrorToGenericError(); 1109 err.SetErrorString(bad_value_error); 1110 return false; 1111 } 1112 1113 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1114 { 1115 if (log) 1116 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1117 err.SetErrorToGenericError(); 1118 err.SetErrorString(bad_value_error); 1119 return false; 1120 } 1121 1122 lldb_private::Scalar result; 1123 1124 switch (inst->getOpcode()) 1125 { 1126 default: 1127 break; 1128 case Instruction::Add: 1129 result = L + R; 1130 break; 1131 case Instruction::Mul: 1132 result = L * R; 1133 break; 1134 case Instruction::Sub: 1135 result = L - R; 1136 break; 1137 case Instruction::SDiv: 1138 result = L / R; 1139 break; 1140 case Instruction::UDiv: 1141 result = L.GetRawBits64(0) / R.GetRawBits64(1); 1142 break; 1143 } 1144 1145 frame.AssignValue(inst, result, llvm_module); 1146 1147 if (log) 1148 { 1149 log->Printf("Interpreted a %s", inst->getOpcodeName()); 1150 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1151 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1152 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1153 } 1154 } 1155 break; 1156 case Instruction::Alloca: 1157 { 1158 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst); 1159 1160 if (!alloca_inst) 1161 { 1162 if (log) 1163 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst"); 1164 err.SetErrorToGenericError(); 1165 err.SetErrorString(interpreter_internal_error); 1166 return false; 1167 } 1168 1169 if (alloca_inst->isArrayAllocation()) 1170 { 1171 if (log) 1172 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true"); 1173 err.SetErrorToGenericError(); 1174 err.SetErrorString(unsupported_opcode_error); 1175 return false; 1176 } 1177 1178 // The semantics of Alloca are: 1179 // Create a region R of virtual memory of type T, backed by a data buffer 1180 // Create a region P of virtual memory of type T*, backed by a data buffer 1181 // Write the virtual address of R into P 1182 1183 Type *T = alloca_inst->getAllocatedType(); 1184 Type *Tptr = alloca_inst->getType(); 1185 1186 Memory::Region R = memory.Malloc(T); 1187 1188 if (R.IsInvalid()) 1189 { 1190 if (log) 1191 log->Printf("Couldn't allocate memory for an AllocaInst"); 1192 err.SetErrorToGenericError(); 1193 err.SetErrorString(memory_allocation_error); 1194 return false; 1195 } 1196 1197 Memory::Region P = memory.Malloc(Tptr); 1198 1199 if (P.IsInvalid()) 1200 { 1201 if (log) 1202 log->Printf("Couldn't allocate the result pointer for an AllocaInst"); 1203 err.SetErrorToGenericError(); 1204 err.SetErrorString(memory_allocation_error); 1205 return false; 1206 } 1207 1208 DataEncoderSP P_encoder = memory.GetEncoder(P); 1209 1210 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX) 1211 { 1212 if (log) 1213 log->Printf("Couldn't write the result pointer for an AllocaInst"); 1214 err.SetErrorToGenericError(); 1215 err.SetErrorString(memory_write_error); 1216 return false; 1217 } 1218 1219 frame.m_values[alloca_inst] = P; 1220 1221 if (log) 1222 { 1223 log->Printf("Interpreted an AllocaInst"); 1224 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1225 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str()); 1226 } 1227 } 1228 break; 1229 case Instruction::BitCast: 1230 case Instruction::ZExt: 1231 { 1232 const CastInst *cast_inst = dyn_cast<CastInst>(inst); 1233 1234 if (!cast_inst) 1235 { 1236 if (log) 1237 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName()); 1238 err.SetErrorToGenericError(); 1239 err.SetErrorString(interpreter_internal_error); 1240 return false; 1241 } 1242 1243 Value *source = cast_inst->getOperand(0); 1244 1245 lldb_private::Scalar S; 1246 1247 if (!frame.EvaluateValue(S, source, llvm_module)) 1248 { 1249 if (log) 1250 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); 1251 err.SetErrorToGenericError(); 1252 err.SetErrorString(bad_value_error); 1253 return false; 1254 } 1255 1256 frame.AssignValue(inst, S, llvm_module); 1257 } 1258 break; 1259 case Instruction::Br: 1260 { 1261 const BranchInst *br_inst = dyn_cast<BranchInst>(inst); 1262 1263 if (!br_inst) 1264 { 1265 if (log) 1266 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst"); 1267 err.SetErrorToGenericError(); 1268 err.SetErrorString(interpreter_internal_error); 1269 return false; 1270 } 1271 1272 if (br_inst->isConditional()) 1273 { 1274 Value *condition = br_inst->getCondition(); 1275 1276 lldb_private::Scalar C; 1277 1278 if (!frame.EvaluateValue(C, condition, llvm_module)) 1279 { 1280 if (log) 1281 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str()); 1282 err.SetErrorToGenericError(); 1283 err.SetErrorString(bad_value_error); 1284 return false; 1285 } 1286 1287 if (C.GetRawBits64(0)) 1288 frame.Jump(br_inst->getSuccessor(0)); 1289 else 1290 frame.Jump(br_inst->getSuccessor(1)); 1291 1292 if (log) 1293 { 1294 log->Printf("Interpreted a BrInst with a condition"); 1295 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str()); 1296 } 1297 } 1298 else 1299 { 1300 frame.Jump(br_inst->getSuccessor(0)); 1301 1302 if (log) 1303 { 1304 log->Printf("Interpreted a BrInst with no condition"); 1305 } 1306 } 1307 } 1308 continue; 1309 case Instruction::GetElementPtr: 1310 { 1311 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst); 1312 1313 if (!gep_inst) 1314 { 1315 if (log) 1316 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst"); 1317 err.SetErrorToGenericError(); 1318 err.SetErrorString(interpreter_internal_error); 1319 return false; 1320 } 1321 1322 const Value *pointer_operand = gep_inst->getPointerOperand(); 1323 Type *pointer_type = pointer_operand->getType(); 1324 1325 lldb_private::Scalar P; 1326 1327 if (!frame.EvaluateValue(P, pointer_operand, llvm_module)) 1328 { 1329 if (log) 1330 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str()); 1331 err.SetErrorToGenericError(); 1332 err.SetErrorString(bad_value_error); 1333 return false; 1334 } 1335 1336 typedef SmallVector <Value *, 8> IndexVector; 1337 typedef IndexVector::iterator IndexIterator; 1338 1339 SmallVector <Value *, 8> indices (gep_inst->idx_begin(), 1340 gep_inst->idx_end()); 1341 1342 SmallVector <Value *, 8> const_indices; 1343 1344 for (IndexIterator ii = indices.begin(), ie = indices.end(); 1345 ii != ie; 1346 ++ii) 1347 { 1348 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii); 1349 1350 if (!constant_index) 1351 { 1352 lldb_private::Scalar I; 1353 1354 if (!frame.EvaluateValue(I, *ii, llvm_module)) 1355 { 1356 if (log) 1357 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str()); 1358 err.SetErrorToGenericError(); 1359 err.SetErrorString(bad_value_error); 1360 return false; 1361 } 1362 1363 if (log) 1364 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS)); 1365 1366 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS))); 1367 } 1368 1369 const_indices.push_back(constant_index); 1370 } 1371 1372 uint64_t offset = target_data.getIndexedOffset(pointer_type, const_indices); 1373 1374 lldb_private::Scalar Poffset = P + offset; 1375 1376 frame.AssignValue(inst, Poffset, llvm_module); 1377 1378 if (log) 1379 { 1380 log->Printf("Interpreted a GetElementPtrInst"); 1381 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1382 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str()); 1383 } 1384 } 1385 break; 1386 case Instruction::ICmp: 1387 { 1388 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst); 1389 1390 if (!icmp_inst) 1391 { 1392 if (log) 1393 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst"); 1394 err.SetErrorToGenericError(); 1395 err.SetErrorString(interpreter_internal_error); 1396 return false; 1397 } 1398 1399 CmpInst::Predicate predicate = icmp_inst->getPredicate(); 1400 1401 Value *lhs = inst->getOperand(0); 1402 Value *rhs = inst->getOperand(1); 1403 1404 lldb_private::Scalar L; 1405 lldb_private::Scalar R; 1406 1407 if (!frame.EvaluateValue(L, lhs, llvm_module)) 1408 { 1409 if (log) 1410 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1411 err.SetErrorToGenericError(); 1412 err.SetErrorString(bad_value_error); 1413 return false; 1414 } 1415 1416 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1417 { 1418 if (log) 1419 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1420 err.SetErrorToGenericError(); 1421 err.SetErrorString(bad_value_error); 1422 return false; 1423 } 1424 1425 lldb_private::Scalar result; 1426 1427 switch (predicate) 1428 { 1429 default: 1430 return false; 1431 case CmpInst::ICMP_EQ: 1432 result = (L == R); 1433 break; 1434 case CmpInst::ICMP_NE: 1435 result = (L != R); 1436 break; 1437 case CmpInst::ICMP_UGT: 1438 result = (L.GetRawBits64(0) > R.GetRawBits64(0)); 1439 break; 1440 case CmpInst::ICMP_UGE: 1441 result = (L.GetRawBits64(0) >= R.GetRawBits64(0)); 1442 break; 1443 case CmpInst::ICMP_ULT: 1444 result = (L.GetRawBits64(0) < R.GetRawBits64(0)); 1445 break; 1446 case CmpInst::ICMP_ULE: 1447 result = (L.GetRawBits64(0) <= R.GetRawBits64(0)); 1448 break; 1449 case CmpInst::ICMP_SGT: 1450 result = (L > R); 1451 break; 1452 case CmpInst::ICMP_SGE: 1453 result = (L >= R); 1454 break; 1455 case CmpInst::ICMP_SLT: 1456 result = (L < R); 1457 break; 1458 case CmpInst::ICMP_SLE: 1459 result = (L <= R); 1460 break; 1461 } 1462 1463 frame.AssignValue(inst, result, llvm_module); 1464 1465 if (log) 1466 { 1467 log->Printf("Interpreted an ICmpInst"); 1468 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1469 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1470 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1471 } 1472 } 1473 break; 1474 case Instruction::IntToPtr: 1475 { 1476 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst); 1477 1478 if (!int_to_ptr_inst) 1479 { 1480 if (log) 1481 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst"); 1482 err.SetErrorToGenericError(); 1483 err.SetErrorString(interpreter_internal_error); 1484 return false; 1485 } 1486 1487 Value *src_operand = int_to_ptr_inst->getOperand(0); 1488 1489 lldb_private::Scalar I; 1490 1491 if (!frame.EvaluateValue(I, src_operand, llvm_module)) 1492 { 1493 if (log) 1494 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1495 err.SetErrorToGenericError(); 1496 err.SetErrorString(bad_value_error); 1497 return false; 1498 } 1499 1500 frame.AssignValue(inst, I, llvm_module); 1501 1502 if (log) 1503 { 1504 log->Printf("Interpreted an IntToPtr"); 1505 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1506 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1507 } 1508 } 1509 break; 1510 case Instruction::PtrToInt: 1511 { 1512 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst); 1513 1514 if (!ptr_to_int_inst) 1515 { 1516 if (log) 1517 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst"); 1518 err.SetErrorToGenericError(); 1519 err.SetErrorString(interpreter_internal_error); 1520 return false; 1521 } 1522 1523 Value *src_operand = ptr_to_int_inst->getOperand(0); 1524 1525 lldb_private::Scalar I; 1526 1527 if (!frame.EvaluateValue(I, src_operand, llvm_module)) 1528 { 1529 if (log) 1530 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1531 err.SetErrorToGenericError(); 1532 err.SetErrorString(bad_value_error); 1533 return false; 1534 } 1535 1536 frame.AssignValue(inst, I, llvm_module); 1537 1538 if (log) 1539 { 1540 log->Printf("Interpreted a PtrToInt"); 1541 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1542 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1543 } 1544 } 1545 break; 1546 case Instruction::Load: 1547 { 1548 const LoadInst *load_inst = dyn_cast<LoadInst>(inst); 1549 1550 if (!load_inst) 1551 { 1552 if (log) 1553 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst"); 1554 err.SetErrorToGenericError(); 1555 err.SetErrorString(interpreter_internal_error); 1556 return false; 1557 } 1558 1559 // The semantics of Load are: 1560 // Create a region D that will contain the loaded data 1561 // Resolve the region P containing a pointer 1562 // Dereference P to get the region R that the data should be loaded from 1563 // Transfer a unit of type type(D) from R to D 1564 1565 const Value *pointer_operand = load_inst->getPointerOperand(); 1566 1567 Type *pointer_ty = pointer_operand->getType(); 1568 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1569 if (!pointer_ptr_ty) 1570 { 1571 if (log) 1572 log->Printf("getPointerOperand()->getType() is not a PointerType"); 1573 err.SetErrorToGenericError(); 1574 err.SetErrorString(interpreter_internal_error); 1575 return false; 1576 } 1577 Type *target_ty = pointer_ptr_ty->getElementType(); 1578 1579 Memory::Region D = frame.ResolveValue(load_inst, llvm_module); 1580 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1581 1582 if (D.IsInvalid()) 1583 { 1584 if (log) 1585 log->Printf("LoadInst's value doesn't resolve to anything"); 1586 err.SetErrorToGenericError(); 1587 err.SetErrorString(bad_value_error); 1588 return false; 1589 } 1590 1591 if (P.IsInvalid()) 1592 { 1593 if (log) 1594 log->Printf("LoadInst's pointer doesn't resolve to anything"); 1595 err.SetErrorToGenericError(); 1596 err.SetErrorString(bad_value_error); 1597 return false; 1598 } 1599 1600 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1601 DataEncoderSP D_encoder(memory.GetEncoder(D)); 1602 1603 uint32_t offset = 0; 1604 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1605 1606 Memory::Region R = memory.Lookup(pointer, target_ty); 1607 1608 if (R.IsValid()) 1609 { 1610 if (!memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty))) 1611 { 1612 if (log) 1613 log->Printf("Couldn't read from a region on behalf of a LoadInst"); 1614 err.SetErrorToGenericError(); 1615 err.SetErrorString(memory_read_error); 1616 return false; 1617 } 1618 } 1619 else 1620 { 1621 if (!memory.ReadFromRawPtr(D_encoder->GetDataStart(), pointer, target_data.getTypeStoreSize(target_ty))) 1622 { 1623 if (log) 1624 log->Printf("Couldn't read from a raw pointer on behalf of a LoadInst"); 1625 err.SetErrorToGenericError(); 1626 err.SetErrorString(memory_read_error); 1627 return false; 1628 } 1629 } 1630 1631 if (log) 1632 { 1633 log->Printf("Interpreted a LoadInst"); 1634 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1635 if (R.IsValid()) 1636 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1637 else 1638 log->Printf(" R : raw pointer 0x%llx", (unsigned long long)pointer); 1639 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str()); 1640 } 1641 } 1642 break; 1643 case Instruction::Ret: 1644 { 1645 if (result_name.IsEmpty()) 1646 return true; 1647 1648 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString()); 1649 1650 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module)) 1651 { 1652 if (log) 1653 log->Printf("Couldn't construct the expression's result"); 1654 err.SetErrorToGenericError(); 1655 err.SetErrorString(bad_result_error); 1656 return false; 1657 } 1658 1659 return true; 1660 } 1661 case Instruction::Store: 1662 { 1663 const StoreInst *store_inst = dyn_cast<StoreInst>(inst); 1664 1665 if (!store_inst) 1666 { 1667 if (log) 1668 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst"); 1669 err.SetErrorToGenericError(); 1670 err.SetErrorString(interpreter_internal_error); 1671 return false; 1672 } 1673 1674 // The semantics of Store are: 1675 // Resolve the region D containing the data to be stored 1676 // Resolve the region P containing a pointer 1677 // Dereference P to get the region R that the data should be stored in 1678 // Transfer a unit of type type(D) from D to R 1679 1680 const Value *value_operand = store_inst->getValueOperand(); 1681 const Value *pointer_operand = store_inst->getPointerOperand(); 1682 1683 Type *pointer_ty = pointer_operand->getType(); 1684 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1685 if (!pointer_ptr_ty) 1686 return false; 1687 Type *target_ty = pointer_ptr_ty->getElementType(); 1688 1689 Memory::Region D = frame.ResolveValue(value_operand, llvm_module); 1690 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1691 1692 if (D.IsInvalid()) 1693 { 1694 if (log) 1695 log->Printf("StoreInst's value doesn't resolve to anything"); 1696 err.SetErrorToGenericError(); 1697 err.SetErrorString(bad_value_error); 1698 return false; 1699 } 1700 1701 if (P.IsInvalid()) 1702 { 1703 if (log) 1704 log->Printf("StoreInst's pointer doesn't resolve to anything"); 1705 err.SetErrorToGenericError(); 1706 err.SetErrorString(bad_value_error); 1707 return false; 1708 } 1709 1710 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1711 DataExtractorSP D_extractor(memory.GetExtractor(D)); 1712 1713 if (!P_extractor || !D_extractor) 1714 return false; 1715 1716 uint32_t offset = 0; 1717 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1718 1719 Memory::Region R = memory.Lookup(pointer, target_ty); 1720 1721 if (R.IsValid()) 1722 { 1723 if (!memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty))) 1724 { 1725 if (log) 1726 log->Printf("Couldn't write to a region on behalf of a LoadInst"); 1727 err.SetErrorToGenericError(); 1728 err.SetErrorString(memory_write_error); 1729 return false; 1730 } 1731 } 1732 else 1733 { 1734 if (!memory.WriteToRawPtr(pointer, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty))) 1735 { 1736 if (log) 1737 log->Printf("Couldn't write to a raw pointer on behalf of a LoadInst"); 1738 err.SetErrorToGenericError(); 1739 err.SetErrorString(memory_write_error); 1740 return false; 1741 } 1742 } 1743 1744 1745 if (log) 1746 { 1747 log->Printf("Interpreted a StoreInst"); 1748 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str()); 1749 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1750 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1751 } 1752 } 1753 break; 1754 } 1755 1756 ++frame.m_ii; 1757 } 1758 1759 if (num_insts >= 4096) 1760 { 1761 err.SetErrorToGenericError(); 1762 err.SetErrorString(infinite_loop_error); 1763 return false; 1764 } 1765 1766 return false; 1767 } 1768