1ac7ddfbfSEd 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 
10ac7ddfbfSEd Maste #include "lldb/Expression/IRDynamicChecks.h"
11ac7ddfbfSEd Maste 
12ac7ddfbfSEd Maste #include "lldb/Core/ConstString.h"
13ac7ddfbfSEd Maste #include "lldb/Core/Log.h"
14ac7ddfbfSEd Maste #include "lldb/Expression/ClangUtilityFunction.h"
15ac7ddfbfSEd Maste #include "lldb/Target/ExecutionContext.h"
16ac7ddfbfSEd Maste #include "lldb/Target/ObjCLanguageRuntime.h"
17ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
18ac7ddfbfSEd Maste #include "lldb/Target/StackFrame.h"
19ac7ddfbfSEd Maste 
20ac7ddfbfSEd Maste #include "llvm/Support/raw_ostream.h"
21ac7ddfbfSEd Maste #include "llvm/IR/Constants.h"
22ac7ddfbfSEd Maste #include "llvm/IR/Function.h"
23ac7ddfbfSEd Maste #include "llvm/IR/Instructions.h"
24ac7ddfbfSEd Maste #include "llvm/IR/Module.h"
25ac7ddfbfSEd Maste #include "llvm/IR/Value.h"
26ac7ddfbfSEd Maste 
27ac7ddfbfSEd Maste using namespace llvm;
28ac7ddfbfSEd Maste using namespace lldb_private;
29ac7ddfbfSEd Maste 
30ac7ddfbfSEd Maste static char ID;
31ac7ddfbfSEd Maste 
32ac7ddfbfSEd Maste #define VALID_POINTER_CHECK_NAME "$__lldb_valid_pointer_check"
33ac7ddfbfSEd Maste #define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check"
34ac7ddfbfSEd Maste 
35ac7ddfbfSEd Maste static const char g_valid_pointer_check_text[] =
36ac7ddfbfSEd Maste "extern \"C\" void\n"
37ac7ddfbfSEd Maste "$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n"
38ac7ddfbfSEd Maste "{\n"
39ac7ddfbfSEd Maste "    unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
40ac7ddfbfSEd Maste "}";
41ac7ddfbfSEd Maste 
42ac7ddfbfSEd Maste DynamicCheckerFunctions::DynamicCheckerFunctions ()
43ac7ddfbfSEd Maste {
44ac7ddfbfSEd Maste }
45ac7ddfbfSEd Maste 
46ac7ddfbfSEd Maste DynamicCheckerFunctions::~DynamicCheckerFunctions ()
47ac7ddfbfSEd Maste {
48ac7ddfbfSEd Maste }
49ac7ddfbfSEd Maste 
50ac7ddfbfSEd Maste bool
51ac7ddfbfSEd Maste DynamicCheckerFunctions::Install(Stream &error_stream,
52ac7ddfbfSEd Maste                                  ExecutionContext &exe_ctx)
53ac7ddfbfSEd Maste {
54ac7ddfbfSEd Maste     m_valid_pointer_check.reset(new ClangUtilityFunction(g_valid_pointer_check_text,
55ac7ddfbfSEd Maste                                                          VALID_POINTER_CHECK_NAME));
56ac7ddfbfSEd Maste     if (!m_valid_pointer_check->Install(error_stream, exe_ctx))
57ac7ddfbfSEd Maste         return false;
58ac7ddfbfSEd Maste 
59ac7ddfbfSEd Maste     Process *process = exe_ctx.GetProcessPtr();
60ac7ddfbfSEd Maste 
61ac7ddfbfSEd Maste     if (process)
62ac7ddfbfSEd Maste     {
63ac7ddfbfSEd Maste         ObjCLanguageRuntime *objc_language_runtime = process->GetObjCLanguageRuntime();
64ac7ddfbfSEd Maste 
65ac7ddfbfSEd Maste         if (objc_language_runtime)
66ac7ddfbfSEd Maste         {
67ac7ddfbfSEd Maste             m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker(VALID_OBJC_OBJECT_CHECK_NAME));
68ac7ddfbfSEd Maste 
69ac7ddfbfSEd Maste             if (!m_objc_object_check->Install(error_stream, exe_ctx))
70ac7ddfbfSEd Maste                 return false;
71ac7ddfbfSEd Maste         }
72ac7ddfbfSEd Maste     }
73ac7ddfbfSEd Maste 
74ac7ddfbfSEd Maste     return true;
75ac7ddfbfSEd Maste }
76ac7ddfbfSEd Maste 
77ac7ddfbfSEd Maste bool
78ac7ddfbfSEd Maste DynamicCheckerFunctions::DoCheckersExplainStop (lldb::addr_t addr, Stream &message)
79ac7ddfbfSEd Maste {
80ac7ddfbfSEd Maste     // FIXME: We have to get the checkers to know why they scotched the call in more detail,
81ac7ddfbfSEd Maste     // so we can print a better message here.
82ac7ddfbfSEd Maste     if (m_valid_pointer_check.get() != NULL && m_valid_pointer_check->ContainsAddress(addr))
83ac7ddfbfSEd Maste     {
84ac7ddfbfSEd Maste         message.Printf ("Attempted to dereference an invalid pointer.");
85ac7ddfbfSEd Maste         return true;
86ac7ddfbfSEd Maste     }
87ac7ddfbfSEd Maste     else if (m_objc_object_check.get() != NULL && m_objc_object_check->ContainsAddress(addr))
88ac7ddfbfSEd Maste     {
89ac7ddfbfSEd Maste         message.Printf ("Attempted to dereference an invalid ObjC Object or send it an unrecognized selector");
90ac7ddfbfSEd Maste         return true;
91ac7ddfbfSEd Maste     }
92ac7ddfbfSEd Maste     return false;
93ac7ddfbfSEd Maste }
94ac7ddfbfSEd Maste 
95ac7ddfbfSEd Maste 
96ac7ddfbfSEd Maste static std::string
97ac7ddfbfSEd Maste PrintValue(llvm::Value *V, bool truncate = false)
98ac7ddfbfSEd Maste {
99ac7ddfbfSEd Maste     std::string s;
100ac7ddfbfSEd Maste     raw_string_ostream rso(s);
101ac7ddfbfSEd Maste     V->print(rso);
102ac7ddfbfSEd Maste     rso.flush();
103ac7ddfbfSEd Maste     if (truncate)
104ac7ddfbfSEd Maste         s.resize(s.length() - 1);
105ac7ddfbfSEd Maste     return s;
106ac7ddfbfSEd Maste }
107ac7ddfbfSEd Maste 
108ac7ddfbfSEd Maste //----------------------------------------------------------------------
109ac7ddfbfSEd Maste /// @class Instrumenter IRDynamicChecks.cpp
110ac7ddfbfSEd Maste /// @brief Finds and instruments individual LLVM IR instructions
111ac7ddfbfSEd Maste ///
112ac7ddfbfSEd Maste /// When instrumenting LLVM IR, it is frequently desirable to first search
113ac7ddfbfSEd Maste /// for instructions, and then later modify them.  This way iterators
114ac7ddfbfSEd Maste /// remain intact, and multiple passes can look at the same code base without
115ac7ddfbfSEd Maste /// treading on each other's toes.
116ac7ddfbfSEd Maste ///
117ac7ddfbfSEd Maste /// The Instrumenter class implements this functionality.  A client first
118ac7ddfbfSEd Maste /// calls Inspect on a function, which populates a list of instructions to
119ac7ddfbfSEd Maste /// be instrumented.  Then, later, when all passes' Inspect functions have
120ac7ddfbfSEd Maste /// been called, the client calls Instrument, which adds the desired
121ac7ddfbfSEd Maste /// instrumentation.
122ac7ddfbfSEd Maste ///
123ac7ddfbfSEd Maste /// A subclass of Instrumenter must override InstrumentInstruction, which
124ac7ddfbfSEd Maste /// is responsible for adding whatever instrumentation is necessary.
125ac7ddfbfSEd Maste ///
126ac7ddfbfSEd Maste /// A subclass of Instrumenter may override:
127ac7ddfbfSEd Maste ///
128ac7ddfbfSEd Maste /// - InspectInstruction [default: does nothing]
129ac7ddfbfSEd Maste ///
130ac7ddfbfSEd Maste /// - InspectBasicBlock [default: iterates through the instructions in a
131ac7ddfbfSEd Maste ///   basic block calling InspectInstruction]
132ac7ddfbfSEd Maste ///
133ac7ddfbfSEd Maste /// - InspectFunction [default: iterates through the basic blocks in a
134ac7ddfbfSEd Maste ///   function calling InspectBasicBlock]
135ac7ddfbfSEd Maste //----------------------------------------------------------------------
136ac7ddfbfSEd Maste class Instrumenter {
137ac7ddfbfSEd Maste public:
138ac7ddfbfSEd Maste     //------------------------------------------------------------------
139ac7ddfbfSEd Maste     /// Constructor
140ac7ddfbfSEd Maste     ///
141ac7ddfbfSEd Maste     /// @param[in] module
142ac7ddfbfSEd Maste     ///     The module being instrumented.
143ac7ddfbfSEd Maste     //------------------------------------------------------------------
144ac7ddfbfSEd Maste     Instrumenter (llvm::Module &module,
145ac7ddfbfSEd Maste                   DynamicCheckerFunctions &checker_functions) :
146ac7ddfbfSEd Maste         m_module(module),
147ac7ddfbfSEd Maste         m_checker_functions(checker_functions),
148ac7ddfbfSEd Maste         m_i8ptr_ty(NULL)
149ac7ddfbfSEd Maste     {
150ac7ddfbfSEd Maste     }
151ac7ddfbfSEd Maste 
152ac7ddfbfSEd Maste     virtual~Instrumenter ()
153ac7ddfbfSEd Maste     {
154ac7ddfbfSEd Maste     }
155ac7ddfbfSEd Maste 
156ac7ddfbfSEd Maste     //------------------------------------------------------------------
157ac7ddfbfSEd Maste     /// Inspect a function to find instructions to instrument
158ac7ddfbfSEd Maste     ///
159ac7ddfbfSEd Maste     /// @param[in] function
160ac7ddfbfSEd Maste     ///     The function to inspect.
161ac7ddfbfSEd Maste     ///
162ac7ddfbfSEd Maste     /// @return
163ac7ddfbfSEd Maste     ///     True on success; false on error.
164ac7ddfbfSEd Maste     //------------------------------------------------------------------
165ac7ddfbfSEd Maste     bool Inspect (llvm::Function &function)
166ac7ddfbfSEd Maste     {
167ac7ddfbfSEd Maste         return InspectFunction(function);
168ac7ddfbfSEd Maste     }
169ac7ddfbfSEd Maste 
170ac7ddfbfSEd Maste     //------------------------------------------------------------------
171ac7ddfbfSEd Maste     /// Instrument all the instructions found by Inspect()
172ac7ddfbfSEd Maste     ///
173ac7ddfbfSEd Maste     /// @return
174ac7ddfbfSEd Maste     ///     True on success; false on error.
175ac7ddfbfSEd Maste     //------------------------------------------------------------------
176ac7ddfbfSEd Maste     bool Instrument ()
177ac7ddfbfSEd Maste     {
178ac7ddfbfSEd Maste         for (InstIterator ii = m_to_instrument.begin(), last_ii = m_to_instrument.end();
179ac7ddfbfSEd Maste              ii != last_ii;
180ac7ddfbfSEd Maste              ++ii)
181ac7ddfbfSEd Maste         {
182ac7ddfbfSEd Maste             if (!InstrumentInstruction(*ii))
183ac7ddfbfSEd Maste                 return false;
184ac7ddfbfSEd Maste         }
185ac7ddfbfSEd Maste 
186ac7ddfbfSEd Maste         return true;
187ac7ddfbfSEd Maste     }
188ac7ddfbfSEd Maste protected:
189ac7ddfbfSEd Maste     //------------------------------------------------------------------
190ac7ddfbfSEd Maste     /// Add instrumentation to a single instruction
191ac7ddfbfSEd Maste     ///
192ac7ddfbfSEd Maste     /// @param[in] inst
193ac7ddfbfSEd Maste     ///     The instruction to be instrumented.
194ac7ddfbfSEd Maste     ///
195ac7ddfbfSEd Maste     /// @return
196ac7ddfbfSEd Maste     ///     True on success; false otherwise.
197ac7ddfbfSEd Maste     //------------------------------------------------------------------
198ac7ddfbfSEd Maste     virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0;
199ac7ddfbfSEd Maste 
200ac7ddfbfSEd Maste     //------------------------------------------------------------------
201ac7ddfbfSEd Maste     /// Register a single instruction to be instrumented
202ac7ddfbfSEd Maste     ///
203ac7ddfbfSEd Maste     /// @param[in] inst
204ac7ddfbfSEd Maste     ///     The instruction to be instrumented.
205ac7ddfbfSEd Maste     //------------------------------------------------------------------
206ac7ddfbfSEd Maste     void RegisterInstruction(llvm::Instruction &i)
207ac7ddfbfSEd Maste     {
208ac7ddfbfSEd Maste         m_to_instrument.push_back(&i);
209ac7ddfbfSEd Maste     }
210ac7ddfbfSEd Maste 
211ac7ddfbfSEd Maste     //------------------------------------------------------------------
212ac7ddfbfSEd Maste     /// Determine whether a single instruction is interesting to
213ac7ddfbfSEd Maste     /// instrument, and, if so, call RegisterInstruction
214ac7ddfbfSEd Maste     ///
215ac7ddfbfSEd Maste     /// @param[in] i
216ac7ddfbfSEd Maste     ///     The instruction to be inspected.
217ac7ddfbfSEd Maste     ///
218ac7ddfbfSEd Maste     /// @return
219ac7ddfbfSEd Maste     ///     False if there was an error scanning; true otherwise.
220ac7ddfbfSEd Maste     //------------------------------------------------------------------
221ac7ddfbfSEd Maste     virtual bool InspectInstruction(llvm::Instruction &i)
222ac7ddfbfSEd Maste     {
223ac7ddfbfSEd Maste         return true;
224ac7ddfbfSEd Maste     }
225ac7ddfbfSEd Maste 
226ac7ddfbfSEd Maste     //------------------------------------------------------------------
227ac7ddfbfSEd Maste     /// Scan a basic block to see if any instructions are interesting
228ac7ddfbfSEd Maste     ///
229ac7ddfbfSEd Maste     /// @param[in] bb
230ac7ddfbfSEd Maste     ///     The basic block to be inspected.
231ac7ddfbfSEd Maste     ///
232ac7ddfbfSEd Maste     /// @return
233ac7ddfbfSEd Maste     ///     False if there was an error scanning; true otherwise.
234ac7ddfbfSEd Maste     //------------------------------------------------------------------
235ac7ddfbfSEd Maste     virtual bool InspectBasicBlock(llvm::BasicBlock &bb)
236ac7ddfbfSEd Maste     {
237ac7ddfbfSEd Maste         for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end();
238ac7ddfbfSEd Maste              ii != last_ii;
239ac7ddfbfSEd Maste              ++ii)
240ac7ddfbfSEd Maste         {
241ac7ddfbfSEd Maste             if (!InspectInstruction(*ii))
242ac7ddfbfSEd Maste                 return false;
243ac7ddfbfSEd Maste         }
244ac7ddfbfSEd Maste 
245ac7ddfbfSEd Maste         return true;
246ac7ddfbfSEd Maste     }
247ac7ddfbfSEd Maste 
248ac7ddfbfSEd Maste     //------------------------------------------------------------------
249ac7ddfbfSEd Maste     /// Scan a function to see if any instructions are interesting
250ac7ddfbfSEd Maste     ///
251ac7ddfbfSEd Maste     /// @param[in] f
252ac7ddfbfSEd Maste     ///     The function to be inspected.
253ac7ddfbfSEd Maste     ///
254ac7ddfbfSEd Maste     /// @return
255ac7ddfbfSEd Maste     ///     False if there was an error scanning; true otherwise.
256ac7ddfbfSEd Maste     //------------------------------------------------------------------
257ac7ddfbfSEd Maste     virtual bool InspectFunction(llvm::Function &f)
258ac7ddfbfSEd Maste     {
259ac7ddfbfSEd Maste         for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end();
260ac7ddfbfSEd Maste              bbi != last_bbi;
261ac7ddfbfSEd Maste              ++bbi)
262ac7ddfbfSEd Maste         {
263ac7ddfbfSEd Maste             if (!InspectBasicBlock(*bbi))
264ac7ddfbfSEd Maste                 return false;
265ac7ddfbfSEd Maste         }
266ac7ddfbfSEd Maste 
267ac7ddfbfSEd Maste         return true;
268ac7ddfbfSEd Maste     }
269ac7ddfbfSEd Maste 
270ac7ddfbfSEd Maste     //------------------------------------------------------------------
271ac7ddfbfSEd Maste     /// Build a function pointer for a function with signature
272ac7ddfbfSEd Maste     /// void (*)(uint8_t*) with a given address
273ac7ddfbfSEd Maste     ///
274ac7ddfbfSEd Maste     /// @param[in] start_address
275ac7ddfbfSEd Maste     ///     The address of the function.
276ac7ddfbfSEd Maste     ///
277ac7ddfbfSEd Maste     /// @return
278ac7ddfbfSEd Maste     ///     The function pointer, for use in a CallInst.
279ac7ddfbfSEd Maste     //------------------------------------------------------------------
280ac7ddfbfSEd Maste     llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address)
281ac7ddfbfSEd Maste     {
282ac7ddfbfSEd Maste         IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
283ac7ddfbfSEd Maste                                                              (m_module.getPointerSize() == llvm::Module::Pointer64) ? 64 : 32);
284ac7ddfbfSEd Maste 
285ac7ddfbfSEd Maste         llvm::Type *param_array[1];
286ac7ddfbfSEd Maste 
287ac7ddfbfSEd Maste         param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
288ac7ddfbfSEd Maste 
289ac7ddfbfSEd Maste         ArrayRef<llvm::Type*> params(param_array, 1);
290ac7ddfbfSEd Maste 
291ac7ddfbfSEd Maste         FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
292ac7ddfbfSEd Maste         PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
293ac7ddfbfSEd Maste         Constant *fun_addr_int = ConstantInt::get(intptr_ty, start_address, false);
294ac7ddfbfSEd Maste         return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
295ac7ddfbfSEd Maste     }
296ac7ddfbfSEd Maste 
297ac7ddfbfSEd Maste     //------------------------------------------------------------------
298ac7ddfbfSEd Maste     /// Build a function pointer for a function with signature
299ac7ddfbfSEd Maste     /// void (*)(uint8_t*, uint8_t*) with a given address
300ac7ddfbfSEd Maste     ///
301ac7ddfbfSEd Maste     /// @param[in] start_address
302ac7ddfbfSEd Maste     ///     The address of the function.
303ac7ddfbfSEd Maste     ///
304ac7ddfbfSEd Maste     /// @return
305ac7ddfbfSEd Maste     ///     The function pointer, for use in a CallInst.
306ac7ddfbfSEd Maste     //------------------------------------------------------------------
307ac7ddfbfSEd Maste     llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address)
308ac7ddfbfSEd Maste     {
309ac7ddfbfSEd Maste         IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
310ac7ddfbfSEd Maste                                                        (m_module.getPointerSize() == llvm::Module::Pointer64) ? 64 : 32);
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);
321ac7ddfbfSEd Maste         Constant *fun_addr_int = ConstantInt::get(intptr_ty, 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 
333ac7ddfbfSEd Maste     typedef std::vector <llvm::Instruction *>   InstVector;
334ac7ddfbfSEd Maste     typedef InstVector::iterator                InstIterator;
335ac7ddfbfSEd Maste 
336ac7ddfbfSEd Maste     InstVector                  m_to_instrument;        ///< List of instructions the inspector found
337ac7ddfbfSEd Maste     llvm::Module               &m_module;               ///< The module which is being instrumented
338ac7ddfbfSEd Maste     DynamicCheckerFunctions    &m_checker_functions;    ///< The dynamic checker functions for the process
339ac7ddfbfSEd Maste private:
340ac7ddfbfSEd Maste     PointerType                *m_i8ptr_ty;
341ac7ddfbfSEd Maste };
342ac7ddfbfSEd Maste 
343ac7ddfbfSEd Maste class ValidPointerChecker : public Instrumenter
344ac7ddfbfSEd Maste {
345ac7ddfbfSEd Maste public:
346ac7ddfbfSEd Maste     ValidPointerChecker (llvm::Module &module,
347ac7ddfbfSEd Maste                          DynamicCheckerFunctions &checker_functions) :
348ac7ddfbfSEd Maste         Instrumenter(module, checker_functions),
349ac7ddfbfSEd Maste         m_valid_pointer_check_func(NULL)
350ac7ddfbfSEd Maste     {
351ac7ddfbfSEd Maste     }
352ac7ddfbfSEd Maste 
353ac7ddfbfSEd Maste     virtual ~ValidPointerChecker ()
354ac7ddfbfSEd Maste     {
355ac7ddfbfSEd Maste     }
356ac7ddfbfSEd Maste private:
357ac7ddfbfSEd Maste     bool InstrumentInstruction(llvm::Instruction *inst)
358ac7ddfbfSEd Maste     {
359ac7ddfbfSEd Maste         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
360ac7ddfbfSEd Maste 
361ac7ddfbfSEd Maste         if (log)
362ac7ddfbfSEd Maste             log->Printf("Instrumenting load/store instruction: %s\n",
363ac7ddfbfSEd Maste                         PrintValue(inst).c_str());
364ac7ddfbfSEd Maste 
365ac7ddfbfSEd Maste         if (!m_valid_pointer_check_func)
366ac7ddfbfSEd Maste             m_valid_pointer_check_func = BuildPointerValidatorFunc(m_checker_functions.m_valid_pointer_check->StartAddress());
367ac7ddfbfSEd Maste 
368ac7ddfbfSEd Maste         llvm::Value *dereferenced_ptr = NULL;
369ac7ddfbfSEd Maste 
370ac7ddfbfSEd Maste         if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst> (inst))
371ac7ddfbfSEd Maste             dereferenced_ptr = li->getPointerOperand();
372ac7ddfbfSEd Maste         else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst> (inst))
373ac7ddfbfSEd Maste             dereferenced_ptr = si->getPointerOperand();
374ac7ddfbfSEd Maste         else
375ac7ddfbfSEd Maste             return false;
376ac7ddfbfSEd Maste 
377ac7ddfbfSEd Maste         // Insert an instruction to cast the loaded value to int8_t*
378ac7ddfbfSEd Maste 
379ac7ddfbfSEd Maste         BitCastInst *bit_cast = new BitCastInst(dereferenced_ptr,
380ac7ddfbfSEd Maste                                                 GetI8PtrTy(),
381ac7ddfbfSEd Maste                                                 "",
382ac7ddfbfSEd Maste                                                 inst);
383ac7ddfbfSEd Maste 
384ac7ddfbfSEd Maste         // Insert an instruction to call the helper with the result
385ac7ddfbfSEd Maste 
386ac7ddfbfSEd Maste         llvm::Value *arg_array[1];
387ac7ddfbfSEd Maste 
388ac7ddfbfSEd Maste         arg_array[0] = bit_cast;
389ac7ddfbfSEd Maste 
390ac7ddfbfSEd Maste         llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
391ac7ddfbfSEd Maste 
392ac7ddfbfSEd Maste         CallInst::Create(m_valid_pointer_check_func,
393ac7ddfbfSEd Maste                          args,
394ac7ddfbfSEd Maste                          "",
395ac7ddfbfSEd Maste                          inst);
396ac7ddfbfSEd Maste 
397ac7ddfbfSEd Maste         return true;
398ac7ddfbfSEd Maste     }
399ac7ddfbfSEd Maste 
400ac7ddfbfSEd Maste     bool InspectInstruction(llvm::Instruction &i)
401ac7ddfbfSEd Maste     {
402ac7ddfbfSEd Maste         if (dyn_cast<llvm::LoadInst> (&i) ||
403ac7ddfbfSEd Maste             dyn_cast<llvm::StoreInst> (&i))
404ac7ddfbfSEd Maste             RegisterInstruction(i);
405ac7ddfbfSEd Maste 
406ac7ddfbfSEd Maste         return true;
407ac7ddfbfSEd Maste     }
408ac7ddfbfSEd Maste 
409ac7ddfbfSEd Maste     llvm::Value         *m_valid_pointer_check_func;
410ac7ddfbfSEd Maste };
411ac7ddfbfSEd Maste 
412ac7ddfbfSEd Maste class ObjcObjectChecker : public Instrumenter
413ac7ddfbfSEd Maste {
414ac7ddfbfSEd Maste public:
415ac7ddfbfSEd Maste     ObjcObjectChecker(llvm::Module &module,
416ac7ddfbfSEd Maste                         DynamicCheckerFunctions &checker_functions) :
417ac7ddfbfSEd Maste         Instrumenter(module, checker_functions),
418ac7ddfbfSEd Maste         m_objc_object_check_func(NULL)
419ac7ddfbfSEd Maste     {
420ac7ddfbfSEd Maste     }
421ac7ddfbfSEd Maste 
422ac7ddfbfSEd Maste     virtual
423ac7ddfbfSEd Maste     ~ObjcObjectChecker ()
424ac7ddfbfSEd Maste     {
425ac7ddfbfSEd Maste     }
426ac7ddfbfSEd Maste 
427ac7ddfbfSEd Maste     enum msgSend_type
428ac7ddfbfSEd Maste     {
429ac7ddfbfSEd Maste         eMsgSend = 0,
430ac7ddfbfSEd Maste         eMsgSendSuper,
431ac7ddfbfSEd Maste         eMsgSendSuper_stret,
432ac7ddfbfSEd Maste         eMsgSend_fpret,
433ac7ddfbfSEd Maste         eMsgSend_stret
434ac7ddfbfSEd Maste     };
435ac7ddfbfSEd Maste 
436ac7ddfbfSEd Maste     std::map <llvm::Instruction *, msgSend_type> msgSend_types;
437ac7ddfbfSEd Maste 
438ac7ddfbfSEd Maste private:
439ac7ddfbfSEd Maste     bool InstrumentInstruction(llvm::Instruction *inst)
440ac7ddfbfSEd Maste     {
441ac7ddfbfSEd Maste         CallInst *call_inst = dyn_cast<CallInst>(inst);
442ac7ddfbfSEd Maste 
443ac7ddfbfSEd Maste         if (!call_inst)
444ac7ddfbfSEd Maste             return false; // call_inst really shouldn't be NULL, because otherwise InspectInstruction wouldn't have registered it
445ac7ddfbfSEd Maste 
446ac7ddfbfSEd Maste         if (!m_objc_object_check_func)
447ac7ddfbfSEd Maste             m_objc_object_check_func = BuildObjectCheckerFunc(m_checker_functions.m_objc_object_check->StartAddress());
448ac7ddfbfSEd Maste 
449ac7ddfbfSEd Maste         // id objc_msgSend(id theReceiver, SEL theSelector, ...)
450ac7ddfbfSEd Maste 
451ac7ddfbfSEd Maste         llvm::Value *target_object;
452ac7ddfbfSEd Maste         llvm::Value *selector;
453ac7ddfbfSEd Maste 
454ac7ddfbfSEd Maste         switch (msgSend_types[inst])
455ac7ddfbfSEd Maste         {
456ac7ddfbfSEd Maste         case eMsgSend:
457ac7ddfbfSEd Maste         case eMsgSend_fpret:
458ac7ddfbfSEd Maste             target_object = call_inst->getArgOperand(0);
459ac7ddfbfSEd Maste             selector = call_inst->getArgOperand(1);
460ac7ddfbfSEd Maste             break;
461ac7ddfbfSEd Maste         case eMsgSend_stret:
462ac7ddfbfSEd Maste             target_object = call_inst->getArgOperand(1);
463ac7ddfbfSEd Maste             selector = call_inst->getArgOperand(2);
464ac7ddfbfSEd Maste             break;
465ac7ddfbfSEd Maste         case eMsgSendSuper:
466ac7ddfbfSEd Maste         case eMsgSendSuper_stret:
467ac7ddfbfSEd Maste             return true;
468ac7ddfbfSEd Maste         }
469ac7ddfbfSEd Maste 
470ac7ddfbfSEd Maste         // These objects should always be valid according to Sean Calannan
471ac7ddfbfSEd Maste         assert (target_object);
472ac7ddfbfSEd Maste         assert (selector);
473ac7ddfbfSEd Maste 
474ac7ddfbfSEd Maste         // Insert an instruction to cast the receiver id to int8_t*
475ac7ddfbfSEd Maste 
476ac7ddfbfSEd Maste         BitCastInst *bit_cast = new BitCastInst(target_object,
477ac7ddfbfSEd Maste                                                 GetI8PtrTy(),
478ac7ddfbfSEd Maste                                                 "",
479ac7ddfbfSEd Maste                                                 inst);
480ac7ddfbfSEd Maste 
481ac7ddfbfSEd Maste         // Insert an instruction to call the helper with the result
482ac7ddfbfSEd Maste 
483ac7ddfbfSEd Maste         llvm::Value *arg_array[2];
484ac7ddfbfSEd Maste 
485ac7ddfbfSEd Maste         arg_array[0] = bit_cast;
486ac7ddfbfSEd Maste         arg_array[1] = selector;
487ac7ddfbfSEd Maste 
488ac7ddfbfSEd Maste         ArrayRef<llvm::Value*> args(arg_array, 2);
489ac7ddfbfSEd Maste 
490ac7ddfbfSEd Maste         CallInst::Create(m_objc_object_check_func,
491ac7ddfbfSEd Maste                          args,
492ac7ddfbfSEd Maste                          "",
493ac7ddfbfSEd Maste                          inst);
494ac7ddfbfSEd Maste 
495ac7ddfbfSEd Maste         return true;
496ac7ddfbfSEd Maste     }
497ac7ddfbfSEd Maste 
498ac7ddfbfSEd Maste     bool InspectInstruction(llvm::Instruction &i)
499ac7ddfbfSEd Maste     {
500ac7ddfbfSEd Maste         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
501ac7ddfbfSEd Maste 
502ac7ddfbfSEd Maste         CallInst *call_inst = dyn_cast<CallInst>(&i);
503ac7ddfbfSEd Maste 
504ac7ddfbfSEd Maste         if (call_inst)
505ac7ddfbfSEd Maste         {
506ac7ddfbfSEd Maste             // This metadata is set by IRForTarget::MaybeHandleCall().
507ac7ddfbfSEd Maste 
508ac7ddfbfSEd Maste             MDNode *metadata = call_inst->getMetadata("lldb.call.realName");
509ac7ddfbfSEd Maste 
510ac7ddfbfSEd Maste             if (!metadata)
511ac7ddfbfSEd Maste                 return true;
512ac7ddfbfSEd Maste 
513ac7ddfbfSEd Maste             if (metadata->getNumOperands() != 1)
514ac7ddfbfSEd Maste             {
515ac7ddfbfSEd Maste                 if (log)
516ac7ddfbfSEd Maste                     log->Printf("Function call metadata has %d operands for [%p] %s", metadata->getNumOperands(), call_inst, PrintValue(call_inst).c_str());
517ac7ddfbfSEd Maste                 return false;
518ac7ddfbfSEd Maste             }
519ac7ddfbfSEd Maste 
520ac7ddfbfSEd Maste             MDString *real_name = dyn_cast<MDString>(metadata->getOperand(0));
521ac7ddfbfSEd Maste 
522ac7ddfbfSEd Maste             if (!real_name)
523ac7ddfbfSEd Maste             {
524ac7ddfbfSEd Maste                 if (log)
525ac7ddfbfSEd Maste                     log->Printf("Function call metadata is not an MDString for [%p] %s", call_inst, PrintValue(call_inst).c_str());
526ac7ddfbfSEd Maste                 return false;
527ac7ddfbfSEd Maste             }
528ac7ddfbfSEd Maste 
529ac7ddfbfSEd Maste             std::string name_str = real_name->getString();
530ac7ddfbfSEd Maste             const char* name_cstr = name_str.c_str();
531ac7ddfbfSEd Maste 
532ac7ddfbfSEd Maste             if (log)
533ac7ddfbfSEd Maste                 log->Printf("Found call to %s: %s\n", name_cstr, PrintValue(call_inst).c_str());
534ac7ddfbfSEd Maste 
535ac7ddfbfSEd Maste             if (name_str.find("objc_msgSend") == std::string::npos)
536ac7ddfbfSEd Maste                 return true;
537ac7ddfbfSEd Maste 
538ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSend"))
539ac7ddfbfSEd Maste             {
540ac7ddfbfSEd Maste                 RegisterInstruction(i);
541ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSend;
542ac7ddfbfSEd Maste                 return true;
543ac7ddfbfSEd Maste             }
544ac7ddfbfSEd Maste 
545ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSend_stret"))
546ac7ddfbfSEd Maste             {
547ac7ddfbfSEd Maste                 RegisterInstruction(i);
548ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSend_stret;
549ac7ddfbfSEd Maste                 return true;
550ac7ddfbfSEd Maste             }
551ac7ddfbfSEd Maste 
552ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSend_fpret"))
553ac7ddfbfSEd Maste             {
554ac7ddfbfSEd Maste                 RegisterInstruction(i);
555ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSend_fpret;
556ac7ddfbfSEd Maste                 return true;
557ac7ddfbfSEd Maste             }
558ac7ddfbfSEd Maste 
559ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSendSuper"))
560ac7ddfbfSEd Maste             {
561ac7ddfbfSEd Maste                 RegisterInstruction(i);
562ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSendSuper;
563ac7ddfbfSEd Maste                 return true;
564ac7ddfbfSEd Maste             }
565ac7ddfbfSEd Maste 
566ac7ddfbfSEd Maste             if (!strcmp(name_cstr, "objc_msgSendSuper_stret"))
567ac7ddfbfSEd Maste             {
568ac7ddfbfSEd Maste                 RegisterInstruction(i);
569ac7ddfbfSEd Maste                 msgSend_types[&i] = eMsgSendSuper_stret;
570ac7ddfbfSEd Maste                 return true;
571ac7ddfbfSEd Maste             }
572ac7ddfbfSEd Maste 
573ac7ddfbfSEd Maste             if (log)
574ac7ddfbfSEd Maste                 log->Printf("Function name '%s' contains 'objc_msgSend' but is not handled", name_str.c_str());
575ac7ddfbfSEd Maste 
576ac7ddfbfSEd Maste             return true;
577ac7ddfbfSEd Maste         }
578ac7ddfbfSEd Maste 
579ac7ddfbfSEd Maste         return true;
580ac7ddfbfSEd Maste     }
581ac7ddfbfSEd Maste 
582ac7ddfbfSEd Maste     llvm::Value         *m_objc_object_check_func;
583ac7ddfbfSEd Maste };
584ac7ddfbfSEd Maste 
585ac7ddfbfSEd Maste IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
586ac7ddfbfSEd Maste                                  const char *func_name) :
587ac7ddfbfSEd Maste     ModulePass(ID),
588ac7ddfbfSEd Maste     m_func_name(func_name),
589ac7ddfbfSEd Maste     m_checker_functions(checker_functions)
590ac7ddfbfSEd Maste {
591ac7ddfbfSEd Maste }
592ac7ddfbfSEd Maste 
593ac7ddfbfSEd Maste IRDynamicChecks::~IRDynamicChecks()
594ac7ddfbfSEd Maste {
595ac7ddfbfSEd Maste }
596ac7ddfbfSEd Maste 
597ac7ddfbfSEd Maste bool
598ac7ddfbfSEd Maste IRDynamicChecks::runOnModule(llvm::Module &M)
599ac7ddfbfSEd Maste {
600ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
601ac7ddfbfSEd Maste 
602ac7ddfbfSEd Maste     llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str()));
603ac7ddfbfSEd Maste 
604ac7ddfbfSEd Maste     if (!function)
605ac7ddfbfSEd Maste     {
606ac7ddfbfSEd Maste         if (log)
607ac7ddfbfSEd Maste             log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
608ac7ddfbfSEd Maste 
609ac7ddfbfSEd Maste         return false;
610ac7ddfbfSEd Maste     }
611ac7ddfbfSEd Maste 
612ac7ddfbfSEd Maste     if (m_checker_functions.m_valid_pointer_check.get())
613ac7ddfbfSEd Maste     {
614ac7ddfbfSEd Maste         ValidPointerChecker vpc(M, m_checker_functions);
615ac7ddfbfSEd Maste 
616ac7ddfbfSEd Maste         if (!vpc.Inspect(*function))
617ac7ddfbfSEd Maste             return false;
618ac7ddfbfSEd Maste 
619ac7ddfbfSEd Maste         if (!vpc.Instrument())
620ac7ddfbfSEd Maste             return false;
621ac7ddfbfSEd Maste     }
622ac7ddfbfSEd Maste 
623ac7ddfbfSEd Maste     if (m_checker_functions.m_objc_object_check.get())
624ac7ddfbfSEd Maste     {
625ac7ddfbfSEd Maste         ObjcObjectChecker ooc(M, m_checker_functions);
626ac7ddfbfSEd Maste 
627ac7ddfbfSEd Maste         if (!ooc.Inspect(*function))
628ac7ddfbfSEd Maste             return false;
629ac7ddfbfSEd Maste 
630ac7ddfbfSEd Maste         if (!ooc.Instrument())
631ac7ddfbfSEd Maste             return false;
632ac7ddfbfSEd Maste     }
633ac7ddfbfSEd Maste 
634ac7ddfbfSEd Maste     if (log && log->GetVerbose())
635ac7ddfbfSEd Maste     {
636ac7ddfbfSEd Maste         std::string s;
637ac7ddfbfSEd Maste         raw_string_ostream oss(s);
638ac7ddfbfSEd Maste 
639ac7ddfbfSEd Maste         M.print(oss, NULL);
640ac7ddfbfSEd Maste 
641ac7ddfbfSEd Maste         oss.flush();
642ac7ddfbfSEd Maste 
643ac7ddfbfSEd Maste         log->Printf ("Module after dynamic checks: \n%s", s.c_str());
644ac7ddfbfSEd Maste     }
645ac7ddfbfSEd Maste 
646ac7ddfbfSEd Maste     return true;
647ac7ddfbfSEd Maste }
648ac7ddfbfSEd Maste 
649ac7ddfbfSEd Maste void
650ac7ddfbfSEd Maste IRDynamicChecks::assignPassManager(PMStack &PMS,
651ac7ddfbfSEd Maste                                    PassManagerType T)
652ac7ddfbfSEd Maste {
653ac7ddfbfSEd Maste }
654ac7ddfbfSEd Maste 
655ac7ddfbfSEd Maste PassManagerType
656ac7ddfbfSEd Maste IRDynamicChecks::getPotentialPassManagerType() const
657ac7ddfbfSEd Maste {
658ac7ddfbfSEd Maste     return PMT_ModulePassManager;
659ac7ddfbfSEd Maste }
660