Exemple #1
0
bool TraceManager::IsNormal(RTN myrtn)
{
	BBL my_bbl=RTN_BblTail(myrtn);	
	if(BBL_Valid(my_bbl))
	{
		INS my_ins=BBL_InsTail(my_bbl);
		while(INS_Valid(my_ins))
		{
			if(INS_IsRet(my_ins))
			{
				//cerr<<"Normal Routine::"<<RTN_Name(myrtn)<<endl;
				return true;
			}
			my_ins=INS_Prev(my_ins);
		}	
		/*if(INS_IsBranch(my_ins)|| INS_IsNop(my_ins))
		{
			cerr<<"!!!Abnormal Routine::"<<RTN_Name(myrtn)<<endl;
			return false;
		}	
		my_bbl=BBL_Prev(my_bbl); */
	}
	//cerr<<"!!!!Abnormal Routine::"<<RTN_Name(myrtn)<<endl;
	return false;
}
Exemple #2
0
VOID Trace(TRACE trace, VOID *v)
{
        
    for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
    {
        INS tail = BBL_InsTail(bbl);
        
        if( INS_IsCall(tail) )
        {
            INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(do_call_indirect),
                               IARG_BRANCH_TARGET_ADDR, IARG_BRANCH_TAKEN, IARG_END);
        }
        else
        {
            // sometimes code is not in an image
            RTN rtn = TRACE_Rtn(trace);
            
            // also track stup jumps into share libraries
            if( RTN_Valid(rtn) && !INS_IsDirectBranchOrCall(tail) && ".plt" == SEC_Name( RTN_Sec( rtn ) ))
            {
                INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(do_call_indirect),
                               IARG_BRANCH_TARGET_ADDR, IARG_BRANCH_TAKEN, IARG_END);
            }
        }
        
    }
}
VOID Trace(TRACE trace, VOID *v)
{
    for (BBL bbl = TRACE_BblTail(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl)) {
        if (BBL_HasFallThrough(bbl)) {
            INS ins = BBL_InsTail(bbl);
            TEST(INS_HasFallThrough(ins), "BBL_HasFallThrough or INS_HasFallThrough failed");
        }
    }
}
Exemple #4
0
VOID Trace(TRACE trace, VOID *v)
{
    const INS beginIns = BBL_InsHead(TRACE_BblHead(trace));
    const INS endIns = BBL_InsTail(TRACE_BblTail(trace));
    const ADDRINT beginAddr = INS_Address(beginIns);
    const ADDRINT endAddr = INS_Address(endIns) + INS_Size(endIns) - 1;

    sandbox.CheckAddressRange(reinterpret_cast<const char *>(beginAddr), reinterpret_cast<const char *>(endAddr));
}
Exemple #5
0
VOID Trace(TRACE trace, VOID *v)
{
    static BOOL programStart = TRUE;

    if (programStart)
    {
        programStart = FALSE;
        next_pc = (void*)INS_Address(BBL_InsHead(TRACE_BblHead(trace)));
    }

    for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
    {
        // check BBL entry PC
        INS_InsertCall(
            BBL_InsHead(bbl), IPOINT_BEFORE, (AFUNPTR)CheckPc,
            IARG_INST_PTR,
            IARG_END);

        INS tail = BBL_InsTail(bbl);
        
        if (INS_IsBranchOrCall(tail))
        {
            // record taken branch targets
            INS_InsertCall(
                tail, IPOINT_BEFORE, AFUNPTR(RecordPc),
                IARG_INST_PTR,
                IARG_BRANCH_TARGET_ADDR,
                IARG_BRANCH_TAKEN,
                IARG_END);
        }

        if (INS_HasFallThrough(tail))
        {
            // record fall-through
            INS_InsertCall(
                tail, IPOINT_AFTER, (AFUNPTR)RecordPc,
                IARG_INST_PTR,
                IARG_FALLTHROUGH_ADDR,
                IARG_BOOL,
                TRUE,
                IARG_END);
        }

#if defined(TARGET_IA32) || defined(TARGET_IA32E)
        if (INS_IsSysenter(tail) ||
            INS_HasRealRep(tail))
        { // sysenter on x86 has some funny control flow that we can't correctly verify for now
            // Genuinely REP prefixed instructions are also odd, they appear to stutter.
            INS_InsertCall(tail, IPOINT_BEFORE, (AFUNPTR)Skip, IARG_END);
        }
#endif
    }
}
Exemple #6
0
VOID Trace(TRACE trace, VOID *v)
{
    for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
    {
        string traceString = "";
        
        for ( INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins))
        {
            traceString +=  "%" + INS_Disassemble(ins) + "\n";
        }
        

        // we try to keep the overhead small 
        // so we only insert a call where control flow may leave the current trace
        
        if (KnobNoCompress)
        {
            INS_InsertCall(BBL_InsTail(bbl), IPOINT_BEFORE, AFUNPTR(docount),
                           IARG_PTR, new string(traceString),
                           IARG_END);
        }
        else
        {
            // Identify traces with an id
            count_trace++;

            // Write the actual trace once at instrumentation time
            string m = "@" + decstr(count_trace) + "\n";
            TraceFile.write(m.c_str(), m.size());            
            TraceFile.write(traceString.c_str(), traceString.size());
            
            
            // at run time, just print the id
            string *s = new string(decstr(count_trace) + "\n");
            INS_InsertCall(BBL_InsTail(bbl), IPOINT_BEFORE, AFUNPTR(docount),
                           IARG_PTR, s,
                           IARG_END);
        }
    }
}
void trace (TRACE trace, void *v)
{
	for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl)) {
		INS tail = BBL_InsTail(bbl);
		if (INS_IsRet(tail)) {
			INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(before_return),
					IARG_THREAD_ID, IARG_REG_VALUE, REG_ESP, IARG_END);
		} else if (INS_IsCall(tail)) {
			INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(before_call),
					IARG_THREAD_ID, IARG_REG_VALUE, REG_ESP, IARG_ADDRINT, INS_NextAddress(tail), IARG_BRANCH_TARGET_ADDR, IARG_END);
		}
	}
}
Exemple #8
0
VOID Trace(TRACE trace, VOID *v)
{
    for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
    {
        DBG_PRINT(printf("Inst: Sequence address %p\n",(CHAR*)(INS_Address(BBL_InsHead(bbl)))));
        for (INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins))
        {
            DBG_PRINT(printf("Inst:   %p\n",(CHAR*)(INS_Address(ins))));
            INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(docount_ins), IARG_INST_PTR, IARG_END);
        }
        
        INT32 icount = BBL_NumIns(bbl);
        DBG_PRINT(printf("Inst:     -> control flow change (bbl size %d)\n", icount));
        INS_InsertCall(BBL_InsTail(bbl), IPOINT_BEFORE, AFUNPTR(docount_bbl_ins), IARG_INST_PTR, IARG_UINT32, icount, IARG_END);
    }
}
Exemple #9
0
VOID Trace(TRACE trace, VOID *v)
{
    //DumpTrace("Before", trace);
    
    BOOL live[REGCOUNT];
    for (INT32 i = 0; i < REGCOUNT; i++)
    {
        live[i] = false;
    }
    
    for (BBL bbl = TRACE_BblTail(trace); BBL_Valid(bbl); bbl = BBL_Prev(bbl))
    {
        for (INS ins = BBL_InsTail(bbl); INS_Valid(ins); ins = INS_Prev(ins))
        {
            WriteShadows(ins, live);
            
            RewriteBases(ins, live);
        }
    }

    WriteLiveShadows(trace, live);
    //DumpTrace("After", trace);
}
Exemple #10
0
VOID Trace(TRACE trace, VOID *v)
{
    const BOOL print_args = KnobPrintArgs.Value();
    
        
    for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
    {
        INS tail = BBL_InsTail(bbl);
        
        if( INS_IsCall(tail) )
        {
            if( INS_IsDirectBranchOrCall(tail) )
            {
                const ADDRINT target = INS_DirectBranchOrCallTargetAddress(tail);
                if( print_args )
                {
                    INS_InsertPredicatedCall(tail, IPOINT_BEFORE, AFUNPTR(do_call_args),
                                             IARG_PTR, Target2String(target), IARG_G_ARG0_CALLER, IARG_END);
                }
                else
                {
                    INS_InsertPredicatedCall(tail, IPOINT_BEFORE, AFUNPTR(do_call),
                                             IARG_PTR, Target2String(target), IARG_END);
                }
                
            }
            else
            {
                if( print_args )
                {
                    INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(do_call_args_indirect),
                                   IARG_BRANCH_TARGET_ADDR, IARG_BRANCH_TAKEN,  IARG_G_ARG0_CALLER, IARG_END);
                }
                else
                {
                    INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(do_call_indirect),
                                   IARG_BRANCH_TARGET_ADDR, IARG_BRANCH_TAKEN, IARG_END);
                }
                
                
            }
        }
        else
        {
            // sometimes code is not in an image
            RTN rtn = TRACE_Rtn(trace);
            
            // also track stup jumps into share libraries
            if( RTN_Valid(rtn) && !INS_IsDirectBranchOrCall(tail) && ".plt" == SEC_Name( RTN_Sec( rtn ) ))
            {
                if( print_args )
                {
                    INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(do_call_args_indirect),
                                   IARG_BRANCH_TARGET_ADDR, IARG_BRANCH_TAKEN,  IARG_G_ARG0_CALLER, IARG_END);
                }
                else
                {
                    INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(do_call_indirect),
                                   IARG_BRANCH_TARGET_ADDR, IARG_BRANCH_TAKEN, IARG_END);

                }
            }
        }
        
    }
}
Exemple #11
0
// -------------------------------------------------------------
// Trace instrumentation function
// -------------------------------------------------------------
void I_Trace(TRACE trace, void *v) {

    BOOL isPLT = IsPLT(TRACE_Rtn(trace));

    #if DEBUG_INS    
    printf("-- Instrumenting trace %X of function %s\n", 
        TRACE_Address(trace), RTN_Valid(TRACE_Rtn(trace)) ? RTN_Name(TRACE_Rtn(trace)).c_str() : "<unknown_routine>");
    #endif

    // scan BBLs within the current trace
    for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl)) {

    	// instrument memory reads and writes
    	for(INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins))
    	    Instruction(ins);

        INS tail = BBL_InsTail(bbl);

        // skip system calls
        if ( INS_IsSyscall(tail) ) continue;
        
        // instrument .plt stub calls
        if ( isPLT ) {

            #if DEBUG_INS
            printf("   > .plt stub call\n");
            #endif

            if (gSetup.callingSite) {
                if (gSetup.memBuf)
                    INS_InsertCall(tail, IPOINT_BEFORE, 
                                (AFUNPTR)A_ProcessIndirectCallCSBuf,
                                IARG_FAST_ANALYSIS_CALL,
                                IARG_INST_PTR,
                                IARG_BRANCH_TARGET_ADDR,
                                IARG_REG_VALUE, REG_STACK_PTR,
                                IARG_THREAD_ID,
                                IARG_CONTEXT,
                                IARG_END);
                else
                    INS_InsertCall(tail, IPOINT_BEFORE, 
                                (AFUNPTR)A_ProcessIndirectCallCS,
                                IARG_FAST_ANALYSIS_CALL,
                                IARG_INST_PTR,
                                IARG_BRANCH_TARGET_ADDR,
                                IARG_REG_VALUE, REG_STACK_PTR,
                                IARG_THREAD_ID,
                                IARG_END);
            }
            else {
                if (gSetup.memBuf)
                    INS_InsertCall(tail, IPOINT_BEFORE, 
                                (AFUNPTR)A_ProcessIndirectCallBuf,
                                IARG_FAST_ANALYSIS_CALL,
                                IARG_BRANCH_TARGET_ADDR,
                                IARG_REG_VALUE, REG_STACK_PTR,
                                IARG_THREAD_ID,
                                IARG_CONTEXT,
                                IARG_END);
                else
                    INS_InsertCall(tail, IPOINT_BEFORE, 
                                (AFUNPTR)A_ProcessIndirectCall,
                                IARG_FAST_ANALYSIS_CALL,
                                IARG_BRANCH_TARGET_ADDR,
                                IARG_REG_VALUE, REG_STACK_PTR,
                                IARG_THREAD_ID,
                                IARG_END);
            }
            continue;
        }

        // instrument all calls and returns
        if ( INS_IsCall(tail) ) {
        
            // direct call
            if( INS_IsDirectBranchOrCall(tail) ) {

                // get target address
                ADDRINT target = Target2FunAddr(INS_DirectBranchOrCallTargetAddress(tail));

                #if DEBUG_INS
                printf("   > Direct call to %s\n", Target2RtnName(target).c_str());
                #endif

                // instrument direct call: target address determined here
                if (gSetup.callingSite) {
                    if (gSetup.memBuf)
                        INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                              (AFUNPTR)A_ProcessDirectCallCSBuf,
                                              IARG_FAST_ANALYSIS_CALL,
                                              IARG_INST_PTR,
                                              IARG_ADDRINT, target,
                                              IARG_REG_VALUE, REG_STACK_PTR,
                                              IARG_THREAD_ID,
                                              IARG_CONTEXT,
                                              IARG_END);
                    else
                        INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                              (AFUNPTR)A_ProcessDirectCallCS,
                                              IARG_FAST_ANALYSIS_CALL,
                                              IARG_INST_PTR,
                                              IARG_ADDRINT, target,
                                              IARG_REG_VALUE, REG_STACK_PTR,
                                              IARG_THREAD_ID,
                                              IARG_END);
                }
                else {
                    if (gSetup.memBuf)
                        INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                              (AFUNPTR)A_ProcessDirectCallBuf,
                                              IARG_FAST_ANALYSIS_CALL,
                                              IARG_ADDRINT, target,
                                              IARG_REG_VALUE, REG_STACK_PTR,
                                              IARG_THREAD_ID,
                                              IARG_CONTEXT,
                                              IARG_END);
                    else
                        INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                              (AFUNPTR)A_ProcessDirectCall,
                                              IARG_FAST_ANALYSIS_CALL,
                                              IARG_ADDRINT, target,
                                              IARG_REG_VALUE, REG_STACK_PTR,
                                              IARG_THREAD_ID,
                                              IARG_END);
                }
            }

            // indirect call: target address determined at call time
            else {

                #if DEBUG_INS
                printf("   > Indirect call\n");
                #endif

                // instrument indirect call
                if (gSetup.callingSite) {
                    if (gSetup.memBuf)
                        INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                              (AFUNPTR)A_ProcessIndirectCallCSBuf,
                                              IARG_FAST_ANALYSIS_CALL,
                                              IARG_INST_PTR,
                                              IARG_BRANCH_TARGET_ADDR,
                                              IARG_REG_VALUE, REG_STACK_PTR,
                                              IARG_THREAD_ID,
                                              IARG_CONTEXT,
                                              IARG_END);
                    else
                        INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                              (AFUNPTR)A_ProcessIndirectCallCS,
                                              IARG_FAST_ANALYSIS_CALL,
                                              IARG_INST_PTR,
                                              IARG_BRANCH_TARGET_ADDR,
                                              IARG_REG_VALUE, REG_STACK_PTR,
                                              IARG_THREAD_ID,
                                              IARG_END);
                }
                else {
                    if (gSetup.memBuf)
                        INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                              (AFUNPTR)A_ProcessIndirectCallBuf,
                                              IARG_FAST_ANALYSIS_CALL,
                                              IARG_BRANCH_TARGET_ADDR,
                                              IARG_REG_VALUE, REG_STACK_PTR,
                                              IARG_THREAD_ID,
                                              IARG_CONTEXT,
                                              IARG_END);
                    else
                        INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                              (AFUNPTR)A_ProcessIndirectCall,
                                              IARG_FAST_ANALYSIS_CALL,
                                              IARG_BRANCH_TARGET_ADDR,
                                              IARG_REG_VALUE, REG_STACK_PTR,
                                              IARG_THREAD_ID,
                                              IARG_END);
                }
            }

            continue;
        }
        
        if ( INS_IsRet(tail) ) {

            #if DEBUG_INS
            printf("   > return\n");
            #endif

            if (gSetup.memBuf)
                INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                     (AFUNPTR)A_ProcessReturnBuf,
                                     IARG_FAST_ANALYSIS_CALL,
                                     IARG_REG_VALUE, REG_STACK_PTR,
                                     IARG_THREAD_ID,
                                     IARG_CONTEXT,
                                     IARG_END);
            else
                INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                     (AFUNPTR)A_ProcessReturn,
                                     IARG_FAST_ANALYSIS_CALL,
                                     IARG_REG_VALUE, REG_STACK_PTR,
                                     IARG_THREAD_ID,
                                     IARG_END);
        }
    }
}
Exemple #12
0
static void I_Trace(TRACE trace, void *v)
{

    //FIXME if (PIN_IsSignalHandler()) {Sequence_ProcessSignalHandler(head)};

    for(BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl)) {

        INS tail = BBL_InsTail(bbl);

        // All memory reads/writes
        for( INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins) ) {

            if( INS_IsMemoryRead(ins)
                || INS_HasMemoryRead2(ins)
                || INS_IsMemoryWrite(ins)
            ) {
                INS_InsertCall(ins, IPOINT_BEFORE,
                               (AFUNPTR)A_DoMem,
                               IARG_BOOL, INS_IsMemoryWrite(ins),
                               (INS_IsMemoryWrite(ins) ? IARG_MEMORYWRITE_EA : (INS_IsMemoryRead(ins) ? IARG_MEMORYREAD_EA : IARG_MEMORYREAD2_EA)),
                               IARG_INST_PTR,
                               IARG_END);
            }
#if defined(TARGET_IA32)  && defined (TARGET_WINDOWS)
            // on ia-32 windows need to identify 
            // push 
            // ret
            // in order to process callstack correctly
            if (ins != tail) 
            {
                INS_InsertPredicatedCall(ins, IPOINT_BEFORE,
                                         (AFUNPTR)ProcessInst,
                                         IARG_INST_PTR,
                                         IARG_END);
                if (INS_Opcode(ins)==XED_ICLASS_PUSH)
                {
                    RecordPush (ins);
                }
            }
#endif
        }


        
        // All calls and returns
        if( INS_IsSyscall(tail) ) {
            INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                     (AFUNPTR)A_ProcessSyscall,
                                     IARG_INST_PTR,
                                     IARG_SYSCALL_NUMBER,
                                     IARG_REG_VALUE, REG_STACK_PTR,
                                     IARG_SYSCALL_ARG0,
                                     IARG_END);

        } else {
            if( INS_IsCall(tail) ) {
                if( INS_IsDirectBranchOrCall(tail) ) {
                    ADDRINT target = INS_DirectBranchOrCallTargetAddress(tail);
                    INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                             (AFUNPTR)A_ProcessDirectCall,
                                             IARG_INST_PTR,
                                             IARG_ADDRINT, target,
                                             IARG_REG_VALUE, REG_STACK_PTR,
                                             IARG_END);
                } else if( !IsPLT(trace) ) {
                    INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                             (AFUNPTR)A_ProcessIndirectCall,
                                             IARG_INST_PTR,
                                             IARG_BRANCH_TARGET_ADDR,
                                             IARG_REG_VALUE, REG_STACK_PTR,
                                             IARG_END);
                }
            }
            if( IsPLT(trace) ) {
                INS_InsertCall(tail, IPOINT_BEFORE, 
                               (AFUNPTR)A_ProcessStub,
                               IARG_INST_PTR,
                               IARG_BRANCH_TARGET_ADDR,
                               IARG_REG_VALUE, REG_STACK_PTR,
                               IARG_END);
            }
            if( INS_IsRet(tail) ) {
                INS_InsertPredicatedCall(tail, IPOINT_BEFORE,
                                         (AFUNPTR)A_ProcessReturn,
                                         IARG_INST_PTR,
                                         IARG_REG_VALUE, REG_STACK_PTR,
                                         IARG_END);
	
            }
        }
    }
}
Exemple #13
0
VOID trace_instrument(TRACE trace, VOID *v){
  for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl)){ 
    /* iterate over all basic blocks */
    
    string codelet_string = "";
    // this writes disassembly 
    char codelet_buffer[65536*2]; int cbs = 0;
    INS head = BBL_InsHead(bbl);
    INS tail = BBL_InsTail(bbl);
    
    ADDRINT stage_entry = INS_Address( head );
    ADDRINT target = 0;
    if (INS_IsCall(tail)){
      if( INS_IsDirectBranchOrCall(tail)){
        target = INS_DirectBranchOrCallTargetAddress(tail);}}

    INS cur ;
    int branch_id = slp_count;
      
    /* If compression is turned off (default), only output the addresses of
     * the BBL once
     */
    if (!KnobNoCompress){
      /* Instrument the head instruction right before it is called, but also
       * before we instrument the instructions in the basic block 
       */
      string msg_pre  = "\n@@BBL(" + decstr( branch_id ) + ") STAGE " + Target2String(stage_entry)->c_str() + "\n" ;
      INS_InsertCall(head, IPOINT_BEFORE, AFUNPTR(string_report),
		     IARG_PTR, new string(msg_pre),
		     IARG_END);
    }
   
    /* Walk the list of instructions inside the BBL. Disassemble each, and add
     * it to the codelet string. Also, instrument each instruction at the
     * point before it is called with the do_count function.
     */
    for ( cur = head; INS_Valid( cur ); cur = INS_Next(cur ) ){
      cbs += sprintf( codelet_buffer + cbs , "\n\t@%llx\t%s", INS_Address( cur ), INS_Disassemble( cur ).c_str() );
      INS_InsertCall(cur, IPOINT_BEFORE, (AFUNPTR)do_count, IARG_ADDRINT, INS_Address( cur ), IARG_END);
    }

    /* Finish off the codelet assembly string with an out message and
     * address ranges of the BBL
     */
    cbs += sprintf( codelet_buffer + cbs , "\n\t}BBL.OUT [%d] %llx - %llx\n", branch_id, INS_Address( head ), INS_Address( tail ));
  
    /* If compression is turned on, output the codelet every single time we
     * hit the same block.
     */
    if(KnobNoCompress){
      INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(string_report),
		     IARG_PTR, new string(codelet_buffer),
		     IARG_END);
      slp_count ++;
    }
    else{
      /* add the mapped BBL to output */
      TraceFile.write(codelet_buffer, cbs);	
 
      /* Instrument the tail instruction by inserting just before it is called
      */
      string msg_post = "+@@BBL(" + decstr( branch_id ) + ") ACHIEVE : GOTO " + Target2String(target)->c_str();
      INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(string_report),
		     IARG_PTR, new string(msg_post),
		     IARG_END);

      slp_count ++;
    }
  }
}
Exemple #14
0
VOID Trace(TRACE trace, VOID *v)
{
	if(TAINT_Analysis_On&&TAINT_Instrumentation_On)
	{
  for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
  {
	  if(bbl_taintedmem)
	  BBL_InsertCall(bbl,IPOINT_BEFORE,(AFUNPTR)bblBegin,IARG_END);

	  
    for (INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins))
    {
		INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)checkEIP,IARG_INST_PTR,IARG_END);
	
		if(INS_IsCall(ins))//detect overflow of stack
		{
			INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)MemofRetAddr,
                                            IARG_MEMORYOP_EA, 0,
                                            IARG_END);
		}
        if ( INS_Opcode(ins) >= XED_ICLASS_MOV && INS_Opcode(ins) <= XED_ICLASS_MOVZX )//&& INS_Address(ins) == 0x7c80a2f0)//||INS_Address(ins)==0x7c80a2f3))//||( (INS_Opcode(ins) >= XED_ICLASS_POP) && (INS_Opcode(ins) <= XED_ICLASS_POPFQ))||((INS_Opcode(ins) >= XED_ICLASS_PUSH) && (INS_Opcode(ins) <= XED_ICLASS_PUSHFQ))||(INS_Opcode(ins) == XED_ICLASS_LEA))
        {
		
            if (INS_has_immed(ins))
            {
                if (INS_IsMemoryWrite(ins)) //immed -> mem
                {
                    INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)ImmedCleanMem,
                                            IARG_MEMORYOP_EA, 0,
                                            IARG_END);
                }
                else						//immed -> reg
                {
                    REG insreg1 = INS_get_write_reg(ins);
                    INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)ImmedCleanReg,
                                            IARG_ADDRINT, (ADDRINT)insreg1,
                                            IARG_END);
                }
            }
            else if (INS_IsMemoryRead(ins)) //mem -> reg 
            {
                //in this case we call MemTaintReg to copy the taint if relevant
                REG insreg2 = INS_get_write_reg(ins);
				REG basereg2 = INS_get_mem_basereg(ins);
				REG indexreg2 = INS_get_mem_indexreg(ins);

				//ADDRINT insadd = INS_Address(ins);
				//string insdis = INS_Disassemble(ins);
				//out <<  "instruction 2 opcode " << INS_Opcode(ins)<<endl;
					INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)MemTaintReg,
                                        IARG_MEMORYOP_EA, 0,
										IARG_UINT32,INS_MemoryScale(ins),
										IARG_ADDRINT, (ADDRINT)basereg2,
										IARG_ADDRINT, (ADDRINT)indexreg2,
                                        IARG_ADDRINT, (ADDRINT)insreg2,										
										IARG_UINT32, INS_Opcode(ins),
										IARG_INST_PTR,
                                        IARG_END);


            }
            else if (INS_IsMemoryWrite(ins)) //reg -> mem 
            {
                //in this case we call RegTaintMem to copy the taint if relevant
                REG insreg3 = INS_get_read_reg(ins);
				REG basereg3 = INS_get_memwr_basereg(ins);
				REG indexreg3 = INS_get_memwr_indexreg(ins);
				//ADDRINT insadd = INS_Address(ins);
				//IARG_INST_PTR


                INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)RegTaintMem,
                                        IARG_ADDRINT,(ADDRINT)insreg3,
										IARG_UINT32,INS_MemoryScale(ins),
										IARG_ADDRINT, (ADDRINT)basereg3,
                                        IARG_ADDRINT, (ADDRINT)indexreg3,	
										IARG_MEMORYOP_EA, 0,
										IARG_UINT32, INS_Opcode(ins),
										IARG_INST_PTR,
										IARG_END);
            }
            else if (INS_RegR(ins, 0) != REG_INVALID()) //reg -> reg
            {
                //in this case we call RegTaintReg
                REG Rreg = INS_get_read_reg(ins); 
                REG Wreg = INS_get_write_reg(ins);
				//ADDRINT insadd = INS_Address(ins);
                INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)RegTaintReg,
                                        IARG_ADDRINT, (ADDRINT)Rreg,
                                        IARG_ADDRINT, (ADDRINT)Wreg,
										IARG_UINT32, INS_Opcode(ins),
										IARG_INST_PTR,
										IARG_END);
            }
            else	//should never happen
            {
                out << "serious error?!\n" << endl;
            }		
		} // IF opcode is a MOV
		/*
		if(bbl_taintedmem == 1&&INS_IsBranch(ins))
		{
			out << BBL_Address(bbl) <<endl;
			out << INS_Address(ins)<<endl;
			out << INS_NextAddress(ins)<<endl;
			out << INS_DirectBranchOrCallTargetAddress(ins)<<endl;
			out << " taintBBL: "<<endl;
			out << INS_Disassemble(ins) <<endl;
			if(INS_NextAddress(ins)>=BBL_Address(bbl)&&INS_NextAddress(ins)<=INS_Address(ins))
			{
				out << "find bbl loop"<<endl;
				//bblLoop = 1;
			}
		}
		*/
		if(bbl_taintedmem ==1 && ins==BBL_InsTail(bbl))
		{
//			out <<"find tainted bbl " <<endl;
//			out <<"bbl start address: "<< BBL_Address(bbl) <<endl;
//			out <<"bbl size: "<<BBL_Size(bbl) << endl;
//			out <<"bbl head: "<< INS_Disassemble(BBL_InsHead(bbl))<<endl;
//			out <<"bbl tail: "<< INS_Disassemble(ins) <<endl;
			if(INS_DirectBranchOrCallTargetAddress(ins)>=BBL_Address(bbl)&&INS_DirectBranchOrCallTargetAddress(ins)<=INS_Address(ins))
			{
				out<<endl<<"this tainted bbl is a loop"<<endl;
				//BBL_InsertCall(bbl,IPOINT_AFTER,(AFUNPTR)loopBblEnd,IARG_END);
			}
		}
    }// For INS
  }  // For BBL
  }//for enable DTA
} // VOID Trace