|
From: Shujunjun <shu...@hu...> - 2015-04-01 08:26:04
|
Hi,
I use I_WRAP_SONAME_FNNAME_ZZ to wrap “malloc”,it's quite strange when the guest program call "malloc" at the first time.
The first time "malloc" called in the "prog.c", it seems to call the "//block 1" code twice and call the "//block 2" twice in "test.wrap.sub.c".
But the second "malloc" called in the "prog.c", it seems normally.
PS. It's only appeared at shared library.
Following is my test code and output.
######in test.wrap.sub.c
#include <stdio.h>
#include "valgrind.h"
int catch_before = 0;
int catch_after = 0;
long I_WRAP_SONAME_FNNAME_ZZ(libcZdsoZd6, malloc)( long x )
{
int result = 0;
char buffer[60];
OrigFn fn;
////////////block 1
catch_before += 1;
sprintf(buffer, "----------- 111 wrap malloc: %x bytes, at:0x%x\n", x, result);
write(1, buffer, strlen(buffer));
VALGRIND_GET_ORIG_FN(fn);
CALL_FN_W_W(result, fn, x);
////////////block 2
(void) sprintf(buffer, "---------- 222 wrap malloc: %x bytes, at:0x%x\n", x, result);
(void) write(1, buffer, strlen(buffer));
catch_after += 3;
result += 1;
return result ;
}
#### in prog.c
#include <stdio.h>
#include <stdlib.h>
extern int catch_before;
extern int catch_after;
int test2()
{ int * addr1 = 0;
int * addr2 = 0;
printf("1.1 catch:%d %d\n", catch_before, catch_after);
addr1 = (int *)malloc( 10 * sizeof(int));
printf("2.1 catch:%d %d addr:0x%x \n", catch_before, catch_after, addr1);
printf("3.1 catch:%d %d\n", catch_before, catch_after);
addr2 = (int *)malloc( 10 * sizeof(int));
printf("4.1 catch:%d %d addr:0x%x \n", catch_before, catch_after, addr2);
return (int)addr1 * (int)addr2;
}
int main()
{
return test2();
}
########compile
gcc -c -m32 prog.c -O0 -o prog.32.o
gcc -o wrap.32.o -O0 -c -m32 test.wrap.sub.c
gcc prog.32.o wrap.32.o -O0 -m32 -o prog.wrap.32.exe
#########run with --tool=lackey or --tool=callgrind (eg. #valgrind --tool=lackey --trace-redir=yes ./prog.wrap.32.exe)
1.1 catch:0 0
--12339-- REDIR: 0x40a7f50 (libc.so.6:malloc) redirected to 0x80487ce (malloc)
----------- 111 wrap malloc: 190 bytes, at:0x0
----------- 111 wrap malloc: 190 bytes, at:0x0
---------- 222 wrap malloc: 190 bytes, at:0x804b008
---------- 222 wrap malloc: 190 bytes, at:0x804b009
2.1 catch:2 6 addr:0x804b00a
3.1 catch:2 6
----------- 111 wrap malloc: 28 bytes, at:0x0
---------- 222 wrap malloc: 28 bytes, at:0x804b1a0
4.1 catch:3 9 addr:0x804b1a1
shu...@hu...<mailto:shu...@hu...>
Best Regards.
|