1// RUN: %clang_tysan -O0 %s -o %t && %run %t 10 >%t.out.0 2>&1
2// RUN: FileCheck %s < %t.out.0
3// RUN: %clang_tysan -O2 %s -o %t && %run %t 10 >%t.out 2>&1
4// RUN: FileCheck %s < %t.out
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10void __attribute__((noinline)) add_flt(float *a) {
11 *a += 2.0f;
12 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
13 // CHECK: READ of size 4 at {{.*}} with type float accesses an existing object of type int
14 // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-3]]
15 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
16 // CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type int
17 // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-6]]
18 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
19 // CHECK: READ of size 4 at {{.*}} with type float accesses an existing object of type long
20 // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-9]]
21 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
22 // CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type long
23 // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-12]]
24 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
25 // CHECK: READ of size 4 at {{.*}} with type float accesses part of an existing object of type long that starts at offset -4
26 // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-15]]
27 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
28 // CHECK: WRITE of size 4 at {{.*}} with type float accesses part of an existing object of type long that starts at offset -4
29 // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-18]]
30 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
31 // CHECK: READ of size 4 at {{.*}} with type float partially accesses an object of type short that starts at offset 2
32 // CHECK: {{#0 0x.* in add_flt .*basic.c:}}[[@LINE-21]]
33}
34
35int main(int argc, char *argv[]) {
36 int x = atoi(nptr: argv[1]);
37 add_flt(a: (float *)&x);
38 printf(format: "x = %d\n", x);
39
40 long y = x;
41 add_flt(a: (float *)&y);
42 printf(format: "y = %ld\n", y);
43
44 add_flt(a: ((float *)&y) + 1);
45 printf(format: "y = %ld\n", y);
46
47 char *mem = (char *)malloc(size: 4 * sizeof(short));
48 memset(s: mem, c: 0, n: 4 * sizeof(short));
49 *(short *)(mem + 2) = x;
50 add_flt(a: (float *)mem);
51 short s1 = *(short *)mem;
52 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
53 // CHECK: READ of size 2 at {{.*}} with type short accesses an existing object of type float
54 // CHECK: {{#0 0x.* in main .*basic.c:}}[[@LINE-3]]
55 short s2 = *(short *)(mem + 2);
56 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
57 // CHECK: READ of size 2 at {{.*}} with type short accesses part of an existing object of type float that starts at offset -2
58 // CHECK: {{#0 0x.* in main .*basic.c:}}[[@LINE-3]]
59 printf(format: "m[0] = %d, m[1] = %d\n", s1, s2);
60 free(ptr: mem);
61
62 return 0;
63}
64
65// CHECK-NOT: ERROR: TypeSanitizer: type-aliasing-violation
66

source code of compiler-rt/test/tysan/basic.c