gpt4 book ai didi

c++ - Pin工具中调用函数时出现段错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:55:34 26 4
gpt4 key购买 nike

我目前正在构建一个 Pin 工具,它基于此 blog post 检测来自 Linux 应用程序的未初始化读取。 .
也可以从博客中看到作者的代码。

因为这个是针对 Windows 的,所以我尝试创建一个与 Linux 兼容的。但是当我用应用程序执行我的 Pin 工具时,出现段错误。奇怪的是调用函数时出现故障(pin工具在调用taint_define函数中的taint_get函数时出现故障),不是因为访问未初始化的堆指针或此类一般段错误点。

段错误的点是这样的:

VOID Instruction(INS ins, VOID *v)
{
Uninit_Instruction(ins, v);
}

void Uninit_Instruction(INS ins, void* v)
{
// check if the stack pointer is altered (i.e. memory is allocated on the
// stack by subtracting an immediate from the stack pointer)
if(INS_Opcode(ins) == XED_ICLASS_SUB &&
INS_OperandReg(ins, 0) == REG_STACK_PTR &&
INS_OperandIsImmediate(ins, 1))
{
// insert call after, so we can pass the stack pointer directly
INS_InsertCall(ins, IPOINT_AFTER, (AFUNPTR)taint_undefined,
IARG_REG_VALUE,
REG_STACK_PTR,
IARG_ADDRINT, (UINT32) INS_OperandImmediate(ins, 1),
IARG_END);
}

UINT32 memOperands = INS_MemoryOperandCount(ins);

for (UINT32 memOp = 0; memOp < memOperands; memOp++)
{
if (INS_MemoryOperandIsRead(ins, memOp))
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)taint_check,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_MEMORYREAD_SIZE,
IARG_END);
}

if (INS_MemoryOperandIsWritten(ins, memOp))
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)taint_define,
IARG_MEMORYOP_EA, memOp,
IARG_MEMORYWRITE_SIZE,
IARG_END);
}
}

}

回调函数如下所示:

// Taint this address as written
void taint_define(ADDRINT addr, UINT32 size)
{
// Debug purpose
TraceFile << "taint_define: " << addr << ", " << size << endl;

// taint the addresses as defined, pretty slow, but easiest to implement
for (UINT32 i = 0; i < size; i++)
{
//TraceFile << "taint_define_loop size: " << size << endl;
UINT32 *t = taint_get(addr + i);
TraceFile << "after taint_get" << endl;
UINT32 index = (addr + i) % 0x20000;

// define this bit
t[index / 32] |= 1 << (index % 32);
}
}


inline UINT32* taint_get(ADDRINT addr)
{
// Debug purpose
TraceFile << "taint_get: " << addr;

// allocate memory to taint these memory pages
if(taint[addr / 0x20000] == NULL) {
// we need an 16kb page to track 128k of memory
/*
taint[addr / 0x20000] = (UINT32 *) W::VirtualAlloc(NULL, 0x20000 / 8,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
*/
taint[addr / 0x20000] = (UINT32*)malloc(0x20000/8);
}

return taint[addr / 0x20000];
}

输出看起来像这样:

C:Tool (or Pin) caused signal 11 at PC 0x7fcf475e08a4
segmentation fault (core dumped)

日志在这里。

Watched Image count: 0x1
WatchedImage: unread_3vars
Uninit_Image
Uninit_Image
Thread start
taint_define: 0x7fff06930d58, 0x8

我目前正在研究 Fedora core 17 x86-64、gcc 4.7.2 和 Pin 2.12-58423。
而且,附上我的 pin 工具代码 here

最佳答案

I am currently building a Pin tool which detects uninitialized reads from Linux application, based on this blog post.

这并不能真正回答您的问题,您可能还有其他原因需要学习 Pin 工具,但是......

我们发现基于 Pin 的工具不足以检测非玩具程序。如果您的目标是检测未初始化的内存读取,请考虑使用 Memory Sanitizer .

关于c++ - Pin工具中调用函数时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19888805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com