llvm / llvm/llvm-project

[compiler-rt][tysan] False positive for struct member array element access

Open
#210,643 0 comments 0 reactions 0 assignees View on GitHub
compiler-rt:tysan false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

# `struct A { int elems[3]; }`

#### Flags: `-fsanitize=type -O0` or `-fsanitize=type -O1`
#### Compiler: x86-64 clang 22.1.0
#### Link: https://godbolt.org/z/ddrb1q9zM

#### Code:
```cpp
struct A {
int elems[3];
};

A a;

int main() {
a.elems[0] = 1;
}
```

#### Logs:
```text
ASM generation compiler returned: 0
Execution build compiler returned: 0
Program returned: 0
==1==ERROR: TypeSanitizer: type-aliasing-violation on address 0x5ab0a1080ee4 (pc 0x5ab0a0726f87 bp 0x7ffeaab9afa0 sp 0x7ffeaab9af30 tid 1)
WRITE of size 4 at 0x5ab0a1080ee4 with type int accesses an existing object of type A
#0 0x5ab0a0726f86 (/app/output.s+0x35f86)
```

#### Explanation:
A global object of type `A` contains an `int[3]` member; `a.elems[0]` designates an `int` subobject of `a`, and writing to that subobject (`a.elems[0] = 1;`) is valid C++ and does not violate aliasing rules. TypeSanitizer incorrectly reports this write as a type-aliasing violation on the enclosing `A` object.

Contributor guide

Open the contributing guide

Research direction

The issue names no repository files or tests. Start by reproducing the global struct-member array access with clang using -fsanitize=type -O0 and -O1, using the linked Compiler Explorer example; done means the valid write runs without a TypeSanitizer type-aliasing-violation report.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.