32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DataLayout.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/IR/Intrinsics.h"
37 #include "llvm/IR/LLVMContext.h"
38 #include "llvm/IR/Module.h"
39 #include "llvm/IR/Operator.h"
40 #include "llvm/Support/raw_ostream.h"
49 raw_string_ostream rso(s);
53 s.resize(s.length() - 1);
56 while ((offset = s.find(
'\n')) != s.npos)
58 while (s[0] ==
' ' || s[0] ==
'\t')
66 raw_string_ostream rso(s);
70 s.resize(s.length() - 1);
75 const llvm::Function *called_function = call->getCalledFunction();
80 if (called_function->isIntrinsic()) {
81 switch (called_function->getIntrinsicID()) {
84 case llvm::Intrinsic::dbg_declare:
85 case llvm::Intrinsic::dbg_value:
95 typedef std::map<const Value *, lldb::addr_t>
ValueMap;
100 const BasicBlock *m_bb =
nullptr;
101 const BasicBlock *m_prev_bb =
nullptr;
102 BasicBlock::const_iterator
m_ii;
103 BasicBlock::const_iterator
m_ie;
116 : m_target_data(target_data), m_execution_unit(execution_unit) {
119 m_addr_byte_size = (target_data.getPointerSize(0));
121 m_frame_process_address = stack_frame_bottom;
122 m_frame_size = stack_frame_top - stack_frame_bottom;
123 m_stack_pointer = stack_frame_top;
128 void Jump(
const BasicBlock *bb) {
131 m_ii = m_bb->begin();
140 ValueMap::iterator i = m_values.find(value);
142 if (i != m_values.end()) {
145 ss.
Printf(
" 0x%llx", (
unsigned long long)addr);
153 size_t type_size = m_target_data.getTypeStoreSize(type);
159 type_size = PowerOf2Ceil(type_size);
161 scalar = value.zextOrTrunc(type_size * 8);
167 const Constant *constant = dyn_cast<Constant>(value);
172 if (!ResolveConstantValue(value_apint, constant))
175 return AssignToMatchType(scalar, value_apint, value->getType());
178 lldb::addr_t process_address = ResolveValue(value, module);
179 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
184 m_execution_unit.
GetMemoryData(value_extractor, process_address,
185 value_size, extract_error);
191 if (value_size <= 8) {
192 uint64_t u64value = value_extractor.
GetMaxU64(&offset, value_size);
193 return AssignToMatchType(scalar, llvm::APInt(64, u64value),
202 lldb::addr_t process_address = ResolveValue(value, module);
210 if (!AssignToMatchType(cast_scalar, scalar.
UInt128(llvm::APInt()),
214 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
221 m_byte_order, get_data_error))
226 m_execution_unit.
WriteMemory(process_address, buf.GetBytes(),
233 switch (constant->getValueID()) {
236 case Value::FunctionVal:
237 if (
const Function *constant_func = dyn_cast<Function>(constant)) {
239 bool missing_weak =
false;
243 value = APInt(m_target_data.getPointerSizeInBits(), addr);
247 case Value::ConstantIntVal:
248 if (
const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) {
249 value = constant_int->getValue();
253 case Value::ConstantFPVal:
254 if (
const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant)) {
255 value = constant_fp->getValueAPF().bitcastToAPInt();
259 case Value::ConstantExprVal:
260 if (
const ConstantExpr *constant_expr =
261 dyn_cast<ConstantExpr>(constant)) {
262 switch (constant_expr->getOpcode()) {
265 case Instruction::IntToPtr:
266 case Instruction::PtrToInt:
267 case Instruction::BitCast:
268 return ResolveConstantValue(value, constant_expr->getOperand(0));
269 case Instruction::GetElementPtr: {
270 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
271 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
273 Constant *base = dyn_cast<Constant>(*op_cursor);
278 if (!ResolveConstantValue(value, base))
283 if (op_cursor == op_end)
286 SmallVector<Value *, 8> indices(op_cursor, op_end);
288 cast<GEPOperator>(constant_expr)->getSourceElementType();
293 m_target_data.getIndexedOffsetInType(src_elem_ty, indices);
295 const bool is_signed =
true;
296 value += APInt(value.getBitWidth(), offset, is_signed);
303 case Value::ConstantPointerNullVal:
304 if (isa<ConstantPointerNull>(constant)) {
305 value = APInt(m_target_data.getPointerSizeInBits(), 0);
325 m_execution_unit.
Free(data_address, free_error);
329 m_values[value] = data_address;
334 LLDB_LOGF(log,
"Made an allocation for argument %s",
336 LLDB_LOGF(log,
" Data region : %llx", (
unsigned long long)address);
338 (
unsigned long long)data_address);
345 APInt resolved_value;
347 if (!ResolveConstantValue(resolved_value, constant))
350 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
356 resolved_value.zextOrTrunc(llvm::NextPowerOf2(constant_size) * 8));
358 m_byte_order, get_data_error))
363 m_execution_unit.
WriteMemory(process_address, buf.GetBytes(),
373 ret -= (ret % byte_alignment);
375 if (ret < m_frame_process_address)
378 m_stack_pointer = ret;
385 return Malloc(m_target_data.getTypeAllocSize(type),
386 m_target_data.getPrefTypeAlignment(type));
390 size_t length = m_target_data.getTypeStoreSize(type);
396 m_execution_unit.
ReadMemory(buf.GetBytes(), addr, length, read_error);
403 for (
size_t i = 0; i < length; i++) {
404 if ((!(i & 0xf)) && i)
405 ss.
Printf(
"%02hhx - ", buf.GetBytes()[i]);
407 ss.
Printf(
"%02hhx ", buf.GetBytes()[i]);
414 ValueMap::iterator i = m_values.find(value);
416 if (i != m_values.end())
423 if (
const Constant *constant = dyn_cast<Constant>(value)) {
424 if (!ResolveConstant(data_address, constant)) {
426 m_execution_unit.
Free(data_address, free_error);
431 m_values[value] = data_address;
437 "Interpreter doesn't handle one of the expression's opcodes";
439 "Interpreter doesn't handle one of the expression's operands";
441 "Interpreter encountered an internal error";
443 "Interpreter couldn't resolve a value during execution";
445 "Interpreter couldn't allocate memory";
450 "Interpreter doesn't handle modules with multiple function bodies.";
453 switch (constant->getValueID()) {
456 case Value::ConstantIntVal:
457 case Value::ConstantFPVal:
458 case Value::FunctionVal:
460 case Value::ConstantExprVal:
461 if (
const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) {
462 switch (constant_expr->getOpcode()) {
465 case Instruction::IntToPtr:
466 case Instruction::PtrToInt:
467 case Instruction::BitCast:
469 case Instruction::GetElementPtr: {
471 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
472 Constant *base = dyn_cast<Constant>(*op_cursor);
477 for (Value *op : make_range(constant_expr->op_begin() + 1,
478 constant_expr->op_end())) {
479 ConstantInt *constant_int = dyn_cast<ConstantInt>(op);
489 case Value::ConstantPointerNullVal:
496 const bool support_function_calls) {
499 bool saw_function_with_body =
false;
500 for (Function &f : module) {
501 if (f.begin() != f.end()) {
502 if (saw_function_with_body) {
503 LLDB_LOGF(log,
"More than one function in the module has a body");
504 error.SetErrorToGenericError();
508 saw_function_with_body =
true;
512 for (BasicBlock &bb :
function) {
513 for (Instruction &ii : bb) {
514 switch (ii.getOpcode()) {
517 error.SetErrorToGenericError();
521 case Instruction::Add:
522 case Instruction::Alloca:
523 case Instruction::BitCast:
524 case Instruction::Br:
525 case Instruction::PHI:
527 case Instruction::Call: {
528 CallInst *call_inst = dyn_cast<CallInst>(&ii);
531 error.SetErrorToGenericError();
537 LLDB_LOGF(log,
"Unsupported instruction: %s",
539 error.SetErrorToGenericError();
544 case Instruction::GetElementPtr:
546 case Instruction::ICmp: {
547 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(&ii);
550 error.SetErrorToGenericError();
555 switch (icmp_inst->getPredicate()) {
557 LLDB_LOGF(log,
"Unsupported ICmp predicate: %s",
560 error.SetErrorToGenericError();
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:
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:
600 for (
unsigned oi = 0, oe = ii.getNumOperands(); oi != oe; ++oi) {
601 Value *operand = ii.getOperand(oi);
602 Type *operand_type = operand->getType();
604 switch (operand_type->getTypeID()) {
607 case Type::FixedVectorTyID:
608 case Type::ScalableVectorTyID: {
609 LLDB_LOGF(log,
"Unsupported operand type: %s",
620 if (operand_type->getPrimitiveSizeInBits() > 64) {
621 LLDB_LOGF(log,
"Unsupported operand type: %s",
627 if (Constant *constant = llvm::dyn_cast<Constant>(operand)) {
629 LLDB_LOGF(log,
"Unsupported constant: %s",
643 llvm::ArrayRef<lldb::addr_t> args,
653 raw_string_ostream oss(s);
655 module.print(oss,
nullptr);
659 LLDB_LOGF(log,
"Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
663 DataLayout data_layout(&module);
669 error.SetErrorString(
"Couldn't allocate stack frame");
674 for (llvm::Function::arg_iterator ai =
function.arg_begin(),
675 ae =
function.arg_end();
676 ai != ae; ++ai, ++arg_index) {
677 if (args.size() <=
static_cast<size_t>(arg_index)) {
678 error.SetErrorString(
"Not enough arguments passed in to function");
689 frame.
Jump(&
function.front());
691 while (frame.
m_ii != frame.
m_ie && (++num_insts < 4096)) {
692 const Instruction *inst = &*frame.
m_ii;
696 switch (inst->getOpcode()) {
700 case Instruction::Add:
701 case Instruction::Sub:
702 case Instruction::Mul:
703 case Instruction::SDiv:
704 case Instruction::UDiv:
705 case Instruction::SRem:
706 case Instruction::URem:
707 case Instruction::Shl:
708 case Instruction::LShr:
709 case Instruction::AShr:
710 case Instruction::And:
711 case Instruction::Or:
712 case Instruction::Xor: {
713 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
718 "getOpcode() returns %s, but instruction is not a BinaryOperator",
719 inst->getOpcodeName());
720 error.SetErrorToGenericError();
725 Value *lhs = inst->getOperand(0);
726 Value *rhs = inst->getOperand(1);
733 error.SetErrorToGenericError();
740 error.SetErrorToGenericError();
747 switch (inst->getOpcode()) {
750 case Instruction::Add:
753 case Instruction::Mul:
756 case Instruction::Sub:
759 case Instruction::SDiv:
764 case Instruction::UDiv:
769 case Instruction::SRem:
774 case Instruction::URem:
779 case Instruction::Shl:
782 case Instruction::AShr:
785 case Instruction::LShr:
789 case Instruction::And:
792 case Instruction::Or:
795 case Instruction::Xor:
803 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
809 case Instruction::Alloca: {
810 const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
812 if (alloca_inst->isArrayAllocation()) {
814 "AllocaInsts are not handled if isArrayAllocation() is true");
815 error.SetErrorToGenericError();
827 Type *T = alloca_inst->getAllocatedType();
828 Type *Tptr = alloca_inst->getType();
833 LLDB_LOGF(log,
"Couldn't allocate memory for an AllocaInst");
834 error.SetErrorToGenericError();
843 "Couldn't allocate the result pointer for an AllocaInst");
844 error.SetErrorToGenericError();
854 LLDB_LOGF(log,
"Couldn't write the result pointer for an AllocaInst");
855 error.SetErrorToGenericError();
858 execution_unit.
Free(P, free_error);
859 execution_unit.
Free(R, free_error);
866 LLDB_LOGF(log,
"Interpreted an AllocaInst");
871 case Instruction::BitCast:
872 case Instruction::ZExt: {
873 const CastInst *cast_inst = cast<CastInst>(inst);
875 Value *source = cast_inst->getOperand(0);
881 error.SetErrorToGenericError();
888 case Instruction::SExt: {
889 const CastInst *cast_inst = cast<CastInst>(inst);
891 Value *source = cast_inst->getOperand(0);
897 error.SetErrorToGenericError();
908 case Instruction::Br: {
909 const BranchInst *br_inst = cast<BranchInst>(inst);
911 if (br_inst->isConditional()) {
912 Value *condition = br_inst->getCondition();
918 error.SetErrorToGenericError();
924 frame.
Jump(br_inst->getSuccessor(0));
926 frame.
Jump(br_inst->getSuccessor(1));
929 LLDB_LOGF(log,
"Interpreted a BrInst with a condition");
934 frame.
Jump(br_inst->getSuccessor(0));
937 LLDB_LOGF(log,
"Interpreted a BrInst with no condition");
942 case Instruction::PHI: {
943 const PHINode *phi_inst = cast<PHINode>(inst);
946 "Encountered PHI node without having jumped from another "
948 error.SetErrorToGenericError();
953 Value *value = phi_inst->getIncomingValueForBlock(frame.
m_prev_bb);
957 error.SetErrorToGenericError();
964 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
969 case Instruction::GetElementPtr: {
970 const GetElementPtrInst *gep_inst = cast<GetElementPtrInst>(inst);
972 const Value *pointer_operand = gep_inst->getPointerOperand();
973 Type *src_elem_ty = gep_inst->getSourceElementType();
980 error.SetErrorToGenericError();
985 typedef SmallVector<Value *, 8> IndexVector;
986 typedef IndexVector::iterator IndexIterator;
988 SmallVector<Value *, 8> indices(gep_inst->idx_begin(),
989 gep_inst->idx_end());
991 SmallVector<Value *, 8> const_indices;
993 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie;
995 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
997 if (!constant_index) {
1002 error.SetErrorToGenericError();
1007 LLDB_LOGF(log,
"Evaluated constant index %s as %llu",
1010 constant_index = cast<ConstantInt>(ConstantInt::get(
1014 const_indices.push_back(constant_index);
1018 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
1025 LLDB_LOGF(log,
"Interpreted a GetElementPtrInst");
1031 case Instruction::ICmp: {
1032 const ICmpInst *icmp_inst = cast<ICmpInst>(inst);
1034 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1036 Value *lhs = inst->getOperand(0);
1037 Value *rhs = inst->getOperand(1);
1044 error.SetErrorToGenericError();
1051 error.SetErrorToGenericError();
1058 switch (predicate) {
1061 case CmpInst::ICMP_EQ:
1064 case CmpInst::ICMP_NE:
1067 case CmpInst::ICMP_UGT:
1072 case CmpInst::ICMP_UGE:
1077 case CmpInst::ICMP_ULT:
1082 case CmpInst::ICMP_ULE:
1087 case CmpInst::ICMP_SGT:
1092 case CmpInst::ICMP_SGE:
1097 case CmpInst::ICMP_SLT:
1102 case CmpInst::ICMP_SLE:
1112 LLDB_LOGF(log,
"Interpreted an ICmpInst");
1118 case Instruction::IntToPtr: {
1119 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1121 Value *src_operand = int_to_ptr_inst->getOperand(0);
1127 error.SetErrorToGenericError();
1135 LLDB_LOGF(log,
"Interpreted an IntToPtr");
1140 case Instruction::PtrToInt: {
1141 const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
1143 Value *src_operand = ptr_to_int_inst->getOperand(0);
1149 error.SetErrorToGenericError();
1157 LLDB_LOGF(log,
"Interpreted a PtrToInt");
1162 case Instruction::Trunc: {
1163 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1165 Value *src_operand = trunc_inst->getOperand(0);
1171 error.SetErrorToGenericError();
1184 case Instruction::Load: {
1185 const LoadInst *load_inst = cast<LoadInst>(inst);
1193 const Value *pointer_operand = load_inst->getPointerOperand();
1199 LLDB_LOGF(log,
"LoadInst's value doesn't resolve to anything");
1200 error.SetErrorToGenericError();
1206 LLDB_LOGF(log,
"LoadInst's pointer doesn't resolve to anything");
1207 error.SetErrorToGenericError();
1217 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1218 error.SetErrorToGenericError();
1223 Type *target_ty = load_inst->getType();
1224 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1231 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a LoadInst");
1232 error.SetErrorToGenericError();
1241 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a LoadInst");
1242 error.SetErrorToGenericError();
1248 LLDB_LOGF(log,
"Interpreted a LoadInst");
1254 case Instruction::Ret: {
1257 case Instruction::Store: {
1258 const StoreInst *store_inst = cast<StoreInst>(inst);
1266 const Value *value_operand = store_inst->getValueOperand();
1267 const Value *pointer_operand = store_inst->getPointerOperand();
1273 LLDB_LOGF(log,
"StoreInst's value doesn't resolve to anything");
1274 error.SetErrorToGenericError();
1280 LLDB_LOGF(log,
"StoreInst's pointer doesn't resolve to anything");
1281 error.SetErrorToGenericError();
1291 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1292 error.SetErrorToGenericError();
1297 Type *target_ty = value_operand->getType();
1298 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1305 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a StoreInst");
1306 error.SetErrorToGenericError();
1315 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a StoreInst");
1316 error.SetErrorToGenericError();
1322 LLDB_LOGF(log,
"Interpreted a StoreInst");
1328 case Instruction::Call: {
1329 const CallInst *call_inst = cast<CallInst>(inst);
1335 llvm::Type *returnType = call_inst->getType();
1336 if (returnType ==
nullptr) {
1337 error.SetErrorToGenericError();
1338 error.SetErrorString(
"unable to access return type");
1343 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1344 !returnType->isPointerTy()) {
1345 error.SetErrorToGenericError();
1346 error.SetErrorString(
"return type is not supported");
1352 error.SetErrorToGenericError();
1353 error.SetErrorString(
"unable to acquire thread");
1359 error.SetErrorToGenericError();
1360 error.SetErrorString(
"unable to get the process");
1366 const llvm::Value *val = call_inst->getCalledOperand();
1369 error.SetErrorToGenericError();
1370 error.SetErrorString(
"unable to get address of function");
1378 llvm::FunctionType *prototype = call_inst->getFunctionType();
1381 const int numArgs = call_inst->arg_size();
1385 if (numArgs >= 16) {
1386 error.SetErrorToGenericError();
1387 error.SetErrorString(
"function takes too many arguments");
1393 for (
int i = 0; i < numArgs; i++) {
1395 llvm::Value *arg_op = call_inst->getArgOperand(i);
1396 llvm::Type *arg_ty = arg_op->getType();
1399 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1400 error.SetErrorToGenericError();
1401 error.SetErrorStringWithFormat(
"argument %d must be integer type", i);
1408 error.SetErrorToGenericError();
1409 error.SetErrorStringWithFormat(
"unable to evaluate argument %d", i);
1414 if (arg_ty->isPointerTy()) {
1416 size_t dataSize = 0;
1418 bool Success = execution_unit.
GetAllocSize(addr, dataSize);
1421 "unable to locate host data for transfer to device");
1423 rawArgs[i].
size = dataSize;
1424 rawArgs[i].
data_up.reset(
new uint8_t[dataSize + 1]);
1427 execution_unit.
ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1429 assert(!
error.Fail() &&
1430 "we have failed to read the string from memory");
1433 rawArgs[i].
data_up[dataSize] =
'\0';
1439 rawArgs[i].
size = arg_ty->getIntegerBitWidth() / 8;
1446 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1449 lldb::ThreadPlanSP call_plan_sp(
1451 exe_ctx.
GetThreadRef(), funcAddr, *prototype, *returnType, args,
1456 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1457 error.SetErrorToGenericError();
1458 error.SetErrorStringWithFormat(
1459 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1468 exe_ctx, call_plan_sp, options, diagnostics);
1472 error.SetErrorToGenericError();
1473 error.SetErrorString(
"ThreadPlanCallFunctionUsingABI failed");
1480 if (returnType->isVoidTy()) {
1484 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1486 lldb::ValueObjectSP retVal = call_plan_sp.get()->GetReturnValueObject();
1492 if (vobj ==
nullptr || !retVal) {
1493 error.SetErrorToGenericError();
1494 error.SetErrorString(
"unable to get the return value");
1511 if (num_insts >= 4096) {
1512 error.SetErrorToGenericError();