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 lldb::SharedPtr <lldb_private::DataEncoder>::Type DataEncoderSP; 73 typedef lldb::SharedPtr <lldb_private::DataExtractor>::Type 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 lldb::SharedPtr <Allocation>::Type 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()) 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 target = GetAccessTarget(addr); 345 346 return m_decl_map.ReadTarget(data, target, length); 347 } 348 349 std::string PrintData (lldb::addr_t addr, size_t length) 350 { 351 lldb_private::Value target = GetAccessTarget(addr); 352 353 lldb_private::DataBufferHeap buf(length, 0); 354 355 if (!m_decl_map.ReadTarget(buf.GetBytes(), target, length)) 356 return std::string("<couldn't read data>"); 357 358 lldb_private::StreamString ss; 359 360 for (size_t i = 0; i < length; i++) 361 { 362 if ((!(i & 0xf)) && i) 363 ss.Printf("%02hhx - ", buf.GetBytes()[i]); 364 else 365 ss.Printf("%02hhx ", buf.GetBytes()[i]); 366 } 367 368 return ss.GetString(); 369 } 370 371 std::string SummarizeRegion (Region ®ion) 372 { 373 lldb_private::StreamString ss; 374 375 lldb_private::Value base = GetAccessTarget(region.m_base); 376 377 ss.Printf("%llx [%s - %s %llx]", 378 region.m_base, 379 lldb_private::Value::GetValueTypeAsCString(base.GetValueType()), 380 lldb_private::Value::GetContextTypeAsCString(base.GetContextType()), 381 base.GetScalar().ULongLong()); 382 383 ss.Printf(" %s", PrintData(region.m_base, region.m_extent).c_str()); 384 385 return ss.GetString(); 386 } 387 }; 388 389 class InterpreterStackFrame 390 { 391 public: 392 typedef std::map <const Value*, Memory::Region> ValueMap; 393 394 ValueMap m_values; 395 Memory &m_memory; 396 TargetData &m_target_data; 397 lldb_private::ClangExpressionDeclMap &m_decl_map; 398 const BasicBlock *m_bb; 399 BasicBlock::const_iterator m_ii; 400 BasicBlock::const_iterator m_ie; 401 402 lldb::ByteOrder m_byte_order; 403 size_t m_addr_byte_size; 404 405 InterpreterStackFrame (TargetData &target_data, 406 Memory &memory, 407 lldb_private::ClangExpressionDeclMap &decl_map) : 408 m_target_data (target_data), 409 m_memory (memory), 410 m_decl_map (decl_map) 411 { 412 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig); 413 m_addr_byte_size = (target_data.getPointerSize()); 414 } 415 416 void Jump (const BasicBlock *bb) 417 { 418 m_bb = bb; 419 m_ii = m_bb->begin(); 420 m_ie = m_bb->end(); 421 } 422 423 bool Cache (Memory::AllocationSP allocation, Type *type) 424 { 425 if (allocation->m_origin.GetContextType() != lldb_private::Value::eContextTypeRegisterInfo) 426 return false; 427 428 return m_decl_map.ReadTarget(allocation->m_data->GetBytes(), allocation->m_origin, allocation->m_data->GetByteSize()); 429 } 430 431 std::string SummarizeValue (const Value *value) 432 { 433 lldb_private::StreamString ss; 434 435 ss.Printf("%s", PrintValue(value).c_str()); 436 437 ValueMap::iterator i = m_values.find(value); 438 439 if (i != m_values.end()) 440 { 441 Memory::Region region = i->second; 442 443 ss.Printf(" %s", m_memory.SummarizeRegion(region).c_str()); 444 } 445 446 return ss.GetString(); 447 } 448 449 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type) 450 { 451 size_t type_size = m_target_data.getTypeStoreSize(type); 452 453 switch (type_size) 454 { 455 case 1: 456 scalar = (uint8_t)u64value; 457 break; 458 case 2: 459 scalar = (uint16_t)u64value; 460 break; 461 case 4: 462 scalar = (uint32_t)u64value; 463 break; 464 case 8: 465 scalar = (uint64_t)u64value; 466 break; 467 default: 468 return false; 469 } 470 471 return true; 472 } 473 474 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module) 475 { 476 const Constant *constant = dyn_cast<Constant>(value); 477 478 if (constant) 479 { 480 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) 481 { 482 return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType()); 483 } 484 } 485 else 486 { 487 Memory::Region region = ResolveValue(value, module); 488 DataExtractorSP value_extractor = m_memory.GetExtractor(region); 489 490 if (!value_extractor) 491 return false; 492 493 size_t value_size = m_target_data.getTypeStoreSize(value->getType()); 494 495 uint32_t offset = 0; 496 uint64_t u64value = value_extractor->GetMaxU64(&offset, value_size); 497 498 return AssignToMatchType(scalar, u64value, value->getType()); 499 } 500 501 return false; 502 } 503 504 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module) 505 { 506 Memory::Region region = ResolveValue (value, module); 507 508 lldb_private::Scalar cast_scalar; 509 510 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType())) 511 return false; 512 513 lldb_private::DataBufferHeap buf(cast_scalar.GetByteSize(), 0); 514 515 lldb_private::Error err; 516 517 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, err)) 518 return false; 519 520 DataEncoderSP region_encoder = m_memory.GetEncoder(region); 521 522 memcpy(region_encoder->GetDataStart(), buf.GetBytes(), buf.GetByteSize()); 523 524 return true; 525 } 526 527 bool ResolveConstant (Memory::Region ®ion, const Constant *constant) 528 { 529 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType()); 530 531 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) 532 { 533 const uint64_t *raw_data = constant_int->getValue().getRawData(); 534 return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size); 535 } 536 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant)) 537 { 538 const uint64_t *raw_data = constant_fp->getValueAPF().bitcastToAPInt().getRawData(); 539 return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size); 540 } 541 542 return false; 543 } 544 545 Memory::Region ResolveValue (const Value *value, Module &module) 546 { 547 ValueMap::iterator i = m_values.find(value); 548 549 if (i != m_values.end()) 550 return i->second; 551 552 const GlobalValue *global_value = dyn_cast<GlobalValue>(value); 553 554 // Attempt to resolve the value using the program's data. 555 // If it is, the values to be created are: 556 // 557 // data_region - a region of memory in which the variable's data resides. 558 // ref_region - a region of memory in which its address (i.e., &var) resides. 559 // In the JIT case, this region would be a member of the struct passed in. 560 // pointer_region - a region of memory in which the address of the pointer 561 // resides. This is an IR-level variable. 562 do 563 { 564 if (!global_value) 565 break; 566 567 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 568 569 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module); 570 571 if (!decl) 572 break; 573 574 lldb_private::Value resolved_value = m_decl_map.LookupDecl(decl); 575 576 if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void) 577 { 578 if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo) 579 { 580 Memory::Region data_region = m_memory.Malloc(value->getType()); 581 data_region.m_allocation->m_origin = resolved_value; 582 Memory::Region ref_region = m_memory.Malloc(value->getType()); 583 Memory::Region pointer_region = m_memory.Malloc(value->getType()); 584 585 if (!Cache(data_region.m_allocation, value->getType())) 586 return Memory::Region(); 587 588 if (ref_region.IsInvalid()) 589 return Memory::Region(); 590 591 if (pointer_region.IsInvalid()) 592 return Memory::Region(); 593 594 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region); 595 596 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX) 597 return Memory::Region(); 598 599 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region); 600 601 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX) 602 return Memory::Region(); 603 604 m_values[value] = pointer_region; 605 return pointer_region; 606 } 607 else if (isa<clang::FunctionDecl>(decl)) 608 { 609 if (log) 610 log->Printf("The interpreter does not handle function pointers at the moment"); 611 612 return Memory::Region(); 613 } 614 else 615 { 616 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value); 617 Memory::Region ref_region = m_memory.Malloc(value->getType()); 618 Memory::Region pointer_region = m_memory.Malloc(value->getType()); 619 620 if (ref_region.IsInvalid()) 621 return Memory::Region(); 622 623 if (pointer_region.IsInvalid()) 624 return Memory::Region(); 625 626 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region); 627 628 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX) 629 return Memory::Region(); 630 631 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region); 632 633 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX) 634 return Memory::Region(); 635 636 m_values[value] = pointer_region; 637 638 if (log) 639 { 640 log->Printf("Made an allocation for %s", PrintValue(global_value).c_str()); 641 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str()); 642 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base); 643 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base); 644 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base); 645 } 646 647 return pointer_region; 648 } 649 } 650 } 651 while(0); 652 653 // Fall back and allocate space [allocation type Alloca] 654 655 Type *type = value->getType(); 656 657 lldb::ValueSP backing_value(new lldb_private::Value); 658 659 Memory::Region data_region = m_memory.Malloc(type); 660 data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes(); 661 data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 662 data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress); 663 664 const Constant *constant = dyn_cast<Constant>(value); 665 666 do 667 { 668 if (!constant) 669 break; 670 671 if (!ResolveConstant (data_region, constant)) 672 return Memory::Region(); 673 } 674 while(0); 675 676 m_values[value] = data_region; 677 return data_region; 678 } 679 680 bool ConstructResult (lldb::ClangExpressionVariableSP &result, 681 const GlobalValue *result_value, 682 const lldb_private::ConstString &result_name, 683 lldb_private::TypeFromParser result_type, 684 Module &module) 685 { 686 // The result_value resolves to P, a pointer to a region R containing the result data. 687 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process. 688 689 if (!result_value) 690 return true; // There was no slot for a result – the expression doesn't return one. 691 692 ValueMap::iterator i = m_values.find(result_value); 693 694 if (i == m_values.end()) 695 return false; // There was a slot for the result, but we didn't write into it. 696 697 Memory::Region P = i->second; 698 DataExtractorSP P_extractor = m_memory.GetExtractor(P); 699 700 if (!P_extractor) 701 return false; 702 703 Type *pointer_ty = result_value->getType(); 704 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 705 if (!pointer_ptr_ty) 706 return false; 707 Type *R_ty = pointer_ptr_ty->getElementType(); 708 709 uint32_t offset = 0; 710 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 711 712 Memory::Region R = m_memory.Lookup(pointer, R_ty); 713 714 if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress || 715 !R.m_allocation->m_data) 716 return false; 717 718 lldb_private::Value base; 719 720 bool transient = false; 721 722 if (m_decl_map.ResultIsReference(result_name)) 723 { 724 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty); 725 if (!R_ptr_ty) 726 return false; 727 Type *R_final_ty = R_ptr_ty->getElementType(); 728 729 DataExtractorSP R_extractor = m_memory.GetExtractor(R); 730 731 if (!R_extractor) 732 return false; 733 734 offset = 0; 735 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset); 736 737 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty); 738 739 if (!R_final.m_allocation) 740 return false; 741 742 if (R_final.m_allocation->m_data) 743 transient = true; // this is a stack allocation 744 745 base = R_final.m_allocation->m_origin; 746 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address); 747 } 748 else 749 { 750 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 751 base.SetValueType(lldb_private::Value::eValueTypeHostAddress); 752 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address); 753 } 754 755 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient); 756 } 757 }; 758 759 bool 760 IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result, 761 const lldb_private::ConstString &result_name, 762 lldb_private::TypeFromParser result_type, 763 Function &llvm_function, 764 Module &llvm_module) 765 { 766 if (supportsFunction (llvm_function)) 767 return runOnFunction(result, 768 result_name, 769 result_type, 770 llvm_function, 771 llvm_module); 772 else 773 return false; 774 } 775 776 bool 777 IRInterpreter::supportsFunction (Function &llvm_function) 778 { 779 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 780 781 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end(); 782 bbi != bbe; 783 ++bbi) 784 { 785 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end(); 786 ii != ie; 787 ++ii) 788 { 789 switch (ii->getOpcode()) 790 { 791 default: 792 { 793 if (log) 794 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str()); 795 return false; 796 } 797 case Instruction::Add: 798 case Instruction::Alloca: 799 case Instruction::BitCast: 800 case Instruction::Br: 801 case Instruction::GetElementPtr: 802 break; 803 case Instruction::ICmp: 804 { 805 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii); 806 807 if (!icmp_inst) 808 return false; 809 810 switch (icmp_inst->getPredicate()) 811 { 812 default: 813 { 814 if (log) 815 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str()); 816 return false; 817 } 818 case CmpInst::ICMP_EQ: 819 case CmpInst::ICMP_NE: 820 case CmpInst::ICMP_UGT: 821 case CmpInst::ICMP_UGE: 822 case CmpInst::ICMP_ULT: 823 case CmpInst::ICMP_ULE: 824 case CmpInst::ICMP_SGT: 825 case CmpInst::ICMP_SGE: 826 case CmpInst::ICMP_SLT: 827 case CmpInst::ICMP_SLE: 828 break; 829 } 830 } 831 break; 832 case Instruction::Load: 833 case Instruction::Mul: 834 case Instruction::Ret: 835 case Instruction::SDiv: 836 case Instruction::Store: 837 case Instruction::Sub: 838 case Instruction::UDiv: 839 break; 840 } 841 } 842 } 843 844 return true; 845 } 846 847 bool 848 IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result, 849 const lldb_private::ConstString &result_name, 850 lldb_private::TypeFromParser result_type, 851 Function &llvm_function, 852 Module &llvm_module) 853 { 854 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 855 856 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo(); 857 858 if (!target_info.IsValid()) 859 return false; 860 861 lldb::addr_t alloc_min; 862 lldb::addr_t alloc_max; 863 864 switch (target_info.address_byte_size) 865 { 866 default: 867 return false; 868 case 4: 869 alloc_min = 0x00001000llu; 870 alloc_max = 0x0000ffffllu; 871 break; 872 case 8: 873 alloc_min = 0x0000000000001000llu; 874 alloc_max = 0x000000000000ffffllu; 875 break; 876 } 877 878 TargetData target_data(&llvm_module); 879 if (target_data.getPointerSize() != target_info.address_byte_size) 880 return false; 881 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle)) 882 return false; 883 884 Memory memory(target_data, m_decl_map, alloc_min, alloc_max); 885 InterpreterStackFrame frame(target_data, memory, m_decl_map); 886 887 uint32_t num_insts = 0; 888 889 frame.Jump(llvm_function.begin()); 890 891 while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) 892 { 893 const Instruction *inst = frame.m_ii; 894 895 if (log) 896 log->Printf("Interpreting %s", PrintValue(inst).c_str()); 897 898 switch (inst->getOpcode()) 899 { 900 default: 901 break; 902 case Instruction::Add: 903 case Instruction::Sub: 904 case Instruction::Mul: 905 case Instruction::SDiv: 906 case Instruction::UDiv: 907 { 908 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst); 909 910 if (!bin_op) 911 { 912 if (log) 913 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName()); 914 915 return false; 916 } 917 918 Value *lhs = inst->getOperand(0); 919 Value *rhs = inst->getOperand(1); 920 921 lldb_private::Scalar L; 922 lldb_private::Scalar R; 923 924 if (!frame.EvaluateValue(L, lhs, llvm_module)) 925 { 926 if (log) 927 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 928 929 return false; 930 } 931 932 if (!frame.EvaluateValue(R, rhs, llvm_module)) 933 { 934 if (log) 935 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 936 937 return false; 938 } 939 940 lldb_private::Scalar result; 941 942 switch (inst->getOpcode()) 943 { 944 default: 945 break; 946 case Instruction::Add: 947 result = L + R; 948 break; 949 case Instruction::Mul: 950 result = L * R; 951 break; 952 case Instruction::Sub: 953 result = L - R; 954 break; 955 case Instruction::SDiv: 956 result = L / R; 957 break; 958 case Instruction::UDiv: 959 result = L.GetRawBits64(0) / R.GetRawBits64(1); 960 break; 961 } 962 963 frame.AssignValue(inst, result, llvm_module); 964 965 if (log) 966 { 967 log->Printf("Interpreted a %s", inst->getOpcodeName()); 968 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 969 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 970 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 971 } 972 } 973 break; 974 case Instruction::Alloca: 975 { 976 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst); 977 978 if (!alloca_inst) 979 { 980 if (log) 981 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst"); 982 983 return false; 984 } 985 986 if (alloca_inst->isArrayAllocation()) 987 { 988 if (log) 989 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true"); 990 991 return false; 992 } 993 994 // The semantics of Alloca are: 995 // Create a region R of virtual memory of type T, backed by a data buffer 996 // Create a region P of virtual memory of type T*, backed by a data buffer 997 // Write the virtual address of R into P 998 999 Type *T = alloca_inst->getAllocatedType(); 1000 Type *Tptr = alloca_inst->getType(); 1001 1002 Memory::Region R = memory.Malloc(T); 1003 1004 if (R.IsInvalid()) 1005 { 1006 if (log) 1007 log->Printf("Couldn't allocate memory for an AllocaInst"); 1008 1009 return false; 1010 } 1011 1012 Memory::Region P = memory.Malloc(Tptr); 1013 1014 if (P.IsInvalid()) 1015 { 1016 if (log) 1017 log->Printf("Couldn't allocate the result pointer for an AllocaInst"); 1018 1019 return false; 1020 } 1021 1022 DataEncoderSP P_encoder = memory.GetEncoder(P); 1023 1024 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX) 1025 { 1026 if (log) 1027 log->Printf("Couldn't write the reseult pointer for an AllocaInst"); 1028 1029 return false; 1030 } 1031 1032 frame.m_values[alloca_inst] = P; 1033 1034 if (log) 1035 { 1036 log->Printf("Interpreted an AllocaInst"); 1037 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1038 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str()); 1039 } 1040 } 1041 break; 1042 case Instruction::BitCast: 1043 { 1044 const BitCastInst *bit_cast_inst = dyn_cast<BitCastInst>(inst); 1045 1046 if (!bit_cast_inst) 1047 { 1048 if (log) 1049 log->Printf("getOpcode() returns BitCast, but instruction is not a BitCastInst"); 1050 1051 return false; 1052 } 1053 1054 Value *source = bit_cast_inst->getOperand(0); 1055 1056 lldb_private::Scalar S; 1057 1058 if (!frame.EvaluateValue(S, source, llvm_module)) 1059 { 1060 if (log) 1061 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); 1062 1063 return false; 1064 } 1065 1066 frame.AssignValue(inst, S, llvm_module); 1067 } 1068 break; 1069 case Instruction::Br: 1070 { 1071 const BranchInst *br_inst = dyn_cast<BranchInst>(inst); 1072 1073 if (!br_inst) 1074 { 1075 if (log) 1076 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst"); 1077 1078 return false; 1079 } 1080 1081 if (br_inst->isConditional()) 1082 { 1083 Value *condition = br_inst->getCondition(); 1084 1085 lldb_private::Scalar C; 1086 1087 if (!frame.EvaluateValue(C, condition, llvm_module)) 1088 { 1089 if (log) 1090 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str()); 1091 1092 return false; 1093 } 1094 1095 if (C.GetRawBits64(0)) 1096 frame.Jump(br_inst->getSuccessor(0)); 1097 else 1098 frame.Jump(br_inst->getSuccessor(1)); 1099 1100 if (log) 1101 { 1102 log->Printf("Interpreted a BrInst with a condition"); 1103 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str()); 1104 } 1105 } 1106 else 1107 { 1108 frame.Jump(br_inst->getSuccessor(0)); 1109 1110 if (log) 1111 { 1112 log->Printf("Interpreted a BrInst with no condition"); 1113 } 1114 } 1115 } 1116 continue; 1117 case Instruction::GetElementPtr: 1118 { 1119 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst); 1120 1121 if (!gep_inst) 1122 { 1123 if (log) 1124 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst"); 1125 1126 return false; 1127 } 1128 1129 const Value *pointer_operand = gep_inst->getPointerOperand(); 1130 Type *pointer_type = pointer_operand->getType(); 1131 1132 lldb_private::Scalar P; 1133 1134 if (!frame.EvaluateValue(P, pointer_operand, llvm_module)) 1135 return false; 1136 1137 SmallVector <Value *, 8> indices (gep_inst->idx_begin(), 1138 gep_inst->idx_end()); 1139 1140 uint64_t offset = target_data.getIndexedOffset(pointer_type, indices); 1141 1142 lldb_private::Scalar Poffset = P + offset; 1143 1144 frame.AssignValue(inst, Poffset, llvm_module); 1145 1146 if (log) 1147 { 1148 log->Printf("Interpreted a GetElementPtrInst"); 1149 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1150 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str()); 1151 } 1152 } 1153 break; 1154 case Instruction::ICmp: 1155 { 1156 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst); 1157 1158 if (!icmp_inst) 1159 { 1160 if (log) 1161 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst"); 1162 1163 return false; 1164 } 1165 1166 CmpInst::Predicate predicate = icmp_inst->getPredicate(); 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 1179 return false; 1180 } 1181 1182 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1183 { 1184 if (log) 1185 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1186 1187 return false; 1188 } 1189 1190 lldb_private::Scalar result; 1191 1192 switch (predicate) 1193 { 1194 default: 1195 return false; 1196 case CmpInst::ICMP_EQ: 1197 result = (L == R); 1198 break; 1199 case CmpInst::ICMP_NE: 1200 result = (L != R); 1201 break; 1202 case CmpInst::ICMP_UGT: 1203 result = (L.GetRawBits64(0) > R.GetRawBits64(0)); 1204 break; 1205 case CmpInst::ICMP_UGE: 1206 result = (L.GetRawBits64(0) >= R.GetRawBits64(0)); 1207 break; 1208 case CmpInst::ICMP_ULT: 1209 result = (L.GetRawBits64(0) < R.GetRawBits64(0)); 1210 break; 1211 case CmpInst::ICMP_ULE: 1212 result = (L.GetRawBits64(0) <= R.GetRawBits64(0)); 1213 break; 1214 case CmpInst::ICMP_SGT: 1215 result = (L > R); 1216 break; 1217 case CmpInst::ICMP_SGE: 1218 result = (L >= R); 1219 break; 1220 case CmpInst::ICMP_SLT: 1221 result = (L < R); 1222 break; 1223 case CmpInst::ICMP_SLE: 1224 result = (L <= R); 1225 break; 1226 } 1227 1228 frame.AssignValue(inst, result, llvm_module); 1229 1230 if (log) 1231 { 1232 log->Printf("Interpreted an ICmpInst"); 1233 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1234 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1235 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1236 } 1237 } 1238 break; 1239 case Instruction::Load: 1240 { 1241 const LoadInst *load_inst = dyn_cast<LoadInst>(inst); 1242 1243 if (!load_inst) 1244 { 1245 if (log) 1246 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst"); 1247 1248 return false; 1249 } 1250 1251 // The semantics of Load are: 1252 // Create a region D that will contain the loaded data 1253 // Resolve the region P containing a pointer 1254 // Dereference P to get the region R that the data should be loaded from 1255 // Transfer a unit of type type(D) from R to D 1256 1257 const Value *pointer_operand = load_inst->getPointerOperand(); 1258 1259 Type *pointer_ty = pointer_operand->getType(); 1260 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1261 if (!pointer_ptr_ty) 1262 return false; 1263 Type *target_ty = pointer_ptr_ty->getElementType(); 1264 1265 Memory::Region D = frame.ResolveValue(load_inst, llvm_module); 1266 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1267 1268 if (D.IsInvalid()) 1269 { 1270 if (log) 1271 log->Printf("LoadInst's value doesn't resolve to anything"); 1272 1273 return false; 1274 } 1275 1276 if (P.IsInvalid()) 1277 { 1278 if (log) 1279 log->Printf("LoadInst's pointer doesn't resolve to anything"); 1280 1281 return false; 1282 } 1283 1284 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1285 DataEncoderSP D_encoder(memory.GetEncoder(D)); 1286 1287 uint32_t offset = 0; 1288 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1289 1290 Memory::Region R = memory.Lookup(pointer, target_ty); 1291 1292 memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty)); 1293 1294 if (log) 1295 { 1296 log->Printf("Interpreted a LoadInst"); 1297 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1298 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1299 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str()); 1300 } 1301 } 1302 break; 1303 case Instruction::Ret: 1304 { 1305 if (result_name.IsEmpty()) 1306 return true; 1307 1308 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString()); 1309 return frame.ConstructResult(result, result_value, result_name, result_type, llvm_module); 1310 } 1311 case Instruction::Store: 1312 { 1313 const StoreInst *store_inst = dyn_cast<StoreInst>(inst); 1314 1315 if (!store_inst) 1316 { 1317 if (log) 1318 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst"); 1319 1320 return false; 1321 } 1322 1323 // The semantics of Store are: 1324 // Resolve the region D containing the data to be stored 1325 // Resolve the region P containing a pointer 1326 // Dereference P to get the region R that the data should be stored in 1327 // Transfer a unit of type type(D) from D to R 1328 1329 const Value *value_operand = store_inst->getValueOperand(); 1330 const Value *pointer_operand = store_inst->getPointerOperand(); 1331 1332 Type *pointer_ty = pointer_operand->getType(); 1333 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1334 if (!pointer_ptr_ty) 1335 return false; 1336 Type *target_ty = pointer_ptr_ty->getElementType(); 1337 1338 Memory::Region D = frame.ResolveValue(value_operand, llvm_module); 1339 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1340 1341 if (D.IsInvalid()) 1342 { 1343 if (log) 1344 log->Printf("StoreInst's value doesn't resolve to anything"); 1345 1346 return false; 1347 } 1348 1349 if (P.IsInvalid()) 1350 { 1351 if (log) 1352 log->Printf("StoreInst's pointer doesn't resolve to anything"); 1353 1354 return false; 1355 } 1356 1357 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1358 DataExtractorSP D_extractor(memory.GetExtractor(D)); 1359 1360 if (!P_extractor || !D_extractor) 1361 return false; 1362 1363 uint32_t offset = 0; 1364 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1365 1366 Memory::Region R = memory.Lookup(pointer, target_ty); 1367 1368 if (R.IsInvalid()) 1369 { 1370 if (log) 1371 log->Printf("StoreInst's pointer doesn't point to a valid target"); 1372 1373 return false; 1374 } 1375 1376 memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)); 1377 1378 if (log) 1379 { 1380 log->Printf("Interpreted a StoreInst"); 1381 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str()); 1382 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1383 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1384 } 1385 } 1386 break; 1387 } 1388 1389 ++frame.m_ii; 1390 } 1391 1392 if (num_insts >= 4096) 1393 return false; 1394 1395 return false; 1396 } 1397