|
From: Myoungkyu S. <mks...@gm...> - 2014-09-23 01:31:14
|
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 ---*/
/*--------------------------------------------------------------------*/
|
|
From: Julian S. <js...@ac...> - 2014-09-26 16:23:31
|
On 09/23/2014 03:31 AM, Myoungkyu Song wrote: > 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. The problem is here > char* I_WRAP_SONAME_FNNAME_ZU(NONE,strcpy) ( char *dest, const char *src ) By using "(NONE,strcpy)" you are requesting to wrap the function strcpy in the main executable (that's what "NONE") means. But strcpy lives in libc.so. Really you need to use VG_Z_LIBC_SONAME (with a suitable definition for VG_Z_LIBC_SONAME) to make it work. Unfortunately the definition of VG_Z_LIBC_SONAME is system-dependent. You should be able to find all the examples you need in shared/vg_replace_strmem.c Note also, using -v is very helpful for debugging wrapping/intercept problems. If you need complete details, also try --trace-redir=yes. J |
|
From: Myoungkyu S. <mks...@gm...> - 2014-09-26 17:14:02
|
Please see the followings. After posting my question at that moment, I realized that I should specify the shared library name, because it's in libc as you said. However, it didn't work using VG_Z_LIBC_SONAME. Best regards, Myoungkyu ==================================================================== mksong@mksong:testwrap$ valgrind --trace-redir=yes -v ./w_main ==5111== Memcheck, a memory error detector ==5111== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==5111== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info ==5111== Command: ./w_main ==5111== --5111-- Valgrind options: --5111-- --trace-redir=yes --5111-- -v --5111-- Contents of /proc/version: --5111-- Linux version 3.13.0-35-generic (buildd@roseapple) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #62-Ubuntu SMP Fri Aug 15 01:58:01 UTC 2014 --5111-- Arch and hwcaps: X86, LittleEndian, x86-mmxext-sse1-sse2 --5111-- Page sizes: currently 4096, max supported 4096 --5111-- Valgrind library directory: /usr/local/lib/valgrind --5111-- << --5111-- ------ REDIR STATE after VG_(redir_initialise) ------ --5111-- TOPSPECS of soname (hardwired) --5111-- ld-linux.so.2 strlen R-> (0000.0) 0x380696b2 --5111-- ld-linux.so.2 index R-> (0000.0) 0x3806968d --5111-- ------ ACTIVE ------ --5111-- >> --5111-- Reading syms from /lib/i386-linux-gnu/ld-2.19.so --5111-- svma 0x0000000860, avma 0x0004000860 --5111-- Considering /lib/i386-linux-gnu/ld-2.19.so .. --5111-- .. CRC mismatch (computed 19686c0d wanted 131a893d) --5111-- Considering /usr/lib/debug/lib/i386-linux-gnu/ld-2.19.so .. --5111-- .. CRC is valid --5111-- << --5111-- ------ REDIR STATE after VG_(redir_notify_new_DebugInfo) ------ --5111-- TOPSPECS of soname ld-linux.so.2 filename /lib/i386-linux-gnu/ ld-2.19.so --5111-- TOPSPECS of soname (hardwired) --5111-- ld-linux.so.2 strlen R-> (0000.0) 0x380696b2 --5111-- ld-linux.so.2 index R-> (0000.0) 0x3806968d --5111-- ------ ACTIVE ------ --5111-- 0x04017ce0 (index ) R-> (0000.0) 0x3806968d ??? --5111-- 0x04017ed0 (strlen ) R-> (0000.0) 0x380696b2 ??? --5111-- >> --5111-- Reading syms from /home/mksong/workspace/.../valgrind_ubuntu14.04/valgrind-3.10.0/testwrap/w_main --5111-- svma 0x00080483a0, avma 0x00080483a0 valgrind: m_demangle/demangle.c:231 (vgPlain_maybe_Z_demangle): the 'impossible' happened. valgrind: symbol with a 'VG_Z_' prefix: _vgw00000ZU_VG_Z_LIBC_SONAME_strcpy. see pub_tool_redir.h for an explanation. host stacktrace: ==5111== at 0x3804E604: ??? (in /usr/local/lib/valgrind/memcheck-x86-linux) sched status: running_tid=0 Note: see also the FAQ in the source distribution. It contains workarounds to several common problems. In particular, if Valgrind aborted or crashed after identifying problems in your program, there's a good chance that fixing those problems will prevent Valgrind aborting or crashing, especially if it happened in m_mallocfree.c. If that doesn't help, please report this bug to: www.valgrind.org In the bug report, send all the above text, the valgrind version, and what OS and version you are using. Thanks. ==================================================================== On Fri, Sep 26, 2014 at 11:22 AM, Julian Seward <js...@ac...> wrote: > On 09/23/2014 03:31 AM, Myoungkyu Song wrote: > > 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. > > The problem is here > > > char* I_WRAP_SONAME_FNNAME_ZU(NONE,strcpy) ( char *dest, const char *src > ) > > By using "(NONE,strcpy)" you are requesting to wrap the function strcpy in > the main executable (that's what "NONE") means. But strcpy lives in > libc.so. Really you need to use VG_Z_LIBC_SONAME (with a suitable > definition for VG_Z_LIBC_SONAME) to make it work. Unfortunately the > definition of VG_Z_LIBC_SONAME is system-dependent. > > You should be able to find all the examples you need in > shared/vg_replace_strmem.c > > Note also, using -v is very helpful for debugging wrapping/intercept > problems. If you need complete details, also try --trace-redir=yes. > > J > > > > |