mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-04 22:49:32 +03:00
shorten comments for pr #390
This commit is contained in:
parent
48996ff449
commit
0e851de2ba
1 changed files with 13 additions and 25 deletions
38
src/random.c
38
src/random.c
|
@ -160,7 +160,7 @@ uintptr_t _mi_random_next(mi_random_ctx_t* ctx) {
|
||||||
/* ----------------------------------------------------------------------------
|
/* ----------------------------------------------------------------------------
|
||||||
To initialize a fresh random context we rely on the OS:
|
To initialize a fresh random context we rely on the OS:
|
||||||
- Windows : BCryptGenRandom (or RtlGenRandom)
|
- Windows : BCryptGenRandom (or RtlGenRandom)
|
||||||
- macOS : CCRandomGenerateBytes
|
- macOS : CCRandomGenerateBytes, arc4random_buf
|
||||||
- bsd,wasi : arc4random_buf
|
- bsd,wasi : arc4random_buf
|
||||||
- Linux : getrandom,/dev/urandom
|
- Linux : getrandom,/dev/urandom
|
||||||
If we cannot get good randomness, we fall back to weak randomness based on a timer and ASLR.
|
If we cannot get good randomness, we fall back to weak randomness based on a timer and ASLR.
|
||||||
|
@ -194,34 +194,22 @@ static bool os_random_buf(void* buf, size_t buf_len) {
|
||||||
|
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
#include <AvailabilityMacros.h>
|
#include <AvailabilityMacros.h>
|
||||||
#if defined(MAC_OS_X_VERSION_10_10) && \
|
#if defined(MAC_OS_X_VERSION_10_10) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_10
|
||||||
MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_10
|
|
||||||
#include <CommonCrypto/CommonRandom.h>
|
#include <CommonCrypto/CommonRandom.h>
|
||||||
#endif
|
#endif
|
||||||
static bool os_random_buf(void* buf, size_t buf_len) {
|
static bool os_random_buf(void* buf, size_t buf_len) {
|
||||||
/* The implementation of arc4random_buf(3) differs from its documentation.
|
#if defined(MAC_OS_X_VERSION_10_15) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_15
|
||||||
* It is documented as "always successful, and no return value is reserved
|
// We prefere CCRandomGenerateBytes as it returns an error code while arc4random_buf
|
||||||
* to indicate an error." However, the actual implementation invokes the
|
// may fail silently on macOS. See PR #390, and <https://opensource.apple.com/source/Libc/Libc-1439.40.11/gen/FreeBSD/arc4random.c.auto.html>
|
||||||
* function "ccrng_generate" without validating the error cases. It might
|
return (CCRandomGenerateBytes(buf, buf_len) == kCCSuccess);
|
||||||
* fail silently, which leads to unexpected source of entropy.
|
#else
|
||||||
* See:
|
// fall back on older macOS
|
||||||
* https://opensource.apple.com/source/Libc/Libc-1439.40.11/gen/FreeBSD/arc4random.c.auto.html
|
arc4random_buf(buf, buf_len);
|
||||||
*
|
return true;
|
||||||
* CCRandomGenerateBytes(), on the contrary, returns cryptographically strong
|
#endif
|
||||||
* random bits with explicit status code.
|
|
||||||
*/
|
|
||||||
#if defined(MAC_OS_X_VERSION_10_15) && \
|
|
||||||
MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_15
|
|
||||||
return CCRandomGenerateBytes(buf, buf_len) == kCCSuccess;
|
|
||||||
#else
|
|
||||||
/* Prior to macOS 10.15, CCRandomGenerateBytes() might take a bit longer time
|
|
||||||
* to complete, so failback to arc4random_buf().
|
|
||||||
*/
|
|
||||||
arc4random_buf(buf, buf_len);
|
|
||||||
return true;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
#elif defined(ANDROID) || defined(__DragonFly__) || \
|
|
||||||
|
#elif defined(__ANDROID__) || defined(__DragonFly__) || \
|
||||||
defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
|
defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
|
||||||
defined(__sun) // todo: what to use with __wasi__?
|
defined(__sun) // todo: what to use with __wasi__?
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
Loading…
Add table
Reference in a new issue