<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/101671>101671</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[asan] False positive ODR violation error for global data (e.g. vtable)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
glebov-andrey
</td>
</tr>
</table>
<pre>
Hi!
This issue initially manifested itself as an ODR violation for `vtable for boost::filesystem::filesystem_error` when Boost.Filesystem is a static library linked into two shared libraries (built with Clang).
After some minimization it appears to be an issue common to all global non-weak data (symbol types `D` and `B` in `nm`'s output). On Clang this affects vtables while on GCC it does not because its vtables are weak symbols (or maybe they just aren't checked since they don't have associated `__odr_asan.` symbols?).
Here's the setup which reproduces the issue on both Clang and GCC:
- Static library `fs` includes an `extern int global_var`.
- Shared library `lib` uses `global_var` from `fs` somehow to avoid it being discarded.
- Executable `exe` uses `global_var` from `fs` somehow to avoid it being discarded, and also links to `lib`.
When running the executable we get the error:
```
=================================================================
==16997==ERROR: AddressSanitizer: odr-violation (0x7fb16f5bafc0):
[1] size=4 'fs::global_var' fs_error.cpp in out/exe
[2] size=4 'fs::global_var' fs_error.cpp in out/lib.so
These globals were registered at these points:
[1]:
#0 0x7fb16eadc752 (out/exe+0x41752)
#1 0x7fb16eadd8f9 (out/exe+0x428f9)
#2 0x7fb16e489eba (/lib/x86_64-linux-gnu/libc.so.6+0x29eba) (BuildId: 490fef8403240c91833978d494d39e537409b92e)
[2]:
#0 0x7fb16eadc752 (out/exe+0x41752)
#1 0x7fb16eadd8f9 (out/exe+0x428f9)
#2 0x7fb16ea5647d (/lib64/ld-linux-x86-64.so.2+0x647d) (BuildId: 4186944c50f8a32b47d74931e3f512b811813b64)
==16997==HINT: if you don't care about these errors you may set ASAN_OPTIONS=detect_odr_violation=0
SUMMARY: AddressSanitizer: odr-violation: global 'fs::global_var' at fs_error.cpp in out/exe
==16997==ABORTING
```
I've minimized the flags required and really the only one is `-fsanitize=address`:
```sh
cpp_flags=(-fPIC -O0 -fsanitize=address)
link_flags=("-Wl,-rpath,\$ORIGIN")
```
`-fvisibility=hidden` doesn't affect anything as long as the symbols of interest are marked `[[gnu::visibility("default")]]`.
I'm attaching a zip file with a full example: [asan-odr-repro.zip](https://github.com/user-attachments/files/16471014/asan-odr-repro.zip)
Tested on Linux x86_64.
Clang 16.0.6, 18.1.8 and `main` all exhibit the same behavior including for vtables.
GCC 11.4 and 12.3 only exhibit the behavior for global variables.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUV11v47oR_TX0y8CCRH1YevCDY1_vBujdXGRTXPQpoKSRxbsU6YqUE-fXF0MqjpNNv9D2oYBhy6I4nDlzZuZIWCsPGnHN8huW7xZicr0Z1weFtTkthW5HPC9q057XXyXjCYt3LN6E74deWpDWTghSSyeFUmcYhJYdWoctSGdRdSAsCA13u3s4SaOEk0ZDZ0ZgRXxyolbo_9XGWMfSDUs3nVRoz9bh8PH_I46jGVkRw1OPGm5oT7S_LIO0IMA64WQDStajGM-gpP5BzmhnwD0ZsL0YsZ2XJVpgvKwnqRw8SdfDVgl9YLyKQoybzuEI1gwIg9RykC8hAOlAHI8oRgvOQI0UYoCiMcNgNN0VSsFBmVoo0EYvn1D8gFY4QSfa81AbBe58JA-KeEdBCd3S9Q1dS02XemBFzPjKgpnccXLkGNzp4CU4SoDoOmychYClhadeKgSj4ct2S262Bi1o46DGRkwWKS2Xh8WI4P0K_ngwzAiDONcIrscz_DFZR49pxlcOmh4bQtNK3cwPtCYs9eKEIKw1jRSUfVbEj4-mHR-FFTqikOYzWLp_wzd8f8URfZSuR7DopiOF0fQw4nE07dRgWAoIGw21eU2VB-3LdktU8baW8P09AVgRdzZA2qipRU9HVsT47HDURIw5S48nQdyKLnaumeLtKFmTocmGpL3bBt1ohrfDiDK9efI8OBlJ1QA1Sn2AVtpGjC22l4N-ecZmCqXg_cL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