Configure autotools project with Clang sanitizers in lib static configuration?

EDIT . If its TL; DR, just skip from the bottom. Its where I ask: How to set up an autotools project to use a static library?

I work with several open source libraries, and I try to run their test suite under Clang sanitizers. To work under Clang disinfectants, we need to (1) specify some parameters and (2) link the static libraries from Clang Compiler-RT as needed. Note: There are no dynamic libraries or shared objects.

Setting parameters is very simple:

export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/lib/clang/3.3/lib/darwin/ export CC=/usr/local/bin/clang export CXX=/usr/local/bin/clang++ export CFLAGS="-g3 -fsanitize=address -fsanitize=undefined" export CXXFLAGS="-g3 -fsanitize=address -fsanitize=undefined -fno-sanitize=vptr" ./configure 

However, this will lead to some warnings about archives (when starting AR ) and link errors (when starting LD ) with undefined characters. The message will look like:

 libjpeg.a(jmemmgr.o): In function `do_sarray_io': /home/jwalton/jpeg-6b/jmemmgr.c:695: undefined reference to `__ubsan_handle_type_mismatch' /home/jwalton/jpeg-6b/jmemmgr.c:695: undefined reference to `__ubsan_handle_type_mismatch' /home/jwalton/jpeg-6b/jmemmgr.c:696: undefined reference to `__ubsan_handle_type_mismatch' 

I know the libraries that need to be linked. For the sanitizers that I use, they are libclang_rt.asan_osx.a and libclang_rt.ubsan_osx.a (or libclang_rt.full-x86_64.a and libclang_rt.ubsan-x86_64.a on Linux).

To provide libraries, I then export the following. Note: these are LIBS , not LDLIBS , as expected by most other make related tools.

 export LIBS="/usr/local/lib/clang/3.3/lib/darwin/libclang_rt.asan_osx.a \ /usr/local/lib/clang/3.3/lib/darwin/libclang_rt.ubsan_osx.a" 

The result is a configure problem:

 configure: error: cannot run C compiled programs. If you meant to cross compile, use `--host'. ... 

Looking at config.log , it looks like there are two problems. Firstly, the path is blocked by a change from /usr/local/... to /Users/jwalton/... And secondly, the file name is blocked by moving from static lib to dynamic lib:

 configure:3346: ./conftest dyld: Library not loaded: /Users/jwalton/clang-llvm/llvm-3.3.src/Release+Asserts/lib/clang/3.3/lib/darwin/libclang_rt.asan_osx_dynamic.dylib Referenced from: /Users/jwalton/libpng-1.6.7/./conftest Reason: image not found 

In another attempt, I tried using LDFLAGS :

 export LDFLAGS="-L/usr/local/lib/clang/3.3/lib/darwin/" export LIBS="libclang_rt.asan_osx.a libclang_rt.ubsan_osx.a" 

This results in a similar error:

 configure: error: in `/Users/jwalton/libpng-1.6.7': configure: error: C compiler cannot create executables 

And config.log :

 configure:3209: /usr/local/bin/clang -g3 -fsanitize=address -fsanitize=undefined -L/usr/local/lib/clang/3.3/lib/darwin/ conftest.c libclang_rt.asan_osx.a libclang_rt.ubsan_osx.a >&5 clang: error: no such file or directory: 'libclang_rt.asan_osx.a' clang: error: no such file or directory: 'libclang_rt.ubsan_osx.a' 

And removing the lib and .a from LIBS results in:

 configure:3209: /usr/local/bin/clang -g3 -fsanitize=address -fsanitize=undefined -L/usr/local/lib/clang/3.3/lib/darwin/ conftest.c clang_rt.asan_osx clang_rt.ubsan_osx >&5 clang: error: no such file or directory: 'clang_rt.asan_osx' clang: error: no such file or directory: 'clang_rt.ubsan_osx' 

And adding -l to LIBS results in:

 configure:3335: /usr/local/bin/clang -o conftest -g3 -fsanitize=address -fsanitize=undefined -L/usr/local/lib/clang/3.3/lib/darwin/ conftest.c -lclang_rt.asan_osx -lclang_rt.ubsan_osx >&5 configure:3339: $? = 0 configure:3346: ./conftest dyld: could not load inserted library: /Users/jwalton/libpng-1.6.7/./conftest ./configure: line 3348: 38224 Trace/BPT trap: 5 ./conftest$ac_cv_exeext 

Finally, the -l argument is valid:

 $ ls /usr/local/lib/clang/3.3/lib/darwin/ libclang_rt.10.4.a libclang_rt.ios.a libclang_rt.asan_osx.a libclang_rt.osx.a libclang_rt.asan_osx_dynamic.dylib libclang_rt.profile_ios.a libclang_rt.cc_kext.a libclang_rt.profile_osx.a libclang_rt.cc_kext_ios5.a libclang_rt.ubsan_osx.a libclang_rt.eprintf.a 

After the whole background: how do I configure the autotools project to use a static library?

Bonus points: why is something so easy to do so difficult?

+6
source share
3 answers

GNU versions of libtool do not pass -fsanitize = ... arguments to the linker. You will need to update libtool using the patch http://savannah.gnu.org/patch/?8775 In particular:

 diff --git a/build-aux/ltmain.in b/build-aux/ltmain.in index 0c40da0..b99b0cd 100644 --- a/build-aux/ltmain.in +++ b/build-aux/ltmain.in @@ -5362,10 +5362,11 @@ func_mode_link () # -O*, -g*, -flto*, -fwhopr*, -fuse-linker-plugin GCC link-time optimization # -specs=* GCC specs files # -stdlib=* select c++ std lib with clang + # -fsanitize=* Clang memory and address sanitizer -64|-mips[0-9]|-r[0-9][0-9]*|-xarch=*|-xtarget=*|+DA*|+DD*|-q*|-m*| \ -t[45]*|-txscale*|-p|-pg|--coverage|-fprofile-*|-F*|@*|-tp=*|--sysroot=*| \ -O*|-g*|-flto*|-fwhopr*|-fuse-linker-plugin|-fstack-protector*|-stdlib=*| \ - -specs=*) + -specs=*|-fsanitize=*) func_quote_for_eval "$arg" arg=$func_quote_for_eval_result func_append compile_command " $arg" 

For libtool to accept a static library, you should simply include the full path to the static library on the command line or use options such as -L/usr/local/lib/clang/3.3/lib/darwin/ -lclang_rt.ubsan_osx . However, cfe should take care of the magic of the library for you, as soon as libtool tells it to use -fsanitize = ... for linking.

+1
source

You need to add the -fsanitize flags to LDFLAGS.

+5
source

As for the problem with the undefined character, if you are linking C ++ objects, then you should use clang++ instead of clang . Otherwise, you may get an error, for example:

 /bin/bash libtool --tag=CC --mode=link clang-3.6 ... -fsanitize=undefined -o freeswitch ... libtool: link: clang-3.6 ... -fsanitize=undefined -o .libs/freeswitch ./.libs/libfreeswitch.so ... ./.libs/libfreeswitch.so: undefined reference to `__ubsan_vptr_type_cache' ./.libs/libfreeswitch.so: undefined reference to `__ubsan_handle_dynamic_type_cache_miss' clang: error: linker command failed with exit code 1 (use -v to see invocation) 

In the above case, the brutal libtool used clang instead of clang ++ because automake thought you wanted to link the set of C objects (he doesn't understand that linking the libtool archive ( libfreeswitch.la ) using C ++ means that a C + linker is needed + (pay attention to --tag=CC ). See also https://lists.debian.org/debian-mentors/2003/06/msg00004.html .

To work around this problem, follow the instructions at https://www.gnu.org/software/automake/manual/html_node/Libtool-Convenience-Libraries.html and add the C ++ source source to your source in your Makefile.am sources:

 SUBDIRS = sub1 sub2 … lib_LTLIBRARIES = libtop.la libtop_la_SOURCES = # Dummy C++ source to cause C++ linking. nodist_EXTRA_libtop_la_SOURCES = dummy.cxx libtop_la_LIBADD = \ sub1/libsub1.la \ sub2/libsub2.la \ … 
0
source

Source: https://habr.com/ru/post/958668/


All Articles