Home | History | Annotate | Download | only in tests
      1 
      2 /* Check for correct handling of static vs non-static, local vs
      3    non-local variables in a zero-biased executable. */
      4 /* Relevant compile flags are:
      5 
      6    -Wall -g -I$prefix/include/valgrind
      7 
      8    eg -Wall -g -I`pwd`/Inst/include/valgrind
      9 */
     10 /* Unfortunately 2008 Feb 26, requires its own filter_varinfo3, since
     11    nonstatic_local_{un}def are not handled properly and so end up with
     12    compiler-dependent names, eg static_local_def.2919 and
     13    static_local_undef.2921.  So filter off the .nnnn part. */
     14 #include <stdio.h>
     15 #include <stdlib.h>
     16 #include <assert.h>
     17 #include "memcheck/memcheck.h"
     18 /* Cause memcheck to complain about the address "a" and so to print
     19    its best guess as to what "a" actually is.  a must be
     20    addressible. */
     21 void croak ( void* aV )
     22 {
     23   char* a = (char*)aV;
     24   char* undefp = malloc(1);
     25   char saved = *a;
     26   assert(undefp);
     27   *a = *undefp;
     28   VALGRIND_CHECK_MEM_IS_DEFINED(a, 1);
     29   *a = saved;
     30   free(undefp);
     31 }
     32 
     33 #include <stdio.h>
     34 
     35 static char static_global_def[10]    = {0,0,0,0,0, 0,0,0,0,0};
     36        char nonstatic_global_def[10] = {0,0,0,0,0, 0,0,0,0,0};
     37 static char static_global_undef[10];
     38        char nonstatic_global_undef[10];
     39 
     40 void bar ( char* p1, char* p2, char* p3, char* p4 )
     41 {
     42    croak(p1);
     43    croak(p2);
     44    croak(p3);
     45    croak(p4);
     46 }
     47 
     48 void foo ( void )
     49 {
     50    static char static_local_def[10]    = {0,0,0,0,0, 0,0,0,0,0};
     51           char nonstatic_local_def[10] = {0,0,0,0,0, 0,0,0,0,0};
     52    static char static_local_undef[10];
     53           char nonstatic_local_undef[10];
     54    croak ( 1 + (char*)&static_global_def );
     55    croak ( 2 + (char*)&nonstatic_global_def );
     56    croak ( 3 + (char*)&static_global_undef );
     57    croak ( 4 + (char*)&nonstatic_global_undef );
     58    bar( 5 + (char*)&static_local_def,
     59         6 + (char*)&nonstatic_local_def,
     60         7 + (char*)&static_local_undef,
     61         8 + (char*)&nonstatic_local_undef );
     62 }
     63 
     64 int main ( void )
     65 {
     66   foo();
     67   return 0;
     68 }
     69