blob: aeb0351c860df54eb8216358aaac0b1b873a228d [file] [log] [blame]
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001//===-- IRInterpreter.cpp ---------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sean Callanan3bfdaa22011-09-15 02:13:07 +00006//
7//===----------------------------------------------------------------------===//
8
Sean Callanan579e70c2016-03-19 00:03:59 +00009#include "lldb/Expression/IRInterpreter.h"
Ewan Crawford90ff7912015-07-14 10:56:58 +000010#include "lldb/Core/Module.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000011#include "lldb/Core/ModuleSpec.h"
Ewan Crawford90ff7912015-07-14 10:56:58 +000012#include "lldb/Core/ValueObject.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000013#include "lldb/Expression/DiagnosticManager.h"
Ted Woodward7071c5fd2016-03-01 21:53:26 +000014#include "lldb/Expression/IRExecutionUnit.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000015#include "lldb/Expression/IRMemoryMap.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000016#include "lldb/Utility/ConstString.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000017#include "lldb/Utility/DataExtractor.h"
Zachary Turner01c32432017-02-14 19:06:07 +000018#include "lldb/Utility/Endian.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000019#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000020#include "lldb/Utility/Scalar.h"
Zachary Turner97206d52017-05-12 04:51:55 +000021#include "lldb/Utility/Status.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000022#include "lldb/Utility/StreamString.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000023
Ewan Crawford90ff7912015-07-14 10:56:58 +000024#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
Chandler Carruth1e157582013-01-02 12:20:07 +000031#include "llvm/IR/Constants.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000032#include "llvm/IR/DataLayout.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000033#include "llvm/IR/Function.h"
34#include "llvm/IR/Instructions.h"
Sean Callanan576a4372014-03-25 19:33:15 +000035#include "llvm/IR/Intrinsics.h"
Ewan Crawford90ff7912015-07-14 10:56:58 +000036#include "llvm/IR/LLVMContext.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000037#include "llvm/IR/Module.h"
Eduard Burtescud05b8992016-01-22 03:43:23 +000038#include "llvm/IR/Operator.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000039#include "llvm/Support/raw_ostream.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000040
41#include <map>
42
43using namespace llvm;
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045static 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);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 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);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 return s;
Sean Callanan3bfdaa22011-09-15 02:13:07 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062static 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;
Sean Callanan3bfdaa22011-09-15 02:13:07 +000070}
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072static bool CanIgnoreCall(const CallInst *call) {
73 const llvm::Function *called_function = call->getCalledFunction();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 if (!called_function)
Sean Callanan576a4372014-03-25 19:33:15 +000076 return false;
Sean Callanan576a4372014-03-25 19:33:15 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 if (called_function->isIntrinsic()) {
79 switch (called_function->getIntrinsicID()) {
Sean Callanan8c62daf2016-02-12 21:16:58 +000080 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 break;
82 case llvm::Intrinsic::dbg_declare:
83 case llvm::Intrinsic::dbg_value:
84 return true;
Sean Callanan8c62daf2016-02-12 21:16:58 +000085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 }
87
88 return false;
Sean Callanan8c62daf2016-02-12 21:16:58 +000089}
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091class InterpreterStackFrame {
92public:
93 typedef std::map<const Value *, lldb::addr_t> ValueMap;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 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;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 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);
Sean Callanan85fc8762013-06-27 01:59:51 +0000145 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 return ss.GetString();
148 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 bool AssignToMatchType(lldb_private::Scalar &scalar, uint64_t u64value,
151 Type *type) {
152 size_t type_size = m_target_data.getTypeStoreSize(type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000153
Frederic Rissae6ca2f2018-08-28 22:50:01 +0000154 if (type_size > 8)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000156
Frederic Rissae6ca2f2018-08-28 22:50:01 +0000157 if (type_size != 1)
158 type_size = PowerOf2Ceil(type_size);
159
160 scalar = llvm::APInt(type_size*8, u64value);
Adrian McCarthy3f998102016-05-04 23:32:35 +0000161 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 }
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 bool EvaluateValue(lldb_private::Scalar &scalar, const Value *value,
165 Module &module) {
166 const Constant *constant = dyn_cast<Constant>(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 if (constant) {
169 APInt value_apint;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 if (!ResolveConstantValue(value_apint, constant))
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000172 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173
174 return AssignToMatchType(scalar, value_apint.getLimitedValue(),
175 value->getType());
176 } else {
177 lldb::addr_t process_address = ResolveValue(value, module);
178 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
179
180 lldb_private::DataExtractor value_extractor;
Zachary Turner97206d52017-05-12 04:51:55 +0000181 lldb_private::Status extract_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182
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;
Frederic Rissae6ca2f2018-08-28 22:50:01 +0000190 if (value_size <= 8) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
192 return AssignToMatchType(scalar, u64value, value->getType());
193 }
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000194 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000195
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000196 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 }
198
199 bool AssignValue(const Value *value, lldb_private::Scalar &scalar,
200 Module &module) {
201 lldb::addr_t process_address = ResolveValue(value, module);
202
203 if (process_address == LLDB_INVALID_ADDRESS)
204 return false;
205
206 lldb_private::Scalar cast_scalar;
207
208 if (!AssignToMatchType(cast_scalar, scalar.ULongLong(), value->getType()))
209 return false;
210
211 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
212
213 lldb_private::DataBufferHeap buf(value_byte_size, 0);
214
Zachary Turner97206d52017-05-12 04:51:55 +0000215 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216
217 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(),
218 m_byte_order, get_data_error))
219 return false;
220
Zachary Turner97206d52017-05-12 04:51:55 +0000221 lldb_private::Status write_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222
223 m_execution_unit.WriteMemory(process_address, buf.GetBytes(),
224 buf.GetByteSize(), write_error);
225
226 return write_error.Success();
227 }
228
229 bool ResolveConstantValue(APInt &value, const Constant *constant) {
230 switch (constant->getValueID()) {
231 default:
232 break;
233 case Value::FunctionVal:
234 if (const Function *constant_func = dyn_cast<Function>(constant)) {
235 lldb_private::ConstString name(constant_func->getName());
Jim Inghamf2128b22019-06-28 21:40:05 +0000236 bool missing_weak = false;
237 lldb::addr_t addr = m_execution_unit.FindSymbol(name, missing_weak);
238 if (addr == LLDB_INVALID_ADDRESS || missing_weak)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 return false;
240 value = APInt(m_target_data.getPointerSizeInBits(), addr);
241 return true;
242 }
243 break;
244 case Value::ConstantIntVal:
245 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) {
246 value = constant_int->getValue();
247 return true;
248 }
249 break;
250 case Value::ConstantFPVal:
251 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant)) {
252 value = constant_fp->getValueAPF().bitcastToAPInt();
253 return true;
254 }
255 break;
256 case Value::ConstantExprVal:
257 if (const ConstantExpr *constant_expr =
258 dyn_cast<ConstantExpr>(constant)) {
259 switch (constant_expr->getOpcode()) {
260 default:
261 return false;
262 case Instruction::IntToPtr:
263 case Instruction::PtrToInt:
264 case Instruction::BitCast:
265 return ResolveConstantValue(value, constant_expr->getOperand(0));
266 case Instruction::GetElementPtr: {
267 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
268 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
269
270 Constant *base = dyn_cast<Constant>(*op_cursor);
271
272 if (!base)
273 return false;
274
275 if (!ResolveConstantValue(value, base))
276 return false;
277
278 op_cursor++;
279
280 if (op_cursor == op_end)
281 return true; // no offset to apply!
282
283 SmallVector<Value *, 8> indices(op_cursor, op_end);
284
285 Type *src_elem_ty =
286 cast<GEPOperator>(constant_expr)->getSourceElementType();
287 uint64_t offset =
288 m_target_data.getIndexedOffsetInType(src_elem_ty, indices);
289
290 const bool is_signed = true;
291 value += APInt(value.getBitWidth(), offset, is_signed);
292
293 return true;
294 }
295 }
296 }
297 break;
298 case Value::ConstantPointerNullVal:
299 if (isa<ConstantPointerNull>(constant)) {
300 value = APInt(m_target_data.getPointerSizeInBits(), 0);
301 return true;
302 }
303 break;
304 }
305 return false;
306 }
307
308 bool MakeArgument(const Argument *value, uint64_t address) {
309 lldb::addr_t data_address = Malloc(value->getType());
310
311 if (data_address == LLDB_INVALID_ADDRESS)
312 return false;
313
Zachary Turner97206d52017-05-12 04:51:55 +0000314 lldb_private::Status write_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315
316 m_execution_unit.WritePointerToMemory(data_address, address, write_error);
317
318 if (!write_error.Success()) {
Zachary Turner97206d52017-05-12 04:51:55 +0000319 lldb_private::Status free_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 m_execution_unit.Free(data_address, free_error);
321 return false;
322 }
323
324 m_values[value] = data_address;
325
326 lldb_private::Log *log(
327 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
328
329 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000330 LLDB_LOGF(log, "Made an allocation for argument %s",
331 PrintValue(value).c_str());
332 LLDB_LOGF(log, " Data region : %llx", (unsigned long long)address);
333 LLDB_LOGF(log, " Ref region : %llx",
334 (unsigned long long)data_address);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 }
336
337 return true;
338 }
339
340 bool ResolveConstant(lldb::addr_t process_address, const Constant *constant) {
341 APInt resolved_value;
342
343 if (!ResolveConstantValue(resolved_value, constant))
344 return false;
345
346 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
347 lldb_private::DataBufferHeap buf(constant_size, 0);
348
Zachary Turner97206d52017-05-12 04:51:55 +0000349 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350
351 lldb_private::Scalar resolved_scalar(
352 resolved_value.zextOrTrunc(llvm::NextPowerOf2(constant_size) * 8));
353 if (!resolved_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(),
354 m_byte_order, get_data_error))
355 return false;
356
Zachary Turner97206d52017-05-12 04:51:55 +0000357 lldb_private::Status write_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358
359 m_execution_unit.WriteMemory(process_address, buf.GetBytes(),
360 buf.GetByteSize(), write_error);
361
362 return write_error.Success();
363 }
364
365 lldb::addr_t Malloc(size_t size, uint8_t byte_alignment) {
366 lldb::addr_t ret = m_stack_pointer;
367
368 ret -= size;
369 ret -= (ret % byte_alignment);
370
371 if (ret < m_frame_process_address)
372 return LLDB_INVALID_ADDRESS;
373
374 m_stack_pointer = ret;
375 return ret;
376 }
377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 lldb::addr_t Malloc(llvm::Type *type) {
Zachary Turner97206d52017-05-12 04:51:55 +0000379 lldb_private::Status alloc_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380
381 return Malloc(m_target_data.getTypeAllocSize(type),
382 m_target_data.getPrefTypeAlignment(type));
383 }
384
385 std::string PrintData(lldb::addr_t addr, llvm::Type *type) {
386 size_t length = m_target_data.getTypeStoreSize(type);
387
388 lldb_private::DataBufferHeap buf(length, 0);
389
Zachary Turner97206d52017-05-12 04:51:55 +0000390 lldb_private::Status read_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391
392 m_execution_unit.ReadMemory(buf.GetBytes(), addr, length, read_error);
393
394 if (!read_error.Success())
395 return std::string("<couldn't read data>");
396
397 lldb_private::StreamString ss;
398
399 for (size_t i = 0; i < length; i++) {
400 if ((!(i & 0xf)) && i)
401 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
402 else
403 ss.Printf("%02hhx ", buf.GetBytes()[i]);
404 }
405
406 return ss.GetString();
407 }
408
409 lldb::addr_t ResolveValue(const Value *value, Module &module) {
410 ValueMap::iterator i = m_values.find(value);
411
412 if (i != m_values.end())
413 return i->second;
414
415 // Fall back and allocate space [allocation type Alloca]
416
417 lldb::addr_t data_address = Malloc(value->getType());
418
419 if (const Constant *constant = dyn_cast<Constant>(value)) {
420 if (!ResolveConstant(data_address, constant)) {
Zachary Turner97206d52017-05-12 04:51:55 +0000421 lldb_private::Status free_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 m_execution_unit.Free(data_address, free_error);
423 return LLDB_INVALID_ADDRESS;
424 }
425 }
426
427 m_values[value] = data_address;
428 return data_address;
429 }
430};
431
432static const char *unsupported_opcode_error =
433 "Interpreter doesn't handle one of the expression's opcodes";
434static const char *unsupported_operand_error =
435 "Interpreter doesn't handle one of the expression's operands";
436// static const char *interpreter_initialization_error = "Interpreter couldn't
437// be initialized";
438static const char *interpreter_internal_error =
439 "Interpreter encountered an internal error";
440static const char *bad_value_error =
441 "Interpreter couldn't resolve a value during execution";
442static const char *memory_allocation_error =
443 "Interpreter couldn't allocate memory";
444static const char *memory_write_error = "Interpreter couldn't write to memory";
445static const char *memory_read_error = "Interpreter couldn't read from memory";
446static const char *infinite_loop_error = "Interpreter ran for too many cycles";
447// static const char *bad_result_error = "Result of expression
448// is in bad memory";
449static const char *too_many_functions_error =
450 "Interpreter doesn't handle modules with multiple function bodies.";
451
452static 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 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
471 Constant *base = dyn_cast<Constant>(*op_cursor);
472 if (!base)
473 return false;
474
475 return CanResolveConstant(base);
476 }
477 }
478 } else {
479 return false;
480 }
481 case Value::ConstantPointerNullVal:
482 return true;
483 }
484}
485
486bool IRInterpreter::CanInterpret(llvm::Module &module, llvm::Function &function,
Zachary Turner97206d52017-05-12 04:51:55 +0000487 lldb_private::Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 const bool support_function_calls) {
489 lldb_private::Log *log(
490 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
491
492 bool saw_function_with_body = false;
Raphael Isemannaf4adb02020-01-13 11:05:53 +0100493 for (Function &f : module) {
494 if (f.begin() != f.end()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 if (saw_function_with_body) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000496 LLDB_LOGF(log, "More than one function in the module has a body");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 error.SetErrorToGenericError();
498 error.SetErrorString(too_many_functions_error);
499 return false;
500 }
501 saw_function_with_body = true;
502 }
503 }
504
Raphael Isemannaf4adb02020-01-13 11:05:53 +0100505 for (BasicBlock &bb : function) {
506 for (Instruction &ii : bb) {
507 switch (ii.getOpcode()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 default: {
Raphael Isemannaf4adb02020-01-13 11:05:53 +0100509 LLDB_LOGF(log, "Unsupported instruction: %s", PrintValue(&ii).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 error.SetErrorToGenericError();
511 error.SetErrorString(unsupported_opcode_error);
512 return false;
513 }
514 case Instruction::Add:
515 case Instruction::Alloca:
516 case Instruction::BitCast:
517 case Instruction::Br:
518 case Instruction::PHI:
519 break;
520 case Instruction::Call: {
Raphael Isemannaf4adb02020-01-13 11:05:53 +0100521 CallInst *call_inst = dyn_cast<CallInst>(&ii);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522
523 if (!call_inst) {
524 error.SetErrorToGenericError();
525 error.SetErrorString(interpreter_internal_error);
526 return false;
527 }
528
529 if (!CanIgnoreCall(call_inst) && !support_function_calls) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000530 LLDB_LOGF(log, "Unsupported instruction: %s",
Raphael Isemannaf4adb02020-01-13 11:05:53 +0100531 PrintValue(&ii).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 error.SetErrorToGenericError();
533 error.SetErrorString(unsupported_opcode_error);
534 return false;
535 }
536 } break;
537 case Instruction::GetElementPtr:
538 break;
539 case Instruction::ICmp: {
Raphael Isemannaf4adb02020-01-13 11:05:53 +0100540 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(&ii);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541
542 if (!icmp_inst) {
543 error.SetErrorToGenericError();
544 error.SetErrorString(interpreter_internal_error);
545 return false;
546 }
547
548 switch (icmp_inst->getPredicate()) {
549 default: {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000550 LLDB_LOGF(log, "Unsupported ICmp predicate: %s",
Raphael Isemannaf4adb02020-01-13 11:05:53 +0100551 PrintValue(&ii).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552
553 error.SetErrorToGenericError();
554 error.SetErrorString(unsupported_opcode_error);
555 return false;
556 }
557 case CmpInst::ICMP_EQ:
558 case CmpInst::ICMP_NE:
559 case CmpInst::ICMP_UGT:
560 case CmpInst::ICMP_UGE:
561 case CmpInst::ICMP_ULT:
562 case CmpInst::ICMP_ULE:
563 case CmpInst::ICMP_SGT:
564 case CmpInst::ICMP_SGE:
565 case CmpInst::ICMP_SLT:
566 case CmpInst::ICMP_SLE:
567 break;
568 }
569 } break;
570 case Instruction::And:
571 case Instruction::AShr:
572 case Instruction::IntToPtr:
573 case Instruction::PtrToInt:
574 case Instruction::Load:
575 case Instruction::LShr:
576 case Instruction::Mul:
577 case Instruction::Or:
578 case Instruction::Ret:
579 case Instruction::SDiv:
580 case Instruction::SExt:
581 case Instruction::Shl:
582 case Instruction::SRem:
583 case Instruction::Store:
584 case Instruction::Sub:
585 case Instruction::Trunc:
586 case Instruction::UDiv:
587 case Instruction::URem:
588 case Instruction::Xor:
589 case Instruction::ZExt:
590 break;
591 }
592
Raphael Isemannaf4adb02020-01-13 11:05:53 +0100593 for (unsigned oi = 0, oe = ii.getNumOperands(); oi != oe; ++oi) {
594 Value *operand = ii.getOperand(oi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 Type *operand_type = operand->getType();
596
Davide Italianod405d272018-09-17 18:14:38 +0000597 switch (operand_type->getTypeID()) {
598 default:
599 break;
600 case Type::VectorTyID: {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000601 LLDB_LOGF(log, "Unsupported operand type: %s",
602 PrintType(operand_type).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603 error.SetErrorString(unsupported_operand_error);
604 return false;
Davide Italianod405d272018-09-17 18:14:38 +0000605 }
606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607
Davide Italiano5f6789e2018-09-14 18:55:31 +0000608 // The IR interpreter currently doesn't know about
609 // 128-bit integers. As they're not that frequent,
610 // we can just fall back to the JIT rather than
611 // choking.
612 if (operand_type->getPrimitiveSizeInBits() > 64) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000613 LLDB_LOGF(log, "Unsupported operand type: %s",
614 PrintType(operand_type).c_str());
Davide Italiano5f6789e2018-09-14 18:55:31 +0000615 error.SetErrorString(unsupported_operand_error);
616 return false;
617 }
618
Davide Italianod405d272018-09-17 18:14:38 +0000619 if (Constant *constant = llvm::dyn_cast<Constant>(operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 if (!CanResolveConstant(constant)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000621 LLDB_LOGF(log, "Unsupported constant: %s",
622 PrintValue(constant).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 error.SetErrorString(unsupported_operand_error);
624 return false;
625 }
626 }
627 }
628 }
629 }
630
631 return true;
632}
633
634bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
635 llvm::ArrayRef<lldb::addr_t> args,
636 lldb_private::IRExecutionUnit &execution_unit,
Zachary Turner97206d52017-05-12 04:51:55 +0000637 lldb_private::Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 lldb::addr_t stack_frame_bottom,
639 lldb::addr_t stack_frame_top,
640 lldb_private::ExecutionContext &exe_ctx) {
641 lldb_private::Log *log(
642 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
643
644 if (log) {
645 std::string s;
646 raw_string_ostream oss(s);
647
Konrad Kleine248a1302019-05-23 11:14:47 +0000648 module.print(oss, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000649
650 oss.flush();
651
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000652 LLDB_LOGF(log, "Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
653 s.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 }
655
656 DataLayout data_layout(&module);
657
658 InterpreterStackFrame frame(data_layout, execution_unit, stack_frame_bottom,
659 stack_frame_top);
660
661 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS) {
662 error.SetErrorString("Couldn't allocate stack frame");
663 }
664
665 int arg_index = 0;
666
667 for (llvm::Function::arg_iterator ai = function.arg_begin(),
668 ae = function.arg_end();
669 ai != ae; ++ai, ++arg_index) {
670 if (args.size() <= static_cast<size_t>(arg_index)) {
671 error.SetErrorString("Not enough arguments passed in to function");
672 return false;
673 }
674
675 lldb::addr_t ptr = args[arg_index];
676
677 frame.MakeArgument(&*ai, ptr);
678 }
679
680 uint32_t num_insts = 0;
681
682 frame.Jump(&function.front());
683
684 while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) {
685 const Instruction *inst = &*frame.m_ii;
686
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000687 LLDB_LOGF(log, "Interpreting %s", PrintValue(inst).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688
689 switch (inst->getOpcode()) {
690 default:
691 break;
692
693 case Instruction::Add:
694 case Instruction::Sub:
695 case Instruction::Mul:
696 case Instruction::SDiv:
697 case Instruction::UDiv:
698 case Instruction::SRem:
699 case Instruction::URem:
700 case Instruction::Shl:
701 case Instruction::LShr:
702 case Instruction::AShr:
703 case Instruction::And:
704 case Instruction::Or:
705 case Instruction::Xor: {
706 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
707
708 if (!bin_op) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000709 LLDB_LOGF(
710 log,
711 "getOpcode() returns %s, but instruction is not a BinaryOperator",
712 inst->getOpcodeName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 error.SetErrorToGenericError();
714 error.SetErrorString(interpreter_internal_error);
715 return false;
716 }
717
718 Value *lhs = inst->getOperand(0);
719 Value *rhs = inst->getOperand(1);
720
721 lldb_private::Scalar L;
722 lldb_private::Scalar R;
723
724 if (!frame.EvaluateValue(L, lhs, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000725 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(lhs).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 error.SetErrorToGenericError();
727 error.SetErrorString(bad_value_error);
728 return false;
729 }
730
731 if (!frame.EvaluateValue(R, rhs, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000732 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(rhs).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 error.SetErrorToGenericError();
734 error.SetErrorString(bad_value_error);
735 return false;
736 }
737
738 lldb_private::Scalar result;
739
740 switch (inst->getOpcode()) {
741 default:
742 break;
743 case Instruction::Add:
744 result = L + R;
745 break;
746 case Instruction::Mul:
747 result = L * R;
748 break;
749 case Instruction::Sub:
750 result = L - R;
751 break;
752 case Instruction::SDiv:
753 L.MakeSigned();
754 R.MakeSigned();
755 result = L / R;
756 break;
757 case Instruction::UDiv:
758 L.MakeUnsigned();
759 R.MakeUnsigned();
760 result = L / R;
761 break;
762 case Instruction::SRem:
763 L.MakeSigned();
764 R.MakeSigned();
765 result = L % R;
766 break;
767 case Instruction::URem:
768 L.MakeUnsigned();
769 R.MakeUnsigned();
770 result = L % R;
771 break;
772 case Instruction::Shl:
773 result = L << R;
774 break;
775 case Instruction::AShr:
776 result = L >> R;
777 break;
778 case Instruction::LShr:
779 result = L;
780 result.ShiftRightLogical(R);
781 break;
782 case Instruction::And:
783 result = L & R;
784 break;
785 case Instruction::Or:
786 result = L | R;
787 break;
788 case Instruction::Xor:
789 result = L ^ R;
790 break;
791 }
792
793 frame.AssignValue(inst, result, module);
794
795 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000796 LLDB_LOGF(log, "Interpreted a %s", inst->getOpcodeName());
797 LLDB_LOGF(log, " L : %s", frame.SummarizeValue(lhs).c_str());
798 LLDB_LOGF(log, " R : %s", frame.SummarizeValue(rhs).c_str());
799 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000800 }
801 } break;
802 case Instruction::Alloca: {
803 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
804
805 if (!alloca_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000806 LLDB_LOGF(log, "getOpcode() returns Alloca, but instruction is not an "
807 "AllocaInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000808 error.SetErrorToGenericError();
809 error.SetErrorString(interpreter_internal_error);
810 return false;
811 }
812
813 if (alloca_inst->isArrayAllocation()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000814 LLDB_LOGF(log,
815 "AllocaInsts are not handled if isArrayAllocation() is true");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816 error.SetErrorToGenericError();
817 error.SetErrorString(unsupported_opcode_error);
818 return false;
819 }
820
821 // The semantics of Alloca are:
822 // Create a region R of virtual memory of type T, backed by a data
823 // buffer
824 // Create a region P of virtual memory of type T*, backed by a data
825 // buffer
826 // Write the virtual address of R into P
827
828 Type *T = alloca_inst->getAllocatedType();
829 Type *Tptr = alloca_inst->getType();
830
831 lldb::addr_t R = frame.Malloc(T);
832
833 if (R == LLDB_INVALID_ADDRESS) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000834 LLDB_LOGF(log, "Couldn't allocate memory for an AllocaInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000835 error.SetErrorToGenericError();
836 error.SetErrorString(memory_allocation_error);
837 return false;
838 }
839
840 lldb::addr_t P = frame.Malloc(Tptr);
841
842 if (P == LLDB_INVALID_ADDRESS) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000843 LLDB_LOGF(log,
844 "Couldn't allocate the result pointer for an AllocaInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845 error.SetErrorToGenericError();
846 error.SetErrorString(memory_allocation_error);
847 return false;
848 }
849
Zachary Turner97206d52017-05-12 04:51:55 +0000850 lldb_private::Status write_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851
852 execution_unit.WritePointerToMemory(P, R, write_error);
853
854 if (!write_error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000855 LLDB_LOGF(log, "Couldn't write the result pointer for an AllocaInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000856 error.SetErrorToGenericError();
857 error.SetErrorString(memory_write_error);
Zachary Turner97206d52017-05-12 04:51:55 +0000858 lldb_private::Status free_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 execution_unit.Free(P, free_error);
860 execution_unit.Free(R, free_error);
861 return false;
862 }
863
864 frame.m_values[alloca_inst] = P;
865
866 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000867 LLDB_LOGF(log, "Interpreted an AllocaInst");
868 LLDB_LOGF(log, " R : 0x%" PRIx64, R);
869 LLDB_LOGF(log, " P : 0x%" PRIx64, P);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870 }
871 } break;
872 case Instruction::BitCast:
873 case Instruction::ZExt: {
874 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
875
876 if (!cast_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000877 LLDB_LOGF(
878 log, "getOpcode() returns %s, but instruction is not a BitCastInst",
879 cast_inst->getOpcodeName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 error.SetErrorToGenericError();
881 error.SetErrorString(interpreter_internal_error);
882 return false;
883 }
884
885 Value *source = cast_inst->getOperand(0);
886
887 lldb_private::Scalar S;
888
889 if (!frame.EvaluateValue(S, source, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000890 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(source).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 error.SetErrorToGenericError();
892 error.SetErrorString(bad_value_error);
893 return false;
894 }
895
896 frame.AssignValue(inst, S, module);
897 } break;
898 case Instruction::SExt: {
899 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
900
901 if (!cast_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000902 LLDB_LOGF(
903 log, "getOpcode() returns %s, but instruction is not a BitCastInst",
904 cast_inst->getOpcodeName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905 error.SetErrorToGenericError();
906 error.SetErrorString(interpreter_internal_error);
907 return false;
908 }
909
910 Value *source = cast_inst->getOperand(0);
911
912 lldb_private::Scalar S;
913
914 if (!frame.EvaluateValue(S, source, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000915 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(source).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916 error.SetErrorToGenericError();
917 error.SetErrorString(bad_value_error);
918 return false;
919 }
920
921 S.MakeSigned();
922
923 lldb_private::Scalar S_signextend(S.SLongLong());
924
925 frame.AssignValue(inst, S_signextend, module);
926 } break;
927 case Instruction::Br: {
928 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
929
930 if (!br_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000931 LLDB_LOGF(
932 log, "getOpcode() returns Br, but instruction is not a BranchInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 error.SetErrorToGenericError();
934 error.SetErrorString(interpreter_internal_error);
935 return false;
936 }
937
938 if (br_inst->isConditional()) {
939 Value *condition = br_inst->getCondition();
940
941 lldb_private::Scalar C;
942
943 if (!frame.EvaluateValue(C, condition, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000944 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(condition).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945 error.SetErrorToGenericError();
946 error.SetErrorString(bad_value_error);
947 return false;
948 }
949
950 if (!C.IsZero())
951 frame.Jump(br_inst->getSuccessor(0));
952 else
953 frame.Jump(br_inst->getSuccessor(1));
954
955 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000956 LLDB_LOGF(log, "Interpreted a BrInst with a condition");
957 LLDB_LOGF(log, " cond : %s",
958 frame.SummarizeValue(condition).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000959 }
960 } else {
961 frame.Jump(br_inst->getSuccessor(0));
962
963 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000964 LLDB_LOGF(log, "Interpreted a BrInst with no condition");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965 }
966 }
967 }
968 continue;
969 case Instruction::PHI: {
970 const PHINode *phi_inst = dyn_cast<PHINode>(inst);
971
972 if (!phi_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000973 LLDB_LOGF(log,
974 "getOpcode() returns PHI, but instruction is not a PHINode");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975 error.SetErrorToGenericError();
976 error.SetErrorString(interpreter_internal_error);
977 return false;
978 }
979 if (!frame.m_prev_bb) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000980 LLDB_LOGF(log,
981 "Encountered PHI node without having jumped from another "
982 "basic block");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000983 error.SetErrorToGenericError();
984 error.SetErrorString(interpreter_internal_error);
985 return false;
986 }
987
988 Value *value = phi_inst->getIncomingValueForBlock(frame.m_prev_bb);
989 lldb_private::Scalar result;
990 if (!frame.EvaluateValue(result, value, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000991 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(value).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 error.SetErrorToGenericError();
993 error.SetErrorString(bad_value_error);
994 return false;
995 }
996 frame.AssignValue(inst, result, module);
997
998 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000999 LLDB_LOGF(log, "Interpreted a %s", inst->getOpcodeName());
1000 LLDB_LOGF(log, " Incoming value : %s",
1001 frame.SummarizeValue(value).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 }
1003 } break;
1004 case Instruction::GetElementPtr: {
1005 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
1006
1007 if (!gep_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001008 LLDB_LOGF(log, "getOpcode() returns GetElementPtr, but instruction is "
1009 "not a GetElementPtrInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 error.SetErrorToGenericError();
1011 error.SetErrorString(interpreter_internal_error);
1012 return false;
1013 }
1014
1015 const Value *pointer_operand = gep_inst->getPointerOperand();
1016 Type *src_elem_ty = gep_inst->getSourceElementType();
1017
1018 lldb_private::Scalar P;
1019
1020 if (!frame.EvaluateValue(P, pointer_operand, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001021 LLDB_LOGF(log, "Couldn't evaluate %s",
1022 PrintValue(pointer_operand).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023 error.SetErrorToGenericError();
1024 error.SetErrorString(bad_value_error);
1025 return false;
1026 }
1027
1028 typedef SmallVector<Value *, 8> IndexVector;
1029 typedef IndexVector::iterator IndexIterator;
1030
1031 SmallVector<Value *, 8> indices(gep_inst->idx_begin(),
1032 gep_inst->idx_end());
1033
1034 SmallVector<Value *, 8> const_indices;
1035
1036 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie;
1037 ++ii) {
1038 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1039
1040 if (!constant_index) {
1041 lldb_private::Scalar I;
1042
1043 if (!frame.EvaluateValue(I, *ii, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001044 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(*ii).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 error.SetErrorToGenericError();
1046 error.SetErrorString(bad_value_error);
1047 return false;
1048 }
1049
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001050 LLDB_LOGF(log, "Evaluated constant index %s as %llu",
1051 PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052
1053 constant_index = cast<ConstantInt>(ConstantInt::get(
1054 (*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1055 }
1056
1057 const_indices.push_back(constant_index);
1058 }
1059
1060 uint64_t offset =
1061 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
1062
1063 lldb_private::Scalar Poffset = P + offset;
1064
1065 frame.AssignValue(inst, Poffset, module);
1066
1067 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001068 LLDB_LOGF(log, "Interpreted a GetElementPtrInst");
1069 LLDB_LOGF(log, " P : %s",
1070 frame.SummarizeValue(pointer_operand).c_str());
1071 LLDB_LOGF(log, " Poffset : %s", frame.SummarizeValue(inst).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072 }
1073 } break;
1074 case Instruction::ICmp: {
1075 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1076
1077 if (!icmp_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001078 LLDB_LOGF(
1079 log,
1080 "getOpcode() returns ICmp, but instruction is not an ICmpInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081 error.SetErrorToGenericError();
1082 error.SetErrorString(interpreter_internal_error);
1083 return false;
1084 }
1085
1086 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1087
1088 Value *lhs = inst->getOperand(0);
1089 Value *rhs = inst->getOperand(1);
1090
1091 lldb_private::Scalar L;
1092 lldb_private::Scalar R;
1093
1094 if (!frame.EvaluateValue(L, lhs, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001095 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(lhs).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 error.SetErrorToGenericError();
1097 error.SetErrorString(bad_value_error);
1098 return false;
1099 }
1100
1101 if (!frame.EvaluateValue(R, rhs, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001102 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(rhs).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 error.SetErrorToGenericError();
1104 error.SetErrorString(bad_value_error);
1105 return false;
1106 }
1107
1108 lldb_private::Scalar result;
1109
1110 switch (predicate) {
1111 default:
1112 return false;
1113 case CmpInst::ICMP_EQ:
1114 result = (L == R);
1115 break;
1116 case CmpInst::ICMP_NE:
1117 result = (L != R);
1118 break;
1119 case CmpInst::ICMP_UGT:
1120 L.MakeUnsigned();
1121 R.MakeUnsigned();
1122 result = (L > R);
1123 break;
1124 case CmpInst::ICMP_UGE:
1125 L.MakeUnsigned();
1126 R.MakeUnsigned();
1127 result = (L >= R);
1128 break;
1129 case CmpInst::ICMP_ULT:
1130 L.MakeUnsigned();
1131 R.MakeUnsigned();
1132 result = (L < R);
1133 break;
1134 case CmpInst::ICMP_ULE:
1135 L.MakeUnsigned();
1136 R.MakeUnsigned();
1137 result = (L <= R);
1138 break;
1139 case CmpInst::ICMP_SGT:
1140 L.MakeSigned();
1141 R.MakeSigned();
1142 result = (L > R);
1143 break;
1144 case CmpInst::ICMP_SGE:
1145 L.MakeSigned();
1146 R.MakeSigned();
1147 result = (L >= R);
1148 break;
1149 case CmpInst::ICMP_SLT:
1150 L.MakeSigned();
1151 R.MakeSigned();
1152 result = (L < R);
1153 break;
1154 case CmpInst::ICMP_SLE:
1155 L.MakeSigned();
1156 R.MakeSigned();
1157 result = (L <= R);
1158 break;
1159 }
1160
1161 frame.AssignValue(inst, result, module);
1162
1163 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001164 LLDB_LOGF(log, "Interpreted an ICmpInst");
1165 LLDB_LOGF(log, " L : %s", frame.SummarizeValue(lhs).c_str());
1166 LLDB_LOGF(log, " R : %s", frame.SummarizeValue(rhs).c_str());
1167 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001168 }
1169 } break;
1170 case Instruction::IntToPtr: {
1171 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1172
1173 if (!int_to_ptr_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001174 LLDB_LOGF(log,
1175 "getOpcode() returns IntToPtr, but instruction is not an "
1176 "IntToPtrInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 error.SetErrorToGenericError();
1178 error.SetErrorString(interpreter_internal_error);
1179 return false;
1180 }
1181
1182 Value *src_operand = int_to_ptr_inst->getOperand(0);
1183
1184 lldb_private::Scalar I;
1185
1186 if (!frame.EvaluateValue(I, src_operand, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001187 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 error.SetErrorToGenericError();
1189 error.SetErrorString(bad_value_error);
1190 return false;
1191 }
1192
1193 frame.AssignValue(inst, I, module);
1194
1195 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001196 LLDB_LOGF(log, "Interpreted an IntToPtr");
1197 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1198 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199 }
1200 } break;
1201 case Instruction::PtrToInt: {
1202 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1203
1204 if (!ptr_to_int_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001205 LLDB_LOGF(log,
1206 "getOpcode() returns PtrToInt, but instruction is not an "
1207 "PtrToIntInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001208 error.SetErrorToGenericError();
1209 error.SetErrorString(interpreter_internal_error);
1210 return false;
1211 }
1212
1213 Value *src_operand = ptr_to_int_inst->getOperand(0);
1214
1215 lldb_private::Scalar I;
1216
1217 if (!frame.EvaluateValue(I, src_operand, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001218 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001219 error.SetErrorToGenericError();
1220 error.SetErrorString(bad_value_error);
1221 return false;
1222 }
1223
1224 frame.AssignValue(inst, I, module);
1225
1226 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001227 LLDB_LOGF(log, "Interpreted a PtrToInt");
1228 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1229 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 }
1231 } break;
1232 case Instruction::Trunc: {
1233 const TruncInst *trunc_inst = dyn_cast<TruncInst>(inst);
1234
1235 if (!trunc_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001236 LLDB_LOGF(
1237 log,
1238 "getOpcode() returns Trunc, but instruction is not a TruncInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 error.SetErrorToGenericError();
1240 error.SetErrorString(interpreter_internal_error);
1241 return false;
1242 }
1243
1244 Value *src_operand = trunc_inst->getOperand(0);
1245
1246 lldb_private::Scalar I;
1247
1248 if (!frame.EvaluateValue(I, src_operand, module)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001249 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 error.SetErrorToGenericError();
1251 error.SetErrorString(bad_value_error);
1252 return false;
1253 }
1254
1255 frame.AssignValue(inst, I, module);
1256
1257 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001258 LLDB_LOGF(log, "Interpreted a Trunc");
1259 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1260 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261 }
1262 } break;
1263 case Instruction::Load: {
1264 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1265
1266 if (!load_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001267 LLDB_LOGF(
1268 log, "getOpcode() returns Load, but instruction is not a LoadInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 error.SetErrorToGenericError();
1270 error.SetErrorString(interpreter_internal_error);
1271 return false;
1272 }
1273
1274 // The semantics of Load are:
1275 // Create a region D that will contain the loaded data
1276 // Resolve the region P containing a pointer
1277 // Dereference P to get the region R that the data should be loaded from
1278 // Transfer a unit of type type(D) from R to D
1279
1280 const Value *pointer_operand = load_inst->getPointerOperand();
1281
1282 Type *pointer_ty = pointer_operand->getType();
1283 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1284 if (!pointer_ptr_ty) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001285 LLDB_LOGF(log, "getPointerOperand()->getType() is not a PointerType");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 error.SetErrorToGenericError();
1287 error.SetErrorString(interpreter_internal_error);
1288 return false;
1289 }
1290 Type *target_ty = pointer_ptr_ty->getElementType();
1291
1292 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1293 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1294
1295 if (D == LLDB_INVALID_ADDRESS) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001296 LLDB_LOGF(log, "LoadInst's value doesn't resolve to anything");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001297 error.SetErrorToGenericError();
1298 error.SetErrorString(bad_value_error);
1299 return false;
1300 }
1301
1302 if (P == LLDB_INVALID_ADDRESS) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001303 LLDB_LOGF(log, "LoadInst's pointer doesn't resolve to anything");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 error.SetErrorToGenericError();
1305 error.SetErrorString(bad_value_error);
1306 return false;
1307 }
1308
1309 lldb::addr_t R;
Zachary Turner97206d52017-05-12 04:51:55 +00001310 lldb_private::Status read_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311 execution_unit.ReadPointerFromMemory(&R, P, read_error);
1312
1313 if (!read_error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001314 LLDB_LOGF(log, "Couldn't read the address to be loaded for a LoadInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 error.SetErrorToGenericError();
1316 error.SetErrorString(memory_read_error);
1317 return false;
1318 }
1319
1320 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1321 lldb_private::DataBufferHeap buffer(target_size, 0);
1322
1323 read_error.Clear();
1324 execution_unit.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(),
1325 read_error);
1326 if (!read_error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001327 LLDB_LOGF(log, "Couldn't read from a region on behalf of a LoadInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328 error.SetErrorToGenericError();
1329 error.SetErrorString(memory_read_error);
1330 return false;
1331 }
1332
Zachary Turner97206d52017-05-12 04:51:55 +00001333 lldb_private::Status write_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001334 execution_unit.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(),
1335 write_error);
1336 if (!write_error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001337 LLDB_LOGF(log, "Couldn't write to a region on behalf of a LoadInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 error.SetErrorToGenericError();
1339 error.SetErrorString(memory_read_error);
1340 return false;
1341 }
1342
1343 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001344 LLDB_LOGF(log, "Interpreted a LoadInst");
1345 LLDB_LOGF(log, " P : 0x%" PRIx64, P);
1346 LLDB_LOGF(log, " R : 0x%" PRIx64, R);
1347 LLDB_LOGF(log, " D : 0x%" PRIx64, D);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001348 }
1349 } break;
1350 case Instruction::Ret: {
1351 return true;
1352 }
1353 case Instruction::Store: {
1354 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1355
1356 if (!store_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001357 LLDB_LOGF(
1358 log,
1359 "getOpcode() returns Store, but instruction is not a StoreInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001360 error.SetErrorToGenericError();
1361 error.SetErrorString(interpreter_internal_error);
1362 return false;
1363 }
1364
1365 // The semantics of Store are:
1366 // Resolve the region D containing the data to be stored
1367 // Resolve the region P containing a pointer
1368 // Dereference P to get the region R that the data should be stored in
1369 // Transfer a unit of type type(D) from D to R
1370
1371 const Value *value_operand = store_inst->getValueOperand();
1372 const Value *pointer_operand = store_inst->getPointerOperand();
1373
1374 Type *pointer_ty = pointer_operand->getType();
1375 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1376 if (!pointer_ptr_ty)
1377 return false;
1378 Type *target_ty = pointer_ptr_ty->getElementType();
1379
1380 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1381 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1382
1383 if (D == LLDB_INVALID_ADDRESS) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001384 LLDB_LOGF(log, "StoreInst's value doesn't resolve to anything");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385 error.SetErrorToGenericError();
1386 error.SetErrorString(bad_value_error);
1387 return false;
1388 }
1389
1390 if (P == LLDB_INVALID_ADDRESS) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001391 LLDB_LOGF(log, "StoreInst's pointer doesn't resolve to anything");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392 error.SetErrorToGenericError();
1393 error.SetErrorString(bad_value_error);
1394 return false;
1395 }
1396
1397 lldb::addr_t R;
Zachary Turner97206d52017-05-12 04:51:55 +00001398 lldb_private::Status read_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 execution_unit.ReadPointerFromMemory(&R, P, read_error);
1400
1401 if (!read_error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001402 LLDB_LOGF(log, "Couldn't read the address to be loaded for a LoadInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 error.SetErrorToGenericError();
1404 error.SetErrorString(memory_read_error);
1405 return false;
1406 }
1407
1408 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1409 lldb_private::DataBufferHeap buffer(target_size, 0);
1410
1411 read_error.Clear();
1412 execution_unit.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(),
1413 read_error);
1414 if (!read_error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001415 LLDB_LOGF(log, "Couldn't read from a region on behalf of a StoreInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 error.SetErrorToGenericError();
1417 error.SetErrorString(memory_read_error);
1418 return false;
1419 }
1420
Zachary Turner97206d52017-05-12 04:51:55 +00001421 lldb_private::Status write_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422 execution_unit.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(),
1423 write_error);
1424 if (!write_error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001425 LLDB_LOGF(log, "Couldn't write to a region on behalf of a StoreInst");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426 error.SetErrorToGenericError();
1427 error.SetErrorString(memory_write_error);
1428 return false;
1429 }
1430
1431 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001432 LLDB_LOGF(log, "Interpreted a StoreInst");
1433 LLDB_LOGF(log, " D : 0x%" PRIx64, D);
1434 LLDB_LOGF(log, " P : 0x%" PRIx64, P);
1435 LLDB_LOGF(log, " R : 0x%" PRIx64, R);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436 }
1437 } break;
1438 case Instruction::Call: {
1439 const CallInst *call_inst = dyn_cast<CallInst>(inst);
1440
1441 if (!call_inst) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001442 LLDB_LOGF(log,
1443 "getOpcode() returns %s, but instruction is not a CallInst",
1444 inst->getOpcodeName());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 error.SetErrorToGenericError();
1446 error.SetErrorString(interpreter_internal_error);
1447 return false;
1448 }
1449
1450 if (CanIgnoreCall(call_inst))
1451 break;
1452
1453 // Get the return type
1454 llvm::Type *returnType = call_inst->getType();
1455 if (returnType == nullptr) {
1456 error.SetErrorToGenericError();
1457 error.SetErrorString("unable to access return type");
1458 return false;
1459 }
1460
1461 // Work with void, integer and pointer return types
1462 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1463 !returnType->isPointerTy()) {
1464 error.SetErrorToGenericError();
1465 error.SetErrorString("return type is not supported");
1466 return false;
1467 }
1468
1469 // Check we can actually get a thread
1470 if (exe_ctx.GetThreadPtr() == nullptr) {
1471 error.SetErrorToGenericError();
1472 error.SetErrorStringWithFormat("unable to acquire thread");
1473 return false;
1474 }
1475
1476 // Make sure we have a valid process
1477 if (!exe_ctx.GetProcessPtr()) {
1478 error.SetErrorToGenericError();
1479 error.SetErrorStringWithFormat("unable to get the process");
1480 return false;
1481 }
1482
1483 // Find the address of the callee function
1484 lldb_private::Scalar I;
1485 const llvm::Value *val = call_inst->getCalledValue();
1486
1487 if (!frame.EvaluateValue(I, val, module)) {
1488 error.SetErrorToGenericError();
1489 error.SetErrorString("unable to get address of function");
1490 return false;
1491 }
1492 lldb_private::Address funcAddr(I.ULongLong(LLDB_INVALID_ADDRESS));
1493
1494 lldb_private::DiagnosticManager diagnostics;
1495 lldb_private::EvaluateExpressionOptions options;
1496
1497 // We generally receive a function pointer which we must dereference
1498 llvm::Type *prototype = val->getType();
1499 if (!prototype->isPointerTy()) {
1500 error.SetErrorToGenericError();
1501 error.SetErrorString("call need function pointer");
1502 return false;
1503 }
1504
1505 // Dereference the function pointer
1506 prototype = prototype->getPointerElementType();
1507 if (!(prototype->isFunctionTy() || prototype->isFunctionVarArg())) {
1508 error.SetErrorToGenericError();
1509 error.SetErrorString("call need function pointer");
1510 return false;
1511 }
1512
1513 // Find number of arguments
1514 const int numArgs = call_inst->getNumArgOperands();
1515
1516 // We work with a fixed array of 16 arguments which is our upper limit
1517 static lldb_private::ABI::CallArgument rawArgs[16];
1518 if (numArgs >= 16) {
1519 error.SetErrorToGenericError();
1520 error.SetErrorStringWithFormat("function takes too many arguments");
1521 return false;
1522 }
1523
Adrian Prantl05097242018-04-30 16:49:04 +00001524 // Push all function arguments to the argument list that will be passed
1525 // to the call function thread plan
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526 for (int i = 0; i < numArgs; i++) {
1527 // Get details of this argument
1528 llvm::Value *arg_op = call_inst->getArgOperand(i);
1529 llvm::Type *arg_ty = arg_op->getType();
1530
1531 // Ensure that this argument is an supported type
1532 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1533 error.SetErrorToGenericError();
1534 error.SetErrorStringWithFormat("argument %d must be integer type", i);
1535 return false;
1536 }
1537
1538 // Extract the arguments value
1539 lldb_private::Scalar tmp_op = 0;
1540 if (!frame.EvaluateValue(tmp_op, arg_op, module)) {
1541 error.SetErrorToGenericError();
1542 error.SetErrorStringWithFormat("unable to evaluate argument %d", i);
1543 return false;
1544 }
1545
1546 // Check if this is a string literal or constant string pointer
1547 if (arg_ty->isPointerTy()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548 lldb::addr_t addr = tmp_op.ULongLong();
1549 size_t dataSize = 0;
1550
David Blaikiea322f362017-01-06 00:38:06 +00001551 bool Success = execution_unit.GetAllocSize(addr, dataSize);
1552 (void)Success;
1553 assert(Success &&
1554 "unable to locate host data for transfer to device");
1555 // Create the required buffer
1556 rawArgs[i].size = dataSize;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001557 rawArgs[i].data_up.reset(new uint8_t[dataSize + 1]);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558
David Blaikiea322f362017-01-06 00:38:06 +00001559 // Read string from host memory
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001560 execution_unit.ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
David Blaikiea322f362017-01-06 00:38:06 +00001561 error);
1562 assert(!error.Fail() &&
1563 "we have failed to read the string from memory");
1564
1565 // Add null terminator
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001566 rawArgs[i].data_up[dataSize] = '\0';
David Blaikiea322f362017-01-06 00:38:06 +00001567 rawArgs[i].type = lldb_private::ABI::CallArgument::HostPointer;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001568 } else /* if ( arg_ty->isPointerTy() ) */
1569 {
1570 rawArgs[i].type = lldb_private::ABI::CallArgument::TargetValue;
1571 // Get argument size in bytes
1572 rawArgs[i].size = arg_ty->getIntegerBitWidth() / 8;
1573 // Push value into argument list for thread plan
1574 rawArgs[i].value = tmp_op.ULongLong();
1575 }
1576 }
1577
1578 // Pack the arguments into an llvm::array
1579 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1580
1581 // Setup a thread plan to call the target function
1582 lldb::ThreadPlanSP call_plan_sp(
1583 new lldb_private::ThreadPlanCallFunctionUsingABI(
1584 exe_ctx.GetThreadRef(), funcAddr, *prototype, *returnType, args,
1585 options));
1586
1587 // Check if the plan is valid
1588 lldb_private::StreamString ss;
1589 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1590 error.SetErrorToGenericError();
1591 error.SetErrorStringWithFormat(
1592 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1593 I.ULongLong());
1594 return false;
1595 }
1596
1597 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
1598
1599 // Execute the actual function call thread plan
1600 lldb::ExpressionResults res = exe_ctx.GetProcessRef().RunThreadPlan(
1601 exe_ctx, call_plan_sp, options, diagnostics);
1602
1603 // Check that the thread plan completed successfully
1604 if (res != lldb::ExpressionResults::eExpressionCompleted) {
1605 error.SetErrorToGenericError();
1606 error.SetErrorStringWithFormat("ThreadPlanCallFunctionUsingABI failed");
1607 return false;
1608 }
1609
1610 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
1611
1612 // Void return type
1613 if (returnType->isVoidTy()) {
1614 // Cant assign to void types, so we leave the frame untouched
1615 } else
1616 // Integer or pointer return type
1617 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1618 // Get the encapsulated return value
1619 lldb::ValueObjectSP retVal = call_plan_sp.get()->GetReturnValueObject();
1620
1621 lldb_private::Scalar returnVal = -1;
1622 lldb_private::ValueObject *vobj = retVal.get();
1623
1624 // Check if the return value is valid
1625 if (vobj == nullptr || retVal.empty()) {
1626 error.SetErrorToGenericError();
1627 error.SetErrorStringWithFormat("unable to get the return value");
1628 return false;
1629 }
1630
1631 // Extract the return value as a integer
1632 lldb_private::Value &value = vobj->GetValue();
1633 returnVal = value.GetScalar();
1634
1635 // Push the return value as the result
1636 frame.AssignValue(inst, returnVal, module);
1637 }
1638 } break;
1639 }
1640
1641 ++frame.m_ii;
1642 }
1643
1644 if (num_insts >= 4096) {
1645 error.SetErrorToGenericError();
1646 error.SetErrorString(infinite_loop_error);
1647 return false;
1648 }
1649
1650 return false;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001651}