diff --git a/.travis.yml b/.travis.yml index ea589b4059cd8a..9c7e0549b764f8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -143,6 +143,11 @@ jobs: env: >- FILE_ENV="./ci/test/00_setup_env_native_asan.sh" + - stage: test + name: 'x86_64 Linux [GOAL: install] [bionic] [depends, sanitizers: memory (MSan)]' + env: >- + FILE_ENV="./ci/test/00_setup_env_native_msan.sh" + - stage: test name: 'x86_64 Linux [GOAL: install] [bionic] [no depends, only system libs, valgrind]' env: >- diff --git a/ci/test/00_setup_env_native_msan.sh b/ci/test/00_setup_env_native_msan.sh new file mode 100644 index 00000000000000..481054a779f596 --- /dev/null +++ b/ci/test/00_setup_env_native_msan.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# +# Copyright (c) 2020 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +export LC_ALL=C.UTF-8 + +export LIBCXX_DIR="${BASE_ROOT_DIR}/ci/scratch/msan/build/" +export MSAN_FLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O1 -fno-optimize-sibling-calls" +LIBCXX_FLAGS="-nostdinc++ -stdlib=libc++ -L${LIBCXX_DIR}lib -lc++abi -I${LIBCXX_DIR}include -I${LIBCXX_DIR}include/c++/v1 -lpthread -Wl,-rpath,${LIBCXX_DIR}lib" +export MSAN_AND_LIBCXX_FLAGS="${MSAN_FLAGS} ${LIBCXX_FLAGS}" +export BDB_PREFIX="${BASE_ROOT_DIR}/db4" + +export CONTAINER_NAME="ci_native_msan" +export PACKAGES="clang-8 llvm-8 cmake" +export DEP_OPTS="NO_WALLET=1 NO_UPNP=1 NO_QT=1 CC='clang-8' CXX='clang++-8' CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}' boost_toolset_linux='clang' boost_cxx='clang++-8' boost_cxxflags='-std=c++11 -fvisibility=hidden -fPIC ${MSAN_AND_LIBCXX_FLAGS}'" +export GOAL="install" +export BITCOIN_CONFIG="--enable-wallet --with-sanitizers=memory --disable-asm --with-asm=no --prefix=${BASE_ROOT_DIR}/depends/x86_64-pc-linux-gnu/ CC=clang-8 CXX=clang++-8 CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}' BDB_LIBS='-L${BDB_PREFIX}/lib -ldb_cxx-4.8' BDB_CFLAGS='-I${BDB_PREFIX}/include'" +export USE_MEMORY_SANITIZER="true" diff --git a/ci/test/04_install.sh b/ci/test/04_install.sh index 32eaaf15bb7db0..7d84bf9cfd0949 100755 --- a/ci/test/04_install.sh +++ b/ci/test/04_install.sh @@ -91,6 +91,13 @@ export DIR_FUZZ_IN=${DIR_QA_ASSETS}/fuzz_seed_corpus/ DOCKER_EXEC mkdir -p "${BASE_SCRATCH_DIR}/sanitizer-output/" +if [[ ${USE_MEMORY_SANITIZER} == "true" ]]; then + DOCKER_EXEC mkdir -p "${BASE_SCRATCH_DIR}/msan/build/" + DOCKER_EXEC git clone --depth=1 https://github.com/llvm/llvm-project "${BASE_SCRATCH_DIR}/msan/llvm-project" + DOCKER_EXEC "cd ${BASE_SCRATCH_DIR}/msan/build/ && cmake -DLLVM_ENABLE_PROJECTS='libcxx;libcxxabi' -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_SANITIZER=Memory -DCMAKE_C_COMPILER=clang-8 -DCMAKE_CXX_COMPILER=clang++-8 -DLLVM_TARGETS_TO_BUILD=X86 ../llvm-project/llvm/" + DOCKER_EXEC "cd ${BASE_SCRATCH_DIR}/msan/build/ && make $MAKEJOBS cxx" +fi + if [ -z "$DANGER_RUN_CI_ON_HOST" ]; then echo "Create $BASE_ROOT_DIR" DOCKER_EXEC rsync -a /ro_base/ $BASE_ROOT_DIR diff --git a/ci/test/05_before_script.sh b/ci/test/05_before_script.sh index 933f4cea91873e..61e9b24cc4a5b8 100755 --- a/ci/test/05_before_script.sh +++ b/ci/test/05_before_script.sh @@ -15,6 +15,10 @@ fi DOCKER_EXEC mkdir -p ${DEPENDS_DIR}/SDKs ${DEPENDS_DIR}/sdk-sources +if [[ ${USE_MEMORY_SANITIZER} == "true" ]]; then + DOCKER_EXEC "contrib/install_db4.sh \$(pwd) --enable-umrw CC=clang-8 CXX=clang++-8 CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}'" +fi + if [ -n "$OSX_SDK" ] && [ ! -f ${DEPENDS_DIR}/sdk-sources/MacOSX${OSX_SDK}.sdk.tar.gz ]; then curl --location --fail $SDK_URL/MacOSX${OSX_SDK}.sdk.tar.gz -o ${DEPENDS_DIR}/sdk-sources/MacOSX${OSX_SDK}.sdk.tar.gz fi diff --git a/contrib/install_db4.sh b/contrib/install_db4.sh index e9130a21ded67c..e89e8ba8e0faee 100755 --- a/contrib/install_db4.sh +++ b/contrib/install_db4.sh @@ -64,7 +64,7 @@ http_get() { mkdir -p "${BDB_PREFIX}" http_get "${BDB_URL}" "${BDB_VERSION}.tar.gz" "${BDB_HASH}" -tar -xzvf ${BDB_VERSION}.tar.gz -C "$BDB_PREFIX" +tar -xzf ${BDB_VERSION}.tar.gz -C "$BDB_PREFIX" cd "${BDB_PREFIX}/${BDB_VERSION}/" # Apply a patch necessary when building with clang and c++11 (see https://community.oracle.com/thread/3952592) diff --git a/depends/packages/boost.mk b/depends/packages/boost.mk index cbe4fe4d970bf8..22e40f4449ee48 100644 --- a/depends/packages/boost.mk +++ b/depends/packages/boost.mk @@ -33,13 +33,13 @@ define $(package)_preprocess_cmds endef define $(package)_config_cmds - ./bootstrap.sh --without-icu --with-libraries=$($(package)_config_libraries) + ./bootstrap.sh --without-icu --with-libraries=$($(package)_config_libraries) toolset=$($(package)_toolset_$(host_os)) endef define $(package)_build_cmds - ./b2 -d2 -j2 -d1 --prefix=$($(package)_staging_prefix_dir) $($(package)_config_opts) stage + ./b2 -d2 -j2 -d1 --prefix=$($(package)_staging_prefix_dir) $($(package)_config_opts) toolset=$($(package)_toolset_$(host_os)) stage endef define $(package)_stage_cmds - ./b2 -d0 -j4 --prefix=$($(package)_staging_prefix_dir) $($(package)_config_opts) install + ./b2 -d0 -j4 --prefix=$($(package)_staging_prefix_dir) $($(package)_config_opts) toolset=$($(package)_toolset_$(host_os)) install endef diff --git a/src/random.cpp b/src/random.cpp index 2a27e6ba0d345d..118334111d32bc 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -32,6 +32,13 @@ #ifdef HAVE_SYS_GETRANDOM #include #include + +#if defined(__has_feature) +#if __has_feature(memory_sanitizer) +#include +#endif +#endif + #endif #if defined(HAVE_GETENTROPY) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)) #include @@ -303,6 +310,22 @@ void GetOSRand(unsigned char *ent32) } else { RandFailure(); } + } else { +#if defined(__has_feature) +#if __has_feature(memory_sanitizer) + /* MemorySanitizer (MSAN) does not support syscall(SYS_getrandom, ..., ..., ...): + * Use __msan_unpoison to make MSAN understand how many bytes that have been + * written to ent32. + * + * __msan_unpoison does not change the actual memory content, but only MSAN's + * perception of the memory content. + * + * See https://github.com/google/sanitizers/issues/852 ("memory sanitizer: not + * tracking memory initialization with getrandom") for details. + */ + __msan_unpoison(ent32, rv); +#endif +#endif } #elif defined(HAVE_GETENTROPY) && defined(__OpenBSD__) /* On OpenBSD this can return up to 256 bytes of entropy, will return an diff --git a/test/functional/p2p_blocksonly.py b/test/functional/p2p_blocksonly.py index 3258a38e3cba70..6b0ec80ed31dc5 100755 --- a/test/functional/p2p_blocksonly.py +++ b/test/functional/p2p_blocksonly.py @@ -14,6 +14,7 @@ class P2PBlocksOnly(BitcoinTestFramework): def set_test_params(self): self.setup_clean_chain = False self.num_nodes = 1 + self.rpc_timeout = 240 self.extra_args = [["-blocksonly"]] def run_test(self):