]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Expression/IRDynamicChecks.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Expression / IRDynamicChecks.cpp
1 //===-- IRDynamicChecks.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 "llvm/IR/Constants.h"
11 #include "llvm/IR/DataLayout.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/Value.h"
16 #include "llvm/Support/raw_ostream.h"
17
18 #include "lldb/Expression/IRDynamicChecks.h"
19
20 #include "lldb/Expression/UtilityFunction.h"
21 #include "lldb/Target/ExecutionContext.h"
22 #include "lldb/Target/ObjCLanguageRuntime.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/StackFrame.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Utility/ConstString.h"
27 #include "lldb/Utility/Log.h"
28
29 using namespace llvm;
30 using namespace lldb_private;
31
32 static char ID;
33
34 #define VALID_POINTER_CHECK_NAME "_$__lldb_valid_pointer_check"
35 #define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check"
36
37 static const char g_valid_pointer_check_text[] =
38     "extern \"C\" void\n"
39     "_$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n"
40     "{\n"
41     "    unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
42     "}";
43
44 DynamicCheckerFunctions::DynamicCheckerFunctions() = default;
45
46 DynamicCheckerFunctions::~DynamicCheckerFunctions() = default;
47
48 bool DynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager,
49                                       ExecutionContext &exe_ctx) {
50   Status error;
51   m_valid_pointer_check.reset(
52       exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
53           g_valid_pointer_check_text, lldb::eLanguageTypeC,
54           VALID_POINTER_CHECK_NAME, error));
55   if (error.Fail())
56     return false;
57
58   if (!m_valid_pointer_check->Install(diagnostic_manager, exe_ctx))
59     return false;
60
61   Process *process = exe_ctx.GetProcessPtr();
62
63   if (process) {
64     ObjCLanguageRuntime *objc_language_runtime =
65         process->GetObjCLanguageRuntime();
66
67     if (objc_language_runtime) {
68       m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker(
69           VALID_OBJC_OBJECT_CHECK_NAME));
70
71       if (!m_objc_object_check->Install(diagnostic_manager, exe_ctx))
72         return false;
73     }
74   }
75
76   return true;
77 }
78
79 bool DynamicCheckerFunctions::DoCheckersExplainStop(lldb::addr_t addr,
80                                                     Stream &message) {
81   // FIXME: We have to get the checkers to know why they scotched the call in
82   // more detail,
83   // so we can print a better message here.
84   if (m_valid_pointer_check && m_valid_pointer_check->ContainsAddress(addr)) {
85     message.Printf("Attempted to dereference an invalid pointer.");
86     return true;
87   } else if (m_objc_object_check &&
88              m_objc_object_check->ContainsAddress(addr)) {
89     message.Printf("Attempted to dereference an invalid ObjC Object or send it "
90                    "an unrecognized selector");
91     return true;
92   }
93   return false;
94 }
95
96 static std::string PrintValue(llvm::Value *V, bool truncate = false) {
97   std::string s;
98   raw_string_ostream rso(s);
99   V->print(rso);
100   rso.flush();
101   if (truncate)
102     s.resize(s.length() - 1);
103   return s;
104 }
105
106 //----------------------------------------------------------------------
107 /// @class Instrumenter IRDynamicChecks.cpp
108 /// Finds and instruments individual LLVM IR instructions
109 ///
110 /// When instrumenting LLVM IR, it is frequently desirable to first search for
111 /// instructions, and then later modify them.  This way iterators remain
112 /// intact, and multiple passes can look at the same code base without
113 /// treading on each other's toes.
114 ///
115 /// The Instrumenter class implements this functionality.  A client first
116 /// calls Inspect on a function, which populates a list of instructions to be
117 /// instrumented.  Then, later, when all passes' Inspect functions have been
118 /// called, the client calls Instrument, which adds the desired
119 /// instrumentation.
120 ///
121 /// A subclass of Instrumenter must override InstrumentInstruction, which
122 /// is responsible for adding whatever instrumentation is necessary.
123 ///
124 /// A subclass of Instrumenter may override:
125 ///
126 /// - InspectInstruction [default: does nothing]
127 ///
128 /// - InspectBasicBlock [default: iterates through the instructions in a
129 ///   basic block calling InspectInstruction]
130 ///
131 /// - InspectFunction [default: iterates through the basic blocks in a
132 ///   function calling InspectBasicBlock]
133 //----------------------------------------------------------------------
134 class Instrumenter {
135 public:
136   //------------------------------------------------------------------
137   /// Constructor
138   ///
139   /// @param[in] module
140   ///     The module being instrumented.
141   //------------------------------------------------------------------
142   Instrumenter(llvm::Module &module, DynamicCheckerFunctions &checker_functions)
143       : m_module(module), m_checker_functions(checker_functions),
144         m_i8ptr_ty(nullptr), m_intptr_ty(nullptr) {}
145
146   virtual ~Instrumenter() = default;
147
148   //------------------------------------------------------------------
149   /// Inspect a function to find instructions to instrument
150   ///
151   /// @param[in] function
152   ///     The function to inspect.
153   ///
154   /// @return
155   ///     True on success; false on error.
156   //------------------------------------------------------------------
157   bool Inspect(llvm::Function &function) { return InspectFunction(function); }
158
159   //------------------------------------------------------------------
160   /// Instrument all the instructions found by Inspect()
161   ///
162   /// @return
163   ///     True on success; false on error.
164   //------------------------------------------------------------------
165   bool Instrument() {
166     for (InstIterator ii = m_to_instrument.begin(),
167                       last_ii = m_to_instrument.end();
168          ii != last_ii; ++ii) {
169       if (!InstrumentInstruction(*ii))
170         return false;
171     }
172
173     return true;
174   }
175
176 protected:
177   //------------------------------------------------------------------
178   /// Add instrumentation to a single instruction
179   ///
180   /// @param[in] inst
181   ///     The instruction to be instrumented.
182   ///
183   /// @return
184   ///     True on success; false otherwise.
185   //------------------------------------------------------------------
186   virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0;
187
188   //------------------------------------------------------------------
189   /// Register a single instruction to be instrumented
190   ///
191   /// @param[in] inst
192   ///     The instruction to be instrumented.
193   //------------------------------------------------------------------
194   void RegisterInstruction(llvm::Instruction &i) {
195     m_to_instrument.push_back(&i);
196   }
197
198   //------------------------------------------------------------------
199   /// Determine whether a single instruction is interesting to instrument,
200   /// and, if so, call RegisterInstruction
201   ///
202   /// @param[in] i
203   ///     The instruction to be inspected.
204   ///
205   /// @return
206   ///     False if there was an error scanning; true otherwise.
207   //------------------------------------------------------------------
208   virtual bool InspectInstruction(llvm::Instruction &i) { return true; }
209
210   //------------------------------------------------------------------
211   /// Scan a basic block to see if any instructions are interesting
212   ///
213   /// @param[in] bb
214   ///     The basic block to be inspected.
215   ///
216   /// @return
217   ///     False if there was an error scanning; true otherwise.
218   //------------------------------------------------------------------
219   virtual bool InspectBasicBlock(llvm::BasicBlock &bb) {
220     for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end();
221          ii != last_ii; ++ii) {
222       if (!InspectInstruction(*ii))
223         return false;
224     }
225
226     return true;
227   }
228
229   //------------------------------------------------------------------
230   /// Scan a function to see if any instructions are interesting
231   ///
232   /// @param[in] f
233   ///     The function to be inspected.
234   ///
235   /// @return
236   ///     False if there was an error scanning; true otherwise.
237   //------------------------------------------------------------------
238   virtual bool InspectFunction(llvm::Function &f) {
239     for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end();
240          bbi != last_bbi; ++bbi) {
241       if (!InspectBasicBlock(*bbi))
242         return false;
243     }
244
245     return true;
246   }
247
248   //------------------------------------------------------------------
249   /// Build a function pointer for a function with signature void
250   /// (*)(uint8_t*) with a given address
251   ///
252   /// @param[in] start_address
253   ///     The address of the function.
254   ///
255   /// @return
256   ///     The function pointer, for use in a CallInst.
257   //------------------------------------------------------------------
258   llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address) {
259     llvm::Type *param_array[1];
260
261     param_array[0] = const_cast<llvm::PointerType *>(GetI8PtrTy());
262
263     ArrayRef<llvm::Type *> params(param_array, 1);
264
265     FunctionType *fun_ty = FunctionType::get(
266         llvm::Type::getVoidTy(m_module.getContext()), params, true);
267     PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
268     Constant *fun_addr_int =
269         ConstantInt::get(GetIntptrTy(), start_address, false);
270     return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
271   }
272
273   //------------------------------------------------------------------
274   /// Build a function pointer for a function with signature void
275   /// (*)(uint8_t*, uint8_t*) with a given address
276   ///
277   /// @param[in] start_address
278   ///     The address of the function.
279   ///
280   /// @return
281   ///     The function pointer, for use in a CallInst.
282   //------------------------------------------------------------------
283   llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address) {
284     llvm::Type *param_array[2];
285
286     param_array[0] = const_cast<llvm::PointerType *>(GetI8PtrTy());
287     param_array[1] = const_cast<llvm::PointerType *>(GetI8PtrTy());
288
289     ArrayRef<llvm::Type *> params(param_array, 2);
290
291     FunctionType *fun_ty = FunctionType::get(
292         llvm::Type::getVoidTy(m_module.getContext()), params, true);
293     PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
294     Constant *fun_addr_int =
295         ConstantInt::get(GetIntptrTy(), start_address, false);
296     return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
297   }
298
299   PointerType *GetI8PtrTy() {
300     if (!m_i8ptr_ty)
301       m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext());
302
303     return m_i8ptr_ty;
304   }
305
306   IntegerType *GetIntptrTy() {
307     if (!m_intptr_ty) {
308       llvm::DataLayout data_layout(&m_module);
309
310       m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
311                                           data_layout.getPointerSizeInBits());
312     }
313
314     return m_intptr_ty;
315   }
316
317   typedef std::vector<llvm::Instruction *> InstVector;
318   typedef InstVector::iterator InstIterator;
319
320   InstVector m_to_instrument; ///< List of instructions the inspector found
321   llvm::Module &m_module;     ///< The module which is being instrumented
322   DynamicCheckerFunctions
323       &m_checker_functions; ///< The dynamic checker functions for the process
324
325 private:
326   PointerType *m_i8ptr_ty;
327   IntegerType *m_intptr_ty;
328 };
329
330 class ValidPointerChecker : public Instrumenter {
331 public:
332   ValidPointerChecker(llvm::Module &module,
333                       DynamicCheckerFunctions &checker_functions)
334       : Instrumenter(module, checker_functions),
335         m_valid_pointer_check_func(nullptr) {}
336
337   ~ValidPointerChecker() override = default;
338
339 protected:
340   bool InstrumentInstruction(llvm::Instruction *inst) override {
341     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
342
343     if (log)
344       log->Printf("Instrumenting load/store instruction: %s\n",
345                   PrintValue(inst).c_str());
346
347     if (!m_valid_pointer_check_func)
348       m_valid_pointer_check_func = BuildPointerValidatorFunc(
349           m_checker_functions.m_valid_pointer_check->StartAddress());
350
351     llvm::Value *dereferenced_ptr = nullptr;
352
353     if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst>(inst))
354       dereferenced_ptr = li->getPointerOperand();
355     else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst>(inst))
356       dereferenced_ptr = si->getPointerOperand();
357     else
358       return false;
359
360     // Insert an instruction to cast the loaded value to int8_t*
361
362     BitCastInst *bit_cast =
363         new BitCastInst(dereferenced_ptr, GetI8PtrTy(), "", inst);
364
365     // Insert an instruction to call the helper with the result
366
367     llvm::Value *arg_array[1];
368
369     arg_array[0] = bit_cast;
370
371     llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
372
373     CallInst::Create(m_valid_pointer_check_func, args, "", inst);
374
375     return true;
376   }
377
378   bool InspectInstruction(llvm::Instruction &i) override {
379     if (dyn_cast<llvm::LoadInst>(&i) || dyn_cast<llvm::StoreInst>(&i))
380       RegisterInstruction(i);
381
382     return true;
383   }
384
385 private:
386   llvm::Value *m_valid_pointer_check_func;
387 };
388
389 class ObjcObjectChecker : public Instrumenter {
390 public:
391   ObjcObjectChecker(llvm::Module &module,
392                     DynamicCheckerFunctions &checker_functions)
393       : Instrumenter(module, checker_functions),
394         m_objc_object_check_func(nullptr) {}
395
396   ~ObjcObjectChecker() override = default;
397
398   enum msgSend_type {
399     eMsgSend = 0,
400     eMsgSendSuper,
401     eMsgSendSuper_stret,
402     eMsgSend_fpret,
403     eMsgSend_stret
404   };
405
406   std::map<llvm::Instruction *, msgSend_type> msgSend_types;
407
408 protected:
409   bool InstrumentInstruction(llvm::Instruction *inst) override {
410     CallInst *call_inst = dyn_cast<CallInst>(inst);
411
412     if (!call_inst)
413       return false; // call_inst really shouldn't be nullptr, because otherwise
414                     // InspectInstruction wouldn't have registered it
415
416     if (!m_objc_object_check_func)
417       m_objc_object_check_func = BuildObjectCheckerFunc(
418           m_checker_functions.m_objc_object_check->StartAddress());
419
420     // id objc_msgSend(id theReceiver, SEL theSelector, ...)
421
422     llvm::Value *target_object;
423     llvm::Value *selector;
424
425     switch (msgSend_types[inst]) {
426     case eMsgSend:
427     case eMsgSend_fpret:
428       target_object = call_inst->getArgOperand(0);
429       selector = call_inst->getArgOperand(1);
430       break;
431     case eMsgSend_stret:
432       target_object = call_inst->getArgOperand(1);
433       selector = call_inst->getArgOperand(2);
434       break;
435     case eMsgSendSuper:
436     case eMsgSendSuper_stret:
437       return true;
438     }
439
440     // These objects should always be valid according to Sean Calannan
441     assert(target_object);
442     assert(selector);
443
444     // Insert an instruction to cast the receiver id to int8_t*
445
446     BitCastInst *bit_cast =
447         new BitCastInst(target_object, GetI8PtrTy(), "", inst);
448
449     // Insert an instruction to call the helper with the result
450
451     llvm::Value *arg_array[2];
452
453     arg_array[0] = bit_cast;
454     arg_array[1] = selector;
455
456     ArrayRef<llvm::Value *> args(arg_array, 2);
457
458     CallInst::Create(m_objc_object_check_func, args, "", inst);
459
460     return true;
461   }
462
463   static llvm::Function *GetFunction(llvm::Value *value) {
464     if (llvm::Function *function = llvm::dyn_cast<llvm::Function>(value)) {
465       return function;
466     }
467
468     if (llvm::ConstantExpr *const_expr =
469             llvm::dyn_cast<llvm::ConstantExpr>(value)) {
470       switch (const_expr->getOpcode()) {
471       default:
472         return nullptr;
473       case llvm::Instruction::BitCast:
474         return GetFunction(const_expr->getOperand(0));
475       }
476     }
477
478     return nullptr;
479   }
480
481   static llvm::Function *GetCalledFunction(llvm::CallInst *inst) {
482     return GetFunction(inst->getCalledValue());
483   }
484
485   bool InspectInstruction(llvm::Instruction &i) override {
486     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
487
488     CallInst *call_inst = dyn_cast<CallInst>(&i);
489
490     if (call_inst) {
491       const llvm::Function *called_function = GetCalledFunction(call_inst);
492
493       if (!called_function)
494         return true;
495
496       std::string name_str = called_function->getName().str();
497       const char *name_cstr = name_str.c_str();
498
499       if (log)
500         log->Printf("Found call to %s: %s\n", name_cstr,
501                     PrintValue(call_inst).c_str());
502
503       if (name_str.find("objc_msgSend") == std::string::npos)
504         return true;
505
506       if (!strcmp(name_cstr, "objc_msgSend")) {
507         RegisterInstruction(i);
508         msgSend_types[&i] = eMsgSend;
509         return true;
510       }
511
512       if (!strcmp(name_cstr, "objc_msgSend_stret")) {
513         RegisterInstruction(i);
514         msgSend_types[&i] = eMsgSend_stret;
515         return true;
516       }
517
518       if (!strcmp(name_cstr, "objc_msgSend_fpret")) {
519         RegisterInstruction(i);
520         msgSend_types[&i] = eMsgSend_fpret;
521         return true;
522       }
523
524       if (!strcmp(name_cstr, "objc_msgSendSuper")) {
525         RegisterInstruction(i);
526         msgSend_types[&i] = eMsgSendSuper;
527         return true;
528       }
529
530       if (!strcmp(name_cstr, "objc_msgSendSuper_stret")) {
531         RegisterInstruction(i);
532         msgSend_types[&i] = eMsgSendSuper_stret;
533         return true;
534       }
535
536       if (log)
537         log->Printf(
538             "Function name '%s' contains 'objc_msgSend' but is not handled",
539             name_str.c_str());
540
541       return true;
542     }
543
544     return true;
545   }
546
547 private:
548   llvm::Value *m_objc_object_check_func;
549 };
550
551 IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
552                                  const char *func_name)
553     : ModulePass(ID), m_func_name(func_name),
554       m_checker_functions(checker_functions) {}
555
556 IRDynamicChecks::~IRDynamicChecks() = default;
557
558 bool IRDynamicChecks::runOnModule(llvm::Module &M) {
559   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
560
561   llvm::Function *function = M.getFunction(StringRef(m_func_name));
562
563   if (!function) {
564     if (log)
565       log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
566
567     return false;
568   }
569
570   if (m_checker_functions.m_valid_pointer_check) {
571     ValidPointerChecker vpc(M, m_checker_functions);
572
573     if (!vpc.Inspect(*function))
574       return false;
575
576     if (!vpc.Instrument())
577       return false;
578   }
579
580   if (m_checker_functions.m_objc_object_check) {
581     ObjcObjectChecker ooc(M, m_checker_functions);
582
583     if (!ooc.Inspect(*function))
584       return false;
585
586     if (!ooc.Instrument())
587       return false;
588   }
589
590   if (log && log->GetVerbose()) {
591     std::string s;
592     raw_string_ostream oss(s);
593
594     M.print(oss, nullptr);
595
596     oss.flush();
597
598     log->Printf("Module after dynamic checks: \n%s", s.c_str());
599   }
600
601   return true;
602 }
603
604 void IRDynamicChecks::assignPassManager(PMStack &PMS, PassManagerType T) {}
605
606 PassManagerType IRDynamicChecks::getPotentialPassManagerType() const {
607   return PMT_ModulePassManager;
608 }