Skip to content

Instantly share code, notes, and snippets.

@asb
Created February 10, 2023 15:12
Show Gist options
  • Save asb/645a071903f0c3cf9ef6c59a3d3e0810 to your computer and use it in GitHub Desktop.
Save asb/645a071903f0c3cf9ef6c59a3d3e0810 to your computer and use it in GitHub Desktop.
#!/bin/sh
die () {
printf "%s\n" "$*" >&2
exit 1
}
do_clean() {
rm -rf msan1 msan2-libcxx msan2-full msan3
}
do_stage1() {
if [ -e "msan1" ]; then
die "msan1 build dir already exists"
fi
mkdir -p msan1 && cd msan1
cmake -G Ninja \
-DCMAKE_BUILD_TYPE="Release" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \
-DLLVM_ENABLE_PROJECTS="clang;compiler-rt;lld" \
-DLLVM_CCACHE_BUILD=ON \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DLLVM_ENABLE_LLD=True \
-DLLVM_TARGETS_TO_BUILD="X86" \
../../llvm
time cmake --build . || die "failed to build msan1"
ninja check-sanitizer
ninja check-msan
}
do_stage2() {
if [ -e "msan2-libcxx" ] || [ -e "msan2-full" ]; then
die "msan2-libcxx or msan2-full build dirs already exist"
fi
if ! [ -e "msan1" ]; then
die "msan1 dir from stage 1 doesn't exist"
fi
fsanitize_flags="-fsanitize=memory -fsanitize-memory-use-after-dtor -fsanitize-memory-param-retval -Oz"
mkdir -p msan2-libcxx && cd msan2-libcxx
cmake -G Ninja \
-DCMAKE_BUILD_TYPE="Release" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \
-DCMAKE_C_COMPILER="$(pwd)/../msan1/bin/clang" \
-DCMAKE_CXX_COMPILER="$(pwd)/../msan1/bin/clang++" \
-DLLVM_USE_LINKER=lld \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
-DLLVM_USE_SANITIZER="Memory" \
-DCMAKE_C_FLAGS="$fsanitize_flags" \
-DCMAKE_CXX_FLAGS="$fsanitize_flags" \
../../runtimes
time ninja cxx cxxabi || die "Failed to build msan2-libcxx"
cd ..
sanitizer_ldflags="-Wl,--rpath=$(pwd)/msan2-libcxx/lib -L$(pwd)/msan2-libcxx/lib"
sanitizer_cflags="-nostdinc++ -isystem $(pwd)/msan2-libcxx/include -isystem $(pwd)/msan2-libcxx/include/c++/v1 $fsanitize_flags $sanitizer_ldflags -w"
mkdir -p msan2-full && cd msan2-full
cmake -G Ninja \
-DCMAKE_BUILD_TYPE="Release" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \
-DCMAKE_C_COMPILER="$(pwd)/../msan1/bin/clang" \
-DCMAKE_CXX_COMPILER="$(pwd)/../msan1/bin/clang++" \
-DLLVM_USE_LINKER=lld \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_USE_SANITIZER="Memory" \
-DLLVM_ENABLE_LIBCXX=ON \
-DCMAKE_C_FLAGS="$sanitizer_cflags" \
-DCMAKE_CXX_FLAGS="$sanitizer_cflags" \
-DCMAKE_EXE_LINKER_FLAGS="$sanitizer_ldflags" \
-DLLVM_TARGETS_TO_BUILD="X86" \
../../llvm
time cmake --build . || die "Failed to build msan2-full"
}
do_stage3() {
if [ -e "msan3" ]; then
die "msan3 build dir already exists"
fi
if ! [ -e "msan2-full" ]; then
die "msan2-full dir from stage 2 doesn't exist"
fi
mkdir -p msan3 && cd msan3
# FIXME: move to -DLLVM_USE_LINKER=lld when issue with lld not being built
# with zlib is identified
cmake -G Ninja \
-DCMAKE_BUILD_TYPE="Release" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \
-DLLVM_ENABLE_PROJECTS="clang" \
-DCMAKE_C_COMPILER="$(pwd)/../msan2-full/bin/clang" \
-DCMAKE_CXX_COMPILER="$(pwd)/../msan2-full/bin/clang++" \
-DLLVM_TARGETS_TO_BUILD="X86" \
../../llvm
time cmake --build . || die "Failed to build msan3"
}
if [ "$(basename $(pwd))" != "build" ]; then
die "Expected to be called within 'build' dir"
fi
COMMAND="do_$1"
if type "$COMMAND" 2> /dev/null > /dev/null; then
shift
set -x
"$COMMAND" "$@"
else
printf "Invalid command name '%s'. Exiting\n" "$1"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment