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#include <stdlib.h>
6
7struct X {
8 int i;
9 int j;
10};
11
12int foo(struct X *p, struct X *q) {
13 q->j = 1;
14 p->i = 0;
15 // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
16 // CHECK: WRITE of size 4 at {{.*}} with type int (in X at offset 0) accesses an existing object of type int (in X at offset 4)
17 // CHECK: {{#0 0x.* in foo .*struct-offset.c:}}[[@LINE-3]]
18 return q->j;
19}
20
21int main() {
22 unsigned char *p = malloc(size: 3 * sizeof(int));
23 printf(format: "%i\n", foo(p: (struct X *)(p + sizeof(int)), q: (struct X *)p));
24}
25
26// CHECK-NOT: ERROR: TypeSanitizer: type-aliasing-violation
27

source code of compiler-rt/test/tysan/struct-offset.c