xref: /llvm-project/compiler-rt/test/tysan/struct.c (revision 641fbf1524338c86c952ebb1ec8d2b497ada3cef)
1 // RUN: %clang_tysan -O0 %s -o %t && %run %t >%t.out 2>&1
2 // RUN: FileCheck %s < %t.out
3 
4 #include <stdio.h>
5 
6 typedef struct S1 {
7   int i1;
8 } s1;
9 typedef struct S2 {
10   int i2;
11 } s2;
12 
13 void g(int *i) {
14   *i = 5;
15   printf("%i\n", *i);
16 }
17 
18 void h(char *c) {
19   *c = 5;
20   printf("%i\n", (int)*c);
21 }
22 
23 void f(s1 *s1p, s2 *s2p) {
24   s1p->i1 = 2;
25   s2p->i2 = 3;
26   // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
27   // CHECK: WRITE of size 4 at {{.*}} with type int (in S2 at offset 0) accesses an existing object of type int (in S1 at offset 0)
28   // CHECK: {{#0 0x.* in f .*struct.c:}}[[@LINE-3]]
29   printf("%i\n", s1p->i1);
30 }
31 
32 int main() {
33   s1 s = {.i1 = 1};
34   f(&s, (s2 *)&s);
35   g(&s.i1);
36   h((char *)&s.i1);
37 }
38 
39 // CHECK-NOT: ERROR: TypeSanitizer: type-aliasing-violation
40