Skip to content

Commit

Permalink
build: Add MemorySanitizer (MSan) in Travis to detect use of uninitia…
Browse files Browse the repository at this point in the history
…lized memory
  • Loading branch information
practicalswift committed Apr 13, 2020
1 parent 6ef45bc commit 7e14297
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >-
Expand Down
20 changes: 20 additions & 0 deletions ci/test/00_setup_env_native_msan.sh
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 7 additions & 0 deletions ci/test/04_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions ci/test/05_before_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion contrib/install_db4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions depends/packages/boost.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
#ifdef HAVE_SYS_GETRANDOM
#include <sys/syscall.h>
#include <linux/random.h>

#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#include <sanitizer/msan_interface.h>
#endif
#endif

#endif
#if defined(HAVE_GETENTROPY) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
#include <unistd.h>
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/functional/p2p_blocksonly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 7e14297

Please sign in to comment.