19f2f44ceSEd Maste //===-- IRDynamicChecks.cpp -------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
109f2f44ceSEd Maste // C Includes
119f2f44ceSEd Maste // C++ Includes
129f2f44ceSEd Maste // Other libraries and framework includes
13ac7ddfbfSEd Maste #include "llvm/Support/raw_ostream.h"
14ac7ddfbfSEd Maste #include "llvm/IR/Constants.h"
1512b93ac6SEd Maste #include "llvm/IR/DataLayout.h"
16ac7ddfbfSEd Maste #include "llvm/IR/Function.h"
17ac7ddfbfSEd Maste #include "llvm/IR/Instructions.h"
18ac7ddfbfSEd Maste #include "llvm/IR/Module.h"
19ac7ddfbfSEd Maste #include "llvm/IR/Value.h"
20ac7ddfbfSEd Maste 
219f2f44ceSEd Maste // Project includes
229f2f44ceSEd Maste #include "lldb/Expression/IRDynamicChecks.h"
239f2f44ceSEd Maste 
249f2f44ceSEd Maste #include "lldb/Core/ConstString.h"
259f2f44ceSEd Maste #include "lldb/Core/Log.h"
269f2f44ceSEd Maste #include "lldb/Expression/UtilityFunction.h"
279f2f44ceSEd Maste #include "lldb/Target/ExecutionContext.h"
289f2f44ceSEd Maste #include "lldb/Target/ObjCLanguageRuntime.h"
299f2f44ceSEd Maste #include "lldb/Target/Process.h"
309f2f44ceSEd Maste #include "lldb/Target/StackFrame.h"
319f2f44ceSEd Maste #include "lldb/Target/Target.h"
329f2f44ceSEd Maste 
33ac7ddfbfSEd Maste using namespace llvm;
34ac7ddfbfSEd Maste using namespace lldb_private;
35ac7ddfbfSEd Maste 
36ac7ddfbfSEd Maste static char ID;
37ac7ddfbfSEd Maste 
38a8bcc4d8SDimitry Andric #define VALID_POINTER_CHECK_NAME "_$__lldb_valid_pointer_check"
39ac7ddfbfSEd Maste #define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check"
40ac7ddfbfSEd Maste 
41ac7ddfbfSEd Maste static const char g_valid_pointer_check_text[] =
42ac7ddfbfSEd Maste "extern \"C\" void\n"
43a8bcc4d8SDimitry Andric "_$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n"
44ac7ddfbfSEd Maste "{\n"
45ac7ddfbfSEd Maste "    unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
46ac7ddfbfSEd Maste "}";
47ac7ddfbfSEd Maste 
489f2f44ceSEd Maste DynamicCheckerFunctions::DynamicCheckerFunctions() = default;
49ac7ddfbfSEd Maste 
509f2f44ceSEd Maste DynamicCheckerFunctions::~DynamicCheckerFunctions() = default;
51ac7ddfbfSEd Maste 
52ac7ddfbfSEd Maste bool
534bb0738eSEd Maste DynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx)
54ac7ddfbfSEd Maste {
559f2f44ceSEd Maste     Error error;
569f2f44ceSEd Maste     m_valid_pointer_check.reset(exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(g_valid_pointer_check_text,
579f2f44ceSEd Maste                                                                                      lldb::eLanguageTypeC,
589f2f44ceSEd Maste                                                                                      VALID_POINTER_CHECK_NAME,
599f2f44ceSEd Maste                                                                                      error));
609f2f44ceSEd Maste     if (error.Fail())
619f2f44ceSEd Maste         return false;
629f2f44ceSEd Maste 
634bb0738eSEd Maste     if (!m_valid_pointer_check->Install(diagnostic_manager, exe_ctx))
64ac7ddfbfSEd Maste         return false;
65ac7ddfbfSEd Maste 
66ac7ddfbfSEd Maste     Process *process = exe_ctx.GetProcessPtr();
67ac7ddfbfSEd Maste 
68ac7ddfbfSEd Maste     if (process)
69ac7ddfbfSEd Maste     {
70ac7ddfbfSEd Maste         ObjCLanguageRuntime *objc_language_runtime = process->GetObjCLanguageRuntime();
71ac7ddfbfSEd Maste 
72ac7ddfbfSEd Maste         if (objc_language_runtime)
73ac7ddfbfSEd Maste         {
74ac7ddfbfSEd Maste             m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker(VALID_OBJC_OBJECT_CHECK_NAME));
75ac7ddfbfSEd Maste 
764bb0738eSEd Maste             if (!m_objc_object_check->Install(diagnostic_manager, exe_ctx))
77ac7ddfbfSEd Maste                 return false;
78ac7ddfbfSEd Maste         }
79ac7ddfbfSEd Maste     }
80ac7ddfbfSEd Maste 
81ac7ddfbfSEd Maste     return true;
82ac7ddfbfSEd Maste }
83ac7ddfbfSEd Maste 
84ac7ddfbfSEd Maste bool
85ac7ddfbfSEd Maste DynamicCheckerFunctions::DoCheckersExplainStop (lldb::addr_t addr, Stream &message)
86ac7ddfbfSEd Maste {
87ac7ddfbfSEd Maste     // FIXME: We have to get the checkers to know why they scotched the call in more detail,
88ac7ddfbfSEd Maste     // so we can print a better message here.
899f2f44ceSEd Maste     if (m_valid_pointer_check && m_valid_pointer_check->ContainsAddress(addr))
90ac7ddfbfSEd Maste     {
91ac7ddfbfSEd Maste         message.Printf ("Attempted to dereference an invalid pointer.");
92ac7ddfbfSEd Maste         return true;
93ac7ddfbfSEd Maste     }
949f2f44ceSEd Maste     else if (m_objc_object_check && m_objc_object_check->ContainsAddress(addr))
95ac7ddfbfSEd Maste     {
96ac7ddfbfSEd Maste         message.Printf ("Attempted to dereference an invalid ObjC Object or send it an unrecognized selector");
97ac7ddfbfSEd Maste         return true;
98ac7ddfbfSEd Maste     }
99ac7ddfbfSEd Maste     return false;
100ac7ddfbfSEd Maste }
101ac7ddfbfSEd Maste 
102ac7ddfbfSEd Maste static std::string
103ac7ddfbfSEd Maste PrintValue(llvm::Value *V, bool truncate = false)
104ac7ddfbfSEd Maste {
105ac7ddfbfSEd Maste     std::string s;
106ac7ddfbfSEd Maste     raw_string_ostream rso(s);
107ac7ddfbfSEd Maste     V->print(rso);
108ac7ddfbfSEd Maste     rso.flush();
109ac7ddfbfSEd Maste     if (truncate)
110ac7ddfbfSEd Maste         s.resize(s.length() - 1);
111ac7ddfbfSEd Maste     return s;
112ac7ddfbfSEd Maste }
113ac7ddfbfSEd Maste 
114ac7ddfbfSEd Maste //----------------------------------------------------------------------
115ac7ddfbfSEd Maste /// @class Instrumenter IRDynamicChecks.cpp
116ac7ddfbfSEd Maste /// @brief Finds and instruments individual LLVM IR instructions
117ac7ddfbfSEd Maste ///
118ac7ddfbfSEd Maste /// When instrumenting LLVM IR, it is frequently desirable to first search
119ac7ddfbfSEd Maste /// for instructions, and then later modify them.  This way iterators
120ac7ddfbfSEd Maste /// remain intact, and multiple passes can look at the same code base without
121ac7ddfbfSEd Maste /// treading on each other's toes.
122ac7ddfbfSEd Maste ///
123ac7ddfbfSEd Maste /// The Instrumenter class implements this functionality.  A client first
124ac7ddfbfSEd Maste /// calls Inspect on a function, which populates a list of instructions to
125ac7ddfbfSEd Maste /// be instrumented.  Then, later, when all passes' Inspect functions have
126ac7ddfbfSEd Maste /// been called, the client calls Instrument, which adds the desired
127ac7ddfbfSEd Maste /// instrumentation.
128ac7ddfbfSEd Maste ///
129ac7ddfbfSEd Maste /// A subclass of Instrumenter must override InstrumentInstruction, which
130ac7ddfbfSEd Maste /// is responsible for adding whatever instrumentation is necessary.
131ac7ddfbfSEd Maste ///
132ac7ddfbfSEd Maste /// A subclass of Instrumenter may override:
133ac7ddfbfSEd Maste ///
134ac7ddfbfSEd Maste /// - InspectInstruction [default: does nothing]
135ac7ddfbfSEd Maste ///
136ac7ddfbfSEd Maste /// - InspectBasicBlock [default: iterates through the instructions in a
137ac7ddfbfSEd Maste ///   basic block calling InspectInstruction]
138ac7ddfbfSEd Maste ///
139ac7ddfbfSEd Maste /// - InspectFunction [default: iterates through the basic blocks in a
140ac7ddfbfSEd Maste ///   function calling InspectBasicBlock]
141ac7ddfbfSEd Maste //----------------------------------------------------------------------
142ac7ddfbfSEd Maste class Instrumenter {
143ac7ddfbfSEd Maste public:
144ac7ddfbfSEd Maste     //------------------------------------------------------------------
145ac7ddfbfSEd Maste     /// Constructor
146ac7ddfbfSEd Maste     ///
147ac7ddfbfSEd Maste     /// @param[in] module
148ac7ddfbfSEd Maste     ///     The module being instrumented.
149ac7ddfbfSEd Maste     //------------------------------------------------------------------
150ac7ddfbfSEd Maste     Instrumenter (llvm::Module &module,
151ac7ddfbfSEd Maste                   DynamicCheckerFunctions &checker_functions) :
152ac7ddfbfSEd Maste         m_module(module),
153ac7ddfbfSEd Maste         m_checker_functions(checker_functions),
1549f2f44ceSEd Maste         m_i8ptr_ty(nullptr),
1559f2f44ceSEd Maste         m_intptr_ty(nullptr)
156ac7ddfbfSEd Maste     {
157ac7ddfbfSEd Maste     }
158ac7ddfbfSEd Maste 
1599f2f44ceSEd Maste     virtual ~Instrumenter() = default;
160ac7ddfbfSEd Maste 
161ac7ddfbfSEd Maste     //------------------------------------------------------------------
162ac7ddfbfSEd Maste     /// Inspect a function to find instructions to instrument
163ac7ddfbfSEd Maste     ///
164ac7ddfbfSEd Maste     /// @param[in] function
165ac7ddfbfSEd Maste     ///     The function to inspect.
166ac7ddfbfSEd Maste     ///
167ac7ddfbfSEd Maste     /// @return
168ac7ddfbfSEd Maste     ///     True on success; false on error.
169ac7ddfbfSEd Maste     //------------------------------------------------------------------
170ac7ddfbfSEd Maste     bool Inspect (llvm::Function &function)
171ac7ddfbfSEd Maste     {
172ac7ddfbfSEd Maste         return InspectFunction(function);
173ac7ddfbfSEd Maste     }
174ac7ddfbfSEd Maste 
175ac7ddfbfSEd Maste     //------------------------------------------------------------------
176ac7ddfbfSEd Maste     /// Instrument all the instructions found by Inspect()
177ac7ddfbfSEd Maste     ///
178ac7ddfbfSEd Maste     /// @return
179ac7ddfbfSEd Maste     ///     True on success; false on error.
180ac7ddfbfSEd Maste     //------------------------------------------------------------------
181ac7ddfbfSEd Maste     bool Instrument ()
182ac7ddfbfSEd Maste     {
183ac7ddfbfSEd Maste         for (InstIterator ii = m_to_instrument.begin(), last_ii = m_to_instrument.end();
184ac7ddfbfSEd Maste              ii != last_ii;
185ac7ddfbfSEd Maste              ++ii)
186ac7ddfbfSEd Maste         {
187ac7ddfbfSEd Maste             if (!InstrumentInstruction(*ii))
188ac7ddfbfSEd Maste                 return false;
189ac7ddfbfSEd Maste         }
190ac7ddfbfSEd Maste 
191ac7ddfbfSEd Maste         return true;
192ac7ddfbfSEd Maste     }
1939f2f44ceSEd Maste 
194ac7ddfbfSEd Maste protected:
195ac7ddfbfSEd Maste     //------------------------------------------------------------------
196ac7ddfbfSEd Maste     /// Add instrumentation to a single instruction
197ac7ddfbfSEd Maste     ///
198ac7ddfbfSEd Maste     /// @param[in] inst
199ac7ddfbfSEd Maste     ///     The instruction to be instrumented.
200ac7ddfbfSEd Maste     ///
201ac7ddfbfSEd Maste     /// @return
202ac7ddfbfSEd Maste     ///     True on success; false otherwise.
203ac7ddfbfSEd Maste     //------------------------------------------------------------------
204ac7ddfbfSEd Maste     virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0;
205ac7ddfbfSEd Maste 
206ac7ddfbfSEd Maste     //------------------------------------------------------------------
207ac7ddfbfSEd Maste     /// Register a single instruction to be instrumented
208ac7ddfbfSEd Maste     ///
209ac7ddfbfSEd Maste     /// @param[in] inst
210ac7ddfbfSEd Maste     ///     The instruction to be instrumented.
211ac7ddfbfSEd Maste     //------------------------------------------------------------------
212ac7ddfbfSEd Maste     void RegisterInstruction(llvm::Instruction &i)
213ac7ddfbfSEd Maste     {
214ac7ddfbfSEd Maste         m_to_instrument.push_back(&i);
215ac7ddfbfSEd Maste     }
216ac7ddfbfSEd Maste 
217ac7ddfbfSEd Maste     //------------------------------------------------------------------
218ac7ddfbfSEd Maste     /// Determine whether a single instruction is interesting to
219ac7ddfbfSEd Maste     /// instrument, and, if so, call RegisterInstruction
220ac7ddfbfSEd Maste     ///
221ac7ddfbfSEd Maste     /// @param[in] i
222ac7ddfbfSEd Maste     ///     The instruction to be inspected.
223ac7ddfbfSEd Maste     ///
224ac7ddfbfSEd Maste     /// @return
225ac7ddfbfSEd Maste     ///     False if there was an error scanning; true otherwise.
226ac7ddfbfSEd Maste     //------------------------------------------------------------------
227ac7ddfbfSEd Maste     virtual bool InspectInstruction(llvm::Instruction &i)
228ac7ddfbfSEd Maste     {
229ac7ddfbfSEd Maste         return true;
230ac7ddfbfSEd Maste     }
231ac7ddfbfSEd Maste 
232ac7ddfbfSEd Maste     //------------------------------------------------------------------
233ac7ddfbfSEd Maste     /// Scan a basic block to see if any instructions are interesting
234ac7ddfbfSEd Maste     ///
235ac7ddfbfSEd Maste     /// @param[in] bb
236ac7ddfbfSEd Maste     ///     The basic block to be inspected.
237ac7ddfbfSEd Maste     ///
238ac7ddfbfSEd Maste     /// @return
239ac7ddfbfSEd Maste     ///     False if there was an error scanning; true otherwise.
240ac7ddfbfSEd Maste     //------------------------------------------------------------------
241ac7ddfbfSEd Maste     virtual bool InspectBasicBlock(llvm::BasicBlock &bb)
242ac7ddfbfSEd Maste     {
243ac7ddfbfSEd Maste         for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end();
244ac7ddfbfSEd Maste              ii != last_ii;
245ac7ddfbfSEd Maste              ++ii)
246ac7ddfbfSEd Maste         {
247ac7ddfbfSEd Maste             if (!InspectInstruction(*ii))
248ac7ddfbfSEd Maste                 return false;
249ac7ddfbfSEd Maste         }
250ac7ddfbfSEd Maste 
251ac7ddfbfSEd Maste         return true;
252ac7ddfbfSEd Maste     }
253ac7ddfbfSEd Maste 
254ac7ddfbfSEd Maste     //------------------------------------------------------------------
255ac7ddfbfSEd Maste     /// Scan a function to see if any instructions are interesting
256ac7ddfbfSEd Maste     ///
257ac7ddfbfSEd Maste     /// @param[in] f
258ac7ddfbfSEd Maste     ///     The function to be inspected.
259ac7ddfbfSEd Maste     ///
260ac7ddfbfSEd Maste     /// @return
261ac7ddfbfSEd Maste     ///     False if there was an error scanning; true otherwise.
262ac7ddfbfSEd Maste     //------------------------------------------------------------------
263ac7ddfbfSEd Maste     virtual bool InspectFunction(llvm::Function &f)
264ac7ddfbfSEd Maste     {
265ac7ddfbfSEd Maste         for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end();
266ac7ddfbfSEd Maste              bbi != last_bbi;
267ac7ddfbfSEd Maste              ++bbi)
268ac7ddfbfSEd Maste         {
269ac7ddfbfSEd Maste             if (!InspectBasicBlock(*bbi))
270ac7ddfbfSEd Maste                 return false;
271ac7ddfbfSEd Maste         }
272ac7ddfbfSEd Maste 
273ac7ddfbfSEd Maste         return true;
274ac7ddfbfSEd Maste     }
275ac7ddfbfSEd Maste 
276ac7ddfbfSEd Maste     //------------------------------------------------------------------
277ac7ddfbfSEd Maste     /// Build a function pointer for a function with signature
278ac7ddfbfSEd Maste     /// void (*)(uint8_t*) with a given address
279ac7ddfbfSEd Maste     ///
280ac7ddfbfSEd Maste     /// @param[in] start_address
281ac7ddfbfSEd Maste     ///     The address of the function.
282ac7ddfbfSEd Maste     ///
283ac7ddfbfSEd Maste     /// @return
284ac7ddfbfSEd Maste     ///     The function pointer, for use in a CallInst.
285ac7ddfbfSEd Maste     //------------------------------------------------------------------
286ac7ddfbfSEd Maste     llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address)
287ac7ddfbfSEd Maste     {
288ac7ddfbfSEd Maste         llvm::Type *param_array[1];
289ac7ddfbfSEd Maste 
290ac7ddfbfSEd Maste         param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
291ac7ddfbfSEd Maste 
292ac7ddfbfSEd Maste         ArrayRef<llvm::Type*> params(param_array, 1);
293ac7ddfbfSEd Maste 
294ac7ddfbfSEd Maste         FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
295ac7ddfbfSEd Maste         PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
29612b93ac6SEd Maste         Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false);
297ac7ddfbfSEd Maste         return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
298ac7ddfbfSEd Maste     }
299ac7ddfbfSEd Maste 
300ac7ddfbfSEd Maste     //------------------------------------------------------------------
301ac7ddfbfSEd Maste     /// Build a function pointer for a function with signature
302ac7ddfbfSEd Maste     /// void (*)(uint8_t*, uint8_t*) with a given address
303ac7ddfbfSEd Maste     ///
304ac7ddfbfSEd Maste     /// @param[in] start_address
305ac7ddfbfSEd Maste     ///     The address of the function.
306ac7ddfbfSEd Maste     ///
307ac7ddfbfSEd Maste     /// @return
308ac7ddfbfSEd Maste     ///     The function pointer, for use in a CallInst.
309ac7ddfbfSEd Maste     //------------------------------------------------------------------
310ac7ddfbfSEd Maste     llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address)
311ac7ddfbfSEd Maste     {
312ac7ddfbfSEd Maste         llvm::Type *param_array[2];
313ac7ddfbfSEd Maste 
314ac7ddfbfSEd Maste         param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
315ac7ddfbfSEd Maste         param_array[1] = const_cast<llvm::PointerType*>(GetI8PtrTy());
316ac7ddfbfSEd Maste 
317ac7ddfbfSEd Maste         ArrayRef<llvm::Type*> params(param_array, 2);
318ac7ddfbfSEd Maste 
319ac7ddfbfSEd Maste         FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
320ac7ddfbfSEd Maste         PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
32112b93ac6SEd Maste         Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false);
322ac7ddfbfSEd Maste         return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
323ac7ddfbfSEd Maste     }
324ac7ddfbfSEd Maste 
325ac7ddfbfSEd Maste     PointerType *GetI8PtrTy()
326ac7ddfbfSEd Maste     {
327ac7ddfbfSEd Maste         if (!m_i8ptr_ty)
328ac7ddfbfSEd Maste             m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext());
329ac7ddfbfSEd Maste 
330ac7ddfbfSEd Maste         return m_i8ptr_ty;
331ac7ddfbfSEd Maste     }
332ac7ddfbfSEd Maste 
33312b93ac6SEd Maste     IntegerType *GetIntptrTy()
33412b93ac6SEd Maste     {
33512b93ac6SEd Maste         if (!m_intptr_ty)
33612b93ac6SEd Maste         {
33712b93ac6SEd Maste             llvm::DataLayout data_layout(&m_module);
33812b93ac6SEd Maste 
33912b93ac6SEd Maste             m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), data_layout.getPointerSizeInBits());
34012b93ac6SEd Maste         }
34112b93ac6SEd Maste 
34212b93ac6SEd Maste         return m_intptr_ty;
34312b93ac6SEd Maste     }
34412b93ac6SEd Maste 
345ac7ddfbfSEd Maste     typedef std::vector <llvm::Instruction *>   InstVector;
346ac7ddfbfSEd Maste     typedef InstVector::iterator                InstIterator;
347ac7ddfbfSEd Maste 
348ac7ddfbfSEd Maste     InstVector                  m_to_instrument;        ///< List of instructions the inspector found
349ac7ddfbfSEd Maste     llvm::Module               &m_module;               ///< The module which is being instrumented
350ac7ddfbfSEd Maste     DynamicCheckerFunctions    &m_checker_functions;    ///< The dynamic checker functions for the process
3519f2f44ceSEd Maste 
352ac7ddfbfSEd Maste private:
353ac7ddfbfSEd Maste     PointerType                *m_i8ptr_ty;
35412b93ac6SEd Maste     IntegerType                *m_intptr_ty;
355ac7ddfbfSEd Maste };
356ac7ddfbfSEd Maste 
357ac7ddfbfSEd Maste class ValidPointerChecker : public Instrumenter
358ac7ddfbfSEd Maste {
359ac7ddfbfSEd Maste public:
360ac7ddfbfSEd Maste     ValidPointerChecker (llvm::Module &module,
361ac7ddfbfSEd Maste                          DynamicCheckerFunctions &checker_functions) :
362ac7ddfbfSEd Maste         Instrumenter(module, checker_functions),
3639f2f44ceSEd Maste         m_valid_pointer_check_func(nullptr)
364ac7ddfbfSEd Maste     {
365ac7ddfbfSEd Maste     }
366ac7ddfbfSEd Maste 
3679f2f44ceSEd Maste     ~ValidPointerChecker() override = default;
3689f2f44ceSEd Maste 
3699f2f44ceSEd Maste protected:
3709f2f44ceSEd Maste     bool InstrumentInstruction(llvm::Instruction *inst) override
371ac7ddfbfSEd Maste     {
372ac7ddfbfSEd Maste         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
373ac7ddfbfSEd Maste 
374ac7ddfbfSEd Maste         if (log)
375ac7ddfbfSEd Maste             log->Printf("Instrumenting load/store instruction: %s\n",
376ac7ddfbfSEd Maste                         PrintValue(inst).c_str());
377ac7ddfbfSEd Maste 
378ac7ddfbfSEd Maste         if (!m_valid_pointer_check_func)
379ac7ddfbfSEd Maste             m_valid_pointer_check_func = BuildPointerValidatorFunc(m_checker_functions.m_valid_pointer_check->StartAddress());
380ac7ddfbfSEd Maste 
3819f2f44ceSEd Maste         llvm::Value *dereferenced_ptr = nullptr;
382ac7ddfbfSEd Maste 
383ac7ddfbfSEd Maste         if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst> (inst))
384ac7ddfbfSEd Maste             dereferenced_ptr = li->getPointerOperand();
385ac7ddfbfSEd Maste         else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst> (inst))
386ac7ddfbfSEd Maste             dereferenced_ptr = si->getPointerOperand();
387ac7ddfbfSEd Maste         else
388ac7ddfbfSEd Maste             return false;
389ac7ddfbfSEd Maste 
390ac7ddfbfSEd Maste         // Insert an instruction to cast the loaded value to int8_t*
391ac7ddfbfSEd Maste 
392ac7ddfbfSEd Maste         BitCastInst *bit_cast = new BitCastInst(dereferenced_ptr,
393ac7ddfbfSEd Maste                                                 GetI8PtrTy(),
394ac7ddfbfSEd Maste                                                 "",
395ac7ddfbfSEd Maste                                                 inst);
396ac7ddfbfSEd Maste 
397ac7ddfbfSEd Maste         // Insert an instruction to call the helper with the result
398ac7ddfbfSEd Maste 
399ac7ddfbfSEd Maste         llvm::Value *arg_array[1];
400ac7ddfbfSEd Maste 
401ac7ddfbfSEd Maste         arg_array[0] = bit_cast;
402ac7ddfbfSEd Maste 
403ac7ddfbfSEd Maste         llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
404ac7ddfbfSEd Maste 
405ac7ddfbfSEd Maste         CallInst::Create(m_valid_pointer_check_func,
406ac7ddfbfSEd Maste                          args,
407ac7ddfbfSEd Maste                          "",
408ac7ddfbfSEd Maste                          inst);
409ac7ddfbfSEd Maste 
410ac7ddfbfSEd Maste         return true;
411ac7ddfbfSEd Maste     }
412ac7ddfbfSEd Maste 
4139f2f44ceSEd Maste     bool InspectInstruction(llvm::Instruction &i) override
414ac7ddfbfSEd Maste     {
415ac7ddfbfSEd Maste         if (dyn_cast<llvm::LoadInst> (&i) ||
416ac7ddfbfSEd Maste             dyn_cast<llvm::StoreInst> (&i))
417ac7ddfbfSEd Maste             RegisterInstruction(i);
418ac7ddfbfSEd Maste 
419ac7ddfbfSEd Maste         return true;
420ac7ddfbfSEd Maste     }
421ac7ddfbfSEd Maste 
4229f2f44ceSEd Maste private:
423ac7ddfbfSEd Maste     llvm::Value         *m_valid_pointer_check_func;
424ac7ddfbfSEd Maste };
425ac7ddfbfSEd Maste 
426ac7ddfbfSEd Maste class ObjcObjectChecker : public Instrumenter
427ac7ddfbfSEd Maste {
428ac7ddfbfSEd Maste public:
429ac7ddfbfSEd Maste     ObjcObjectChecker(llvm::Module &module,
430ac7ddfbfSEd Maste                         DynamicCheckerFunctions &checker_functions) :
431ac7ddfbfSEd Maste         Instrumenter(module, checker_functions),
4329f2f44ceSEd Maste         m_objc_object_check_func(nullptr)
433ac7ddfbfSEd Maste     {
434ac7ddfbfSEd Maste     }
435ac7ddfbfSEd Maste 
4369f2f44ceSEd Maste     ~ObjcObjectChecker() override = default;
437ac7ddfbfSEd Maste 
438ac7ddfbfSEd Maste     enum msgSend_type
439ac7ddfbfSEd Maste     {
440ac7ddfbfSEd Maste         eMsgSend = 0,
441ac7ddfbfSEd Maste         eMsgSendSuper,
442ac7ddfbfSEd Maste         eMsgSendSuper_stret,
443ac7ddfbfSEd Maste         eMsgSend_fpret,
444ac7ddfbfSEd Maste         eMsgSend_stret
445ac7ddfbfSEd Maste     };
446ac7ddfbfSEd Maste 
447ac7ddfbfSEd Maste     std::map <llvm::Instruction *, msgSend_type> msgSend_types;
448ac7ddfbfSEd Maste 
4499f2f44ceSEd Maste protected:
4509f2f44ceSEd Maste     bool InstrumentInstruction(llvm::Instruction *inst) override
451ac7ddfbfSEd Maste     {
452ac7ddfbfSEd Maste         CallInst *call_inst = dyn_cast<CallInst>(inst);
453ac7ddfbfSEd Maste 
454ac7ddfbfSEd Maste         if (!call_inst)
4559f2f44ceSEd Maste             return false; // call_inst really shouldn't be nullptr, because otherwise InspectInstruction wouldn't have registered it
456ac7ddfbfSEd Maste 
457ac7ddfbfSEd Maste         if (!m_objc_object_check_func)
458ac7ddfbfSEd Maste             m_objc_object_check_func = BuildObjectCheckerFunc(m_checker_functions.m_objc_object_check->StartAddress());
459ac7ddfbfSEd Maste 
460ac7ddfbfSEd Maste         // id objc_msgSend(id theReceiver, SEL theSelector, ...)
461ac7ddfbfSEd Maste 
462ac7ddfbfSEd Maste         llvm::Value *target_object;
463ac7ddfbfSEd Maste         llvm::Value *selector;
464ac7ddfbfSEd Maste 
465ac7ddfbfSEd Maste         switch (msgSend_types[inst])
466ac7ddfbfSEd Maste         {
467ac7ddfbfSEd Maste         case eMsgSend:
468ac7ddfbfSEd Maste         case eMsgSend_fpret:
469ac7ddfbfSEd Maste             target_object = call_inst->getArgOperand(0);
470ac7ddfbfSEd Maste             selector = call_inst->getArgOperand(1);
471ac7ddfbfSEd Maste             break;
472ac7ddfbfSEd Maste         case eMsgSend_stret:
473ac7ddfbfSEd Maste             target_object = call_inst->getArgOperand(1);
474ac7ddfbfSEd Maste             selector = call_inst->getArgOperand(2);
475ac7ddfbfSEd Maste             break;
476ac7ddfbfSEd Maste         case eMsgSendSuper:
477ac7ddfbfSEd Maste         case eMsgSendSuper_stret:
478ac7ddfbfSEd Maste             return true;
479ac7ddfbfSEd Maste         }
480ac7ddfbfSEd Maste 
481ac7ddfbfSEd Maste         // These objects should always be valid according to Sean Calannan
482ac7ddfbfSEd Maste         assert (target_object);
483ac7ddfbfSEd Maste         assert (selector);
484ac7ddfbfSEd Maste 
485ac7ddfbfSEd Maste         // Insert an instruction to cast the receiver id to int8_t*
486ac7ddfbfSEd Maste 
487ac7ddfbfSEd Maste         BitCastInst *bit_cast = new BitCastInst(target_object,
488ac7ddfbfSEd Maste                                                 GetI8PtrTy(),
489ac7ddfbfSEd Maste                                                 "",
490ac7ddfbfSEd Maste                                                 inst);
491ac7ddfbfSEd Maste 
492ac7ddfbfSEd Maste         // Insert an instruction to call the helper with the result
493ac7ddfbfSEd Maste 
494ac7ddfbfSEd Maste         llvm::Value *arg_array[2];
495ac7ddfbfSEd Maste 
496ac7ddfbfSEd Maste         arg_array[0] = bit_cast;
497ac7ddfbfSEd Maste         arg_array[1] = selector;
498ac7ddfbfSEd Maste 
499ac7ddfbfSEd Maste         ArrayRef<llvm::Value*> args(arg_array, 2);
500ac7ddfbfSEd Maste 
501ac7ddfbfSEd Maste         CallInst::Create(m_objc_object_check_func,
502ac7ddfbfSEd Maste                          args,
503ac7ddfbfSEd Maste                          "",
504ac7ddfbfSEd Maste                          inst);
505ac7ddfbfSEd Maste 
506ac7ddfbfSEd Maste         return true;
507ac7ddfbfSEd Maste     }
508ac7ddfbfSEd Maste 
5094bb0738eSEd Maste     static llvm::Function *GetFunction(llvm::Value *value)
5104bb0738eSEd Maste     {
5114bb0738eSEd Maste         if (llvm::Function *function = llvm::dyn_cast<llvm::Function>(value))
5124bb0738eSEd Maste         {
5134bb0738eSEd Maste             return function;
5144bb0738eSEd Maste         }
5154bb0738eSEd Maste 
5164bb0738eSEd Maste         if (llvm::ConstantExpr *const_expr = llvm::dyn_cast<llvm::ConstantExpr>(value))
5174bb0738eSEd Maste         {
5184bb0738eSEd Maste             switch (const_expr->getOpcode())
5194bb0738eSEd Maste             {
5204bb0738eSEd Maste             default:
5214bb0738eSEd Maste                 return nullptr;
5224bb0738eSEd Maste             case llvm::Instruction::BitCast:
5234bb0738eSEd Maste                 return GetFunction(const_expr->getOperand(0));
5244bb0738eSEd Maste             }
5254bb0738eSEd Maste         }
5264bb0738eSEd Maste 
5274bb0738eSEd Maste         return nullptr;
5284bb0738eSEd Maste     }
5294bb0738eSEd Maste 
5304bb0738eSEd Maste     static llvm::Function *GetCalledFunction(llvm::CallInst *inst)
5314bb0738eSEd Maste     {
5324bb0738eSEd Maste         return GetFunction(inst->getCalledValue());
5334bb0738eSEd Maste     }
5344bb0738eSEd Maste 
5359f2f44ceSEd Maste     bool InspectInstruction(llvm::Instruction &i) override
536ac7ddfbfSEd Maste     {
537ac7ddfbfSEd Maste         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
538ac7ddfbfSEd Maste 
539ac7ddfbfSEd Maste         CallInst *call_inst = dyn_cast<CallInst>(&i);
540ac7ddfbfSEd Maste 
541ac7ddfbfSEd Maste         if (call_inst)
542ac7ddfbfSEd Maste         {
5434bb0738eSEd Maste             const llvm::Function *called_function = GetCalledFunction(call_inst);
544ac7ddfbfSEd Maste 
5454bb0738eSEd Maste             if (!called_function)
546ac7ddfbfSEd Maste                 return true;
547ac7ddfbfSEd Maste 
5484bb0738eSEd Maste             std::string name_str = called_function->getName().str();
549ac7ddfbfSEd Maste             const char* name_cstr = name_str.c_str();
550ac7ddfbfSEd Maste 
551ac7ddfbfSEd Maste             if (log)
552ac7ddfbfSEd Maste                 log->Printf("Found call to %s: %s\n", name_cstr, PrintValue(call_inst).c_str());
553ac7ddfbfSEd Maste 
554ac7ddfbfSEd Maste             if (name_str.find("objc_msgSend") == std::string::npos)
555ac7ddfbfSEd Maste                 return true;
556ac7ddfbfSEd Maste 
557ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSend"))
558ac7ddfbfSEd Maste             {
559ac7ddfbfSEd Maste                 RegisterInstruction(i);
560ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSend;
561ac7ddfbfSEd Maste                 return true;
562ac7ddfbfSEd Maste             }
563ac7ddfbfSEd Maste 
564ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSend_stret"))
565ac7ddfbfSEd Maste             {
566ac7ddfbfSEd Maste                 RegisterInstruction(i);
567ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSend_stret;
568ac7ddfbfSEd Maste                 return true;
569ac7ddfbfSEd Maste             }
570ac7ddfbfSEd Maste 
571ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSend_fpret"))
572ac7ddfbfSEd Maste             {
573ac7ddfbfSEd Maste                 RegisterInstruction(i);
574ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSend_fpret;
575ac7ddfbfSEd Maste                 return true;
576ac7ddfbfSEd Maste             }
577ac7ddfbfSEd Maste 
578ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSendSuper"))
579ac7ddfbfSEd Maste             {
580ac7ddfbfSEd Maste                 RegisterInstruction(i);
581ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSendSuper;
582ac7ddfbfSEd Maste                 return true;
583ac7ddfbfSEd Maste             }
584ac7ddfbfSEd Maste 
585ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSendSuper_stret"))
586ac7ddfbfSEd Maste             {
587ac7ddfbfSEd Maste                 RegisterInstruction(i);
588ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSendSuper_stret;
589ac7ddfbfSEd Maste                 return true;
590ac7ddfbfSEd Maste             }
591ac7ddfbfSEd Maste 
592ac7ddfbfSEd Maste             if (log)
593ac7ddfbfSEd Maste                 log->Printf("Function name '%s' contains 'objc_msgSend' but is not handled", name_str.c_str());
594ac7ddfbfSEd Maste 
595ac7ddfbfSEd Maste             return true;
596ac7ddfbfSEd Maste         }
597ac7ddfbfSEd Maste 
598ac7ddfbfSEd Maste         return true;
599ac7ddfbfSEd Maste     }
600ac7ddfbfSEd Maste 
6019f2f44ceSEd Maste private:
602ac7ddfbfSEd Maste     llvm::Value         *m_objc_object_check_func;
603ac7ddfbfSEd Maste };
604ac7ddfbfSEd Maste 
605ac7ddfbfSEd Maste IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
606ac7ddfbfSEd Maste                                  const char *func_name) :
607ac7ddfbfSEd Maste     ModulePass(ID),
608ac7ddfbfSEd Maste     m_func_name(func_name),
609ac7ddfbfSEd Maste     m_checker_functions(checker_functions)
610ac7ddfbfSEd Maste {
611ac7ddfbfSEd Maste }
612ac7ddfbfSEd Maste 
6139f2f44ceSEd Maste IRDynamicChecks::~IRDynamicChecks() = default;
614ac7ddfbfSEd Maste 
615ac7ddfbfSEd Maste bool
616ac7ddfbfSEd Maste IRDynamicChecks::runOnModule(llvm::Module &M)
617ac7ddfbfSEd Maste {
618ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
619ac7ddfbfSEd Maste 
620ac7ddfbfSEd Maste     llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str()));
621ac7ddfbfSEd Maste 
622ac7ddfbfSEd Maste     if (!function)
623ac7ddfbfSEd Maste     {
624ac7ddfbfSEd Maste         if (log)
625ac7ddfbfSEd Maste             log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
626ac7ddfbfSEd Maste 
627ac7ddfbfSEd Maste         return false;
628ac7ddfbfSEd Maste     }
629ac7ddfbfSEd Maste 
6309f2f44ceSEd Maste     if (m_checker_functions.m_valid_pointer_check)
631ac7ddfbfSEd Maste     {
632ac7ddfbfSEd Maste         ValidPointerChecker vpc(M, m_checker_functions);
633ac7ddfbfSEd Maste 
634ac7ddfbfSEd Maste         if (!vpc.Inspect(*function))
635ac7ddfbfSEd Maste             return false;
636ac7ddfbfSEd Maste 
637ac7ddfbfSEd Maste         if (!vpc.Instrument())
638ac7ddfbfSEd Maste             return false;
639ac7ddfbfSEd Maste     }
640ac7ddfbfSEd Maste 
6419f2f44ceSEd Maste     if (m_checker_functions.m_objc_object_check)
642ac7ddfbfSEd Maste     {
643ac7ddfbfSEd Maste         ObjcObjectChecker ooc(M, m_checker_functions);
644ac7ddfbfSEd Maste 
645ac7ddfbfSEd Maste         if (!ooc.Inspect(*function))
646ac7ddfbfSEd Maste             return false;
647ac7ddfbfSEd Maste 
648ac7ddfbfSEd Maste         if (!ooc.Instrument())
649ac7ddfbfSEd Maste             return false;
650ac7ddfbfSEd Maste     }
651ac7ddfbfSEd Maste 
652ac7ddfbfSEd Maste     if (log && log->GetVerbose())
653ac7ddfbfSEd Maste     {
654ac7ddfbfSEd Maste         std::string s;
655ac7ddfbfSEd Maste         raw_string_ostream oss(s);
656ac7ddfbfSEd Maste 
6579f2f44ceSEd Maste         M.print(oss, nullptr);
658ac7ddfbfSEd Maste 
659ac7ddfbfSEd Maste         oss.flush();
660ac7ddfbfSEd Maste 
661ac7ddfbfSEd Maste         log->Printf ("Module after dynamic checks: \n%s", s.c_str());
662ac7ddfbfSEd Maste     }
663ac7ddfbfSEd Maste 
664ac7ddfbfSEd Maste     return true;
665ac7ddfbfSEd Maste }
666ac7ddfbfSEd Maste 
667ac7ddfbfSEd Maste void
668ac7ddfbfSEd Maste IRDynamicChecks::assignPassManager(PMStack &PMS,
669ac7ddfbfSEd Maste                                    PassManagerType T)
670ac7ddfbfSEd Maste {
671ac7ddfbfSEd Maste }
672ac7ddfbfSEd Maste 
673ac7ddfbfSEd Maste PassManagerType
674ac7ddfbfSEd Maste IRDynamicChecks::getPotentialPassManagerType() const
675ac7ddfbfSEd Maste {
676ac7ddfbfSEd Maste     return PMT_ModulePassManager;
677ac7ddfbfSEd Maste }
678