File: conditional_callbacks.c

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (108 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// RUN: %clang_dfsan -fno-sanitize=dataflow -O2 -fPIE -DCALLBACKS -c %s -o %t-callbacks.o
// RUN: %clang_dfsan -fsanitize-ignorelist=%S/Inputs/flags_abilist.txt -O2 -mllvm -dfsan-conditional-callbacks %s %t-callbacks.o -o %t
// RUN: %run %t FooBarBaz 2>&1 | FileCheck %s
//
// RUN: %clang_dfsan -fno-sanitize=dataflow -O2 -fPIE -DCALLBACKS -DORIGINS -c %s -o %t-callbacks-orig.o
// RUN: %clang_dfsan -fsanitize-ignorelist=%S/Inputs/flags_abilist.txt -O2 -mllvm -dfsan-conditional-callbacks -mllvm -dfsan-track-origins=1 -DORIGINS %s %t-callbacks-orig.o -o %t-orig
// RUN: %run %t-orig FooBarBaz 2>&1 | FileCheck %s

// Tests that callbacks are inserted for conditionals when
// -dfsan-conditional-callbacks is specified.

#include <assert.h>
#include <sanitizer/dfsan_interface.h>
#include <stdio.h>
#include <string.h>

#ifdef CALLBACKS
// Compile this code without DFSan to avoid recursive instrumentation.

extern dfsan_label LabelI;
extern dfsan_label LabelJ;
extern dfsan_label LabelIJ;

void my_dfsan_conditional_callback(dfsan_label Label, dfsan_origin Origin) {
  assert(Label != 0);
#ifdef ORIGINS
  assert(Origin != 0);
#else
  assert(Origin == 0);
#endif

  static int Count = 0;
  switch (Count++) {
  case 0:
    assert(Label == LabelI);
    break;
  case 1:
    assert(Label == LabelJ);
    break;
  case 2:
    assert(Label == LabelIJ);
    break;
  default:
    break;
  }

  fprintf(stderr, "Label %u used as condition\n", Label);
}

#else
// Compile this code with DFSan and -dfsan-conditional-callbacks to insert the
// callbacks.

dfsan_label LabelI;
dfsan_label LabelJ;
dfsan_label LabelIJ;

extern void my_dfsan_conditional_callback(dfsan_label Label,
                                          dfsan_origin Origin);

int main(int Argc, char *Argv[]) {
  assert(Argc == 2);

  dfsan_set_conditional_callback(my_dfsan_conditional_callback);

  int result = 0;
  // Make these not look like constants, otherwise the branch we're expecting
  // may be optimized out.
  int DataI = (Argv[0][0] != 0) ? 1 : 0;
  int DataJ = (Argv[1][0] != 0) ? 2 : 0;
  LabelI = 1;
  dfsan_set_label(LabelI, &DataI, sizeof(DataI));
  LabelJ = 2;
  dfsan_set_label(LabelJ, &DataJ, sizeof(DataJ));
  LabelIJ = dfsan_union(LabelI, LabelJ);

  assert(dfsan_get_label(DataI) == LabelI);

  // CHECK: Label 1 used as condition
  if (DataI) {
    result = 42;
  }

  assert(dfsan_get_label(DataJ) == LabelJ);

  // CHECK: Label 2 used as condition
  switch (DataJ) {
  case 1:
    result += 10000;
    break;
  case 2:
    result += 4200;
    break;
  default:
    break;
  }

  int tainted_cond = ((DataI * DataJ) != 1);
  assert(dfsan_get_label(tainted_cond) == LabelIJ);

  // CHECK: Label 3 used as condition
  result = tainted_cond ? result + 420000 : 9;

  assert(result == 424242);
  return 0;
}

#endif // #ifdef CALLBACKS