1 //===-- IRInterpreter.cpp ---------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Core/DataExtractor.h"
11 #include "lldb/Core/Error.h"
12 #include "lldb/Core/Log.h"
13 #include "lldb/Core/Scalar.h"
14 #include "lldb/Core/StreamString.h"
15 #include "lldb/Expression/IRMemoryMap.h"
16 #include "lldb/Expression/IRInterpreter.h"
17
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/raw_ostream.h"
24
25 #include <map>
26
27 using namespace llvm;
28
29 static std::string
PrintValue(const Value * value,bool truncate=false)30 PrintValue(const Value *value, bool truncate = false)
31 {
32 std::string s;
33 raw_string_ostream rso(s);
34 value->print(rso);
35 rso.flush();
36 if (truncate)
37 s.resize(s.length() - 1);
38
39 size_t offset;
40 while ((offset = s.find('\n')) != s.npos)
41 s.erase(offset, 1);
42 while (s[0] == ' ' || s[0] == '\t')
43 s.erase(0, 1);
44
45 return s;
46 }
47
48 static std::string
PrintType(const Type * type,bool truncate=false)49 PrintType(const Type *type, bool truncate = false)
50 {
51 std::string s;
52 raw_string_ostream rso(s);
53 type->print(rso);
54 rso.flush();
55 if (truncate)
56 s.resize(s.length() - 1);
57 return s;
58 }
59
60 class InterpreterStackFrame
61 {
62 public:
63 typedef std::map <const Value*, lldb::addr_t> ValueMap;
64
65 ValueMap m_values;
66 DataLayout &m_target_data;
67 lldb_private::IRMemoryMap &m_memory_map;
68 const BasicBlock *m_bb;
69 BasicBlock::const_iterator m_ii;
70 BasicBlock::const_iterator m_ie;
71
72 lldb::addr_t m_frame_process_address;
73 size_t m_frame_size;
74 lldb::addr_t m_stack_pointer;
75
76 lldb::ByteOrder m_byte_order;
77 size_t m_addr_byte_size;
78
InterpreterStackFrame(DataLayout & target_data,lldb_private::IRMemoryMap & memory_map,lldb::addr_t stack_frame_bottom,lldb::addr_t stack_frame_top)79 InterpreterStackFrame (DataLayout &target_data,
80 lldb_private::IRMemoryMap &memory_map,
81 lldb::addr_t stack_frame_bottom,
82 lldb::addr_t stack_frame_top) :
83 m_target_data (target_data),
84 m_memory_map (memory_map)
85 {
86 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
87 m_addr_byte_size = (target_data.getPointerSize(0));
88
89 m_frame_process_address = stack_frame_bottom;
90 m_frame_size = stack_frame_top - stack_frame_bottom;
91 m_stack_pointer = stack_frame_top;
92 }
93
~InterpreterStackFrame()94 ~InterpreterStackFrame ()
95 {
96 }
97
Jump(const BasicBlock * bb)98 void Jump (const BasicBlock *bb)
99 {
100 m_bb = bb;
101 m_ii = m_bb->begin();
102 m_ie = m_bb->end();
103 }
104
SummarizeValue(const Value * value)105 std::string SummarizeValue (const Value *value)
106 {
107 lldb_private::StreamString ss;
108
109 ss.Printf("%s", PrintValue(value).c_str());
110
111 ValueMap::iterator i = m_values.find(value);
112
113 if (i != m_values.end())
114 {
115 lldb::addr_t addr = i->second;
116
117 ss.Printf(" 0x%llx", (unsigned long long)addr);
118 }
119
120 return ss.GetString();
121 }
122
AssignToMatchType(lldb_private::Scalar & scalar,uint64_t u64value,Type * type)123 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
124 {
125 size_t type_size = m_target_data.getTypeStoreSize(type);
126
127 switch (type_size)
128 {
129 case 1:
130 scalar = (uint8_t)u64value;
131 break;
132 case 2:
133 scalar = (uint16_t)u64value;
134 break;
135 case 4:
136 scalar = (uint32_t)u64value;
137 break;
138 case 8:
139 scalar = (uint64_t)u64value;
140 break;
141 default:
142 return false;
143 }
144
145 return true;
146 }
147
EvaluateValue(lldb_private::Scalar & scalar,const Value * value,Module & module)148 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
149 {
150 const Constant *constant = dyn_cast<Constant>(value);
151
152 if (constant)
153 {
154 APInt value_apint;
155
156 if (!ResolveConstantValue(value_apint, constant))
157 return false;
158
159 return AssignToMatchType(scalar, value_apint.getLimitedValue(), value->getType());
160 }
161 else
162 {
163 lldb::addr_t process_address = ResolveValue(value, module);
164 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
165
166 lldb_private::DataExtractor value_extractor;
167 lldb_private::Error extract_error;
168
169 m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error);
170
171 if (!extract_error.Success())
172 return false;
173
174 lldb::offset_t offset = 0;
175 if (value_size == 1 || value_size == 2 || value_size == 4 || value_size == 8)
176 {
177 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
178 return AssignToMatchType(scalar, u64value, value->getType());
179 }
180 }
181
182 return false;
183 }
184
AssignValue(const Value * value,lldb_private::Scalar & scalar,Module & module)185 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
186 {
187 lldb::addr_t process_address = ResolveValue (value, module);
188
189 if (process_address == LLDB_INVALID_ADDRESS)
190 return false;
191
192 lldb_private::Scalar cast_scalar;
193
194 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
195 return false;
196
197 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
198
199 lldb_private::DataBufferHeap buf(value_byte_size, 0);
200
201 lldb_private::Error get_data_error;
202
203 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
204 return false;
205
206 lldb_private::Error write_error;
207
208 m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
209
210 return write_error.Success();
211 }
212
ResolveConstantValue(APInt & value,const Constant * constant)213 bool ResolveConstantValue (APInt &value, const Constant *constant)
214 {
215 switch (constant->getValueID())
216 {
217 default:
218 break;
219 case Value::ConstantIntVal:
220 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
221 {
222 value = constant_int->getValue();
223 return true;
224 }
225 break;
226 case Value::ConstantFPVal:
227 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
228 {
229 value = constant_fp->getValueAPF().bitcastToAPInt();
230 return true;
231 }
232 break;
233 case Value::ConstantExprVal:
234 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
235 {
236 switch (constant_expr->getOpcode())
237 {
238 default:
239 return false;
240 case Instruction::IntToPtr:
241 case Instruction::PtrToInt:
242 case Instruction::BitCast:
243 return ResolveConstantValue(value, constant_expr->getOperand(0));
244 case Instruction::GetElementPtr:
245 {
246 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
247 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
248
249 Constant *base = dyn_cast<Constant>(*op_cursor);
250
251 if (!base)
252 return false;
253
254 if (!ResolveConstantValue(value, base))
255 return false;
256
257 op_cursor++;
258
259 if (op_cursor == op_end)
260 return true; // no offset to apply!
261
262 SmallVector <Value *, 8> indices (op_cursor, op_end);
263
264 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
265
266 const bool is_signed = true;
267 value += APInt(value.getBitWidth(), offset, is_signed);
268
269 return true;
270 }
271 }
272 }
273 break;
274 case Value::ConstantPointerNullVal:
275 if (isa<ConstantPointerNull>(constant))
276 {
277 value = APInt(m_target_data.getPointerSizeInBits(), 0);
278 return true;
279 }
280 break;
281 }
282 return false;
283 }
284
MakeArgument(const Argument * value,uint64_t address)285 bool MakeArgument(const Argument *value, uint64_t address)
286 {
287 lldb::addr_t data_address = Malloc(value->getType());
288
289 if (data_address == LLDB_INVALID_ADDRESS)
290 return false;
291
292 lldb_private::Error write_error;
293
294 m_memory_map.WritePointerToMemory(data_address, address, write_error);
295
296 if (!write_error.Success())
297 {
298 lldb_private::Error free_error;
299 m_memory_map.Free(data_address, free_error);
300 return false;
301 }
302
303 m_values[value] = data_address;
304
305 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
306
307 if (log)
308 {
309 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str());
310 log->Printf(" Data region : %llx", (unsigned long long)address);
311 log->Printf(" Ref region : %llx", (unsigned long long)data_address);
312 }
313
314 return true;
315 }
316
ResolveConstant(lldb::addr_t process_address,const Constant * constant)317 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant)
318 {
319 APInt resolved_value;
320
321 if (!ResolveConstantValue(resolved_value, constant))
322 return false;
323
324 const uint64_t *raw_data = resolved_value.getRawData();
325
326 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
327
328 lldb_private::Error write_error;
329
330 m_memory_map.WriteMemory(process_address, (uint8_t*)raw_data, constant_size, write_error);
331
332 return write_error.Success();
333 }
334
Malloc(size_t size,uint8_t byte_alignment)335 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
336 {
337 lldb::addr_t ret = m_stack_pointer;
338
339 ret -= size;
340 ret -= (ret % byte_alignment);
341
342 if (ret < m_frame_process_address)
343 return LLDB_INVALID_ADDRESS;
344
345 m_stack_pointer = ret;
346 return ret;
347 }
348
MallocPointer()349 lldb::addr_t MallocPointer ()
350 {
351 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
352 }
353
Malloc(llvm::Type * type)354 lldb::addr_t Malloc (llvm::Type *type)
355 {
356 lldb_private::Error alloc_error;
357
358 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
359 }
360
PrintData(lldb::addr_t addr,llvm::Type * type)361 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
362 {
363 size_t length = m_target_data.getTypeStoreSize(type);
364
365 lldb_private::DataBufferHeap buf(length, 0);
366
367 lldb_private::Error read_error;
368
369 m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error);
370
371 if (!read_error.Success())
372 return std::string("<couldn't read data>");
373
374 lldb_private::StreamString ss;
375
376 for (size_t i = 0; i < length; i++)
377 {
378 if ((!(i & 0xf)) && i)
379 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
380 else
381 ss.Printf("%02hhx ", buf.GetBytes()[i]);
382 }
383
384 return ss.GetString();
385 }
386
ResolveValue(const Value * value,Module & module)387 lldb::addr_t ResolveValue (const Value *value, Module &module)
388 {
389 ValueMap::iterator i = m_values.find(value);
390
391 if (i != m_values.end())
392 return i->second;
393
394 // Fall back and allocate space [allocation type Alloca]
395
396 lldb::addr_t data_address = Malloc(value->getType());
397
398 if (const Constant *constant = dyn_cast<Constant>(value))
399 {
400 if (!ResolveConstant (data_address, constant))
401 {
402 lldb_private::Error free_error;
403 m_memory_map.Free(data_address, free_error);
404 return LLDB_INVALID_ADDRESS;
405 }
406 }
407
408 m_values[value] = data_address;
409 return data_address;
410 }
411 };
412
413 static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
414 static const char *unsupported_operand_error = "Interpreter doesn't handle one of the expression's operands";
415 //static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
416 static const char *interpreter_internal_error = "Interpreter encountered an internal error";
417 static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
418 static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
419 static const char *memory_write_error = "Interpreter couldn't write to memory";
420 static const char *memory_read_error = "Interpreter couldn't read from memory";
421 static const char *infinite_loop_error = "Interpreter ran for too many cycles";
422 //static const char *bad_result_error = "Result of expression is in bad memory";
423
424 bool
CanInterpret(llvm::Module & module,llvm::Function & function,lldb_private::Error & error)425 IRInterpreter::CanInterpret (llvm::Module &module,
426 llvm::Function &function,
427 lldb_private::Error &error)
428 {
429 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
430
431 bool saw_function_with_body = false;
432
433 for (Module::iterator fi = module.begin(), fe = module.end();
434 fi != fe;
435 ++fi)
436 {
437 if (fi->begin() != fi->end())
438 {
439 if (saw_function_with_body)
440 return false;
441 saw_function_with_body = true;
442 }
443 }
444
445 for (Function::iterator bbi = function.begin(), bbe = function.end();
446 bbi != bbe;
447 ++bbi)
448 {
449 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
450 ii != ie;
451 ++ii)
452 {
453 switch (ii->getOpcode())
454 {
455 default:
456 {
457 if (log)
458 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
459 error.SetErrorToGenericError();
460 error.SetErrorString(unsupported_opcode_error);
461 return false;
462 }
463 case Instruction::Add:
464 case Instruction::Alloca:
465 case Instruction::BitCast:
466 case Instruction::Br:
467 case Instruction::GetElementPtr:
468 break;
469 case Instruction::ICmp:
470 {
471 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
472
473 if (!icmp_inst)
474 {
475 error.SetErrorToGenericError();
476 error.SetErrorString(interpreter_internal_error);
477 return false;
478 }
479
480 switch (icmp_inst->getPredicate())
481 {
482 default:
483 {
484 if (log)
485 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
486
487 error.SetErrorToGenericError();
488 error.SetErrorString(unsupported_opcode_error);
489 return false;
490 }
491 case CmpInst::ICMP_EQ:
492 case CmpInst::ICMP_NE:
493 case CmpInst::ICMP_UGT:
494 case CmpInst::ICMP_UGE:
495 case CmpInst::ICMP_ULT:
496 case CmpInst::ICMP_ULE:
497 case CmpInst::ICMP_SGT:
498 case CmpInst::ICMP_SGE:
499 case CmpInst::ICMP_SLT:
500 case CmpInst::ICMP_SLE:
501 break;
502 }
503 }
504 break;
505 case Instruction::And:
506 case Instruction::AShr:
507 case Instruction::IntToPtr:
508 case Instruction::PtrToInt:
509 case Instruction::Load:
510 case Instruction::LShr:
511 case Instruction::Mul:
512 case Instruction::Or:
513 case Instruction::Ret:
514 case Instruction::SDiv:
515 case Instruction::SExt:
516 case Instruction::Shl:
517 case Instruction::SRem:
518 case Instruction::Store:
519 case Instruction::Sub:
520 case Instruction::UDiv:
521 case Instruction::URem:
522 case Instruction::Xor:
523 case Instruction::ZExt:
524 break;
525 }
526
527 for (int oi = 0, oe = ii->getNumOperands();
528 oi != oe;
529 ++oi)
530 {
531 Value *operand = ii->getOperand(oi);
532 Type *operand_type = operand->getType();
533
534 switch (operand_type->getTypeID())
535 {
536 default:
537 break;
538 case Type::VectorTyID:
539 {
540 if (log)
541 log->Printf("Unsupported operand type: %s", PrintType(operand_type).c_str());
542 error.SetErrorString(unsupported_operand_error);
543 return false;
544 }
545 }
546 }
547 }
548
549 }
550
551 return true;}
552
553 bool
Interpret(llvm::Module & module,llvm::Function & function,llvm::ArrayRef<lldb::addr_t> args,lldb_private::IRMemoryMap & memory_map,lldb_private::Error & error,lldb::addr_t stack_frame_bottom,lldb::addr_t stack_frame_top)554 IRInterpreter::Interpret (llvm::Module &module,
555 llvm::Function &function,
556 llvm::ArrayRef<lldb::addr_t> args,
557 lldb_private::IRMemoryMap &memory_map,
558 lldb_private::Error &error,
559 lldb::addr_t stack_frame_bottom,
560 lldb::addr_t stack_frame_top)
561 {
562 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
563
564 if (log)
565 {
566 std::string s;
567 raw_string_ostream oss(s);
568
569 module.print(oss, NULL);
570
571 oss.flush();
572
573 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
574 }
575
576 DataLayout data_layout(&module);
577
578 InterpreterStackFrame frame(data_layout, memory_map, stack_frame_bottom, stack_frame_top);
579
580 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
581 {
582 error.SetErrorString("Couldn't allocate stack frame");
583 }
584
585 int arg_index = 0;
586
587 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
588 ai != ae;
589 ++ai, ++arg_index)
590 {
591 if (args.size() < arg_index)
592 {
593 error.SetErrorString ("Not enough arguments passed in to function");
594 return false;
595 }
596
597 lldb::addr_t ptr = args[arg_index];
598
599 frame.MakeArgument(ai, ptr);
600 }
601
602 uint32_t num_insts = 0;
603
604 frame.Jump(function.begin());
605
606 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
607 {
608 const Instruction *inst = frame.m_ii;
609
610 if (log)
611 log->Printf("Interpreting %s", PrintValue(inst).c_str());
612
613 switch (inst->getOpcode())
614 {
615 default:
616 break;
617 case Instruction::Add:
618 case Instruction::Sub:
619 case Instruction::Mul:
620 case Instruction::SDiv:
621 case Instruction::UDiv:
622 case Instruction::SRem:
623 case Instruction::URem:
624 case Instruction::Shl:
625 case Instruction::LShr:
626 case Instruction::AShr:
627 case Instruction::And:
628 case Instruction::Or:
629 case Instruction::Xor:
630 {
631 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
632
633 if (!bin_op)
634 {
635 if (log)
636 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
637 error.SetErrorToGenericError();
638 error.SetErrorString(interpreter_internal_error);
639 return false;
640 }
641
642 Value *lhs = inst->getOperand(0);
643 Value *rhs = inst->getOperand(1);
644
645 lldb_private::Scalar L;
646 lldb_private::Scalar R;
647
648 if (!frame.EvaluateValue(L, lhs, module))
649 {
650 if (log)
651 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
652 error.SetErrorToGenericError();
653 error.SetErrorString(bad_value_error);
654 return false;
655 }
656
657 if (!frame.EvaluateValue(R, rhs, module))
658 {
659 if (log)
660 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
661 error.SetErrorToGenericError();
662 error.SetErrorString(bad_value_error);
663 return false;
664 }
665
666 lldb_private::Scalar result;
667
668 switch (inst->getOpcode())
669 {
670 default:
671 break;
672 case Instruction::Add:
673 result = L + R;
674 break;
675 case Instruction::Mul:
676 result = L * R;
677 break;
678 case Instruction::Sub:
679 result = L - R;
680 break;
681 case Instruction::SDiv:
682 L.MakeSigned();
683 R.MakeSigned();
684 result = L / R;
685 break;
686 case Instruction::UDiv:
687 result = L.GetRawBits64(0) / R.GetRawBits64(1);
688 break;
689 case Instruction::SRem:
690 L.MakeSigned();
691 R.MakeSigned();
692 result = L % R;
693 break;
694 case Instruction::URem:
695 result = L.GetRawBits64(0) % R.GetRawBits64(1);
696 break;
697 case Instruction::Shl:
698 result = L << R;
699 break;
700 case Instruction::AShr:
701 result = L >> R;
702 break;
703 case Instruction::LShr:
704 result = L;
705 result.ShiftRightLogical(R);
706 break;
707 case Instruction::And:
708 result = L & R;
709 break;
710 case Instruction::Or:
711 result = L | R;
712 break;
713 case Instruction::Xor:
714 result = L ^ R;
715 break;
716 }
717
718 frame.AssignValue(inst, result, module);
719
720 if (log)
721 {
722 log->Printf("Interpreted a %s", inst->getOpcodeName());
723 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
724 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
725 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
726 }
727 }
728 break;
729 case Instruction::Alloca:
730 {
731 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
732
733 if (!alloca_inst)
734 {
735 if (log)
736 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
737 error.SetErrorToGenericError();
738 error.SetErrorString(interpreter_internal_error);
739 return false;
740 }
741
742 if (alloca_inst->isArrayAllocation())
743 {
744 if (log)
745 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
746 error.SetErrorToGenericError();
747 error.SetErrorString(unsupported_opcode_error);
748 return false;
749 }
750
751 // The semantics of Alloca are:
752 // Create a region R of virtual memory of type T, backed by a data buffer
753 // Create a region P of virtual memory of type T*, backed by a data buffer
754 // Write the virtual address of R into P
755
756 Type *T = alloca_inst->getAllocatedType();
757 Type *Tptr = alloca_inst->getType();
758
759 lldb::addr_t R = frame.Malloc(T);
760
761 if (R == LLDB_INVALID_ADDRESS)
762 {
763 if (log)
764 log->Printf("Couldn't allocate memory for an AllocaInst");
765 error.SetErrorToGenericError();
766 error.SetErrorString(memory_allocation_error);
767 return false;
768 }
769
770 lldb::addr_t P = frame.Malloc(Tptr);
771
772 if (P == LLDB_INVALID_ADDRESS)
773 {
774 if (log)
775 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
776 error.SetErrorToGenericError();
777 error.SetErrorString(memory_allocation_error);
778 return false;
779 }
780
781 lldb_private::Error write_error;
782
783 memory_map.WritePointerToMemory(P, R, write_error);
784
785 if (!write_error.Success())
786 {
787 if (log)
788 log->Printf("Couldn't write the result pointer for an AllocaInst");
789 error.SetErrorToGenericError();
790 error.SetErrorString(memory_write_error);
791 lldb_private::Error free_error;
792 memory_map.Free(P, free_error);
793 memory_map.Free(R, free_error);
794 return false;
795 }
796
797 frame.m_values[alloca_inst] = P;
798
799 if (log)
800 {
801 log->Printf("Interpreted an AllocaInst");
802 log->Printf(" R : 0x%" PRIx64, R);
803 log->Printf(" P : 0x%" PRIx64, P);
804 }
805 }
806 break;
807 case Instruction::BitCast:
808 case Instruction::ZExt:
809 {
810 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
811
812 if (!cast_inst)
813 {
814 if (log)
815 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
816 error.SetErrorToGenericError();
817 error.SetErrorString(interpreter_internal_error);
818 return false;
819 }
820
821 Value *source = cast_inst->getOperand(0);
822
823 lldb_private::Scalar S;
824
825 if (!frame.EvaluateValue(S, source, module))
826 {
827 if (log)
828 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
829 error.SetErrorToGenericError();
830 error.SetErrorString(bad_value_error);
831 return false;
832 }
833
834 frame.AssignValue(inst, S, module);
835 }
836 break;
837 case Instruction::SExt:
838 {
839 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
840
841 if (!cast_inst)
842 {
843 if (log)
844 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
845 error.SetErrorToGenericError();
846 error.SetErrorString(interpreter_internal_error);
847 return false;
848 }
849
850 Value *source = cast_inst->getOperand(0);
851
852 lldb_private::Scalar S;
853
854 if (!frame.EvaluateValue(S, source, module))
855 {
856 if (log)
857 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
858 error.SetErrorToGenericError();
859 error.SetErrorString(bad_value_error);
860 return false;
861 }
862
863 S.MakeSigned();
864
865 lldb_private::Scalar S_signextend(S.SLongLong());
866
867 frame.AssignValue(inst, S_signextend, module);
868 }
869 break;
870 case Instruction::Br:
871 {
872 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
873
874 if (!br_inst)
875 {
876 if (log)
877 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
878 error.SetErrorToGenericError();
879 error.SetErrorString(interpreter_internal_error);
880 return false;
881 }
882
883 if (br_inst->isConditional())
884 {
885 Value *condition = br_inst->getCondition();
886
887 lldb_private::Scalar C;
888
889 if (!frame.EvaluateValue(C, condition, module))
890 {
891 if (log)
892 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
893 error.SetErrorToGenericError();
894 error.SetErrorString(bad_value_error);
895 return false;
896 }
897
898 if (C.GetRawBits64(0))
899 frame.Jump(br_inst->getSuccessor(0));
900 else
901 frame.Jump(br_inst->getSuccessor(1));
902
903 if (log)
904 {
905 log->Printf("Interpreted a BrInst with a condition");
906 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
907 }
908 }
909 else
910 {
911 frame.Jump(br_inst->getSuccessor(0));
912
913 if (log)
914 {
915 log->Printf("Interpreted a BrInst with no condition");
916 }
917 }
918 }
919 continue;
920 case Instruction::GetElementPtr:
921 {
922 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
923
924 if (!gep_inst)
925 {
926 if (log)
927 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
928 error.SetErrorToGenericError();
929 error.SetErrorString(interpreter_internal_error);
930 return false;
931 }
932
933 const Value *pointer_operand = gep_inst->getPointerOperand();
934 Type *pointer_type = pointer_operand->getType();
935
936 lldb_private::Scalar P;
937
938 if (!frame.EvaluateValue(P, pointer_operand, module))
939 {
940 if (log)
941 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
942 error.SetErrorToGenericError();
943 error.SetErrorString(bad_value_error);
944 return false;
945 }
946
947 typedef SmallVector <Value *, 8> IndexVector;
948 typedef IndexVector::iterator IndexIterator;
949
950 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
951 gep_inst->idx_end());
952
953 SmallVector <Value *, 8> const_indices;
954
955 for (IndexIterator ii = indices.begin(), ie = indices.end();
956 ii != ie;
957 ++ii)
958 {
959 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
960
961 if (!constant_index)
962 {
963 lldb_private::Scalar I;
964
965 if (!frame.EvaluateValue(I, *ii, module))
966 {
967 if (log)
968 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
969 error.SetErrorToGenericError();
970 error.SetErrorString(bad_value_error);
971 return false;
972 }
973
974 if (log)
975 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
976
977 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
978 }
979
980 const_indices.push_back(constant_index);
981 }
982
983 uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices);
984
985 lldb_private::Scalar Poffset = P + offset;
986
987 frame.AssignValue(inst, Poffset, module);
988
989 if (log)
990 {
991 log->Printf("Interpreted a GetElementPtrInst");
992 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
993 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
994 }
995 }
996 break;
997 case Instruction::ICmp:
998 {
999 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1000
1001 if (!icmp_inst)
1002 {
1003 if (log)
1004 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
1005 error.SetErrorToGenericError();
1006 error.SetErrorString(interpreter_internal_error);
1007 return false;
1008 }
1009
1010 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1011
1012 Value *lhs = inst->getOperand(0);
1013 Value *rhs = inst->getOperand(1);
1014
1015 lldb_private::Scalar L;
1016 lldb_private::Scalar R;
1017
1018 if (!frame.EvaluateValue(L, lhs, module))
1019 {
1020 if (log)
1021 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
1022 error.SetErrorToGenericError();
1023 error.SetErrorString(bad_value_error);
1024 return false;
1025 }
1026
1027 if (!frame.EvaluateValue(R, rhs, module))
1028 {
1029 if (log)
1030 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
1031 error.SetErrorToGenericError();
1032 error.SetErrorString(bad_value_error);
1033 return false;
1034 }
1035
1036 lldb_private::Scalar result;
1037
1038 switch (predicate)
1039 {
1040 default:
1041 return false;
1042 case CmpInst::ICMP_EQ:
1043 result = (L == R);
1044 break;
1045 case CmpInst::ICMP_NE:
1046 result = (L != R);
1047 break;
1048 case CmpInst::ICMP_UGT:
1049 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1050 break;
1051 case CmpInst::ICMP_UGE:
1052 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1053 break;
1054 case CmpInst::ICMP_ULT:
1055 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1056 break;
1057 case CmpInst::ICMP_ULE:
1058 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1059 break;
1060 case CmpInst::ICMP_SGT:
1061 L.MakeSigned();
1062 R.MakeSigned();
1063 result = (L > R);
1064 break;
1065 case CmpInst::ICMP_SGE:
1066 L.MakeSigned();
1067 R.MakeSigned();
1068 result = (L >= R);
1069 break;
1070 case CmpInst::ICMP_SLT:
1071 L.MakeSigned();
1072 R.MakeSigned();
1073 result = (L < R);
1074 break;
1075 case CmpInst::ICMP_SLE:
1076 L.MakeSigned();
1077 R.MakeSigned();
1078 result = (L <= R);
1079 break;
1080 }
1081
1082 frame.AssignValue(inst, result, module);
1083
1084 if (log)
1085 {
1086 log->Printf("Interpreted an ICmpInst");
1087 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1088 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1089 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1090 }
1091 }
1092 break;
1093 case Instruction::IntToPtr:
1094 {
1095 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1096
1097 if (!int_to_ptr_inst)
1098 {
1099 if (log)
1100 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
1101 error.SetErrorToGenericError();
1102 error.SetErrorString(interpreter_internal_error);
1103 return false;
1104 }
1105
1106 Value *src_operand = int_to_ptr_inst->getOperand(0);
1107
1108 lldb_private::Scalar I;
1109
1110 if (!frame.EvaluateValue(I, src_operand, module))
1111 {
1112 if (log)
1113 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1114 error.SetErrorToGenericError();
1115 error.SetErrorString(bad_value_error);
1116 return false;
1117 }
1118
1119 frame.AssignValue(inst, I, module);
1120
1121 if (log)
1122 {
1123 log->Printf("Interpreted an IntToPtr");
1124 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1125 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1126 }
1127 }
1128 break;
1129 case Instruction::PtrToInt:
1130 {
1131 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1132
1133 if (!ptr_to_int_inst)
1134 {
1135 if (log)
1136 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1137 error.SetErrorToGenericError();
1138 error.SetErrorString(interpreter_internal_error);
1139 return false;
1140 }
1141
1142 Value *src_operand = ptr_to_int_inst->getOperand(0);
1143
1144 lldb_private::Scalar I;
1145
1146 if (!frame.EvaluateValue(I, src_operand, module))
1147 {
1148 if (log)
1149 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1150 error.SetErrorToGenericError();
1151 error.SetErrorString(bad_value_error);
1152 return false;
1153 }
1154
1155 frame.AssignValue(inst, I, module);
1156
1157 if (log)
1158 {
1159 log->Printf("Interpreted a PtrToInt");
1160 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1161 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1162 }
1163 }
1164 break;
1165 case Instruction::Load:
1166 {
1167 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1168
1169 if (!load_inst)
1170 {
1171 if (log)
1172 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
1173 error.SetErrorToGenericError();
1174 error.SetErrorString(interpreter_internal_error);
1175 return false;
1176 }
1177
1178 // The semantics of Load are:
1179 // Create a region D that will contain the loaded data
1180 // Resolve the region P containing a pointer
1181 // Dereference P to get the region R that the data should be loaded from
1182 // Transfer a unit of type type(D) from R to D
1183
1184 const Value *pointer_operand = load_inst->getPointerOperand();
1185
1186 Type *pointer_ty = pointer_operand->getType();
1187 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1188 if (!pointer_ptr_ty)
1189 {
1190 if (log)
1191 log->Printf("getPointerOperand()->getType() is not a PointerType");
1192 error.SetErrorToGenericError();
1193 error.SetErrorString(interpreter_internal_error);
1194 return false;
1195 }
1196 Type *target_ty = pointer_ptr_ty->getElementType();
1197
1198 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1199 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1200
1201 if (D == LLDB_INVALID_ADDRESS)
1202 {
1203 if (log)
1204 log->Printf("LoadInst's value doesn't resolve to anything");
1205 error.SetErrorToGenericError();
1206 error.SetErrorString(bad_value_error);
1207 return false;
1208 }
1209
1210 if (P == LLDB_INVALID_ADDRESS)
1211 {
1212 if (log)
1213 log->Printf("LoadInst's pointer doesn't resolve to anything");
1214 error.SetErrorToGenericError();
1215 error.SetErrorString(bad_value_error);
1216 return false;
1217 }
1218
1219 lldb::addr_t R;
1220 lldb_private::Error read_error;
1221 memory_map.ReadPointerFromMemory(&R, P, read_error);
1222
1223 if (!read_error.Success())
1224 {
1225 if (log)
1226 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1227 error.SetErrorToGenericError();
1228 error.SetErrorString(memory_read_error);
1229 return false;
1230 }
1231
1232 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1233 lldb_private::DataBufferHeap buffer(target_size, 0);
1234
1235 read_error.Clear();
1236 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
1237 if (!read_error.Success())
1238 {
1239 if (log)
1240 log->Printf("Couldn't read from a region on behalf of a LoadInst");
1241 error.SetErrorToGenericError();
1242 error.SetErrorString(memory_read_error);
1243 return false;
1244 }
1245
1246 lldb_private::Error write_error;
1247 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1248 if (!write_error.Success())
1249 {
1250 if (log)
1251 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1252 error.SetErrorToGenericError();
1253 error.SetErrorString(memory_read_error);
1254 return false;
1255 }
1256
1257 if (log)
1258 {
1259 log->Printf("Interpreted a LoadInst");
1260 log->Printf(" P : 0x%" PRIx64, P);
1261 log->Printf(" R : 0x%" PRIx64, R);
1262 log->Printf(" D : 0x%" PRIx64, D);
1263 }
1264 }
1265 break;
1266 case Instruction::Ret:
1267 {
1268 return true;
1269 }
1270 case Instruction::Store:
1271 {
1272 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1273
1274 if (!store_inst)
1275 {
1276 if (log)
1277 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
1278 error.SetErrorToGenericError();
1279 error.SetErrorString(interpreter_internal_error);
1280 return false;
1281 }
1282
1283 // The semantics of Store are:
1284 // Resolve the region D containing the data to be stored
1285 // Resolve the region P containing a pointer
1286 // Dereference P to get the region R that the data should be stored in
1287 // Transfer a unit of type type(D) from D to R
1288
1289 const Value *value_operand = store_inst->getValueOperand();
1290 const Value *pointer_operand = store_inst->getPointerOperand();
1291
1292 Type *pointer_ty = pointer_operand->getType();
1293 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1294 if (!pointer_ptr_ty)
1295 return false;
1296 Type *target_ty = pointer_ptr_ty->getElementType();
1297
1298 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1299 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1300
1301 if (D == LLDB_INVALID_ADDRESS)
1302 {
1303 if (log)
1304 log->Printf("StoreInst's value doesn't resolve to anything");
1305 error.SetErrorToGenericError();
1306 error.SetErrorString(bad_value_error);
1307 return false;
1308 }
1309
1310 if (P == LLDB_INVALID_ADDRESS)
1311 {
1312 if (log)
1313 log->Printf("StoreInst's pointer doesn't resolve to anything");
1314 error.SetErrorToGenericError();
1315 error.SetErrorString(bad_value_error);
1316 return false;
1317 }
1318
1319 lldb::addr_t R;
1320 lldb_private::Error read_error;
1321 memory_map.ReadPointerFromMemory(&R, P, read_error);
1322
1323 if (!read_error.Success())
1324 {
1325 if (log)
1326 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1327 error.SetErrorToGenericError();
1328 error.SetErrorString(memory_read_error);
1329 return false;
1330 }
1331
1332 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1333 lldb_private::DataBufferHeap buffer(target_size, 0);
1334
1335 read_error.Clear();
1336 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
1337 if (!read_error.Success())
1338 {
1339 if (log)
1340 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1341 error.SetErrorToGenericError();
1342 error.SetErrorString(memory_read_error);
1343 return false;
1344 }
1345
1346 lldb_private::Error write_error;
1347 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1348 if (!write_error.Success())
1349 {
1350 if (log)
1351 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1352 error.SetErrorToGenericError();
1353 error.SetErrorString(memory_write_error);
1354 return false;
1355 }
1356
1357 if (log)
1358 {
1359 log->Printf("Interpreted a StoreInst");
1360 log->Printf(" D : 0x%" PRIx64, D);
1361 log->Printf(" P : 0x%" PRIx64, P);
1362 log->Printf(" R : 0x%" PRIx64, R);
1363 }
1364 }
1365 break;
1366 }
1367
1368 ++frame.m_ii;
1369 }
1370
1371 if (num_insts >= 4096)
1372 {
1373 error.SetErrorToGenericError();
1374 error.SetErrorString(infinite_loop_error);
1375 return false;
1376 }
1377
1378 return false;
1379 }
1380