AddressSanitizer

Hi lists,

I am encountering a large number of check-clang failures after building Clang/LLVM with -DLLVM_USE_SANITIZER=Address. I have reported the issue on Bugzilla (https://bugs.llvm.org/show_bug.cgi?id=47678). I cannot even compile helloworld.c with the resulting clang tool without a lot of false positives from ASan. Is it because I am not supposed to use GCC’s AddressSanitizer when building Clang?

Hi Bryan,

Yes, building LLVM with GCC+ASAN is a warranty void zone.
It can probably be made to work with some effort, but unless someone is willing to
maintain a public bot with this build, it will remain unsupported.

Building LLVM with LLVM+ASAN is fully supported, and the bots are maintained.

thanks!

–kcc

Thanks Kostya, using Clang to build itself with ASan enabled, and running my

build container in privileged mode, helped solve a lot of the problems. But I
now encounter a lot of link errors while testing my stage-2 build:

FAIL: libomp :: api/has_openmp.c (52345 of 54886)
******************** TEST ‘libomp :: api/has_openmp.c’ FAILED ********************
Script:

+Vitaly Buka to help with the links to build bots.

Not sure if any of those cover OpenMP.

Can you please share the full cmake command line?

Hi Vitaly,

This is the CMake command line I used:

cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/stage2
-DCMAKE_C_COMPILER=$HOME/stage1/bin/clang
-DCMAKE_CXX_COMPILER=$HOME/stage1/bin/clang++
-DCOMPILER_RT_BUILD_BUILTINS=off -DCOMPILER_RT_BUILD_PROFILE=on
-DCOMPILER_RT_BUILD_SANITIZERS=on -DCOMPILER_RT_BUILD_XRAY=off
-DLLVM_ENABLE_PROJECTS=“clang;compiler-rt;lld;openmp”
-DLLVM_ENABLE_ASSERTIONS=on -DLLVM_OPTIMIZED_TABLEGEN=on
-DLLVM_STATIC_LINK_CXX_STDLIB=on -DLLVM_TARGETS_TO_BUILD=“AArch64;X86”
-DLLVM_USE_LINKER=gold -DLLVM_USE_SANITIZER=Address …/llvm

Stage 1 was built from the same source using the same CMake command, without
-DLLVM_USE_SANITIZER. CMAKE_C_FLAGS and CMAKE_CXX_FLAGS contained some
hardening options like “-fstack-protector-strong -D_FORTIFY_SOURCE=2” which
I omitted for brevity. I am building on an AArch64 CentOS system.

The Builtins-aarch64-linux test failures have disappeared after I set the
PATH and LD_LIBRARY_PATH environment variables to point to $HOME/stage1/bin
and $HOME/stage1/lib. But I still see the tools/gold/X86 and OpenMP test
failures.

Thanks,

I guess the problem is the following:
Stage2 builds libomp.so instrumented with AddressSanitizer. Then omp tests are compiled without -fsanitize=address and load instrumented libomp.so which fails on missing symbols.

You can try to solve this in two ways:

  1. Disable sanitizers on libomp.so even -DLLVM_USE_SANITIZER= is set. That can be hard if it uses some part of llvm as dependency. If so, you can try to build non-instrumented libomp.so using ExternalProject_Add.
    Should work but sanitizer will cover only the compiler binary not libomp.so.

  2. Use asan in all tests e.g. with -DOPENMP_TEST_FLAGS=-fsanitize=address (better to do so in cmake file). Problem here is that stage2 does not build asan because it uses LLVM_USE_SANITIZER. We can just use stage1 compiler which contains asan libs. With a hack like this:

if(LLVM_USE_SANITIZER)
set(OPENMP_TEST_C_COMPILER ${CMAKE_C_COMPILER})
set(OPENMP_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER})

set(saved_CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
set(saved_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})

unset(CMAKE_C_FLAGS)
append_common_sanitizer_flags()
set(OPENMP_TEST_FLAGS “${CMAKE_C_FLAGS} -fsanitize=address ${OPENMP_TEST_FLAGS}”)

set(CMAKE_C_FLAGS ${saved_CMAKE_C_FLAGS})
set(CMAKE_CXX_FLAGS ${saved_CMAKE_CXX_FLAGS})
endif()

I can run most of the test:

Unsupported : 39

Passed : 241
Expectedly Failed: 2
Failed : 33 (it was 270+ before)

then you can fix or disable failing tests

Hi Bryan,

Clang doesn't seem to play nicely when using GCC ASAN, It should work
fine using clang as the host compiler though.

~Nathan

Disregard that, Didn't reaslise this had already been answered