On behalf of Dean De Leo

llvm-svn: 248003
This commit is contained in:
Aidan Dodds
2015-09-18 16:49:39 +00:00
parent 6165d31a83
commit 827802871b
2 changed files with 237 additions and 92 deletions

View File

@@ -210,15 +210,64 @@ RenderScriptRuntime::CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bo
const RenderScriptRuntime::HookDefn RenderScriptRuntime::s_runtimeHookDefns[] =
{
//rsdScript
{"rsdScriptInit", "_Z13rsdScriptInitPKN7android12renderscript7ContextEPNS0_7ScriptCEPKcS7_PKhjj", 0, RenderScriptRuntime::eModuleKindDriver, &lldb_private::RenderScriptRuntime::CaptureScriptInit1},
{"rsdScriptInvokeForEach", "_Z22rsdScriptInvokeForEachPKN7android12renderscript7ContextEPNS0_6ScriptEjPKNS0_10AllocationEPS6_PKvjPK12RsScriptCall", 0, RenderScriptRuntime::eModuleKindDriver, nullptr},
{"rsdScriptInvokeForEachMulti", "_Z27rsdScriptInvokeForEachMultiPKN7android12renderscript7ContextEPNS0_6ScriptEjPPKNS0_10AllocationEjPS6_PKvjPK12RsScriptCall", 0, RenderScriptRuntime::eModuleKindDriver, nullptr},
{"rsdScriptInvokeFunction", "_Z23rsdScriptInvokeFunctionPKN7android12renderscript7ContextEPNS0_6ScriptEjPKvj", 0, RenderScriptRuntime::eModuleKindDriver, nullptr},
{"rsdScriptSetGlobalVar", "_Z21rsdScriptSetGlobalVarPKN7android12renderscript7ContextEPKNS0_6ScriptEjPvj", 0, RenderScriptRuntime::eModuleKindDriver, &lldb_private::RenderScriptRuntime::CaptureSetGlobalVar1},
{
"rsdScriptInit", //name
"_Z13rsdScriptInitPKN7android12renderscript7ContextEPNS0_7ScriptCEPKcS7_PKhjj", // symbol name 32 bit
"_Z13rsdScriptInitPKN7android12renderscript7ContextEPNS0_7ScriptCEPKcS7_PKhmj", // symbol name 64 bit
0, // version
RenderScriptRuntime::eModuleKindDriver, // type
&lldb_private::RenderScriptRuntime::CaptureScriptInit1 // handler
},
{
"rsdScriptInvokeForEach", // name
"_Z22rsdScriptInvokeForEachPKN7android12renderscript7ContextEPNS0_6ScriptEjPKNS0_10AllocationEPS6_PKvjPK12RsScriptCall", // symbol name 32bit
"_Z22rsdScriptInvokeForEachPKN7android12renderscript7ContextEPNS0_6ScriptEjPKNS0_10AllocationEPS6_PKvmPK12RsScriptCall", // symbol name 64bit
0, // version
RenderScriptRuntime::eModuleKindDriver, // type
nullptr // handler
},
{
"rsdScriptInvokeForEachMulti", // name
"_Z27rsdScriptInvokeForEachMultiPKN7android12renderscript7ContextEPNS0_6ScriptEjPPKNS0_10AllocationEjPS6_PKvjPK12RsScriptCall", // symbol name 32bit
"_Z27rsdScriptInvokeForEachMultiPKN7android12renderscript7ContextEPNS0_6ScriptEjPPKNS0_10AllocationEmPS6_PKvmPK12RsScriptCall", // symbol name 64bit
0, // version
RenderScriptRuntime::eModuleKindDriver, // type
nullptr // handler
},
{
"rsdScriptInvokeFunction", // name
"_Z23rsdScriptInvokeFunctionPKN7android12renderscript7ContextEPNS0_6ScriptEjPKvj", // symbol name 32bit
"_Z23rsdScriptInvokeFunctionPKN7android12renderscript7ContextEPNS0_6ScriptEjPKvm", // symbol name 64bit
0, // version
RenderScriptRuntime::eModuleKindDriver, // type
nullptr // handler
},
{
"rsdScriptSetGlobalVar", // name
"_Z21rsdScriptSetGlobalVarPKN7android12renderscript7ContextEPKNS0_6ScriptEjPvj", // symbol name 32bit
"_Z21rsdScriptSetGlobalVarPKN7android12renderscript7ContextEPKNS0_6ScriptEjPvm", // symbol name 64bit
0, // version
RenderScriptRuntime::eModuleKindDriver, // type
&lldb_private::RenderScriptRuntime::CaptureSetGlobalVar1 // handler
},
//rsdAllocation
{"rsdAllocationInit", "_Z17rsdAllocationInitPKN7android12renderscript7ContextEPNS0_10AllocationEb", 0, RenderScriptRuntime::eModuleKindDriver, &lldb_private::RenderScriptRuntime::CaptureAllocationInit1},
{"rsdAllocationRead2D", "_Z19rsdAllocationRead2DPKN7android12renderscript7ContextEPKNS0_10AllocationEjjj23RsAllocationCubemapFacejjPvjj", 0, RenderScriptRuntime::eModuleKindDriver, nullptr},
{
"rsdAllocationInit", // name
"_Z17rsdAllocationInitPKN7android12renderscript7ContextEPNS0_10AllocationEb", // symbol name 32bit
"_Z17rsdAllocationInitPKN7android12renderscript7ContextEPNS0_10AllocationEb", // symbol name 64bit
0, // version
RenderScriptRuntime::eModuleKindDriver, // type
&lldb_private::RenderScriptRuntime::CaptureAllocationInit1 // handler
},
{
"rsdAllocationRead2D", //name
"_Z19rsdAllocationRead2DPKN7android12renderscript7ContextEPKNS0_10AllocationEjjj23RsAllocationCubemapFacejjPvjj", // symbol name 32bit
"_Z19rsdAllocationRead2DPKN7android12renderscript7ContextEPKNS0_10AllocationEjjj23RsAllocationCubemapFacejjPvmm", // symbol name 64bit
0, // version
RenderScriptRuntime::eModuleKindDriver, // type
nullptr // handler
},
};
const size_t RenderScriptRuntime::s_runtimeHookCount = sizeof(s_runtimeHookDefns)/sizeof(s_runtimeHookDefns[0]);
@@ -242,7 +291,7 @@ RenderScriptRuntime::HookCallback(RuntimeHook* hook_info, ExecutionContext& cont
{
Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
if(log)
if (log)
log->Printf ("RenderScriptRuntime::HookCallback - '%s' .", hook_info->defn->name);
if (hook_info->defn->grabber)
@@ -253,54 +302,115 @@ RenderScriptRuntime::HookCallback(RuntimeHook* hook_info, ExecutionContext& cont
bool
RenderScriptRuntime::GetArg32Simple(ExecutionContext& context, uint32_t arg, uint32_t *data)
RenderScriptRuntime::GetArgSimple(ExecutionContext &context, uint32_t arg, uint64_t *data)
{
Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
if (!data)
return false;
Log* log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
Error error;
RegisterContext* reg_ctx = context.GetRegisterContext();
Process* process = context.GetProcessPtr();
bool success = false; // return value
if (context.GetTargetPtr()->GetArchitecture().GetMachine() == llvm::Triple::ArchType::x86)
if (!context.GetTargetPtr())
{
uint64_t sp = reg_ctx->GetSP();
{
uint32_t offset = (1 + arg) * sizeof(uint32_t);
process->ReadMemory(sp + offset, data, sizeof(uint32_t), error);
if(error.Fail())
{
if(log)
log->Printf ("RenderScriptRuntime:: GetArg32Simple - error reading X86 stack: %s.", error.AsCString());
}
}
if (log)
log->Printf("RenderScriptRuntime::GetArgSimple - Invalid target");
return false;
}
else if (context.GetTargetPtr()->GetArchitecture().GetMachine() == llvm::Triple::ArchType::arm)
switch (context.GetTargetPtr()->GetArchitecture().GetMachine())
{
if (arg < 4)
{
const RegisterInfo* rArg = reg_ctx->GetRegisterInfoAtIndex(arg);
RegisterValue rVal;
reg_ctx->ReadRegister(rArg, rVal);
(*data) = rVal.GetAsUInt32();
}
else
case llvm::Triple::ArchType::x86:
{
uint64_t sp = reg_ctx->GetSP();
uint32_t offset = (1 + arg) * sizeof(uint32_t);
uint32_t result = 0;
process->ReadMemory(sp + offset, &result, sizeof(uint32_t), error);
if (error.Fail())
{
uint32_t offset = (arg-4) * sizeof(uint32_t);
process->ReadMemory(sp + offset, &data, sizeof(uint32_t), error);
if(error.Fail())
if (log)
log->Printf ("RenderScriptRuntime:: GetArgSimple - error reading X86 stack: %s.", error.AsCString());
}
else
{
*data = result;
success = true;
}
break;
}
case llvm::Triple::ArchType::arm:
{
// arm 32 bit
if (arg < 4)
{
const RegisterInfo* rArg = reg_ctx->GetRegisterInfoAtIndex(arg);
RegisterValue rVal;
reg_ctx->ReadRegister(rArg, rVal);
(*data) = rVal.GetAsUInt32();
success = true;
}
else
{
uint64_t sp = reg_ctx->GetSP();
{
if(log)
log->Printf ("RenderScriptRuntime:: GetArg32Simple - error reading ARM stack: %s.", error.AsCString());
uint32_t offset = (arg-4) * sizeof(uint32_t);
process->ReadMemory(sp + offset, &data, sizeof(uint32_t), error);
if (error.Fail())
{
if (log)
log->Printf ("RenderScriptRuntime:: GetArgSimple - error reading ARM stack: %s.", error.AsCString());
}
else
{
success = true;
}
}
}
}
break;
}
case llvm::Triple::ArchType::aarch64:
{
// arm 64 bit
// first 8 arguments are in the registers
if (arg < 8)
{
const RegisterInfo* rArg = reg_ctx->GetRegisterInfoAtIndex(arg);
RegisterValue rVal;
success = reg_ctx->ReadRegister(rArg, rVal);
if (success)
{
*data = rVal.GetAsUInt64();
}
else
{
if (log)
log->Printf("RenderScriptRuntime::GetArgSimple() - AARCH64 - Error while reading the argument #%d", arg);
}
}
else
{
// @TODO: need to find the argument in the stack
if (log)
log->Printf("RenderScriptRuntime::GetArgSimple - AARCH64 - FOR #ARG >= 8 NOT IMPLEMENTED YET. Argument number: %d", arg);
}
break;
}
default:
{
// invalid architecture
if (log)
log->Printf("RenderScriptRuntime::GetArgSimple - Architecture not supported");
}
}
return true;
return success;
}
void
@@ -310,35 +420,38 @@ RenderScriptRuntime::CaptureSetGlobalVar1(RuntimeHook* hook_info, ExecutionConte
//Context, Script, int, data, length
Error error;
uint32_t rs_context_u32 = 0U;
uint32_t rs_script_u32 = 0U;
uint32_t rs_id_u32 = 0U;
uint32_t rs_data_u32 = 0U;
uint32_t rs_length_u32 = 0U;
uint64_t rs_context_u64 = 0U;
uint64_t rs_script_u64 = 0U;
uint64_t rs_id_u64 = 0U;
uint64_t rs_data_u64 = 0U;
uint64_t rs_length_u64 = 0U;
std::string resname;
std::string cachedir;
bool success =
GetArgSimple(context, 0, &rs_context_u64) &&
GetArgSimple(context, 1, &rs_script_u64) &&
GetArgSimple(context, 2, &rs_id_u64) &&
GetArgSimple(context, 3, &rs_data_u64) &&
GetArgSimple(context, 4, &rs_length_u64);
GetArg32Simple(context, 0, &rs_context_u32);
GetArg32Simple(context, 1, &rs_script_u32);
GetArg32Simple(context, 2, &rs_id_u32);
GetArg32Simple(context, 3, &rs_data_u32);
GetArg32Simple(context, 4, &rs_length_u32);
if (!success)
{
if (log)
log->Printf("RenderScriptRuntime::CaptureSetGlobalVar1 - Error while reading the function parameters");
return;
}
if(log)
if (log)
{
log->Printf ("RenderScriptRuntime::CaptureSetGlobalVar1 - 0x%" PRIx64 ",0x%" PRIx64 " slot %" PRIu64 " = 0x%" PRIx64 ":%" PRIu64 "bytes.",
(uint64_t)rs_context_u32, (uint64_t)rs_script_u32, (uint64_t)rs_id_u32, (uint64_t)rs_data_u32, (uint64_t)rs_length_u32);
rs_context_u64, rs_script_u64, rs_id_u64, rs_data_u64, rs_length_u64);
addr_t script_addr = (addr_t)rs_script_u32;
addr_t script_addr = (addr_t)rs_script_u64;
if (m_scriptMappings.find( script_addr ) != m_scriptMappings.end())
{
auto rsm = m_scriptMappings[script_addr];
if (rs_id_u32 < rsm->m_globals.size())
if (rs_id_u64 < rsm->m_globals.size())
{
auto rsg = rsm->m_globals[rs_id_u32];
auto rsg = rsm->m_globals[rs_id_u64];
log->Printf ("RenderScriptRuntime::CaptureSetGlobalVar1 - Setting of '%s' within '%s' inferred", rsg.m_name.AsCString(),
rsm->m_module->GetFileSpec().GetFilename().AsCString());
}
@@ -353,19 +466,24 @@ RenderScriptRuntime::CaptureAllocationInit1(RuntimeHook* hook_info, ExecutionCon
//Context, Alloc, bool
Error error;
uint32_t rs_context_u32 = 0U;
uint32_t rs_alloc_u32 = 0U;
uint32_t rs_forceZero_u32 = 0U;
uint64_t rs_context_u64 = 0U;
uint64_t rs_alloc_u64 = 0U;
uint64_t rs_forceZero_u64 = 0U;
GetArg32Simple(context, 0, &rs_context_u32);
GetArg32Simple(context, 1, &rs_alloc_u32);
GetArg32Simple(context, 2, &rs_forceZero_u32);
if(log)
bool success =
GetArgSimple(context, 0, &rs_context_u64) &&
GetArgSimple(context, 1, &rs_alloc_u64) &&
GetArgSimple(context, 2, &rs_forceZero_u64);
if (!success) // error case
{
if (log)
log->Printf("RenderScriptRuntime::CaptureAllocationInit1 - Error while reading the function parameters");
return; // abort
}
if (log)
log->Printf ("RenderScriptRuntime::CaptureAllocationInit1 - 0x%" PRIx64 ",0x%" PRIx64 ",0x%" PRIx64 " .",
(uint64_t)rs_context_u32, (uint64_t)rs_alloc_u32, (uint64_t)rs_forceZero_u32);
rs_context_u64, rs_alloc_u64, rs_forceZero_u64);
}
void
@@ -377,37 +495,46 @@ RenderScriptRuntime::CaptureScriptInit1(RuntimeHook* hook_info, ExecutionContext
Error error;
Process* process = context.GetProcessPtr();
uint32_t rs_context_u32 = 0U;
uint32_t rs_script_u32 = 0U;
uint32_t rs_resnameptr_u32 = 0U;
uint32_t rs_cachedirptr_u32 = 0U;
uint64_t rs_context_u64 = 0U;
uint64_t rs_script_u64 = 0U;
uint64_t rs_resnameptr_u64 = 0U;
uint64_t rs_cachedirptr_u64 = 0U;
std::string resname;
std::string cachedir;
GetArg32Simple(context, 0, &rs_context_u32);
GetArg32Simple(context, 1, &rs_script_u32);
GetArg32Simple(context, 2, &rs_resnameptr_u32);
GetArg32Simple(context, 3, &rs_cachedirptr_u32);
// read the function parameters
bool success =
GetArgSimple(context, 0, &rs_context_u64) &&
GetArgSimple(context, 1, &rs_script_u64) &&
GetArgSimple(context, 2, &rs_resnameptr_u64) &&
GetArgSimple(context, 3, &rs_cachedirptr_u64);
process->ReadCStringFromMemory((lldb::addr_t)rs_resnameptr_u32, resname, error);
if (!success)
{
if (log)
log->Printf("RenderScriptRuntime::CaptureScriptInit1 - Error while reading the function parameters");
return;
}
process->ReadCStringFromMemory((lldb::addr_t)rs_resnameptr_u64, resname, error);
if (error.Fail())
{
if(log)
if (log)
log->Printf ("RenderScriptRuntime::CaptureScriptInit1 - error reading resname: %s.", error.AsCString());
}
process->ReadCStringFromMemory((lldb::addr_t)rs_cachedirptr_u32, cachedir, error);
process->ReadCStringFromMemory((lldb::addr_t)rs_cachedirptr_u64, cachedir, error);
if (error.Fail())
{
if(log)
if (log)
log->Printf ("RenderScriptRuntime::CaptureScriptInit1 - error reading cachedir: %s.", error.AsCString());
}
if (log)
log->Printf ("RenderScriptRuntime::CaptureScriptInit1 - 0x%" PRIx64 ",0x%" PRIx64 " => '%s' at '%s' .",
(uint64_t)rs_context_u32, (uint64_t)rs_script_u32, resname.c_str(), cachedir.c_str());
rs_context_u64, rs_script_u64, resname.c_str(), cachedir.c_str());
if (resname.size() > 0)
{
@@ -418,14 +545,14 @@ RenderScriptRuntime::CaptureScriptInit1(RuntimeHook* hook_info, ExecutionContext
script.cachedir = cachedir;
script.resname = resname;
script.scriptDyLib.assign(strm.GetData());
script.script = rs_script_u32;
script.context = rs_context_u32;
script.script = (addr_t) rs_script_u64;
script.context = (addr_t) rs_context_u64;
m_scripts.push_back(script);
if (log)
log->Printf ("RenderScriptRuntime::CaptureScriptInit1 - '%s' tagged with context 0x%" PRIx64 " and script 0x%" PRIx64 ".",
strm.GetData(), (uint64_t)rs_context_u32, (uint64_t)rs_script_u32);
strm.GetData(), rs_context_u64, rs_script_u64);
}
else if (log)
{
@@ -445,8 +572,12 @@ RenderScriptRuntime::LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind)
return;
}
if ((GetProcess()->GetTarget().GetArchitecture().GetMachine() != llvm::Triple::ArchType::x86)
&& (GetProcess()->GetTarget().GetArchitecture().GetMachine() != llvm::Triple::ArchType::arm))
Target &target = GetProcess()->GetTarget();
llvm::Triple::ArchType targetArchType = target.GetArchitecture().GetMachine();
if (targetArchType != llvm::Triple::ArchType::x86
&& targetArchType != llvm::Triple::ArchType::arm
&& targetArchType != llvm::Triple::ArchType::aarch64)
{
if (log)
log->Printf ("RenderScriptRuntime::LoadRuntimeHooks - Unable to hook runtime. Only X86, ARM supported currently.");
@@ -454,7 +585,7 @@ RenderScriptRuntime::LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind)
return;
}
Target &target = GetProcess()->GetTarget();
uint32_t archByteSize = target.GetArchitecture().GetAddressByteSize();
for (size_t idx = 0; idx < s_runtimeHookCount; idx++)
{
@@ -463,16 +594,29 @@ RenderScriptRuntime::LoadRuntimeHooks(lldb::ModuleSP module, ModuleKind kind)
continue;
}
const Symbol *sym = module->FindFirstSymbolWithNameAndType(ConstString(hook_defn->symbol_name), eSymbolTypeCode);
const char* symbol_name = (archByteSize == 4) ? hook_defn->symbol_name_m32 : hook_defn->symbol_name_m64;
const Symbol *sym = module->FindFirstSymbolWithNameAndType(ConstString(symbol_name), eSymbolTypeCode);
if (!sym){
if (log){
log->Printf("RenderScriptRuntime::LoadRuntimeHooks - ERROR: Symbol '%s' related to the function %s not found", symbol_name, hook_defn->name);
}
continue;
}
addr_t addr = sym->GetLoadAddress(&target);
if (addr == LLDB_INVALID_ADDRESS)
{
if(log)
if (log)
log->Printf ("RenderScriptRuntime::LoadRuntimeHooks - Unable to resolve the address of hook function '%s' with symbol '%s'.",
hook_defn->name, hook_defn->symbol_name);
hook_defn->name, symbol_name);
continue;
}
else
{
if (log)
log->Printf("RenderScriptRuntime::LoadRuntimeHooks - Function %s, address resolved at 0x%" PRIx64, hook_defn->name, addr);
}
RuntimeHookSP hook(new RuntimeHook());
hook->address = addr;