<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/120855>120855</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed tysan violation with optimization
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
thesamesam
</td>
</tr>
</table>
<pre>
With `-O0 -fsanitize=type`, we detect the violation correctly, but fail to with `-O1` or greater:
```
#include <stdbool.h>
typedef struct k {
int a;
int b;
} k;
typedef struct l {
bool a;
bool b;
} l;
k my_k;
l my_l;
void frobnicate_the_struct(k *my_k) {
int a = ((k*) &my_k)->a;
__builtin_printf("got k.a=%d\n", a);
}
int main() {
frobnicate_the_struct((k*)&my_l);
}
```
```
$ clang -O0 -fsanitize=type a.c -o a && ./a
==1==ERROR: TypeSanitizer: type-aliasing-violation on address 0x7ffcfff2a198 (pc 0x5f6739b118d1 bp 0x7ffcfff2a110 sp 0x7ffcfff2a0b8 tid 1)
READ of size 4 at 0x7ffcfff2a198 with type int (in k at offset 0) accesses an existing object of type p1 _ZTS1k
#0 0x5f6739b118d0 (/app/output.s+0x2a8d0)
got k.a=977714280
```
```
$ clang -O1 -fsanitize=type a.c -o a && ./a
got k.a=1389137000
```
godbolt: https://godbolt.org/z/cYPT3vYnY
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVM1u6zgPfRp6QySQKP8uvHCbevfhfugtMOhsAtmWE00Uy7DktunTD-RkmjRzB5gBElgSycND8kjSOb0blCoheYBkE8nZ7-1U-r1y8hj-UWO7U_mb9nuElK1-MFz1Tg7a608FYuNPo4KUAT3iu8JOedV69HuFb9oa6bUdsLXTpFpvTsGpmT32Uhv0Ft-_QDmkDO2Eu0lJryYQFbAqwJ5_rAISemjN3CkE8eh811hr1nsQT8HIqkCjUz06P82txwNC9gCsQkTUg0cJ4nbbnLeQbfBwWf4NwtxAhGS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