Home | History | Annotate | Download | only in tests
      1 
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include "../memcheck.h"
      5 
      6 /* Program demonstrating copying of metadata in memcheck. */
      7 
      8 int main ( void )
      9 {
     10   int* a = malloc(10 * sizeof(int));
     11   int* b = malloc(10 * sizeof(int));
     12   int* v = malloc(10 * sizeof(int));
     13   int i, sum, res;
     14 
     15   for (i = 0; i < 10; i++) {
     16      if (i != 5)
     17         a[i] = i;
     18   }
     19 
     20   /* a[0 .. 4] and [6 .. 9] are defined, [5] is undefined. */
     21   for (i = 0; i < 10; i++)
     22      b[i] = 0;
     23 
     24   /* b[0 .. 9] is defined. */
     25 
     26   /* Get metadata for a and put it in v. */
     27   res = VALGRIND_GET_VBITS(a, v, 10*sizeof(int) );
     28   printf("result of GET is %d (1 for success)\n", res);
     29 
     30   for (i = 0; i < 10; i++)
     31      printf("%d 0x%08x\n", i, v[i]);
     32 
     33   /* and copy to b. */
     34   res = VALGRIND_SET_VBITS(b, v, 10*sizeof(int) );
     35   printf("result of SET is %d (1 for success)\n", res);
     36 
     37   /* Now we should have that b[5] is undefined since a[5] is
     38      undefined. */
     39   sum = 100;
     40   for (i = 0; i < 10; i++)
     41      sum += b[i];
     42 
     43   /* V should yelp at this point, that sum is undefined. */
     44   if (sum == 0)
     45     printf("sum == 0\n");
     46   else
     47     printf("sum != 0\n");
     48 
     49   return 0;
     50 }
     51