Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build fails on clang 10 with UBSAN #76

Closed
jktjkt opened this issue May 16, 2020 · 5 comments
Closed

Build fails on clang 10 with UBSAN #76

jktjkt opened this issue May 16, 2020 · 5 comments

Comments

@jktjkt
Copy link

jktjkt commented May 16, 2020

I'm on clang 10.0.0:

$ clang++-10 --version
clang version 10.0.0 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm/10/bin

And I cannot build replxx when using USAN (-fsanitize=undefined):

$ CC=clang-10 CXX=clang++-10 LD=clang-10 \
  CFLAGS='-fsanitize=undefined' \
  CXXFLAGS='-fsanitize=undefined' \
  LDFLAGS='-fsanitize=undefined' \
  cmake -GNinja -DBUILD_SHARED_LIBS=ON path/to/replxx && ninja -k 666
-- The CXX compiler identification is Clang 10.0.0
-- The C compiler identification is Clang 10.0.0
-- Check for working CXX compiler: /usr/lib/llvm/10/bin/clang++-10
-- Check for working CXX compiler: /usr/lib/llvm/10/bin/clang++-10 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/lib/llvm/10/bin/clang-10
-- Check for working C compiler: /usr/lib/llvm/10/bin/clang-10 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
CMake Warning (dev) at CMakeLists.txt:41 (message):
  CMAKE_BUILD_TYPE not set.  Defaulting to Release
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jkt/temp/xx
[0/2] Re-checking globbed directories...
[17/19] Linking C executable replxx-example-c-api
FAILED: replxx-example-c-api 
: && /usr/lib/llvm/10/bin/clang-10 -fsanitize=undefined -O3 -DNDEBUG  -fsanitize=undefined CMakeFiles/replxx-example-c-api.dir/examples/c-api.c.o CMakeFiles/replxx-example-c-api.dir/examples/util.c.o  -o replxx-example-c-api  -Wl,-rpath,/home/jkt/temp/xx libreplxx.so.0.0.2 && :
libreplxx.so.0.0.2: undefined reference to `__ubsan_vptr_type_cache'
libreplxx.so.0.0.2: undefined reference to `__ubsan_handle_function_type_mismatch_v1'
libreplxx.so.0.0.2: undefined reference to `__ubsan_handle_dynamic_type_cache_miss'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
[19/19] Linking CXX executable replxx-example-cxx-api
ninja: build stopped: cannot make progress due to previous errors.

I can reproduce this on a Gentoo Linux system with clang 10, and on Fedora 31 with clang 9. Configuring with (-DREPLXX_BUILD_EXAMPLES=OFF) makes this problem go away.

@jktjkt
Copy link
Author

jktjkt commented May 16, 2020

...and when I add -DCMAKE_BUILD_TYPE=Debug, I also get:

[19/19] Linking CXX executable replxx-example-cxx-api
FAILED: replxx-example-cxx-api 
: && /usr/lib/llvm/10/bin/clang++-10  -fsanitize=undefined -stdlib=libc++ -g  -fsanitize=undefined -stdlib=libc++ CMakeFiles/replxx-example-cxx-api.dir/examples/cxx-api.cxx.o CMakeFiles/replxx-example-cxx-api.dir/examples/util.c.o  -o replxx-example-cxx-api  -Wl,-rpath,/home/jkt/temp/xx libreplxx-d.so.0.0.2 && :
/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../x86_64-pc-linux-gnu/bin/ld: replxx-example-cxx-api: hidden symbol `_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE11__recommendEm' isn't defined
/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../x86_64-pc-linux-gnu/bin/ld: final link failed: Bad value
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: cannot make progress due to previous errors.

I'm building stuff (other libraries and applications) with UBSAN just fine otherwise. Demangler says that this is:

std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__recommend(unsigned long)

This particular error goes away if I remove -stdlib=libc++ from CXXFLAGS and LDFLAGS.

@doug-moen
Copy link
Contributor

You are using LD=clang-10 to link C++ code (this is the C compiler, not the C++ compiler).
I suggest trying LD=clang++-10 instead.

@jktjkt
Copy link
Author

jktjkt commented May 16, 2020

@doug-moen , there's just one LD for linking both C and C++ code. I've never heard about setting LD to g++ or to clang++.

Anyway, I tried what you propose, and there's apparently no change on my test system:

jkt@kolibrik ~/temp/xx $ CC=clang-10 CXX=clang++-10 LD=clang++-10 CFLAGS='-fsanitize=undefined' CXXFLAGS='-fsanitize=undefined -stdlib=libc++' LDFLAGS='-fsanitize=undefined -stdlib=libc++' cmake -GNinja -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Debug ~/work/cesnet/gerrit/CzechLight/cla-sysrepo/submodules/dependencies/replxx && ninja -k 666
-- The CXX compiler identification is Clang 10.0.0
-- The C compiler identification is Clang 10.0.0
-- Check for working CXX compiler: /usr/lib/llvm/10/bin/clang++-10
-- Check for working CXX compiler: /usr/lib/llvm/10/bin/clang++-10 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/lib/llvm/10/bin/clang-10
-- Check for working C compiler: /usr/lib/llvm/10/bin/clang-10 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jkt/temp/xx
[0/2] Re-checking globbed directories...
[17/19] Linking C executable replxx-example-c-api
FAILED: replxx-example-c-api 
: && /usr/lib/llvm/10/bin/clang-10 -fsanitize=undefined -g  -fsanitize=undefined -stdlib=libc++ CMakeFiles/replxx-example-c-api.dir/examples/c-api.c.o CMakeFiles/replxx-example-c-api.dir/examples/util.c.o  -o replxx-example-c-api  -Wl,-rpath,/home/jkt/temp/xx libreplxx-d.so.0.0.2 && :
libreplxx-d.so.0.0.2: undefined reference to `__ubsan_handle_function_type_mismatch_v1'
libreplxx-d.so.0.0.2: undefined reference to `__ubsan_vptr_type_cache'
libreplxx-d.so.0.0.2: undefined reference to `__ubsan_handle_dynamic_type_cache_miss'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
[19/19] Linking CXX executable replxx-example-cxx-api
FAILED: replxx-example-cxx-api 
: && /usr/lib/llvm/10/bin/clang++-10  -fsanitize=undefined -stdlib=libc++ -g  -fsanitize=undefined -stdlib=libc++ CMakeFiles/replxx-example-cxx-api.dir/examples/cxx-api.cxx.o CMakeFiles/replxx-example-cxx-api.dir/examples/util.c.o  -o replxx-example-cxx-api  -Wl,-rpath,/home/jkt/temp/xx libreplxx-d.so.0.0.2 && :
/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../x86_64-pc-linux-gnu/bin/ld: replxx-example-cxx-api: hidden symbol `_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE11__recommendEm' isn't defined
/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../x86_64-pc-linux-gnu/bin/ld: final link failed: Bad value
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: cannot make progress due to previous errors.

Notice that the linker in the first call gets "somehow" set "to the C compiler" by the build system. Also, that first target is not C++ code, it's a C code if I'm aprsing its name correctly.

@doug-moen
Copy link
Contributor

I see two errors in your build.

The first error happens when linking replxx-example-c-api, which is a C program, but it is being linked with a C++ library. The problem is that cmake is using the C compiler to link a C++ program. cmake is passing -fsanitize=undefined to clang-10, and clang-10 thinks it is linking a C program, so it doesn't use the additional C++ library required for use with -fsanitize=undefined for sanitizing undefined behaviour in C++ programs relating to vtables and dynamic types, which are C++ only features. That's what I think is happening.

To test my hypothesis about the first link error, you can take the linker command that cmake is running:

/usr/lib/llvm/10/bin/clang-10 -fsanitize=undefined -g  -fsanitize=undefined -stdlib=libc++ CMakeFiles/replxx-example-c-api.dir/examples/c-api.c.o CMakeFiles/replxx-example-c-api.dir/examples/util.c.o  -o replxx-example-c-api  -Wl,-rpath,/home/jkt/temp/xx libreplxx-d.so.0.0.2

and you can modify this command to invoke clang++-10 instead of clang-10. After running this command directly from your shell, I bet that the 3 undefined references to C++ symbols that you are currently seeing will go away.

How to configure cmake to use the C++ compiler to link replxx-example-c-api is a separate issue.

The second build error occurs when linking replxx-example-cxx-api. You've already figured out that it is caused by using libc++ instead of the default.

@AmokHuginnsson
Copy link
Owner

Does replxx code/build system has any action items for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants