|
From: <sv...@va...> - 2005-12-07 17:39:01
|
Author: sewardj
Date: 2005-12-07 17:38:48 +0000 (Wed, 07 Dec 2005)
New Revision: 5311
Log:
Some simple tests for the function wrapping machinery. They work.
Not connected to regtest driver yet tho.
Added:
branches/FNWRAP/memcheck/tests/wrap1.c
branches/FNWRAP/memcheck/tests/wrap2.c
branches/FNWRAP/memcheck/tests/wrap3.c
branches/FNWRAP/memcheck/tests/wrap4.c
branches/FNWRAP/memcheck/tests/wrap5.c
Added: branches/FNWRAP/memcheck/tests/wrap1.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- branches/FNWRAP/memcheck/tests/wrap1.c (rev 0=
)
+++ branches/FNWRAP/memcheck/tests/wrap1.c 2005-12-07 17:38:48 UTC (rev 5=
311)
@@ -0,0 +1,33 @@
+
+#include <stdio.h>
+#include "valgrind.h"
+
+/* The simplest possible wrapping test: just call a wrapped function
+ and check we run the wrapper instead. */
+
+/* The "original" function */
+void actual ( void )
+{
+ printf("in actual\n");
+}
+
+/* The wrapper. Since this executable won't have a soname, we have to
+ use "NONE", since V treats any executable/.so which lacks a soname
+ as if its soname was "NONE". */
+void I_REPLACE_SONAME_FNNAME_ZU(NONE,actual) ( void )
+{
+ printf("wrapper-pre\n");
+
+ CALL_ORIG_VOIDFN_0(actual);
+
+ printf("wrapper-post\n");
+}
+
+/* --------------- */
+
+int main ( void )
+{
+ printf("starting\n");
+ actual();
+ return 0;
+}
Added: branches/FNWRAP/memcheck/tests/wrap2.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- branches/FNWRAP/memcheck/tests/wrap2.c (rev 0=
)
+++ branches/FNWRAP/memcheck/tests/wrap2.c 2005-12-07 17:38:48 UTC (rev 5=
311)
@@ -0,0 +1,34 @@
+
+#include <stdio.h>
+#include "valgrind.h"
+
+/* Check that function wrapping works for a recursive function. */
+
+/* This is needed to stop gcc4 turning 'fact' into a loop */
+__attribute__((noinline))
+int mul ( int x, int y ) { return x * y; }
+
+int fact ( int n )
+{
+ if (n =3D=3D 0) return 1; else return mul(n, fact(n-1));
+}
+
+int I_REPLACE_SONAME_FNNAME_ZU(NONE,fact) ( int n )
+{
+ int r;
+ printf("in wrapper1-pre: fact(%d)\n", n);
+ CALL_ORIG_FN_1(r,fact,n);
+ printf("in wrapper1-post: fact(%d) =3D %d\n", n, r);
+ return r;
+}
+
+/* --------------- */
+
+int main ( void )
+{
+ int r;
+ printf("computing fact(5)\n");
+ r =3D fact(5);
+ printf("fact(5) =3D %d\n", r);
+ return 0;
+}
Added: branches/FNWRAP/memcheck/tests/wrap3.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- branches/FNWRAP/memcheck/tests/wrap3.c (rev 0=
)
+++ branches/FNWRAP/memcheck/tests/wrap3.c 2005-12-07 17:38:48 UTC (rev 5=
311)
@@ -0,0 +1,52 @@
+
+#include <stdio.h>
+#include "valgrind.h"
+
+/* Check that function wrapping works for a mutually recursive
+ pair. */
+
+static int fact1 ( int n );
+static int fact2 ( int n );
+
+/* This is needed to stop gcc4 turning 'fact' into a loop */
+__attribute__((noinline))
+int mul ( int x, int y ) { return x * y; }
+
+int fact1 ( int n )
+{
+ if (n =3D=3D 0) return 1; else return mul(n, fact2(n-1));
+}
+int fact2 ( int n )
+{
+ if (n =3D=3D 0) return 1; else return mul(n, fact1(n-1));
+}
+
+
+int I_REPLACE_SONAME_FNNAME_ZU(NONE,fact1) ( int n )
+{
+ int r;
+ printf("in wrapper1-pre: fact(%d)\n", n);
+ CALL_ORIG_FN_1(r,fact1,n);
+ printf("in wrapper1-post: fact(%d) =3D %d\n", n, r);
+ return r;
+}
+
+int I_REPLACE_SONAME_FNNAME_ZU(NONE,fact2) ( int n )
+{
+ int r;
+ printf("in wrapper2-pre: fact(%d)\n", n);
+ CALL_ORIG_FN_1(r,fact2,n);
+ printf("in wrapper2-post: fact(%d) =3D %d\n", n, r);
+ return r;
+}
+
+/* --------------- */
+
+int main ( void )
+{
+ int r;
+ printf("computing fact1(5)\n");
+ r =3D fact1(5);
+ printf("fact1(5) =3D %d\n", r);
+ return 0;
+}
Added: branches/FNWRAP/memcheck/tests/wrap4.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- branches/FNWRAP/memcheck/tests/wrap4.c (rev 0=
)
+++ branches/FNWRAP/memcheck/tests/wrap4.c 2005-12-07 17:38:48 UTC (rev 5=
311)
@@ -0,0 +1,57 @@
+
+#include <stdio.h>
+#include "valgrind.h"
+
+/* Check that it's safe to call a wrapped function from some other
+ function's wrapper. Note that because the wrapper for fact1
+ actually interferes with the computation of the result, this
+ program produces a different answer when run on V (162) from
+ natively (120).
+*/
+
+static int fact1 ( int n );
+static int fact2 ( int n );
+
+/* This is needed to stop gcc4 turning 'fact' into a loop */
+__attribute__((noinline))
+int mul ( int x, int y ) { return x * y; }
+
+int fact1 ( int n )
+{
+ if (n =3D=3D 0) return 1; else return mul(n, fact2(n-1));
+}
+int fact2 ( int n )
+{
+ if (n =3D=3D 0) return 1; else return mul(n, fact1(n-1));
+}
+
+
+int I_REPLACE_SONAME_FNNAME_ZU(NONE,fact1) ( int n )
+{
+ int r;
+ printf("in wrapper1-pre: fact(%d)\n", n);
+ CALL_ORIG_FN_1(r,fact1,n);
+ printf("in wrapper1-post: fact(%d) =3D %d\n", n, r);
+ if (n >=3D 3) r +=3D fact2(2);
+ return r;
+}
+
+int I_REPLACE_SONAME_FNNAME_ZU(NONE,fact2) ( int n )
+{
+ int r;
+ printf("in wrapper2-pre: fact(%d)\n", n);
+ CALL_ORIG_FN_1(r,fact2,n);
+ printf("in wrapper2-post: fact(%d) =3D %d\n", n, r);
+ return r;
+}
+
+/* --------------- */
+
+int main ( void )
+{
+ int r;
+ printf("computing fact1(5)\n");
+ r =3D fact1(5);
+ printf("fact1(5) =3D %d\n", r);
+ return 0;
+}
Added: branches/FNWRAP/memcheck/tests/wrap5.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- branches/FNWRAP/memcheck/tests/wrap5.c (rev 0=
)
+++ branches/FNWRAP/memcheck/tests/wrap5.c 2005-12-07 17:38:48 UTC (rev 5=
311)
@@ -0,0 +1,91 @@
+
+#include <stdio.h>
+#include <malloc.h>
+#include "valgrind.h"
+
+/* As wrap4.c, but also throw in various calls to another redirected
+ function (malloc) to check that that doesn't screw anything up.
+*/
+
+typedef=20
+ struct _Lard {
+ struct _Lard* next;=20
+ char stuff[999];=20
+ }
+ Lard;
+
+Lard* lard =3D NULL;
+static int ctr =3D 0;
+
+void addMoreLard ( void )
+{
+ Lard* p;
+ ctr++;
+ if ((ctr % 3) =3D=3D 1) {
+ p =3D malloc(sizeof(Lard));
+ p->next =3D lard;
+ lard =3D p;
+ }
+}
+
+
+static int fact1 ( int n );
+static int fact2 ( int n );
+
+/* This is needed to stop gcc4 turning 'fact' into a loop */
+__attribute__((noinline))
+int mul ( int x, int y ) { return x * y; }
+
+int fact1 ( int n )
+{
+ addMoreLard();
+ if (n =3D=3D 0) return 1; else return mul(n, fact2(n-1));
+}
+int fact2 ( int n )
+{
+ addMoreLard();
+ if (n =3D=3D 0) return 1; else return mul(n, fact1(n-1));
+}
+
+
+int I_REPLACE_SONAME_FNNAME_ZU(NONE,fact1) ( int n )
+{
+ int r;
+ printf("in wrapper1-pre: fact(%d)\n", n);
+ addMoreLard();
+ CALL_ORIG_FN_1(r,fact1,n);
+ addMoreLard();
+ printf("in wrapper1-post: fact(%d) =3D %d\n", n, r);
+ if (n >=3D 3) r +=3D fact2(2);
+ return r;
+}
+
+int I_REPLACE_SONAME_FNNAME_ZU(NONE,fact2) ( int n )
+{
+ int r;
+ printf("in wrapper2-pre: fact(%d)\n", n);
+ addMoreLard();
+ CALL_ORIG_FN_1(r,fact2,n);
+ addMoreLard();
+ printf("in wrapper2-post: fact(%d) =3D %d\n", n, r);
+ return r;
+}
+
+/* --------------- */
+
+int main ( void )
+{
+ int r;
+ Lard *p, *p_next;
+ printf("computing fact1(7)\n");
+ r =3D fact1(7);
+ printf("fact1(7) =3D %d\n", r);
+
+ printf("allocated %d Lards\n", ctr);
+ for (p =3D lard; p; p =3D p_next) {
+ p_next =3D p->next;
+ free(p);
+ }
+
+ return 0;
+}
|