707 llvm::ArrayRef<lldb::addr_t> args,
718 raw_string_ostream oss(s);
720 module.print(oss, nullptr);
722 LLDB_LOGF(log,
"Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
726 const DataLayout &data_layout =
module.getDataLayout();
738 for (llvm::Function::arg_iterator ai = function.arg_begin(),
739 ae = function.arg_end();
740 ai != ae; ++ai, ++arg_index) {
741 if (args.size() <=
static_cast<size_t>(arg_index)) {
743 "Not enough arguments passed in to function");
752 frame.
Jump(&function.front());
757 using clock = std::chrono::steady_clock;
760 std::optional<clock::time_point> end_time;
761 if (timeout && timeout->count() > 0)
762 end_time = clock::now() + *timeout;
766 if (end_time && clock::now() >= *end_time) {
774 "Interrupted in IR interpreting.")) {
780 const Instruction *inst = &*frame.
m_ii;
784 switch (inst->getOpcode()) {
788 case Instruction::Add:
789 case Instruction::Sub:
790 case Instruction::Mul:
791 case Instruction::SDiv:
792 case Instruction::UDiv:
793 case Instruction::SRem:
794 case Instruction::URem:
795 case Instruction::Shl:
796 case Instruction::LShr:
797 case Instruction::AShr:
798 case Instruction::And:
799 case Instruction::Or:
800 case Instruction::Xor:
801 case Instruction::FAdd:
802 case Instruction::FSub:
803 case Instruction::FMul:
804 case Instruction::FDiv: {
805 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
810 "getOpcode() returns %s, but instruction is not a BinaryOperator",
811 inst->getOpcodeName());
817 Value *lhs = inst->getOperand(0);
818 Value *rhs = inst->getOperand(1);
837 switch (inst->getOpcode()) {
840 case Instruction::Add:
841 case Instruction::FAdd:
844 case Instruction::Mul:
845 case Instruction::FMul:
848 case Instruction::Sub:
849 case Instruction::FSub:
852 case Instruction::SDiv:
857 case Instruction::UDiv:
862 case Instruction::FDiv:
865 case Instruction::SRem:
870 case Instruction::URem:
875 case Instruction::Shl:
878 case Instruction::AShr:
881 case Instruction::LShr:
885 case Instruction::And:
888 case Instruction::Or:
891 case Instruction::Xor:
898 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
903 case Instruction::Alloca: {
904 const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
906 std::optional<TypeSize> alloca_size =
908 if (!alloca_size || alloca_size->isScalable()) {
909 LLDB_LOGF(log,
"AllocaInsts are not handled if size is not computable");
921 Type *Tptr = alloca_inst->getType();
924 alloca_inst->getAlign().value());
927 LLDB_LOGF(log,
"Couldn't allocate memory for an AllocaInst");
936 "Couldn't allocate the result pointer for an AllocaInst");
946 LLDB_LOGF(log,
"Couldn't write the result pointer for an AllocaInst");
949 execution_unit.
Free(P, free_error);
950 execution_unit.
Free(R, free_error);
956 LLDB_LOGF(log,
"Interpreted an AllocaInst");
960 case Instruction::BitCast:
961 case Instruction::ZExt: {
962 const CastInst *cast_inst = cast<CastInst>(inst);
964 Value *source = cast_inst->getOperand(0);
976 case Instruction::SExt: {
977 const CastInst *cast_inst = cast<CastInst>(inst);
979 Value *source = cast_inst->getOperand(0);
995 case Instruction::UncondBr:
996 frame.
Jump(cast<UncondBrInst>(inst)->getSuccessor());
997 LLDB_LOGF(log,
"Interpreted an UncondBrInst");
999 case Instruction::CondBr: {
1000 const CondBrInst *br_inst = cast<CondBrInst>(inst);
1002 Value *condition = br_inst->getCondition();
1013 frame.
Jump(br_inst->getSuccessor(0));
1015 frame.
Jump(br_inst->getSuccessor(1));
1017 LLDB_LOGF(log,
"Interpreted a CondBrInst");
1021 case Instruction::PHI: {
1022 const PHINode *phi_inst = cast<PHINode>(inst);
1025 "Encountered PHI node without having jumped from another "
1032 Value *value = phi_inst->getIncomingValueForBlock(frame.
m_prev_bb);
1041 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1045 case Instruction::GetElementPtr: {
1046 const GetElementPtrInst *gep_inst = cast<GetElementPtrInst>(inst);
1048 const Value *pointer_operand = gep_inst->getPointerOperand();
1049 Type *src_elem_ty = gep_inst->getSourceElementType();
1060 typedef SmallVector<Value *, 8> IndexVector;
1061 typedef IndexVector::iterator IndexIterator;
1063 SmallVector<Value *, 8> indices(gep_inst->idx_begin(),
1064 gep_inst->idx_end());
1066 SmallVector<Value *, 8> const_indices;
1068 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie;
1070 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1072 if (!constant_index) {
1081 LLDB_LOGF(log,
"Evaluated constant index %s as %llu",
1084 constant_index = cast<ConstantInt>(ConstantInt::get(
1088 const_indices.push_back(constant_index);
1092 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
1098 LLDB_LOGF(log,
"Interpreted a GetElementPtrInst");
1103 case Instruction::FCmp:
1104 case Instruction::ICmp: {
1105 const CmpInst *icmp_inst = cast<CmpInst>(inst);
1107 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1109 Value *lhs = inst->getOperand(0);
1110 Value *rhs = inst->getOperand(1);
1129 switch (predicate) {
1132 case CmpInst::ICMP_EQ:
1133 case CmpInst::FCMP_OEQ:
1136 case CmpInst::ICMP_NE:
1137 case CmpInst::FCMP_UNE:
1140 case CmpInst::ICMP_UGT:
1145 case CmpInst::ICMP_UGE:
1150 case CmpInst::FCMP_OGE:
1153 case CmpInst::FCMP_OGT:
1156 case CmpInst::ICMP_ULT:
1161 case CmpInst::FCMP_OLT:
1164 case CmpInst::ICMP_ULE:
1169 case CmpInst::FCMP_OLE:
1172 case CmpInst::ICMP_SGT:
1177 case CmpInst::ICMP_SGE:
1182 case CmpInst::ICMP_SLT:
1187 case CmpInst::ICMP_SLE:
1196 LLDB_LOGF(log,
"Interpreted an ICmpInst");
1201 case Instruction::IntToPtr: {
1202 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1204 Value *src_operand = int_to_ptr_inst->getOperand(0);
1216 LLDB_LOGF(log,
"Interpreted an IntToPtr");
1220 case Instruction::PtrToInt: {
1221 const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
1223 Value *src_operand = ptr_to_int_inst->getOperand(0);
1235 LLDB_LOGF(log,
"Interpreted a PtrToInt");
1239 case Instruction::Trunc: {
1240 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1242 Value *src_operand = trunc_inst->getOperand(0);
1258 case Instruction::FPToUI:
1259 case Instruction::FPToSI: {
1260 Value *src_operand = inst->getOperand(0);
1269 assert(inst->getType()->isIntegerTy() &&
"Unexpected target type");
1270 llvm::APSInt result(inst->getType()->getIntegerBitWidth(),
1271 inst->getOpcode() ==
1272 Instruction::FPToUI);
1274 "Unexpected source type");
1276 llvm::APFloatBase::opStatus status = S.GetAPFloat().convertToInteger(
1277 result, llvm::APFloat::rmTowardZero, &isExact);
1280 if (status & llvm::APFloatBase::opInvalidOp) {
1282 raw_string_ostream rso(s);
1283 rso <<
"Conversion error: " << S <<
" cannot be converted to ";
1284 if (inst->getOpcode() == Instruction::FPToUI)
1286 rso << *inst->getType();
1294 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1298 case Instruction::UIToFP:
1299 case Instruction::SIToFP:
1300 case Instruction::FPTrunc:
1301 case Instruction::FPExt: {
1302 Value *src_operand = inst->getOperand(0);
1312 Type *result_type = inst->getType();
1314 (result_type->isFloatTy() || result_type->isDoubleTy()) &&
1315 "Unsupported result type; CanInterpret() should have checked that");
1316 if (result_type->isFloatTy())
1322 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1326 case Instruction::Load: {
1327 const LoadInst *load_inst = cast<LoadInst>(inst);
1335 const Value *pointer_operand = load_inst->getPointerOperand();
1341 LLDB_LOGF(log,
"LoadInst's value doesn't resolve to anything");
1347 LLDB_LOGF(log,
"LoadInst's pointer doesn't resolve to anything");
1357 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1362 Type *target_ty = load_inst->getType();
1363 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1370 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a LoadInst");
1379 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a LoadInst");
1384 LLDB_LOGF(log,
"Interpreted a LoadInst");
1389 case Instruction::Ret: {
1392 case Instruction::Store: {
1393 const StoreInst *store_inst = cast<StoreInst>(inst);
1401 const Value *value_operand = store_inst->getValueOperand();
1402 const Value *pointer_operand = store_inst->getPointerOperand();
1408 LLDB_LOGF(log,
"StoreInst's value doesn't resolve to anything");
1414 LLDB_LOGF(log,
"StoreInst's pointer doesn't resolve to anything");
1424 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1429 Type *target_ty = value_operand->getType();
1430 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1437 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a StoreInst");
1446 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a StoreInst");
1451 LLDB_LOGF(log,
"Interpreted a StoreInst");
1456 case Instruction::Call: {
1457 const CallInst *call_inst = cast<CallInst>(inst);
1459 if (
auto *fn = call_inst->getCalledFunction()) {
1460 if (fn->isIntrinsic() &&
1461 ((fn->getIntrinsicID() == llvm::Intrinsic::ptrauth_auth &&
1462 call_inst->arg_size() == 3) ||
1463 (fn->getIntrinsicID() == llvm::Intrinsic::ptrauth_strip &&
1464 call_inst->arg_size() == 2)) &&
1466 llvm::Value *auth_ptr = call_inst->getArgOperand(0);
1471 "unable to evaluate argument 0");
1477 abi_sp->FixCodeAddress(auth_ptr_val.
ULongLong()));
1478 frame.
AssignValue(call_inst, stripped_ptr, module);
1482 if (fn->isIntrinsic() &&
1483 fn->getIntrinsicID() == llvm::Intrinsic::ptrauth_blend) {
1484 llvm::Value *addr = call_inst->getArgOperand(0);
1485 llvm::Value *lobits = call_inst->getArgOperand(1);
1491 "unable to evaluate argument %d", 0);
1499 "unable to evaluate argument %d", 1);
1512 llvm::Type *returnType = call_inst->getType();
1513 if (returnType ==
nullptr) {
1515 "unable to access return type");
1520 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1521 !returnType->isPointerTy()) {
1523 "return type is not supported");
1543 const llvm::Value *val = call_inst->getCalledOperand();
1547 "unable to get address of function");
1555 llvm::FunctionType *prototype = call_inst->getFunctionType();
1558 const int numArgs = call_inst->arg_size();
1562 if (numArgs >= 16) {
1564 "function takes too many arguments");
1570 for (
int i = 0; i < numArgs; i++) {
1572 llvm::Value *arg_op = call_inst->getArgOperand(i);
1573 llvm::Type *arg_ty = arg_op->getType();
1576 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1578 "argument %d must be integer type", i);
1586 "unable to evaluate argument %d", i);
1591 if (arg_ty->isPointerTy()) {
1593 size_t dataSize = 0;
1595 bool Success = execution_unit.
GetAllocSize(addr, dataSize);
1598 "unable to locate host data for transfer to device");
1600 rawArgs[i].
size = dataSize;
1601 rawArgs[i].
data_up.reset(
new uint8_t[dataSize + 1]);
1604 execution_unit.
ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1606 assert(!
error.Fail() &&
1607 "we have failed to read the string from memory");
1610 rawArgs[i].
data_up[dataSize] =
'\0';
1616 rawArgs[i].
size = arg_ty->getIntegerBitWidth() / 8;
1623 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1628 exe_ctx.
GetThreadRef(), funcAddr, *prototype, *returnType, args,
1633 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1635 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1647 process->
RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
1652 "ThreadPlanCallFunctionUsingABI failed");
1659 if (returnType->isVoidTy()) {
1663 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1671 if (vobj ==
nullptr || !retVal) {
1673 "unable to get the return value");