xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_IRDYNAMICCHECKS_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_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   ~ClangDynamicCheckerFunctions() override;
34 
classof(const DynamicCheckerFunctions * checker_funcs)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   ///     Either llvm::ErrorSuccess or Error with llvm::ErrorInfo
50   ///
51   llvm::Error 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_objc_object_check;
57 };
58 
59 /// \class IRDynamicChecks IRDynamicChecks.h
60 /// "lldb/Expression/IRDynamicChecks.h" Adds dynamic checks to a user-entered
61 /// expression to reduce its likelihood of crashing
62 ///
63 /// When an IR function is executed in the target process, it may cause
64 /// crashes or hangs by dereferencing NULL pointers, trying to call
65 /// Objective-C methods on objects that do not respond to them, and so forth.
66 ///
67 /// IRDynamicChecks adds calls to the functions in DynamicCheckerFunctions to
68 /// appropriate locations in an expression's IR.
69 class IRDynamicChecks : public llvm::ModulePass {
70 public:
71   /// Constructor
72   ///
73   /// \param[in] checker_functions
74   ///     The checker functions for the target process.
75   ///
76   /// \param[in] func_name
77   ///     The name of the function to prepare for execution in the target.
78   IRDynamicChecks(ClangDynamicCheckerFunctions &checker_functions,
79                   const char *func_name = "$__lldb_expr");
80 
81   /// Destructor
82   ~IRDynamicChecks() override;
83 
84   /// Run this IR transformer on a single module
85   ///
86   /// \param[in] M
87   ///     The module to run on.  This module is searched for the function
88   ///     $__lldb_expr, and that function is passed to the passes one by
89   ///     one.
90   ///
91   /// \return
92   ///     True on success; false otherwise
93   bool runOnModule(llvm::Module &M) override;
94 
95   /// Interface stub
96   void assignPassManager(
97       llvm::PMStack &PMS,
98       llvm::PassManagerType T = llvm::PMT_ModulePassManager) override;
99 
100   /// Returns PMT_ModulePassManager
101   llvm::PassManagerType getPotentialPassManagerType() const override;
102 
103 private:
104   /// A basic block-level pass to find all pointer dereferences and
105   /// validate them before use.
106 
107   /// The top-level pass implementation
108   ///
109   /// \param[in] M
110   ///     The module currently being processed.
111   ///
112   /// \param[in] BB
113   ///     The basic block currently being processed.
114   ///
115   /// \return
116   ///     True on success; false otherwise
117   bool FindDataLoads(llvm::Module &M, llvm::BasicBlock &BB);
118 
119   std::string m_func_name; ///< The name of the function to add checks to
120   ClangDynamicCheckerFunctions
121       &m_checker_functions; ///< The checker functions for the process
122 };
123 
124 } // namespace lldb_private
125 
126 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_IRDYNAMICCHECKS_H
127