blob: 5c8d0e5288ba73968179cc50b753e26f7513acda [file] [log] [blame]
Sean Callanan6961e872010-09-01 00:58:00 +00001//===-- IRDynamicChecks.cpp -------------------------------------------*- 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#include "lldb/Expression/IRDynamicChecks.h"
Sean Callanan6961e872010-09-01 00:58:00 +000011
Sean Callanan9e6ed532010-09-13 21:34:21 +000012#include "lldb/Core/ConstString.h"
Sean Callanan6961e872010-09-01 00:58:00 +000013#include "lldb/Core/Log.h"
Jim Ingham151c0322015-09-15 21:13:50 +000014#include "lldb/Expression/UtilityFunction.h"
Sean Callanan9e6ed532010-09-13 21:34:21 +000015#include "lldb/Target/ExecutionContext.h"
Sean Callanan10af7c42010-11-04 01:51:38 +000016#include "lldb/Target/ObjCLanguageRuntime.h"
17#include "lldb/Target/Process.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000018#include "lldb/Target/StackFrame.h"
Jim Ingham151c0322015-09-15 21:13:50 +000019#include "lldb/Target/Target.h"
Sean Callanan6961e872010-09-01 00:58:00 +000020
21#include "llvm/Support/raw_ostream.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000022#include "llvm/IR/Constants.h"
Sean Callanan439dcae2013-12-20 19:55:02 +000023#include "llvm/IR/DataLayout.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000024#include "llvm/IR/Function.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/Module.h"
27#include "llvm/IR/Value.h"
Sean Callanan6961e872010-09-01 00:58:00 +000028
29using namespace llvm;
30using namespace lldb_private;
31
32static char ID;
33
Greg Clayton7b462cc2010-10-15 22:48:33 +000034#define VALID_POINTER_CHECK_NAME "$__lldb_valid_pointer_check"
35#define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check"
Sean Callanan9e6ed532010-09-13 21:34:21 +000036
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000037static const char g_valid_pointer_check_text[] =
Greg Clayton7b462cc2010-10-15 22:48:33 +000038"extern \"C\" void\n"
Greg Claytond59cea22010-10-16 21:09:32 +000039"$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n"
Greg Clayton7b462cc2010-10-15 22:48:33 +000040"{\n"
Greg Claytondd36def2010-10-17 22:03:32 +000041" unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
Greg Clayton7b462cc2010-10-15 22:48:33 +000042"}";
Sean Callanan9e6ed532010-09-13 21:34:21 +000043
Sean Callanan6961e872010-09-01 00:58:00 +000044DynamicCheckerFunctions::DynamicCheckerFunctions ()
45{
Sean Callanan6961e872010-09-01 00:58:00 +000046}
47
48DynamicCheckerFunctions::~DynamicCheckerFunctions ()
49{
50}
51
52bool
53DynamicCheckerFunctions::Install(Stream &error_stream,
54 ExecutionContext &exe_ctx)
55{
Jim Ingham151c0322015-09-15 21:13:50 +000056 Error error;
57 m_valid_pointer_check.reset(exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(g_valid_pointer_check_text,
58 lldb::eLanguageTypeC,
59 VALID_POINTER_CHECK_NAME,
60 error));
61 if (error.Fail())
62 return false;
63
Sean Callanan6961e872010-09-01 00:58:00 +000064 if (!m_valid_pointer_check->Install(error_stream, exe_ctx))
65 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000066
Greg Claytonc14ee322011-09-22 04:58:26 +000067 Process *process = exe_ctx.GetProcessPtr();
68
69 if (process)
Sean Callanan10af7c42010-11-04 01:51:38 +000070 {
Greg Claytonc14ee322011-09-22 04:58:26 +000071 ObjCLanguageRuntime *objc_language_runtime = process->GetObjCLanguageRuntime();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000072
Sean Callanan10af7c42010-11-04 01:51:38 +000073 if (objc_language_runtime)
74 {
75 m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker(VALID_OBJC_OBJECT_CHECK_NAME));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000076
Sean Callanan10af7c42010-11-04 01:51:38 +000077 if (!m_objc_object_check->Install(error_stream, exe_ctx))
78 return false;
79 }
80 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000081
Sean Callanan6961e872010-09-01 00:58:00 +000082 return true;
83}
84
Jim Inghamce553d82011-11-01 02:46:54 +000085bool
86DynamicCheckerFunctions::DoCheckersExplainStop (lldb::addr_t addr, Stream &message)
87{
88 // FIXME: We have to get the checkers to know why they scotched the call in more detail,
89 // so we can print a better message here.
90 if (m_valid_pointer_check.get() != NULL && m_valid_pointer_check->ContainsAddress(addr))
91 {
92 message.Printf ("Attempted to dereference an invalid pointer.");
93 return true;
94 }
95 else if (m_objc_object_check.get() != NULL && m_objc_object_check->ContainsAddress(addr))
96 {
97 message.Printf ("Attempted to dereference an invalid ObjC Object or send it an unrecognized selector");
98 return true;
99 }
100 return false;
101}
102
103
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000104static std::string
Sean Callanan6961e872010-09-01 00:58:00 +0000105PrintValue(llvm::Value *V, bool truncate = false)
106{
107 std::string s;
108 raw_string_ostream rso(s);
109 V->print(rso);
110 rso.flush();
111 if (truncate)
112 s.resize(s.length() - 1);
113 return s;
114}
115
Sean Callanan8e999e42010-09-02 00:37:32 +0000116//----------------------------------------------------------------------
117/// @class Instrumenter IRDynamicChecks.cpp
118/// @brief Finds and instruments individual LLVM IR instructions
119///
120/// When instrumenting LLVM IR, it is frequently desirable to first search
121/// for instructions, and then later modify them. This way iterators
122/// remain intact, and multiple passes can look at the same code base without
123/// treading on each other's toes.
124///
125/// The Instrumenter class implements this functionality. A client first
126/// calls Inspect on a function, which populates a list of instructions to
127/// be instrumented. Then, later, when all passes' Inspect functions have
128/// been called, the client calls Instrument, which adds the desired
129/// instrumentation.
130///
131/// A subclass of Instrumenter must override InstrumentInstruction, which
132/// is responsible for adding whatever instrumentation is necessary.
133///
134/// A subclass of Instrumenter may override:
135///
136/// - InspectInstruction [default: does nothing]
137///
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000138/// - InspectBasicBlock [default: iterates through the instructions in a
Sean Callanan8e999e42010-09-02 00:37:32 +0000139/// basic block calling InspectInstruction]
140///
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000141/// - InspectFunction [default: iterates through the basic blocks in a
Sean Callanan8e999e42010-09-02 00:37:32 +0000142/// function calling InspectBasicBlock]
143//----------------------------------------------------------------------
144class Instrumenter {
145public:
146 //------------------------------------------------------------------
147 /// Constructor
148 ///
149 /// @param[in] module
150 /// The module being instrumented.
151 //------------------------------------------------------------------
152 Instrumenter (llvm::Module &module,
153 DynamicCheckerFunctions &checker_functions) :
154 m_module(module),
Sean Callanan9e6ed532010-09-13 21:34:21 +0000155 m_checker_functions(checker_functions),
Sean Callanan439dcae2013-12-20 19:55:02 +0000156 m_i8ptr_ty(NULL),
157 m_intptr_ty(NULL)
Sean Callanan8e999e42010-09-02 00:37:32 +0000158 {
159 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000160
Greg Clayton1a65ae12011-01-25 23:55:37 +0000161 virtual~Instrumenter ()
162 {
163 }
164
Sean Callanan8e999e42010-09-02 00:37:32 +0000165 //------------------------------------------------------------------
166 /// Inspect a function to find instructions to instrument
167 ///
168 /// @param[in] function
169 /// The function to inspect.
170 ///
171 /// @return
172 /// True on success; false on error.
173 //------------------------------------------------------------------
174 bool Inspect (llvm::Function &function)
175 {
176 return InspectFunction(function);
177 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000178
Sean Callanan8e999e42010-09-02 00:37:32 +0000179 //------------------------------------------------------------------
180 /// Instrument all the instructions found by Inspect()
181 ///
182 /// @return
183 /// True on success; false on error.
184 //------------------------------------------------------------------
185 bool Instrument ()
186 {
187 for (InstIterator ii = m_to_instrument.begin(), last_ii = m_to_instrument.end();
188 ii != last_ii;
189 ++ii)
190 {
191 if (!InstrumentInstruction(*ii))
192 return false;
193 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000194
Sean Callanan8e999e42010-09-02 00:37:32 +0000195 return true;
196 }
197protected:
198 //------------------------------------------------------------------
199 /// Add instrumentation to a single instruction
200 ///
201 /// @param[in] inst
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000202 /// The instruction to be instrumented.
Sean Callanan8e999e42010-09-02 00:37:32 +0000203 ///
204 /// @return
205 /// True on success; false otherwise.
206 //------------------------------------------------------------------
207 virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000208
Sean Callanan8e999e42010-09-02 00:37:32 +0000209 //------------------------------------------------------------------
210 /// Register a single instruction to be instrumented
211 ///
212 /// @param[in] inst
213 /// The instruction to be instrumented.
214 //------------------------------------------------------------------
215 void RegisterInstruction(llvm::Instruction &i)
216 {
217 m_to_instrument.push_back(&i);
218 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000219
Sean Callanan8e999e42010-09-02 00:37:32 +0000220 //------------------------------------------------------------------
221 /// Determine whether a single instruction is interesting to
222 /// instrument, and, if so, call RegisterInstruction
223 ///
224 /// @param[in] i
225 /// The instruction to be inspected.
226 ///
227 /// @return
228 /// False if there was an error scanning; true otherwise.
229 //------------------------------------------------------------------
230 virtual bool InspectInstruction(llvm::Instruction &i)
231 {
232 return true;
233 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000234
Sean Callanan8e999e42010-09-02 00:37:32 +0000235 //------------------------------------------------------------------
236 /// Scan a basic block to see if any instructions are interesting
237 ///
238 /// @param[in] bb
239 /// The basic block to be inspected.
240 ///
241 /// @return
242 /// False if there was an error scanning; true otherwise.
243 //------------------------------------------------------------------
244 virtual bool InspectBasicBlock(llvm::BasicBlock &bb)
245 {
246 for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end();
247 ii != last_ii;
248 ++ii)
249 {
250 if (!InspectInstruction(*ii))
251 return false;
252 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000253
Sean Callanan8e999e42010-09-02 00:37:32 +0000254 return true;
255 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000256
Sean Callanan8e999e42010-09-02 00:37:32 +0000257 //------------------------------------------------------------------
258 /// Scan a function to see if any instructions are interesting
259 ///
260 /// @param[in] f
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000261 /// The function to be inspected.
Sean Callanan8e999e42010-09-02 00:37:32 +0000262 ///
263 /// @return
264 /// False if there was an error scanning; true otherwise.
265 //------------------------------------------------------------------
266 virtual bool InspectFunction(llvm::Function &f)
267 {
268 for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end();
269 bbi != last_bbi;
270 ++bbi)
271 {
272 if (!InspectBasicBlock(*bbi))
273 return false;
274 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000275
Sean Callanan8e999e42010-09-02 00:37:32 +0000276 return true;
277 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000278
Sean Callanan9e6ed532010-09-13 21:34:21 +0000279 //------------------------------------------------------------------
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000280 /// Build a function pointer for a function with signature
Sean Callanan9e6ed532010-09-13 21:34:21 +0000281 /// void (*)(uint8_t*) with a given address
282 ///
283 /// @param[in] start_address
284 /// The address of the function.
285 ///
286 /// @return
287 /// The function pointer, for use in a CallInst.
288 //------------------------------------------------------------------
289 llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address)
290 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000291 llvm::Type *param_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000292
Sean Callanancc427fa2011-07-30 02:42:06 +0000293 param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000294
Sean Callanancc427fa2011-07-30 02:42:06 +0000295 ArrayRef<llvm::Type*> params(param_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000296
Sean Callanan9e6ed532010-09-13 21:34:21 +0000297 FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
298 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000299 Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000300 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
301 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000302
Sean Callanan7ba96362011-10-27 00:02:05 +0000303 //------------------------------------------------------------------
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000304 /// Build a function pointer for a function with signature
Sean Callanan7ba96362011-10-27 00:02:05 +0000305 /// void (*)(uint8_t*, uint8_t*) with a given address
306 ///
307 /// @param[in] start_address
308 /// The address of the function.
309 ///
310 /// @return
311 /// The function pointer, for use in a CallInst.
312 //------------------------------------------------------------------
313 llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address)
314 {
Sean Callanan7ba96362011-10-27 00:02:05 +0000315 llvm::Type *param_array[2];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000316
Sean Callanan7ba96362011-10-27 00:02:05 +0000317 param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
318 param_array[1] = const_cast<llvm::PointerType*>(GetI8PtrTy());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000319
Sean Callanan7ba96362011-10-27 00:02:05 +0000320 ArrayRef<llvm::Type*> params(param_array, 2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000321
Sean Callanan7ba96362011-10-27 00:02:05 +0000322 FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
323 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000324 Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false);
Sean Callanan7ba96362011-10-27 00:02:05 +0000325 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
326 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000327
Sean Callanancc427fa2011-07-30 02:42:06 +0000328 PointerType *GetI8PtrTy()
Sean Callanan10af7c42010-11-04 01:51:38 +0000329 {
330 if (!m_i8ptr_ty)
331 m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000332
Sean Callanan10af7c42010-11-04 01:51:38 +0000333 return m_i8ptr_ty;
334 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000335
Sean Callanan439dcae2013-12-20 19:55:02 +0000336 IntegerType *GetIntptrTy()
337 {
338 if (!m_intptr_ty)
339 {
340 llvm::DataLayout data_layout(&m_module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000341
Sean Callanan439dcae2013-12-20 19:55:02 +0000342 m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), data_layout.getPointerSizeInBits());
343 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000344
Sean Callanan439dcae2013-12-20 19:55:02 +0000345 return m_intptr_ty;
346 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000347
Sean Callanan8e999e42010-09-02 00:37:32 +0000348 typedef std::vector <llvm::Instruction *> InstVector;
349 typedef InstVector::iterator InstIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000350
Sean Callanan8e999e42010-09-02 00:37:32 +0000351 InstVector m_to_instrument; ///< List of instructions the inspector found
352 llvm::Module &m_module; ///< The module which is being instrumented
353 DynamicCheckerFunctions &m_checker_functions; ///< The dynamic checker functions for the process
Sean Callanan10af7c42010-11-04 01:51:38 +0000354private:
Sean Callanancc427fa2011-07-30 02:42:06 +0000355 PointerType *m_i8ptr_ty;
Sean Callanan439dcae2013-12-20 19:55:02 +0000356 IntegerType *m_intptr_ty;
Sean Callanan8e999e42010-09-02 00:37:32 +0000357};
358
359class ValidPointerChecker : public Instrumenter
360{
361public:
Greg Clayton1a65ae12011-01-25 23:55:37 +0000362 ValidPointerChecker (llvm::Module &module,
363 DynamicCheckerFunctions &checker_functions) :
Sean Callanan8e999e42010-09-02 00:37:32 +0000364 Instrumenter(module, checker_functions),
365 m_valid_pointer_check_func(NULL)
366 {
367 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000368
Greg Clayton1a65ae12011-01-25 23:55:37 +0000369 virtual ~ValidPointerChecker ()
370 {
371 }
Sean Callanan8e999e42010-09-02 00:37:32 +0000372private:
373 bool InstrumentInstruction(llvm::Instruction *inst)
374 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000375 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8e999e42010-09-02 00:37:32 +0000376
Enrico Granata20edcdb2011-07-19 18:03:25 +0000377 if (log)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000378 log->Printf("Instrumenting load/store instruction: %s\n",
Sean Callanan8e999e42010-09-02 00:37:32 +0000379 PrintValue(inst).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000380
Sean Callanan8e999e42010-09-02 00:37:32 +0000381 if (!m_valid_pointer_check_func)
Sean Callanan9e6ed532010-09-13 21:34:21 +0000382 m_valid_pointer_check_func = BuildPointerValidatorFunc(m_checker_functions.m_valid_pointer_check->StartAddress());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000383
Sean Callanan77eaf442011-07-08 00:39:14 +0000384 llvm::Value *dereferenced_ptr = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000385
Sean Callanan8e999e42010-09-02 00:37:32 +0000386 if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst> (inst))
387 dereferenced_ptr = li->getPointerOperand();
388 else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst> (inst))
389 dereferenced_ptr = si->getPointerOperand();
390 else
391 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000392
Sean Callanan8e999e42010-09-02 00:37:32 +0000393 // Insert an instruction to cast the loaded value to int8_t*
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000394
Sean Callanan8e999e42010-09-02 00:37:32 +0000395 BitCastInst *bit_cast = new BitCastInst(dereferenced_ptr,
Sean Callanan10af7c42010-11-04 01:51:38 +0000396 GetI8PtrTy(),
Sean Callanan8e999e42010-09-02 00:37:32 +0000397 "",
398 inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000399
Sean Callanan8e999e42010-09-02 00:37:32 +0000400 // Insert an instruction to call the helper with the result
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000401
Sean Callanancc427fa2011-07-30 02:42:06 +0000402 llvm::Value *arg_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000403
Sean Callanancc427fa2011-07-30 02:42:06 +0000404 arg_array[0] = bit_cast;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000405
Sean Callanancc427fa2011-07-30 02:42:06 +0000406 llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000407
408 CallInst::Create(m_valid_pointer_check_func,
Sean Callanancc427fa2011-07-30 02:42:06 +0000409 args,
Sean Callanan8e999e42010-09-02 00:37:32 +0000410 "",
411 inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000412
Sean Callanan8e999e42010-09-02 00:37:32 +0000413 return true;
414 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000415
Sean Callanan8e999e42010-09-02 00:37:32 +0000416 bool InspectInstruction(llvm::Instruction &i)
417 {
418 if (dyn_cast<llvm::LoadInst> (&i) ||
419 dyn_cast<llvm::StoreInst> (&i))
420 RegisterInstruction(i);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000421
Sean Callanan8e999e42010-09-02 00:37:32 +0000422 return true;
423 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000424
Sean Callanan8e999e42010-09-02 00:37:32 +0000425 llvm::Value *m_valid_pointer_check_func;
Sean Callanan9e6ed532010-09-13 21:34:21 +0000426};
427
428class ObjcObjectChecker : public Instrumenter
429{
430public:
431 ObjcObjectChecker(llvm::Module &module,
432 DynamicCheckerFunctions &checker_functions) :
433 Instrumenter(module, checker_functions),
434 m_objc_object_check_func(NULL)
435 {
436 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000437
Greg Clayton1a65ae12011-01-25 23:55:37 +0000438 virtual
439 ~ObjcObjectChecker ()
440 {
441 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000442
Sean Callanana6cbf062011-11-16 00:20:50 +0000443 enum msgSend_type
444 {
445 eMsgSend = 0,
446 eMsgSendSuper,
447 eMsgSendSuper_stret,
448 eMsgSend_fpret,
449 eMsgSend_stret
450 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000451
Sean Callanana6cbf062011-11-16 00:20:50 +0000452 std::map <llvm::Instruction *, msgSend_type> msgSend_types;
Greg Clayton1a65ae12011-01-25 23:55:37 +0000453
Sean Callanan9e6ed532010-09-13 21:34:21 +0000454private:
455 bool InstrumentInstruction(llvm::Instruction *inst)
456 {
457 CallInst *call_inst = dyn_cast<CallInst>(inst);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000458
Sean Callanan9e6ed532010-09-13 21:34:21 +0000459 if (!call_inst)
Sean Callanan10af7c42010-11-04 01:51:38 +0000460 return false; // call_inst really shouldn't be NULL, because otherwise InspectInstruction wouldn't have registered it
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000461
Sean Callanan9e6ed532010-09-13 21:34:21 +0000462 if (!m_objc_object_check_func)
Sean Callanan7ba96362011-10-27 00:02:05 +0000463 m_objc_object_check_func = BuildObjectCheckerFunc(m_checker_functions.m_objc_object_check->StartAddress());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000464
Sean Callanan9e6ed532010-09-13 21:34:21 +0000465 // id objc_msgSend(id theReceiver, SEL theSelector, ...)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000466
Sean Callanana6cbf062011-11-16 00:20:50 +0000467 llvm::Value *target_object;
468 llvm::Value *selector;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000469
Sean Callanana6cbf062011-11-16 00:20:50 +0000470 switch (msgSend_types[inst])
471 {
472 case eMsgSend:
473 case eMsgSend_fpret:
474 target_object = call_inst->getArgOperand(0);
475 selector = call_inst->getArgOperand(1);
476 break;
477 case eMsgSend_stret:
478 target_object = call_inst->getArgOperand(1);
479 selector = call_inst->getArgOperand(2);
Greg Clayton23f59502012-07-17 03:23:13 +0000480 break;
Sean Callanana6cbf062011-11-16 00:20:50 +0000481 case eMsgSendSuper:
482 case eMsgSendSuper_stret:
483 return true;
484 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000485
Greg Clayton23f59502012-07-17 03:23:13 +0000486 // These objects should always be valid according to Sean Calannan
487 assert (target_object);
488 assert (selector);
489
Sean Callanan9e6ed532010-09-13 21:34:21 +0000490 // Insert an instruction to cast the receiver id to int8_t*
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000491
Sean Callanan9e6ed532010-09-13 21:34:21 +0000492 BitCastInst *bit_cast = new BitCastInst(target_object,
Sean Callanan10af7c42010-11-04 01:51:38 +0000493 GetI8PtrTy(),
Sean Callanan9e6ed532010-09-13 21:34:21 +0000494 "",
495 inst);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000496
Sean Callanan9e6ed532010-09-13 21:34:21 +0000497 // Insert an instruction to call the helper with the result
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000498
Sean Callanan7ba96362011-10-27 00:02:05 +0000499 llvm::Value *arg_array[2];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000500
Sean Callanancc427fa2011-07-30 02:42:06 +0000501 arg_array[0] = bit_cast;
Sean Callanan7ba96362011-10-27 00:02:05 +0000502 arg_array[1] = selector;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000503
Sean Callananfc8feb82011-10-31 22:11:40 +0000504 ArrayRef<llvm::Value*> args(arg_array, 2);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000505
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000506 CallInst::Create(m_objc_object_check_func,
Sean Callanancc427fa2011-07-30 02:42:06 +0000507 args,
Sean Callanan9e6ed532010-09-13 21:34:21 +0000508 "",
509 inst);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000510
Sean Callanan9e6ed532010-09-13 21:34:21 +0000511 return true;
512 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000513
Sean Callanan9e6ed532010-09-13 21:34:21 +0000514 bool InspectInstruction(llvm::Instruction &i)
515 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000516 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000517
518 CallInst *call_inst = dyn_cast<CallInst>(&i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000519
Sean Callanan9e6ed532010-09-13 21:34:21 +0000520 if (call_inst)
521 {
522 // This metadata is set by IRForTarget::MaybeHandleCall().
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000523
Duncan P. N. Exon Smith68caa7d2014-11-12 01:59:53 +0000524 MDNode *metadata = call_inst->getMetadata("lldb.call.realName");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000525
Sean Callanan9e6ed532010-09-13 21:34:21 +0000526 if (!metadata)
527 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000528
Sean Callanan9e6ed532010-09-13 21:34:21 +0000529 if (metadata->getNumOperands() != 1)
530 {
531 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000532 log->Printf("Function call metadata has %d operands for [%p] %s",
533 metadata->getNumOperands(),
534 static_cast<void*>(call_inst),
535 PrintValue(call_inst).c_str());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000536 return false;
537 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000538
Daniel Maleaf051dbc2013-06-03 20:45:54 +0000539 MDString *real_name = dyn_cast<MDString>(metadata->getOperand(0));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000540
Sean Callanan9e6ed532010-09-13 21:34:21 +0000541 if (!real_name)
542 {
543 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000544 log->Printf("Function call metadata is not an MDString for [%p] %s",
545 static_cast<void*>(call_inst),
546 PrintValue(call_inst).c_str());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000547 return false;
548 }
Daniel Maleaf051dbc2013-06-03 20:45:54 +0000549
550 std::string name_str = real_name->getString();
Sean Callanana6cbf062011-11-16 00:20:50 +0000551 const char* name_cstr = name_str.c_str();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000552
Sean Callanand2b465f2012-02-09 03:22:41 +0000553 if (log)
554 log->Printf("Found call to %s: %s\n", name_cstr, PrintValue(call_inst).c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000555
Sean Callanana6cbf062011-11-16 00:20:50 +0000556 if (name_str.find("objc_msgSend") == std::string::npos)
557 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000558
Sean Callanana6cbf062011-11-16 00:20:50 +0000559 if (!strcmp(name_cstr, "objc_msgSend"))
560 {
Sean Callanan9e6ed532010-09-13 21:34:21 +0000561 RegisterInstruction(i);
Sean Callanana6cbf062011-11-16 00:20:50 +0000562 msgSend_types[&i] = eMsgSend;
563 return true;
564 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000565
Sean Callanana6cbf062011-11-16 00:20:50 +0000566 if (!strcmp(name_cstr, "objc_msgSend_stret"))
567 {
568 RegisterInstruction(i);
569 msgSend_types[&i] = eMsgSend_stret;
570 return true;
571 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000572
Sean Callanana6cbf062011-11-16 00:20:50 +0000573 if (!strcmp(name_cstr, "objc_msgSend_fpret"))
574 {
575 RegisterInstruction(i);
576 msgSend_types[&i] = eMsgSend_fpret;
577 return true;
578 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000579
Sean Callanana6cbf062011-11-16 00:20:50 +0000580 if (!strcmp(name_cstr, "objc_msgSendSuper"))
581 {
582 RegisterInstruction(i);
583 msgSend_types[&i] = eMsgSendSuper;
584 return true;
585 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000586
Sean Callanana6cbf062011-11-16 00:20:50 +0000587 if (!strcmp(name_cstr, "objc_msgSendSuper_stret"))
588 {
589 RegisterInstruction(i);
590 msgSend_types[&i] = eMsgSendSuper_stret;
591 return true;
592 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000593
Sean Callanana6cbf062011-11-16 00:20:50 +0000594 if (log)
595 log->Printf("Function name '%s' contains 'objc_msgSend' but is not handled", name_str.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000596
Sean Callanana6cbf062011-11-16 00:20:50 +0000597 return true;
Sean Callanan9e6ed532010-09-13 21:34:21 +0000598 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000599
Sean Callanan9e6ed532010-09-13 21:34:21 +0000600 return true;
601 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000602
Sean Callanan9e6ed532010-09-13 21:34:21 +0000603 llvm::Value *m_objc_object_check_func;
Sean Callanan8e999e42010-09-02 00:37:32 +0000604};
605
606IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
607 const char *func_name) :
Sean Callanane2ef6e32010-09-23 03:01:22 +0000608 ModulePass(ID),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000609 m_func_name(func_name),
610 m_checker_functions(checker_functions)
Sean Callanan8e999e42010-09-02 00:37:32 +0000611{
612}
613
Sean Callanan6961e872010-09-01 00:58:00 +0000614IRDynamicChecks::~IRDynamicChecks()
615{
616}
617
618bool
619IRDynamicChecks::runOnModule(llvm::Module &M)
620{
Greg Clayton5160ce52013-03-27 23:08:40 +0000621 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000622
Sean Callanan6961e872010-09-01 00:58:00 +0000623 llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000624
Sean Callanan6961e872010-09-01 00:58:00 +0000625 if (!function)
626 {
627 if (log)
628 log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000629
Sean Callanan6961e872010-09-01 00:58:00 +0000630 return false;
631 }
Sean Callanan8e999e42010-09-02 00:37:32 +0000632
Sean Callanan53bacf212012-05-29 23:46:46 +0000633 if (m_checker_functions.m_valid_pointer_check.get())
634 {
635 ValidPointerChecker vpc(M, m_checker_functions);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000636
Sean Callanan53bacf212012-05-29 23:46:46 +0000637 if (!vpc.Inspect(*function))
638 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000639
Sean Callanan53bacf212012-05-29 23:46:46 +0000640 if (!vpc.Instrument())
641 return false;
642 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000643
Sean Callanan53bacf212012-05-29 23:46:46 +0000644 if (m_checker_functions.m_objc_object_check.get())
645 {
646 ObjcObjectChecker ooc(M, m_checker_functions);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000647
Sean Callanan53bacf212012-05-29 23:46:46 +0000648 if (!ooc.Inspect(*function))
649 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000650
Sean Callanan53bacf212012-05-29 23:46:46 +0000651 if (!ooc.Instrument())
652 return false;
653 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000654
Jim Inghame3be0c52011-01-22 01:25:40 +0000655 if (log && log->GetVerbose())
Sean Callananafa42372010-09-08 20:04:08 +0000656 {
657 std::string s;
658 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000659
Sean Callananafa42372010-09-08 20:04:08 +0000660 M.print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000661
Sean Callananafa42372010-09-08 20:04:08 +0000662 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000663
Jim Inghame3be0c52011-01-22 01:25:40 +0000664 log->Printf ("Module after dynamic checks: \n%s", s.c_str());
Sean Callananafa42372010-09-08 20:04:08 +0000665 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000666
667 return true;
Sean Callanan6961e872010-09-01 00:58:00 +0000668}
669
670void
671IRDynamicChecks::assignPassManager(PMStack &PMS,
672 PassManagerType T)
673{
674}
675
676PassManagerType
677IRDynamicChecks::getPotentialPassManagerType() const
678{
679 return PMT_ModulePassManager;
680}