Expose entropy on POSIX platforms
This provides a common definition for many POSIX platforms. It can be disabled.
This commit is contained in:
committed by
Matthew Parkinson
parent
6daa261161
commit
cc5f1cfe9f
@@ -18,8 +18,11 @@ 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;
|
||||
static constexpr uint64_t pal_features =
|
||||
PALPOSIX::pal_features & ~(Entropy);
|
||||
|
||||
/**
|
||||
* Haiku requires an explicit no-reserve flag in `mmap` to guarantee lazy
|
||||
|
||||
@@ -12,6 +12,12 @@
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
#if __has_include(<sys/random.h>)
|
||||
# include <sys/random.h>
|
||||
#endif
|
||||
#if __has_include(<unistd.h>)
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
extern "C" int puts(const char* str);
|
||||
|
||||
@@ -113,7 +119,7 @@ namespace snmalloc
|
||||
*
|
||||
* POSIX systems are assumed to support lazy commit.
|
||||
*/
|
||||
static constexpr uint64_t pal_features = LazyCommit;
|
||||
static constexpr uint64_t pal_features = LazyCommit | Entropy;
|
||||
|
||||
static constexpr size_t page_size = Aal::smallest_page_size;
|
||||
|
||||
@@ -264,5 +270,31 @@ namespace snmalloc
|
||||
|
||||
OS::error("Out of memory");
|
||||
}
|
||||
|
||||
/**
|
||||
* Source of Entropy
|
||||
*
|
||||
* This is a default that works on many POSIX platforms.
|
||||
*/
|
||||
static uint64_t get_entropy64()
|
||||
{
|
||||
if constexpr (!pal_supports<Entropy, OS>)
|
||||
{
|
||||
// Derived Pal does not provide entropy.
|
||||
return 0;
|
||||
}
|
||||
else if constexpr (OS::get_entropy64 != get_entropy64)
|
||||
{
|
||||
// Derived Pal has provided a custom definition.
|
||||
return OS::get_entropy64();
|
||||
}
|
||||
else
|
||||
{
|
||||
uint64_t result;
|
||||
if (getentropy(&result, sizeof(result)) != 0)
|
||||
error("Failed to get system randomness");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace snmalloc
|
||||
|
||||
Reference in New Issue
Block a user