1 //===-- IRDynamicChecks.h ---------------------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef liblldb_IRDynamicChecks_h_
12 #define liblldb_IRDynamicChecks_h_
14 #include "lldb/lldb-types.h"
15 #include "llvm/Pass.h"
28 namespace lldb_private {
30 class ClangExpressionDeclMap;
31 class ExecutionContext;
34 //----------------------------------------------------------------------
35 /// @class DynamicCheckerFunctions IRDynamicChecks.h
36 /// "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 {
50 //------------------------------------------------------------------
52 //------------------------------------------------------------------
53 DynamicCheckerFunctions();
55 //------------------------------------------------------------------
57 //------------------------------------------------------------------
58 ~DynamicCheckerFunctions();
60 //------------------------------------------------------------------
61 /// Install the utility functions into a process. This binds the
62 /// instance of DynamicCheckerFunctions to that process.
64 /// @param[in] diagnostic_manager
65 /// A diagnostic manager to report errors to.
67 /// @param[in] exe_ctx
68 /// The execution context to install the functions into.
71 /// True on success; false on failure, or if the functions have
72 /// already been installed.
73 //------------------------------------------------------------------
74 bool Install(DiagnosticManager &diagnostic_manager,
75 ExecutionContext &exe_ctx);
77 bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message);
79 std::unique_ptr<UtilityFunction> m_valid_pointer_check;
80 std::unique_ptr<UtilityFunction> m_objc_object_check;
83 //----------------------------------------------------------------------
84 /// @class IRDynamicChecks IRDynamicChecks.h "lldb/Expression/IRDynamicChecks.h"
85 /// @brief Adds dynamic checks to a user-entered expression to reduce its
86 /// 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 {
97 //------------------------------------------------------------------
100 /// @param[in] checker_functions
101 /// The checker functions for the target process.
103 /// @param[in] func_name
104 /// The name of the function to prepare for execution in the target.
106 /// @param[in] decl_map
107 /// The mapping used to look up entities in the target process. In
108 /// this case, used to find objc_msgSend
109 //------------------------------------------------------------------
110 IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
111 const char *func_name = "$__lldb_expr");
113 //------------------------------------------------------------------
115 //------------------------------------------------------------------
116 ~IRDynamicChecks() override;
118 //------------------------------------------------------------------
119 /// Run this IR transformer on a single module
122 /// The module to run on. This module is searched for the function
123 /// $__lldb_expr, and that function is passed to the passes one by
127 /// True on success; false otherwise
128 //------------------------------------------------------------------
129 bool runOnModule(llvm::Module &M) override;
131 //------------------------------------------------------------------
133 //------------------------------------------------------------------
134 void assignPassManager(
136 llvm::PassManagerType T = llvm::PMT_ModulePassManager) override;
138 //------------------------------------------------------------------
139 /// Returns PMT_ModulePassManager
140 //------------------------------------------------------------------
141 llvm::PassManagerType getPotentialPassManagerType() const override;
144 //------------------------------------------------------------------
145 /// A basic block-level pass to find all pointer dereferences and
146 /// validate them before use.
147 //------------------------------------------------------------------
149 //------------------------------------------------------------------
150 /// The top-level pass implementation
153 /// The module currently being processed.
156 /// The basic block currently being processed.
159 /// True on success; false otherwise
160 //------------------------------------------------------------------
161 bool FindDataLoads(llvm::Module &M, llvm::BasicBlock &BB);
163 std::string m_func_name; ///< The name of the function to add checks to
164 DynamicCheckerFunctions
165 &m_checker_functions; ///< The checker functions for the process
168 } // namespace lldb_private
170 #endif // liblldb_IRDynamicChecks_h_