2010-09-01 08:58:00 +08:00
|
|
|
//===-- IRDynamicChecks.cpp -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Expression/IRDynamicChecks.h"
|
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
#include "lldb/Core/ConstString.h"
|
2010-09-01 08:58:00 +08:00
|
|
|
#include "lldb/Core/Log.h"
|
2010-09-14 05:34:21 +08:00
|
|
|
#include "lldb/Expression/ClangUtilityFunction.h"
|
|
|
|
#include "lldb/Target/ExecutionContext.h"
|
2010-11-04 09:51:38 +08:00
|
|
|
#include "lldb/Target/ObjCLanguageRuntime.h"
|
|
|
|
#include "lldb/Target/Process.h"
|
2010-09-14 05:34:21 +08:00
|
|
|
#include "lldb/Target/StackFrame.h"
|
2010-09-01 08:58:00 +08:00
|
|
|
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-04-14 10:01:31 +08:00
|
|
|
#include "llvm/Constants.h"
|
2010-09-01 08:58:00 +08:00
|
|
|
#include "llvm/Function.h"
|
2010-09-02 08:37:32 +08:00
|
|
|
#include "llvm/Instructions.h"
|
2010-09-01 08:58:00 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Value.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
static char ID;
|
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
#define VALID_POINTER_CHECK_NAME "$__lldb_valid_pointer_check"
|
|
|
|
#define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check"
|
2010-09-14 05:34:21 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
static const char g_valid_pointer_check_text[] =
|
|
|
|
"extern \"C\" void\n"
|
2010-10-17 05:09:32 +08:00
|
|
|
"$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n"
|
2010-10-16 06:48:33 +08:00
|
|
|
"{\n"
|
Added a new Host call to find LLDB related paths:
static bool
Host::GetLLDBPath (lldb::PathType path_type, FileSpec &file_spec);
This will fill in "file_spec" with an appropriate path that is appropriate
for the current Host OS. MacOSX will return paths within the LLDB.framework,
and other unixes will return the paths they want. The current PathType
enums are:
typedef enum PathType
{
ePathTypeLLDBShlibDir, // The directory where the lldb.so (unix) or LLDB mach-o file in LLDB.framework (MacOSX) exists
ePathTypeSupportExecutableDir, // Find LLDB support executable directory (debugserver, etc)
ePathTypeHeaderDir, // Find LLDB header file directory
ePathTypePythonDir // Find Python modules (PYTHONPATH) directory
} PathType;
All places that were finding executables are and python paths are now updated
to use this Host call.
Added another new host call to launch the inferior in a terminal. This ability
will be very host specific and doesn't need to be supported on all systems.
MacOSX currently will create a new .command file and tell Terminal.app to open
the .command file. It also uses the new "darwin-debug" app which is a small
app that uses posix to exec (no fork) and stop at the entry point of the
program. The GDB remote plug-in is almost able launch a process and attach to
it, it currently will spawn the process, but it won't attach to it just yet.
This will let LLDB not have to share the terminal with another process and a
new terminal window will pop up when you launch. This won't get hooked up
until we work out all of the kinks. The new Host function is:
static lldb::pid_t
Host::LaunchInNewTerminal (
const char **argv, // argv[0] is executable
const char **envp,
const ArchSpec *arch_spec,
bool stop_at_entry,
bool disable_aslr);
Cleaned up FileSpec::GetPath to not use strncpy() as it was always zero
filling the entire path buffer.
Fixed an issue with the dynamic checker function where I missed a '$' prefix
that should have been added.
llvm-svn: 116690
2010-10-18 06:03:32 +08:00
|
|
|
" unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
|
2010-10-16 06:48:33 +08:00
|
|
|
"}";
|
2010-09-14 05:34:21 +08:00
|
|
|
|
2010-09-01 08:58:00 +08:00
|
|
|
DynamicCheckerFunctions::DynamicCheckerFunctions ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicCheckerFunctions::~DynamicCheckerFunctions ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DynamicCheckerFunctions::Install(Stream &error_stream,
|
|
|
|
ExecutionContext &exe_ctx)
|
|
|
|
{
|
2010-10-16 06:48:33 +08:00
|
|
|
m_valid_pointer_check.reset(new ClangUtilityFunction(g_valid_pointer_check_text,
|
|
|
|
VALID_POINTER_CHECK_NAME));
|
2010-09-01 08:58:00 +08:00
|
|
|
if (!m_valid_pointer_check->Install(error_stream, exe_ctx))
|
|
|
|
return false;
|
2010-11-04 09:51:38 +08:00
|
|
|
|
2011-09-22 12:58:26 +08:00
|
|
|
Process *process = exe_ctx.GetProcessPtr();
|
|
|
|
|
|
|
|
if (process)
|
2010-11-04 09:51:38 +08:00
|
|
|
{
|
2011-09-22 12:58:26 +08:00
|
|
|
ObjCLanguageRuntime *objc_language_runtime = process->GetObjCLanguageRuntime();
|
2010-11-04 09:51:38 +08:00
|
|
|
|
|
|
|
if (objc_language_runtime)
|
|
|
|
{
|
|
|
|
m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker(VALID_OBJC_OBJECT_CHECK_NAME));
|
|
|
|
|
|
|
|
if (!m_objc_object_check->Install(error_stream, exe_ctx))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-09-01 08:58:00 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-01 10:46:54 +08:00
|
|
|
bool
|
|
|
|
DynamicCheckerFunctions::DoCheckersExplainStop (lldb::addr_t addr, Stream &message)
|
|
|
|
{
|
|
|
|
// FIXME: We have to get the checkers to know why they scotched the call in more detail,
|
|
|
|
// so we can print a better message here.
|
|
|
|
if (m_valid_pointer_check.get() != NULL && m_valid_pointer_check->ContainsAddress(addr))
|
|
|
|
{
|
|
|
|
message.Printf ("Attempted to dereference an invalid pointer.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (m_objc_object_check.get() != NULL && m_objc_object_check->ContainsAddress(addr))
|
|
|
|
{
|
|
|
|
message.Printf ("Attempted to dereference an invalid ObjC Object or send it an unrecognized selector");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-01 08:58:00 +08:00
|
|
|
static std::string
|
|
|
|
PrintValue(llvm::Value *V, bool truncate = false)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
raw_string_ostream rso(s);
|
|
|
|
V->print(rso);
|
|
|
|
rso.flush();
|
|
|
|
if (truncate)
|
|
|
|
s.resize(s.length() - 1);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
/// @class Instrumenter IRDynamicChecks.cpp
|
|
|
|
/// @brief Finds and instruments individual LLVM IR instructions
|
|
|
|
///
|
|
|
|
/// When instrumenting LLVM IR, it is frequently desirable to first search
|
|
|
|
/// for instructions, and then later modify them. This way iterators
|
|
|
|
/// remain intact, and multiple passes can look at the same code base without
|
|
|
|
/// treading on each other's toes.
|
|
|
|
///
|
|
|
|
/// The Instrumenter class implements this functionality. A client first
|
|
|
|
/// calls Inspect on a function, which populates a list of instructions to
|
|
|
|
/// be instrumented. Then, later, when all passes' Inspect functions have
|
|
|
|
/// been called, the client calls Instrument, which adds the desired
|
|
|
|
/// instrumentation.
|
|
|
|
///
|
|
|
|
/// A subclass of Instrumenter must override InstrumentInstruction, which
|
|
|
|
/// is responsible for adding whatever instrumentation is necessary.
|
|
|
|
///
|
|
|
|
/// A subclass of Instrumenter may override:
|
|
|
|
///
|
|
|
|
/// - InspectInstruction [default: does nothing]
|
|
|
|
///
|
|
|
|
/// - InspectBasicBlock [default: iterates through the instructions in a
|
|
|
|
/// basic block calling InspectInstruction]
|
|
|
|
///
|
|
|
|
/// - InspectFunction [default: iterates through the basic blocks in a
|
|
|
|
/// function calling InspectBasicBlock]
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
class Instrumenter {
|
|
|
|
public:
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Constructor
|
|
|
|
///
|
|
|
|
/// @param[in] module
|
|
|
|
/// The module being instrumented.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
Instrumenter (llvm::Module &module,
|
|
|
|
DynamicCheckerFunctions &checker_functions) :
|
|
|
|
m_module(module),
|
2010-09-14 05:34:21 +08:00
|
|
|
m_checker_functions(checker_functions),
|
|
|
|
m_i8ptr_ty(NULL)
|
2010-09-02 08:37:32 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-26 07:55:37 +08:00
|
|
|
virtual~Instrumenter ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Inspect a function to find instructions to instrument
|
|
|
|
///
|
|
|
|
/// @param[in] function
|
|
|
|
/// The function to inspect.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// True on success; false on error.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
bool Inspect (llvm::Function &function)
|
|
|
|
{
|
|
|
|
return InspectFunction(function);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Instrument all the instructions found by Inspect()
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// True on success; false on error.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
bool Instrument ()
|
|
|
|
{
|
|
|
|
for (InstIterator ii = m_to_instrument.begin(), last_ii = m_to_instrument.end();
|
|
|
|
ii != last_ii;
|
|
|
|
++ii)
|
|
|
|
{
|
|
|
|
if (!InstrumentInstruction(*ii))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Add instrumentation to a single instruction
|
|
|
|
///
|
|
|
|
/// @param[in] inst
|
|
|
|
/// The instruction to be instrumented.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// True on success; false otherwise.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0;
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Register a single instruction to be instrumented
|
|
|
|
///
|
|
|
|
/// @param[in] inst
|
|
|
|
/// The instruction to be instrumented.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
void RegisterInstruction(llvm::Instruction &i)
|
|
|
|
{
|
|
|
|
m_to_instrument.push_back(&i);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Determine whether a single instruction is interesting to
|
|
|
|
/// instrument, and, if so, call RegisterInstruction
|
|
|
|
///
|
|
|
|
/// @param[in] i
|
|
|
|
/// The instruction to be inspected.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// False if there was an error scanning; true otherwise.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
virtual bool InspectInstruction(llvm::Instruction &i)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Scan a basic block to see if any instructions are interesting
|
|
|
|
///
|
|
|
|
/// @param[in] bb
|
|
|
|
/// The basic block to be inspected.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// False if there was an error scanning; true otherwise.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
virtual bool InspectBasicBlock(llvm::BasicBlock &bb)
|
|
|
|
{
|
|
|
|
for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end();
|
|
|
|
ii != last_ii;
|
|
|
|
++ii)
|
|
|
|
{
|
|
|
|
if (!InspectInstruction(*ii))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Scan a function to see if any instructions are interesting
|
|
|
|
///
|
|
|
|
/// @param[in] f
|
|
|
|
/// The function to be inspected.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// False if there was an error scanning; true otherwise.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
virtual bool InspectFunction(llvm::Function &f)
|
|
|
|
{
|
|
|
|
for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end();
|
|
|
|
bbi != last_bbi;
|
|
|
|
++bbi)
|
|
|
|
{
|
|
|
|
if (!InspectBasicBlock(*bbi))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Build a function pointer for a function with signature
|
|
|
|
/// void (*)(uint8_t*) with a given address
|
|
|
|
///
|
|
|
|
/// @param[in] start_address
|
|
|
|
/// The address of the function.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// The function pointer, for use in a CallInst.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address)
|
|
|
|
{
|
2011-07-30 10:42:06 +08:00
|
|
|
IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
|
2010-09-14 05:34:21 +08:00
|
|
|
(m_module.getPointerSize() == llvm::Module::Pointer64) ? 64 : 32);
|
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
llvm::Type *param_array[1];
|
|
|
|
|
|
|
|
param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
|
|
|
|
|
|
|
|
ArrayRef<llvm::Type*> params(param_array, 1);
|
2010-09-14 05:34:21 +08:00
|
|
|
|
|
|
|
FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
|
|
|
|
PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
|
|
|
|
Constant *fun_addr_int = ConstantInt::get(intptr_ty, start_address, false);
|
|
|
|
return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
|
|
|
|
}
|
|
|
|
|
2011-10-27 08:02:05 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Build a function pointer for a function with signature
|
|
|
|
/// void (*)(uint8_t*, uint8_t*) with a given address
|
|
|
|
///
|
|
|
|
/// @param[in] start_address
|
|
|
|
/// The address of the function.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// The function pointer, for use in a CallInst.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address)
|
|
|
|
{
|
|
|
|
IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
|
|
|
|
(m_module.getPointerSize() == llvm::Module::Pointer64) ? 64 : 32);
|
|
|
|
|
|
|
|
llvm::Type *param_array[2];
|
|
|
|
|
|
|
|
param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
|
|
|
|
param_array[1] = const_cast<llvm::PointerType*>(GetI8PtrTy());
|
|
|
|
|
|
|
|
ArrayRef<llvm::Type*> params(param_array, 2);
|
|
|
|
|
|
|
|
FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
|
|
|
|
PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
|
|
|
|
Constant *fun_addr_int = ConstantInt::get(intptr_ty, start_address, false);
|
|
|
|
return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
|
|
|
|
}
|
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
PointerType *GetI8PtrTy()
|
2010-11-04 09:51:38 +08:00
|
|
|
{
|
|
|
|
if (!m_i8ptr_ty)
|
|
|
|
m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext());
|
|
|
|
|
|
|
|
return m_i8ptr_ty;
|
|
|
|
}
|
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
typedef std::vector <llvm::Instruction *> InstVector;
|
|
|
|
typedef InstVector::iterator InstIterator;
|
|
|
|
|
|
|
|
InstVector m_to_instrument; ///< List of instructions the inspector found
|
|
|
|
llvm::Module &m_module; ///< The module which is being instrumented
|
|
|
|
DynamicCheckerFunctions &m_checker_functions; ///< The dynamic checker functions for the process
|
2010-11-04 09:51:38 +08:00
|
|
|
private:
|
2011-07-30 10:42:06 +08:00
|
|
|
PointerType *m_i8ptr_ty;
|
2010-09-02 08:37:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ValidPointerChecker : public Instrumenter
|
|
|
|
{
|
|
|
|
public:
|
2011-01-26 07:55:37 +08:00
|
|
|
ValidPointerChecker (llvm::Module &module,
|
|
|
|
DynamicCheckerFunctions &checker_functions) :
|
2010-09-02 08:37:32 +08:00
|
|
|
Instrumenter(module, checker_functions),
|
|
|
|
m_valid_pointer_check_func(NULL)
|
|
|
|
{
|
|
|
|
}
|
2011-01-26 07:55:37 +08:00
|
|
|
|
|
|
|
virtual ~ValidPointerChecker ()
|
|
|
|
{
|
|
|
|
}
|
2010-09-02 08:37:32 +08:00
|
|
|
private:
|
|
|
|
bool InstrumentInstruction(llvm::Instruction *inst)
|
|
|
|
{
|
2010-11-06 09:53:30 +08:00
|
|
|
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
2010-09-02 08:37:32 +08:00
|
|
|
|
The implementation of categories is now synchronization safe
Code cleanup:
- The Format Manager implementation is now split between two files: FormatClasses.{h|cpp} where the
actual formatter classes (ValueFormat, SummaryFormat, ...) are implemented and
FormatManager.{h|cpp} where the infrastructure classes (FormatNavigator, FormatManager, ...)
are contained. The wrapper code always remains in Debugger.{h|cpp}
- Several leftover fields, methods and comments from previous design choices have been removed
type category subcommands (enable, disable, delete) now can take a list of category names as input
- for type category enable, saying "enable A B C" is the same as saying
enable C
enable B
enable A
(the ordering is relevant in enabling categories, and it is expected that a user typing
enable A B C wants to look into category A, then into B, then into C and not the other
way round)
- for the other two commands, the order is not really relevant (however, the same inverted ordering
is used for consistency)
llvm-svn: 135494
2011-07-20 02:03:25 +08:00
|
|
|
if (log)
|
2010-09-02 08:37:32 +08:00
|
|
|
log->Printf("Instrumenting load/store instruction: %s\n",
|
|
|
|
PrintValue(inst).c_str());
|
|
|
|
|
|
|
|
if (!m_valid_pointer_check_func)
|
2010-09-14 05:34:21 +08:00
|
|
|
m_valid_pointer_check_func = BuildPointerValidatorFunc(m_checker_functions.m_valid_pointer_check->StartAddress());
|
2010-09-02 08:37:32 +08:00
|
|
|
|
2011-07-08 08:39:14 +08:00
|
|
|
llvm::Value *dereferenced_ptr = NULL;
|
2010-09-02 08:37:32 +08:00
|
|
|
|
|
|
|
if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst> (inst))
|
|
|
|
dereferenced_ptr = li->getPointerOperand();
|
|
|
|
else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst> (inst))
|
|
|
|
dereferenced_ptr = si->getPointerOperand();
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Insert an instruction to cast the loaded value to int8_t*
|
|
|
|
|
|
|
|
BitCastInst *bit_cast = new BitCastInst(dereferenced_ptr,
|
2010-11-04 09:51:38 +08:00
|
|
|
GetI8PtrTy(),
|
2010-09-02 08:37:32 +08:00
|
|
|
"",
|
|
|
|
inst);
|
|
|
|
|
|
|
|
// Insert an instruction to call the helper with the result
|
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
llvm::Value *arg_array[1];
|
|
|
|
|
|
|
|
arg_array[0] = bit_cast;
|
|
|
|
|
|
|
|
llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
|
2010-09-02 08:37:32 +08:00
|
|
|
|
|
|
|
CallInst::Create(m_valid_pointer_check_func,
|
2011-07-30 10:42:06 +08:00
|
|
|
args,
|
2010-09-02 08:37:32 +08:00
|
|
|
"",
|
|
|
|
inst);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InspectInstruction(llvm::Instruction &i)
|
|
|
|
{
|
|
|
|
if (dyn_cast<llvm::LoadInst> (&i) ||
|
|
|
|
dyn_cast<llvm::StoreInst> (&i))
|
|
|
|
RegisterInstruction(i);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *m_valid_pointer_check_func;
|
2010-09-14 05:34:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ObjcObjectChecker : public Instrumenter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ObjcObjectChecker(llvm::Module &module,
|
|
|
|
DynamicCheckerFunctions &checker_functions) :
|
|
|
|
Instrumenter(module, checker_functions),
|
|
|
|
m_objc_object_check_func(NULL)
|
|
|
|
{
|
|
|
|
}
|
2011-01-26 07:55:37 +08:00
|
|
|
|
|
|
|
virtual
|
|
|
|
~ObjcObjectChecker ()
|
|
|
|
{
|
|
|
|
}
|
2011-11-16 08:20:50 +08:00
|
|
|
|
|
|
|
enum msgSend_type
|
|
|
|
{
|
|
|
|
eMsgSend = 0,
|
|
|
|
eMsgSendSuper,
|
|
|
|
eMsgSendSuper_stret,
|
|
|
|
eMsgSend_fpret,
|
|
|
|
eMsgSend_stret
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map <llvm::Instruction *, msgSend_type> msgSend_types;
|
2011-01-26 07:55:37 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
private:
|
|
|
|
bool InstrumentInstruction(llvm::Instruction *inst)
|
|
|
|
{
|
|
|
|
CallInst *call_inst = dyn_cast<CallInst>(inst);
|
|
|
|
|
|
|
|
if (!call_inst)
|
2010-11-04 09:51:38 +08:00
|
|
|
return false; // call_inst really shouldn't be NULL, because otherwise InspectInstruction wouldn't have registered it
|
2010-09-14 05:34:21 +08:00
|
|
|
|
|
|
|
if (!m_objc_object_check_func)
|
2011-10-27 08:02:05 +08:00
|
|
|
m_objc_object_check_func = BuildObjectCheckerFunc(m_checker_functions.m_objc_object_check->StartAddress());
|
2010-09-14 05:34:21 +08:00
|
|
|
|
|
|
|
// id objc_msgSend(id theReceiver, SEL theSelector, ...)
|
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
llvm::Value *target_object;
|
|
|
|
llvm::Value *selector;
|
|
|
|
|
|
|
|
switch (msgSend_types[inst])
|
|
|
|
{
|
|
|
|
case eMsgSend:
|
|
|
|
case eMsgSend_fpret:
|
|
|
|
target_object = call_inst->getArgOperand(0);
|
|
|
|
selector = call_inst->getArgOperand(1);
|
|
|
|
break;
|
|
|
|
case eMsgSend_stret:
|
|
|
|
target_object = call_inst->getArgOperand(1);
|
|
|
|
selector = call_inst->getArgOperand(2);
|
|
|
|
case eMsgSendSuper:
|
|
|
|
case eMsgSendSuper_stret:
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-27 08:02:05 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
// Insert an instruction to cast the receiver id to int8_t*
|
|
|
|
|
|
|
|
BitCastInst *bit_cast = new BitCastInst(target_object,
|
2010-11-04 09:51:38 +08:00
|
|
|
GetI8PtrTy(),
|
2010-09-14 05:34:21 +08:00
|
|
|
"",
|
|
|
|
inst);
|
|
|
|
|
|
|
|
// Insert an instruction to call the helper with the result
|
|
|
|
|
2011-10-27 08:02:05 +08:00
|
|
|
llvm::Value *arg_array[2];
|
2011-07-30 10:42:06 +08:00
|
|
|
|
|
|
|
arg_array[0] = bit_cast;
|
2011-10-27 08:02:05 +08:00
|
|
|
arg_array[1] = selector;
|
2011-07-30 10:42:06 +08:00
|
|
|
|
2011-11-01 06:11:40 +08:00
|
|
|
ArrayRef<llvm::Value*> args(arg_array, 2);
|
2010-09-14 05:34:21 +08:00
|
|
|
|
|
|
|
CallInst::Create(m_objc_object_check_func,
|
2011-07-30 10:42:06 +08:00
|
|
|
args,
|
2010-09-14 05:34:21 +08:00
|
|
|
"",
|
|
|
|
inst);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InspectInstruction(llvm::Instruction &i)
|
|
|
|
{
|
2010-11-06 09:53:30 +08:00
|
|
|
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
2010-09-14 05:34:21 +08:00
|
|
|
|
|
|
|
CallInst *call_inst = dyn_cast<CallInst>(&i);
|
|
|
|
|
|
|
|
if (call_inst)
|
|
|
|
{
|
|
|
|
// This metadata is set by IRForTarget::MaybeHandleCall().
|
|
|
|
|
|
|
|
MDNode *metadata = call_inst->getMetadata("lldb.call.realName");
|
|
|
|
|
|
|
|
if (!metadata)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (metadata->getNumOperands() != 1)
|
|
|
|
{
|
|
|
|
if (log)
|
|
|
|
log->Printf("Function call metadata has %d operands for [%p] %s", metadata->getNumOperands(), call_inst, PrintValue(call_inst).c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantArray *real_name = dyn_cast<ConstantArray>(metadata->getOperand(0));
|
|
|
|
|
|
|
|
if (!real_name)
|
|
|
|
{
|
|
|
|
if (log)
|
|
|
|
log->Printf("Function call metadata is not a ConstantArray for [%p] %s", call_inst, PrintValue(call_inst).c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!real_name->isString())
|
|
|
|
{
|
|
|
|
if (log)
|
|
|
|
log->Printf("Function call metadata is not a string for [%p] %s", call_inst, PrintValue(call_inst).c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (log)
|
|
|
|
log->Printf("Found call to %s: %s\n", real_name->getAsString().c_str(), PrintValue(call_inst).c_str());
|
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
std::string name_str = real_name->getAsString();
|
|
|
|
const char* name_cstr = name_str.c_str();
|
|
|
|
|
|
|
|
if (name_str.find("objc_msgSend") == std::string::npos)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!strcmp(name_cstr, "objc_msgSend"))
|
|
|
|
{
|
2010-09-14 05:34:21 +08:00
|
|
|
RegisterInstruction(i);
|
2011-11-16 08:20:50 +08:00
|
|
|
msgSend_types[&i] = eMsgSend;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name_cstr, "objc_msgSend_stret"))
|
|
|
|
{
|
|
|
|
RegisterInstruction(i);
|
|
|
|
msgSend_types[&i] = eMsgSend_stret;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name_cstr, "objc_msgSend_fpret"))
|
|
|
|
{
|
|
|
|
RegisterInstruction(i);
|
|
|
|
msgSend_types[&i] = eMsgSend_fpret;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name_cstr, "objc_msgSendSuper"))
|
|
|
|
{
|
|
|
|
RegisterInstruction(i);
|
|
|
|
msgSend_types[&i] = eMsgSendSuper;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name_cstr, "objc_msgSendSuper_stret"))
|
|
|
|
{
|
|
|
|
RegisterInstruction(i);
|
|
|
|
msgSend_types[&i] = eMsgSendSuper_stret;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (log)
|
|
|
|
log->Printf("Function name '%s' contains 'objc_msgSend' but is not handled", name_str.c_str());
|
|
|
|
|
|
|
|
return true;
|
2010-09-14 05:34:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *m_objc_object_check_func;
|
2010-09-02 08:37:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
|
|
|
|
const char *func_name) :
|
2010-09-23 11:01:22 +08:00
|
|
|
ModulePass(ID),
|
2011-04-12 03:41:40 +08:00
|
|
|
m_func_name(func_name),
|
|
|
|
m_checker_functions(checker_functions)
|
2010-09-02 08:37:32 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-01 08:58:00 +08:00
|
|
|
IRDynamicChecks::~IRDynamicChecks()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRDynamicChecks::runOnModule(llvm::Module &M)
|
|
|
|
{
|
2010-11-06 09:53:30 +08:00
|
|
|
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
2010-09-01 08:58:00 +08:00
|
|
|
|
|
|
|
llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str()));
|
|
|
|
|
|
|
|
if (!function)
|
|
|
|
{
|
|
|
|
if (log)
|
|
|
|
log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-02 08:37:32 +08:00
|
|
|
|
|
|
|
ValidPointerChecker vpc(M, m_checker_functions);
|
2010-09-01 08:58:00 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
if (!vpc.Inspect(*function))
|
|
|
|
return false;
|
2010-09-01 08:58:00 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
if (!vpc.Instrument())
|
|
|
|
return false;
|
2010-09-01 08:58:00 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
ObjcObjectChecker ooc(M, m_checker_functions);
|
|
|
|
|
|
|
|
if (!ooc.Inspect(*function))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!ooc.Instrument())
|
|
|
|
return false;
|
2011-01-22 09:25:40 +08:00
|
|
|
|
|
|
|
if (log && log->GetVerbose())
|
2010-09-09 04:04:08 +08:00
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
raw_string_ostream oss(s);
|
|
|
|
|
|
|
|
M.print(oss, NULL);
|
|
|
|
|
|
|
|
oss.flush();
|
|
|
|
|
2011-01-22 09:25:40 +08:00
|
|
|
log->Printf ("Module after dynamic checks: \n%s", s.c_str());
|
2010-09-09 04:04:08 +08:00
|
|
|
}
|
|
|
|
|
2010-09-01 08:58:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IRDynamicChecks::assignPassManager(PMStack &PMS,
|
|
|
|
PassManagerType T)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PassManagerType
|
|
|
|
IRDynamicChecks::getPotentialPassManagerType() const
|
|
|
|
{
|
|
|
|
return PMT_ModulePassManager;
|
|
|
|
}
|