Search code examples
assemblyx86intel-pin

Pin tool,the static ins address (INS_Address(ins)) is different from the address of the instrumented instruction(IARG_INST_PTR)


I want to print every rtn name, and instructions belong to this rtn. My code looks like this: '''

for (SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec))
{ 
    //OutFile << "SEC name: " << SEC_Name(sec) << endl;
    for (RTN rtn = SEC_RtnHead(sec); RTN_Valid(rtn); rtn = RTN_Next(rtn))
    {
        if(RTN_NumIns(rtn) >= 1){
            OutFile << RTN_Name(rtn) << ";" << std::dec << RTN_NumIns(rtn) << endl;

            RTN_Open(rtn);

            for (INS ins = RTN_InsHead(rtn); INS_Valid(ins); ins = INS_Next(ins))
            {
                OutFile << hex << INS_Address(ins) << ";" << INS_Disassemble(ins) << ";" \
                ...;
            }
            // to preserve space, release data associated with RTN after we have processed it
            RTN_Close(rtn);
        }     
    }
}

''' then i ues INS_AddInstrumentFunction to print memery trace. '''

VOID RecordMemRead(VOID * ip, VOID * addr)
{
    fprintf(trace,"%p: R %p\n", ip, addr);
}
VOID RecordMemWrite(VOID * ip, VOID * addr)
{
    fprintf(trace,"%p: W %p\n", ip, addr);
}

VOID Instruction(INS ins, VOID *v)
{
    UINT32 memOperands = INS_MemoryOperandCount(ins);

    for (UINT32 memOp = 0; memOp < memOperands; memOp++)
    {
        if (INS_MemoryOperandIsRead(ins, memOp))
        {
            INS_InsertPredicatedCall(
                ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
                IARG_INST_PTR,
                IARG_MEMORYOP_EA, memOp,
                IARG_END);
        }
        if (INS_MemoryOperandIsWritten(ins, memOp))
        {
            INS_InsertPredicatedCall(
                ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite,
                IARG_INST_PTR,
                IARG_MEMORYOP_EA, memOp,
                IARG_END);
        }
    }
}

''' finally, i got different ins address, Why is it happen? I've been stuck here too long.


Solution

  • INS_Address(ins) print address where an instruction is loaded during the initial loading of image. IARG_INST_PTR gives the address of the instruction when it is executing. Relocation of image happens at runtime. Hence addresses may be different.

    see instruction point value of dynamic linking and static linking. Your question is similar. just worded differently.