Home | History | Annotate | Download | only in tests
      1 
      2 /* Check of variable location identification when using .debug_types.  */
      3 
      4 /* Relevant compile flags are:
      5 
      6    -Wall -g -I$prefix/include/valgrind -gdwarf-4 -fdebug-types-section
      7 
      8    eg -Wall -g -I`pwd`/Inst/include/valgrind -gdwarf-4 -fdebug-types-section
      9 */
     10 
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 #include <string.h>
     14 #include <assert.h>
     15 #include "tests/sys_mman.h"
     16 #include <sys/types.h>
     17 #include <sys/stat.h>
     18 #include <fcntl.h>
     19 #include "memcheck/memcheck.h"
     20 
     21 /* Cause memcheck to complain about the address "a" and so to print
     22    its best guess as to what "a" actually is.*/
     23 void croak ( void* aV )
     24 {
     25   if(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(aV,1) != 0)
     26      return;
     27   char* a = (char*)aV;
     28   char* undefp = malloc(1);
     29   char saved = *a;
     30   assert(undefp);
     31   *a = *undefp;
     32   (void) VALGRIND_CHECK_MEM_IS_DEFINED(a, 1);
     33   *a = saved;
     34   free(undefp);
     35 }
     36 
     37 struct s1
     38 {
     39   char c;
     40   short s;
     41   int i;
     42   long l;
     43   float f;
     44   double d;
     45 };
     46 
     47 struct s1 S2[30];
     48 
     49 int main ( void )
     50 {
     51   struct s1 local;
     52   struct s1* onheap = malloc(sizeof (struct s1));
     53   void *p, *q;
     54   int fd;
     55   int n;
     56   char filename[256];
     57 
     58   assert(onheap);
     59   croak(&onheap->i);
     60 
     61   croak( &S2[0].i );
     62   croak( &local.i );
     63 
     64   /* Describe anonymous mmap-ed */
     65   p = mmap( 0, 16 * 1024, PROT_READ|PROT_WRITE,
     66             MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
     67   assert(p != MAP_FAILED);
     68   croak( p);
     69 
     70   /* Describe file mmap-ed */
     71   snprintf(filename, sizeof(filename), "./valgrind-dw4-test.%ld",
     72            (long) getpid());
     73 
     74   unlink(filename);
     75 
     76   fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
     77   assert (fd > 0);
     78   n = write(fd, filename, strlen(filename));
     79   assert (n > 8);
     80   q = mmap(NULL, 100, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
     81   assert (q != MAP_FAILED);
     82   croak( q);
     83   unlink(filename);
     84 
     85   /* Describe memory in or past the heap end. */
     86   void *addr = sbrk(0);
     87   croak(addr); // in the first brk page, after brk_limit
     88   sbrk(4 * 1024); // increase brk segment
     89   croak(addr); // Now, must be inside.
     90   addr = (void *) ((char*)addr + 2 * 1024);
     91   croak(addr); // Must still be inside.
     92   sbrk(-3*1024);
     93   croak(addr); // Must now be after.
     94 
     95   return 0;
     96 }
     97