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