From ca40bcb8dc0fc6e9749587f4662a62e63f549fb0 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 13 Jul 2023 08:16:44 -0700 Subject: [PATCH 1/9] Fixes the way activity id is generated --- .../nativeaot/Runtime/eventpipe/ds-rt-aot.h | 9 +- .../nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 126 +++++++++++++++++- .../nativeaot/Runtime/eventpipe/ep-rt-aot.h | 79 ++--------- 3 files changed, 135 insertions(+), 79 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h index 811a39b574f96..18c8753475148 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h @@ -181,13 +181,8 @@ ds_rt_generate_core_dump ( { STATIC_CONTRACT_NOTHROW; - ds_ipc_result_t result = DS_IPC_E_FAIL; - uint32_t flags = ds_generate_core_dump_command_payload_get_flags(payload); - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Generate an exception dump - // PalDebugBreak(); - - return 0; + // Eventpipe driven core_dump is not currently supported in NativeAOT + return DS_IPC_E_NOTSUPPORTED; } /* diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index 90d2c27523c28..c216898d86303 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -17,6 +17,10 @@ #include #endif +#if defined(__APPLE__) && __APPLE__ +#include +#endif + // The regdisplay.h, StackFrameIterator.h, and thread.h includes are present only to access the Thread // class and can be removed if it turns out that the required ep_rt_thread_handle_t can be // implemented in some manner that doesn't rely on the Thread class. @@ -139,8 +143,6 @@ ep_rt_aot_atomic_inc_int64_t (volatile int64_t *value) { STATIC_CONTRACT_NOTHROW; - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Consider replacing with a new PalInterlockedIncrement64 service int64_t currentValue; do { currentValue = *value; @@ -154,8 +156,6 @@ int64_t ep_rt_aot_atomic_dec_int64_t (volatile int64_t *value) { STATIC_CONTRACT_NOTHROW; - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Consider replacing with a new PalInterlockedDecrement64 service int64_t currentValue; do { currentValue = *value; @@ -268,8 +268,7 @@ ep_rt_aot_thread_create ( STATIC_CONTRACT_NOTHROW; EP_ASSERT (thread_func != NULL); - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Fill in the outgoing id if any callers ever need it + // Note that none of the callers examine the return id in any way if (id) *reinterpret_cast(id) = 0xffffffff; @@ -707,6 +706,121 @@ void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array) #endif } +// Generate cryptographically strong random bytes. +// Return 0 on success, -1 on failure. +// copying code from SystemNative_GetCryptographicallySecureRandomBytes with slight modification +int32_t aot_get_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength) +{ + assert(buffer != NULL); +#ifdef __linux__ + static volatile int rand_des = -1; + static bool sMissingDevURandom; + + if (!sMissingDevURandom) + { + if (rand_des == -1) + { + int fd; + + do + { + fd = open("/dev/urandom", O_RDONLY); + fcntl(fd, F_SETFD, FD_CLOEXEC); + } + while ((fd == -1) && (errno == EINTR)); + + if (fd != -1) + { + int expected = -1; + if (!__atomic_compare_exchange_n(&rand_des, &expected, fd, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) + { + // Another thread has already set the rand_des + close(fd); + } + } + else if (errno == ENOENT) + { + sMissingDevURandom = true; + } + } + + if (rand_des != -1) + { + int32_t offset = 0; + do + { + ssize_t n = read(rand_des, buffer + offset , (size_t)(bufferLength - offset)); + if (n == -1) + { + if (errno == EINTR) + { + continue; + } + return -1; + } + + offset += n; + } + while (offset != bufferLength); + return 0; + } + } +#elif defined(__APPLE__) && __APPLE__ + CCRNGStatus status = CCRandomGenerateBytes(buffer, bufferLength); + + if (status == kCCSuccess) + { + return 0; + } + else + { + return -1; + } +#endif + return -1; +} + +void ep_rt_aot_create_activity_id (uint8_t *activity_id, uint32_t activity_id_len) +{ + // We call CoCreateGuid for windows, and use a random generator for non-windows + STATIC_CONTRACT_NOTHROW; + EP_ASSERT (activity_id != NULL); + EP_ASSERT (activity_id_len == EP_ACTIVITY_ID_SIZE); +#ifdef HOST_WIN32 + CoCreateGuid (reinterpret_cast(activity_id)); +#else + if(aot_get_cryptographically_secure_random_bytes(activity_id, activity_id_len)==-1) + { + *activity_id=0; + return; + } + + const uint16_t version_mask = 0xF000; + const uint16_t random_guid_version = 0x4000; + const uint8_t clock_seq_hi_and_reserved_mask = 0xC0; + const uint8_t clock_seq_hi_and_reserved_value = 0x80; + + // Modify bits indicating the type of the GUID + uint8_t *activity_id_c = activity_id + sizeof (uint32_t) + sizeof (uint16_t); + uint8_t *activity_id_d = activity_id + sizeof (uint32_t) + sizeof (uint16_t) + sizeof (uint16_t); + + uint16_t c; + memcpy (&c, activity_id_c, sizeof (c)); + + uint8_t d; + memcpy (&d, activity_id_d, sizeof (d)); + + // time_hi_and_version + c = ((c & ~version_mask) | random_guid_version); + // clock_seq_hi_and_reserved + d = ((d & ~clock_seq_hi_and_reserved_mask) | clock_seq_hi_and_reserved_value); + + memcpy (activity_id_c, &c, sizeof (c)); + memcpy (activity_id_d, &d, sizeof (d)); +#endif +} + + #ifdef EP_CHECKED_BUILD void ep_rt_aot_lock_requires_lock_held (const ep_rt_lock_handle_t *lock) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index 25c9666b78c01..180cee398acfa 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -56,8 +56,7 @@ extern pthread_key_t eventpipe_tls_key; extern __thread EventPipeThreadHolder* eventpipe_tls_instance; #endif -// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase -// TODO: The NativeAOT ALIGN_UP is defined in a tangled manner that generates linker errors if +// The NativeAOT ALIGN_UP is defined in a tangled manner that generates linker errors if // it is used here; instead, define a version tailored to the existing usage in the shared // EventPipe code. static inline uint8_t* _rt_aot_align_up(uint8_t* val, uintptr_t alignment) @@ -326,10 +325,7 @@ ep_rt_method_get_simple_assembly_name ( { STATIC_CONTRACT_NOTHROW; - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Design MethodDesc and method name services if/when needed - //PalDebugBreak(); - + // NativeAOT does not support method_desc operations return false; } @@ -341,10 +337,7 @@ ep_rt_method_get_full_name ( ep_char8_t *name, size_t name_len) { - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Design MethodDesc and method name services if/when needed - //PalDebugBreak(); - + // NativeAOT does not support method_desc operations return false; } @@ -615,11 +608,9 @@ EventPipeWaitHandle ep_rt_wait_event_get_wait_handle (ep_rt_wait_event_handle_t *wait_event) { STATIC_CONTRACT_NOTHROW; - // EP_ASSERT (wait_event != NULL && wait_event->event != NULL); - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: NativeAOT CLREventStatic doesn't have GetHandleUNHOSTED - // PalDebugBreak(); + // This is not reached in the current product + abort(); return 0; } @@ -677,41 +668,8 @@ ep_rt_create_activity_id ( uint8_t *activity_id, uint32_t activity_id_len) { - STATIC_CONTRACT_NOTHROW; - EP_ASSERT (activity_id != NULL); - EP_ASSERT (activity_id_len == EP_ACTIVITY_ID_SIZE); - - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Implement a way to generate a real Guid - // CoCreateGuid (reinterpret_cast(activity_id)); - - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Using roughly Mono's implementation but Mono randomly generates this, hardcoding for now - uint8_t data1[] = {0x67,0xac,0x33,0xf1,0x8d,0xed,0x41,0x01,0xb4,0x26,0xc9,0xb7,0x94,0x35,0xf7,0x8a}; - memcpy (activity_id, data1, EP_ACTIVITY_ID_SIZE); - - const uint16_t version_mask = 0xF000; - const uint16_t random_guid_version = 0x4000; - const uint8_t clock_seq_hi_and_reserved_mask = 0xC0; - const uint8_t clock_seq_hi_and_reserved_value = 0x80; - - // Modify bits indicating the type of the GUID - uint8_t *activity_id_c = activity_id + sizeof (uint32_t) + sizeof (uint16_t); - uint8_t *activity_id_d = activity_id + sizeof (uint32_t) + sizeof (uint16_t) + sizeof (uint16_t); - - uint16_t c; - memcpy (&c, activity_id_c, sizeof (c)); - - uint8_t d; - memcpy (&d, activity_id_d, sizeof (d)); - - // time_hi_and_version - c = ((c & ~version_mask) | random_guid_version); - // clock_seq_hi_and_reserved - d = ((d & ~clock_seq_hi_and_reserved_mask) | clock_seq_hi_and_reserved_value); - - memcpy (activity_id_c, &c, sizeof (c)); - memcpy (activity_id_d, &d, sizeof (d)); + extern void ep_rt_aot_create_activity_id (uint8_t *activity_id, uint32_t activity_id_len); + ep_rt_aot_create_activity_id(activity_id, activity_id_len); } static @@ -720,9 +678,9 @@ bool ep_rt_is_running (void) { STATIC_CONTRACT_NOTHROW; - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Does NativeAot have the concept of EEStarted - // PalDebugBreak(); + + // This is only used to check if the profiler can be attached + // Profiler attach is not supported in NativeAOT return false; } @@ -734,11 +692,7 @@ ep_rt_execute_rundown (dn_vector_ptr_t *execution_checkpoints) { STATIC_CONTRACT_NOTHROW; - //TODO: Write execution checkpoint rundown events. - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: EventPipe Configuration values - RhConfig? - // (CLRConfig::INTERNAL_EventPipeCircularMB) - // PalDebugBreak(); + // NativeAOT does not currently support rundown } /* @@ -774,8 +728,6 @@ EP_RT_DEFINE_THREAD_FUNC (ep_rt_thread_aot_start_session_or_sampling_thread) ep_rt_thread_params_t* thread_params = reinterpret_cast(data); - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Implement thread creation/management if needed. // The session and sampling threads both assert that the incoming thread handle is // non-null, but do not necessarily rely on it otherwise; just pass a meaningless non-null // value until testing shows that a meaningful value is needed. @@ -810,9 +762,7 @@ inline void ep_rt_set_server_name(void) { - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Need to set name for the thread - // ::SetThreadName(GetCurrentThread(), W(".NET EventPipe")); + // This is optional, decorates the thread name with EventPipe specific information } @@ -1588,10 +1538,7 @@ ep_rt_thread_setup (void) { STATIC_CONTRACT_NOTHROW; - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // TODO: Implement thread creation/management if needed - // Thread* thread_handle = SetupThreadNoThrow (); - // EP_ASSERT (thread_handle != NULL); + // Likely not needed and do nothing until testing shows to be required } #ifdef TARGET_UNIX From 5c1deb93d2d928e70b302f1c829186cd09536de2 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Tue, 18 Jul 2023 08:35:33 -0700 Subject: [PATCH 2/9] FB --- .../Runtime/eventpipe/CMakeLists.txt | 17 +++- .../nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 78 +------------------ src/native/libs/System.Native/CMakeLists.txt | 14 +++- src/native/libs/System.Native/entrypoints.c | 2 +- .../pal_random.c => minipal/random.c} | 4 +- .../pal_random.h => minipal/random.h} | 11 ++- 6 files changed, 41 insertions(+), 85 deletions(-) rename src/native/{libs/System.Native/pal_random.c => minipal/random.c} (98%) rename src/native/{libs/System.Native/pal_random.h => minipal/random.h} (73%) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt b/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt index b8a8808f9b0f1..dea22e9c44890 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt +++ b/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt @@ -9,33 +9,40 @@ set(AOT_EVENTPIPE_SHIM_DIR "${CMAKE_CURRENT_SOURCE_DIR}") set (CONTAINER_SOURCES "") set (CONTAINER_HEADERS "") +set (MINIPAL_SOURCES "") set (EVENTPIPE_SOURCES "") set (EVENTPIPE_HEADERS "") set (GEN_EVENTPIPE_SOURCES "") set (SHARED_CONTAINERS_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/containers") set (SHARED_EVENTPIPE_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/eventpipe") +set (SHARED_MINIPAL_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/minipal") include (${SHARED_EVENTPIPE_SOURCE_PATH}/eventpipe.cmake) include (${SHARED_CONTAINERS_SOURCE_PATH}/containers.cmake) if(CLR_CMAKE_HOST_WIN32) list(APPEND SHARED_DIAGNOSTIC_SERVER_SOURCES - ds-ipc-pal-namedpipe.c + ds-ipc-pal-namedpipe.c ) list(APPEND SHARED_DIAGNOSTIC_SERVER_HEADERS - ds-ipc-pal-namedpipe.h + ds-ipc-pal-namedpipe.h ) endif(CLR_CMAKE_HOST_WIN32) if(CLR_CMAKE_HOST_UNIX) list(APPEND SHARED_DIAGNOSTIC_SERVER_SOURCES - ds-ipc-pal-socket.c + ds-ipc-pal-socket.c ) list(APPEND SHARED_DIAGNOSTIC_SERVER_HEADERS - ds-ipc-pal-socket.h + ds-ipc-pal-socket.h ) + + list(APPEND MINIPAL_SOURCES + random.c + ) + endif(CLR_CMAKE_HOST_UNIX) list(APPEND EVENTPIPE_SOURCES @@ -50,6 +57,7 @@ list(APPEND EVENTPIPE_HEADERS addprefix(CONTAINER_SOURCES ${SHARED_CONTAINERS_SOURCE_PATH} "${SHARED_CONTAINER_SOURCES}") addprefix(CONTAINER_HEADERS ${SHARED_CONTAINERS_SOURCE_PATH} "${SHARED_CONTAINER_HEADERS}") +addprefix(MINIPAL_SOURCES ${SHARED_MINIPAL_SOURCE_PATH} "${MINIPAL_SOURCES}") addprefix(EVENTPIPE_SOURCES ${SHARED_EVENTPIPE_SOURCE_PATH} "${EVENTPIPE_SOURCES}") addprefix(EVENTPIPE_HEADERS ${SHARED_EVENTPIPE_SOURCE_PATH} "${EVENTPIPE_HEADERS}") @@ -106,6 +114,7 @@ list(APPEND EVENTPIPE_SOURCES ${GEN_EVENTPIPE_SOURCES} ${CONTAINER_SOURCES} ${CONTAINER_HEADERS} + ${MINIPAL_SOURCES} ) list(APPEND AOT_EVENTPIPE_DISABLED_SOURCES diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index c216898d86303..c83a3a322ea4a 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -21,6 +21,8 @@ #include #endif +#include + // The regdisplay.h, StackFrameIterator.h, and thread.h includes are present only to access the Thread // class and can be removed if it turns out that the required ep_rt_thread_handle_t can be // implemented in some manner that doesn't rely on the Thread class. @@ -706,80 +708,6 @@ void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array) #endif } -// Generate cryptographically strong random bytes. -// Return 0 on success, -1 on failure. -// copying code from SystemNative_GetCryptographicallySecureRandomBytes with slight modification -int32_t aot_get_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength) -{ - assert(buffer != NULL); -#ifdef __linux__ - static volatile int rand_des = -1; - static bool sMissingDevURandom; - - if (!sMissingDevURandom) - { - if (rand_des == -1) - { - int fd; - - do - { - fd = open("/dev/urandom", O_RDONLY); - fcntl(fd, F_SETFD, FD_CLOEXEC); - } - while ((fd == -1) && (errno == EINTR)); - - if (fd != -1) - { - int expected = -1; - if (!__atomic_compare_exchange_n(&rand_des, &expected, fd, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) - { - // Another thread has already set the rand_des - close(fd); - } - } - else if (errno == ENOENT) - { - sMissingDevURandom = true; - } - } - - if (rand_des != -1) - { - int32_t offset = 0; - do - { - ssize_t n = read(rand_des, buffer + offset , (size_t)(bufferLength - offset)); - if (n == -1) - { - if (errno == EINTR) - { - continue; - } - return -1; - } - - offset += n; - } - while (offset != bufferLength); - return 0; - } - } -#elif defined(__APPLE__) && __APPLE__ - CCRNGStatus status = CCRandomGenerateBytes(buffer, bufferLength); - - if (status == kCCSuccess) - { - return 0; - } - else - { - return -1; - } -#endif - return -1; -} - void ep_rt_aot_create_activity_id (uint8_t *activity_id, uint32_t activity_id_len) { // We call CoCreateGuid for windows, and use a random generator for non-windows @@ -789,7 +717,7 @@ void ep_rt_aot_create_activity_id (uint8_t *activity_id, uint32_t activity_id_le #ifdef HOST_WIN32 CoCreateGuid (reinterpret_cast(activity_id)); #else - if(aot_get_cryptographically_secure_random_bytes(activity_id, activity_id_len)==-1) + if(SystemNative_GetCryptographicallySecureRandomBytes(activity_id, activity_id_len)==-1) { *activity_id=0; return; diff --git a/src/native/libs/System.Native/CMakeLists.txt b/src/native/libs/System.Native/CMakeLists.txt index 2c6249cc51627..05e2d0b4ebc20 100644 --- a/src/native/libs/System.Native/CMakeLists.txt +++ b/src/native/libs/System.Native/CMakeLists.txt @@ -14,6 +14,15 @@ if (CLR_CMAKE_TARGET_OSX) add_definitions(-D_DARWIN_C_SOURCE) endif () +set (MINIPAL_SOURCES "") +set (SHARED_MINIPAL_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/minipal") + +list(APPEND MINIPAL_SOURCES +random.c +) + +addprefix(MINIPAL_SOURCES ${SHARED_MINIPAL_SOURCE_PATH} "${MINIPAL_SOURCES}") + set(NATIVE_SOURCES pal_errno.c pal_interfaceaddresses.c @@ -21,7 +30,6 @@ set(NATIVE_SOURCES pal_maphardwaretype.c pal_memory.c pal_networkstatistics.c - pal_random.c pal_runtimeinformation.c pal_string.c pal_tcpstate.c @@ -30,6 +38,10 @@ set(NATIVE_SOURCES pal_sysctl.c ) +list(APPEND NATIVE_SOURCES + ${MINIPAL_SOURCES} +) + if (NOT CLR_CMAKE_TARGET_WASI) list (APPEND NATIVE_SOURCES pal_dynamicload.c diff --git a/src/native/libs/System.Native/entrypoints.c b/src/native/libs/System.Native/entrypoints.c index 394a39b0d0036..ad2102cd433b7 100644 --- a/src/native/libs/System.Native/entrypoints.c +++ b/src/native/libs/System.Native/entrypoints.c @@ -20,7 +20,7 @@ #include "pal_networking.h" #include "pal_networkstatistics.h" #include "pal_process.h" -#include "pal_random.h" +#include #include "pal_runtimeinformation.h" #include "pal_searchpath.h" #include "pal_signal.h" diff --git a/src/native/libs/System.Native/pal_random.c b/src/native/minipal/random.c similarity index 98% rename from src/native/libs/System.Native/pal_random.c rename to src/native/minipal/random.c index bdacacb7828d5..7bacd10effdca 100644 --- a/src/native/libs/System.Native/pal_random.c +++ b/src/native/minipal/random.c @@ -14,8 +14,8 @@ #include #endif -#include "pal_config.h" -#include "pal_random.h" +// #include "pal_config.h" +#include "random.h" /* diff --git a/src/native/libs/System.Native/pal_random.h b/src/native/minipal/random.h similarity index 73% rename from src/native/libs/System.Native/pal_random.h rename to src/native/minipal/random.h index 787b47bfd0686..a68f03ba00b04 100644 --- a/src/native/libs/System.Native/pal_random.h +++ b/src/native/minipal/random.h @@ -3,8 +3,15 @@ #pragma once -#include "pal_compiler.h" -#include "pal_types.h" +#include +#ifdef __cplusplus +extern "C" +{ +#endif // __cplusplus PALEXPORT void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength); PALEXPORT int32_t SystemNative_GetCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength); + +#ifdef __cplusplus +} +#endif // __cplusplus From a51d102ef91e0f06c91ece190a6e9b57939a0e64 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Wed, 19 Jul 2023 13:58:36 -0700 Subject: [PATCH 3/9] FB --- .../nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 2 +- .../System.Native/Interop.GetRandomBytes.cs | 4 +- src/native/libs/System.Native/entrypoints.c | 4 +- src/native/libs/configure.cmake | 3 + src/native/minipal/compiler.h | 63 +++++++++++++++++++ src/native/minipal/random.c | 8 +-- src/native/minipal/random.h | 7 ++- 7 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 src/native/minipal/compiler.h diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index c83a3a322ea4a..9aceb2ee6cf04 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -717,7 +717,7 @@ void ep_rt_aot_create_activity_id (uint8_t *activity_id, uint32_t activity_id_le #ifdef HOST_WIN32 CoCreateGuid (reinterpret_cast(activity_id)); #else - if(SystemNative_GetCryptographicallySecureRandomBytes(activity_id, activity_id_len)==-1) + if(minipal_get_cryptographically_secure_random_bytes(activity_id, activity_id_len)==-1) { *activity_id=0; return; diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetRandomBytes.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetRandomBytes.cs index 9d80ea4406860..c325f4044838b 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetRandomBytes.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetRandomBytes.cs @@ -8,10 +8,10 @@ internal static partial class Interop { internal static partial class Sys { - [LibraryImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetNonCryptographicallySecureRandomBytes")] + [LibraryImport(Interop.Libraries.SystemNative, EntryPoint = "minipal_get_non_cryptographically_secure_random_bytes")] internal static unsafe partial void GetNonCryptographicallySecureRandomBytes(byte* buffer, int length); - [LibraryImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetCryptographicallySecureRandomBytes")] + [LibraryImport(Interop.Libraries.SystemNative, EntryPoint = "minipal_get_cryptographically_secure_random_bytes")] internal static unsafe partial int GetCryptographicallySecureRandomBytes(byte* buffer, int length); } diff --git a/src/native/libs/System.Native/entrypoints.c b/src/native/libs/System.Native/entrypoints.c index ad2102cd433b7..8cab3483c10ef 100644 --- a/src/native/libs/System.Native/entrypoints.c +++ b/src/native/libs/System.Native/entrypoints.c @@ -215,8 +215,8 @@ static const Entry s_sysNative[] = DllImportEntry(SystemNative_SchedSetAffinity) DllImportEntry(SystemNative_SchedGetAffinity) DllImportEntry(SystemNative_GetProcessPath) - DllImportEntry(SystemNative_GetNonCryptographicallySecureRandomBytes) - DllImportEntry(SystemNative_GetCryptographicallySecureRandomBytes) + DllImportEntry(minipal_get_non_cryptographically_secure_random_bytes) + DllImportEntry(minipal_get_cryptographically_secure_random_bytes) DllImportEntry(SystemNative_GetUnixRelease) DllImportEntry(SystemNative_GetUnixVersion) DllImportEntry(SystemNative_GetOSArchitecture) diff --git a/src/native/libs/configure.cmake b/src/native/libs/configure.cmake index cbaac692f2630..067b3b30ec4c9 100644 --- a/src/native/libs/configure.cmake +++ b/src/native/libs/configure.cmake @@ -1180,3 +1180,6 @@ check_c_source_compiles( configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Common/pal_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/Common/pal_config.h) +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/Common/pal_config.h.in + ${CMAKE_CURRENT_BINARY_DIR}/Common/config.h) \ No newline at end of file diff --git a/src/native/minipal/compiler.h b/src/native/minipal/compiler.h new file mode 100644 index 0000000000000..6c1f50b7a893f --- /dev/null +++ b/src/native/minipal/compiler.h @@ -0,0 +1,63 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#pragma once + +#ifndef __cplusplus +#include +#endif + +#if defined(TARGET_ANDROID) +#include +#include +#endif + +#ifndef __has_extension +#define __has_extension(...) 0 +#endif + +#if defined(TARGET_ANDROID) +static inline void +do_abort_unless (bool condition, const char* fmt, ...) +{ + if (condition) { + return; + } + + va_list ap; + + va_start (ap, fmt); + __android_log_vprint (ANDROID_LOG_FATAL, "DOTNET", fmt, ap); + va_end (ap); + + abort (); +} +#endif + +#define abort_unless(_condition_, _fmt_, ...) do_abort_unless (_condition_, "%s:%d (%s): " _fmt_, __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__) +#define abort_if_invalid_pointer_argument(_ptr_) abort_unless ((_ptr_) != NULL, "Parameter '%s' must be a valid pointer", #_ptr_) +#define abort_if_negative_integer_argument(_arg_) abort_unless ((_arg_) > 0, "Parameter '%s' must be larger than 0", #_arg_) + +#ifndef PALEXPORT +#ifdef TARGET_UNIX +#define PALEXPORT __attribute__ ((__visibility__ ("default"))) +#else +#define PALEXPORT +#endif +#endif // PALEXPORT + +#ifndef EXTERN_C +#ifdef __cplusplus +#define EXTERN_C extern "C" +#else +#define EXTERN_C extern +#endif // __cplusplus +#endif // EXTERN_C + +#ifndef TYPEOF +#ifdef __cplusplus +#define TYPEOF decltype +#else +#define TYPEOF __typeof +#endif // __cplusplus +#endif // TYPEOF diff --git a/src/native/minipal/random.c b/src/native/minipal/random.c index 7bacd10effdca..968966d21afe6 100644 --- a/src/native/minipal/random.c +++ b/src/native/minipal/random.c @@ -14,7 +14,7 @@ #include #endif -// #include "pal_config.h" +#include "config.h" #include "random.h" /* @@ -23,7 +23,7 @@ Generate random bytes. The generated bytes are not cryptographically strong. */ -void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength) +void minipal_get_non_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength) { assert(buffer != NULL); @@ -34,7 +34,7 @@ void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* buffer, int3 static bool sInitializedMRand; // Fall back to the secure version - SystemNative_GetCryptographicallySecureRandomBytes(buffer, bufferLength); + minipal_get_cryptographically_secure_random_bytes(buffer, bufferLength); if (!sInitializedMRand) { @@ -64,7 +64,7 @@ Generate cryptographically strong random bytes. Return 0 on success, -1 on failure. */ -int32_t SystemNative_GetCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength) +int32_t minipal_get_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength) { assert(buffer != NULL); diff --git a/src/native/minipal/random.h b/src/native/minipal/random.h index a68f03ba00b04..1cc1f2c350d90 100644 --- a/src/native/minipal/random.h +++ b/src/native/minipal/random.h @@ -3,14 +3,15 @@ #pragma once -#include +#include "compiler.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus -PALEXPORT void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength); -PALEXPORT int32_t SystemNative_GetCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength); +PALEXPORT void minipal_get_non_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength); +PALEXPORT int32_t minipal_get_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength); + #ifdef __cplusplus } From 4bfba04964dda4685244b0f9d9c59759453a9e6e Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 20 Jul 2023 06:27:06 -0700 Subject: [PATCH 4/9] FB --- .../System.Native/Interop.GetRandomBytes.cs | 4 +- src/native/libs/System.Native/CMakeLists.txt | 1 + src/native/libs/System.Native/entrypoints.c | 6 +- src/native/libs/System.Native/pal_random.c | 27 ++++++++ src/native/libs/System.Native/pal_random.h | 10 +++ src/native/minipal/compiler.h | 63 ------------------- src/native/minipal/random.h | 5 +- 7 files changed, 45 insertions(+), 71 deletions(-) create mode 100644 src/native/libs/System.Native/pal_random.c create mode 100644 src/native/libs/System.Native/pal_random.h delete mode 100644 src/native/minipal/compiler.h diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetRandomBytes.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetRandomBytes.cs index c325f4044838b..9d80ea4406860 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetRandomBytes.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetRandomBytes.cs @@ -8,10 +8,10 @@ internal static partial class Interop { internal static partial class Sys { - [LibraryImport(Interop.Libraries.SystemNative, EntryPoint = "minipal_get_non_cryptographically_secure_random_bytes")] + [LibraryImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetNonCryptographicallySecureRandomBytes")] internal static unsafe partial void GetNonCryptographicallySecureRandomBytes(byte* buffer, int length); - [LibraryImport(Interop.Libraries.SystemNative, EntryPoint = "minipal_get_cryptographically_secure_random_bytes")] + [LibraryImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetCryptographicallySecureRandomBytes")] internal static unsafe partial int GetCryptographicallySecureRandomBytes(byte* buffer, int length); } diff --git a/src/native/libs/System.Native/CMakeLists.txt b/src/native/libs/System.Native/CMakeLists.txt index 05e2d0b4ebc20..4ced2a87df8e9 100644 --- a/src/native/libs/System.Native/CMakeLists.txt +++ b/src/native/libs/System.Native/CMakeLists.txt @@ -30,6 +30,7 @@ set(NATIVE_SOURCES pal_maphardwaretype.c pal_memory.c pal_networkstatistics.c + pal_random.c pal_runtimeinformation.c pal_string.c pal_tcpstate.c diff --git a/src/native/libs/System.Native/entrypoints.c b/src/native/libs/System.Native/entrypoints.c index 8cab3483c10ef..394a39b0d0036 100644 --- a/src/native/libs/System.Native/entrypoints.c +++ b/src/native/libs/System.Native/entrypoints.c @@ -20,7 +20,7 @@ #include "pal_networking.h" #include "pal_networkstatistics.h" #include "pal_process.h" -#include +#include "pal_random.h" #include "pal_runtimeinformation.h" #include "pal_searchpath.h" #include "pal_signal.h" @@ -215,8 +215,8 @@ static const Entry s_sysNative[] = DllImportEntry(SystemNative_SchedSetAffinity) DllImportEntry(SystemNative_SchedGetAffinity) DllImportEntry(SystemNative_GetProcessPath) - DllImportEntry(minipal_get_non_cryptographically_secure_random_bytes) - DllImportEntry(minipal_get_cryptographically_secure_random_bytes) + DllImportEntry(SystemNative_GetNonCryptographicallySecureRandomBytes) + DllImportEntry(SystemNative_GetCryptographicallySecureRandomBytes) DllImportEntry(SystemNative_GetUnixRelease) DllImportEntry(SystemNative_GetUnixVersion) DllImportEntry(SystemNative_GetOSArchitecture) diff --git a/src/native/libs/System.Native/pal_random.c b/src/native/libs/System.Native/pal_random.c new file mode 100644 index 0000000000000..cf4abbeeeb395 --- /dev/null +++ b/src/native/libs/System.Native/pal_random.c @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include "pal_random.h" +#include + +/* + +Generate random bytes. The generated bytes are not cryptographically strong. + +*/ + +void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength) +{ + minipal_get_non_cryptographically_secure_random_bytes(buffer, bufferLength); +} + +/* + +Generate cryptographically strong random bytes. + +Return 0 on success, -1 on failure. +*/ +int32_t SystemNative_GetCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength) +{ + return minipal_get_cryptographically_secure_random_bytes(buffer, bufferLength); +} diff --git a/src/native/libs/System.Native/pal_random.h b/src/native/libs/System.Native/pal_random.h new file mode 100644 index 0000000000000..787b47bfd0686 --- /dev/null +++ b/src/native/libs/System.Native/pal_random.h @@ -0,0 +1,10 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#pragma once + +#include "pal_compiler.h" +#include "pal_types.h" + +PALEXPORT void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength); +PALEXPORT int32_t SystemNative_GetCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength); diff --git a/src/native/minipal/compiler.h b/src/native/minipal/compiler.h deleted file mode 100644 index 6c1f50b7a893f..0000000000000 --- a/src/native/minipal/compiler.h +++ /dev/null @@ -1,63 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#pragma once - -#ifndef __cplusplus -#include -#endif - -#if defined(TARGET_ANDROID) -#include -#include -#endif - -#ifndef __has_extension -#define __has_extension(...) 0 -#endif - -#if defined(TARGET_ANDROID) -static inline void -do_abort_unless (bool condition, const char* fmt, ...) -{ - if (condition) { - return; - } - - va_list ap; - - va_start (ap, fmt); - __android_log_vprint (ANDROID_LOG_FATAL, "DOTNET", fmt, ap); - va_end (ap); - - abort (); -} -#endif - -#define abort_unless(_condition_, _fmt_, ...) do_abort_unless (_condition_, "%s:%d (%s): " _fmt_, __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__) -#define abort_if_invalid_pointer_argument(_ptr_) abort_unless ((_ptr_) != NULL, "Parameter '%s' must be a valid pointer", #_ptr_) -#define abort_if_negative_integer_argument(_arg_) abort_unless ((_arg_) > 0, "Parameter '%s' must be larger than 0", #_arg_) - -#ifndef PALEXPORT -#ifdef TARGET_UNIX -#define PALEXPORT __attribute__ ((__visibility__ ("default"))) -#else -#define PALEXPORT -#endif -#endif // PALEXPORT - -#ifndef EXTERN_C -#ifdef __cplusplus -#define EXTERN_C extern "C" -#else -#define EXTERN_C extern -#endif // __cplusplus -#endif // EXTERN_C - -#ifndef TYPEOF -#ifdef __cplusplus -#define TYPEOF decltype -#else -#define TYPEOF __typeof -#endif // __cplusplus -#endif // TYPEOF diff --git a/src/native/minipal/random.h b/src/native/minipal/random.h index 1cc1f2c350d90..e9cd636105d02 100644 --- a/src/native/minipal/random.h +++ b/src/native/minipal/random.h @@ -3,14 +3,13 @@ #pragma once -#include "compiler.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus -PALEXPORT void minipal_get_non_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength); -PALEXPORT int32_t minipal_get_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength); +void minipal_get_non_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength); +int32_t minipal_get_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength); #ifdef __cplusplus From c158c5b4972b0f9ccacbae1455618a132604a3c6 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 20 Jul 2023 07:29:43 -0700 Subject: [PATCH 5/9] Apply suggestions from code review Co-authored-by: Jan Kotas --- src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 4 ---- src/native/libs/configure.cmake | 5 +---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index 9aceb2ee6cf04..609aa0e516088 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -17,10 +17,6 @@ #include #endif -#if defined(__APPLE__) && __APPLE__ -#include -#endif - #include // The regdisplay.h, StackFrameIterator.h, and thread.h includes are present only to access the Thread diff --git a/src/native/libs/configure.cmake b/src/native/libs/configure.cmake index 067b3b30ec4c9..d93e8d37e9f9c 100644 --- a/src/native/libs/configure.cmake +++ b/src/native/libs/configure.cmake @@ -1179,7 +1179,4 @@ check_c_source_compiles( configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Common/pal_config.h.in - ${CMAKE_CURRENT_BINARY_DIR}/Common/pal_config.h) -configure_file( - ${CMAKE_CURRENT_SOURCE_DIR}/Common/pal_config.h.in - ${CMAKE_CURRENT_BINARY_DIR}/Common/config.h) \ No newline at end of file + ${CMAKE_CURRENT_BINARY_DIR}/Common/pal_config.h) \ No newline at end of file From 092cd08362d9283fb47f62c0f23b3e1e32a223d9 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 20 Jul 2023 10:16:15 -0700 Subject: [PATCH 6/9] letting each library specify the config.h --- src/native/libs/configure.cmake | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/native/libs/configure.cmake b/src/native/libs/configure.cmake index d93e8d37e9f9c..675711f22faa4 100644 --- a/src/native/libs/configure.cmake +++ b/src/native/libs/configure.cmake @@ -1179,4 +1179,8 @@ check_c_source_compiles( configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Common/pal_config.h.in - ${CMAKE_CURRENT_BINARY_DIR}/Common/pal_config.h) \ No newline at end of file + ${CMAKE_CURRENT_BINARY_DIR}/Common/pal_config.h) + +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/Common/pal_config.h.in + ${CMAKE_CURRENT_BINARY_DIR}/Common/config.h) \ No newline at end of file From 473520f91983b0405d929c857eb39df5e4118037 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 20 Jul 2023 13:19:39 -0700 Subject: [PATCH 7/9] Add needed constants to minipalconfig --- src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt | 1 + src/native/libs/System.Native/CMakeLists.txt | 3 ++- src/native/libs/configure.cmake | 4 ---- src/native/minipal/minipalconfig.h.in | 2 ++ src/native/minipal/random.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt b/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt index c396294a640a7..930ff5cf9f086 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt +++ b/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt @@ -39,6 +39,7 @@ if(CLR_CMAKE_HOST_UNIX) ds-ipc-pal-socket.h ) + include(${CLR_SRC_NATIVE_DIR}/minipal/configure.cmake) list(APPEND MINIPAL_SOURCES random.c ) diff --git a/src/native/libs/System.Native/CMakeLists.txt b/src/native/libs/System.Native/CMakeLists.txt index 4ced2a87df8e9..35fa5693e9fce 100644 --- a/src/native/libs/System.Native/CMakeLists.txt +++ b/src/native/libs/System.Native/CMakeLists.txt @@ -17,8 +17,9 @@ endif () set (MINIPAL_SOURCES "") set (SHARED_MINIPAL_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/minipal") +include(${CLR_SRC_NATIVE_DIR}/minipal/configure.cmake) list(APPEND MINIPAL_SOURCES -random.c + random.c ) addprefix(MINIPAL_SOURCES ${SHARED_MINIPAL_SOURCE_PATH} "${MINIPAL_SOURCES}") diff --git a/src/native/libs/configure.cmake b/src/native/libs/configure.cmake index 675711f22faa4..cbaac692f2630 100644 --- a/src/native/libs/configure.cmake +++ b/src/native/libs/configure.cmake @@ -1180,7 +1180,3 @@ check_c_source_compiles( configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Common/pal_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/Common/pal_config.h) - -configure_file( - ${CMAKE_CURRENT_SOURCE_DIR}/Common/pal_config.h.in - ${CMAKE_CURRENT_BINARY_DIR}/Common/config.h) \ No newline at end of file diff --git a/src/native/minipal/minipalconfig.h.in b/src/native/minipal/minipalconfig.h.in index b84247632ab00..b1d7261eb4560 100644 --- a/src/native/minipal/minipalconfig.h.in +++ b/src/native/minipal/minipalconfig.h.in @@ -1,7 +1,9 @@ #ifndef HAVE_MINIPAL_MINIPALCONFIG_H #define HAVE_MINIPAL_MINIPALCONFIG_H +#cmakedefine01 HAVE_ARC4RANDOM_BUF #cmakedefine01 HAVE_AUXV_HWCAP_H +#cmakedefine01 HAVE_O_CLOEXEC #cmakedefine01 HAVE_SYSCTLBYNAME #endif diff --git a/src/native/minipal/random.c b/src/native/minipal/random.c index 968966d21afe6..2a0f57d53163a 100644 --- a/src/native/minipal/random.c +++ b/src/native/minipal/random.c @@ -14,7 +14,7 @@ #include #endif -#include "config.h" +#include "minipalconfig.h" #include "random.h" /* From 8b364f12e42863f3b26fc9e1bff3c078f7bf4ad7 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 21 Jul 2023 05:38:39 -0700 Subject: [PATCH 8/9] triggering random constants in configure.cmake --- src/native/libs/Common/pal_config.h.in | 1 - src/native/libs/configure.cmake | 5 ----- src/native/minipal/configure.cmake | 3 +++ 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/native/libs/Common/pal_config.h.in b/src/native/libs/Common/pal_config.h.in index a5ce0ce2a46d4..42837a9a557d0 100644 --- a/src/native/libs/Common/pal_config.h.in +++ b/src/native/libs/Common/pal_config.h.in @@ -51,7 +51,6 @@ #cmakedefine01 HAVE_SCHED_GETCPU #cmakedefine01 HAVE_PTHREAD_SETCANCELSTATE #cmakedefine01 HAVE_GNU_LIBNAMES_H -#cmakedefine01 HAVE_ARC4RANDOM_BUF #cmakedefine01 KEVENT_HAS_VOID_UDATA #cmakedefine01 HAVE_FDS_BITS #cmakedefine01 HAVE_PRIVATE_FDS_BITS diff --git a/src/native/libs/configure.cmake b/src/native/libs/configure.cmake index cbaac692f2630..a1e151de76d95 100644 --- a/src/native/libs/configure.cmake +++ b/src/native/libs/configure.cmake @@ -254,11 +254,6 @@ check_include_files( gnu/lib-names.h HAVE_GNU_LIBNAMES_H) -check_symbol_exists( - arc4random_buf - "stdlib.h" - HAVE_ARC4RANDOM_BUF) - check_symbol_exists( TIOCGWINSZ "sys/ioctl.h" diff --git a/src/native/minipal/configure.cmake b/src/native/minipal/configure.cmake index c7b7715c1d78a..cb4cd1abbbd30 100644 --- a/src/native/minipal/configure.cmake +++ b/src/native/minipal/configure.cmake @@ -4,4 +4,7 @@ include(CheckIncludeFiles) check_include_files("sys/auxv.h;asm/hwcap.h" HAVE_AUXV_HWCAP_H) check_function_exists(sysctlbyname HAVE_SYSCTLBYNAME) +check_symbol_exists(arc4random_buf "stdlib.h" HAVE_ARC4RANDOM_BUF) +check_symbol_exists(O_CLOEXEC fcntl.h HAVE_O_CLOEXEC) + configure_file(${CMAKE_CURRENT_LIST_DIR}/minipalconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/minipalconfig.h) From 06e192de75383efc44428e5d629c7ac41de7dbf6 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Sat, 22 Jul 2023 03:59:54 -0700 Subject: [PATCH 9/9] FB --- .../nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 4 ---- src/native/minipal/configure.cmake | 1 + src/native/minipal/random.h | 13 ++++++++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index 1e00e9b4126ed..617f146e74325 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -19,10 +19,6 @@ #include -// The regdisplay.h, StackFrameIterator.h, and thread.h includes are present only to access the Thread -// class and can be removed if it turns out that the required ep_rt_thread_handle_t can be -// implemented in some manner that doesn't rely on the Thread class. - #include "gcenv.h" #ifndef DIRECTORY_SEPARATOR_CHAR diff --git a/src/native/minipal/configure.cmake b/src/native/minipal/configure.cmake index cb4cd1abbbd30..97153932a7b87 100644 --- a/src/native/minipal/configure.cmake +++ b/src/native/minipal/configure.cmake @@ -1,5 +1,6 @@ include(CheckFunctionExists) include(CheckIncludeFiles) +include(CheckSymbolExists) check_include_files("sys/auxv.h;asm/hwcap.h" HAVE_AUXV_HWCAP_H) check_function_exists(sysctlbyname HAVE_SYSCTLBYNAME) diff --git a/src/native/minipal/random.h b/src/native/minipal/random.h index e9cd636105d02..4296fd0a5742b 100644 --- a/src/native/minipal/random.h +++ b/src/native/minipal/random.h @@ -1,17 +1,28 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma once +#ifndef HAVE_MINIPAL_RANDOM_H +#define HAVE_MINIPAL_RANDOM_H #ifdef __cplusplus extern "C" { #endif // __cplusplus +/** + * Generate random bytes. The generated bytes are not cryptographically strong. + */ void minipal_get_non_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength); + +/** + * Generate cryptographically strong random bytes. + * + * Return 0 on success, -1 on failure. + */ int32_t minipal_get_cryptographically_secure_random_bytes(uint8_t* buffer, int32_t bufferLength); #ifdef __cplusplus } #endif // __cplusplus +#endif /* HAVE_MINIPAL_RANDOM_H */