1 //===-- IRInterpreter.cpp ---------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Expression/IRInterpreter.h" 10 #include "lldb/Core/Module.h" 11 #include "lldb/Core/ModuleSpec.h" 12 #include "lldb/Core/ValueObject.h" 13 #include "lldb/Expression/DiagnosticManager.h" 14 #include "lldb/Expression/IRExecutionUnit.h" 15 #include "lldb/Expression/IRMemoryMap.h" 16 #include "lldb/Utility/ConstString.h" 17 #include "lldb/Utility/DataExtractor.h" 18 #include "lldb/Utility/Endian.h" 19 #include "lldb/Utility/Log.h" 20 #include "lldb/Utility/Scalar.h" 21 #include "lldb/Utility/Status.h" 22 #include "lldb/Utility/StreamString.h" 23 24 #include "lldb/Target/ABI.h" 25 #include "lldb/Target/ExecutionContext.h" 26 #include "lldb/Target/Target.h" 27 #include "lldb/Target/Thread.h" 28 #include "lldb/Target/ThreadPlan.h" 29 #include "lldb/Target/ThreadPlanCallFunctionUsingABI.h" 30 31 #include "llvm/IR/Constants.h" 32 #include "llvm/IR/DataLayout.h" 33 #include "llvm/IR/Function.h" 34 #include "llvm/IR/Instructions.h" 35 #include "llvm/IR/Intrinsics.h" 36 #include "llvm/IR/LLVMContext.h" 37 #include "llvm/IR/Module.h" 38 #include "llvm/IR/Operator.h" 39 #include "llvm/Support/raw_ostream.h" 40 41 #include <map> 42 43 using namespace llvm; 44 45 static std::string PrintValue(const Value *value, bool truncate = false) { 46 std::string s; 47 raw_string_ostream rso(s); 48 value->print(rso); 49 rso.flush(); 50 if (truncate) 51 s.resize(s.length() - 1); 52 53 size_t offset; 54 while ((offset = s.find('\n')) != s.npos) 55 s.erase(offset, 1); 56 while (s[0] == ' ' || s[0] == '\t') 57 s.erase(0, 1); 58 59 return s; 60 } 61 62 static std::string PrintType(const Type *type, bool truncate = false) { 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 static bool CanIgnoreCall(const CallInst *call) { 73 const llvm::Function *called_function = call->getCalledFunction(); 74 75 if (!called_function) 76 return false; 77 78 if (called_function->isIntrinsic()) { 79 switch (called_function->getIntrinsicID()) { 80 default: 81 break; 82 case llvm::Intrinsic::dbg_declare: 83 case llvm::Intrinsic::dbg_value: 84 return true; 85 } 86 } 87 88 return false; 89 } 90 91 class InterpreterStackFrame { 92 public: 93 typedef std::map<const Value *, lldb::addr_t> ValueMap; 94 95 ValueMap m_values; 96 DataLayout &m_target_data; 97 lldb_private::IRExecutionUnit &m_execution_unit; 98 const BasicBlock *m_bb; 99 const BasicBlock *m_prev_bb; 100 BasicBlock::const_iterator m_ii; 101 BasicBlock::const_iterator m_ie; 102 103 lldb::addr_t m_frame_process_address; 104 size_t m_frame_size; 105 lldb::addr_t m_stack_pointer; 106 107 lldb::ByteOrder m_byte_order; 108 size_t m_addr_byte_size; 109 110 InterpreterStackFrame(DataLayout &target_data, 111 lldb_private::IRExecutionUnit &execution_unit, 112 lldb::addr_t stack_frame_bottom, 113 lldb::addr_t stack_frame_top) 114 : m_target_data(target_data), m_execution_unit(execution_unit), 115 m_bb(nullptr), m_prev_bb(nullptr) { 116 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle 117 : lldb::eByteOrderBig); 118 m_addr_byte_size = (target_data.getPointerSize(0)); 119 120 m_frame_process_address = stack_frame_bottom; 121 m_frame_size = stack_frame_top - stack_frame_bottom; 122 m_stack_pointer = stack_frame_top; 123 } 124 125 ~InterpreterStackFrame() {} 126 127 void Jump(const BasicBlock *bb) { 128 m_prev_bb = m_bb; 129 m_bb = bb; 130 m_ii = m_bb->begin(); 131 m_ie = m_bb->end(); 132 } 133 134 std::string SummarizeValue(const Value *value) { 135 lldb_private::StreamString ss; 136 137 ss.Printf("%s", PrintValue(value).c_str()); 138 139 ValueMap::iterator i = m_values.find(value); 140 141 if (i != m_values.end()) { 142 lldb::addr_t addr = i->second; 143 144 ss.Printf(" 0x%llx", (unsigned long long)addr); 145 } 146 147 return ss.GetString(); 148 } 149 150 bool AssignToMatchType(lldb_private::Scalar &scalar, uint64_t u64value, 151 Type *type) { 152 size_t type_size = m_target_data.getTypeStoreSize(type); 153 154 if (type_size > 8) 155 return false; 156 157 if (type_size != 1) 158 type_size = PowerOf2Ceil(type_size); 159 160 scalar = llvm::APInt(type_size*8, u64value); 161 return true; 162 } 163 164 bool EvaluateValue(lldb_private::Scalar &scalar, const Value *value, 165 Module &module) { 166 const Constant *constant = dyn_cast<Constant>(value); 167 168 if (constant) { 169 APInt value_apint; 170 171 if (!ResolveConstantValue(value_apint, constant)) 172 return false; 173 174 return AssignToMatchType(scalar, value_apint.getLimitedValue(), 175 value->getType()); 176 } else { 177 lldb::addr_t process_address = ResolveValue(value, module); 178 size_t value_size = m_target_data.getTypeStoreSize(value->getType()); 179 180 lldb_private::DataExtractor value_extractor; 181 lldb_private::Status extract_error; 182 183 m_execution_unit.GetMemoryData(value_extractor, process_address, 184 value_size, extract_error); 185 186 if (!extract_error.Success()) 187 return false; 188 189 lldb::offset_t offset = 0; 190 if (value_size <= 8) { 191 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size); 192 return AssignToMatchType(scalar, u64value, value->getType()); 193 } 194 } 195 196 return false; 197 } 198 199 bool AssignValue(const Value *value, lldb_private::Scalar &scalar, 200 Module &module) { 201 lldb::addr_t process_address = ResolveValue(value, module); 202 203 if (process_address == LLDB_INVALID_ADDRESS) 204 return false; 205 206 lldb_private::Scalar cast_scalar; 207 208 if (!AssignToMatchType(cast_scalar, scalar.ULongLong(), value->getType())) 209 return false; 210 211 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType()); 212 213 lldb_private::DataBufferHeap buf(value_byte_size, 0); 214 215 lldb_private::Status get_data_error; 216 217 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), 218 m_byte_order, get_data_error)) 219 return false; 220 221 lldb_private::Status write_error; 222 223 m_execution_unit.WriteMemory(process_address, buf.GetBytes(), 224 buf.GetByteSize(), write_error); 225 226 return write_error.Success(); 227 } 228 229 bool ResolveConstantValue(APInt &value, const Constant *constant) { 230 switch (constant->getValueID()) { 231 default: 232 break; 233 case Value::FunctionVal: 234 if (const Function *constant_func = dyn_cast<Function>(constant)) { 235 lldb_private::ConstString name(constant_func->getName()); 236 bool missing_weak = false; 237 lldb::addr_t addr = m_execution_unit.FindSymbol(name, missing_weak); 238 if (addr == LLDB_INVALID_ADDRESS || missing_weak) 239 return false; 240 value = APInt(m_target_data.getPointerSizeInBits(), addr); 241 return true; 242 } 243 break; 244 case Value::ConstantIntVal: 245 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) { 246 value = constant_int->getValue(); 247 return true; 248 } 249 break; 250 case Value::ConstantFPVal: 251 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant)) { 252 value = constant_fp->getValueAPF().bitcastToAPInt(); 253 return true; 254 } 255 break; 256 case Value::ConstantExprVal: 257 if (const ConstantExpr *constant_expr = 258 dyn_cast<ConstantExpr>(constant)) { 259 switch (constant_expr->getOpcode()) { 260 default: 261 return false; 262 case Instruction::IntToPtr: 263 case Instruction::PtrToInt: 264 case Instruction::BitCast: 265 return ResolveConstantValue(value, constant_expr->getOperand(0)); 266 case Instruction::GetElementPtr: { 267 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin(); 268 ConstantExpr::const_op_iterator op_end = constant_expr->op_end(); 269 270 Constant *base = dyn_cast<Constant>(*op_cursor); 271 272 if (!base) 273 return false; 274 275 if (!ResolveConstantValue(value, base)) 276 return false; 277 278 op_cursor++; 279 280 if (op_cursor == op_end) 281 return true; // no offset to apply! 282 283 SmallVector<Value *, 8> indices(op_cursor, op_end); 284 285 Type *src_elem_ty = 286 cast<GEPOperator>(constant_expr)->getSourceElementType(); 287 uint64_t offset = 288 m_target_data.getIndexedOffsetInType(src_elem_ty, indices); 289 290 const bool is_signed = true; 291 value += APInt(value.getBitWidth(), offset, is_signed); 292 293 return true; 294 } 295 } 296 } 297 break; 298 case Value::ConstantPointerNullVal: 299 if (isa<ConstantPointerNull>(constant)) { 300 value = APInt(m_target_data.getPointerSizeInBits(), 0); 301 return true; 302 } 303 break; 304 } 305 return false; 306 } 307 308 bool MakeArgument(const Argument *value, uint64_t address) { 309 lldb::addr_t data_address = Malloc(value->getType()); 310 311 if (data_address == LLDB_INVALID_ADDRESS) 312 return false; 313 314 lldb_private::Status write_error; 315 316 m_execution_unit.WritePointerToMemory(data_address, address, write_error); 317 318 if (!write_error.Success()) { 319 lldb_private::Status free_error; 320 m_execution_unit.Free(data_address, free_error); 321 return false; 322 } 323 324 m_values[value] = data_address; 325 326 lldb_private::Log *log( 327 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 328 329 if (log) { 330 log->Printf("Made an allocation for argument %s", 331 PrintValue(value).c_str()); 332 log->Printf(" Data region : %llx", (unsigned long long)address); 333 log->Printf(" Ref region : %llx", (unsigned long long)data_address); 334 } 335 336 return true; 337 } 338 339 bool ResolveConstant(lldb::addr_t process_address, const Constant *constant) { 340 APInt resolved_value; 341 342 if (!ResolveConstantValue(resolved_value, constant)) 343 return false; 344 345 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType()); 346 lldb_private::DataBufferHeap buf(constant_size, 0); 347 348 lldb_private::Status get_data_error; 349 350 lldb_private::Scalar resolved_scalar( 351 resolved_value.zextOrTrunc(llvm::NextPowerOf2(constant_size) * 8)); 352 if (!resolved_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), 353 m_byte_order, get_data_error)) 354 return false; 355 356 lldb_private::Status write_error; 357 358 m_execution_unit.WriteMemory(process_address, buf.GetBytes(), 359 buf.GetByteSize(), write_error); 360 361 return write_error.Success(); 362 } 363 364 lldb::addr_t Malloc(size_t size, uint8_t byte_alignment) { 365 lldb::addr_t ret = m_stack_pointer; 366 367 ret -= size; 368 ret -= (ret % byte_alignment); 369 370 if (ret < m_frame_process_address) 371 return LLDB_INVALID_ADDRESS; 372 373 m_stack_pointer = ret; 374 return ret; 375 } 376 377 lldb::addr_t Malloc(llvm::Type *type) { 378 lldb_private::Status alloc_error; 379 380 return Malloc(m_target_data.getTypeAllocSize(type), 381 m_target_data.getPrefTypeAlignment(type)); 382 } 383 384 std::string PrintData(lldb::addr_t addr, llvm::Type *type) { 385 size_t length = m_target_data.getTypeStoreSize(type); 386 387 lldb_private::DataBufferHeap buf(length, 0); 388 389 lldb_private::Status read_error; 390 391 m_execution_unit.ReadMemory(buf.GetBytes(), addr, length, read_error); 392 393 if (!read_error.Success()) 394 return std::string("<couldn't read data>"); 395 396 lldb_private::StreamString ss; 397 398 for (size_t i = 0; i < length; i++) { 399 if ((!(i & 0xf)) && i) 400 ss.Printf("%02hhx - ", buf.GetBytes()[i]); 401 else 402 ss.Printf("%02hhx ", buf.GetBytes()[i]); 403 } 404 405 return ss.GetString(); 406 } 407 408 lldb::addr_t ResolveValue(const Value *value, Module &module) { 409 ValueMap::iterator i = m_values.find(value); 410 411 if (i != m_values.end()) 412 return i->second; 413 414 // Fall back and allocate space [allocation type Alloca] 415 416 lldb::addr_t data_address = Malloc(value->getType()); 417 418 if (const Constant *constant = dyn_cast<Constant>(value)) { 419 if (!ResolveConstant(data_address, constant)) { 420 lldb_private::Status free_error; 421 m_execution_unit.Free(data_address, free_error); 422 return LLDB_INVALID_ADDRESS; 423 } 424 } 425 426 m_values[value] = data_address; 427 return data_address; 428 } 429 }; 430 431 static const char *unsupported_opcode_error = 432 "Interpreter doesn't handle one of the expression's opcodes"; 433 static const char *unsupported_operand_error = 434 "Interpreter doesn't handle one of the expression's operands"; 435 // static const char *interpreter_initialization_error = "Interpreter couldn't 436 // be initialized"; 437 static const char *interpreter_internal_error = 438 "Interpreter encountered an internal error"; 439 static const char *bad_value_error = 440 "Interpreter couldn't resolve a value during execution"; 441 static const char *memory_allocation_error = 442 "Interpreter couldn't allocate memory"; 443 static const char *memory_write_error = "Interpreter couldn't write to memory"; 444 static const char *memory_read_error = "Interpreter couldn't read from memory"; 445 static const char *infinite_loop_error = "Interpreter ran for too many cycles"; 446 // static const char *bad_result_error = "Result of expression 447 // is in bad memory"; 448 static const char *too_many_functions_error = 449 "Interpreter doesn't handle modules with multiple function bodies."; 450 451 static bool CanResolveConstant(llvm::Constant *constant) { 452 switch (constant->getValueID()) { 453 default: 454 return false; 455 case Value::ConstantIntVal: 456 case Value::ConstantFPVal: 457 case Value::FunctionVal: 458 return true; 459 case Value::ConstantExprVal: 460 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) { 461 switch (constant_expr->getOpcode()) { 462 default: 463 return false; 464 case Instruction::IntToPtr: 465 case Instruction::PtrToInt: 466 case Instruction::BitCast: 467 return CanResolveConstant(constant_expr->getOperand(0)); 468 case Instruction::GetElementPtr: { 469 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin(); 470 Constant *base = dyn_cast<Constant>(*op_cursor); 471 if (!base) 472 return false; 473 474 return CanResolveConstant(base); 475 } 476 } 477 } else { 478 return false; 479 } 480 case Value::ConstantPointerNullVal: 481 return true; 482 } 483 } 484 485 bool IRInterpreter::CanInterpret(llvm::Module &module, llvm::Function &function, 486 lldb_private::Status &error, 487 const bool support_function_calls) { 488 lldb_private::Log *log( 489 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 490 491 bool saw_function_with_body = false; 492 493 for (Module::iterator fi = module.begin(), fe = module.end(); fi != fe; 494 ++fi) { 495 if (fi->begin() != fi->end()) { 496 if (saw_function_with_body) { 497 if (log) 498 log->Printf("More than one function in the module has a body"); 499 error.SetErrorToGenericError(); 500 error.SetErrorString(too_many_functions_error); 501 return false; 502 } 503 saw_function_with_body = true; 504 } 505 } 506 507 for (Function::iterator bbi = function.begin(), bbe = function.end(); 508 bbi != bbe; ++bbi) { 509 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end(); ii != ie; 510 ++ii) { 511 switch (ii->getOpcode()) { 512 default: { 513 if (log) 514 log->Printf("Unsupported instruction: %s", PrintValue(&*ii).c_str()); 515 error.SetErrorToGenericError(); 516 error.SetErrorString(unsupported_opcode_error); 517 return false; 518 } 519 case Instruction::Add: 520 case Instruction::Alloca: 521 case Instruction::BitCast: 522 case Instruction::Br: 523 case Instruction::PHI: 524 break; 525 case Instruction::Call: { 526 CallInst *call_inst = dyn_cast<CallInst>(ii); 527 528 if (!call_inst) { 529 error.SetErrorToGenericError(); 530 error.SetErrorString(interpreter_internal_error); 531 return false; 532 } 533 534 if (!CanIgnoreCall(call_inst) && !support_function_calls) { 535 if (log) 536 log->Printf("Unsupported instruction: %s", 537 PrintValue(&*ii).c_str()); 538 error.SetErrorToGenericError(); 539 error.SetErrorString(unsupported_opcode_error); 540 return false; 541 } 542 } break; 543 case Instruction::GetElementPtr: 544 break; 545 case Instruction::ICmp: { 546 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii); 547 548 if (!icmp_inst) { 549 error.SetErrorToGenericError(); 550 error.SetErrorString(interpreter_internal_error); 551 return false; 552 } 553 554 switch (icmp_inst->getPredicate()) { 555 default: { 556 if (log) 557 log->Printf("Unsupported ICmp predicate: %s", 558 PrintValue(&*ii).c_str()); 559 560 error.SetErrorToGenericError(); 561 error.SetErrorString(unsupported_opcode_error); 562 return false; 563 } 564 case CmpInst::ICMP_EQ: 565 case CmpInst::ICMP_NE: 566 case CmpInst::ICMP_UGT: 567 case CmpInst::ICMP_UGE: 568 case CmpInst::ICMP_ULT: 569 case CmpInst::ICMP_ULE: 570 case CmpInst::ICMP_SGT: 571 case CmpInst::ICMP_SGE: 572 case CmpInst::ICMP_SLT: 573 case CmpInst::ICMP_SLE: 574 break; 575 } 576 } break; 577 case Instruction::And: 578 case Instruction::AShr: 579 case Instruction::IntToPtr: 580 case Instruction::PtrToInt: 581 case Instruction::Load: 582 case Instruction::LShr: 583 case Instruction::Mul: 584 case Instruction::Or: 585 case Instruction::Ret: 586 case Instruction::SDiv: 587 case Instruction::SExt: 588 case Instruction::Shl: 589 case Instruction::SRem: 590 case Instruction::Store: 591 case Instruction::Sub: 592 case Instruction::Trunc: 593 case Instruction::UDiv: 594 case Instruction::URem: 595 case Instruction::Xor: 596 case Instruction::ZExt: 597 break; 598 } 599 600 for (int oi = 0, oe = ii->getNumOperands(); oi != oe; ++oi) { 601 Value *operand = ii->getOperand(oi); 602 Type *operand_type = operand->getType(); 603 604 switch (operand_type->getTypeID()) { 605 default: 606 break; 607 case Type::VectorTyID: { 608 if (log) 609 log->Printf("Unsupported operand type: %s", 610 PrintType(operand_type).c_str()); 611 error.SetErrorString(unsupported_operand_error); 612 return false; 613 } 614 } 615 616 // The IR interpreter currently doesn't know about 617 // 128-bit integers. As they're not that frequent, 618 // we can just fall back to the JIT rather than 619 // choking. 620 if (operand_type->getPrimitiveSizeInBits() > 64) { 621 if (log) 622 log->Printf("Unsupported operand type: %s", 623 PrintType(operand_type).c_str()); 624 error.SetErrorString(unsupported_operand_error); 625 return false; 626 } 627 628 if (Constant *constant = llvm::dyn_cast<Constant>(operand)) { 629 if (!CanResolveConstant(constant)) { 630 if (log) 631 log->Printf("Unsupported constant: %s", 632 PrintValue(constant).c_str()); 633 error.SetErrorString(unsupported_operand_error); 634 return false; 635 } 636 } 637 } 638 } 639 } 640 641 return true; 642 } 643 644 bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function, 645 llvm::ArrayRef<lldb::addr_t> args, 646 lldb_private::IRExecutionUnit &execution_unit, 647 lldb_private::Status &error, 648 lldb::addr_t stack_frame_bottom, 649 lldb::addr_t stack_frame_top, 650 lldb_private::ExecutionContext &exe_ctx) { 651 lldb_private::Log *log( 652 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 653 654 if (log) { 655 std::string s; 656 raw_string_ostream oss(s); 657 658 module.print(oss, nullptr); 659 660 oss.flush(); 661 662 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", 663 s.c_str()); 664 } 665 666 DataLayout data_layout(&module); 667 668 InterpreterStackFrame frame(data_layout, execution_unit, stack_frame_bottom, 669 stack_frame_top); 670 671 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS) { 672 error.SetErrorString("Couldn't allocate stack frame"); 673 } 674 675 int arg_index = 0; 676 677 for (llvm::Function::arg_iterator ai = function.arg_begin(), 678 ae = function.arg_end(); 679 ai != ae; ++ai, ++arg_index) { 680 if (args.size() <= static_cast<size_t>(arg_index)) { 681 error.SetErrorString("Not enough arguments passed in to function"); 682 return false; 683 } 684 685 lldb::addr_t ptr = args[arg_index]; 686 687 frame.MakeArgument(&*ai, ptr); 688 } 689 690 uint32_t num_insts = 0; 691 692 frame.Jump(&function.front()); 693 694 while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) { 695 const Instruction *inst = &*frame.m_ii; 696 697 if (log) 698 log->Printf("Interpreting %s", PrintValue(inst).c_str()); 699 700 switch (inst->getOpcode()) { 701 default: 702 break; 703 704 case Instruction::Add: 705 case Instruction::Sub: 706 case Instruction::Mul: 707 case Instruction::SDiv: 708 case Instruction::UDiv: 709 case Instruction::SRem: 710 case Instruction::URem: 711 case Instruction::Shl: 712 case Instruction::LShr: 713 case Instruction::AShr: 714 case Instruction::And: 715 case Instruction::Or: 716 case Instruction::Xor: { 717 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst); 718 719 if (!bin_op) { 720 if (log) 721 log->Printf( 722 "getOpcode() returns %s, but instruction is not a BinaryOperator", 723 inst->getOpcodeName()); 724 error.SetErrorToGenericError(); 725 error.SetErrorString(interpreter_internal_error); 726 return false; 727 } 728 729 Value *lhs = inst->getOperand(0); 730 Value *rhs = inst->getOperand(1); 731 732 lldb_private::Scalar L; 733 lldb_private::Scalar R; 734 735 if (!frame.EvaluateValue(L, lhs, module)) { 736 if (log) 737 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 738 error.SetErrorToGenericError(); 739 error.SetErrorString(bad_value_error); 740 return false; 741 } 742 743 if (!frame.EvaluateValue(R, rhs, module)) { 744 if (log) 745 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 746 error.SetErrorToGenericError(); 747 error.SetErrorString(bad_value_error); 748 return false; 749 } 750 751 lldb_private::Scalar result; 752 753 switch (inst->getOpcode()) { 754 default: 755 break; 756 case Instruction::Add: 757 result = L + R; 758 break; 759 case Instruction::Mul: 760 result = L * R; 761 break; 762 case Instruction::Sub: 763 result = L - R; 764 break; 765 case Instruction::SDiv: 766 L.MakeSigned(); 767 R.MakeSigned(); 768 result = L / R; 769 break; 770 case Instruction::UDiv: 771 L.MakeUnsigned(); 772 R.MakeUnsigned(); 773 result = L / R; 774 break; 775 case Instruction::SRem: 776 L.MakeSigned(); 777 R.MakeSigned(); 778 result = L % R; 779 break; 780 case Instruction::URem: 781 L.MakeUnsigned(); 782 R.MakeUnsigned(); 783 result = L % R; 784 break; 785 case Instruction::Shl: 786 result = L << R; 787 break; 788 case Instruction::AShr: 789 result = L >> R; 790 break; 791 case Instruction::LShr: 792 result = L; 793 result.ShiftRightLogical(R); 794 break; 795 case Instruction::And: 796 result = L & R; 797 break; 798 case Instruction::Or: 799 result = L | R; 800 break; 801 case Instruction::Xor: 802 result = L ^ R; 803 break; 804 } 805 806 frame.AssignValue(inst, result, module); 807 808 if (log) { 809 log->Printf("Interpreted a %s", inst->getOpcodeName()); 810 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 811 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 812 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 813 } 814 } break; 815 case Instruction::Alloca: { 816 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst); 817 818 if (!alloca_inst) { 819 if (log) 820 log->Printf("getOpcode() returns Alloca, but instruction is not an " 821 "AllocaInst"); 822 error.SetErrorToGenericError(); 823 error.SetErrorString(interpreter_internal_error); 824 return false; 825 } 826 827 if (alloca_inst->isArrayAllocation()) { 828 if (log) 829 log->Printf( 830 "AllocaInsts are not handled if isArrayAllocation() is true"); 831 error.SetErrorToGenericError(); 832 error.SetErrorString(unsupported_opcode_error); 833 return false; 834 } 835 836 // The semantics of Alloca are: 837 // Create a region R of virtual memory of type T, backed by a data 838 // buffer 839 // Create a region P of virtual memory of type T*, backed by a data 840 // buffer 841 // Write the virtual address of R into P 842 843 Type *T = alloca_inst->getAllocatedType(); 844 Type *Tptr = alloca_inst->getType(); 845 846 lldb::addr_t R = frame.Malloc(T); 847 848 if (R == LLDB_INVALID_ADDRESS) { 849 if (log) 850 log->Printf("Couldn't allocate memory for an AllocaInst"); 851 error.SetErrorToGenericError(); 852 error.SetErrorString(memory_allocation_error); 853 return false; 854 } 855 856 lldb::addr_t P = frame.Malloc(Tptr); 857 858 if (P == LLDB_INVALID_ADDRESS) { 859 if (log) 860 log->Printf("Couldn't allocate the result pointer for an AllocaInst"); 861 error.SetErrorToGenericError(); 862 error.SetErrorString(memory_allocation_error); 863 return false; 864 } 865 866 lldb_private::Status write_error; 867 868 execution_unit.WritePointerToMemory(P, R, write_error); 869 870 if (!write_error.Success()) { 871 if (log) 872 log->Printf("Couldn't write the result pointer for an AllocaInst"); 873 error.SetErrorToGenericError(); 874 error.SetErrorString(memory_write_error); 875 lldb_private::Status free_error; 876 execution_unit.Free(P, free_error); 877 execution_unit.Free(R, free_error); 878 return false; 879 } 880 881 frame.m_values[alloca_inst] = P; 882 883 if (log) { 884 log->Printf("Interpreted an AllocaInst"); 885 log->Printf(" R : 0x%" PRIx64, R); 886 log->Printf(" P : 0x%" PRIx64, P); 887 } 888 } break; 889 case Instruction::BitCast: 890 case Instruction::ZExt: { 891 const CastInst *cast_inst = dyn_cast<CastInst>(inst); 892 893 if (!cast_inst) { 894 if (log) 895 log->Printf( 896 "getOpcode() returns %s, but instruction is not a BitCastInst", 897 cast_inst->getOpcodeName()); 898 error.SetErrorToGenericError(); 899 error.SetErrorString(interpreter_internal_error); 900 return false; 901 } 902 903 Value *source = cast_inst->getOperand(0); 904 905 lldb_private::Scalar S; 906 907 if (!frame.EvaluateValue(S, source, module)) { 908 if (log) 909 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); 910 error.SetErrorToGenericError(); 911 error.SetErrorString(bad_value_error); 912 return false; 913 } 914 915 frame.AssignValue(inst, S, module); 916 } break; 917 case Instruction::SExt: { 918 const CastInst *cast_inst = dyn_cast<CastInst>(inst); 919 920 if (!cast_inst) { 921 if (log) 922 log->Printf( 923 "getOpcode() returns %s, but instruction is not a BitCastInst", 924 cast_inst->getOpcodeName()); 925 error.SetErrorToGenericError(); 926 error.SetErrorString(interpreter_internal_error); 927 return false; 928 } 929 930 Value *source = cast_inst->getOperand(0); 931 932 lldb_private::Scalar S; 933 934 if (!frame.EvaluateValue(S, source, module)) { 935 if (log) 936 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); 937 error.SetErrorToGenericError(); 938 error.SetErrorString(bad_value_error); 939 return false; 940 } 941 942 S.MakeSigned(); 943 944 lldb_private::Scalar S_signextend(S.SLongLong()); 945 946 frame.AssignValue(inst, S_signextend, module); 947 } break; 948 case Instruction::Br: { 949 const BranchInst *br_inst = dyn_cast<BranchInst>(inst); 950 951 if (!br_inst) { 952 if (log) 953 log->Printf( 954 "getOpcode() returns Br, but instruction is not a BranchInst"); 955 error.SetErrorToGenericError(); 956 error.SetErrorString(interpreter_internal_error); 957 return false; 958 } 959 960 if (br_inst->isConditional()) { 961 Value *condition = br_inst->getCondition(); 962 963 lldb_private::Scalar C; 964 965 if (!frame.EvaluateValue(C, condition, module)) { 966 if (log) 967 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str()); 968 error.SetErrorToGenericError(); 969 error.SetErrorString(bad_value_error); 970 return false; 971 } 972 973 if (!C.IsZero()) 974 frame.Jump(br_inst->getSuccessor(0)); 975 else 976 frame.Jump(br_inst->getSuccessor(1)); 977 978 if (log) { 979 log->Printf("Interpreted a BrInst with a condition"); 980 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str()); 981 } 982 } else { 983 frame.Jump(br_inst->getSuccessor(0)); 984 985 if (log) { 986 log->Printf("Interpreted a BrInst with no condition"); 987 } 988 } 989 } 990 continue; 991 case Instruction::PHI: { 992 const PHINode *phi_inst = dyn_cast<PHINode>(inst); 993 994 if (!phi_inst) { 995 if (log) 996 log->Printf( 997 "getOpcode() returns PHI, but instruction is not a PHINode"); 998 error.SetErrorToGenericError(); 999 error.SetErrorString(interpreter_internal_error); 1000 return false; 1001 } 1002 if (!frame.m_prev_bb) { 1003 if (log) 1004 log->Printf("Encountered PHI node without having jumped from another " 1005 "basic block"); 1006 error.SetErrorToGenericError(); 1007 error.SetErrorString(interpreter_internal_error); 1008 return false; 1009 } 1010 1011 Value *value = phi_inst->getIncomingValueForBlock(frame.m_prev_bb); 1012 lldb_private::Scalar result; 1013 if (!frame.EvaluateValue(result, value, module)) { 1014 if (log) 1015 log->Printf("Couldn't evaluate %s", PrintValue(value).c_str()); 1016 error.SetErrorToGenericError(); 1017 error.SetErrorString(bad_value_error); 1018 return false; 1019 } 1020 frame.AssignValue(inst, result, module); 1021 1022 if (log) { 1023 log->Printf("Interpreted a %s", inst->getOpcodeName()); 1024 log->Printf(" Incoming value : %s", 1025 frame.SummarizeValue(value).c_str()); 1026 } 1027 } break; 1028 case Instruction::GetElementPtr: { 1029 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst); 1030 1031 if (!gep_inst) { 1032 if (log) 1033 log->Printf("getOpcode() returns GetElementPtr, but instruction is " 1034 "not a GetElementPtrInst"); 1035 error.SetErrorToGenericError(); 1036 error.SetErrorString(interpreter_internal_error); 1037 return false; 1038 } 1039 1040 const Value *pointer_operand = gep_inst->getPointerOperand(); 1041 Type *src_elem_ty = gep_inst->getSourceElementType(); 1042 1043 lldb_private::Scalar P; 1044 1045 if (!frame.EvaluateValue(P, pointer_operand, module)) { 1046 if (log) 1047 log->Printf("Couldn't evaluate %s", 1048 PrintValue(pointer_operand).c_str()); 1049 error.SetErrorToGenericError(); 1050 error.SetErrorString(bad_value_error); 1051 return false; 1052 } 1053 1054 typedef SmallVector<Value *, 8> IndexVector; 1055 typedef IndexVector::iterator IndexIterator; 1056 1057 SmallVector<Value *, 8> indices(gep_inst->idx_begin(), 1058 gep_inst->idx_end()); 1059 1060 SmallVector<Value *, 8> const_indices; 1061 1062 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie; 1063 ++ii) { 1064 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii); 1065 1066 if (!constant_index) { 1067 lldb_private::Scalar I; 1068 1069 if (!frame.EvaluateValue(I, *ii, module)) { 1070 if (log) 1071 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str()); 1072 error.SetErrorToGenericError(); 1073 error.SetErrorString(bad_value_error); 1074 return false; 1075 } 1076 1077 if (log) 1078 log->Printf("Evaluated constant index %s as %llu", 1079 PrintValue(*ii).c_str(), 1080 I.ULongLong(LLDB_INVALID_ADDRESS)); 1081 1082 constant_index = cast<ConstantInt>(ConstantInt::get( 1083 (*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS))); 1084 } 1085 1086 const_indices.push_back(constant_index); 1087 } 1088 1089 uint64_t offset = 1090 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices); 1091 1092 lldb_private::Scalar Poffset = P + offset; 1093 1094 frame.AssignValue(inst, Poffset, module); 1095 1096 if (log) { 1097 log->Printf("Interpreted a GetElementPtrInst"); 1098 log->Printf(" P : %s", 1099 frame.SummarizeValue(pointer_operand).c_str()); 1100 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str()); 1101 } 1102 } break; 1103 case Instruction::ICmp: { 1104 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst); 1105 1106 if (!icmp_inst) { 1107 if (log) 1108 log->Printf( 1109 "getOpcode() returns ICmp, but instruction is not an ICmpInst"); 1110 error.SetErrorToGenericError(); 1111 error.SetErrorString(interpreter_internal_error); 1112 return false; 1113 } 1114 1115 CmpInst::Predicate predicate = icmp_inst->getPredicate(); 1116 1117 Value *lhs = inst->getOperand(0); 1118 Value *rhs = inst->getOperand(1); 1119 1120 lldb_private::Scalar L; 1121 lldb_private::Scalar R; 1122 1123 if (!frame.EvaluateValue(L, lhs, module)) { 1124 if (log) 1125 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); 1126 error.SetErrorToGenericError(); 1127 error.SetErrorString(bad_value_error); 1128 return false; 1129 } 1130 1131 if (!frame.EvaluateValue(R, rhs, module)) { 1132 if (log) 1133 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); 1134 error.SetErrorToGenericError(); 1135 error.SetErrorString(bad_value_error); 1136 return false; 1137 } 1138 1139 lldb_private::Scalar result; 1140 1141 switch (predicate) { 1142 default: 1143 return false; 1144 case CmpInst::ICMP_EQ: 1145 result = (L == R); 1146 break; 1147 case CmpInst::ICMP_NE: 1148 result = (L != R); 1149 break; 1150 case CmpInst::ICMP_UGT: 1151 L.MakeUnsigned(); 1152 R.MakeUnsigned(); 1153 result = (L > R); 1154 break; 1155 case CmpInst::ICMP_UGE: 1156 L.MakeUnsigned(); 1157 R.MakeUnsigned(); 1158 result = (L >= R); 1159 break; 1160 case CmpInst::ICMP_ULT: 1161 L.MakeUnsigned(); 1162 R.MakeUnsigned(); 1163 result = (L < R); 1164 break; 1165 case CmpInst::ICMP_ULE: 1166 L.MakeUnsigned(); 1167 R.MakeUnsigned(); 1168 result = (L <= R); 1169 break; 1170 case CmpInst::ICMP_SGT: 1171 L.MakeSigned(); 1172 R.MakeSigned(); 1173 result = (L > R); 1174 break; 1175 case CmpInst::ICMP_SGE: 1176 L.MakeSigned(); 1177 R.MakeSigned(); 1178 result = (L >= R); 1179 break; 1180 case CmpInst::ICMP_SLT: 1181 L.MakeSigned(); 1182 R.MakeSigned(); 1183 result = (L < R); 1184 break; 1185 case CmpInst::ICMP_SLE: 1186 L.MakeSigned(); 1187 R.MakeSigned(); 1188 result = (L <= R); 1189 break; 1190 } 1191 1192 frame.AssignValue(inst, result, module); 1193 1194 if (log) { 1195 log->Printf("Interpreted an ICmpInst"); 1196 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); 1197 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); 1198 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1199 } 1200 } break; 1201 case Instruction::IntToPtr: { 1202 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst); 1203 1204 if (!int_to_ptr_inst) { 1205 if (log) 1206 log->Printf("getOpcode() returns IntToPtr, but instruction is not an " 1207 "IntToPtrInst"); 1208 error.SetErrorToGenericError(); 1209 error.SetErrorString(interpreter_internal_error); 1210 return false; 1211 } 1212 1213 Value *src_operand = int_to_ptr_inst->getOperand(0); 1214 1215 lldb_private::Scalar I; 1216 1217 if (!frame.EvaluateValue(I, src_operand, module)) { 1218 if (log) 1219 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1220 error.SetErrorToGenericError(); 1221 error.SetErrorString(bad_value_error); 1222 return false; 1223 } 1224 1225 frame.AssignValue(inst, I, module); 1226 1227 if (log) { 1228 log->Printf("Interpreted an IntToPtr"); 1229 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1230 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1231 } 1232 } break; 1233 case Instruction::PtrToInt: { 1234 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst); 1235 1236 if (!ptr_to_int_inst) { 1237 if (log) 1238 log->Printf("getOpcode() returns PtrToInt, but instruction is not an " 1239 "PtrToIntInst"); 1240 error.SetErrorToGenericError(); 1241 error.SetErrorString(interpreter_internal_error); 1242 return false; 1243 } 1244 1245 Value *src_operand = ptr_to_int_inst->getOperand(0); 1246 1247 lldb_private::Scalar I; 1248 1249 if (!frame.EvaluateValue(I, src_operand, module)) { 1250 if (log) 1251 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1252 error.SetErrorToGenericError(); 1253 error.SetErrorString(bad_value_error); 1254 return false; 1255 } 1256 1257 frame.AssignValue(inst, I, module); 1258 1259 if (log) { 1260 log->Printf("Interpreted a PtrToInt"); 1261 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1262 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1263 } 1264 } break; 1265 case Instruction::Trunc: { 1266 const TruncInst *trunc_inst = dyn_cast<TruncInst>(inst); 1267 1268 if (!trunc_inst) { 1269 if (log) 1270 log->Printf( 1271 "getOpcode() returns Trunc, but instruction is not a TruncInst"); 1272 error.SetErrorToGenericError(); 1273 error.SetErrorString(interpreter_internal_error); 1274 return false; 1275 } 1276 1277 Value *src_operand = trunc_inst->getOperand(0); 1278 1279 lldb_private::Scalar I; 1280 1281 if (!frame.EvaluateValue(I, src_operand, module)) { 1282 if (log) 1283 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); 1284 error.SetErrorToGenericError(); 1285 error.SetErrorString(bad_value_error); 1286 return false; 1287 } 1288 1289 frame.AssignValue(inst, I, module); 1290 1291 if (log) { 1292 log->Printf("Interpreted a Trunc"); 1293 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); 1294 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); 1295 } 1296 } break; 1297 case Instruction::Load: { 1298 const LoadInst *load_inst = dyn_cast<LoadInst>(inst); 1299 1300 if (!load_inst) { 1301 if (log) 1302 log->Printf( 1303 "getOpcode() returns Load, but instruction is not a LoadInst"); 1304 error.SetErrorToGenericError(); 1305 error.SetErrorString(interpreter_internal_error); 1306 return false; 1307 } 1308 1309 // The semantics of Load are: 1310 // Create a region D that will contain the loaded data 1311 // Resolve the region P containing a pointer 1312 // Dereference P to get the region R that the data should be loaded from 1313 // Transfer a unit of type type(D) from R to D 1314 1315 const Value *pointer_operand = load_inst->getPointerOperand(); 1316 1317 Type *pointer_ty = pointer_operand->getType(); 1318 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1319 if (!pointer_ptr_ty) { 1320 if (log) 1321 log->Printf("getPointerOperand()->getType() is not a PointerType"); 1322 error.SetErrorToGenericError(); 1323 error.SetErrorString(interpreter_internal_error); 1324 return false; 1325 } 1326 Type *target_ty = pointer_ptr_ty->getElementType(); 1327 1328 lldb::addr_t D = frame.ResolveValue(load_inst, module); 1329 lldb::addr_t P = frame.ResolveValue(pointer_operand, module); 1330 1331 if (D == LLDB_INVALID_ADDRESS) { 1332 if (log) 1333 log->Printf("LoadInst's value doesn't resolve to anything"); 1334 error.SetErrorToGenericError(); 1335 error.SetErrorString(bad_value_error); 1336 return false; 1337 } 1338 1339 if (P == LLDB_INVALID_ADDRESS) { 1340 if (log) 1341 log->Printf("LoadInst's pointer doesn't resolve to anything"); 1342 error.SetErrorToGenericError(); 1343 error.SetErrorString(bad_value_error); 1344 return false; 1345 } 1346 1347 lldb::addr_t R; 1348 lldb_private::Status read_error; 1349 execution_unit.ReadPointerFromMemory(&R, P, read_error); 1350 1351 if (!read_error.Success()) { 1352 if (log) 1353 log->Printf("Couldn't read the address to be loaded for a LoadInst"); 1354 error.SetErrorToGenericError(); 1355 error.SetErrorString(memory_read_error); 1356 return false; 1357 } 1358 1359 size_t target_size = data_layout.getTypeStoreSize(target_ty); 1360 lldb_private::DataBufferHeap buffer(target_size, 0); 1361 1362 read_error.Clear(); 1363 execution_unit.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), 1364 read_error); 1365 if (!read_error.Success()) { 1366 if (log) 1367 log->Printf("Couldn't read from a region on behalf of a LoadInst"); 1368 error.SetErrorToGenericError(); 1369 error.SetErrorString(memory_read_error); 1370 return false; 1371 } 1372 1373 lldb_private::Status write_error; 1374 execution_unit.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), 1375 write_error); 1376 if (!write_error.Success()) { 1377 if (log) 1378 log->Printf("Couldn't write to a region on behalf of a LoadInst"); 1379 error.SetErrorToGenericError(); 1380 error.SetErrorString(memory_read_error); 1381 return false; 1382 } 1383 1384 if (log) { 1385 log->Printf("Interpreted a LoadInst"); 1386 log->Printf(" P : 0x%" PRIx64, P); 1387 log->Printf(" R : 0x%" PRIx64, R); 1388 log->Printf(" D : 0x%" PRIx64, D); 1389 } 1390 } break; 1391 case Instruction::Ret: { 1392 return true; 1393 } 1394 case Instruction::Store: { 1395 const StoreInst *store_inst = dyn_cast<StoreInst>(inst); 1396 1397 if (!store_inst) { 1398 if (log) 1399 log->Printf( 1400 "getOpcode() returns Store, but instruction is not a StoreInst"); 1401 error.SetErrorToGenericError(); 1402 error.SetErrorString(interpreter_internal_error); 1403 return false; 1404 } 1405 1406 // The semantics of Store are: 1407 // Resolve the region D containing the data to be stored 1408 // Resolve the region P containing a pointer 1409 // Dereference P to get the region R that the data should be stored in 1410 // Transfer a unit of type type(D) from D to R 1411 1412 const Value *value_operand = store_inst->getValueOperand(); 1413 const Value *pointer_operand = store_inst->getPointerOperand(); 1414 1415 Type *pointer_ty = pointer_operand->getType(); 1416 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); 1417 if (!pointer_ptr_ty) 1418 return false; 1419 Type *target_ty = pointer_ptr_ty->getElementType(); 1420 1421 lldb::addr_t D = frame.ResolveValue(value_operand, module); 1422 lldb::addr_t P = frame.ResolveValue(pointer_operand, module); 1423 1424 if (D == LLDB_INVALID_ADDRESS) { 1425 if (log) 1426 log->Printf("StoreInst's value doesn't resolve to anything"); 1427 error.SetErrorToGenericError(); 1428 error.SetErrorString(bad_value_error); 1429 return false; 1430 } 1431 1432 if (P == LLDB_INVALID_ADDRESS) { 1433 if (log) 1434 log->Printf("StoreInst's pointer doesn't resolve to anything"); 1435 error.SetErrorToGenericError(); 1436 error.SetErrorString(bad_value_error); 1437 return false; 1438 } 1439 1440 lldb::addr_t R; 1441 lldb_private::Status read_error; 1442 execution_unit.ReadPointerFromMemory(&R, P, read_error); 1443 1444 if (!read_error.Success()) { 1445 if (log) 1446 log->Printf("Couldn't read the address to be loaded for a LoadInst"); 1447 error.SetErrorToGenericError(); 1448 error.SetErrorString(memory_read_error); 1449 return false; 1450 } 1451 1452 size_t target_size = data_layout.getTypeStoreSize(target_ty); 1453 lldb_private::DataBufferHeap buffer(target_size, 0); 1454 1455 read_error.Clear(); 1456 execution_unit.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), 1457 read_error); 1458 if (!read_error.Success()) { 1459 if (log) 1460 log->Printf("Couldn't read from a region on behalf of a StoreInst"); 1461 error.SetErrorToGenericError(); 1462 error.SetErrorString(memory_read_error); 1463 return false; 1464 } 1465 1466 lldb_private::Status write_error; 1467 execution_unit.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), 1468 write_error); 1469 if (!write_error.Success()) { 1470 if (log) 1471 log->Printf("Couldn't write to a region on behalf of a StoreInst"); 1472 error.SetErrorToGenericError(); 1473 error.SetErrorString(memory_write_error); 1474 return false; 1475 } 1476 1477 if (log) { 1478 log->Printf("Interpreted a StoreInst"); 1479 log->Printf(" D : 0x%" PRIx64, D); 1480 log->Printf(" P : 0x%" PRIx64, P); 1481 log->Printf(" R : 0x%" PRIx64, R); 1482 } 1483 } break; 1484 case Instruction::Call: { 1485 const CallInst *call_inst = dyn_cast<CallInst>(inst); 1486 1487 if (!call_inst) { 1488 if (log) 1489 log->Printf( 1490 "getOpcode() returns %s, but instruction is not a CallInst", 1491 inst->getOpcodeName()); 1492 error.SetErrorToGenericError(); 1493 error.SetErrorString(interpreter_internal_error); 1494 return false; 1495 } 1496 1497 if (CanIgnoreCall(call_inst)) 1498 break; 1499 1500 // Get the return type 1501 llvm::Type *returnType = call_inst->getType(); 1502 if (returnType == nullptr) { 1503 error.SetErrorToGenericError(); 1504 error.SetErrorString("unable to access return type"); 1505 return false; 1506 } 1507 1508 // Work with void, integer and pointer return types 1509 if (!returnType->isVoidTy() && !returnType->isIntegerTy() && 1510 !returnType->isPointerTy()) { 1511 error.SetErrorToGenericError(); 1512 error.SetErrorString("return type is not supported"); 1513 return false; 1514 } 1515 1516 // Check we can actually get a thread 1517 if (exe_ctx.GetThreadPtr() == nullptr) { 1518 error.SetErrorToGenericError(); 1519 error.SetErrorStringWithFormat("unable to acquire thread"); 1520 return false; 1521 } 1522 1523 // Make sure we have a valid process 1524 if (!exe_ctx.GetProcessPtr()) { 1525 error.SetErrorToGenericError(); 1526 error.SetErrorStringWithFormat("unable to get the process"); 1527 return false; 1528 } 1529 1530 // Find the address of the callee function 1531 lldb_private::Scalar I; 1532 const llvm::Value *val = call_inst->getCalledValue(); 1533 1534 if (!frame.EvaluateValue(I, val, module)) { 1535 error.SetErrorToGenericError(); 1536 error.SetErrorString("unable to get address of function"); 1537 return false; 1538 } 1539 lldb_private::Address funcAddr(I.ULongLong(LLDB_INVALID_ADDRESS)); 1540 1541 lldb_private::DiagnosticManager diagnostics; 1542 lldb_private::EvaluateExpressionOptions options; 1543 1544 // We generally receive a function pointer which we must dereference 1545 llvm::Type *prototype = val->getType(); 1546 if (!prototype->isPointerTy()) { 1547 error.SetErrorToGenericError(); 1548 error.SetErrorString("call need function pointer"); 1549 return false; 1550 } 1551 1552 // Dereference the function pointer 1553 prototype = prototype->getPointerElementType(); 1554 if (!(prototype->isFunctionTy() || prototype->isFunctionVarArg())) { 1555 error.SetErrorToGenericError(); 1556 error.SetErrorString("call need function pointer"); 1557 return false; 1558 } 1559 1560 // Find number of arguments 1561 const int numArgs = call_inst->getNumArgOperands(); 1562 1563 // We work with a fixed array of 16 arguments which is our upper limit 1564 static lldb_private::ABI::CallArgument rawArgs[16]; 1565 if (numArgs >= 16) { 1566 error.SetErrorToGenericError(); 1567 error.SetErrorStringWithFormat("function takes too many arguments"); 1568 return false; 1569 } 1570 1571 // Push all function arguments to the argument list that will be passed 1572 // to the call function thread plan 1573 for (int i = 0; i < numArgs; i++) { 1574 // Get details of this argument 1575 llvm::Value *arg_op = call_inst->getArgOperand(i); 1576 llvm::Type *arg_ty = arg_op->getType(); 1577 1578 // Ensure that this argument is an supported type 1579 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) { 1580 error.SetErrorToGenericError(); 1581 error.SetErrorStringWithFormat("argument %d must be integer type", i); 1582 return false; 1583 } 1584 1585 // Extract the arguments value 1586 lldb_private::Scalar tmp_op = 0; 1587 if (!frame.EvaluateValue(tmp_op, arg_op, module)) { 1588 error.SetErrorToGenericError(); 1589 error.SetErrorStringWithFormat("unable to evaluate argument %d", i); 1590 return false; 1591 } 1592 1593 // Check if this is a string literal or constant string pointer 1594 if (arg_ty->isPointerTy()) { 1595 lldb::addr_t addr = tmp_op.ULongLong(); 1596 size_t dataSize = 0; 1597 1598 bool Success = execution_unit.GetAllocSize(addr, dataSize); 1599 (void)Success; 1600 assert(Success && 1601 "unable to locate host data for transfer to device"); 1602 // Create the required buffer 1603 rawArgs[i].size = dataSize; 1604 rawArgs[i].data_up.reset(new uint8_t[dataSize + 1]); 1605 1606 // Read string from host memory 1607 execution_unit.ReadMemory(rawArgs[i].data_up.get(), addr, dataSize, 1608 error); 1609 assert(!error.Fail() && 1610 "we have failed to read the string from memory"); 1611 1612 // Add null terminator 1613 rawArgs[i].data_up[dataSize] = '\0'; 1614 rawArgs[i].type = lldb_private::ABI::CallArgument::HostPointer; 1615 } else /* if ( arg_ty->isPointerTy() ) */ 1616 { 1617 rawArgs[i].type = lldb_private::ABI::CallArgument::TargetValue; 1618 // Get argument size in bytes 1619 rawArgs[i].size = arg_ty->getIntegerBitWidth() / 8; 1620 // Push value into argument list for thread plan 1621 rawArgs[i].value = tmp_op.ULongLong(); 1622 } 1623 } 1624 1625 // Pack the arguments into an llvm::array 1626 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs); 1627 1628 // Setup a thread plan to call the target function 1629 lldb::ThreadPlanSP call_plan_sp( 1630 new lldb_private::ThreadPlanCallFunctionUsingABI( 1631 exe_ctx.GetThreadRef(), funcAddr, *prototype, *returnType, args, 1632 options)); 1633 1634 // Check if the plan is valid 1635 lldb_private::StreamString ss; 1636 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) { 1637 error.SetErrorToGenericError(); 1638 error.SetErrorStringWithFormat( 1639 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx", 1640 I.ULongLong()); 1641 return false; 1642 } 1643 1644 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true); 1645 1646 // Execute the actual function call thread plan 1647 lldb::ExpressionResults res = exe_ctx.GetProcessRef().RunThreadPlan( 1648 exe_ctx, call_plan_sp, options, diagnostics); 1649 1650 // Check that the thread plan completed successfully 1651 if (res != lldb::ExpressionResults::eExpressionCompleted) { 1652 error.SetErrorToGenericError(); 1653 error.SetErrorStringWithFormat("ThreadPlanCallFunctionUsingABI failed"); 1654 return false; 1655 } 1656 1657 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false); 1658 1659 // Void return type 1660 if (returnType->isVoidTy()) { 1661 // Cant assign to void types, so we leave the frame untouched 1662 } else 1663 // Integer or pointer return type 1664 if (returnType->isIntegerTy() || returnType->isPointerTy()) { 1665 // Get the encapsulated return value 1666 lldb::ValueObjectSP retVal = call_plan_sp.get()->GetReturnValueObject(); 1667 1668 lldb_private::Scalar returnVal = -1; 1669 lldb_private::ValueObject *vobj = retVal.get(); 1670 1671 // Check if the return value is valid 1672 if (vobj == nullptr || retVal.empty()) { 1673 error.SetErrorToGenericError(); 1674 error.SetErrorStringWithFormat("unable to get the return value"); 1675 return false; 1676 } 1677 1678 // Extract the return value as a integer 1679 lldb_private::Value &value = vobj->GetValue(); 1680 returnVal = value.GetScalar(); 1681 1682 // Push the return value as the result 1683 frame.AssignValue(inst, returnVal, module); 1684 } 1685 } break; 1686 } 1687 1688 ++frame.m_ii; 1689 } 1690 1691 if (num_insts >= 4096) { 1692 error.SetErrorToGenericError(); 1693 error.SetErrorString(infinite_loop_error); 1694 return false; 1695 } 1696 1697 return false; 1698 } 1699