1 //===-- IRDynamicChecks.h ---------------------------------------------*- C++ 2 //-*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #ifndef liblldb_IRDynamicChecks_h_ 12 #define liblldb_IRDynamicChecks_h_ 13 14 #include "lldb/lldb-types.h" 15 #include "llvm/Pass.h" 16 17 namespace llvm { 18 class BasicBlock; 19 class CallInst; 20 class Constant; 21 class Function; 22 class Instruction; 23 class Module; 24 class DataLayout; 25 class Value; 26 } 27 28 namespace lldb_private { 29 30 class ClangExpressionDeclMap; 31 class ExecutionContext; 32 class Stream; 33 34 //---------------------------------------------------------------------- 35 /// @class DynamicCheckerFunctions IRDynamicChecks.h 36 /// "lldb/Expression/IRDynamicChecks.h" Encapsulates dynamic check functions 37 /// used by expressions. 38 /// 39 /// Each of the utility functions encapsulated in this class is responsible 40 /// for validating some data that an expression is about to use. Examples 41 /// are: 42 /// 43 /// a = *b; // check that b is a valid pointer [b init]; // check that b 44 /// is a valid object to send "init" to 45 /// 46 /// The class installs each checker function into the target process and makes 47 /// it available to IRDynamicChecks to use. 48 //---------------------------------------------------------------------- 49 class DynamicCheckerFunctions { 50 public: 51 //------------------------------------------------------------------ 52 /// Constructor 53 //------------------------------------------------------------------ 54 DynamicCheckerFunctions(); 55 56 //------------------------------------------------------------------ 57 /// Destructor 58 //------------------------------------------------------------------ 59 ~DynamicCheckerFunctions(); 60 61 //------------------------------------------------------------------ 62 /// Install the utility functions into a process. This binds the instance 63 /// of DynamicCheckerFunctions to that process. 64 /// 65 /// @param[in] diagnostic_manager 66 /// A diagnostic manager to report errors to. 67 /// 68 /// @param[in] exe_ctx 69 /// The execution context to install the functions into. 70 /// 71 /// @return 72 /// True on success; false on failure, or if the functions have 73 /// already been installed. 74 //------------------------------------------------------------------ 75 bool Install(DiagnosticManager &diagnostic_manager, 76 ExecutionContext &exe_ctx); 77 78 bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message); 79 80 std::unique_ptr<UtilityFunction> m_valid_pointer_check; 81 std::unique_ptr<UtilityFunction> m_objc_object_check; 82 }; 83 84 //---------------------------------------------------------------------- 85 /// @class IRDynamicChecks IRDynamicChecks.h 86 /// "lldb/Expression/IRDynamicChecks.h" Adds dynamic checks to a user-entered 87 /// expression to reduce its likelihood of crashing 88 /// 89 /// When an IR function is executed in the target process, it may cause 90 /// crashes or hangs by dereferencing NULL pointers, trying to call 91 /// Objective-C methods on objects that do not respond to them, and so forth. 92 /// 93 /// IRDynamicChecks adds calls to the functions in DynamicCheckerFunctions to 94 /// appropriate locations in an expression's IR. 95 //---------------------------------------------------------------------- 96 class IRDynamicChecks : public llvm::ModulePass { 97 public: 98 //------------------------------------------------------------------ 99 /// Constructor 100 /// 101 /// @param[in] checker_functions 102 /// The checker functions for the target process. 103 /// 104 /// @param[in] func_name 105 /// The name of the function to prepare for execution in the target. 106 /// 107 /// @param[in] decl_map 108 /// The mapping used to look up entities in the target process. In 109 /// this case, used to find objc_msgSend 110 //------------------------------------------------------------------ 111 IRDynamicChecks(DynamicCheckerFunctions &checker_functions, 112 const char *func_name = "$__lldb_expr"); 113 114 //------------------------------------------------------------------ 115 /// Destructor 116 //------------------------------------------------------------------ 117 ~IRDynamicChecks() override; 118 119 //------------------------------------------------------------------ 120 /// Run this IR transformer on a single module 121 /// 122 /// @param[in] M 123 /// The module to run on. This module is searched for the function 124 /// $__lldb_expr, and that function is passed to the passes one by 125 /// one. 126 /// 127 /// @return 128 /// True on success; false otherwise 129 //------------------------------------------------------------------ 130 bool runOnModule(llvm::Module &M) override; 131 132 //------------------------------------------------------------------ 133 /// Interface stub 134 //------------------------------------------------------------------ 135 void assignPassManager( 136 llvm::PMStack &PMS, 137 llvm::PassManagerType T = llvm::PMT_ModulePassManager) override; 138 139 //------------------------------------------------------------------ 140 /// Returns PMT_ModulePassManager 141 //------------------------------------------------------------------ 142 llvm::PassManagerType getPotentialPassManagerType() const override; 143 144 private: 145 //------------------------------------------------------------------ 146 /// A basic block-level pass to find all pointer dereferences and 147 /// validate them before use. 148 //------------------------------------------------------------------ 149 150 //------------------------------------------------------------------ 151 /// The top-level pass implementation 152 /// 153 /// @param[in] M 154 /// The module currently being processed. 155 /// 156 /// @param[in] BB 157 /// The basic block currently being processed. 158 /// 159 /// @return 160 /// True on success; false otherwise 161 //------------------------------------------------------------------ 162 bool FindDataLoads(llvm::Module &M, llvm::BasicBlock &BB); 163 164 std::string m_func_name; ///< The name of the function to add checks to 165 DynamicCheckerFunctions 166 &m_checker_functions; ///< The checker functions for the process 167 }; 168 169 } // namespace lldb_private 170 171 #endif // liblldb_IRDynamicChecks_h_ 172