1 //===-- IRDynamicChecks.h ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_IRDynamicChecks_h_
11 #define liblldb_IRDynamicChecks_h_
13 #include "lldb/lldb-types.h"
14 #include "llvm/Pass.h"
27 namespace lldb_private
30 class ClangExpressionDeclMap;
31 class ClangUtilityFunction;
32 class ExecutionContext;
35 //----------------------------------------------------------------------
36 /// @class DynamicCheckerFunctions IRDynamicChecks.h "lldb/Expression/IRDynamicChecks.h"
37 /// @brief Encapsulates dynamic check functions used by expressions.
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 are:
42 /// a = *b; // check that b is a valid pointer
43 /// [b init]; // check that b is a valid object to send "init" to
45 /// The class installs each checker function into the target process and
46 /// makes it available to IRDynamicChecks to use.
47 //----------------------------------------------------------------------
48 class DynamicCheckerFunctions
51 //------------------------------------------------------------------
53 //------------------------------------------------------------------
54 DynamicCheckerFunctions ();
56 //------------------------------------------------------------------
58 //------------------------------------------------------------------
59 ~DynamicCheckerFunctions ();
61 //------------------------------------------------------------------
62 /// Install the utility functions into a process. This binds the
63 /// instance of DynamicCheckerFunctions to that process.
65 /// @param[in] error_stream
66 /// A stream to print errors on.
68 /// @param[in] exe_ctx
69 /// The execution context to install the functions into.
72 /// True on success; false on failure, or if the functions have
73 /// already been installed.
74 //------------------------------------------------------------------
75 bool Install (Stream &error_stream,
76 ExecutionContext &exe_ctx);
78 bool DoCheckersExplainStop (lldb::addr_t addr, Stream &message);
80 std::unique_ptr<ClangUtilityFunction> m_valid_pointer_check;
81 std::unique_ptr<ClangUtilityFunction> m_objc_object_check;
84 //----------------------------------------------------------------------
85 /// @class IRDynamicChecks IRDynamicChecks.h "lldb/Expression/IRDynamicChecks.h"
86 /// @brief Adds dynamic checks to a user-entered expression to reduce its likelihood of crashing
88 /// When an IR function is executed in the target process, it may cause
89 /// crashes or hangs by dereferencing NULL pointers, trying to call Objective-C
90 /// methods on objects that do not respond to them, and so forth.
92 /// IRDynamicChecks adds calls to the functions in DynamicCheckerFunctions
93 /// to appropriate locations in an expression's IR.
94 //----------------------------------------------------------------------
95 class IRDynamicChecks : public llvm::ModulePass
98 //------------------------------------------------------------------
101 /// @param[in] checker_functions
102 /// The checker functions for the target process.
104 /// @param[in] func_name
105 /// The name of the function to prepare for execution in the target.
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");
114 //------------------------------------------------------------------
116 //------------------------------------------------------------------
117 virtual ~IRDynamicChecks();
119 //------------------------------------------------------------------
120 /// Run this IR transformer on a single module
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
128 /// True on success; false otherwise
129 //------------------------------------------------------------------
130 bool runOnModule(llvm::Module &M);
132 //------------------------------------------------------------------
134 //------------------------------------------------------------------
135 void assignPassManager(llvm::PMStack &PMS,
136 llvm::PassManagerType T = llvm::PMT_ModulePassManager);
138 //------------------------------------------------------------------
139 /// Returns PMT_ModulePassManager
140 //------------------------------------------------------------------
141 llvm::PassManagerType getPotentialPassManagerType() const;
143 //------------------------------------------------------------------
144 /// A basic block-level pass to find all pointer dereferences and
145 /// validate them before use.
146 //------------------------------------------------------------------
148 //------------------------------------------------------------------
149 /// The top-level pass implementation
152 /// The module currently being processed.
155 /// The basic block currently being processed.
158 /// True on success; false otherwise
159 //------------------------------------------------------------------
160 bool FindDataLoads(llvm::Module &M,
161 llvm::BasicBlock &BB);
163 std::string m_func_name; ///< The name of the function to add checks to
164 DynamicCheckerFunctions &m_checker_functions; ///< The checker functions for the process