aboutsummaryrefslogtreecommitdiff
path: root/compiler-rt/test/tysan/basic.c
blob: 28b94c425757ef1e4e70a4068ce8c3533fa93db3 (plain)
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
// RUN: %clang_tysan -O0 -mllvm -tysan-outline-instrumentation=false %s -o %t && %run %t 10 >%t.out.0 2>&1
// RUN: FileCheck %s < %t.out.0
// RUN: %clang_tysan -O2 -mllvm -tysan-outline-instrumentation=false %s -o %t && %run %t 10 >%t.out 2>&1
// RUN: FileCheck %s < %t.out
// RUN: %clang_tysan -O0 -mllvm -tysan-outline-instrumentation=true %s -o %t && %run %t 10 >%t.out.0 2>&1
// RUN: FileCheck %s < %t.out.0
// RUN: %clang_tysan -O2 -mllvm -tysan-outline-instrumentation=true %s -o %t && %run %t 10 >%t.out 2>&1
// RUN: FileCheck %s < %t.out

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void __attribute__((noinline)) add_flt(float *a) {
  *a += 2.0f;
  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
  // CHECK: READ of size 4 at {{.*}} with type float accesses an existing object of type int
  // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-3]]
  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
  // CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type int
  // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-6]]
  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
  // CHECK: READ of size 4 at {{.*}} with type float accesses an existing object of type long
  // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-9]]
  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
  // CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type long
  // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-12]]
  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
  // CHECK: READ of size 4 at {{.*}} with type float accesses part of an existing object of type long that starts at offset -4
  // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-15]]
  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
  // CHECK: WRITE of size 4 at {{.*}} with type float accesses part of an existing object of type long that starts at offset -4
  // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-18]]
  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
  // CHECK: READ of size 4 at {{.*}} with type float partially accesses an object of type short that starts at offset 2
  // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-21]]
}

int main(int argc, char *argv[]) {
  int x = atoi(argv[1]);
  add_flt((float *)&x);
  printf("x = %d\n", x);

  long y = x;
  add_flt((float *)&y);
  printf("y = %ld\n", y);

  add_flt(((float *)&y) + 1);
  printf("y = %ld\n", y);

  char *mem = (char *)malloc(4 * sizeof(short));
  memset(mem, 0, 4 * sizeof(short));
  *(short *)(mem + 2) = x;
  add_flt((float *)mem);
  short s1 = *(short *)mem;
  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
  // CHECK: READ of size 2 at {{.*}} with type short accesses an existing object of type float
  // CHECK: {{#0 0x.* in main .*basic.c:}}[[@LINE-3]]
  short s2 = *(short *)(mem + 2);
  // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
  // CHECK: READ of size 2 at {{.*}} with type short accesses part of an existing object of type float that starts at offset -2
  // CHECK: {{#0 0x.* in main .*basic.c:}}[[@LINE-3]]
  printf("m[0] = %d, m[1] = %d\n", s1, s2);
  free(mem);

  return 0;
}

// CHECK-NOT: ERROR: TypeSanitizer: type-aliasing-violation