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