https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125225

            Bug ID: 125225
           Summary: ASan finds an ODR violation in piecewise_construct
                    with -flto=auto
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stuart.a.hayhurst at gmail dot com
  Target Milestone: ---

When compiling a shared library that contains a global unordered_map in an
anonymous namespace and a binary that uses the shared library and also contains
a global unordered_map in an anonymous namespace, ASan finds an ODR violation.

This only happens when -flto=auto is used, and doesn't happen with clang. The
code compiles just fine with -Wall -Wextra -Werror -Wpedantic.

Currently running G++ 16.0.1 20260425 on Debian Sid, but this also happened
with the G++ 15 series.

Let me know if you need any more information :)

Compiled with:

CXX=g++
CXXFLAGS="-std=c++20 -flto=auto -fsanitize=address"

$CXX a.cpp -c $CXXFLAGS -o a.o
$CXX b.cpp -c $CXXFLAGS -fpic -o b.o
$CXX -shared -o libb.so b.o $CXXFLAGS -fpic "-Wl,-soname,libb.so"
$CXX -o demo a.o $CXXFLAGS -L./ -lb

LD_LIBRARY_PATH="$LD_LIBRARY_PATH:./" ./demo


Couldn't figure out how to attach multiple files, so I'll paste the reproducer
below:

a.cpp:
```
#include <unordered_map>

namespace {
  std::unordered_map<int, bool> example_map = {
    {1, {true}}
  };
}

int main() {
  return example_map[1];
}
```

b.cpp:
```
#include <iostream>
#include <unordered_map>

namespace {
  std::unordered_map<int, bool> example_map = {
    {1, {true}}
  };
}

namespace exposed {
  void something() {
    std::cout << example_map[1] << std::endl;
  }
}
```

Reply via email to