I am making a valgrind tool. I want to wrap a function "int getint(int x)". When I run my tool an assertion fails. This fails regardless if the client program contains getint().
==20490== wg-1.0, description
==20490== will
==20490== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==20490== Command: ./small2
==20490==
valgrind: m_redir.c:627 (vgPlain_redir_notify_new_DebugInfo): Assertion 'is_plausible_guest_addr(sym_avmas.main)' failed.
Segmentation fault (core dumped)
Here is my wg_main.c
#include "pub_tool_basics.h"
#include "pub_tool_tooliface.h"
#include "pub_tool_redir.h"
#include "valgrind.h"
int VG_WRAP_FUNCTION_ZU(NONE, getint)(int x);
int VG_WRAP_FUNCTION_ZU(NONE, getint)(int x)
{
int result;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
CALL_FN_W_W(result, fn, x);
return result;
}
static void wg_post_clo_init(void) {}
static IRSB* wg_instrument(VgCallbackClosure* closure, IRSB* bb_in,
const VexGuestLayout* layout,
const VexGuestExtents* vge,
const VexArchInfo* archinfo_host, IRType gWordTy,
IRType hWordTy) {
return bb_in;
}
static void wg_fini(Int exitcode) { VG_(printf)("Finished!"); }
static void wg_pre_clo_init(void) {
VG_(details_name)("wg");
VG_(details_version)("1.0");
VG_(details_description)("description");
VG_(details_copyright_author)("will");
VG_(details_bug_reports_to)(VG_BUGS_TO);
VG_(details_avg_translation_sizeB)(275);
VG_(basic_tool_funcs)(wg_post_clo_init, wg_instrument, wg_fini);
/* No needs, no core events to track */
}
VG_DETERMINE_INTERFACE_VERSION(wg_pre_clo_init)
I have also tried leaving the body of VG_WRAP_FUNCTION_ZU empty. I still receive the same assertion fail.
is_plausible_guest_addr(sym_avmas.main) is checking that the provided address is a valid guest address.
The code you have written does not provide a valid guest address, as the getint wrapping code is not guest code, but rather host code (part of the valgrind tool).
To wrap a function, you must put the wrapping code either in your application, or in a tool preloaded shared library.
See e.g. memcheck Makefile.am that is building a preload shared lib to wrap or replace functions such as strcpy.