Search code examples
intel-pindynamic-analysis

Resume execution at arbitrary positions inside a callback function


I am using Pin for dynamic analysis.

In my dynamic analysis task on 64-bit x86 binary code, I would like to resume the execution at arbitrary program positions (e.g., the second instruction of current executed function) after I fix certain memory access error inside the signal handling callbacks.

It would be something like this:

BOOL catchSignalSEGV(THREADID tid, INT32 sig, CONTEXT *ctx, BOOL hasHandler, const EXCEPTION_INFO *pExceptInfo, VOID *v)
{
    //  I will first fix the memory access error according to certain rules.
    fix();

   // then I would like to resume the execution at an arbitrary position, say, at the beginning of current monitored function
   set_reg(rip, 0x123456);                          // set the rip register 
   PIN_ExecuteAt(ctx);                              // resume the execution

   return false;
}

However, I got this exception: E: PIN_ExecuteAt() cannot be called from a callback.

I know I can resume the execution at "current instruction" by return false at the end of the signal handling function, but basically can I resume at arbitrary positions?

Am I clear? Thank you for your help!


Solution

  • The documentation is clear on this:

    A tool can call this API to abandon the current analysis function and resume execution of the calling thread at a new application register state. Note that this API does not return back to the caller's analysis function.

    This API can be called from an analysis function or a replacement routine, but not from a callback.

    The signal handler is considered a callback. You can only use PIN_ExecuteAt in an analysis function or a replacement routine.

    One thing you may try to do is to save the context you are interested in and allow the application to resume, ensuring that the next instruction to be executed has an analysis callback attached. You may be able to use if-then instrumentation to improve performance. Then you can call ExecuteAt from that analysis routine.