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