Linking with libcxx built with asan reports container-overflow error

Is it possible to use a sanitized libc++ in LLVM 21.1.3? When I do that, ASAN always reports a container-overflow. When I do not link with a sanitized libc++, then ASAN does not report any error.

I am upgrading from LLVM 14.0.6 where this used to work.

My test program is:

#include <vector>
#include <string>

int main() {
  std::string test = "hello";
  std::vector<std::string> strs;
  strs.push_back(test);
  test = "hello again";
  strs.push_back(test);
  return 0;
}

I’m trying to build the sanitized libc++ like this (though I’ve tried a few variations like this)

/usr/bin/cmake ${src_dir}/runtimes                                  \
    -DLLVM_USE_SANITIZER=Address                                    \
    -DCMAKE_INSTALL_PREFIX=${install_dir}/libcxx-asan               \
    -DCMAKE_C_COMPILER=${install_dir}/bin/clang                     \
    -DCMAKE_CXX_COMPILER=${install_dir}/bin/clang++                 \
    -DCMAKE_BUILD_TYPE=Release                                      \
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON                            \
    -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" \
    -DLLVM_INCLUDE_TESTS=OFF                                        \
    -DLIBCXX_HAS_ATOMIC_LIB=NO                                      \
    -DLIBCXXABI_USE_COMPILER_RT=ON                                  \
    -DLIBCXX_USE_COMPILER_RT=ON                                     \
    -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON                           \
    -DLIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY=OFF

make -j$(nproc) install

And I’m compiling my test program like this:

/opt/llvm-21.1.3/bin/clang++ -fsanitize=address test.cpp -L/opt/llvm-21.1.3/libcxx-asan/lib/ -static-libstdc++ -static-libgcc

Running it I get a container-overflow error

==166788==ERROR: AddressSanitizer: container-overflow on address 0x7bc086160017 at pc 0x5577299ad04b bp 0x7fff3795dc80 sp 0x7fff3795d440

(fwiw, it reports a container overflow whether I static link or not)

However if I do not link with the sanitized libc++, then there is no error.

/opt/llvm-21.1.3/bin/clang++ -fsanitize=address test.cpp -static-libstdc++ -static-libgcc

Is it possible to use a sanitized libc++ with asan anymore? Has anyone done this recently?

I’ve tried to look at the CMake builds within the LLVM source tree, but I haven’t been successful trying out similar options in my own build.

I can of course not link with the sanitized libc++ anymore, but I’ve been doing that to try to get the most sanitization possible with asan and tsan :slight_smile:

I figured this out! It turned out to be this:

Which just means that I needed to use the headers from the sanitized libc++ when compiling.

So my example works when compiling like this:

/opt/llvm-21.1.3/bin/clang++ -fsanitize=address test.cpp -nostdinc++ -isystem /opt/llvm-21.1.3/libcxx-asan/include -isystem /opt/llvm-21.1.3/libcxx-asan/include/c++/v1 -L/opt/llvm-21.1.3/libcxx-asan/lib/ -static-libstdc++ -static-libgcc

Previously this didn’t matter since the sanitized libc++ includes used to be identical to the normal ones, but that’s no longer true.