Skip to content

MemorySanitizerBootstrappingClang

Vitaly Buka edited this page May 7, 2016 · 14 revisions

Introduction

MemorySanitizer code itself can not be tested with MemorySanitizer for obvious reasons, but the rest of Clang/LLVM can. Instructions on this page show how to do it.

Details

MSan requires that all code in the process is instrumented (see handling-external-code). Luckily, the only external dependency of Clang is the C++ standard library (and, of course, libc, but MSan almost takes care of it).

Checkout the source:

(svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm && cd llvm &&
(R=$(svn info | grep Revision: | awk '{print $2}') &&
(cd tools && svn co -r $R http://llvm.org/svn/llvm-project/cfe/trunk clang)  &&
(cd projects && svn co -r $R http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt)  &&
(cd projects && svn co -r $R http://llvm.org/svn/llvm-project/libcxx/trunk libcxx)  &&
(cd projects && svn co -r $R http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi)))

Build Clang

(mkdir -p build && cd build && cmake -GNinja -DCMAKE_BUILD_TYPE=Release ../llvm && ninja)

Build libc++ and libc++abi with MemorySanitizer

(mkdir -p build-libcxx-msan && cd build-libcxx-msan &&
(CC=$PWD/../build/bin/clang CXX=$PWD/../build/bin/clang++ \
  cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_SANITIZER=MemoryWithOrigins ../llvm) &&
ninja cxx cxxabi)

Build clang with MemorySanitizer, using the new libc++

(mkdir -p build-clang-msan && cd build-clang-msan &&
(CLANG_BUILD=$PWD/../build LIBCXX_BUILD=$PWD/../build-libcxx-msan \
MSAN_FLAGS=" \
  -nostdinc++ \
  -isystem $LIBCXX_BUILD/include \
  -isystem $LIBCXX_BUILD/include/c++/v1  \
  -lc++abi \
  -Wl,--rpath=$LIBCXX_BUILD/lib \
  -L$LIBCXX_BUILD/lib \
  -fsanitize=memory \
  -fsanitize-memory-track-origins \
  -w" \
CC=$CLANG_BUILD/bin/clang \
CXX=$CLANG_BUILD/bin/clang++ \
CFLAGS=$MSAN_FLAGS \
CXXFLAGS=$MSAN_FLAGS \
cmake -GNinja \
 -DCMAKE_BUILD_TYPE=Release \
 -DLLVM_USE_SANITIZER=MemoryWithOrigins \
 -DLLVM_ENABLE_LIBCXX=ON \
 -DCMAKE_EXE_LINKER_FLAGS="-lc++abi -Wl,--rpath=$LIBCXX_BUILD/lib -L$LIBCXX_BUILD/lib" \
 ../llvm) &&
ninja clang check-clang check-llvm)

Note that building all targets in the instrumented tree will attempt to link newly built MSan runtime with MSan runtime from the previous stage, which is not a good idea.

Clone this wiki locally