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