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"
|
2013-11-04 17:33:30 +08:00
|
|
|
#include "lldb/Target/StackFrame.h"
|
2010-09-01 08:58:00 +08:00
|
|
|
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-01-02 20:20:07 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2013-12-21 03:55:02 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2013-01-02 20:20:07 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2010-09-01 08:58:00 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-07-07 01:54:58 +08:00
|
|
|
static const char g_valid_pointer_check_text[] =
|
2010-10-16 06:48:33 +08:00
|
|
|
"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;
|
2014-07-07 01:54:58 +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();
|
2014-07-07 01:54:58 +08:00
|
|
|
|
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));
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-11-04 09:51:38 +08:00
|
|
|
if (!m_objc_object_check->Install(error_stream, exe_ctx))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-07 01:54:58 +08:00
|
|
|
static std::string
|
2010-09-01 08:58:00 +08:00
|
|
|
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]
|
|
|
|
///
|
2014-07-07 01:54:58 +08:00
|
|
|
/// - InspectBasicBlock [default: iterates through the instructions in a
|
2010-09-02 08:37:32 +08:00
|
|
|
/// basic block calling InspectInstruction]
|
|
|
|
///
|
2014-07-07 01:54:58 +08:00
|
|
|
/// - InspectFunction [default: iterates through the basic blocks in a
|
2010-09-02 08:37:32 +08:00
|
|
|
/// 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),
|
2013-12-21 03:55:02 +08:00
|
|
|
m_i8ptr_ty(NULL),
|
|
|
|
m_intptr_ty(NULL)
|
2010-09-02 08:37:32 +08:00
|
|
|
{
|
|
|
|
}
|
2014-07-07 01:54:58 +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);
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// 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;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Add instrumentation to a single instruction
|
|
|
|
///
|
|
|
|
/// @param[in] inst
|
2014-07-07 01:54:58 +08:00
|
|
|
/// The instruction to be instrumented.
|
2010-09-02 08:37:32 +08:00
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// True on success; false otherwise.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0;
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// 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);
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// 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;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// 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;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Scan a function to see if any instructions are interesting
|
|
|
|
///
|
|
|
|
/// @param[in] f
|
2014-07-07 01:54:58 +08:00
|
|
|
/// The function to be inspected.
|
2010-09-02 08:37:32 +08:00
|
|
|
///
|
|
|
|
/// @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;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
//------------------------------------------------------------------
|
2014-07-07 01:54:58 +08:00
|
|
|
/// Build a function pointer for a function with signature
|
2010-09-14 05:34:21 +08:00
|
|
|
/// 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
|
|
|
llvm::Type *param_array[1];
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
ArrayRef<llvm::Type*> params(param_array, 1);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
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);
|
2013-12-21 03:55:02 +08:00
|
|
|
Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false);
|
2010-09-14 05:34:21 +08:00
|
|
|
return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-10-27 08:02:05 +08:00
|
|
|
//------------------------------------------------------------------
|
2014-07-07 01:54:58 +08:00
|
|
|
/// Build a function pointer for a function with signature
|
2011-10-27 08:02:05 +08:00
|
|
|
/// 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)
|
|
|
|
{
|
|
|
|
llvm::Type *param_array[2];
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-10-27 08:02:05 +08:00
|
|
|
param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
|
|
|
|
param_array[1] = const_cast<llvm::PointerType*>(GetI8PtrTy());
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-10-27 08:02:05 +08:00
|
|
|
ArrayRef<llvm::Type*> params(param_array, 2);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-10-27 08:02:05 +08:00
|
|
|
FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
|
|
|
|
PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
|
2013-12-21 03:55:02 +08:00
|
|
|
Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false);
|
2011-10-27 08:02:05 +08:00
|
|
|
return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
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());
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-11-04 09:51:38 +08:00
|
|
|
return m_i8ptr_ty;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2013-12-21 03:55:02 +08:00
|
|
|
IntegerType *GetIntptrTy()
|
|
|
|
{
|
|
|
|
if (!m_intptr_ty)
|
|
|
|
{
|
|
|
|
llvm::DataLayout data_layout(&m_module);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2013-12-21 03:55:02 +08:00
|
|
|
m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), data_layout.getPointerSizeInBits());
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2013-12-21 03:55:02 +08:00
|
|
|
return m_intptr_ty;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
typedef std::vector <llvm::Instruction *> InstVector;
|
|
|
|
typedef InstVector::iterator InstIterator;
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
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;
|
2013-12-21 03:55:02 +08:00
|
|
|
IntegerType *m_intptr_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)
|
|
|
|
{
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-01-26 07:55:37 +08:00
|
|
|
virtual ~ValidPointerChecker ()
|
|
|
|
{
|
|
|
|
}
|
2010-09-02 08:37:32 +08:00
|
|
|
private:
|
|
|
|
bool InstrumentInstruction(llvm::Instruction *inst)
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *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)
|
2014-07-07 01:54:58 +08:00
|
|
|
log->Printf("Instrumenting load/store instruction: %s\n",
|
2010-09-02 08:37:32 +08:00
|
|
|
PrintValue(inst).c_str());
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
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());
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-07-08 08:39:14 +08:00
|
|
|
llvm::Value *dereferenced_ptr = NULL;
|
2014-07-07 01:54:58 +08:00
|
|
|
|
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;
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
// Insert an instruction to cast the loaded value to int8_t*
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
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);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
// Insert an instruction to call the helper with the result
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
llvm::Value *arg_array[1];
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
arg_array[0] = bit_cast;
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
|
2014-07-07 01:54:58 +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);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
bool InspectInstruction(llvm::Instruction &i)
|
|
|
|
{
|
|
|
|
if (dyn_cast<llvm::LoadInst> (&i) ||
|
|
|
|
dyn_cast<llvm::StoreInst> (&i))
|
|
|
|
RegisterInstruction(i);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-02 08:37:32 +08:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-01-26 07:55:37 +08:00
|
|
|
virtual
|
|
|
|
~ObjcObjectChecker ()
|
|
|
|
{
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
enum msgSend_type
|
|
|
|
{
|
|
|
|
eMsgSend = 0,
|
|
|
|
eMsgSendSuper,
|
|
|
|
eMsgSendSuper_stret,
|
|
|
|
eMsgSend_fpret,
|
|
|
|
eMsgSend_stret
|
|
|
|
};
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
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);
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
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
|
2014-04-04 12:06:10 +08:00
|
|
|
|
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());
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
// id objc_msgSend(id theReceiver, SEL theSelector, ...)
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
llvm::Value *target_object;
|
|
|
|
llvm::Value *selector;
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
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);
|
2012-07-17 11:23:13 +08:00
|
|
|
break;
|
2011-11-16 08:20:50 +08:00
|
|
|
case eMsgSendSuper:
|
|
|
|
case eMsgSendSuper_stret:
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2012-07-17 11:23:13 +08:00
|
|
|
// These objects should always be valid according to Sean Calannan
|
|
|
|
assert (target_object);
|
|
|
|
assert (selector);
|
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
// Insert an instruction to cast the receiver id to int8_t*
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
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);
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
// Insert an instruction to call the helper with the result
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-10-27 08:02:05 +08:00
|
|
|
llvm::Value *arg_array[2];
|
2014-04-04 12:06:10 +08:00
|
|
|
|
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;
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-01 06:11:40 +08:00
|
|
|
ArrayRef<llvm::Value*> args(arg_array, 2);
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2014-07-07 01:54:58 +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);
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
bool InspectInstruction(llvm::Instruction &i)
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
2010-09-14 05:34:21 +08:00
|
|
|
|
|
|
|
CallInst *call_inst = dyn_cast<CallInst>(&i);
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
if (call_inst)
|
|
|
|
{
|
|
|
|
// This metadata is set by IRForTarget::MaybeHandleCall().
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
MDNode *metadata = call_inst->getMetadata("lldb.call.realName");
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
if (!metadata)
|
|
|
|
return true;
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
if (metadata->getNumOperands() != 1)
|
|
|
|
{
|
|
|
|
if (log)
|
2014-04-04 12:06:10 +08:00
|
|
|
log->Printf("Function call metadata has %d operands for [%p] %s",
|
|
|
|
metadata->getNumOperands(),
|
|
|
|
static_cast<void*>(call_inst),
|
|
|
|
PrintValue(call_inst).c_str());
|
2010-09-14 05:34:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2013-06-04 04:45:54 +08:00
|
|
|
MDString *real_name = dyn_cast<MDString>(metadata->getOperand(0));
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
if (!real_name)
|
|
|
|
{
|
|
|
|
if (log)
|
2014-04-04 12:06:10 +08:00
|
|
|
log->Printf("Function call metadata is not an MDString for [%p] %s",
|
|
|
|
static_cast<void*>(call_inst),
|
|
|
|
PrintValue(call_inst).c_str());
|
2010-09-14 05:34:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-04 04:45:54 +08:00
|
|
|
|
|
|
|
std::string name_str = real_name->getString();
|
2011-11-16 08:20:50 +08:00
|
|
|
const char* name_cstr = name_str.c_str();
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2012-02-09 11:22:41 +08:00
|
|
|
if (log)
|
|
|
|
log->Printf("Found call to %s: %s\n", name_cstr, PrintValue(call_inst).c_str());
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
if (name_str.find("objc_msgSend") == std::string::npos)
|
|
|
|
return true;
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
if (!strcmp(name_cstr, "objc_msgSend_stret"))
|
|
|
|
{
|
|
|
|
RegisterInstruction(i);
|
|
|
|
msgSend_types[&i] = eMsgSend_stret;
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
if (!strcmp(name_cstr, "objc_msgSend_fpret"))
|
|
|
|
{
|
|
|
|
RegisterInstruction(i);
|
|
|
|
msgSend_types[&i] = eMsgSend_fpret;
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
if (!strcmp(name_cstr, "objc_msgSendSuper"))
|
|
|
|
{
|
|
|
|
RegisterInstruction(i);
|
|
|
|
msgSend_types[&i] = eMsgSendSuper;
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
if (!strcmp(name_cstr, "objc_msgSendSuper_stret"))
|
|
|
|
{
|
|
|
|
RegisterInstruction(i);
|
|
|
|
msgSend_types[&i] = eMsgSendSuper_stret;
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
if (log)
|
|
|
|
log->Printf("Function name '%s' contains 'objc_msgSend' but is not handled", name_str.c_str());
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2011-11-16 08:20:50 +08:00
|
|
|
return true;
|
2010-09-14 05:34:21 +08:00
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
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)
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-01 08:58:00 +08:00
|
|
|
llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str()));
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-01 08:58:00 +08:00
|
|
|
if (!function)
|
|
|
|
{
|
|
|
|
if (log)
|
|
|
|
log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-01 08:58:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-02 08:37:32 +08:00
|
|
|
|
2012-05-30 07:46:46 +08:00
|
|
|
if (m_checker_functions.m_valid_pointer_check.get())
|
|
|
|
{
|
|
|
|
ValidPointerChecker vpc(M, m_checker_functions);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2012-05-30 07:46:46 +08:00
|
|
|
if (!vpc.Inspect(*function))
|
|
|
|
return false;
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2012-05-30 07:46:46 +08:00
|
|
|
if (!vpc.Instrument())
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2012-05-30 07:46:46 +08:00
|
|
|
if (m_checker_functions.m_objc_object_check.get())
|
|
|
|
{
|
|
|
|
ObjcObjectChecker ooc(M, m_checker_functions);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2012-05-30 07:46:46 +08:00
|
|
|
if (!ooc.Inspect(*function))
|
|
|
|
return false;
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2012-05-30 07:46:46 +08:00
|
|
|
if (!ooc.Instrument())
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
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);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-09 04:04:08 +08:00
|
|
|
M.print(oss, NULL);
|
2014-07-07 01:54:58 +08:00
|
|
|
|
2010-09-09 04:04:08 +08:00
|
|
|
oss.flush();
|
2014-07-07 01:54:58 +08:00
|
|
|
|
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
|
|
|
}
|
2014-07-07 01:54:58 +08:00
|
|
|
|
|
|
|
return true;
|
2010-09-01 08:58:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IRDynamicChecks::assignPassManager(PMStack &PMS,
|
|
|
|
PassManagerType T)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PassManagerType
|
|
|
|
IRDynamicChecks::getPotentialPassManagerType() const
|
|
|
|
{
|
|
|
|
return PMT_ModulePassManager;
|
|
|
|
}
|