From 0377fd88d420e03197e5428a2b4e283682c18cd1 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Sun, 28 Mar 2021 20:31:49 +0100 Subject: [PATCH] Improve BSDs support. --- src/mem/entropy.h | 1 + src/pal/pal_dragonfly.h | 13 +++++++++++++ src/pal/pal_netbsd.h | 7 ++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/mem/entropy.h b/src/mem/entropy.h index 359e185..17b69a2 100644 --- a/src/mem/entropy.h +++ b/src/mem/entropy.h @@ -1,4 +1,5 @@ #include "../ds/address.h" +#include "../pal/pal.h" #include #include diff --git a/src/pal/pal_dragonfly.h b/src/pal/pal_dragonfly.h index f0c3daf..5b572d9 100644 --- a/src/pal/pal_dragonfly.h +++ b/src/pal/pal_dragonfly.h @@ -25,6 +25,19 @@ namespace snmalloc * to add new features that they should add any required feature flags. */ static constexpr uint64_t pal_features = PALPOSIX::pal_features; + + /** + * Source of Entropy + * + * DragonflyBSD does not have getentropy, so use getrandom instead. + */ + static uint64_t get_entropy64() + { + uint64_t result; + if (getrandom(&result, sizeof(result), 0) != sizeof(result)) + error("Failed to get system randomness"); + return result; + } }; } // namespace snmalloc #endif diff --git a/src/pal/pal_netbsd.h b/src/pal/pal_netbsd.h index d6ebf71..ee1d566 100644 --- a/src/pal/pal_netbsd.h +++ b/src/pal/pal_netbsd.h @@ -22,8 +22,13 @@ namespace snmalloc * generic BSD with support for arbitrary alignment from `mmap`. This * 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. */ - static constexpr uint64_t pal_features = PALBSD_Aligned::pal_features; + static constexpr uint64_t pal_features = + PALBSD_Aligned::pal_features & ~Entropy; }; } // namespace snmalloc #endif