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 // If the variable is a function pointer, we do not need to 628 // build an extra layer of indirection for it because it is 629 // accessed directly. 630 bool variable_is_function_address = false; 631 632 // Attempt to resolve the value using the program's data. 633 // If it is, the values to be created are: 634 // 635 // data_region - a region of memory in which the variable's data resides. 636 // ref_region - a region of memory in which its address (i.e., &var) resides. 637 // In the JIT case, this region would be a member of the struct passed in. 638 // pointer_region - a region of memory in which the address of the pointer 639 // resides. This is an IR-level variable. 640 do 641 { 642 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 643 644 lldb_private::Value resolved_value; 645 lldb_private::ClangExpressionVariable::FlagType flags = 0; 646 647 if (global_value) 648 { 649 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module); 650 651 if (!decl) 652 break; 653 654 if (isa<clang::FunctionDecl>(decl)) 655 variable_is_function_address = true; 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 bool no_extra_redirect = (variable_is_this || variable_is_function_address); 790 791 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value); 792 Memory::Region ref_region = m_memory.Malloc(value->getType()); 793 Memory::Region pointer_region; 794 795 if (!no_extra_redirect) 796 pointer_region = m_memory.Malloc(value->getType()); 797 798 if (ref_region.IsInvalid()) 799 return Memory::Region(); 800 801 if (pointer_region.IsInvalid() && !no_extra_redirect) 802 return Memory::Region(); 803 804 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region); 805 806 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX) 807 return Memory::Region(); 808 809 if (!no_extra_redirect) 810 { 811 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region); 812 813 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX) 814 return Memory::Region(); 815 816 m_values[value] = pointer_region; 817 } 818 819 if (log) 820 { 821 log->Printf("Made an allocation for %s", PrintValue(value).c_str()); 822 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str()); 823 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base); 824 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base); 825 if (!variable_is_this) 826 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base); 827 } 828 829 if (no_extra_redirect) 830 return ref_region; 831 else 832 return pointer_region; 833 } 834 } 835 } 836 while(0); 837 838 // Fall back and allocate space [allocation type Alloca] 839 840 Type *type = value->getType(); 841 842 Memory::Region data_region = m_memory.Malloc(type); 843 data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes(); 844 data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 845 data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress); 846 847 const Constant *constant = dyn_cast<Constant>(value); 848 849 do 850 { 851 if (!constant) 852 break; 853 854 if (!ResolveConstant (data_region, constant)) 855 return Memory::Region(); 856 } 857 while(0); 858 859 m_values[value] = data_region; 860 return data_region; 861 } 862 863 bool ConstructResult (lldb::ClangExpressionVariableSP &result, 864 const GlobalValue *result_value, 865 const lldb_private::ConstString &result_name, 866 lldb_private::TypeFromParser result_type, 867 Module &module) 868 { 869 // The result_value resolves to P, a pointer to a region R containing the result data. 870 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process. 871 872 if (!result_value) 873 return true; // There was no slot for a result – the expression doesn't return one. 874 875 ValueMap::iterator i = m_values.find(result_value); 876 877 if (i == m_values.end()) 878 return false; // There was a slot for the result, but we didn't write into it. 879 880 Memory::Region P = i->second; 881 DataExtractorSP P_extractor = m_memory.GetExtractor(P); 882 883 if (!P_extractor) 884 return false; 885 886 Type *pointer_ty = result_value->getType(); 887 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 888 if (!pointer_ptr_ty) 889 return false; 890 Type *R_ty = pointer_ptr_ty->getElementType(); 891 892 lldb::offset_t offset = 0; 893 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 894 895 Memory::Region R = m_memory.Lookup(pointer, R_ty); 896 897 if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress || 898 !R.m_allocation->m_data) 899 return false; 900 901 lldb_private::Value base; 902 903 bool transient = false; 904 bool maybe_make_load = false; 905 906 if (m_decl_map.ResultIsReference(result_name)) 907 { 908 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty); 909 if (!R_ptr_ty) 910 return false; 911 Type *R_final_ty = R_ptr_ty->getElementType(); 912 913 DataExtractorSP R_extractor = m_memory.GetExtractor(R); 914 915 if (!R_extractor) 916 return false; 917 918 offset = 0; 919 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset); 920 921 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty); 922 923 if (R_final.m_allocation) 924 { 925 if (R_final.m_allocation->m_data) 926 transient = true; // this is a stack allocation 927 928 base = R_final.m_allocation->m_origin; 929 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address); 930 } 931 else 932 { 933 // We got a bare pointer. We are going to treat it as a load address 934 // or a file address, letting decl_map make the choice based on whether 935 // or not a process exists. 936 937 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 938 base.SetValueType(lldb_private::Value::eValueTypeFileAddress); 939 base.GetScalar() = (unsigned long long)R_pointer; 940 maybe_make_load = true; 941 } 942 } 943 else 944 { 945 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 946 base.SetValueType(lldb_private::Value::eValueTypeHostAddress); 947 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address); 948 } 949 950 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient, maybe_make_load); 951 } 952 }; 953 954 bool 955 IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result, 956 const lldb_private::ConstString &result_name, 957 lldb_private::TypeFromParser result_type, 958 Function &llvm_function, 959 Module &llvm_module, 960 lldb_private::Error &err) 961 { 962 if (supportsFunction (llvm_function, err)) 963 return runOnFunction(result, 964 result_name, 965 result_type, 966 llvm_function, 967 llvm_module, 968 err); 969 else 970 return false; 971 } 972 973 static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes"; 974 static const char *interpreter_initialization_error = "Interpreter couldn't be initialized"; 975 static const char *interpreter_internal_error = "Interpreter encountered an internal error"; 976 static const char *bad_value_error = "Interpreter couldn't resolve a value during execution"; 977 static const char *memory_allocation_error = "Interpreter couldn't allocate memory"; 978 static const char *memory_write_error = "Interpreter couldn't write to memory"; 979 static const char *memory_read_error = "Interpreter couldn't read from memory"; 980 static const char *infinite_loop_error = "Interpreter ran for too many cycles"; 981 static const char *bad_result_error = "Result of expression is in bad memory"; 982 983 bool 984 IRInterpreter::supportsFunction (Function &llvm_function, 985 lldb_private::Error &err) 986 { 987 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 988 989 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end(); 990 bbi != bbe; 991 ++bbi) 992 { 993 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end(); 994 ii != ie; 995 ++ii) 996 { 997 switch (ii->getOpcode()) 998 { 999 default: 1000 { 1001 if (log) 1002 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str()); 1003 err.SetErrorToGenericError(); 1004 err.SetErrorString(unsupported_opcode_error); 1005 return false; 1006 } 1007 case Instruction::Add: 1008 case Instruction::Alloca: 1009 case Instruction::BitCast: 1010 case Instruction::Br: 1011 case Instruction::GetElementPtr: 1012 break; 1013 case Instruction::ICmp: 1014 { 1015 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii); 1016 1017 if (!icmp_inst) 1018 { 1019 err.SetErrorToGenericError(); 1020 err.SetErrorString(interpreter_internal_error); 1021 return false; 1022 } 1023 1024 switch (icmp_inst->getPredicate()) 1025 { 1026 default: 1027 { 1028 if (log) 1029 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str()); 1030 1031 err.SetErrorToGenericError(); 1032 err.SetErrorString(unsupported_opcode_error); 1033 return false; 1034 } 1035 case CmpInst::ICMP_EQ: 1036 case CmpInst::ICMP_NE: 1037 case CmpInst::ICMP_UGT: 1038 case CmpInst::ICMP_UGE: 1039 case CmpInst::ICMP_ULT: 1040 case CmpInst::ICMP_ULE: 1041 case CmpInst::ICMP_SGT: 1042 case CmpInst::ICMP_SGE: 1043 case CmpInst::ICMP_SLT: 1044 case CmpInst::ICMP_SLE: 1045 break; 1046 } 1047 } 1048 break; 1049 case Instruction::And: 1050 case Instruction::AShr: 1051 case Instruction::IntToPtr: 1052 case Instruction::PtrToInt: 1053 case Instruction::Load: 1054 case Instruction::LShr: 1055 case Instruction::Mul: 1056 case Instruction::Or: 1057 case Instruction::Ret: 1058 case Instruction::SDiv: 1059 case Instruction::Shl: 1060 case Instruction::SRem: 1061 case Instruction::Store: 1062 case Instruction::Sub: 1063 case Instruction::UDiv: 1064 case Instruction::URem: 1065 case Instruction::Xor: 1066 case Instruction::ZExt: 1067 break; 1068 } 1069 } 1070 } 1071 1072 return true; 1073 } 1074 1075 bool 1076 IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result, 1077 const lldb_private::ConstString &result_name, 1078 lldb_private::TypeFromParser result_type, 1079 Function &llvm_function, 1080 Module &llvm_module, 1081 lldb_private::Error &err) 1082 { 1083 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1084 1085 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo(); 1086 1087 if (!target_info.IsValid()) 1088 { 1089 err.SetErrorToGenericError(); 1090 err.SetErrorString(interpreter_initialization_error); 1091 return false; 1092 } 1093 1094 lldb::addr_t alloc_min; 1095 lldb::addr_t alloc_max; 1096 1097 switch (target_info.address_byte_size) 1098 { 1099 default: 1100 err.SetErrorToGenericError(); 1101 err.SetErrorString(interpreter_initialization_error); 1102 return false; 1103 case 4: 1104 alloc_min = 0x00001000llu; 1105 alloc_max = 0x0000ffffllu; 1106 break; 1107 case 8: 1108 alloc_min = 0x0000000000001000llu; 1109 alloc_max = 0x000000000000ffffllu; 1110 break; 1111 } 1112 1113 DataLayout target_data(&llvm_module); 1114 if (target_data.getPointerSize(0) != target_info.address_byte_size) 1115 { 1116 err.SetErrorToGenericError(); 1117 err.SetErrorString(interpreter_initialization_error); 1118 return false; 1119 } 1120 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle)) 1121 { 1122 err.SetErrorToGenericError(); 1123 err.SetErrorString(interpreter_initialization_error); 1124 return false; 1125 } 1126 1127 Memory memory(target_data, m_decl_map, alloc_min, alloc_max); 1128 InterpreterStackFrame frame(target_data, memory, m_decl_map); 1129 1130 uint32_t num_insts = 0; 1131 1132 frame.Jump(llvm_function.begin()); 1133 1134 while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) 1135 { 1136 const Instruction *inst = frame.m_ii; 1137 1138 if (log) 1139 log->Printf("Interpreting %s", PrintValue(inst).c_str()); 1140 1141 switch (inst->getOpcode()) 1142 { 1143 default: 1144 break; 1145 case Instruction::Add: 1146 case Instruction::Sub: 1147 case Instruction::Mul: 1148 case Instruction::SDiv: 1149 case Instruction::UDiv: 1150 case Instruction::SRem: 1151 case Instruction::URem: 1152 case Instruction::Shl: 1153 case Instruction::LShr: 1154 case Instruction::AShr: 1155 case Instruction::And: 1156 case Instruction::Or: 1157 case Instruction::Xor: 1158 { 1159 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst); 1160 1161 if (!bin_op) 1162 { 1163 if (log) 1164 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName()); 1165 err.SetErrorToGenericError(); 1166 err.SetErrorString(interpreter_internal_error); 1167 return false; 1168 } 1169 1170 Value *lhs = inst->getOperand(0); 1171 Value *rhs = inst->getOperand(1); 1172 1173 lldb_private::Scalar L; 1174 lldb_private::Scalar R; 1175 1176 if (!frame.EvaluateValue(L, lhs, llvm_module)) 1177 { 1178 if (log) 1179 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1180 err.SetErrorToGenericError(); 1181 err.SetErrorString(bad_value_error); 1182 return false; 1183 } 1184 1185 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1186 { 1187 if (log) 1188 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1189 err.SetErrorToGenericError(); 1190 err.SetErrorString(bad_value_error); 1191 return false; 1192 } 1193 1194 lldb_private::Scalar result; 1195 1196 switch (inst->getOpcode()) 1197 { 1198 default: 1199 break; 1200 case Instruction::Add: 1201 result = L + R; 1202 break; 1203 case Instruction::Mul: 1204 result = L * R; 1205 break; 1206 case Instruction::Sub: 1207 result = L - R; 1208 break; 1209 case Instruction::SDiv: 1210 result = L / R; 1211 break; 1212 case Instruction::UDiv: 1213 result = L.GetRawBits64(0) / R.GetRawBits64(1); 1214 break; 1215 case Instruction::SRem: 1216 result = L % R; 1217 break; 1218 case Instruction::URem: 1219 result = L.GetRawBits64(0) % R.GetRawBits64(1); 1220 break; 1221 case Instruction::Shl: 1222 result = L << R; 1223 break; 1224 case Instruction::AShr: 1225 result = L >> R; 1226 break; 1227 case Instruction::LShr: 1228 result = L; 1229 result.ShiftRightLogical(R); 1230 break; 1231 case Instruction::And: 1232 result = L & R; 1233 break; 1234 case Instruction::Or: 1235 result = L | R; 1236 break; 1237 case Instruction::Xor: 1238 result = L ^ R; 1239 break; 1240 } 1241 1242 frame.AssignValue(inst, result, llvm_module); 1243 1244 if (log) 1245 { 1246 log->Printf("Interpreted a %s", inst->getOpcodeName()); 1247 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1248 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1249 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1250 } 1251 } 1252 break; 1253 case Instruction::Alloca: 1254 { 1255 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst); 1256 1257 if (!alloca_inst) 1258 { 1259 if (log) 1260 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst"); 1261 err.SetErrorToGenericError(); 1262 err.SetErrorString(interpreter_internal_error); 1263 return false; 1264 } 1265 1266 if (alloca_inst->isArrayAllocation()) 1267 { 1268 if (log) 1269 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true"); 1270 err.SetErrorToGenericError(); 1271 err.SetErrorString(unsupported_opcode_error); 1272 return false; 1273 } 1274 1275 // The semantics of Alloca are: 1276 // Create a region R of virtual memory of type T, backed by a data buffer 1277 // Create a region P of virtual memory of type T*, backed by a data buffer 1278 // Write the virtual address of R into P 1279 1280 Type *T = alloca_inst->getAllocatedType(); 1281 Type *Tptr = alloca_inst->getType(); 1282 1283 Memory::Region R = memory.Malloc(T); 1284 1285 if (R.IsInvalid()) 1286 { 1287 if (log) 1288 log->Printf("Couldn't allocate memory for an AllocaInst"); 1289 err.SetErrorToGenericError(); 1290 err.SetErrorString(memory_allocation_error); 1291 return false; 1292 } 1293 1294 Memory::Region P = memory.Malloc(Tptr); 1295 1296 if (P.IsInvalid()) 1297 { 1298 if (log) 1299 log->Printf("Couldn't allocate the result pointer for an AllocaInst"); 1300 err.SetErrorToGenericError(); 1301 err.SetErrorString(memory_allocation_error); 1302 return false; 1303 } 1304 1305 DataEncoderSP P_encoder = memory.GetEncoder(P); 1306 1307 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX) 1308 { 1309 if (log) 1310 log->Printf("Couldn't write the result pointer for an AllocaInst"); 1311 err.SetErrorToGenericError(); 1312 err.SetErrorString(memory_write_error); 1313 return false; 1314 } 1315 1316 frame.m_values[alloca_inst] = P; 1317 1318 if (log) 1319 { 1320 log->Printf("Interpreted an AllocaInst"); 1321 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1322 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str()); 1323 } 1324 } 1325 break; 1326 case Instruction::BitCast: 1327 case Instruction::ZExt: 1328 { 1329 const CastInst *cast_inst = dyn_cast<CastInst>(inst); 1330 1331 if (!cast_inst) 1332 { 1333 if (log) 1334 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName()); 1335 err.SetErrorToGenericError(); 1336 err.SetErrorString(interpreter_internal_error); 1337 return false; 1338 } 1339 1340 Value *source = cast_inst->getOperand(0); 1341 1342 lldb_private::Scalar S; 1343 1344 if (!frame.EvaluateValue(S, source, llvm_module)) 1345 { 1346 if (log) 1347 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); 1348 err.SetErrorToGenericError(); 1349 err.SetErrorString(bad_value_error); 1350 return false; 1351 } 1352 1353 frame.AssignValue(inst, S, llvm_module); 1354 } 1355 break; 1356 case Instruction::Br: 1357 { 1358 const BranchInst *br_inst = dyn_cast<BranchInst>(inst); 1359 1360 if (!br_inst) 1361 { 1362 if (log) 1363 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst"); 1364 err.SetErrorToGenericError(); 1365 err.SetErrorString(interpreter_internal_error); 1366 return false; 1367 } 1368 1369 if (br_inst->isConditional()) 1370 { 1371 Value *condition = br_inst->getCondition(); 1372 1373 lldb_private::Scalar C; 1374 1375 if (!frame.EvaluateValue(C, condition, llvm_module)) 1376 { 1377 if (log) 1378 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str()); 1379 err.SetErrorToGenericError(); 1380 err.SetErrorString(bad_value_error); 1381 return false; 1382 } 1383 1384 if (C.GetRawBits64(0)) 1385 frame.Jump(br_inst->getSuccessor(0)); 1386 else 1387 frame.Jump(br_inst->getSuccessor(1)); 1388 1389 if (log) 1390 { 1391 log->Printf("Interpreted a BrInst with a condition"); 1392 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str()); 1393 } 1394 } 1395 else 1396 { 1397 frame.Jump(br_inst->getSuccessor(0)); 1398 1399 if (log) 1400 { 1401 log->Printf("Interpreted a BrInst with no condition"); 1402 } 1403 } 1404 } 1405 continue; 1406 case Instruction::GetElementPtr: 1407 { 1408 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst); 1409 1410 if (!gep_inst) 1411 { 1412 if (log) 1413 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst"); 1414 err.SetErrorToGenericError(); 1415 err.SetErrorString(interpreter_internal_error); 1416 return false; 1417 } 1418 1419 const Value *pointer_operand = gep_inst->getPointerOperand(); 1420 Type *pointer_type = pointer_operand->getType(); 1421 1422 lldb_private::Scalar P; 1423 1424 if (!frame.EvaluateValue(P, pointer_operand, llvm_module)) 1425 { 1426 if (log) 1427 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str()); 1428 err.SetErrorToGenericError(); 1429 err.SetErrorString(bad_value_error); 1430 return false; 1431 } 1432 1433 typedef SmallVector <Value *, 8> IndexVector; 1434 typedef IndexVector::iterator IndexIterator; 1435 1436 SmallVector <Value *, 8> indices (gep_inst->idx_begin(), 1437 gep_inst->idx_end()); 1438 1439 SmallVector <Value *, 8> const_indices; 1440 1441 for (IndexIterator ii = indices.begin(), ie = indices.end(); 1442 ii != ie; 1443 ++ii) 1444 { 1445 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii); 1446 1447 if (!constant_index) 1448 { 1449 lldb_private::Scalar I; 1450 1451 if (!frame.EvaluateValue(I, *ii, llvm_module)) 1452 { 1453 if (log) 1454 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str()); 1455 err.SetErrorToGenericError(); 1456 err.SetErrorString(bad_value_error); 1457 return false; 1458 } 1459 1460 if (log) 1461 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS)); 1462 1463 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS))); 1464 } 1465 1466 const_indices.push_back(constant_index); 1467 } 1468 1469 uint64_t offset = target_data.getIndexedOffset(pointer_type, const_indices); 1470 1471 lldb_private::Scalar Poffset = P + offset; 1472 1473 frame.AssignValue(inst, Poffset, llvm_module); 1474 1475 if (log) 1476 { 1477 log->Printf("Interpreted a GetElementPtrInst"); 1478 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1479 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str()); 1480 } 1481 } 1482 break; 1483 case Instruction::ICmp: 1484 { 1485 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst); 1486 1487 if (!icmp_inst) 1488 { 1489 if (log) 1490 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst"); 1491 err.SetErrorToGenericError(); 1492 err.SetErrorString(interpreter_internal_error); 1493 return false; 1494 } 1495 1496 CmpInst::Predicate predicate = icmp_inst->getPredicate(); 1497 1498 Value *lhs = inst->getOperand(0); 1499 Value *rhs = inst->getOperand(1); 1500 1501 lldb_private::Scalar L; 1502 lldb_private::Scalar R; 1503 1504 if (!frame.EvaluateValue(L, lhs, llvm_module)) 1505 { 1506 if (log) 1507 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1508 err.SetErrorToGenericError(); 1509 err.SetErrorString(bad_value_error); 1510 return false; 1511 } 1512 1513 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1514 { 1515 if (log) 1516 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1517 err.SetErrorToGenericError(); 1518 err.SetErrorString(bad_value_error); 1519 return false; 1520 } 1521 1522 lldb_private::Scalar result; 1523 1524 switch (predicate) 1525 { 1526 default: 1527 return false; 1528 case CmpInst::ICMP_EQ: 1529 result = (L == R); 1530 break; 1531 case CmpInst::ICMP_NE: 1532 result = (L != R); 1533 break; 1534 case CmpInst::ICMP_UGT: 1535 result = (L.GetRawBits64(0) > R.GetRawBits64(0)); 1536 break; 1537 case CmpInst::ICMP_UGE: 1538 result = (L.GetRawBits64(0) >= R.GetRawBits64(0)); 1539 break; 1540 case CmpInst::ICMP_ULT: 1541 result = (L.GetRawBits64(0) < R.GetRawBits64(0)); 1542 break; 1543 case CmpInst::ICMP_ULE: 1544 result = (L.GetRawBits64(0) <= R.GetRawBits64(0)); 1545 break; 1546 case CmpInst::ICMP_SGT: 1547 result = (L > R); 1548 break; 1549 case CmpInst::ICMP_SGE: 1550 result = (L >= R); 1551 break; 1552 case CmpInst::ICMP_SLT: 1553 result = (L < R); 1554 break; 1555 case CmpInst::ICMP_SLE: 1556 result = (L <= R); 1557 break; 1558 } 1559 1560 frame.AssignValue(inst, result, llvm_module); 1561 1562 if (log) 1563 { 1564 log->Printf("Interpreted an ICmpInst"); 1565 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1566 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1567 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1568 } 1569 } 1570 break; 1571 case Instruction::IntToPtr: 1572 { 1573 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst); 1574 1575 if (!int_to_ptr_inst) 1576 { 1577 if (log) 1578 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst"); 1579 err.SetErrorToGenericError(); 1580 err.SetErrorString(interpreter_internal_error); 1581 return false; 1582 } 1583 1584 Value *src_operand = int_to_ptr_inst->getOperand(0); 1585 1586 lldb_private::Scalar I; 1587 1588 if (!frame.EvaluateValue(I, src_operand, llvm_module)) 1589 { 1590 if (log) 1591 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1592 err.SetErrorToGenericError(); 1593 err.SetErrorString(bad_value_error); 1594 return false; 1595 } 1596 1597 frame.AssignValue(inst, I, llvm_module); 1598 1599 if (log) 1600 { 1601 log->Printf("Interpreted an IntToPtr"); 1602 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1603 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1604 } 1605 } 1606 break; 1607 case Instruction::PtrToInt: 1608 { 1609 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst); 1610 1611 if (!ptr_to_int_inst) 1612 { 1613 if (log) 1614 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst"); 1615 err.SetErrorToGenericError(); 1616 err.SetErrorString(interpreter_internal_error); 1617 return false; 1618 } 1619 1620 Value *src_operand = ptr_to_int_inst->getOperand(0); 1621 1622 lldb_private::Scalar I; 1623 1624 if (!frame.EvaluateValue(I, src_operand, llvm_module)) 1625 { 1626 if (log) 1627 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1628 err.SetErrorToGenericError(); 1629 err.SetErrorString(bad_value_error); 1630 return false; 1631 } 1632 1633 frame.AssignValue(inst, I, llvm_module); 1634 1635 if (log) 1636 { 1637 log->Printf("Interpreted a PtrToInt"); 1638 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1639 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1640 } 1641 } 1642 break; 1643 case Instruction::Load: 1644 { 1645 const LoadInst *load_inst = dyn_cast<LoadInst>(inst); 1646 1647 if (!load_inst) 1648 { 1649 if (log) 1650 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst"); 1651 err.SetErrorToGenericError(); 1652 err.SetErrorString(interpreter_internal_error); 1653 return false; 1654 } 1655 1656 // The semantics of Load are: 1657 // Create a region D that will contain the loaded data 1658 // Resolve the region P containing a pointer 1659 // Dereference P to get the region R that the data should be loaded from 1660 // Transfer a unit of type type(D) from R to D 1661 1662 const Value *pointer_operand = load_inst->getPointerOperand(); 1663 1664 Type *pointer_ty = pointer_operand->getType(); 1665 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1666 if (!pointer_ptr_ty) 1667 { 1668 if (log) 1669 log->Printf("getPointerOperand()->getType() is not a PointerType"); 1670 err.SetErrorToGenericError(); 1671 err.SetErrorString(interpreter_internal_error); 1672 return false; 1673 } 1674 Type *target_ty = pointer_ptr_ty->getElementType(); 1675 1676 Memory::Region D = frame.ResolveValue(load_inst, llvm_module); 1677 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1678 1679 if (D.IsInvalid()) 1680 { 1681 if (log) 1682 log->Printf("LoadInst's value doesn't resolve to anything"); 1683 err.SetErrorToGenericError(); 1684 err.SetErrorString(bad_value_error); 1685 return false; 1686 } 1687 1688 if (P.IsInvalid()) 1689 { 1690 if (log) 1691 log->Printf("LoadInst's pointer doesn't resolve to anything"); 1692 err.SetErrorToGenericError(); 1693 err.SetErrorString(bad_value_error); 1694 return false; 1695 } 1696 1697 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1698 DataEncoderSP D_encoder(memory.GetEncoder(D)); 1699 1700 lldb::offset_t offset = 0; 1701 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1702 1703 Memory::Region R = memory.Lookup(pointer, target_ty); 1704 1705 if (R.IsValid()) 1706 { 1707 if (!memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty))) 1708 { 1709 if (log) 1710 log->Printf("Couldn't read from a region on behalf of a LoadInst"); 1711 err.SetErrorToGenericError(); 1712 err.SetErrorString(memory_read_error); 1713 return false; 1714 } 1715 } 1716 else 1717 { 1718 if (!memory.ReadFromRawPtr(D_encoder->GetDataStart(), pointer, target_data.getTypeStoreSize(target_ty))) 1719 { 1720 if (log) 1721 log->Printf("Couldn't read from a raw pointer on behalf of a LoadInst"); 1722 err.SetErrorToGenericError(); 1723 err.SetErrorString(memory_read_error); 1724 return false; 1725 } 1726 } 1727 1728 if (log) 1729 { 1730 log->Printf("Interpreted a LoadInst"); 1731 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1732 if (R.IsValid()) 1733 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1734 else 1735 log->Printf(" R : raw pointer 0x%llx", (unsigned long long)pointer); 1736 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str()); 1737 } 1738 } 1739 break; 1740 case Instruction::Ret: 1741 { 1742 if (result_name.IsEmpty()) 1743 return true; 1744 1745 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString()); 1746 1747 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module)) 1748 { 1749 if (log) 1750 log->Printf("Couldn't construct the expression's result"); 1751 err.SetErrorToGenericError(); 1752 err.SetErrorString(bad_result_error); 1753 return false; 1754 } 1755 1756 return true; 1757 } 1758 case Instruction::Store: 1759 { 1760 const StoreInst *store_inst = dyn_cast<StoreInst>(inst); 1761 1762 if (!store_inst) 1763 { 1764 if (log) 1765 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst"); 1766 err.SetErrorToGenericError(); 1767 err.SetErrorString(interpreter_internal_error); 1768 return false; 1769 } 1770 1771 // The semantics of Store are: 1772 // Resolve the region D containing the data to be stored 1773 // Resolve the region P containing a pointer 1774 // Dereference P to get the region R that the data should be stored in 1775 // Transfer a unit of type type(D) from D to R 1776 1777 const Value *value_operand = store_inst->getValueOperand(); 1778 const Value *pointer_operand = store_inst->getPointerOperand(); 1779 1780 Type *pointer_ty = pointer_operand->getType(); 1781 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1782 if (!pointer_ptr_ty) 1783 return false; 1784 Type *target_ty = pointer_ptr_ty->getElementType(); 1785 1786 Memory::Region D = frame.ResolveValue(value_operand, llvm_module); 1787 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module); 1788 1789 if (D.IsInvalid()) 1790 { 1791 if (log) 1792 log->Printf("StoreInst's value doesn't resolve to anything"); 1793 err.SetErrorToGenericError(); 1794 err.SetErrorString(bad_value_error); 1795 return false; 1796 } 1797 1798 if (P.IsInvalid()) 1799 { 1800 if (log) 1801 log->Printf("StoreInst's pointer doesn't resolve to anything"); 1802 err.SetErrorToGenericError(); 1803 err.SetErrorString(bad_value_error); 1804 return false; 1805 } 1806 1807 DataExtractorSP P_extractor(memory.GetExtractor(P)); 1808 DataExtractorSP D_extractor(memory.GetExtractor(D)); 1809 1810 if (!P_extractor || !D_extractor) 1811 return false; 1812 1813 lldb::offset_t offset = 0; 1814 lldb::addr_t pointer = P_extractor->GetAddress(&offset); 1815 1816 Memory::Region R = memory.Lookup(pointer, target_ty); 1817 1818 if (R.IsValid()) 1819 { 1820 if (!memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty))) 1821 { 1822 if (log) 1823 log->Printf("Couldn't write to a region on behalf of a LoadInst"); 1824 err.SetErrorToGenericError(); 1825 err.SetErrorString(memory_write_error); 1826 return false; 1827 } 1828 } 1829 else 1830 { 1831 if (!memory.WriteToRawPtr(pointer, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty))) 1832 { 1833 if (log) 1834 log->Printf("Couldn't write to a raw pointer on behalf of a LoadInst"); 1835 err.SetErrorToGenericError(); 1836 err.SetErrorString(memory_write_error); 1837 return false; 1838 } 1839 } 1840 1841 1842 if (log) 1843 { 1844 log->Printf("Interpreted a StoreInst"); 1845 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str()); 1846 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1847 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str()); 1848 } 1849 } 1850 break; 1851 } 1852 1853 ++frame.m_ii; 1854 } 1855 1856 if (num_insts >= 4096) 1857 { 1858 err.SetErrorToGenericError(); 1859 err.SetErrorString(infinite_loop_error); 1860 return false; 1861 } 1862 1863 return false; 1864 } 1865