1//===-- IRDynamicChecks.h ---------------------------------------------*- 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#ifndef liblldb_IRDynamicChecks_h_
11#define liblldb_IRDynamicChecks_h_
12
13#include "lldb/lldb-types.h"
14#include "llvm/Pass.h"
15
16namespace llvm {
17    class BasicBlock;
18    class CallInst;
19    class Constant;
20    class Function;
21    class Instruction;
22    class Module;
23    class DataLayout;
24    class Value;
25}
26
27namespace lldb_private
28{
29
30class ClangExpressionDeclMap;
31class ClangUtilityFunction;
32class ExecutionContext;
33class Stream;
34
35//----------------------------------------------------------------------
36/// @class DynamicCheckerFunctions IRDynamicChecks.h "lldb/Expression/IRDynamicChecks.h"
37/// @brief Encapsulates dynamic check functions 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 are:
41///
42/// a = *b;     // check that b is a valid pointer
43/// [b init];   // check that b is a valid object to send "init" to
44///
45/// The class installs each checker function into the target process and
46/// makes it available to IRDynamicChecks to use.
47//----------------------------------------------------------------------
48class DynamicCheckerFunctions
49{
50public:
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
63    /// instance of DynamicCheckerFunctions to that process.
64    ///
65    /// @param[in] error_stream
66    ///     A stream to print errors on.
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 (Stream &error_stream,
76                  ExecutionContext &exe_ctx);
77
78    bool DoCheckersExplainStop (lldb::addr_t addr, Stream &message);
79
80    std::unique_ptr<ClangUtilityFunction> m_valid_pointer_check;
81    std::unique_ptr<ClangUtilityFunction> m_objc_object_check;
82};
83
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
87///
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.
91///
92/// IRDynamicChecks adds calls to the functions in DynamicCheckerFunctions
93/// to appropriate locations in an expression's IR.
94//----------------------------------------------------------------------
95class IRDynamicChecks : public llvm::ModulePass
96{
97public:
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    virtual ~IRDynamicChecks();
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);
131
132    //------------------------------------------------------------------
133    /// Interface stub
134    //------------------------------------------------------------------
135    void assignPassManager(llvm::PMStack &PMS,
136                           llvm::PassManagerType T = llvm::PMT_ModulePassManager);
137
138    //------------------------------------------------------------------
139    /// Returns PMT_ModulePassManager
140    //------------------------------------------------------------------
141    llvm::PassManagerType getPotentialPassManagerType() const;
142private:
143    //------------------------------------------------------------------
144    /// A basic block-level pass to find all pointer dereferences and
145    /// validate them before use.
146    //------------------------------------------------------------------
147
148    //------------------------------------------------------------------
149    /// The top-level pass implementation
150    ///
151    /// @param[in] M
152    ///     The module currently being processed.
153    ///
154    /// @param[in] BB
155    ///     The basic block currently being processed.
156    ///
157    /// @return
158    ///     True on success; false otherwise
159    //------------------------------------------------------------------
160    bool FindDataLoads(llvm::Module &M,
161                       llvm::BasicBlock &BB);
162
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
165};
166
167}
168
169#endif
170