xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1 //===-- IRDynamicChecks.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef liblldb_IRDynamicChecks_h_
10 #define liblldb_IRDynamicChecks_h_
11 
12 #include "lldb/Expression/DynamicCheckerFunctions.h"
13 #include "lldb/lldb-types.h"
14 #include "llvm/Pass.h"
15 
16 namespace llvm {
17 class BasicBlock;
18 class Module;
19 }
20 
21 namespace lldb_private {
22 
23 class ExecutionContext;
24 class Stream;
25 
26 class ClangDynamicCheckerFunctions
27     : public lldb_private::DynamicCheckerFunctions {
28 public:
29   /// Constructor
30   ClangDynamicCheckerFunctions();
31 
32   /// Destructor
33   virtual ~ClangDynamicCheckerFunctions();
34 
35   static bool classof(const DynamicCheckerFunctions *checker_funcs) {
36     return checker_funcs->GetKind() == DCF_Clang;
37   }
38 
39   /// Install the utility functions into a process.  This binds the instance
40   /// of DynamicCheckerFunctions to that process.
41   ///
42   /// \param[in] diagnostic_manager
43   ///     A diagnostic manager to report errors to.
44   ///
45   /// \param[in] exe_ctx
46   ///     The execution context to install the functions into.
47   ///
48   /// \return
49   ///     True on success; false on failure, or if the functions have
50   ///     already been installed.
51   bool Install(DiagnosticManager &diagnostic_manager,
52                ExecutionContext &exe_ctx) override;
53 
54   bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message) override;
55 
56   std::shared_ptr<UtilityFunction> m_valid_pointer_check;
57   std::shared_ptr<UtilityFunction> m_objc_object_check;
58 };
59 
60 /// \class IRDynamicChecks IRDynamicChecks.h
61 /// "lldb/Expression/IRDynamicChecks.h" Adds dynamic checks to a user-entered
62 /// expression to reduce its likelihood of crashing
63 ///
64 /// When an IR function is executed in the target process, it may cause
65 /// crashes or hangs by dereferencing NULL pointers, trying to call
66 /// Objective-C methods on objects that do not respond to them, and so forth.
67 ///
68 /// IRDynamicChecks adds calls to the functions in DynamicCheckerFunctions to
69 /// appropriate locations in an expression's IR.
70 class IRDynamicChecks : public llvm::ModulePass {
71 public:
72   /// Constructor
73   ///
74   /// \param[in] checker_functions
75   ///     The checker functions for the target process.
76   ///
77   /// \param[in] func_name
78   ///     The name of the function to prepare for execution in the target.
79   ///
80   /// \param[in] decl_map
81   ///     The mapping used to look up entities in the target process. In
82   ///     this case, used to find objc_msgSend
83   IRDynamicChecks(ClangDynamicCheckerFunctions &checker_functions,
84                   const char *func_name = "$__lldb_expr");
85 
86   /// Destructor
87   ~IRDynamicChecks() override;
88 
89   /// Run this IR transformer on a single module
90   ///
91   /// \param[in] M
92   ///     The module to run on.  This module is searched for the function
93   ///     $__lldb_expr, and that function is passed to the passes one by
94   ///     one.
95   ///
96   /// \return
97   ///     True on success; false otherwise
98   bool runOnModule(llvm::Module &M) override;
99 
100   /// Interface stub
101   void assignPassManager(
102       llvm::PMStack &PMS,
103       llvm::PassManagerType T = llvm::PMT_ModulePassManager) override;
104 
105   /// Returns PMT_ModulePassManager
106   llvm::PassManagerType getPotentialPassManagerType() const override;
107 
108 private:
109   /// A basic block-level pass to find all pointer dereferences and
110   /// validate them before use.
111 
112   /// The top-level pass implementation
113   ///
114   /// \param[in] M
115   ///     The module currently being processed.
116   ///
117   /// \param[in] BB
118   ///     The basic block currently being processed.
119   ///
120   /// \return
121   ///     True on success; false otherwise
122   bool FindDataLoads(llvm::Module &M, llvm::BasicBlock &BB);
123 
124   std::string m_func_name; ///< The name of the function to add checks to
125   ClangDynamicCheckerFunctions
126       &m_checker_functions; ///< The checker functions for the process
127 };
128 
129 } // namespace lldb_private
130 
131 #endif // liblldb_IRDynamicChecks_h_
132