llvm / llvm/llvm-project

ASAN: detect ODR violation in templates/types

Open
#169,296 0 comments 2 reactions 0 assignees View on GitHub
compiler-rt:asan
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

ODR violation detector in ASAN is great. However, it doesn't catch all possible ODRs. One painful ODR violation is due to different template instantiation in multiple translation units. E.g. with old versions of fmt (https://fmt.dev/latest/syntax/) it was possible to get different formatting template impls dependending on previous `` header inclusion.

`TString` is a string-like type that can be implicitly casted to `const std::string&` and can be serialized to `std::ostream`. Depending on `` previous inclusion, `fmt::format` can use either of two methods of stringifying `TString`. The following example shows ODR violation because it leads to inconsistent inline code and code instantiated from non-inline methods of template classes.

```
--- taxi/uservices/userver/core/src/utils/fmt_tstring2_test.cpp (d796f294f01d81270921865625b36ff228293e76)
+++ taxi/uservices/userver/core/src/utils/fmt_tstring2_test.cpp (4fae94450ad73e330ba5b034716ddf8130047664)
@@ -0,0 +1,10 @@
+#include
+#include
+
+#include
+#include
+
+TEST(Fmt, CoreDump2) {
+ TString token("123123123123123123123123123123132");
+ (void)fmt::vformat("some string '{}'", fmt::make_format_args(token));
+}
--- taxi/uservices/userver/core/src/utils/fmt_tstring_test.cpp (d796f294f01d81270921865625b36ff228293e76)
+++ taxi/uservices/userver/core/src/utils/fmt_tstring_test.cpp (4fae94450ad73e330ba5b034716ddf8130047664)
@@ -0,0 +1,9 @@
+#include
+#include
+
+#include
+
+TEST(Fmt, CoreDump1) {
+ TString token("123123123123123123123123123123132");
+ (void)fmt::vformat("some string '{}'", fmt::make_format_args(token));
+}
```

It would be great if such type of ODR violation could be automatically detected by ASAN. It was VERY painful to debug this specific case for hours of manual digging into fmt metaprogramming and includes bisecting.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.