Home | History | Annotate | Download | only in tests
      1 
      2 /* Test simple use of the disable/enable macros. */
      3 
      4 #include <stdlib.h>
      5 #include <stdio.h>
      6 
      7 #include "../include/valgrind.h"
      8 
      9 char* block = NULL;
     10 
     11 __attribute__((noinline)) void usechar ( char c )
     12 {
     13    // Spook gcc into believing mysterious bad things are
     14    // happening behind its back, and that 'c' is definitely
     15    // used in some (unknown) way.
     16    __asm__ __volatile__("" : : "r"(c) : "memory","cc");
     17 }
     18 
     19 __attribute__((noinline)) void err ( void )
     20 {
     21    usechar( block[5] );
     22 }
     23 
     24 int main ( void )
     25 {
     26   block = malloc(10);
     27   free(block);
     28 
     29   fprintf(stderr, "\n--------- SIMPLE TEST ---------\n\n");
     30   fprintf(stderr, "\n--------- enabled (expect 1) ---------\n\n");
     31 
     32   err();
     33 
     34   fprintf(stderr, "\n--------- disabled (expect 0) ---------\n\n");
     35   VALGRIND_DISABLE_ERROR_REPORTING;
     36 
     37   err();
     38 
     39   fprintf(stderr, "\n--------- re-enabled (expect 1) ---------\n\n");
     40   VALGRIND_ENABLE_ERROR_REPORTING;
     41 
     42   err();
     43 
     44 
     45 
     46   fprintf(stderr, "\n--------- MULTI-LEVEL TEST (expect 2) ---------\n\n");
     47 
     48   // 4 times
     49   VALGRIND_DISABLE_ERROR_REPORTING;
     50   VALGRIND_DISABLE_ERROR_REPORTING;
     51   VALGRIND_DISABLE_ERROR_REPORTING;
     52   VALGRIND_DISABLE_ERROR_REPORTING; // lev = 4
     53 
     54   // now gradually undo them until an error appears
     55   err();  // hidden
     56 
     57   VALGRIND_ENABLE_ERROR_REPORTING; // lev = 3
     58   err();  // hidden
     59 
     60   VALGRIND_ENABLE_ERROR_REPORTING; // lev = 2
     61   err();  // hidden
     62 
     63   VALGRIND_ENABLE_ERROR_REPORTING; // lev = 1
     64   err();  // hidden
     65 
     66   VALGRIND_ENABLE_ERROR_REPORTING; // lev = 0
     67   err();  // visible
     68 
     69   VALGRIND_ENABLE_ERROR_REPORTING; // lev = 0 (won't go down further)
     70   err();  // visible
     71 
     72   fprintf(stderr, "\n--------- MULTI-LEVEL TEST end ---------\n\n");
     73 
     74   return 0;
     75 }
     76