Bug#990101: clang-11: Problem building mixed C & C++ code with UBSan sanitizer

Nikolay Shaplov dhyan at nataraj.su
Sun Jun 20 16:29:06 BST 2021

Package: clang-11
Version: 1:11.0.1-2~bpo10+1
Severity: normal

If I have a mixed C/C++ code with classes with virtual function in it, and I
am trying to build it with BUSan with -fno-sanitize-recover=undefined option,
then I get undefined reference error messages

/home/nataraj/test/ubsan_cpp_c/library.cpp:8: undefined reference to `__ubsan_vptr_type_cache'
/usr/bin/ld: /home/nataraj/test/ubsan_cpp_c/library.cpp:8: undefined reference to `__ubsan_handle_dynamic_type_cache_miss_abort'

I can fix these undefined references by specifying explicitly linking to
clang_rt.ubsan_standalone_cxx-x86_64 library, but this is not how it should
work as I can guess.

Here I offer a sample project that allows to reproduce the problem. (I am not
sure I will be able to add attach via reportbug, if not, I will attach it in
the next message)

When I buitld this project as written in build_ubsan_broken.sh:

make clean

CC=clang-11 CXX=clang++-11 \
CFLAGS="-fsanitize=undefined -fno-sanitize-recover=undefined" \
CXXFLAGS="-fsanitize=undefined -fno-sanitize-recover=undefined" \
LDFLAGS="-fsanitize=undefined -fno-sanitize-recover=undefined"\

building will fail.

But if I add -Wl,--whole-archive -L/usr/lib/clang/11/lib/linux/ -lclang_rt.ubsan_standalone_cxx-x86_64 -Wl,--no-whole-archive
to LDFLAGS (see build_ubsan_wokarounded.sh), it will successfully build:

make clean

# Using hack suggested in http://clang-developers.42468.n3.nabble.com/fsanitize-undefined-and-shared-libraries-td4029561.html#a4029653
CC=clang-11 CXX=clang++-11 \
CFLAGS="-fsanitize=undefined -fno-sanitize-recover=undefined" \
CXXFLAGS="-fsanitize=undefined -fno-sanitize-recover=undefined" \
LDFLAGS="-fsanitize=undefined -fno-sanitize-recover=undefined -Wl,--whole-archive -L/usr/lib/clang/11/lib/linux/ -lclang_rt.ubsan_standalone_cxx-x86_64 -Wl,--no-whole-archive"\

I guess this should be fixed and symbols from 
clang_rt.ubsan_standalone_cxx-x86_64 should be added to some library that are
linked by default when you use -fsanitize=undefined

-- System Information:
Debian Release: 10.4
  APT prefers stable-updates
  APT policy: (500, 'stable-updates'), (500, 'stable')
Architecture: amd64 (x86_64)

Kernel: Linux 4.19.0-9-amd64 (SMP w/8 CPU cores)
Locale: LANG=ru_RU.UTF-8, LC_CTYPE=ru_RU.UTF-8 (charmap=UTF-8), LANGUAGE=ru_RU.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages clang-11 depends on:
ii  binutils                2.31.1-16
ii  libc6                   2.28-10
ii  libc6-dev               2.28-10
ii  libclang-common-11-dev  1:11.0.1-2~bpo10+1
ii  libclang-cpp11          1:11.0.1-2~bpo10+1
ii  libclang1-11            1:11.0.1-2~bpo10+1
ii  libgcc-8-dev            8.3.0-6
ii  libgcc1                 1:8.3.0-6
ii  libllvm11               1:11.0.1-2~bpo10+1
ii  libobjc-8-dev           8.3.0-6
ii  libstdc++-8-dev         8.3.0-6
ii  libstdc++6              8.3.0-6

Versions of packages clang-11 recommends:
pn  libomp-11-dev  <none>
ii  llvm-11-dev    1:11.0.1-2~bpo10+1
ii  python3        3.7.3-1

Versions of packages clang-11 suggests:
pn  clang-11-doc  <none>

-- no debconf information
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ubsan_cpp_c.tgz
Type: application/gzip
Size: 1482 bytes
Desc: not available
URL: <http://alioth-lists.debian.net/pipermail/pkg-llvm-team/attachments/20210620/95aaa104/attachment.gz>

More information about the Pkg-llvm-team mailing list