Skip to content

Instantly share code, notes, and snippets.

@bjodah
Last active August 4, 2020 20:04
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bjodah/82e1013e2025003e35f365b89921ddc2 to your computer and use it in GitHub Desktop.
Script to compile symengine with memory sanitizer enabled.
mount_*/
environment_*/
output.log
diff --git a/boost_1_73_0/boost/rational.hpp b/boost_1_73_0/boost/rational.hpp
index f33bc3b..afe9983 100644
--- a/boost_1_73_0/boost/rational.hpp
+++ b/boost_1_73_0/boost/rational.hpp
@@ -902,7 +902,7 @@ BOOST_CXX14_CONSTEXPR void rational<IntType>::normalize()
num /= g;
den /= g;
- if (den < -(std::numeric_limits<IntType>::max)()) {
+ if (std::numeric_limits<IntType>::is_bounded && den < -(std::numeric_limits<IntType>::max)()) {
BOOST_THROW_EXCEPTION(bad_rational("bad rational: non-zero singular denominator"));
}
#!/bin/bash -xeu
# This script uses docker to build a specific symengine commit with memory sanitizer enabled.
#
# docker_symengine_msan.sh symengine_commit_hash llvm_org_tag
#
# Example usage:
#
# $ ./docker_symengine_msan.sh 403c076f1ddba0db02820f832a668b8e6a436d97 10.0.0-rc4
#
# Alternative libcxx build procedures:
# - https://github.com/bitcoin/bitcoin/pull/18288/files#diff-6e5924095ab93719fe8d563123784861R114
SYMENGINE_COMMIT=${1:-5da398247e4ea04022a6edd71abe16aae132399b}
LLVM_ORG_VER=${2:-"10.0.1"} # 9.0.1
LLVM_MAJOR=$(echo $LLVM_ORG_VER | cut -f1 -d.)
echo "LLVM_MAJOR=${LLVM_MAJOR}, LLVM_ORG_VER=${LLVM_ORG_VER}"
ENVROOT="environment_msan_${LLVM_ORG_VER}"
MNTPOINT=$(realpath "mount_${LLVM_ORG_VER}_${SYMENGINE_COMMIT}")
if ! which docker; then
2>&1 echo "You need to have docker installed. See e.g. https://www.docker.com/community-edition#/download"
exit 1
fi
if [ ! -d $ENVROOT ]; then
mkdir $ENVROOT
cp boost173-rational-gh41.patch $ENVROOT/
cat <<EOF >$ENVROOT/Dockerfile
FROM ubuntu:20.04
RUN apt-get update && apt-get --quiet --assume-yes upgrade && \\
apt-get --quiet --assume-yes --no-install-recommends install libgmp-dev binutils-dev cmake curl zlib1g-dev sudo wget gnupg2 make patch\
clang-${LLVM_MAJOR} llvm-${LLVM_MAJOR}-dev lldb-${LLVM_MAJOR} llvm-${LLVM_MAJOR}-tools
ENV CC=clang-${LLVM_MAJOR} CXX=clang++-${LLVM_MAJOR}
RUN cd /tmp
RUN curl -Lsk https://github.com/llvm/llvm-project/archive/llvmorg-${LLVM_ORG_VER}.tar.gz | tar xz -C /tmp && \
mkdir /tmp/build_libcxx && \
cd /tmp/build_libcxx && \
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_SANITIZER=MemoryWithOrigins -DLLVM_CONFIG_PATH=/usr/bin/llvm-config-${LLVM_MAJOR} -DCMAKE_INSTALL_PREFIX=/opt/libcxx_msan /tmp/llvm-project-llvmorg-${LLVM_ORG_VER}/libcxx && \
cmake --build . && \
cmake --build . --target install && \
mkdir /tmp/build_libcxxabi && \
cd /tmp/build_libcxxabi && \
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_SANITIZER=MemoryWithOrigins -DLLVM_CONFIG_PATH=/usr/bin/llvm-config-${LLVM_MAJOR} -DCMAKE_INSTALL_PREFIX=/opt/libcxx_msan -DLIBCXXABI_LIBCXX_INCLUDES=/opt/libcxx_msan/include/c++/v1 -DLIBCXXABI_LIBCXX_PATH=/tmp/llvm-project-llvmorg-${LLVM_ORG_VER}/libcxx /tmp/llvm-project-llvmorg-${LLVM_ORG_VER}/libcxxabi && \
cmake --build . && \
cmake --build . --target install && \
cp /tmp/llvm-project-llvmorg-${LLVM_ORG_VER}/libcxxabi/include/* /opt/libcxx_msan/include/ && \
curl -Lsk https://dl.bintray.com/boostorg/release/1.73.0/source/boost_1_73_0.tar.bz2 | tar xj -C /opt
# https://github.com/boostorg/rational/issues/27
# https://github.com/boostorg/rational/pull/41
COPY boost173-rational-gh41.patch /opt/
RUN cd /opt/ && patch -p1 <boost173-rational-gh41.patch
EOF
fi
if groups | grep docker; then
DOCKERCMD=docker
else
DOCKERCMD="sudo docker"
fi
set -o pipefail
DOCKERIMAGE=$($DOCKERCMD build $ENVROOT | tee /dev/tty | tail -n 1 | cut -d' ' -f3)
if [ ! -d $MNTPOINT ]; then
mkdir $MNTPOINT
fi
if [ ! -e $MNTPOINT/run ]; then
cat <<EOF >$MNTPOINT/run.sh
#!/bin/bash -xe
if [ ! -d symengine-${SYMENGINE_COMMIT} ]; then
wget --no-check-certificate https://github.com/symengine/symengine/archive/${SYMENGINE_COMMIT}.tar.gz
tar xzf ${SYMENGINE_COMMIT}.tar.gz
fi
mkdir -p symengine-build
cd symengine-build
export CXXFLAGS="\$CXXFLAGS -fsanitize=memory -fsanitize-memory-track-origins=2 -stdlib=libc++ -I/opt/libcxx_msan/include -I/opt/libcxx_msan/include/c++/v1 -fno-omit-frame-pointer -fno-optimize-sibling-calls -O1 -glldb -DHAVE_GCC_ABI_DEMANGLE=no"
export LDFLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 \$LDFLAGS -Wl,-rpath,/opt/libcxx_msan/lib -L/opt/libcxx_msan/lib -lc++abi"
CXX=clang++-${LLVM_MAJOR} CC=clang-${LLVM_MAJOR} cmake \
-DCMAKE_BUILD_TYPE:STRING="Debug" \
-DWITH_COTIRE=OFF \
-DWITH_BFD:BOOL=OFF \
-DWITH_LLVM:BOOL=OFF \
-DINTEGER_CLASS="boostmp" \
-DWITH_SYMENGINE_RCP:BOOL=ON \
-DBUILD_TESTS:BOOL=ON \
-DBUILD_BENCHMARKS:BOOL=ON \
-DBOOST_ROOT=/opt/boost_1_73_0 \
../symengine-${SYMENGINE_COMMIT}
make VERBOSE=1 -j $(nproc)
set +e
ctest --output-on-failure
set -e
export MSAN_OPTIONS=abort_on_error=1,symbolize=1,external_symbolizer_path=/usr/lib/llvm-${LLVM_MAJOR}/bin/llvm-symbolizer
./symengine/utilities/matchpycpp/tests/bipartite/test_bipartite
./symengine/utilities/matchpycpp/tests/hopcroft_karp/test_hopcroft_karp
EOF
chmod +x $MNTPOINT/run.sh
fi
HOST_USER=${SUDO_USER:-${LOGNAME}}
$DOCKERCMD run --rm --name "symengine_compile" \
-e HOST_WHOAMI=${HOST_USER} -e HOST_UID=$(id -u ${HOST_USER}) -e HOST_GID=$(id -g ${HOST_USER}) \
-v $MNTPOINT:/mount -w /mount --cap-add SYS_PTRACE \
-it $DOCKERIMAGE /usr/bin/env bash -x -c \
"groupadd -f --gid \$HOST_GID \$HOST_WHOAMI; \
useradd --uid \$HOST_UID --gid \$HOST_GID --home /mount \$HOST_WHOAMI; \
sudo --login -u \$HOST_WHOAMI ./run.sh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment