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