Hello,

I have a question, which maybe was discussed.  I would like to implement a
wrapper function for standard API such as "strcpy".  However, I failed to
do.  When I tested a user-defined function like "hello(char *, char *)"
below, I successfully wrapped it, accessing to arguments and a return
value.  Please see the code being as followings.  I have tested in Ubuntu
14.04.1 (installed at VirtualBox), gcc 4.8.2, and valgrind-3.10.0.

I'd appreciate it if you'd share your comments or advice.

Thanks in advance,
Myoungkyu

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// run/output
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mksong@mksong:testwrap$ ../inst/bin/valgrind --tool=testinst ./w_main
==3747== Testinstgrind, the minimal Valgrind tool
==3747==
==3747== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==3747== Command: ./w_main
==3747==
wrapper-pre-hello: 4 4
hello
wrapper-post-hello
==3747==

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// w_main.c
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "valgrind.h"

extern void hello ( char *dest, const char *src );

void I_WRAP_SONAME_FNNAME_ZU(NONE,hello) ( char *dest, const char *src )
{
   OrigFn fn;
   VALGRIND_GET_ORIG_FN(fn);
   printf("wrapper-pre-hello: %d %d\n", sizeof(dest), sizeof(src));
   CALL_FN_v_WW(fn, dest, src);
   printf("wrapper-post-hello\n");
}

char* I_WRAP_SONAME_FNNAME_ZU(NONE,strcpy) ( char *dest, const char *src )
{
   OrigFn fn;
   char *result;
   VALGRIND_GET_ORIG_FN(fn);
   printf("wrapper-pre-strcpy\n");
   CALL_FN_W_WW(result, fn, dest, src);
   printf("wrapper-post-strcpy\n");
   return result;
}

int main(int argc, char** argv) {
char *str;
str = (char *) malloc(15);
hello(str, "Hello world");
strcpy(str, "Hello strcpy!");
return 1;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// hello.c
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void hello(char dest[], const char *src) {
    printf( "hello\n");
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ti_main.c <not yet developed>
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "pub_tool_basics.h"
#include "pub_tool_tooliface.h"

static void ti_post_clo_init(void)
{
}

static
IRSB* ti_instrument ( VgCallbackClosure* closure,
                      IRSB* bb,
                      VexGuestLayout* layout,
                      VexGuestExtents* vge,
                      VexArchInfo* archinfo_host,
                      IRType gWordTy, IRType hWordTy )
{
    return bb;
}

static void ti_fini(Int exitcode)
{
}

static void ti_pre_clo_init(void)
{
   VG_(details_name)            ("Testinstgrind");
   VG_(details_version)         (NULL);
   VG_(details_description)     ("the minimal Valgrind tool");
   VG_(details_copyright_author)("");
   VG_(details_bug_reports_to)  (VG_BUGS_TO);

   VG_(details_avg_translation_sizeB) ( 275 );

   VG_(basic_tool_funcs)        (ti_post_clo_init,
                                 ti_instrument,
                                 ti_fini);

   /* No needs, no core events to track */
}

VG_DETERMINE_INTERFACE_VERSION(ti_pre_clo_init)

/*--------------------------------------------------------------------*/
/*--- end                                                          ---*/
/*--------------------------------------------------------------------*/
------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to