Hello all,

I'm writing a Valgrind tool that makes use of function wrapping. From
what I understand, wrappers need to run on the simulated CPU. I wrote
a simple example which imitates the way Memcheck uses
mc_replace_strmem.c to generate a preloaded .so that contains my own
wrappers. A simplified version of my code is attached; I've been
having some problems with it.

After compiling and running the tool (using the 3.12.0 codebase on
Linux), the instrumented programs crash in odd ways:

$ inst/bin/valgrind --tool=simplewrap ls
==1201== SimpleWrap, do only basic function wrapping
==1201== basic example for mailing list
==1201== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==1201== Command: ls
==1201==
--1201-- VG_USERREQ__CLIENT_CALL1: func=0x0
--1201-- VG_USERREQ__CLIENT_CALL1: func=0x0
--1201-- VG_USERREQ__CLIENT_CALL1: func=0x0
--1201-- VG_USERREQ__CLIENT_CALL1: func=0x0
--1201-- VG_USERREQ__CLIENT_CALL1: func=0x0
--1201-- VG_USERREQ__CLIENT_CALL1: func=0x0
--1201-- VG_USERREQ__CLIENT_CALL1: func=0x0
ls: memory exhausted
==1201==

$ ./vg-in-place --tool=wrapsimple gcc --help

==1215== SimpleWrap, do only basic function wrapping
==1215== basic example for mailing list
==1215== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==1215== Command: gcc --help
==1215==
--1215-- VG_USERREQ__CLIENT_CALL1: func=0x0
--1215-- VG_USERREQ__CLIENT_CALL1: func=0x0
terminate called without an active exception
==1215==
==1215== Process terminating with default action of signal 6 (SIGABRT)
==1215==    at 0x516F91F: raise (raise.c:58)
==1215==    by 0x5171519: abort (abort.c:89)
==1215==    by 0x47347C: ??? (in /usr/bin/gcc)
==1215==    by 0x472F05: ??? (in /usr/bin/gcc)
==1215==    by 0x472F50: ??? (in /usr/bin/gcc)
==1215==    by 0x471C51: ??? (in /usr/bin/gcc)
==1215==    by 0x4717A7: ??? (in /usr/bin/gcc)
==1215==    by 0x4657B6: ??? (in /usr/bin/gcc)
==1215==    by 0x441FED: ??? (in /usr/bin/gcc)
==1215==    by 0x481AAC: ??? (in /usr/bin/gcc)
==1215==    by 0x515A38F: (below main) (libc-start.c:245)
==1215==
Aborted (core dumped)

These crashes also happen if I comment out the wrapper function and
compile the tool with an 'empty' sw_wrap.c, so the cause appears to
relate to something I'm (not) doing with the build system to set up
the preloaded .so, rather than the way the wrapper itself is written.
Does anyone have a suggestion for how to properly add function
wrappers to a Valgrind tool?

All the best,
   Serguei Makarov

Attachment: Makefile.am
Description: Binary data

#include "pub_tool_basics.h"
#include "pub_tool_tooliface.h"

static void sw_post_clo_init(void)
{
}

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

static void sw_fini(Int exitcode)
{
}

static void sw_pre_clo_init(void)
{
   VG_(details_name)            ("SimpleWrap");
   VG_(details_version)         (NULL);
   VG_(details_description)     ("do only basic function wrapping");
   VG_(details_copyright_author)(
      "basic example for mailing list");
   VG_(details_bug_reports_to)  (VG_BUGS_TO);

   VG_(details_avg_translation_sizeB) ( 275 );

   VG_(basic_tool_funcs)        (sw_post_clo_init,
                                 sw_instrument,
                                 sw_fini);

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

VG_DETERMINE_INTERFACE_VERSION(sw_pre_clo_init)
#include "pub_tool_basics.h"
// #include "pub_tool_poolalloc.h"
#include "pub_tool_hashtable.h"
#include "pub_tool_redir.h"
#include "pub_tool_tooliface.h"
#include "pub_tool_clreq.h"

static UWord count = 0;

#if 1
// #if 0
// Let's wrap malloc just for the sake of argument:
void *VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, malloc) (SSizeT size)
{
  OrigFn fn; VALGRIND_GET_ORIG_FN(fn);
  UWord result;
  CALL_FN_W_W(result, fn, size);
  count++;
  return result;
}
#endif
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to