From 589ecfab02d0f95f30fc04584dc5398ef544462e Mon Sep 17 00:00:00 2001 From: Matthias Wahl Date: Tue, 25 May 2021 17:46:06 +0200 Subject: [PATCH] Fix building on old libc systems without `getentropy` (#329) Signed-off-by: Matthias Wahl --- CMakeLists.txt | 22 ++++++++++++++++++++++ azure-pipelines.yml | 2 +- src/pal/pal_haiku.h | 16 +--------------- src/pal/pal_netbsd.h | 20 +++----------------- src/pal/pal_posix.h | 15 +++++++++++++-- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index efff25d..dfb1c13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,22 @@ size_t malloc_usable_size(const void* ptr) { return 0; } int main() { return 0; } " CONST_QUALIFIED_MALLOC_USABLE_SIZE) +# older libcs might not have getentropy, e.g. it appeared in gliobc 2.25 +# so we need to fallback if we cannot compile this +CHECK_C_SOURCE_COMPILES(" +#if __has_include() +# include +#endif +#if __has_include() +#include +#endif +int main() { + int entropy = 0; + int res = getentropy(&entropy, sizeof(entropy)); + return res; +} +" SNMALLOC_PLATFORM_HAS_GETENTROPY) + if (NOT SNMALLOC_CI_BUILD) option(USE_POSIX_COMMIT_CHECKS "Instrument Posix PAL to check for access to unused blocks of memory." Off) else () @@ -160,6 +176,12 @@ if(USE_POSIX_COMMIT_CHECKS) target_compile_definitions(snmalloc_lib INTERFACE -DUSE_POSIX_COMMIT_CHECKS) endif() +if(SNMALLOC_PLATFORM_HAS_GETENTROPY) + target_compile_definitions(snmalloc_lib INTERFACE -DSNMALLOC_PLATFORM_HAS_GETENTROPY=Entropy) +else() + target_compile_definitions(snmalloc_lib INTERFACE -DSNMALLOC_PLATFORM_HAS_GETENTROPY=0) +endif() + if(CONST_QUALIFIED_MALLOC_USABLE_SIZE) target_compile_definitions(snmalloc_lib INTERFACE -DMALLOC_USABLE_SIZE_QUALIFIER=const) endif() diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 1555d92..6674f31 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -342,6 +342,6 @@ jobs: - script: | set -eo pipefail - clang-tidy-9 src/override/malloc.cc -header-filter="`pwd`/*" -warnings-as-errors='*' -export-fixes=tidy.fail -- -std=c++17 -mcx16 + clang-tidy-9 src/override/malloc.cc -header-filter="`pwd`/*" -warnings-as-errors='*' -export-fixes=tidy.fail -- -std=c++17 -mcx16 -DSNMALLOC_PLATFORM_HAS_GETENTROPY=0 displayName: 'Clang-Tidy' diff --git a/src/pal/pal_haiku.h b/src/pal/pal_haiku.h index 52bb7ef..9cc2b8d 100644 --- a/src/pal/pal_haiku.h +++ b/src/pal/pal_haiku.h @@ -1,17 +1,6 @@ #pragma once #if defined(__HAIKU__) -# include - -namespace snmalloc -{ - template - int getentropy(Args...) - { - assert(0 && "Unreachable path"); - return -1; - } -} # include "pal_posix.h" @@ -30,11 +19,8 @@ namespace snmalloc * Bitmap of PalFeatures flags indicating the optional features that this * PAL supports. * - * Disable the POSIX default implementation of Entropy as not supported on - * Haiku. */ - static constexpr uint64_t pal_features = - PALPOSIX::pal_features & ~(Entropy); + static constexpr uint64_t pal_features = PALPOSIX::pal_features; /** * Haiku requires an explicit no-reserve flag in `mmap` to guarantee lazy diff --git a/src/pal/pal_netbsd.h b/src/pal/pal_netbsd.h index 0afc025..e91c4b2 100644 --- a/src/pal/pal_netbsd.h +++ b/src/pal/pal_netbsd.h @@ -1,18 +1,6 @@ #pragma once #ifdef __NetBSD__ -# include - -namespace snmalloc -{ - template - int getentropy(Args...) - { - assert(0 && "Unreachable path"); - return -1; - } -} - # include "pal_bsd_aligned.h" namespace snmalloc @@ -35,12 +23,10 @@ namespace snmalloc * field is declared explicitly to remind anyone modifying this class to * add new features that they should add any required feature flags. * - * We disable the default implementation of randomness on NetBSD as it - * does not have the getentropy call. This will currently fallback to - * C++ libraries std::random_device. + * As NetBSD does not have the getentropy call, get_entropy64 will + * currently fallback to C++ libraries std::random_device. */ - static constexpr uint64_t pal_features = - PALBSD_Aligned::pal_features & ~Entropy; + static constexpr uint64_t pal_features = PALBSD_Aligned::pal_features; }; } // namespace snmalloc #endif diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index 954cc06..fbb037e 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -19,6 +19,12 @@ # include #endif +// default value for builds not using CMake +#ifndef SNMALLOC_PLATFORM_HAS_GETENTROPY +# define SNMALLOC_PLATFORM_HAS_GETENTROPY 0 +# warning snmalloc not using entropy source. To silence this warning please define SNMALLOC_PLATFORM_HAS_GETENTROPY to 0, or to Entropy if your platform implements getentropy(). +#endif + extern "C" int puts(const char* str); namespace snmalloc @@ -117,9 +123,11 @@ namespace snmalloc * Bitmap of PalFeatures flags indicating the optional features that this * PAL supports. * - * POSIX systems are assumed to support lazy commit. + * POSIX systems are assumed to support lazy commit. The build system checks + * getentropy is available, only then this PAL supports Entropy. */ - static constexpr uint64_t pal_features = LazyCommit | Entropy; + static constexpr uint64_t pal_features = + LazyCommit | SNMALLOC_PLATFORM_HAS_GETENTROPY; static constexpr size_t page_size = Aal::smallest_page_size; @@ -290,11 +298,14 @@ namespace snmalloc } else { +#ifdef SNMALLOC_PLATFORM_HAS_GETENTROPY uint64_t result; if (getentropy(&result, sizeof(result)) != 0) error("Failed to get system randomness"); return result; +#endif } + error("Entropy requested on platform that does not provide entropy"); } }; } // namespace snmalloc