Pin
Functions

Functions

VOID INS_RewriteMemoryOperand (INS ins, UINT32 memindex, REG reg)
 
VOID INS_RewriteScatteredMemoryOperand (INS ins, UINT32 memindex)
 
VOID INS_InsertIndirectJump (INS ins, IPOINT ipoint, REG reg)
 
VOID INS_InsertDirectJump (INS ins, IPOINT ipoint, ADDRINT tgt)
 
VOID INS_Delete (INS ins)
 

Detailed Description

Use these functions to modify instructions. They work for all instruction sets. For experts only!

Availability:
Mode: JIT & Probe
O/S: Linux & Windows
CPU: All

Function Documentation

◆ INS_Delete()

VOID INS_Delete ( INS  ins)

Delete the instruction

Availability:
Mode: JIT
O/S: Linux, Windows & macOS*
CPU: All

◆ INS_InsertDirectJump()

VOID INS_InsertDirectJump ( INS  ins,
IPOINT  ipoint,
ADDRINT  tgt 
)

Insert a direct jump instruction relative to the given instruction When used with INS_Delete it can be used to emulate control transfer instructions.

Parameters
[in]insinput instruction
[in]ipointlocation relative to ins (only IPOINT_BEFORE and IPOINT_AFTER are supported)
[in]tgtabsolute address of the target
Availability:
Mode: JIT
O/S: Linux, Windows & macOS*
CPU: IA-32 and Intel(R) 64 architectures

◆ INS_InsertIndirectJump()

VOID INS_InsertIndirectJump ( INS  ins,
IPOINT  ipoint,
REG  reg 
)

Insert an indirect jump instruction relative to the given instruction. When used with INS_Delete it can be used to emulate control transfer instructions.

Parameters
[in]insinput instruction
[in]ipointlocation relative to ins (only IPOINT_BEFORE and IPOINT_AFTER are supported)
[in]regregister holding the target
Availability:
Mode: JIT
O/S: Linux, Windows & macOS*
CPU: IA-32 and Intel(R) 64 architectures

◆ INS_RewriteMemoryOperand()

VOID INS_RewriteMemoryOperand ( INS  ins,
UINT32  memindex,
REG  reg 
)

Change this memory access instruction to reference the virtual memory location contained in the given register.
This function will generate an error for memory operands with scattered access, i.e. vgather/vscatter. In this case use INS_RewriteScatteredMemoryOperand instead.

Parameters
[in]insinput instruction
[in]memopIdxcontrols which memory operand to rewrite (0,1,...)
[in]newBaseregister containing the base address of the new operand which will normally be a scratch register allocated via PIN_ClaimToolRegister()

On IA-32 and Intel64, the modified operand uses only base register addressing with the new base register newBase. Any index, scale, or offset fields from that operand in the original instruction are removed. In addition, if the original instruction's operand uses a segment override, the instruction is changed to use the default segment.

This function can be used to rewrite memory operands even when they are implicit (for instance call, ret, push, pop), though in this case the instruction may ultimately be replaced by a sequence of instructions which achieve the same effect. (This is transparent to instrumentation, which continues to see the original instruction).

The only instruction which cannot be rewritten is enter with a second operand > 0.

Note that the address in newBase is always the lowest address which will be accessed by this operand. This is consistent with the way in which Pin returns addresses in IARG_*_EA, but means that if the operand is modified by the instruction before the memory access occurs (for instance a push instruction), the value in newBase will not be the value in the stack pointer, but the address of the memory which is accessed by the instruction.

This can also be confusing for xlat; where the value of newBase is the address from which data is loaded, not the address of the base of the translation table. (Again, this is consistent with the IARG_*_EA which Pin will report for an xlat operation).

Similarly for the bt,btc,btr and bts insructions, if the bit index is larger than the operand size (so that parts of the bit index affect the EA), they are included in Pin's normal EA calculation. In this case, Pin automatically masks the bit index operand so that it only includes the index within the addressed unit of memory. This ensures that your address manipulation function need only consider the translation of the EA, it does not have to worry about additional offsets generated by the bit index operand of these instructions. (This is equivalent to saying that if you replace all memory operands, but use an address computation function that simply returns the original EA, the code will continue to execute correctly).

Call order

Please note the analysis routine that is used for setting the rewritten address must be called last among all the IPOINT_BEFORE analysis routines for this instruction (IARG_CALL_ORDER, CALL_ORDER_LAST). The reason is so that the register used to store the rewritten address will not get accidentally rewritten with another value by another analysis routine. The actual rewrite happens just before executing the instruction, so the value of the register containing the rewritten address must remain intact.

The canonical instrumentation code for memory address rewriting now looks something like this

// Map the originalEa to a translated address.
static ADDRINT ProcessAddress(ADDRINT originalEa, ADDRINT size, UINT32 access);
...
for (UINT32 op = 0; op<INS_MemoryOperandCount(ins); op++)
{
UINT32 access = (INS_MemoryOperandIsRead(ins,op) ? 1 : 0) |
(INS_MemoryOperandIsWritten(ins,op) ? 2 : 0);
AFUNPTR(ProcessAddress),
IARG_UINT32, access,
IARG_END);
}

There is no need to handle any instructions specially.

Availability:
Mode: JIT
O/S: Linux, Windows & macOS*
CPU: IA-32 and Intel(R) 64 architectures

◆ INS_RewriteScatteredMemoryOperand()

VOID INS_RewriteScatteredMemoryOperand ( INS  ins,
UINT32  memindex 
)

Change this memory access instruction to reference the virtual memory location previously configured by an analysis routine. This function is supported only for memory operands with scattered access (vscatter/vgather), and will generate an error for all other instructions. For other instructions use INS_RewriteMemoryOperand instead.
Call INS_HasScatteredMemoryAccess to check if this function can be used for an instruction.

Parameters
[in]insinput instruction
[in]memopIdxcontrols which memory operand to rewrite (0,1,...)

In order to rewrite a vscatter/vgather memory operand, two calls are required:
(1) A call to this function
(2) An IPOINT_BEFORE analysis routine with IARG_REWRITE_SCATTERED_MEMOP that will set the new addresses.
If (1) is missing then the (2) will still execute as it may perform other tasks other than rewrite.
However no rewrite will be executed and the addresses set by the analysis routine will not be used.
If (2) is missing then there will be no rewrite.

The canonical instrumentation code for scattered access memory rewriting now looks something like this

// In instrumentation callback
...
{
for (UINT32 op = 0; op<INS_MemoryOperandCount(ins); op++)
{
(AFUNPTR)DoRewrite,
IARG_END);
}
}
// Rewrite analysis routine
static VOID DoRewrite(ISCATTERED_MEMORY_REWRITE* memRewrite)
{
for (UINT32 i = 0; i < memRewrite->NumOfElements(); i++)
{
ADDRINT newAddr = ...
memRewrite->SetElementAddress(i, newAddr);
}
}
Availability:
Mode: JIT
O/S: Linux, Windows & macOS*
CPU: IA-32 and Intel(R) 64 architectures
ISCATTERED_MEMORY_REWRITE::SetElementAddress
virtual VOID SetElementAddress(UINT32 element_index, ADDRINT address)=0
IARG_CALL_ORDER
@ IARG_CALL_ORDER
Type: CALL_ORDER. Determine order of analysis calls. See CALL_ORDER.
Definition: types_vmapi.PH:473
INS_MemoryOperandIsRead
BOOL INS_MemoryOperandIsRead(INS ins, UINT32 memopIdx)
IARG_MEMORYOP_SIZE
@ IARG_MEMORYOP_SIZE
Type: UINT32. Size of a memory op (memory op index is next arg)
Definition: types_vmapi.PH:480
INS_RewriteMemoryOperand
VOID INS_RewriteMemoryOperand(INS ins, UINT32 memindex, REG reg)
IARG_MEMORYOP_EA
@ IARG_MEMORYOP_EA
Type: ADDRINT. Effective address of a memory op (memory op index is next arg); only valid at IPOINT_B...
Definition: types_vmapi.PH:479
INS_MemoryOperandIsWritten
BOOL INS_MemoryOperandIsWritten(INS ins, UINT32 memopIdx)
IARG_REWRITE_SCATTERED_MEMOP
@ IARG_REWRITE_SCATTERED_MEMOP
Definition: types_vmapi.PH:339
INS_RewriteScatteredMemoryOperand
VOID INS_RewriteScatteredMemoryOperand(INS ins, UINT32 memindex)
ISCATTERED_MEMORY_REWRITE::NumOfElements
virtual UINT32 NumOfElements() const =0
ISCATTERED_MEMORY_REWRITE
Definition: types_vmapi.PH:1134
IARG_RETURN_REGS
@ IARG_RETURN_REGS
Register to write analysis function return value (additional register arg required)....
Definition: types_vmapi.PH:471
REG_INST_G0
@ REG_INST_G0
Scratch register used in pintools.
Definition: reg_ia32.PH:535
INS_HasScatteredMemoryAccess
BOOL INS_HasScatteredMemoryAccess(INS ins)
CALL_ORDER_LAST
@ CALL_ORDER_LAST
Execute this call last. Value is 300.
Definition: types_vmapi.PH:204
INS_InsertCall
VOID INS_InsertCall(INS ins, IPOINT action, AFUNPTR funptr,...)
INS_MemoryOperandCount
UINT32 INS_MemoryOperandCount(INS ins)
IPOINT_BEFORE
@ IPOINT_BEFORE
Insert a call before the first instruction of the instrumented object. Always valid.
Definition: types_vmapi.PH:135
IARG_UINT32
@ IARG_UINT32
Type: UINT32. Constant (additional integer arg required)
Definition: types_vmapi.PH:218
REG
REG
Definition: reg_ia32.PH:18