• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #include <stdio.h>
3 #include "valgrind.h"
4 
5 /* Check that it's safe to call a wrapped function from some other
6    function's wrapper.  Note that because the wrapper for fact1
7    actually interferes with the computation of the result, this
8    program produces a different answer when run on V (162) from
9    natively (120).
10 */
11 
12 static int fact1 ( int n );
13 static int fact2 ( int n );
14 
15 /* This is needed to stop gcc4 turning 'fact' into a loop */
16 __attribute__((noinline))
mul(int x,int y)17 int mul ( int x, int y ) { return x * y; }
18 
fact1(int n)19 int fact1 ( int n )
20 {
21    if (n == 0) return 1; else return mul(n, fact2(n-1));
22 }
fact2(int n)23 int fact2 ( int n )
24 {
25    if (n == 0) return 1; else return mul(n, fact1(n-1));
26 }
27 
28 
I_WRAP_SONAME_FNNAME_ZU(NONE,fact1)29 int I_WRAP_SONAME_FNNAME_ZU(NONE,fact1) ( int n )
30 {
31    int    r;
32    OrigFn fn;
33    VALGRIND_GET_ORIG_FN(fn);
34    printf("in wrapper1-pre:  fact(%d)\n", n);
35    CALL_FN_W_W(r, fn, n);
36    printf("in wrapper1-post: fact(%d) = %d\n", n, r);
37    if (n >= 3) r += fact2(2);
38    return r;
39 }
40 
I_WRAP_SONAME_FNNAME_ZU(NONE,fact2)41 int I_WRAP_SONAME_FNNAME_ZU(NONE,fact2) ( int n )
42 {
43    int    r;
44    OrigFn fn;
45    VALGRIND_GET_ORIG_FN(fn);
46    printf("in wrapper2-pre:  fact(%d)\n", n);
47    CALL_FN_W_W(r, fn, n);
48    printf("in wrapper2-post: fact(%d) = %d\n", n, r);
49    return r;
50 }
51 
52 /* --------------- */
53 
main(void)54 int main ( void )
55 {
56    int r;
57    printf("computing fact1(5)\n");
58    r = fact1(5);
59    printf("fact1(5) = %d\n", r);
60    return 0;
61 }
62