/*BEGIN_LEGAL Intel Open Source License Copyright (c) 2002-2005 Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. END_LEGAL */ /* ===================================================================== */ /* @ORIGINAL_AUTHOR: Vijay Janapa Reddi */ /* ===================================================================== */ /*! @file Replace an original function with a custom function defined in the tool. The new function can have either the same or different signature from that of its original function. */ /* ===================================================================== */ #include "pin.H" #include using namespace std; typedef enum { A, B, C } abc; /* ===================================================================== */ typedef VOID * (*FUNCPTR_MALLOC)(size_t); static void * (*pf_malloc)(size_t size); /* ===================================================================== */ #if defined ( TARGET_IA32 ) || defined ( TARGET_IA32E ) VOID * Malloc_IA32( FUNCPTR_MALLOC orgFuncptr, UINT32 arg0, ADDRINT returnIp, ADDRINT esp, ADDRINT ebp ) { cout << "Malloc_IA32 (" << hex << (ADDRINT) orgFuncptr << ", " << dec << arg0 << ", " << hex << returnIp << "," << hex << esp << ", " << hex << ebp << ")" << endl << flush; if ( ADDRINT(orgFuncptr) != ADDRINT(pf_malloc) ) cout << "Error: original function pointers are different." << endl; VOID * v = orgFuncptr(arg0); return v; } #endif #if defined ( TARGET_IPF ) VOID * Malloc_IPF( FUNCPTR_MALLOC orgFuncptr, UINT32 arg0, ADDRINT returnIp, ADDRINT g12 ) { cout << "Malloc_IPF (" << hex << (ADDRINT) orgFuncptr << ", " << dec << arg0 << ", " << hex << returnIp << "," << hex << g12 << ")" << endl << flush; if ( ADDRINT(orgFuncptr) != ADDRINT(pf_malloc) ) cout << "Error: original function pointers are different." << endl; VOID * v = orgFuncptr(arg0); return v; } #endif /* ===================================================================== */ VOID ImageLoad(IMG img, VOID *v) { cout << IMG_Name(img) << endl; #if defined (TARGET_IA32) PROTO proto_malloc = PROTO_Allocate( PIN_PARG(void *), CALLINGSTD_CDECL, "malloc", PIN_PARG(int), PIN_PARG_END() ); #elif defined (TARGET_IPF) || defined (TARGET_IA32E) PROTO proto_malloc = PROTO_Allocate( PIN_PARG(void *), CALLINGSTD_DEFAULT, "malloc", PIN_PARG(int), PIN_PARG_END() ); #endif PROTO proto_noarg = PROTO_Allocate( PIN_PARG(int), CALLINGSTD_DEFAULT, "noarg", PIN_PARG_END() ); PROTO proto_enum = PROTO_Allocate( PIN_PARG(float), CALLINGSTD_DEFAULT, "enum", PIN_PARG(int), PIN_PARG_ENUM(abc), PIN_PARG(float), PIN_PARG_END() ); RTN rtn = RTN_FindByName(img, "malloc"); if (RTN_Valid(rtn)) { cout << "Replacing malloc in " << IMG_Name(img) << endl; #if defined ( TARGET_IA32 ) || defined ( TARGET_IA32E ) pf_malloc = (void * (*)(size_t))RTN_ReplaceSignatureProbed( rtn, AFUNPTR(Malloc_IA32), IARG_PROTOTYPE, proto_malloc, IARG_ORIG_FUNCPTR, IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_RETURN_IP, IARG_REG_VALUE, REG_ESP, IARG_REG_VALUE, REG_EBP, IARG_END); #else pf_malloc = (void * (*)(size_t))RTN_ReplaceSignatureProbed( rtn, AFUNPTR(Malloc_IPF), IARG_PROTOTYPE, proto_malloc, IARG_ORIG_FUNCPTR, IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_RETURN_IP, IARG_REG_VALUE, REG_G12, IARG_END); #endif } PROTO_Free( proto_malloc ); PROTO_Free( proto_noarg ); PROTO_Free( proto_enum ); } /* ===================================================================== */ int main(INT32 argc, CHAR *argv[]) { PIN_InitSymbols(); PIN_Init(argc, argv); IMG_AddInstrumentFunction(ImageLoad, 0); PIN_StartProgramProbed(); return 0; } /* ===================================================================== */ /* eof */ /* ===================================================================== */