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