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 if (m_decl_map.ResultIsReference(result_name)) 721 { 722 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty); 723 if (!R_ptr_ty) 724 return false; 725 Type *R_final_ty = R_ptr_ty->getElementType(); 726 727 DataExtractorSP R_extractor = m_memory.GetExtractor(R); 728 729 if (!R_extractor) 730 return false; 731 732 offset = 0; 733 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset); 734 735 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty); 736 737 if (!R_final.m_allocation) 738 return false; 739 740 base = R_final.m_allocation->m_origin; 741 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address); 742 } 743 else 744 { 745 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 746 base.SetValueType(lldb_private::Value::eValueTypeHostAddress); 747 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address); 748 } 749 750 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type); 751 } 752 }; 753 754 bool 755 IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result, 756 const lldb_private::ConstString &result_name, 757 lldb_private::TypeFromParser result_type, 758 Function &llvm_function, 759 Module &llvm_module) 760 { 761 if (supportsFunction (llvm_function)) 762 return runOnFunction(result, 763 result_name, 764 result_type, 765 llvm_function, 766 llvm_module); 767 else 768 return false; 769 } 770 771 bool 772 IRInterpreter::supportsFunction (Function &llvm_function) 773 { 774 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 775 776 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end(); 777 bbi != bbe; 778 ++bbi) 779 { 780 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end(); 781 ii != ie; 782 ++ii) 783 { 784 switch (ii->getOpcode()) 785 { 786 default: 787 { 788 if (log) 789 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str()); 790 return false; 791 } 792 case Instruction::Add: 793 case Instruction::Alloca: 794 case Instruction::BitCast: 795 case Instruction::Br: 796 case Instruction::GetElementPtr: 797 break; 798 case Instruction::ICmp: 799 { 800 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii); 801 802 if (!icmp_inst) 803 return false; 804 805 switch (icmp_inst->getPredicate()) 806 { 807 default: 808 { 809 if (log) 810 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str()); 811 return false; 812 } 813 case CmpInst::ICMP_EQ: 814 case CmpInst::ICMP_NE: 815 case CmpInst::ICMP_UGT: 816 case CmpInst::ICMP_UGE: 817 case CmpInst::ICMP_ULT: 818 case CmpInst::ICMP_ULE: 819 case CmpInst::ICMP_SGT: 820 case CmpInst::ICMP_SGE: 821 case CmpInst::ICMP_SLT: 822 case CmpInst::ICMP_SLE: 823 break; 824 } 825 } 826 break; 827 case Instruction::Load: 828 case Instruction::Mul: 829 case Instruction::Ret: 830 case Instruction::SDiv: 831 case Instruction::Store: 832 case Instruction::Sub: 833 case Instruction::UDiv: 834 break; 835 } 836 } 837 } 838 839 return true; 840 } 841 842 bool 843 IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result, 844 const lldb_private::ConstString &result_name, 845 lldb_private::TypeFromParser result_type, 846 Function &llvm_function, 847 Module &llvm_module) 848 { 849 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 850 851 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo(); 852 853 if (!target_info.IsValid()) 854 return false; 855 856 lldb::addr_t alloc_min; 857 lldb::addr_t alloc_max; 858 859 switch (target_info.address_byte_size) 860 { 861 default: 862 return false; 863 case 4: 864 alloc_min = 0x00001000llu; 865 alloc_max = 0x0000ffffllu; 866 break; 867 case 8: 868 alloc_min = 0x0000000000001000llu; 869 alloc_max = 0x000000000000ffffllu; 870 break; 871 } 872 873 TargetData target_data(&llvm_module); 874 if (target_data.getPointerSize() != target_info.address_byte_size) 875 return false; 876 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle)) 877 return false; 878 879 Memory memory(target_data, m_decl_map, alloc_min, alloc_max); 880 InterpreterStackFrame frame(target_data, memory, m_decl_map); 881 882 uint32_t num_insts = 0; 883 884 frame.Jump(llvm_function.begin()); 885 886 while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) 887 { 888 const Instruction *inst = frame.m_ii; 889 890 if (log) 891 log->Printf("Interpreting %s", PrintValue(inst).c_str()); 892 893 switch (inst->getOpcode()) 894 { 895 default: 896 break; 897 case Instruction::Add: 898 case Instruction::Sub: 899 case Instruction::Mul: 900 case Instruction::SDiv: 901 case Instruction::UDiv: 902 { 903 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst); 904 905 if (!bin_op) 906 { 907 if (log) 908 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName()); 909 910 return false; 911 } 912 913 Value *lhs = inst->getOperand(0); 914 Value *rhs = inst->getOperand(1); 915 916 lldb_private::Scalar L; 917 lldb_private::Scalar R; 918 919 if (!frame.EvaluateValue(L, lhs, llvm_module)) 920 { 921 if (log) 922 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 923 924 return false; 925 } 926 927 if (!frame.EvaluateValue(R, rhs, llvm_module)) 928 { 929 if (log) 930 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 931 932 return false; 933 } 934 935 lldb_private::Scalar result; 936 937 switch (inst->getOpcode()) 938 { 939 default: 940 break; 941 case Instruction::Add: 942 result = L + R; 943 break; 944 case Instruction::Mul: 945 result = L * R; 946 break; 947 case Instruction::Sub: 948 result = L - R; 949 break; 950 case Instruction::SDiv: 951 result = L / R; 952 break; 953 case Instruction::UDiv: 954 result = L.GetRawBits64(0) / R.GetRawBits64(1); 955 break; 956 } 957 958 frame.AssignValue(inst, result, llvm_module); 959 960 if (log) 961 { 962 log->Printf("Interpreted a %s", inst->getOpcodeName()); 963 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 964 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 965 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 966 } 967 } 968 break; 969 case Instruction::Alloca: 970 { 971 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst); 972 973 if (!alloca_inst) 974 { 975 if (log) 976 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst"); 977 978 return false; 979 } 980 981 if (alloca_inst->isArrayAllocation()) 982 { 983 if (log) 984 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true"); 985 986 return false; 987 } 988 989 // The semantics of Alloca are: 990 // Create a region R of virtual memory of type T, backed by a data buffer 991 // Create a region P of virtual memory of type T*, backed by a data buffer 992 // Write the virtual address of R into P 993 994 Type *T = alloca_inst->getAllocatedType(); 995 Type *Tptr = alloca_inst->getType(); 996 997 Memory::Region R = memory.Malloc(T); 998 999 if (R.IsInvalid()) 1000 { 1001 if (log) 1002 log->Printf("Couldn't allocate memory for an AllocaInst"); 1003 1004 return false; 1005 } 1006 1007 Memory::Region P = memory.Malloc(Tptr); 1008 1009 if (P.IsInvalid()) 1010 { 1011 if (log) 1012 log->Printf("Couldn't allocate the result pointer for an AllocaInst"); 1013 1014 return false; 1015 } 1016 1017 DataEncoderSP P_encoder = memory.GetEncoder(P); 1018 1019 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX) 1020 { 1021 if (log) 1022 log->Printf("Couldn't write the reseult pointer for an AllocaInst"); 1023 1024 return false; 1025 } 1026 1027 frame.m_values[alloca_inst] = P; 1028 1029 if (log) 1030 { 1031 log->Printf("Interpreted an AllocaInst"); 1032 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1033 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str()); 1034 } 1035 } 1036 break; 1037 case Instruction::BitCast: 1038 { 1039 const BitCastInst *bit_cast_inst = dyn_cast<BitCastInst>(inst); 1040 1041 if (!bit_cast_inst) 1042 { 1043 if (log) 1044 log->Printf("getOpcode() returns BitCast, but instruction is not a BitCastInst"); 1045 1046 return false; 1047 } 1048 1049 Value *source = bit_cast_inst->getOperand(0); 1050 1051 lldb_private::Scalar S; 1052 1053 if (!frame.EvaluateValue(S, source, llvm_module)) 1054 { 1055 if (log) 1056 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); 1057 1058 return false; 1059 } 1060 1061 frame.AssignValue(inst, S, llvm_module); 1062 } 1063 break; 1064 case Instruction::Br: 1065 { 1066 const BranchInst *br_inst = dyn_cast<BranchInst>(inst); 1067 1068 if (!br_inst) 1069 { 1070 if (log) 1071 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst"); 1072 1073 return false; 1074 } 1075 1076 if (br_inst->isConditional()) 1077 { 1078 Value *condition = br_inst->getCondition(); 1079 1080 lldb_private::Scalar C; 1081 1082 if (!frame.EvaluateValue(C, condition, llvm_module)) 1083 { 1084 if (log) 1085 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str()); 1086 1087 return false; 1088 } 1089 1090 if (C.GetRawBits64(0)) 1091 frame.Jump(br_inst->getSuccessor(0)); 1092 else 1093 frame.Jump(br_inst->getSuccessor(1)); 1094 1095 if (log) 1096 { 1097 log->Printf("Interpreted a BrInst with a condition"); 1098 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str()); 1099 } 1100 } 1101 else 1102 { 1103 frame.Jump(br_inst->getSuccessor(0)); 1104 1105 if (log) 1106 { 1107 log->Printf("Interpreted a BrInst with no condition"); 1108 } 1109 } 1110 } 1111 continue; 1112 case Instruction::GetElementPtr: 1113 { 1114 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst); 1115 1116 if (!gep_inst) 1117 { 1118 if (log) 1119 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst"); 1120 1121 return false; 1122 } 1123 1124 const Value *pointer_operand = gep_inst->getPointerOperand(); 1125 Type *pointer_type = pointer_operand->getType(); 1126 1127 lldb_private::Scalar P; 1128 1129 if (!frame.EvaluateValue(P, pointer_operand, llvm_module)) 1130 return false; 1131 1132 SmallVector <Value *, 8> indices (gep_inst->idx_begin(), 1133 gep_inst->idx_end()); 1134 1135 uint64_t offset = target_data.getIndexedOffset(pointer_type, indices); 1136 1137 lldb_private::Scalar Poffset = P + offset; 1138 1139 frame.AssignValue(inst, Poffset, llvm_module); 1140 1141 if (log) 1142 { 1143 log->Printf("Interpreted a GetElementPtrInst"); 1144 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1145 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str()); 1146 } 1147 } 1148 break; 1149 case Instruction::ICmp: 1150 { 1151 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst); 1152 1153 if (!icmp_inst) 1154 { 1155 if (log) 1156 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst"); 1157 1158 return false; 1159 } 1160 1161 CmpInst::Predicate predicate = icmp_inst->getPredicate(); 1162 1163 Value *lhs = inst->getOperand(0); 1164 Value *rhs = inst->getOperand(1); 1165 1166 lldb_private::Scalar L; 1167 lldb_private::Scalar R; 1168 1169 if (!frame.EvaluateValue(L, lhs, llvm_module)) 1170 { 1171 if (log) 1172 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1173 1174 return false; 1175 } 1176 1177 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1178 { 1179 if (log) 1180 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1181 1182 return false; 1183 } 1184 1185 lldb_private::Scalar result; 1186 1187 switch (predicate) 1188 { 1189 default: 1190 return false; 1191 case CmpInst::ICMP_EQ: 1192 result = (L == R); 1193 break; 1194 case CmpInst::ICMP_NE: 1195 result = (L != R); 1196 break; 1197 case CmpInst::ICMP_UGT: 1198 result = (L.GetRawBits64(0) > R.GetRawBits64(0)); 1199 break; 1200 case CmpInst::ICMP_UGE: 1201 result = (L.GetRawBits64(0) >= R.GetRawBits64(0)); 1202 break; 1203 case CmpInst::ICMP_ULT: 1204 result = (L.GetRawBits64(0) < R.GetRawBits64(0)); 1205 break; 1206 case CmpInst::ICMP_ULE: 1207 result = (L.GetRawBits64(0) <= R.GetRawBits64(0)); 1208 break; 1209 case CmpInst::ICMP_SGT: 1210 result = (L > R); 1211 break; 1212 case CmpInst::ICMP_SGE: 1213 result = (L >= R); 1214 break; 1215 case CmpInst::ICMP_SLT: 1216 result = (L < R); 1217 break; 1218 case CmpInst::ICMP_SLE: 1219 result = (L <= R); 1220 break; 1221 } 1222 1223 frame.AssignValue(inst, result, llvm_module); 1224 1225 if (log) 1226 { 1227 log->Printf("Interpreted an ICmpInst"); 1228 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1229 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1230 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1231 } 1232 } 1233 break; 1234 case Instruction::Load: 1235 { 1236 const LoadInst *load_inst = dyn_cast<LoadInst>(inst); 1237 1238 if (!load_inst) 1239 { 1240 if (log) 1241 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst"); 1242 1243 return false; 1244 } 1245 1246 // The semantics of Load are: 1247 // Create a region D that will contain the loaded data 1248 // Resolve the region P containing a pointer 1249 // Dereference P to get the region R that the data should be loaded from 1250 // Transfer a unit of type type(D) from R to D 1251 1252 const Value *pointer_operand = load_inst->getPointerOperand(); 1253 1254 Type *pointer_ty = pointer_operand->getType(); 1255 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1256 if (!pointer_ptr_ty) 1257 return false; 1258 Type *target_ty = pointer_ptr_ty->getElementType(); 1259 1260 Memory::Region D = frame.ResolveValue(load_inst, llvm_module); 1261 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1262 1263 if (D.IsInvalid()) 1264 { 1265 if (log) 1266 log->Printf("LoadInst's value doesn't resolve to anything"); 1267 1268 return false; 1269 } 1270 1271 if (P.IsInvalid()) 1272 { 1273 if (log) 1274 log->Printf("LoadInst's pointer doesn't resolve to anything"); 1275 1276 return false; 1277 } 1278 1279 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1280 DataEncoderSP D_encoder(memory.GetEncoder(D)); 1281 1282 uint32_t offset = 0; 1283 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1284 1285 Memory::Region R = memory.Lookup(pointer, target_ty); 1286 1287 memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty)); 1288 1289 if (log) 1290 { 1291 log->Printf("Interpreted a LoadInst"); 1292 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1293 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1294 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str()); 1295 } 1296 } 1297 break; 1298 case Instruction::Ret: 1299 { 1300 if (result_name.IsEmpty()) 1301 return true; 1302 1303 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString()); 1304 return frame.ConstructResult(result, result_value, result_name, result_type, llvm_module); 1305 } 1306 case Instruction::Store: 1307 { 1308 const StoreInst *store_inst = dyn_cast<StoreInst>(inst); 1309 1310 if (!store_inst) 1311 { 1312 if (log) 1313 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst"); 1314 1315 return false; 1316 } 1317 1318 // The semantics of Store are: 1319 // Resolve the region D containing the data to be stored 1320 // Resolve the region P containing a pointer 1321 // Dereference P to get the region R that the data should be stored in 1322 // Transfer a unit of type type(D) from D to R 1323 1324 const Value *value_operand = store_inst->getValueOperand(); 1325 const Value *pointer_operand = store_inst->getPointerOperand(); 1326 1327 Type *pointer_ty = pointer_operand->getType(); 1328 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1329 if (!pointer_ptr_ty) 1330 return false; 1331 Type *target_ty = pointer_ptr_ty->getElementType(); 1332 1333 Memory::Region D = frame.ResolveValue(value_operand, llvm_module); 1334 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1335 1336 if (D.IsInvalid()) 1337 { 1338 if (log) 1339 log->Printf("StoreInst's value doesn't resolve to anything"); 1340 1341 return false; 1342 } 1343 1344 if (P.IsInvalid()) 1345 { 1346 if (log) 1347 log->Printf("StoreInst's pointer doesn't resolve to anything"); 1348 1349 return false; 1350 } 1351 1352 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1353 DataExtractorSP D_extractor(memory.GetExtractor(D)); 1354 1355 if (!P_extractor || !D_extractor) 1356 return false; 1357 1358 uint32_t offset = 0; 1359 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1360 1361 Memory::Region R = memory.Lookup(pointer, target_ty); 1362 1363 if (R.IsInvalid()) 1364 { 1365 if (log) 1366 log->Printf("StoreInst's pointer doesn't point to a valid target"); 1367 1368 return false; 1369 } 1370 1371 memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)); 1372 1373 if (log) 1374 { 1375 log->Printf("Interpreted a StoreInst"); 1376 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str()); 1377 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1378 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1379 } 1380 } 1381 break; 1382 } 1383 1384 ++frame.m_ii; 1385 } 1386 1387 if (num_insts >= 4096) 1388 return false; 1389 1390 return false; 1391 }