Fix building on old libc systems without getentropy (#329)
Signed-off-by: Matthias Wahl <mwahl@wayfair.com>
This commit is contained in:
@@ -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(<unistd.h>)
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
#if __has_include(<sys/random.h>)
|
||||
#include <sys/random.h>
|
||||
#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()
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -1,17 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(__HAIKU__)
|
||||
# include <cassert>
|
||||
|
||||
namespace snmalloc
|
||||
{
|
||||
template<typename... Args>
|
||||
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
|
||||
|
||||
@@ -1,18 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __NetBSD__
|
||||
# include <cassert>
|
||||
|
||||
namespace snmalloc
|
||||
{
|
||||
template<typename... Args>
|
||||
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
|
||||
|
||||
@@ -19,6 +19,12 @@
|
||||
# include <unistd.h>
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user