IRInterpreter.cpp revision 0f0551e67d8ea8d63ace5456f7d42d951827b017
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 27using namespace llvm; 28 29static std::string 30PrintValue(const Value *value, bool truncate = false) 31{ 32 std::string s; 33 raw_string_ostream rso(s); 34 value->print(rso); 35 rso.flush(); 36 if (truncate) 37 s.resize(s.length() - 1); 38 39 size_t offset; 40 while ((offset = s.find('\n')) != s.npos) 41 s.erase(offset, 1); 42 while (s[0] == ' ' || s[0] == '\t') 43 s.erase(0, 1); 44 45 return s; 46} 47 48static std::string 49PrintType(const Type *type, bool truncate = false) 50{ 51 std::string s; 52 raw_string_ostream rso(s); 53 type->print(rso); 54 rso.flush(); 55 if (truncate) 56 s.resize(s.length() - 1); 57 return s; 58} 59 60class InterpreterStackFrame 61{ 62public: 63 typedef std::map <const Value*, lldb::addr_t> ValueMap; 64 65 struct PlacedValue 66 { 67 lldb_private::Value lldb_value; 68 lldb::addr_t process_address; 69 size_t size; 70 71 PlacedValue (lldb_private::Value &_lldb_value, 72 lldb::addr_t _process_address, 73 size_t _size) : 74 lldb_value(_lldb_value), 75 process_address(_process_address), 76 size(_size) 77 { 78 } 79 }; 80 81 typedef std::vector <PlacedValue> PlacedValueVector; 82 83 ValueMap m_values; 84 PlacedValueVector m_placed_values; 85 DataLayout &m_target_data; 86 lldb_private::ClangExpressionDeclMap *m_decl_map; 87 lldb_private::IRMemoryMap &m_memory_map; 88 const BasicBlock *m_bb; 89 BasicBlock::const_iterator m_ii; 90 BasicBlock::const_iterator m_ie; 91 92 lldb::addr_t m_frame_process_address; 93 size_t m_frame_size; 94 lldb::addr_t m_stack_pointer; 95 96 lldb::ByteOrder m_byte_order; 97 size_t m_addr_byte_size; 98 99 InterpreterStackFrame (DataLayout &target_data, 100 lldb_private::ClangExpressionDeclMap *decl_map, 101 lldb_private::IRMemoryMap &memory_map) : 102 m_target_data (target_data), 103 m_decl_map (decl_map), 104 m_memory_map (memory_map) 105 { 106 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig); 107 m_addr_byte_size = (target_data.getPointerSize(0)); 108 109 m_frame_size = 512 * 1024; 110 111 lldb_private::Error alloc_error; 112 113 m_frame_process_address = memory_map.Malloc(m_frame_size, 114 m_addr_byte_size, 115 lldb::ePermissionsReadable | lldb::ePermissionsWritable, 116 lldb_private::IRMemoryMap::eAllocationPolicyMirror, 117 alloc_error); 118 119 if (alloc_error.Success()) 120 { 121 m_stack_pointer = m_frame_process_address + m_frame_size; 122 } 123 else 124 { 125 m_frame_process_address = LLDB_INVALID_ADDRESS; 126 m_stack_pointer = LLDB_INVALID_ADDRESS; 127 } 128 } 129 130 ~InterpreterStackFrame () 131 { 132 if (m_frame_process_address != LLDB_INVALID_ADDRESS) 133 { 134 lldb_private::Error free_error; 135 m_memory_map.Free(m_frame_process_address, free_error); 136 m_frame_process_address = LLDB_INVALID_ADDRESS; 137 } 138 } 139 140 void Jump (const BasicBlock *bb) 141 { 142 m_bb = bb; 143 m_ii = m_bb->begin(); 144 m_ie = m_bb->end(); 145 } 146 147 std::string SummarizeValue (const Value *value) 148 { 149 lldb_private::StreamString ss; 150 151 ss.Printf("%s", PrintValue(value).c_str()); 152 153 ValueMap::iterator i = m_values.find(value); 154 155 if (i != m_values.end()) 156 { 157 lldb::addr_t addr = i->second; 158 159 ss.Printf(" 0x%llx", (unsigned long long)addr); 160 } 161 162 return ss.GetString(); 163 } 164 165 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type) 166 { 167 size_t type_size = m_target_data.getTypeStoreSize(type); 168 169 switch (type_size) 170 { 171 case 1: 172 scalar = (uint8_t)u64value; 173 break; 174 case 2: 175 scalar = (uint16_t)u64value; 176 break; 177 case 4: 178 scalar = (uint32_t)u64value; 179 break; 180 case 8: 181 scalar = (uint64_t)u64value; 182 break; 183 default: 184 return false; 185 } 186 187 return true; 188 } 189 190 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module) 191 { 192 const Constant *constant = dyn_cast<Constant>(value); 193 194 if (constant) 195 { 196 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) 197 { 198 return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType()); 199 } 200 } 201 else 202 { 203 lldb::addr_t process_address = ResolveValue(value, module); 204 size_t value_size = m_target_data.getTypeStoreSize(value->getType()); 205 206 lldb_private::DataExtractor value_extractor; 207 lldb_private::Error extract_error; 208 209 m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error); 210 211 if (!extract_error.Success()) 212 return false; 213 214 lldb::offset_t offset = 0; 215 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size); 216 217 return AssignToMatchType(scalar, u64value, value->getType()); 218 } 219 220 return false; 221 } 222 223 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module) 224 { 225 lldb::addr_t process_address = ResolveValue (value, module); 226 227 if (process_address == LLDB_INVALID_ADDRESS) 228 return false; 229 230 lldb_private::Scalar cast_scalar; 231 232 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType())) 233 return false; 234 235 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType()); 236 237 lldb_private::DataBufferHeap buf(value_byte_size, 0); 238 239 lldb_private::Error get_data_error; 240 241 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error)) 242 return false; 243 244 lldb_private::Error write_error; 245 246 m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error); 247 248 return write_error.Success(); 249 } 250 251 bool ResolveConstantValue (APInt &value, const Constant *constant) 252 { 253 switch (constant->getValueID()) 254 { 255 default: 256 break; 257 case Value::ConstantIntVal: 258 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) 259 { 260 value = constant_int->getValue(); 261 return true; 262 } 263 break; 264 case Value::ConstantFPVal: 265 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant)) 266 { 267 value = constant_fp->getValueAPF().bitcastToAPInt(); 268 return true; 269 } 270 break; 271 case Value::ConstantExprVal: 272 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) 273 { 274 switch (constant_expr->getOpcode()) 275 { 276 default: 277 return false; 278 case Instruction::IntToPtr: 279 case Instruction::PtrToInt: 280 case Instruction::BitCast: 281 return ResolveConstantValue(value, constant_expr->getOperand(0)); 282 case Instruction::GetElementPtr: 283 { 284 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin(); 285 ConstantExpr::const_op_iterator op_end = constant_expr->op_end(); 286 287 Constant *base = dyn_cast<Constant>(*op_cursor); 288 289 if (!base) 290 return false; 291 292 if (!ResolveConstantValue(value, base)) 293 return false; 294 295 op_cursor++; 296 297 if (op_cursor == op_end) 298 return true; // no offset to apply! 299 300 SmallVector <Value *, 8> indices (op_cursor, op_end); 301 302 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices); 303 304 const bool is_signed = true; 305 value += APInt(value.getBitWidth(), offset, is_signed); 306 307 return true; 308 } 309 } 310 } 311 break; 312 case Value::ConstantPointerNullVal: 313 if (isa<ConstantPointerNull>(constant)) 314 { 315 value = APInt(m_target_data.getPointerSizeInBits(), 0); 316 return true; 317 } 318 break; 319 } 320 return false; 321 } 322 323 bool MakeArgument(const Argument *value, uint64_t address) 324 { 325 lldb::addr_t data_address = Malloc(value->getType()); 326 327 if (data_address == LLDB_INVALID_ADDRESS) 328 return false; 329 330 lldb_private::Error write_error; 331 332 m_memory_map.WritePointerToMemory(data_address, address, write_error); 333 334 if (!write_error.Success()) 335 { 336 lldb_private::Error free_error; 337 m_memory_map.Free(data_address, free_error); 338 return false; 339 } 340 341 m_values[value] = data_address; 342 343 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 344 345 if (log) 346 { 347 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str()); 348 log->Printf(" Data region : %llx", (unsigned long long)address); 349 log->Printf(" Ref region : %llx", (unsigned long long)data_address); 350 } 351 352 return true; 353 } 354 355 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant) 356 { 357 APInt resolved_value; 358 359 if (!ResolveConstantValue(resolved_value, constant)) 360 return false; 361 362 const uint64_t *raw_data = resolved_value.getRawData(); 363 364 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType()); 365 366 lldb_private::Error write_error; 367 368 m_memory_map.WriteMemory(process_address, (uint8_t*)raw_data, constant_size, write_error); 369 370 return write_error.Success(); 371 } 372 373 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment) 374 { 375 lldb::addr_t ret = m_stack_pointer; 376 377 ret -= size; 378 ret -= (ret % byte_alignment); 379 380 if (ret < m_frame_process_address) 381 return LLDB_INVALID_ADDRESS; 382 383 m_stack_pointer = ret; 384 return ret; 385 } 386 387 lldb::addr_t MallocPointer () 388 { 389 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment()); 390 } 391 392 lldb::addr_t Malloc (llvm::Type *type) 393 { 394 lldb_private::Error alloc_error; 395 396 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type)); 397 } 398 399 lldb::addr_t PlaceLLDBValue (const llvm::Value *value, lldb_private::Value lldb_value) 400 { 401 if (!m_decl_map) 402 return false; 403 404 lldb_private::Error alloc_error; 405 lldb_private::RegisterInfo *reg_info = lldb_value.GetRegisterInfo(); 406 407 lldb::addr_t ret; 408 409 size_t value_size = m_target_data.getTypeStoreSize(value->getType()); 410 size_t value_align = m_target_data.getPrefTypeAlignment(value->getType()); 411 412 if (reg_info && (reg_info->encoding == lldb::eEncodingVector)) 413 value_size = reg_info->byte_size; 414 415 if (!reg_info && (lldb_value.GetValueType() == lldb_private::Value::eValueTypeLoadAddress)) 416 return lldb_value.GetScalar().ULongLong(); 417 418 ret = Malloc(value_size, value_align); 419 420 if (ret == LLDB_INVALID_ADDRESS) 421 return LLDB_INVALID_ADDRESS; 422 423 lldb_private::DataBufferHeap buf(value_size, 0); 424 425 m_decl_map->ReadTarget(m_memory_map, buf.GetBytes(), lldb_value, value_size); 426 427 lldb_private::Error write_error; 428 429 m_memory_map.WriteMemory(ret, buf.GetBytes(), buf.GetByteSize(), write_error); 430 431 if (!write_error.Success()) 432 { 433 lldb_private::Error free_error; 434 m_memory_map.Free(ret, free_error); 435 return LLDB_INVALID_ADDRESS; 436 } 437 438 m_placed_values.push_back(PlacedValue(lldb_value, ret, value_size)); 439 440 return ret; 441 } 442 443 void RestoreLLDBValues () 444 { 445 if (!m_decl_map) 446 return; 447 448 for (PlacedValue &placed_value : m_placed_values) 449 { 450 lldb_private::DataBufferHeap buf(placed_value.size, 0); 451 452 lldb_private::Error read_error; 453 454 m_memory_map.ReadMemory(buf.GetBytes(), placed_value.process_address, buf.GetByteSize(), read_error); 455 456 if (read_error.Success()) 457 m_decl_map->WriteTarget(m_memory_map, placed_value.lldb_value, buf.GetBytes(), buf.GetByteSize()); 458 } 459 } 460 461 std::string PrintData (lldb::addr_t addr, llvm::Type *type) 462 { 463 size_t length = m_target_data.getTypeStoreSize(type); 464 465 lldb_private::DataBufferHeap buf(length, 0); 466 467 lldb_private::Error read_error; 468 469 m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error); 470 471 if (!read_error.Success()) 472 return std::string("<couldn't read data>"); 473 474 lldb_private::StreamString ss; 475 476 for (size_t i = 0; i < length; i++) 477 { 478 if ((!(i & 0xf)) && i) 479 ss.Printf("%02hhx - ", buf.GetBytes()[i]); 480 else 481 ss.Printf("%02hhx ", buf.GetBytes()[i]); 482 } 483 484 return ss.GetString(); 485 } 486 487 lldb::addr_t ResolveValue (const Value *value, Module &module) 488 { 489 ValueMap::iterator i = m_values.find(value); 490 491 if (i != m_values.end()) 492 return i->second; 493 494 // Fall back and allocate space [allocation type Alloca] 495 496 lldb::addr_t data_address = Malloc(value->getType()); 497 498 if (const Constant *constant = dyn_cast<Constant>(value)) 499 { 500 if (!ResolveConstant (data_address, constant)) 501 { 502 lldb_private::Error free_error; 503 m_memory_map.Free(data_address, free_error); 504 return LLDB_INVALID_ADDRESS; 505 } 506 } 507 508 m_values[value] = data_address; 509 return data_address; 510 511 const GlobalValue *global_value = dyn_cast<GlobalValue>(value); 512 513 // If the variable is indirected through the argument 514 // array then we need to build an extra level of indirection 515 // for it. This is the default; only magic arguments like 516 // "this", "self", and "_cmd" are direct. 517 bool variable_is_this = false; 518 519 // If the variable is a function pointer, we do not need to 520 // build an extra layer of indirection for it because it is 521 // accessed directly. 522 bool variable_is_function_address = false; 523 524 // Attempt to resolve the value using the program's data. 525 // If it is, the values to be created are: 526 // 527 // data_region - a region of memory in which the variable's data resides. 528 // ref_region - a region of memory in which its address (i.e., &var) resides. 529 // In the JIT case, this region would be a member of the struct passed in. 530 // pointer_region - a region of memory in which the address of the pointer 531 // resides. This is an IR-level variable. 532 do 533 { 534 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 535 536 lldb_private::Value resolved_value; 537 lldb_private::ClangExpressionVariable::FlagType flags = 0; 538 539 if (global_value) 540 { 541 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module); 542 543 if (!decl) 544 break; 545 546 if (isa<clang::FunctionDecl>(decl)) 547 variable_is_function_address = true; 548 549 resolved_value = m_decl_map->LookupDecl(decl, flags); 550 } 551 else 552 { 553 // Special-case "this", "self", and "_cmd" 554 555 std::string name_str = value->getName().str(); 556 557 if (name_str == "this" || 558 name_str == "self" || 559 name_str == "_cmd") 560 { 561 resolved_value = m_decl_map->GetSpecialValue(lldb_private::ConstString(name_str.c_str())); 562 variable_is_this = true; 563 } 564 } 565 566 if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void) 567 { 568 if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo) 569 { 570 if (variable_is_this) 571 { 572 lldb_private::Error alloc_error; 573 lldb::addr_t ref_addr = Malloc(value->getType()); 574 575 if (ref_addr == LLDB_INVALID_ADDRESS) 576 return LLDB_INVALID_ADDRESS; 577 578 lldb_private::Error write_error; 579 m_memory_map.WritePointerToMemory(ref_addr, resolved_value.GetScalar().ULongLong(), write_error); 580 581 if (!write_error.Success()) 582 return LLDB_INVALID_ADDRESS; 583 584 if (log) 585 { 586 log->Printf("Made an allocation for \"this\" register variable %s", PrintValue(value).c_str()); 587 log->Printf(" Data region : %llx", (unsigned long long)resolved_value.GetScalar().ULongLong()); 588 log->Printf(" Ref region : %llx", (unsigned long long)ref_addr); 589 } 590 591 m_values[value] = ref_addr; 592 return ref_addr; 593 } 594 else if (flags & lldb_private::ClangExpressionVariable::EVBareRegister) 595 { 596 lldb::addr_t data_address = PlaceLLDBValue(value, resolved_value); 597 598 if (!data_address) 599 return LLDB_INVALID_ADDRESS; 600 601 lldb::addr_t ref_address = MallocPointer(); 602 603 if (ref_address == LLDB_INVALID_ADDRESS) 604 { 605 lldb_private::Error free_error; 606 m_memory_map.Free(data_address, free_error); 607 return LLDB_INVALID_ADDRESS; 608 } 609 610 lldb_private::Error write_error; 611 612 m_memory_map.WritePointerToMemory(ref_address, data_address, write_error); 613 614 if (!write_error.Success()) 615 { 616 lldb_private::Error free_error; 617 m_memory_map.Free(data_address, free_error); 618 m_memory_map.Free(ref_address, free_error); 619 return LLDB_INVALID_ADDRESS; 620 } 621 622 if (log) 623 { 624 log->Printf("Made an allocation for bare register variable %s", PrintValue(value).c_str()); 625 log->Printf(" Data contents : %s", PrintData(data_address, value->getType()).c_str()); 626 log->Printf(" Data region : 0x%llx", (unsigned long long)data_address); 627 log->Printf(" Ref region : 0x%llx", (unsigned long long)ref_address); 628 } 629 630 m_values[value] = ref_address; 631 return ref_address; 632 } 633 else 634 { 635 lldb::addr_t data_address = PlaceLLDBValue(value, resolved_value); 636 637 if (data_address == LLDB_INVALID_ADDRESS) 638 return LLDB_INVALID_ADDRESS; 639 640 lldb::addr_t ref_address = MallocPointer(); 641 642 if (ref_address == LLDB_INVALID_ADDRESS) 643 { 644 lldb_private::Error free_error; 645 m_memory_map.Free(data_address, free_error); 646 return LLDB_INVALID_ADDRESS; 647 } 648 649 lldb::addr_t pointer_address = MallocPointer(); 650 651 if (pointer_address == LLDB_INVALID_ADDRESS) 652 { 653 lldb_private::Error free_error; 654 m_memory_map.Free(data_address, free_error); 655 m_memory_map.Free(ref_address, free_error); 656 return LLDB_INVALID_ADDRESS; 657 } 658 659 lldb_private::Error write_error; 660 661 m_memory_map.WritePointerToMemory(ref_address, data_address, write_error); 662 663 if (!write_error.Success()) 664 { 665 lldb_private::Error free_error; 666 m_memory_map.Free(data_address, free_error); 667 m_memory_map.Free(ref_address, free_error); 668 m_memory_map.Free(pointer_address, free_error); 669 return LLDB_INVALID_ADDRESS; 670 } 671 672 write_error.Clear(); 673 674 m_memory_map.WritePointerToMemory(pointer_address, ref_address, write_error); 675 676 if (!write_error.Success()) 677 { 678 lldb_private::Error free_error; 679 m_memory_map.Free(data_address, free_error); 680 m_memory_map.Free(ref_address, free_error); 681 m_memory_map.Free(pointer_address, free_error); 682 return LLDB_INVALID_ADDRESS; 683 } 684 685 if (log) 686 { 687 log->Printf("Made an allocation for ordinary register variable %s", PrintValue(value).c_str()); 688 log->Printf(" Data contents : %s", PrintData(data_address, value->getType()).c_str()); 689 log->Printf(" Data region : 0x%llx", (unsigned long long)data_address); 690 log->Printf(" Ref region : 0x%llx", (unsigned long long)ref_address); 691 log->Printf(" Pointer region : 0x%llx", (unsigned long long)pointer_address); 692 } 693 694 m_values[value] = pointer_address; 695 return pointer_address; 696 } 697 } 698 else 699 { 700 bool no_extra_redirect = (variable_is_this || variable_is_function_address); 701 702 lldb::addr_t data_address = PlaceLLDBValue(value, resolved_value); 703 704 if (data_address == LLDB_INVALID_ADDRESS) 705 return LLDB_INVALID_ADDRESS; 706 707 lldb::addr_t ref_address = MallocPointer(); 708 709 if (ref_address == LLDB_INVALID_ADDRESS) 710 { 711 lldb_private::Error free_error; 712 m_memory_map.Free(data_address, free_error); 713 return LLDB_INVALID_ADDRESS; 714 } 715 716 lldb::addr_t pointer_address = LLDB_INVALID_ADDRESS; 717 718 if (!no_extra_redirect) 719 { 720 pointer_address = MallocPointer(); 721 722 if (pointer_address == LLDB_INVALID_ADDRESS) 723 { 724 lldb_private::Error free_error; 725 m_memory_map.Free(data_address, free_error); 726 m_memory_map.Free(ref_address, free_error); 727 return LLDB_INVALID_ADDRESS; 728 } 729 } 730 731 lldb_private::Error write_error; 732 733 m_memory_map.WritePointerToMemory(ref_address, data_address, write_error); 734 735 if (!write_error.Success()) 736 { 737 lldb_private::Error free_error; 738 m_memory_map.Free(data_address, free_error); 739 m_memory_map.Free(ref_address, free_error); 740 if (pointer_address != LLDB_INVALID_ADDRESS) 741 m_memory_map.Free(pointer_address, free_error); 742 return LLDB_INVALID_ADDRESS; 743 } 744 745 if (!no_extra_redirect) 746 { 747 write_error.Clear(); 748 749 m_memory_map.WritePointerToMemory(pointer_address, ref_address, write_error); 750 751 if (!write_error.Success()) 752 { 753 lldb_private::Error free_error; 754 m_memory_map.Free(data_address, free_error); 755 m_memory_map.Free(ref_address, free_error); 756 if (pointer_address != LLDB_INVALID_ADDRESS) 757 m_memory_map.Free(pointer_address, free_error); 758 return LLDB_INVALID_ADDRESS; 759 } 760 } 761 762 if (log) 763 { 764 log->Printf("Made an allocation for %s", PrintValue(value).c_str()); 765 log->Printf(" Data contents : %s", PrintData(data_address, value->getType()).c_str()); 766 log->Printf(" Data region : %llx", (unsigned long long)data_address); 767 log->Printf(" Ref region : %llx", (unsigned long long)ref_address); 768 if (!variable_is_this) 769 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_address); 770 } 771 772 if (no_extra_redirect) 773 { 774 m_values[value] = ref_address; 775 return ref_address; 776 } 777 else 778 { 779 m_values[value] = pointer_address; 780 return pointer_address; 781 } 782 } 783 } 784 } 785 while(0); 786 } 787 788 bool ConstructResult (lldb::ClangExpressionVariableSP &result, 789 const GlobalValue *result_value, 790 const lldb_private::ConstString &result_name, 791 lldb_private::TypeFromParser result_type, 792 Module &module) 793 { 794 if (!m_decl_map) 795 return false; 796 797 // The result_value resolves to P, a pointer to a region R containing the result data. 798 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process. 799 800 if (!result_value) 801 return true; // There was no slot for a result – the expression doesn't return one. 802 803 ValueMap::iterator i = m_values.find(result_value); 804 805 if (i == m_values.end()) 806 return false; // There was a slot for the result, but we didn't write into it. 807 808 lldb::addr_t P = i->second; 809 810 Type *pointer_ty = result_value->getType(); 811 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 812 if (!pointer_ptr_ty) 813 return false; 814 Type *R_ty = pointer_ptr_ty->getElementType(); 815 816 lldb_private::Error read_error; 817 lldb::addr_t R; 818 m_memory_map.ReadPointerFromMemory(&R, P, read_error); 819 if (!read_error.Success()) 820 return false; 821 822 lldb_private::Value base; 823 824 bool transient = false; 825 bool maybe_make_load = false; 826 827 if (m_decl_map->ResultIsReference(result_name)) 828 { 829 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty); 830 if (!R_ptr_ty) 831 return false; 832 833 read_error.Clear(); 834 lldb::addr_t R_pointer; 835 m_memory_map.ReadPointerFromMemory(&R_pointer, R, read_error); 836 if (!read_error.Success()) 837 return false; 838 839 // We got a bare pointer. We are going to treat it as a load address 840 // or a file address, letting decl_map make the choice based on whether 841 // or not a process exists. 842 843 bool was_placed = false; 844 845 for (PlacedValue &value : m_placed_values) 846 { 847 if (value.process_address == R_pointer) 848 { 849 base = value.lldb_value; 850 was_placed = true; 851 break; 852 } 853 } 854 855 if (!was_placed) 856 { 857 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 858 base.SetValueType(lldb_private::Value::eValueTypeFileAddress); 859 base.GetScalar() = (unsigned long long)R_pointer; 860 maybe_make_load = true; 861 } 862 } 863 else 864 { 865 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL); 866 base.SetValueType(lldb_private::Value::eValueTypeLoadAddress); 867 base.GetScalar() = (unsigned long long)R; 868 } 869 870 return m_decl_map->CompleteResultVariable (result, m_memory_map, base, result_name, result_type, transient, maybe_make_load); 871 } 872}; 873 874bool 875IRInterpreter::maybeRunOnFunction (lldb_private::ClangExpressionDeclMap *decl_map, 876 lldb_private::IRMemoryMap &memory_map, 877 lldb_private::Stream *error_stream, 878 lldb::ClangExpressionVariableSP &result, 879 const lldb_private::ConstString &result_name, 880 lldb_private::TypeFromParser result_type, 881 Function &llvm_function, 882 Module &llvm_module, 883 lldb_private::Error &err) 884{ 885 if (supportsFunction (llvm_function, err)) 886 return runOnFunction(decl_map, 887 memory_map, 888 error_stream, 889 result, 890 result_name, 891 result_type, 892 llvm_function, 893 llvm_module, 894 err); 895 else 896 return false; 897} 898 899static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes"; 900//static const char *interpreter_initialization_error = "Interpreter couldn't be initialized"; 901static const char *interpreter_internal_error = "Interpreter encountered an internal error"; 902static const char *bad_value_error = "Interpreter couldn't resolve a value during execution"; 903static const char *memory_allocation_error = "Interpreter couldn't allocate memory"; 904static const char *memory_write_error = "Interpreter couldn't write to memory"; 905static const char *memory_read_error = "Interpreter couldn't read from memory"; 906static const char *infinite_loop_error = "Interpreter ran for too many cycles"; 907static const char *bad_result_error = "Result of expression is in bad memory"; 908 909bool 910IRInterpreter::supportsFunction (Function &llvm_function, 911 lldb_private::Error &err) 912{ 913 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 914 915 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end(); 916 bbi != bbe; 917 ++bbi) 918 { 919 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end(); 920 ii != ie; 921 ++ii) 922 { 923 switch (ii->getOpcode()) 924 { 925 default: 926 { 927 if (log) 928 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str()); 929 err.SetErrorToGenericError(); 930 err.SetErrorString(unsupported_opcode_error); 931 return false; 932 } 933 case Instruction::Add: 934 case Instruction::Alloca: 935 case Instruction::BitCast: 936 case Instruction::Br: 937 case Instruction::GetElementPtr: 938 break; 939 case Instruction::ICmp: 940 { 941 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii); 942 943 if (!icmp_inst) 944 { 945 err.SetErrorToGenericError(); 946 err.SetErrorString(interpreter_internal_error); 947 return false; 948 } 949 950 switch (icmp_inst->getPredicate()) 951 { 952 default: 953 { 954 if (log) 955 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str()); 956 957 err.SetErrorToGenericError(); 958 err.SetErrorString(unsupported_opcode_error); 959 return false; 960 } 961 case CmpInst::ICMP_EQ: 962 case CmpInst::ICMP_NE: 963 case CmpInst::ICMP_UGT: 964 case CmpInst::ICMP_UGE: 965 case CmpInst::ICMP_ULT: 966 case CmpInst::ICMP_ULE: 967 case CmpInst::ICMP_SGT: 968 case CmpInst::ICMP_SGE: 969 case CmpInst::ICMP_SLT: 970 case CmpInst::ICMP_SLE: 971 break; 972 } 973 } 974 break; 975 case Instruction::And: 976 case Instruction::AShr: 977 case Instruction::IntToPtr: 978 case Instruction::PtrToInt: 979 case Instruction::Load: 980 case Instruction::LShr: 981 case Instruction::Mul: 982 case Instruction::Or: 983 case Instruction::Ret: 984 case Instruction::SDiv: 985 case Instruction::Shl: 986 case Instruction::SRem: 987 case Instruction::Store: 988 case Instruction::Sub: 989 case Instruction::UDiv: 990 case Instruction::URem: 991 case Instruction::Xor: 992 case Instruction::ZExt: 993 break; 994 } 995 } 996 } 997 998 return true; 999} 1000 1001bool 1002IRInterpreter::runOnFunction (lldb_private::ClangExpressionDeclMap *decl_map, 1003 lldb_private::IRMemoryMap &memory_map, 1004 lldb_private::Stream *error_stream, 1005 lldb::ClangExpressionVariableSP &result, 1006 const lldb_private::ConstString &result_name, 1007 lldb_private::TypeFromParser result_type, 1008 Function &llvm_function, 1009 Module &llvm_module, 1010 lldb_private::Error &err) 1011{ 1012 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1013 1014 DataLayout target_data(&llvm_module); 1015 1016 InterpreterStackFrame frame(target_data, decl_map, memory_map); 1017 1018 uint32_t num_insts = 0; 1019 1020 frame.Jump(llvm_function.begin()); 1021 1022 while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) 1023 { 1024 const Instruction *inst = frame.m_ii; 1025 1026 if (log) 1027 log->Printf("Interpreting %s", PrintValue(inst).c_str()); 1028 1029 switch (inst->getOpcode()) 1030 { 1031 default: 1032 break; 1033 case Instruction::Add: 1034 case Instruction::Sub: 1035 case Instruction::Mul: 1036 case Instruction::SDiv: 1037 case Instruction::UDiv: 1038 case Instruction::SRem: 1039 case Instruction::URem: 1040 case Instruction::Shl: 1041 case Instruction::LShr: 1042 case Instruction::AShr: 1043 case Instruction::And: 1044 case Instruction::Or: 1045 case Instruction::Xor: 1046 { 1047 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst); 1048 1049 if (!bin_op) 1050 { 1051 if (log) 1052 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName()); 1053 err.SetErrorToGenericError(); 1054 err.SetErrorString(interpreter_internal_error); 1055 return false; 1056 } 1057 1058 Value *lhs = inst->getOperand(0); 1059 Value *rhs = inst->getOperand(1); 1060 1061 lldb_private::Scalar L; 1062 lldb_private::Scalar R; 1063 1064 if (!frame.EvaluateValue(L, lhs, llvm_module)) 1065 { 1066 if (log) 1067 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1068 err.SetErrorToGenericError(); 1069 err.SetErrorString(bad_value_error); 1070 return false; 1071 } 1072 1073 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1074 { 1075 if (log) 1076 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1077 err.SetErrorToGenericError(); 1078 err.SetErrorString(bad_value_error); 1079 return false; 1080 } 1081 1082 lldb_private::Scalar result; 1083 1084 switch (inst->getOpcode()) 1085 { 1086 default: 1087 break; 1088 case Instruction::Add: 1089 result = L + R; 1090 break; 1091 case Instruction::Mul: 1092 result = L * R; 1093 break; 1094 case Instruction::Sub: 1095 result = L - R; 1096 break; 1097 case Instruction::SDiv: 1098 result = L / R; 1099 break; 1100 case Instruction::UDiv: 1101 result = L.GetRawBits64(0) / R.GetRawBits64(1); 1102 break; 1103 case Instruction::SRem: 1104 result = L % R; 1105 break; 1106 case Instruction::URem: 1107 result = L.GetRawBits64(0) % R.GetRawBits64(1); 1108 break; 1109 case Instruction::Shl: 1110 result = L << R; 1111 break; 1112 case Instruction::AShr: 1113 result = L >> R; 1114 break; 1115 case Instruction::LShr: 1116 result = L; 1117 result.ShiftRightLogical(R); 1118 break; 1119 case Instruction::And: 1120 result = L & R; 1121 break; 1122 case Instruction::Or: 1123 result = L | R; 1124 break; 1125 case Instruction::Xor: 1126 result = L ^ R; 1127 break; 1128 } 1129 1130 frame.AssignValue(inst, result, llvm_module); 1131 1132 if (log) 1133 { 1134 log->Printf("Interpreted a %s", inst->getOpcodeName()); 1135 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1136 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1137 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1138 } 1139 } 1140 break; 1141 case Instruction::Alloca: 1142 { 1143 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst); 1144 1145 if (!alloca_inst) 1146 { 1147 if (log) 1148 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst"); 1149 err.SetErrorToGenericError(); 1150 err.SetErrorString(interpreter_internal_error); 1151 return false; 1152 } 1153 1154 if (alloca_inst->isArrayAllocation()) 1155 { 1156 if (log) 1157 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true"); 1158 err.SetErrorToGenericError(); 1159 err.SetErrorString(unsupported_opcode_error); 1160 return false; 1161 } 1162 1163 // The semantics of Alloca are: 1164 // Create a region R of virtual memory of type T, backed by a data buffer 1165 // Create a region P of virtual memory of type T*, backed by a data buffer 1166 // Write the virtual address of R into P 1167 1168 Type *T = alloca_inst->getAllocatedType(); 1169 Type *Tptr = alloca_inst->getType(); 1170 1171 lldb::addr_t R = frame.Malloc(T); 1172 1173 if (R == LLDB_INVALID_ADDRESS) 1174 { 1175 if (log) 1176 log->Printf("Couldn't allocate memory for an AllocaInst"); 1177 err.SetErrorToGenericError(); 1178 err.SetErrorString(memory_allocation_error); 1179 return false; 1180 } 1181 1182 lldb::addr_t P = frame.Malloc(Tptr); 1183 1184 if (P == LLDB_INVALID_ADDRESS) 1185 { 1186 if (log) 1187 log->Printf("Couldn't allocate the result pointer for an AllocaInst"); 1188 err.SetErrorToGenericError(); 1189 err.SetErrorString(memory_allocation_error); 1190 return false; 1191 } 1192 1193 lldb_private::Error write_error; 1194 1195 memory_map.WritePointerToMemory(P, R, write_error); 1196 1197 if (!write_error.Success()) 1198 { 1199 if (log) 1200 log->Printf("Couldn't write the result pointer for an AllocaInst"); 1201 err.SetErrorToGenericError(); 1202 err.SetErrorString(memory_write_error); 1203 lldb_private::Error free_error; 1204 memory_map.Free(P, free_error); 1205 memory_map.Free(R, free_error); 1206 return false; 1207 } 1208 1209 frame.m_values[alloca_inst] = P; 1210 1211 if (log) 1212 { 1213 log->Printf("Interpreted an AllocaInst"); 1214 log->Printf(" R : 0x%llx", R); 1215 log->Printf(" P : 0x%llx", P); 1216 } 1217 } 1218 break; 1219 case Instruction::BitCast: 1220 case Instruction::ZExt: 1221 { 1222 const CastInst *cast_inst = dyn_cast<CastInst>(inst); 1223 1224 if (!cast_inst) 1225 { 1226 if (log) 1227 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName()); 1228 err.SetErrorToGenericError(); 1229 err.SetErrorString(interpreter_internal_error); 1230 return false; 1231 } 1232 1233 Value *source = cast_inst->getOperand(0); 1234 1235 lldb_private::Scalar S; 1236 1237 if (!frame.EvaluateValue(S, source, llvm_module)) 1238 { 1239 if (log) 1240 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); 1241 err.SetErrorToGenericError(); 1242 err.SetErrorString(bad_value_error); 1243 return false; 1244 } 1245 1246 frame.AssignValue(inst, S, llvm_module); 1247 } 1248 break; 1249 case Instruction::Br: 1250 { 1251 const BranchInst *br_inst = dyn_cast<BranchInst>(inst); 1252 1253 if (!br_inst) 1254 { 1255 if (log) 1256 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst"); 1257 err.SetErrorToGenericError(); 1258 err.SetErrorString(interpreter_internal_error); 1259 return false; 1260 } 1261 1262 if (br_inst->isConditional()) 1263 { 1264 Value *condition = br_inst->getCondition(); 1265 1266 lldb_private::Scalar C; 1267 1268 if (!frame.EvaluateValue(C, condition, llvm_module)) 1269 { 1270 if (log) 1271 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str()); 1272 err.SetErrorToGenericError(); 1273 err.SetErrorString(bad_value_error); 1274 return false; 1275 } 1276 1277 if (C.GetRawBits64(0)) 1278 frame.Jump(br_inst->getSuccessor(0)); 1279 else 1280 frame.Jump(br_inst->getSuccessor(1)); 1281 1282 if (log) 1283 { 1284 log->Printf("Interpreted a BrInst with a condition"); 1285 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str()); 1286 } 1287 } 1288 else 1289 { 1290 frame.Jump(br_inst->getSuccessor(0)); 1291 1292 if (log) 1293 { 1294 log->Printf("Interpreted a BrInst with no condition"); 1295 } 1296 } 1297 } 1298 continue; 1299 case Instruction::GetElementPtr: 1300 { 1301 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst); 1302 1303 if (!gep_inst) 1304 { 1305 if (log) 1306 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst"); 1307 err.SetErrorToGenericError(); 1308 err.SetErrorString(interpreter_internal_error); 1309 return false; 1310 } 1311 1312 const Value *pointer_operand = gep_inst->getPointerOperand(); 1313 Type *pointer_type = pointer_operand->getType(); 1314 1315 lldb_private::Scalar P; 1316 1317 if (!frame.EvaluateValue(P, pointer_operand, llvm_module)) 1318 { 1319 if (log) 1320 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str()); 1321 err.SetErrorToGenericError(); 1322 err.SetErrorString(bad_value_error); 1323 return false; 1324 } 1325 1326 typedef SmallVector <Value *, 8> IndexVector; 1327 typedef IndexVector::iterator IndexIterator; 1328 1329 SmallVector <Value *, 8> indices (gep_inst->idx_begin(), 1330 gep_inst->idx_end()); 1331 1332 SmallVector <Value *, 8> const_indices; 1333 1334 for (IndexIterator ii = indices.begin(), ie = indices.end(); 1335 ii != ie; 1336 ++ii) 1337 { 1338 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii); 1339 1340 if (!constant_index) 1341 { 1342 lldb_private::Scalar I; 1343 1344 if (!frame.EvaluateValue(I, *ii, llvm_module)) 1345 { 1346 if (log) 1347 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str()); 1348 err.SetErrorToGenericError(); 1349 err.SetErrorString(bad_value_error); 1350 return false; 1351 } 1352 1353 if (log) 1354 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS)); 1355 1356 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS))); 1357 } 1358 1359 const_indices.push_back(constant_index); 1360 } 1361 1362 uint64_t offset = target_data.getIndexedOffset(pointer_type, const_indices); 1363 1364 lldb_private::Scalar Poffset = P + offset; 1365 1366 frame.AssignValue(inst, Poffset, llvm_module); 1367 1368 if (log) 1369 { 1370 log->Printf("Interpreted a GetElementPtrInst"); 1371 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 1372 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str()); 1373 } 1374 } 1375 break; 1376 case Instruction::ICmp: 1377 { 1378 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst); 1379 1380 if (!icmp_inst) 1381 { 1382 if (log) 1383 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst"); 1384 err.SetErrorToGenericError(); 1385 err.SetErrorString(interpreter_internal_error); 1386 return false; 1387 } 1388 1389 CmpInst::Predicate predicate = icmp_inst->getPredicate(); 1390 1391 Value *lhs = inst->getOperand(0); 1392 Value *rhs = inst->getOperand(1); 1393 1394 lldb_private::Scalar L; 1395 lldb_private::Scalar R; 1396 1397 if (!frame.EvaluateValue(L, lhs, llvm_module)) 1398 { 1399 if (log) 1400 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1401 err.SetErrorToGenericError(); 1402 err.SetErrorString(bad_value_error); 1403 return false; 1404 } 1405 1406 if (!frame.EvaluateValue(R, rhs, llvm_module)) 1407 { 1408 if (log) 1409 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1410 err.SetErrorToGenericError(); 1411 err.SetErrorString(bad_value_error); 1412 return false; 1413 } 1414 1415 lldb_private::Scalar result; 1416 1417 switch (predicate) 1418 { 1419 default: 1420 return false; 1421 case CmpInst::ICMP_EQ: 1422 result = (L == R); 1423 break; 1424 case CmpInst::ICMP_NE: 1425 result = (L != R); 1426 break; 1427 case CmpInst::ICMP_UGT: 1428 result = (L.GetRawBits64(0) > R.GetRawBits64(0)); 1429 break; 1430 case CmpInst::ICMP_UGE: 1431 result = (L.GetRawBits64(0) >= R.GetRawBits64(0)); 1432 break; 1433 case CmpInst::ICMP_ULT: 1434 result = (L.GetRawBits64(0) < R.GetRawBits64(0)); 1435 break; 1436 case CmpInst::ICMP_ULE: 1437 result = (L.GetRawBits64(0) <= R.GetRawBits64(0)); 1438 break; 1439 case CmpInst::ICMP_SGT: 1440 result = (L > R); 1441 break; 1442 case CmpInst::ICMP_SGE: 1443 result = (L >= R); 1444 break; 1445 case CmpInst::ICMP_SLT: 1446 result = (L < R); 1447 break; 1448 case CmpInst::ICMP_SLE: 1449 result = (L <= R); 1450 break; 1451 } 1452 1453 frame.AssignValue(inst, result, llvm_module); 1454 1455 if (log) 1456 { 1457 log->Printf("Interpreted an ICmpInst"); 1458 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1459 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1460 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1461 } 1462 } 1463 break; 1464 case Instruction::IntToPtr: 1465 { 1466 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst); 1467 1468 if (!int_to_ptr_inst) 1469 { 1470 if (log) 1471 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst"); 1472 err.SetErrorToGenericError(); 1473 err.SetErrorString(interpreter_internal_error); 1474 return false; 1475 } 1476 1477 Value *src_operand = int_to_ptr_inst->getOperand(0); 1478 1479 lldb_private::Scalar I; 1480 1481 if (!frame.EvaluateValue(I, src_operand, llvm_module)) 1482 { 1483 if (log) 1484 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1485 err.SetErrorToGenericError(); 1486 err.SetErrorString(bad_value_error); 1487 return false; 1488 } 1489 1490 frame.AssignValue(inst, I, llvm_module); 1491 1492 if (log) 1493 { 1494 log->Printf("Interpreted an IntToPtr"); 1495 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1496 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1497 } 1498 } 1499 break; 1500 case Instruction::PtrToInt: 1501 { 1502 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst); 1503 1504 if (!ptr_to_int_inst) 1505 { 1506 if (log) 1507 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst"); 1508 err.SetErrorToGenericError(); 1509 err.SetErrorString(interpreter_internal_error); 1510 return false; 1511 } 1512 1513 Value *src_operand = ptr_to_int_inst->getOperand(0); 1514 1515 lldb_private::Scalar I; 1516 1517 if (!frame.EvaluateValue(I, src_operand, llvm_module)) 1518 { 1519 if (log) 1520 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1521 err.SetErrorToGenericError(); 1522 err.SetErrorString(bad_value_error); 1523 return false; 1524 } 1525 1526 frame.AssignValue(inst, I, llvm_module); 1527 1528 if (log) 1529 { 1530 log->Printf("Interpreted a PtrToInt"); 1531 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1532 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1533 } 1534 } 1535 break; 1536 case Instruction::Load: 1537 { 1538 const LoadInst *load_inst = dyn_cast<LoadInst>(inst); 1539 1540 if (!load_inst) 1541 { 1542 if (log) 1543 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst"); 1544 err.SetErrorToGenericError(); 1545 err.SetErrorString(interpreter_internal_error); 1546 return false; 1547 } 1548 1549 // The semantics of Load are: 1550 // Create a region D that will contain the loaded data 1551 // Resolve the region P containing a pointer 1552 // Dereference P to get the region R that the data should be loaded from 1553 // Transfer a unit of type type(D) from R to D 1554 1555 const Value *pointer_operand = load_inst->getPointerOperand(); 1556 1557 Type *pointer_ty = pointer_operand->getType(); 1558 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1559 if (!pointer_ptr_ty) 1560 { 1561 if (log) 1562 log->Printf("getPointerOperand()->getType() is not a PointerType"); 1563 err.SetErrorToGenericError(); 1564 err.SetErrorString(interpreter_internal_error); 1565 return false; 1566 } 1567 Type *target_ty = pointer_ptr_ty->getElementType(); 1568 1569 lldb::addr_t D = frame.ResolveValue(load_inst, llvm_module); 1570 lldb::addr_t P = frame.ResolveValue(pointer_operand, llvm_module); 1571 1572 if (D == LLDB_INVALID_ADDRESS) 1573 { 1574 if (log) 1575 log->Printf("LoadInst's value doesn't resolve to anything"); 1576 err.SetErrorToGenericError(); 1577 err.SetErrorString(bad_value_error); 1578 return false; 1579 } 1580 1581 if (P == LLDB_INVALID_ADDRESS) 1582 { 1583 if (log) 1584 log->Printf("LoadInst's pointer doesn't resolve to anything"); 1585 err.SetErrorToGenericError(); 1586 err.SetErrorString(bad_value_error); 1587 return false; 1588 } 1589 1590 lldb::addr_t R; 1591 lldb_private::Error read_error; 1592 memory_map.ReadPointerFromMemory(&R, P, read_error); 1593 1594 if (!read_error.Success()) 1595 { 1596 if (log) 1597 log->Printf("Couldn't read the address to be loaded for a LoadInst"); 1598 err.SetErrorToGenericError(); 1599 err.SetErrorString(memory_read_error); 1600 return false; 1601 } 1602 1603 size_t target_size = target_data.getTypeStoreSize(target_ty); 1604 lldb_private::DataBufferHeap buffer(target_size, 0); 1605 1606 read_error.Clear(); 1607 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error); 1608 if (!read_error.Success()) 1609 { 1610 if (log) 1611 log->Printf("Couldn't read from a region on behalf of a LoadInst"); 1612 err.SetErrorToGenericError(); 1613 err.SetErrorString(memory_read_error); 1614 return false; 1615 } 1616 1617 lldb_private::Error write_error; 1618 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error); 1619 if (!write_error.Success()) 1620 { 1621 if (log) 1622 log->Printf("Couldn't write to a region on behalf of a LoadInst"); 1623 err.SetErrorToGenericError(); 1624 err.SetErrorString(memory_read_error); 1625 return false; 1626 } 1627 1628 if (log) 1629 { 1630 log->Printf("Interpreted a LoadInst"); 1631 log->Printf(" P : 0x%llx", P); 1632 log->Printf(" R : 0x%llx", R); 1633 log->Printf(" D : 0x%llx", D); 1634 } 1635 } 1636 break; 1637 case Instruction::Ret: 1638 { 1639 frame.RestoreLLDBValues(); 1640 1641 if (result_name.IsEmpty()) 1642 return true; 1643 1644 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString()); 1645 1646 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module)) 1647 { 1648 if (log) 1649 log->Printf("Couldn't construct the expression's result"); 1650 err.SetErrorToGenericError(); 1651 err.SetErrorString(bad_result_error); 1652 return false; 1653 } 1654 1655 return true; 1656 } 1657 case Instruction::Store: 1658 { 1659 const StoreInst *store_inst = dyn_cast<StoreInst>(inst); 1660 1661 if (!store_inst) 1662 { 1663 if (log) 1664 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst"); 1665 err.SetErrorToGenericError(); 1666 err.SetErrorString(interpreter_internal_error); 1667 return false; 1668 } 1669 1670 // The semantics of Store are: 1671 // Resolve the region D containing the data to be stored 1672 // Resolve the region P containing a pointer 1673 // Dereference P to get the region R that the data should be stored in 1674 // Transfer a unit of type type(D) from D to R 1675 1676 const Value *value_operand = store_inst->getValueOperand(); 1677 const Value *pointer_operand = store_inst->getPointerOperand(); 1678 1679 Type *pointer_ty = pointer_operand->getType(); 1680 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1681 if (!pointer_ptr_ty) 1682 return false; 1683 Type *target_ty = pointer_ptr_ty->getElementType(); 1684 1685 lldb::addr_t D = frame.ResolveValue(value_operand, llvm_module); 1686 lldb::addr_t P = frame.ResolveValue(pointer_operand, llvm_module); 1687 1688 if (D == LLDB_INVALID_ADDRESS) 1689 { 1690 if (log) 1691 log->Printf("StoreInst's value doesn't resolve to anything"); 1692 err.SetErrorToGenericError(); 1693 err.SetErrorString(bad_value_error); 1694 return false; 1695 } 1696 1697 if (P == LLDB_INVALID_ADDRESS) 1698 { 1699 if (log) 1700 log->Printf("StoreInst's pointer doesn't resolve to anything"); 1701 err.SetErrorToGenericError(); 1702 err.SetErrorString(bad_value_error); 1703 return false; 1704 } 1705 1706 lldb::addr_t R; 1707 lldb_private::Error read_error; 1708 memory_map.ReadPointerFromMemory(&R, P, read_error); 1709 1710 if (!read_error.Success()) 1711 { 1712 if (log) 1713 log->Printf("Couldn't read the address to be loaded for a LoadInst"); 1714 err.SetErrorToGenericError(); 1715 err.SetErrorString(memory_read_error); 1716 return false; 1717 } 1718 1719 size_t target_size = target_data.getTypeStoreSize(target_ty); 1720 lldb_private::DataBufferHeap buffer(target_size, 0); 1721 1722 read_error.Clear(); 1723 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error); 1724 if (!read_error.Success()) 1725 { 1726 if (log) 1727 log->Printf("Couldn't read from a region on behalf of a StoreInst"); 1728 err.SetErrorToGenericError(); 1729 err.SetErrorString(memory_read_error); 1730 return false; 1731 } 1732 1733 lldb_private::Error write_error; 1734 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error); 1735 if (!write_error.Success()) 1736 { 1737 if (log) 1738 log->Printf("Couldn't write to a region on behalf of a StoreInst"); 1739 err.SetErrorToGenericError(); 1740 err.SetErrorString(memory_read_error); 1741 return false; 1742 } 1743 1744 if (log) 1745 { 1746 log->Printf("Interpreted a StoreInst"); 1747 log->Printf(" D : 0x%llx", D); 1748 log->Printf(" P : 0x%llx", P); 1749 log->Printf(" R : 0x%llx", R); 1750 } 1751 } 1752 break; 1753 } 1754 1755 ++frame.m_ii; 1756 } 1757 1758 if (num_insts >= 4096) 1759 { 1760 err.SetErrorToGenericError(); 1761 err.SetErrorString(infinite_loop_error); 1762 return false; 1763 } 1764 1765 return false; 1766} 1767 1768// new api 1769 1770bool 1771IRInterpreter::CanInterpret (llvm::Module &module, 1772 llvm::Function &function, 1773 lldb_private::Error &error) 1774{ 1775 return supportsFunction(function, error); 1776} 1777 1778bool 1779IRInterpreter::Interpret (llvm::Module &module, 1780 llvm::Function &function, 1781 llvm::ArrayRef<lldb::addr_t> args, 1782 lldb_private::IRMemoryMap &memory_map, 1783 lldb_private::Error &error) 1784{ 1785 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1786 1787 if (log) 1788 { 1789 std::string s; 1790 raw_string_ostream oss(s); 1791 1792 module.print(oss, NULL); 1793 1794 oss.flush(); 1795 1796 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str()); 1797 } 1798 1799 DataLayout data_layout(&module); 1800 1801 InterpreterStackFrame frame(data_layout, NULL, memory_map); 1802 1803 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS) 1804 { 1805 error.SetErrorString("Couldn't allocate stack frame"); 1806 } 1807 1808 int arg_index = 0; 1809 1810 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end(); 1811 ai != ae; 1812 ++ai, ++arg_index) 1813 { 1814 if (args.size() < arg_index) 1815 { 1816 error.SetErrorString ("Not enough arguments passed in to function"); 1817 return false; 1818 } 1819 1820 lldb::addr_t ptr = args[arg_index]; 1821 1822 frame.MakeArgument(ai, ptr); 1823 } 1824 1825 uint32_t num_insts = 0; 1826 1827 frame.Jump(function.begin()); 1828 1829 while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) 1830 { 1831 const Instruction *inst = frame.m_ii; 1832 1833 if (log) 1834 log->Printf("Interpreting %s", PrintValue(inst).c_str()); 1835 1836 switch (inst->getOpcode()) 1837 { 1838 default: 1839 break; 1840 case Instruction::Add: 1841 case Instruction::Sub: 1842 case Instruction::Mul: 1843 case Instruction::SDiv: 1844 case Instruction::UDiv: 1845 case Instruction::SRem: 1846 case Instruction::URem: 1847 case Instruction::Shl: 1848 case Instruction::LShr: 1849 case Instruction::AShr: 1850 case Instruction::And: 1851 case Instruction::Or: 1852 case Instruction::Xor: 1853 { 1854 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst); 1855 1856 if (!bin_op) 1857 { 1858 if (log) 1859 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName()); 1860 error.SetErrorToGenericError(); 1861 error.SetErrorString(interpreter_internal_error); 1862 return false; 1863 } 1864 1865 Value *lhs = inst->getOperand(0); 1866 Value *rhs = inst->getOperand(1); 1867 1868 lldb_private::Scalar L; 1869 lldb_private::Scalar R; 1870 1871 if (!frame.EvaluateValue(L, lhs, module)) 1872 { 1873 if (log) 1874 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1875 error.SetErrorToGenericError(); 1876 error.SetErrorString(bad_value_error); 1877 return false; 1878 } 1879 1880 if (!frame.EvaluateValue(R, rhs, module)) 1881 { 1882 if (log) 1883 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1884 error.SetErrorToGenericError(); 1885 error.SetErrorString(bad_value_error); 1886 return false; 1887 } 1888 1889 lldb_private::Scalar result; 1890 1891 switch (inst->getOpcode()) 1892 { 1893 default: 1894 break; 1895 case Instruction::Add: 1896 result = L + R; 1897 break; 1898 case Instruction::Mul: 1899 result = L * R; 1900 break; 1901 case Instruction::Sub: 1902 result = L - R; 1903 break; 1904 case Instruction::SDiv: 1905 result = L / R; 1906 break; 1907 case Instruction::UDiv: 1908 result = L.GetRawBits64(0) / R.GetRawBits64(1); 1909 break; 1910 case Instruction::SRem: 1911 result = L % R; 1912 break; 1913 case Instruction::URem: 1914 result = L.GetRawBits64(0) % R.GetRawBits64(1); 1915 break; 1916 case Instruction::Shl: 1917 result = L << R; 1918 break; 1919 case Instruction::AShr: 1920 result = L >> R; 1921 break; 1922 case Instruction::LShr: 1923 result = L; 1924 result.ShiftRightLogical(R); 1925 break; 1926 case Instruction::And: 1927 result = L & R; 1928 break; 1929 case Instruction::Or: 1930 result = L | R; 1931 break; 1932 case Instruction::Xor: 1933 result = L ^ R; 1934 break; 1935 } 1936 1937 frame.AssignValue(inst, result, module); 1938 1939 if (log) 1940 { 1941 log->Printf("Interpreted a %s", inst->getOpcodeName()); 1942 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1943 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1944 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1945 } 1946 } 1947 break; 1948 case Instruction::Alloca: 1949 { 1950 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst); 1951 1952 if (!alloca_inst) 1953 { 1954 if (log) 1955 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst"); 1956 error.SetErrorToGenericError(); 1957 error.SetErrorString(interpreter_internal_error); 1958 return false; 1959 } 1960 1961 if (alloca_inst->isArrayAllocation()) 1962 { 1963 if (log) 1964 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true"); 1965 error.SetErrorToGenericError(); 1966 error.SetErrorString(unsupported_opcode_error); 1967 return false; 1968 } 1969 1970 // The semantics of Alloca are: 1971 // Create a region R of virtual memory of type T, backed by a data buffer 1972 // Create a region P of virtual memory of type T*, backed by a data buffer 1973 // Write the virtual address of R into P 1974 1975 Type *T = alloca_inst->getAllocatedType(); 1976 Type *Tptr = alloca_inst->getType(); 1977 1978 lldb::addr_t R = frame.Malloc(T); 1979 1980 if (R == LLDB_INVALID_ADDRESS) 1981 { 1982 if (log) 1983 log->Printf("Couldn't allocate memory for an AllocaInst"); 1984 error.SetErrorToGenericError(); 1985 error.SetErrorString(memory_allocation_error); 1986 return false; 1987 } 1988 1989 lldb::addr_t P = frame.Malloc(Tptr); 1990 1991 if (P == LLDB_INVALID_ADDRESS) 1992 { 1993 if (log) 1994 log->Printf("Couldn't allocate the result pointer for an AllocaInst"); 1995 error.SetErrorToGenericError(); 1996 error.SetErrorString(memory_allocation_error); 1997 return false; 1998 } 1999 2000 lldb_private::Error write_error; 2001 2002 memory_map.WritePointerToMemory(P, R, write_error); 2003 2004 if (!write_error.Success()) 2005 { 2006 if (log) 2007 log->Printf("Couldn't write the result pointer for an AllocaInst"); 2008 error.SetErrorToGenericError(); 2009 error.SetErrorString(memory_write_error); 2010 lldb_private::Error free_error; 2011 memory_map.Free(P, free_error); 2012 memory_map.Free(R, free_error); 2013 return false; 2014 } 2015 2016 frame.m_values[alloca_inst] = P; 2017 2018 if (log) 2019 { 2020 log->Printf("Interpreted an AllocaInst"); 2021 log->Printf(" R : 0x%llx", R); 2022 log->Printf(" P : 0x%llx", P); 2023 } 2024 } 2025 break; 2026 case Instruction::BitCast: 2027 case Instruction::ZExt: 2028 { 2029 const CastInst *cast_inst = dyn_cast<CastInst>(inst); 2030 2031 if (!cast_inst) 2032 { 2033 if (log) 2034 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName()); 2035 error.SetErrorToGenericError(); 2036 error.SetErrorString(interpreter_internal_error); 2037 return false; 2038 } 2039 2040 Value *source = cast_inst->getOperand(0); 2041 2042 lldb_private::Scalar S; 2043 2044 if (!frame.EvaluateValue(S, source, module)) 2045 { 2046 if (log) 2047 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); 2048 error.SetErrorToGenericError(); 2049 error.SetErrorString(bad_value_error); 2050 return false; 2051 } 2052 2053 frame.AssignValue(inst, S, module); 2054 } 2055 break; 2056 case Instruction::Br: 2057 { 2058 const BranchInst *br_inst = dyn_cast<BranchInst>(inst); 2059 2060 if (!br_inst) 2061 { 2062 if (log) 2063 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst"); 2064 error.SetErrorToGenericError(); 2065 error.SetErrorString(interpreter_internal_error); 2066 return false; 2067 } 2068 2069 if (br_inst->isConditional()) 2070 { 2071 Value *condition = br_inst->getCondition(); 2072 2073 lldb_private::Scalar C; 2074 2075 if (!frame.EvaluateValue(C, condition, module)) 2076 { 2077 if (log) 2078 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str()); 2079 error.SetErrorToGenericError(); 2080 error.SetErrorString(bad_value_error); 2081 return false; 2082 } 2083 2084 if (C.GetRawBits64(0)) 2085 frame.Jump(br_inst->getSuccessor(0)); 2086 else 2087 frame.Jump(br_inst->getSuccessor(1)); 2088 2089 if (log) 2090 { 2091 log->Printf("Interpreted a BrInst with a condition"); 2092 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str()); 2093 } 2094 } 2095 else 2096 { 2097 frame.Jump(br_inst->getSuccessor(0)); 2098 2099 if (log) 2100 { 2101 log->Printf("Interpreted a BrInst with no condition"); 2102 } 2103 } 2104 } 2105 continue; 2106 case Instruction::GetElementPtr: 2107 { 2108 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst); 2109 2110 if (!gep_inst) 2111 { 2112 if (log) 2113 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst"); 2114 error.SetErrorToGenericError(); 2115 error.SetErrorString(interpreter_internal_error); 2116 return false; 2117 } 2118 2119 const Value *pointer_operand = gep_inst->getPointerOperand(); 2120 Type *pointer_type = pointer_operand->getType(); 2121 2122 lldb_private::Scalar P; 2123 2124 if (!frame.EvaluateValue(P, pointer_operand, module)) 2125 { 2126 if (log) 2127 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str()); 2128 error.SetErrorToGenericError(); 2129 error.SetErrorString(bad_value_error); 2130 return false; 2131 } 2132 2133 typedef SmallVector <Value *, 8> IndexVector; 2134 typedef IndexVector::iterator IndexIterator; 2135 2136 SmallVector <Value *, 8> indices (gep_inst->idx_begin(), 2137 gep_inst->idx_end()); 2138 2139 SmallVector <Value *, 8> const_indices; 2140 2141 for (IndexIterator ii = indices.begin(), ie = indices.end(); 2142 ii != ie; 2143 ++ii) 2144 { 2145 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii); 2146 2147 if (!constant_index) 2148 { 2149 lldb_private::Scalar I; 2150 2151 if (!frame.EvaluateValue(I, *ii, module)) 2152 { 2153 if (log) 2154 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str()); 2155 error.SetErrorToGenericError(); 2156 error.SetErrorString(bad_value_error); 2157 return false; 2158 } 2159 2160 if (log) 2161 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS)); 2162 2163 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS))); 2164 } 2165 2166 const_indices.push_back(constant_index); 2167 } 2168 2169 uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices); 2170 2171 lldb_private::Scalar Poffset = P + offset; 2172 2173 frame.AssignValue(inst, Poffset, module); 2174 2175 if (log) 2176 { 2177 log->Printf("Interpreted a GetElementPtrInst"); 2178 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); 2179 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str()); 2180 } 2181 } 2182 break; 2183 case Instruction::ICmp: 2184 { 2185 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst); 2186 2187 if (!icmp_inst) 2188 { 2189 if (log) 2190 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst"); 2191 error.SetErrorToGenericError(); 2192 error.SetErrorString(interpreter_internal_error); 2193 return false; 2194 } 2195 2196 CmpInst::Predicate predicate = icmp_inst->getPredicate(); 2197 2198 Value *lhs = inst->getOperand(0); 2199 Value *rhs = inst->getOperand(1); 2200 2201 lldb_private::Scalar L; 2202 lldb_private::Scalar R; 2203 2204 if (!frame.EvaluateValue(L, lhs, module)) 2205 { 2206 if (log) 2207 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 2208 error.SetErrorToGenericError(); 2209 error.SetErrorString(bad_value_error); 2210 return false; 2211 } 2212 2213 if (!frame.EvaluateValue(R, rhs, module)) 2214 { 2215 if (log) 2216 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 2217 error.SetErrorToGenericError(); 2218 error.SetErrorString(bad_value_error); 2219 return false; 2220 } 2221 2222 lldb_private::Scalar result; 2223 2224 switch (predicate) 2225 { 2226 default: 2227 return false; 2228 case CmpInst::ICMP_EQ: 2229 result = (L == R); 2230 break; 2231 case CmpInst::ICMP_NE: 2232 result = (L != R); 2233 break; 2234 case CmpInst::ICMP_UGT: 2235 result = (L.GetRawBits64(0) > R.GetRawBits64(0)); 2236 break; 2237 case CmpInst::ICMP_UGE: 2238 result = (L.GetRawBits64(0) >= R.GetRawBits64(0)); 2239 break; 2240 case CmpInst::ICMP_ULT: 2241 result = (L.GetRawBits64(0) < R.GetRawBits64(0)); 2242 break; 2243 case CmpInst::ICMP_ULE: 2244 result = (L.GetRawBits64(0) <= R.GetRawBits64(0)); 2245 break; 2246 case CmpInst::ICMP_SGT: 2247 result = (L > R); 2248 break; 2249 case CmpInst::ICMP_SGE: 2250 result = (L >= R); 2251 break; 2252 case CmpInst::ICMP_SLT: 2253 result = (L < R); 2254 break; 2255 case CmpInst::ICMP_SLE: 2256 result = (L <= R); 2257 break; 2258 } 2259 2260 frame.AssignValue(inst, result, module); 2261 2262 if (log) 2263 { 2264 log->Printf("Interpreted an ICmpInst"); 2265 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 2266 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 2267 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 2268 } 2269 } 2270 break; 2271 case Instruction::IntToPtr: 2272 { 2273 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst); 2274 2275 if (!int_to_ptr_inst) 2276 { 2277 if (log) 2278 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst"); 2279 error.SetErrorToGenericError(); 2280 error.SetErrorString(interpreter_internal_error); 2281 return false; 2282 } 2283 2284 Value *src_operand = int_to_ptr_inst->getOperand(0); 2285 2286 lldb_private::Scalar I; 2287 2288 if (!frame.EvaluateValue(I, src_operand, module)) 2289 { 2290 if (log) 2291 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 2292 error.SetErrorToGenericError(); 2293 error.SetErrorString(bad_value_error); 2294 return false; 2295 } 2296 2297 frame.AssignValue(inst, I, module); 2298 2299 if (log) 2300 { 2301 log->Printf("Interpreted an IntToPtr"); 2302 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 2303 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 2304 } 2305 } 2306 break; 2307 case Instruction::PtrToInt: 2308 { 2309 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst); 2310 2311 if (!ptr_to_int_inst) 2312 { 2313 if (log) 2314 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst"); 2315 error.SetErrorToGenericError(); 2316 error.SetErrorString(interpreter_internal_error); 2317 return false; 2318 } 2319 2320 Value *src_operand = ptr_to_int_inst->getOperand(0); 2321 2322 lldb_private::Scalar I; 2323 2324 if (!frame.EvaluateValue(I, src_operand, module)) 2325 { 2326 if (log) 2327 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 2328 error.SetErrorToGenericError(); 2329 error.SetErrorString(bad_value_error); 2330 return false; 2331 } 2332 2333 frame.AssignValue(inst, I, module); 2334 2335 if (log) 2336 { 2337 log->Printf("Interpreted a PtrToInt"); 2338 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 2339 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 2340 } 2341 } 2342 break; 2343 case Instruction::Load: 2344 { 2345 const LoadInst *load_inst = dyn_cast<LoadInst>(inst); 2346 2347 if (!load_inst) 2348 { 2349 if (log) 2350 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst"); 2351 error.SetErrorToGenericError(); 2352 error.SetErrorString(interpreter_internal_error); 2353 return false; 2354 } 2355 2356 // The semantics of Load are: 2357 // Create a region D that will contain the loaded data 2358 // Resolve the region P containing a pointer 2359 // Dereference P to get the region R that the data should be loaded from 2360 // Transfer a unit of type type(D) from R to D 2361 2362 const Value *pointer_operand = load_inst->getPointerOperand(); 2363 2364 Type *pointer_ty = pointer_operand->getType(); 2365 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 2366 if (!pointer_ptr_ty) 2367 { 2368 if (log) 2369 log->Printf("getPointerOperand()->getType() is not a PointerType"); 2370 error.SetErrorToGenericError(); 2371 error.SetErrorString(interpreter_internal_error); 2372 return false; 2373 } 2374 Type *target_ty = pointer_ptr_ty->getElementType(); 2375 2376 lldb::addr_t D = frame.ResolveValue(load_inst, module); 2377 lldb::addr_t P = frame.ResolveValue(pointer_operand, module); 2378 2379 if (D == LLDB_INVALID_ADDRESS) 2380 { 2381 if (log) 2382 log->Printf("LoadInst's value doesn't resolve to anything"); 2383 error.SetErrorToGenericError(); 2384 error.SetErrorString(bad_value_error); 2385 return false; 2386 } 2387 2388 if (P == LLDB_INVALID_ADDRESS) 2389 { 2390 if (log) 2391 log->Printf("LoadInst's pointer doesn't resolve to anything"); 2392 error.SetErrorToGenericError(); 2393 error.SetErrorString(bad_value_error); 2394 return false; 2395 } 2396 2397 lldb::addr_t R; 2398 lldb_private::Error read_error; 2399 memory_map.ReadPointerFromMemory(&R, P, read_error); 2400 2401 if (!read_error.Success()) 2402 { 2403 if (log) 2404 log->Printf("Couldn't read the address to be loaded for a LoadInst"); 2405 error.SetErrorToGenericError(); 2406 error.SetErrorString(memory_read_error); 2407 return false; 2408 } 2409 2410 size_t target_size = data_layout.getTypeStoreSize(target_ty); 2411 lldb_private::DataBufferHeap buffer(target_size, 0); 2412 2413 read_error.Clear(); 2414 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error); 2415 if (!read_error.Success()) 2416 { 2417 if (log) 2418 log->Printf("Couldn't read from a region on behalf of a LoadInst"); 2419 error.SetErrorToGenericError(); 2420 error.SetErrorString(memory_read_error); 2421 return false; 2422 } 2423 2424 lldb_private::Error write_error; 2425 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error); 2426 if (!write_error.Success()) 2427 { 2428 if (log) 2429 log->Printf("Couldn't write to a region on behalf of a LoadInst"); 2430 error.SetErrorToGenericError(); 2431 error.SetErrorString(memory_read_error); 2432 return false; 2433 } 2434 2435 if (log) 2436 { 2437 log->Printf("Interpreted a LoadInst"); 2438 log->Printf(" P : 0x%llx", P); 2439 log->Printf(" R : 0x%llx", R); 2440 log->Printf(" D : 0x%llx", D); 2441 } 2442 } 2443 break; 2444 case Instruction::Ret: 2445 { 2446 return true; 2447 } 2448 case Instruction::Store: 2449 { 2450 const StoreInst *store_inst = dyn_cast<StoreInst>(inst); 2451 2452 if (!store_inst) 2453 { 2454 if (log) 2455 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst"); 2456 error.SetErrorToGenericError(); 2457 error.SetErrorString(interpreter_internal_error); 2458 return false; 2459 } 2460 2461 // The semantics of Store are: 2462 // Resolve the region D containing the data to be stored 2463 // Resolve the region P containing a pointer 2464 // Dereference P to get the region R that the data should be stored in 2465 // Transfer a unit of type type(D) from D to R 2466 2467 const Value *value_operand = store_inst->getValueOperand(); 2468 const Value *pointer_operand = store_inst->getPointerOperand(); 2469 2470 Type *pointer_ty = pointer_operand->getType(); 2471 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 2472 if (!pointer_ptr_ty) 2473 return false; 2474 Type *target_ty = pointer_ptr_ty->getElementType(); 2475 2476 lldb::addr_t D = frame.ResolveValue(value_operand, module); 2477 lldb::addr_t P = frame.ResolveValue(pointer_operand, module); 2478 2479 if (D == LLDB_INVALID_ADDRESS) 2480 { 2481 if (log) 2482 log->Printf("StoreInst's value doesn't resolve to anything"); 2483 error.SetErrorToGenericError(); 2484 error.SetErrorString(bad_value_error); 2485 return false; 2486 } 2487 2488 if (P == LLDB_INVALID_ADDRESS) 2489 { 2490 if (log) 2491 log->Printf("StoreInst's pointer doesn't resolve to anything"); 2492 error.SetErrorToGenericError(); 2493 error.SetErrorString(bad_value_error); 2494 return false; 2495 } 2496 2497 lldb::addr_t R; 2498 lldb_private::Error read_error; 2499 memory_map.ReadPointerFromMemory(&R, P, read_error); 2500 2501 if (!read_error.Success()) 2502 { 2503 if (log) 2504 log->Printf("Couldn't read the address to be loaded for a LoadInst"); 2505 error.SetErrorToGenericError(); 2506 error.SetErrorString(memory_read_error); 2507 return false; 2508 } 2509 2510 size_t target_size = data_layout.getTypeStoreSize(target_ty); 2511 lldb_private::DataBufferHeap buffer(target_size, 0); 2512 2513 read_error.Clear(); 2514 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error); 2515 if (!read_error.Success()) 2516 { 2517 if (log) 2518 log->Printf("Couldn't read from a region on behalf of a StoreInst"); 2519 error.SetErrorToGenericError(); 2520 error.SetErrorString(memory_read_error); 2521 return false; 2522 } 2523 2524 lldb_private::Error write_error; 2525 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error); 2526 if (!write_error.Success()) 2527 { 2528 if (log) 2529 log->Printf("Couldn't write to a region on behalf of a StoreInst"); 2530 error.SetErrorToGenericError(); 2531 error.SetErrorString(memory_read_error); 2532 return false; 2533 } 2534 2535 if (log) 2536 { 2537 log->Printf("Interpreted a StoreInst"); 2538 log->Printf(" D : 0x%llx", D); 2539 log->Printf(" P : 0x%llx", P); 2540 log->Printf(" R : 0x%llx", R); 2541 } 2542 } 2543 break; 2544 } 2545 2546 ++frame.m_ii; 2547 } 2548 2549 if (num_insts >= 4096) 2550 { 2551 error.SetErrorToGenericError(); 2552 error.SetErrorString(infinite_loop_error); 2553 return false; 2554 } 2555 2556 return false; 2557} 2558