diff --git a/ci/dash/matrix.sh b/ci/dash/matrix.sh index e54e279422c4..26ea6d1fc10f 100755 --- a/ci/dash/matrix.sh +++ b/ci/dash/matrix.sh @@ -22,6 +22,8 @@ elif [ "$BUILD_TARGET" = "linux64" ]; then source ./ci/test/00_setup_env_native_qt5.sh elif [ "$BUILD_TARGET" = "linux64_asan" ]; then source ./ci/test/00_setup_env_native_asan.sh +elif [ "$BUILD_TARGET" = "linux64_msan" ]; then + source ./ci/test/00_setup_env_native_msan.sh elif [ "$BUILD_TARGET" = "linux64_fuzz" ]; then source ./ci/test/00_setup_env_native_fuzz.sh elif [ "$BUILD_TARGET" = "linux64_multiprocess" ]; then diff --git a/ci/test/00_setup_env_native_fuzz_with_msan.sh b/ci/test/00_setup_env_native_fuzz_with_msan.sh new file mode 100755 index 000000000000..f1c358082d5c --- /dev/null +++ b/ci/test/00_setup_env_native_fuzz_with_msan.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# +# Copyright (c) 2020-present 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 CI_IMAGE_NAME_TAG="docker.io/ubuntu:24.04" +LIBCXX_DIR="/msan/cxx_build/" +export MSAN_FLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O1 -fno-optimize-sibling-calls" +LIBCXX_FLAGS="-nostdinc++ -nostdlib++ -isystem ${LIBCXX_DIR}include/c++/v1 -L${LIBCXX_DIR}lib -Wl,-rpath,${LIBCXX_DIR}lib -lc++ -lc++abi -lpthread -Wno-unused-command-line-argument" +export MSAN_AND_LIBCXX_FLAGS="${MSAN_FLAGS} ${LIBCXX_FLAGS}" + +export CONTAINER_NAME="ci_native_fuzz_msan" +export PACKAGES="ninja-build" +# BDB generates false-positives and will be removed in future +export DEP_OPTS="DEBUG=1 NO_BDB=1 NO_QT=1 CC=clang CXX=clang++ CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}'" +export GOAL="install" +# _FORTIFY_SOURCE is not compatible with MSAN. +export BITCOIN_CONFIG="--enable-fuzz --with-sanitizers=fuzzer,memory CPPFLAGS='-DBOOST_MULTI_INDEX_ENABLE_SAFE_MODE -U_FORTIFY_SOURCE'" +export USE_MEMORY_SANITIZER="true" +export RUN_UNIT_TESTS="false" +export RUN_FUNCTIONAL_TESTS="false" +export RUN_FUZZ_TESTS=true +export CCACHE_MAXSIZE=250M diff --git a/ci/test/00_setup_env_native_msan.sh b/ci/test/00_setup_env_native_msan.sh new file mode 100755 index 000000000000..af0008bf0d0c --- /dev/null +++ b/ci/test/00_setup_env_native_msan.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# +# Copyright (c) 2020-present 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 CONTAINER_NAME="ci_native_msan" +export HOST=x86_64-pc-linux-gnu +# Built into the CI image by contrib/containers/ci/ci-msan.Dockerfile so that it +# lands in a cached layer instead of being rebuilt on every run. Upstream does +# the equivalent in ci/test/01_base_install.sh, which Dash does not have. +LIBCXX_DIR="/cxx_build/" +export MSAN_FLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O1 -fno-optimize-sibling-calls" +LIBCXX_FLAGS="-nostdinc++ -nostdlib++ -isystem ${LIBCXX_DIR}include/c++/v1 -L${LIBCXX_DIR}lib -Wl,-rpath,${LIBCXX_DIR}lib -lc++ -lc++abi -lpthread -Wno-unused-command-line-argument" +export MSAN_AND_LIBCXX_FLAGS="${MSAN_FLAGS} ${LIBCXX_FLAGS}" + +# BDB generates false-positives and will be removed in future +export DEP_OPTS="DEBUG=1 NO_BDB=1 NO_QT=1 NO_UPNP=1 NO_NATPMP=1 CC=clang-19 CXX=clang++-19 CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}'" +export GOAL="install" +# CC/CXX/CFLAGS/CXXFLAGS are repeated here rather than left to depends' +# config.site. Upstream dropped them in bitcoin#29800 because its +# ci/test/03_test_script.sh sets CONFIG_SITE explicitly (bitcoin#26683); +# ci/dash/build_src.sh still uses the older --prefix form, and this is the only +# target whose compiler differs from the system default, so nothing else in +# Dash's CI exercises that hand-off. +# _FORTIFY_SOURCE is not compatible with MSAN. +# --with-asm=no and --with-backend=easy keep secp256k1 and relic off +# hand-written assembly, which MSan cannot see through. +# --disable-mimalloc selects the plain malloc secure allocator; mimalloc seeds +# itself with a raw getrandom syscall before main(), which MSan cannot track. +export BITCOIN_CONFIG="--with-sanitizers=memory --with-gui=no --without-bdb --with-sqlite \ +--with-asm=no --with-backend=easy --disable-mimalloc \ +CC=clang-19 CXX=clang++-19 CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}' \ +CPPFLAGS='-U_FORTIFY_SOURCE'" +export USE_MEMORY_SANITIZER="true" +export TEST_RUNNER_TIMEOUT_FACTOR=40 diff --git a/configure.ac b/configure.ac index daf733fb2224..097a0b3cd812 100644 --- a/configure.ac +++ b/configure.ac @@ -1276,17 +1276,16 @@ if test "$LINK_WRAP_SUPPORTED" = "yes"; then fi dnl Check for different ways of gathering OS randomness -AC_MSG_CHECKING([for Linux getrandom syscall]) -AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include - #include - #include ]], - [[ syscall(SYS_getrandom, nullptr, 32, 0); ]])], - [ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_SYS_GETRANDOM], [1], [Define this symbol if the Linux getrandom system call is available]) ], +AC_MSG_CHECKING([for Linux getrandom function]) +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ + #include ]], + [[ getrandom(nullptr, 32, 0); ]])], + [ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETRANDOM], [1], [Define this symbol if the Linux getrandom function call is available]) ], [ AC_MSG_RESULT([no])] ) -AC_MSG_CHECKING([for getentropy via random.h]) -AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include +AC_MSG_CHECKING([for getentropy via sys/random.h]) +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ #include ]], [[ getentropy(nullptr, 32) ]])], [ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETENTROPY_RAND], [1], [Define this symbol if the BSD getentropy system call is available with sys/random.h]) ], diff --git a/contrib/containers/ci/ci-msan.Dockerfile b/contrib/containers/ci/ci-msan.Dockerfile new file mode 100644 index 000000000000..00ad298b77d8 --- /dev/null +++ b/contrib/containers/ci/ci-msan.Dockerfile @@ -0,0 +1,37 @@ +# syntax = devthefuture/dockerfile-x + +FROM ./ci.Dockerfile + +# MemorySanitizer reports any read of memory it did not watch being written, +# so the C++ standard library has to be instrumented too. A stock libstdc++ or +# libc++ produces a flood of reports that cannot be told apart from real ones. +# +# Upstream builds this in ci/test/01_base_install.sh, which runs inside its +# docker build. Dash has no such script, so it lives here instead, which keeps +# the result in a cached image layer rather than rebuilding it on every run. + +USER root + +ARG LLVM_VERSION=19 +ARG LLVM_TAG=llvmorg-19.1.7 + +RUN set -ex; \ + apt-get update && apt-get install ${APT_ARGS} ninja-build; \ + rm -rf /var/lib/apt/lists/*; \ + git clone --depth=1 -b "${LLVM_TAG}" https://github.com/llvm/llvm-project /llvm-project; \ + cmake -G Ninja -B /cxx_build/ \ + -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ + -DCMAKE_BUILD_TYPE=Release \ + -DLLVM_USE_SANITIZER=MemoryWithOrigins \ + -DCMAKE_C_COMPILER=clang-${LLVM_VERSION} \ + -DCMAKE_CXX_COMPILER=clang++-${LLVM_VERSION} \ + -DLLVM_TARGETS_TO_BUILD=Native \ + -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \ + -DLIBCXXABI_USE_LLVM_UNWINDER=OFF \ + -DLIBCXX_HARDENING_MODE=debug \ + -S /llvm-project/runtimes; \ + ninja -C /cxx_build/; \ + du -sh /llvm-project; \ + rm -rf /llvm-project; + +USER dash diff --git a/doc/dependencies.md b/doc/dependencies.md index 2a2b7ec28370..a9be6395aaa0 100644 --- a/doc/dependencies.md +++ b/doc/dependencies.md @@ -20,6 +20,7 @@ You can find installation instructions in the `build-*.md` file for your platfor | [Boost](../depends/packages/boost.mk) | [link](https://www.boost.org/users/download/) | 1.81.0 | [1.73.0](https://github.com/bitcoin/bitcoin/pull/29066) | No | | [libevent](../depends/packages/libevent.mk) | [link](https://github.com/libevent/libevent/releases) | [2.1.12-stable](https://github.com/bitcoin/bitcoin/pull/21991) | [2.1.8](https://github.com/bitcoin/bitcoin/pull/24681) | No | | glibc | [link](https://www.gnu.org/software/libc/) | N/A | [2.31](https://github.com/bitcoin/bitcoin/pull/29987) | Yes | +| Linux Kernel | [link](https://www.kernel.org/) | N/A | [3.17.0](https://github.com/bitcoin/bitcoin/pull/27699) | Yes | ## Optional diff --git a/src/random.cpp b/src/random.cpp index 3b5266ffc6ea..b955cbbd55c2 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -28,14 +28,10 @@ #include #endif -#ifdef HAVE_SYS_GETRANDOM -#include -#include -#endif -#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) -#include +#if defined(HAVE_GETRANDOM) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)) #include #endif + #ifdef HAVE_SYSCTL_ARND #include #endif @@ -325,7 +321,7 @@ static void Strengthen(const unsigned char (&seed)[32], SteadyClock::duration du /** Fallback: get 32 bytes of system entropy from /dev/urandom. The most * compatible way to get cryptographic randomness on UNIX-ish platforms. */ -static void GetDevURandom(unsigned char *ent32) +[[maybe_unused]] static void GetDevURandom(unsigned char *ent32) { int f = open("/dev/urandom", O_RDONLY); if (f == -1) { @@ -357,23 +353,14 @@ void GetOSRand(unsigned char *ent32) if (status != STATUS_SUCCESS) { RandFailure(); } -#elif defined(HAVE_SYS_GETRANDOM) +#elif defined(HAVE_GETRANDOM) /* Linux. From the getrandom(2) man page: * "If the urandom source has been initialized, reads of up to 256 bytes * will always return as many bytes as requested and will not be * interrupted by signals." */ - int rv = syscall(SYS_getrandom, ent32, NUM_OS_RANDOM_BYTES, 0); - if (rv != NUM_OS_RANDOM_BYTES) { - if (rv < 0 && errno == ENOSYS) { - /* Fallback for kernel <3.17: the return value will be -1 and errno - * ENOSYS if the syscall is not available, in that case fall back - * to /dev/urandom. - */ - GetDevURandom(ent32); - } else { - RandFailure(); - } + if (getrandom(ent32, NUM_OS_RANDOM_BYTES, 0) != NUM_OS_RANDOM_BYTES) { + RandFailure(); } #elif defined(__OpenBSD__) /* OpenBSD. From the arc4random(3) man page: @@ -383,16 +370,10 @@ void GetOSRand(unsigned char *ent32) The function call is always successful. */ arc4random_buf(ent32, NUM_OS_RANDOM_BYTES); - // Silence a compiler warning about unused function. - (void)GetDevURandom; #elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) - /* getentropy() is available on macOS 10.12 and later. - */ if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { RandFailure(); } - // Silence a compiler warning about unused function. - (void)GetDevURandom; #elif defined(HAVE_SYSCTL_ARND) /* FreeBSD, NetBSD and similar. It is possible for the call to return less * bytes than requested, so need to read in a loop. @@ -406,8 +387,6 @@ void GetOSRand(unsigned char *ent32) } have += len; } while (have < NUM_OS_RANDOM_BYTES); - // Silence a compiler warning about unused function. - (void)GetDevURandom; #else /* Fall back to /dev/urandom if there is no specific method implemented to * get system entropy for this OS.