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/IRForTarget.h" 15 #include "lldb/Expression/IRInterpreter.h" 16 17 #include "llvm/Constants.h" 18 #include "llvm/Function.h" 19 #include "llvm/Instructions.h" 20 #include "llvm/Module.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include "llvm/Target/TargetData.h" 23 24 #include <map> 25 26 using namespace llvm; 27 28 IRInterpreter::IRInterpreter(lldb_private::ClangExpressionDeclMap &decl_map, 29 lldb_private::Stream *error_stream) : 30 m_decl_map(decl_map), 31 m_error_stream(error_stream) 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 SHARED_PTR(lldb_private::DataEncoder) DataEncoderSP; 73 typedef 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 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 m_allocation != NULL; 162 } 163 164 bool IsInvalid () 165 { 166 return m_allocation == NULL; 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 TargetData &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 (TargetData &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()); 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("%llx [%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 TargetData &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 (TargetData &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()); 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::BitCast: 561 return ResolveConstantValue(value, constant_expr->getOperand(0)); 562 case Instruction::GetElementPtr: 563 { 564 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin(); 565 ConstantExpr::const_op_iterator op_end = constant_expr->op_end(); 566 567 Constant *base = dyn_cast<Constant>(*op_cursor); 568 569 if (!base) 570 return false; 571 572 if (!ResolveConstantValue(value, base)) 573 return false; 574 575 op_cursor++; 576 577 if (op_cursor == op_end) 578 return true; // no offset to apply! 579 580 SmallVector <Value *, 8> indices (op_cursor, op_end); 581 582 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices); 583 584 const bool is_signed = true; 585 value += APInt(value.getBitWidth(), offset, is_signed); 586 587 return true; 588 } 589 } 590 } 591 592 return false; 593 } 594 595 bool ResolveConstant (Memory::Region ®ion, const Constant *constant) 596 { 597 APInt resolved_value; 598 599 if (!ResolveConstantValue(resolved_value, constant)) 600 return false; 601 602 const uint64_t *raw_data = resolved_value.getRawData(); 603 604 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType()); 605 return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size); 606 } 607 608 Memory::Region ResolveValue (const Value *value, Module &module) 609 { 610 ValueMap::iterator i = m_values.find(value); 611 612 if (i != m_values.end()) 613 return i->second; 614 615 const GlobalValue *global_value = dyn_cast<GlobalValue>(value); 616 617 // If the variable is indirected through the argument 618 // array then we need to build an extra level of indirection 619 // for it. This is the default; only magic arguments like 620 // "this", "self", and "_cmd" are direct. 621 bool indirect_variable = true; 622 623 // Attempt to resolve the value using the program's data. 624 // If it is, the values to be created are: 625 // 626 // data_region - a region of memory in which the variable's data resides. 627 // ref_region - a region of memory in which its address (i.e., &var) resides. 628 // In the JIT case, this region would be a member of the struct passed in. 629 // pointer_region - a region of memory in which the address of the pointer 630 // resides. This is an IR-level variable. 631 do 632 { 633 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 634 635 lldb_private::Value resolved_value; 636 637 if (global_value) 638 { 639 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module); 640 641 if (!decl) 642 break; 643 644 if (isa<clang::FunctionDecl>(decl)) 645 { 646 if (log) 647 log->Printf("The interpreter does not handle function pointers at the moment"); 648 649 return Memory::Region(); 650 } 651 652 resolved_value = m_decl_map.LookupDecl(decl); 653 } 654 else 655 { 656 // Special-case "this", "self", and "_cmd" 657 658 std::string name_str = value->getName().str(); 659 660 if (name_str == "this" || 661 name_str == "self" || 662 name_str == "_cmd") 663 resolved_value = m_decl_map.GetSpecialValue(lldb_private::ConstString(name_str.c_str())); 664 665 indirect_variable = false; 666 } 667 668 if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void) 669 { 670 if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo) 671 { 672 Memory::Region data_region = m_memory.Malloc(value->getType()); 673 data_region.m_allocation->m_origin = resolved_value; 674 Memory::Region ref_region = m_memory.Malloc(value->getType()); 675 Memory::Region pointer_region; 676 677 if (indirect_variable) 678 pointer_region = m_memory.Malloc(value->getType()); 679 680 if (!Cache(data_region.m_allocation, value->getType())) 681 return Memory::Region(); 682 683 if (ref_region.IsInvalid()) 684 return Memory::Region(); 685 686 if (pointer_region.IsInvalid() && indirect_variable) 687 return Memory::Region(); 688 689 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region); 690 691 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX) 692 return Memory::Region(); 693 694 if (log) 695 { 696 log->Printf("Made an allocation for register variable %s", PrintValue(value).c_str()); 697 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str()); 698 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base); 699 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base); 700 if (indirect_variable) 701 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base); 702 } 703 704 if (indirect_variable) 705 { 706 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region); 707 708 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX) 709 return Memory::Region(); 710 711 m_values[value] = pointer_region; 712 return pointer_region; 713 } 714 else 715 { 716 m_values[value] = ref_region; 717 return ref_region; 718 } 719 } 720 else 721 { 722 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value); 723 Memory::Region ref_region = m_memory.Malloc(value->getType()); 724 Memory::Region pointer_region; 725 726 if (indirect_variable) 727 pointer_region = m_memory.Malloc(value->getType()); 728 729 if (ref_region.IsInvalid()) 730 return Memory::Region(); 731 732 if (pointer_region.IsInvalid() && indirect_variable) 733 return Memory::Region(); 734 735 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region); 736 737 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX) 738 return Memory::Region(); 739 740 if (indirect_variable) 741 { 742 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region); 743 744 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX) 745 return Memory::Region(); 746 747 m_values[value] = pointer_region; 748 } 749 750 if (log) 751 { 752 log->Printf("Made an allocation for %s", PrintValue(value).c_str()); 753 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str()); 754 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base); 755 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base); 756 if (indirect_variable) 757 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base); 758 } 759 760 if (indirect_variable) 761 return pointer_region; 762 else 763 return ref_region; 764 } 765 } 766 } 767 while(0); 768 769 // Fall back and allocate space [allocation type Alloca] 770 771 Type *type = value->getType(); 772 773 lldb::ValueSP backing_value(new lldb_private::Value); 774 775 Memory::Region data_region = m_memory.Malloc(type); 776 data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes(); 777 data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 778 data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress); 779 780 const Constant *constant = dyn_cast<Constant>(value); 781 782 do 783 { 784 if (!constant) 785 break; 786 787 if (!ResolveConstant (data_region, constant)) 788 return Memory::Region(); 789 } 790 while(0); 791 792 m_values[value] = data_region; 793 return data_region; 794 } 795 796 bool ConstructResult (lldb::ClangExpressionVariableSP &result, 797 const GlobalValue *result_value, 798 const lldb_private::ConstString &result_name, 799 lldb_private::TypeFromParser result_type, 800 Module &module) 801 { 802 // The result_value resolves to P, a pointer to a region R containing the result data. 803 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process. 804 805 if (!result_value) 806 return true; // There was no slot for a result – the expression doesn't return one. 807 808 ValueMap::iterator i = m_values.find(result_value); 809 810 if (i == m_values.end()) 811 return false; // There was a slot for the result, but we didn't write into it. 812 813 Memory::Region P = i->second; 814 DataExtractorSP P_extractor = m_memory.GetExtractor(P); 815 816 if (!P_extractor) 817 return false; 818 819 Type *pointer_ty = result_value->getType(); 820 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 821 if (!pointer_ptr_ty) 822 return false; 823 Type *R_ty = pointer_ptr_ty->getElementType(); 824 825 uint32_t offset = 0; 826 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 827 828 Memory::Region R = m_memory.Lookup(pointer, R_ty); 829 830 if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress || 831 !R.m_allocation->m_data) 832 return false; 833 834 lldb_private::Value base; 835 836 bool transient = false; 837 bool maybe_make_load = false; 838 839 if (m_decl_map.ResultIsReference(result_name)) 840 { 841 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty); 842 if (!R_ptr_ty) 843 return false; 844 Type *R_final_ty = R_ptr_ty->getElementType(); 845 846 DataExtractorSP R_extractor = m_memory.GetExtractor(R); 847 848 if (!R_extractor) 849 return false; 850 851 offset = 0; 852 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset); 853 854 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty); 855 856 if (R_final.m_allocation) 857 { 858 if (R_final.m_allocation->m_data) 859 transient = true; // this is a stack allocation 860 861 base = R_final.m_allocation->m_origin; 862 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address); 863 } 864 else 865 { 866 // We got a bare pointer. We are going to treat it as a load address 867 // or a file address, letting decl_map make the choice based on whether 868 // or not a process exists. 869 870 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 871 base.SetValueType(lldb_private::Value::eValueTypeFileAddress); 872 base.GetScalar() = (unsigned long long)R_pointer; 873 maybe_make_load = true; 874 } 875 } 876 else 877 { 878 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 879 base.SetValueType(lldb_private::Value::eValueTypeHostAddress); 880 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address); 881 } 882 883 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient, maybe_make_load); 884 } 885 }; 886 887 bool 888 IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result, 889 const lldb_private::ConstString &result_name, 890 lldb_private::TypeFromParser result_type, 891 Function &llvm_function, 892 Module &llvm_module, 893 lldb_private::Error &err) 894 { 895 if (supportsFunction (llvm_function, err)) 896 return runOnFunction(result, 897 result_name, 898 result_type, 899 llvm_function, 900 llvm_module, 901 err); 902 else 903 return false; 904 } 905 906 static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes"; 907 static const char *interpreter_initialization_error = "Interpreter couldn't be initialized"; 908 static const char *interpreter_internal_error = "Interpreter encountered an internal error"; 909 static const char *bad_value_error = "Interpreter couldn't resolve a value during execution"; 910 static const char *memory_allocation_error = "Interpreter couldn't allocate memory"; 911 static const char *memory_write_error = "Interpreter couldn't write to memory"; 912 static const char *memory_read_error = "Interpreter couldn't read from memory"; 913 static const char *infinite_loop_error = "Interpreter ran for too many cycles"; 914 static const char *bad_result_error = "Result of expression is in bad memory"; 915 916 bool 917 IRInterpreter::supportsFunction (Function &llvm_function, 918 lldb_private::Error &err) 919 { 920 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 921 922 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end(); 923 bbi != bbe; 924 ++bbi) 925 { 926 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end(); 927 ii != ie; 928 ++ii) 929 { 930 switch (ii->getOpcode()) 931 { 932 default: 933 { 934 if (log) 935 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str()); 936 err.SetErrorToGenericError(); 937 err.SetErrorString(unsupported_opcode_error); 938 return false; 939 } 940 case Instruction::Add: 941 case Instruction::Alloca: 942 case Instruction::BitCast: 943 case Instruction::Br: 944 case Instruction::GetElementPtr: 945 break; 946 case Instruction::ICmp: 947 { 948 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii); 949 950 if (!icmp_inst) 951 { 952 err.SetErrorToGenericError(); 953 err.SetErrorString(interpreter_internal_error); 954 return false; 955 } 956 957 switch (icmp_inst->getPredicate()) 958 { 959 default: 960 { 961 if (log) 962 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str()); 963 964 err.SetErrorToGenericError(); 965 err.SetErrorString(unsupported_opcode_error); 966 return false; 967 } 968 case CmpInst::ICMP_EQ: 969 case CmpInst::ICMP_NE: 970 case CmpInst::ICMP_UGT: 971 case CmpInst::ICMP_UGE: 972 case CmpInst::ICMP_ULT: 973 case CmpInst::ICMP_ULE: 974 case CmpInst::ICMP_SGT: 975 case CmpInst::ICMP_SGE: 976 case CmpInst::ICMP_SLT: 977 case CmpInst::ICMP_SLE: 978 break; 979 } 980 } 981 break; 982 case Instruction::IntToPtr: 983 case Instruction::Load: 984 case Instruction::Mul: 985 case Instruction::Ret: 986 case Instruction::SDiv: 987 case Instruction::Store: 988 case Instruction::Sub: 989 case Instruction::UDiv: 990 break; 991 } 992 } 993 } 994 995 return true; 996 } 997 998 bool 999 IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result, 1000 const lldb_private::ConstString &result_name, 1001 lldb_private::TypeFromParser result_type, 1002 Function &llvm_function, 1003 Module &llvm_module, 1004 lldb_private::Error &err) 1005 { 1006 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1007 1008 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo(); 1009 1010 if (!target_info.IsValid()) 1011 { 1012 err.SetErrorToGenericError(); 1013 err.SetErrorString(interpreter_initialization_error); 1014 return false; 1015 } 1016 1017 lldb::addr_t alloc_min; 1018 lldb::addr_t alloc_max; 1019 1020 switch (target_info.address_byte_size) 1021 { 1022 default: 1023 err.SetErrorToGenericError(); 1024 err.SetErrorString(interpreter_initialization_error); 1025 return false; 1026 case 4: 1027 alloc_min = 0x00001000llu; 1028 alloc_max = 0x0000ffffllu; 1029 break; 1030 case 8: 1031 alloc_min = 0x0000000000001000llu; 1032 alloc_max = 0x000000000000ffffllu; 1033 break; 1034 } 1035 1036 TargetData target_data(&llvm_module); 1037 if (target_data.getPointerSize() != target_info.address_byte_size) 1038 { 1039 err.SetErrorToGenericError(); 1040 err.SetErrorString(interpreter_initialization_error); 1041 return false; 1042 } 1043 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle)) 1044 { 1045 err.SetErrorToGenericError(); 1046 err.SetErrorString(interpreter_initialization_error); 1047 return false; 1048 } 1049 1050 Memory memory(target_data, m_decl_map, alloc_min, alloc_max); 1051 InterpreterStackFrame frame(target_data, memory, m_decl_map); 1052 1053 uint32_t num_insts = 0; 1054 1055 frame.Jump(llvm_function.begin()); 1056 1057 while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) 1058 { 1059 const Instruction *inst = frame.m_ii; 1060 1061 if (log) 1062 log->Printf("Interpreting %s", PrintValue(inst).c_str()); 1063 1064 switch (inst->getOpcode()) 1065 { 1066 default: 1067 break; 1068 case Instruction::Add: 1069 case Instruction::Sub: 1070 case Instruction::Mul: 1071 case Instruction::SDiv: 1072 case Instruction::UDiv: 1073 { 1074 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst); 1075 1076 if (!bin_op) 1077 { 1078 if (log) 1079 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName()); 1080 err.SetErrorToGenericError(); 1081 err.SetErrorString(interpreter_internal_error); 1082 return false; 1083 } 1084 1085 Value *lhs = inst->getOperand(0); 1086 Value *rhs = inst->getOperand(1); 1087 1088 lldb_private::Scalar L; 1089 lldb_private::Scalar R; 1090 1091 if (!frame.EvaluateValue(L, lhs, llvm_module)) 1092 { 1093 if (log) 1094 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1095 err.SetErrorToGenericError(); 1096 err.SetErrorString(bad_value_error); 1097 return false; 1098 } 1099 1100 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1101 { 1102 if (log) 1103 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1104 err.SetErrorToGenericError(); 1105 err.SetErrorString(bad_value_error); 1106 return false; 1107 } 1108 1109 lldb_private::Scalar result; 1110 1111 switch (inst->getOpcode()) 1112 { 1113 default: 1114 break; 1115 case Instruction::Add: 1116 result = L + R; 1117 break; 1118 case Instruction::Mul: 1119 result = L * R; 1120 break; 1121 case Instruction::Sub: 1122 result = L - R; 1123 break; 1124 case Instruction::SDiv: 1125 result = L / R; 1126 break; 1127 case Instruction::UDiv: 1128 result = L.GetRawBits64(0) / R.GetRawBits64(1); 1129 break; 1130 } 1131 1132 frame.AssignValue(inst, result, llvm_module); 1133 1134 if (log) 1135 { 1136 log->Printf("Interpreted a %s", inst->getOpcodeName()); 1137 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1138 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1139 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1140 } 1141 } 1142 break; 1143 case Instruction::Alloca: 1144 { 1145 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst); 1146 1147 if (!alloca_inst) 1148 { 1149 if (log) 1150 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst"); 1151 err.SetErrorToGenericError(); 1152 err.SetErrorString(interpreter_internal_error); 1153 return false; 1154 } 1155 1156 if (alloca_inst->isArrayAllocation()) 1157 { 1158 if (log) 1159 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true"); 1160 err.SetErrorToGenericError(); 1161 err.SetErrorString(unsupported_opcode_error); 1162 return false; 1163 } 1164 1165 // The semantics of Alloca are: 1166 // Create a region R of virtual memory of type T, backed by a data buffer 1167 // Create a region P of virtual memory of type T*, backed by a data buffer 1168 // Write the virtual address of R into P 1169 1170 Type *T = alloca_inst->getAllocatedType(); 1171 Type *Tptr = alloca_inst->getType(); 1172 1173 Memory::Region R = memory.Malloc(T); 1174 1175 if (R.IsInvalid()) 1176 { 1177 if (log) 1178 log->Printf("Couldn't allocate memory for an AllocaInst"); 1179 err.SetErrorToGenericError(); 1180 err.SetErrorString(memory_allocation_error); 1181 return false; 1182 } 1183 1184 Memory::Region P = memory.Malloc(Tptr); 1185 1186 if (P.IsInvalid()) 1187 { 1188 if (log) 1189 log->Printf("Couldn't allocate the result pointer for an AllocaInst"); 1190 err.SetErrorToGenericError(); 1191 err.SetErrorString(memory_allocation_error); 1192 return false; 1193 } 1194 1195 DataEncoderSP P_encoder = memory.GetEncoder(P); 1196 1197 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX) 1198 { 1199 if (log) 1200 log->Printf("Couldn't write the result pointer for an AllocaInst"); 1201 err.SetErrorToGenericError(); 1202 err.SetErrorString(memory_write_error); 1203 return false; 1204 } 1205 1206 frame.m_values[alloca_inst] = P; 1207 1208 if (log) 1209 { 1210 log->Printf("Interpreted an AllocaInst"); 1211 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1212 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str()); 1213 } 1214 } 1215 break; 1216 case Instruction::BitCast: 1217 { 1218 const BitCastInst *bit_cast_inst = dyn_cast<BitCastInst>(inst); 1219 1220 if (!bit_cast_inst) 1221 { 1222 if (log) 1223 log->Printf("getOpcode() returns BitCast, but instruction is not a BitCastInst"); 1224 err.SetErrorToGenericError(); 1225 err.SetErrorString(interpreter_internal_error); 1226 return false; 1227 } 1228 1229 Value *source = bit_cast_inst->getOperand(0); 1230 1231 lldb_private::Scalar S; 1232 1233 if (!frame.EvaluateValue(S, source, llvm_module)) 1234 { 1235 if (log) 1236 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); 1237 err.SetErrorToGenericError(); 1238 err.SetErrorString(bad_value_error); 1239 return false; 1240 } 1241 1242 frame.AssignValue(inst, S, llvm_module); 1243 } 1244 break; 1245 case Instruction::Br: 1246 { 1247 const BranchInst *br_inst = dyn_cast<BranchInst>(inst); 1248 1249 if (!br_inst) 1250 { 1251 if (log) 1252 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst"); 1253 err.SetErrorToGenericError(); 1254 err.SetErrorString(interpreter_internal_error); 1255 return false; 1256 } 1257 1258 if (br_inst->isConditional()) 1259 { 1260 Value *condition = br_inst->getCondition(); 1261 1262 lldb_private::Scalar C; 1263 1264 if (!frame.EvaluateValue(C, condition, llvm_module)) 1265 { 1266 if (log) 1267 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str()); 1268 err.SetErrorToGenericError(); 1269 err.SetErrorString(bad_value_error); 1270 return false; 1271 } 1272 1273 if (C.GetRawBits64(0)) 1274 frame.Jump(br_inst->getSuccessor(0)); 1275 else 1276 frame.Jump(br_inst->getSuccessor(1)); 1277 1278 if (log) 1279 { 1280 log->Printf("Interpreted a BrInst with a condition"); 1281 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str()); 1282 } 1283 } 1284 else 1285 { 1286 frame.Jump(br_inst->getSuccessor(0)); 1287 1288 if (log) 1289 { 1290 log->Printf("Interpreted a BrInst with no condition"); 1291 } 1292 } 1293 } 1294 continue; 1295 case Instruction::GetElementPtr: 1296 { 1297 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst); 1298 1299 if (!gep_inst) 1300 { 1301 if (log) 1302 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst"); 1303 err.SetErrorToGenericError(); 1304 err.SetErrorString(interpreter_internal_error); 1305 return false; 1306 } 1307 1308 const Value *pointer_operand = gep_inst->getPointerOperand(); 1309 Type *pointer_type = pointer_operand->getType(); 1310 1311 lldb_private::Scalar P; 1312 1313 if (!frame.EvaluateValue(P, pointer_operand, llvm_module)) 1314 { 1315 if (log) 1316 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str()); 1317 err.SetErrorToGenericError(); 1318 err.SetErrorString(bad_value_error); 1319 return false; 1320 } 1321 1322 SmallVector <Value *, 8> indices (gep_inst->idx_begin(), 1323 gep_inst->idx_end()); 1324 1325 uint64_t offset = target_data.getIndexedOffset(pointer_type, indices); 1326 1327 lldb_private::Scalar Poffset = P + offset; 1328 1329 frame.AssignValue(inst, Poffset, llvm_module); 1330 1331 if (log) 1332 { 1333 log->Printf("Interpreted a GetElementPtrInst"); 1334 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1335 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str()); 1336 } 1337 } 1338 break; 1339 case Instruction::ICmp: 1340 { 1341 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst); 1342 1343 if (!icmp_inst) 1344 { 1345 if (log) 1346 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst"); 1347 err.SetErrorToGenericError(); 1348 err.SetErrorString(interpreter_internal_error); 1349 return false; 1350 } 1351 1352 CmpInst::Predicate predicate = icmp_inst->getPredicate(); 1353 1354 Value *lhs = inst->getOperand(0); 1355 Value *rhs = inst->getOperand(1); 1356 1357 lldb_private::Scalar L; 1358 lldb_private::Scalar R; 1359 1360 if (!frame.EvaluateValue(L, lhs, llvm_module)) 1361 { 1362 if (log) 1363 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1364 err.SetErrorToGenericError(); 1365 err.SetErrorString(bad_value_error); 1366 return false; 1367 } 1368 1369 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1370 { 1371 if (log) 1372 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1373 err.SetErrorToGenericError(); 1374 err.SetErrorString(bad_value_error); 1375 return false; 1376 } 1377 1378 lldb_private::Scalar result; 1379 1380 switch (predicate) 1381 { 1382 default: 1383 return false; 1384 case CmpInst::ICMP_EQ: 1385 result = (L == R); 1386 break; 1387 case CmpInst::ICMP_NE: 1388 result = (L != R); 1389 break; 1390 case CmpInst::ICMP_UGT: 1391 result = (L.GetRawBits64(0) > R.GetRawBits64(0)); 1392 break; 1393 case CmpInst::ICMP_UGE: 1394 result = (L.GetRawBits64(0) >= R.GetRawBits64(0)); 1395 break; 1396 case CmpInst::ICMP_ULT: 1397 result = (L.GetRawBits64(0) < R.GetRawBits64(0)); 1398 break; 1399 case CmpInst::ICMP_ULE: 1400 result = (L.GetRawBits64(0) <= R.GetRawBits64(0)); 1401 break; 1402 case CmpInst::ICMP_SGT: 1403 result = (L > R); 1404 break; 1405 case CmpInst::ICMP_SGE: 1406 result = (L >= R); 1407 break; 1408 case CmpInst::ICMP_SLT: 1409 result = (L < R); 1410 break; 1411 case CmpInst::ICMP_SLE: 1412 result = (L <= R); 1413 break; 1414 } 1415 1416 frame.AssignValue(inst, result, llvm_module); 1417 1418 if (log) 1419 { 1420 log->Printf("Interpreted an ICmpInst"); 1421 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1422 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1423 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1424 } 1425 } 1426 break; 1427 case Instruction::IntToPtr: 1428 { 1429 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst); 1430 1431 if (!int_to_ptr_inst) 1432 { 1433 if (log) 1434 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst"); 1435 err.SetErrorToGenericError(); 1436 err.SetErrorString(interpreter_internal_error); 1437 return false; 1438 } 1439 1440 Value *src_operand = int_to_ptr_inst->getOperand(0); 1441 1442 lldb_private::Scalar I; 1443 1444 if (!frame.EvaluateValue(I, src_operand, llvm_module)) 1445 { 1446 if (log) 1447 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1448 err.SetErrorToGenericError(); 1449 err.SetErrorString(bad_value_error); 1450 return false; 1451 } 1452 1453 frame.AssignValue(inst, I, llvm_module); 1454 1455 if (log) 1456 { 1457 log->Printf("Interpreted an IntToPtr"); 1458 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1459 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1460 } 1461 } 1462 break; 1463 case Instruction::Load: 1464 { 1465 const LoadInst *load_inst = dyn_cast<LoadInst>(inst); 1466 1467 if (!load_inst) 1468 { 1469 if (log) 1470 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst"); 1471 err.SetErrorToGenericError(); 1472 err.SetErrorString(interpreter_internal_error); 1473 return false; 1474 } 1475 1476 // The semantics of Load are: 1477 // Create a region D that will contain the loaded data 1478 // Resolve the region P containing a pointer 1479 // Dereference P to get the region R that the data should be loaded from 1480 // Transfer a unit of type type(D) from R to D 1481 1482 const Value *pointer_operand = load_inst->getPointerOperand(); 1483 1484 Type *pointer_ty = pointer_operand->getType(); 1485 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1486 if (!pointer_ptr_ty) 1487 { 1488 if (log) 1489 log->Printf("getPointerOperand()->getType() is not a PointerType"); 1490 err.SetErrorToGenericError(); 1491 err.SetErrorString(interpreter_internal_error); 1492 return false; 1493 } 1494 Type *target_ty = pointer_ptr_ty->getElementType(); 1495 1496 Memory::Region D = frame.ResolveValue(load_inst, llvm_module); 1497 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1498 1499 if (D.IsInvalid()) 1500 { 1501 if (log) 1502 log->Printf("LoadInst's value doesn't resolve to anything"); 1503 err.SetErrorToGenericError(); 1504 err.SetErrorString(bad_value_error); 1505 return false; 1506 } 1507 1508 if (P.IsInvalid()) 1509 { 1510 if (log) 1511 log->Printf("LoadInst's pointer doesn't resolve to anything"); 1512 err.SetErrorToGenericError(); 1513 err.SetErrorString(bad_value_error); 1514 return false; 1515 } 1516 1517 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1518 DataEncoderSP D_encoder(memory.GetEncoder(D)); 1519 1520 uint32_t offset = 0; 1521 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1522 1523 Memory::Region R = memory.Lookup(pointer, target_ty); 1524 1525 if (R.IsValid()) 1526 { 1527 if (!memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty))) 1528 { 1529 if (log) 1530 log->Printf("Couldn't read from a region on behalf of a LoadInst"); 1531 err.SetErrorToGenericError(); 1532 err.SetErrorString(memory_read_error); 1533 return false; 1534 } 1535 } 1536 else 1537 { 1538 if (!memory.ReadFromRawPtr(D_encoder->GetDataStart(), pointer, target_data.getTypeStoreSize(target_ty))) 1539 { 1540 if (log) 1541 log->Printf("Couldn't read from a raw pointer on behalf of a LoadInst"); 1542 err.SetErrorToGenericError(); 1543 err.SetErrorString(memory_read_error); 1544 return false; 1545 } 1546 } 1547 1548 if (log) 1549 { 1550 log->Printf("Interpreted a LoadInst"); 1551 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1552 if (R.IsValid()) 1553 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1554 else 1555 log->Printf(" R : raw pointer 0x%llx", (unsigned long long)pointer); 1556 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str()); 1557 } 1558 } 1559 break; 1560 case Instruction::Ret: 1561 { 1562 if (result_name.IsEmpty()) 1563 return true; 1564 1565 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString()); 1566 1567 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module)) 1568 { 1569 if (log) 1570 log->Printf("Couldn't construct the expression's result"); 1571 err.SetErrorToGenericError(); 1572 err.SetErrorString(bad_result_error); 1573 return false; 1574 } 1575 1576 return true; 1577 } 1578 case Instruction::Store: 1579 { 1580 const StoreInst *store_inst = dyn_cast<StoreInst>(inst); 1581 1582 if (!store_inst) 1583 { 1584 if (log) 1585 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst"); 1586 err.SetErrorToGenericError(); 1587 err.SetErrorString(interpreter_internal_error); 1588 return false; 1589 } 1590 1591 // The semantics of Store are: 1592 // Resolve the region D containing the data to be stored 1593 // Resolve the region P containing a pointer 1594 // Dereference P to get the region R that the data should be stored in 1595 // Transfer a unit of type type(D) from D to R 1596 1597 const Value *value_operand = store_inst->getValueOperand(); 1598 const Value *pointer_operand = store_inst->getPointerOperand(); 1599 1600 Type *pointer_ty = pointer_operand->getType(); 1601 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1602 if (!pointer_ptr_ty) 1603 return false; 1604 Type *target_ty = pointer_ptr_ty->getElementType(); 1605 1606 Memory::Region D = frame.ResolveValue(value_operand, llvm_module); 1607 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1608 1609 if (D.IsInvalid()) 1610 { 1611 if (log) 1612 log->Printf("StoreInst's value doesn't resolve to anything"); 1613 err.SetErrorToGenericError(); 1614 err.SetErrorString(bad_value_error); 1615 return false; 1616 } 1617 1618 if (P.IsInvalid()) 1619 { 1620 if (log) 1621 log->Printf("StoreInst's pointer doesn't resolve to anything"); 1622 err.SetErrorToGenericError(); 1623 err.SetErrorString(bad_value_error); 1624 return false; 1625 } 1626 1627 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1628 DataExtractorSP D_extractor(memory.GetExtractor(D)); 1629 1630 if (!P_extractor || !D_extractor) 1631 return false; 1632 1633 uint32_t offset = 0; 1634 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1635 1636 Memory::Region R = memory.Lookup(pointer, target_ty); 1637 1638 if (R.IsValid()) 1639 { 1640 if (!memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty))) 1641 { 1642 if (log) 1643 log->Printf("Couldn't write to a region on behalf of a LoadInst"); 1644 err.SetErrorToGenericError(); 1645 err.SetErrorString(memory_write_error); 1646 return false; 1647 } 1648 } 1649 else 1650 { 1651 if (!memory.WriteToRawPtr(pointer, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty))) 1652 { 1653 if (log) 1654 log->Printf("Couldn't write to a raw pointer on behalf of a LoadInst"); 1655 err.SetErrorToGenericError(); 1656 err.SetErrorString(memory_write_error); 1657 return false; 1658 } 1659 } 1660 1661 1662 if (log) 1663 { 1664 log->Printf("Interpreted a StoreInst"); 1665 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str()); 1666 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1667 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1668 } 1669 } 1670 break; 1671 } 1672 1673 ++frame.m_ii; 1674 } 1675 1676 if (num_insts >= 4096) 1677 { 1678 err.SetErrorToGenericError(); 1679 err.SetErrorString(infinite_loop_error); 1680 return false; 1681 } 1682 1683 return false; 1684 } 1685