Home | History | Annotate | Download | only in dfsan
      1 // RUN: %clang_dfsan %s -o %t
      2 // RUN: DFSAN_OPTIONS=dump_labels_at_exit=/dev/stdout %run %t 2>&1 | FileCheck %s
      3 // RUN: DFSAN_OPTIONS=dump_labels_at_exit=/dev/stdout not %run %t c 2>&1 | FileCheck %s --check-prefix=CHECK-OOL
      4 // RUN: DFSAN_OPTIONS=dump_labels_at_exit=/dev/stdout not %run %t u 2>&1 | FileCheck %s --check-prefix=CHECK-OOL
      5 
      6 // Tests that labels are properly dumped at program termination.
      7 
      8 #include <sanitizer/dfsan_interface.h>
      9 #include <assert.h>
     10 #include <stdio.h>
     11 
     12 int main(int argc, char** argv) {
     13   int i = 1;
     14   dfsan_label i_label = dfsan_create_label("i", 0);
     15   dfsan_set_label(i_label, &i, sizeof(i));
     16 
     17   int j = 2;
     18   dfsan_label j_label = dfsan_create_label("j", 0);
     19   dfsan_set_label(j_label, &j, sizeof(j));
     20 
     21   int k = 3;
     22   dfsan_label k_label = dfsan_create_label("k", 0);
     23   dfsan_set_label(k_label, &k, sizeof(k));
     24 
     25   dfsan_label ij_label = dfsan_get_label(i + j);
     26   dfsan_label ijk_label = dfsan_get_label(i + j + k);
     27 
     28   fprintf(stderr, "i %d j %d k %d ij %d ijk %d\n", i_label, j_label, k_label,
     29           ij_label, ijk_label);
     30 
     31   // CHECK: 1 0 0 i
     32   // CHECK: 2 0 0 j
     33   // CHECK: 3 0 0 k
     34   // CHECK: 4 1 2
     35   // CHECK: 5 3 4
     36 
     37   if (argc > 1) {
     38     // Exhaust the labels.
     39     unsigned long num_labels = 1 << (sizeof(dfsan_label) * 8);
     40     for (unsigned long i =  ijk_label + 1; i < num_labels - 2; ++i) {
     41       dfsan_label l = dfsan_create_label("l", 0);
     42       assert(l == i);
     43     }
     44 
     45     // Consume the last available label.
     46     dfsan_label l = dfsan_union(5, 6);
     47     assert(l == num_labels - 2);
     48 
     49     // Try to allocate another label (either explicitly or by unioning two
     50     // existing labels), but expect a crash.
     51     if (argv[1][0] == 'c') {
     52       l = dfsan_create_label("l", 0);
     53     } else {
     54       l = dfsan_union(6, 7);
     55     }
     56 
     57     // CHECK-OOL: FATAL: DataFlowSanitizer: out of labels
     58     // CHECK-OOL: 1 0 0 i
     59     // CHECK-OOL: 2 0 0 j
     60     // CHECK-OOL: 3 0 0 k
     61     // CHECK-OOL: 4 1 2
     62     // CHECK-OOL: 5 3 4
     63     // CHECK-OOL: 6 0 0
     64     // CHECK-OOL: 65534 5 6
     65     // CHECK-OOL: 65535 0 0 <init label>
     66   }
     67 
     68   return 0;
     69 }
     70