Skip to content

Commit 254263f

Browse files
LakshanFjkotas
andauthored
Generates a guid for activity id (#88838)
* Fixes the way activity id is generated * FB * FB * FB * Apply suggestions from code review Co-authored-by: Jan Kotas <jkotas@microsoft.com> * letting each library specify the config.h * Add needed constants to minipalconfig * triggering random constants in configure.cmake * FB --------- Co-authored-by: Jan Kotas <jkotas@microsoft.com>
1 parent 0f9daf0 commit 254263f

File tree

12 files changed

+273
-218
lines changed

12 files changed

+273
-218
lines changed

src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt

+10-4
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,28 @@ list(APPEND MINIPAL_SOURCES
2626

2727
if(CLR_CMAKE_HOST_WIN32)
2828
list(APPEND SHARED_DIAGNOSTIC_SERVER_SOURCES
29-
ds-ipc-pal-namedpipe.c
29+
ds-ipc-pal-namedpipe.c
3030
)
3131

3232
list(APPEND SHARED_DIAGNOSTIC_SERVER_HEADERS
33-
ds-ipc-pal-namedpipe.h
33+
ds-ipc-pal-namedpipe.h
3434
)
3535
endif(CLR_CMAKE_HOST_WIN32)
3636

3737
if(CLR_CMAKE_HOST_UNIX)
3838
list(APPEND SHARED_DIAGNOSTIC_SERVER_SOURCES
39-
ds-ipc-pal-socket.c
39+
ds-ipc-pal-socket.c
4040
)
4141

4242
list(APPEND SHARED_DIAGNOSTIC_SERVER_HEADERS
43-
ds-ipc-pal-socket.h
43+
ds-ipc-pal-socket.h
4444
)
45+
46+
include(${CLR_SRC_NATIVE_DIR}/minipal/configure.cmake)
47+
list(APPEND MINIPAL_SOURCES
48+
random.c
49+
)
50+
4551
endif(CLR_CMAKE_HOST_UNIX)
4652

4753
list(APPEND EVENTPIPE_SOURCES

src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,8 @@ ds_rt_generate_core_dump (
181181
{
182182
STATIC_CONTRACT_NOTHROW;
183183

184-
ds_ipc_result_t result = DS_IPC_E_FAIL;
185-
uint32_t flags = ds_generate_core_dump_command_payload_get_flags(payload);
186-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
187-
// TODO: Generate an exception dump
188-
// PalDebugBreak();
189-
190-
return 0;
184+
// Eventpipe driven core_dump is not currently supported in NativeAOT
185+
return DS_IPC_E_NOTSUPPORTED;
191186
}
192187

193188
/*

src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp

+44-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <unistd.h>
1818
#endif
1919

20+
#include <minipal/random.h>
21+
2022
#include "gcenv.h"
2123

2224
#ifndef DIRECTORY_SEPARATOR_CHAR
@@ -237,8 +239,6 @@ ep_rt_aot_atomic_inc_int64_t (volatile int64_t *value)
237239
{
238240
STATIC_CONTRACT_NOTHROW;
239241

240-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
241-
// TODO: Consider replacing with a new PalInterlockedIncrement64 service
242242
int64_t currentValue;
243243
do {
244244
currentValue = *value;
@@ -252,8 +252,6 @@ int64_t
252252
ep_rt_aot_atomic_dec_int64_t (volatile int64_t *value) {
253253
STATIC_CONTRACT_NOTHROW;
254254

255-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
256-
// TODO: Consider replacing with a new PalInterlockedDecrement64 service
257255
int64_t currentValue;
258256
do {
259257
currentValue = *value;
@@ -366,8 +364,7 @@ ep_rt_aot_thread_create (
366364
STATIC_CONTRACT_NOTHROW;
367365
EP_ASSERT (thread_func != NULL);
368366

369-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
370-
// TODO: Fill in the outgoing id if any callers ever need it
367+
// Note that none of the callers examine the return id in any way
371368
if (id)
372369
*reinterpret_cast<DWORD*>(id) = 0xffffffff;
373370

@@ -789,6 +786,47 @@ void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array)
789786
#endif
790787
}
791788

789+
void ep_rt_aot_create_activity_id (uint8_t *activity_id, uint32_t activity_id_len)
790+
{
791+
// We call CoCreateGuid for windows, and use a random generator for non-windows
792+
STATIC_CONTRACT_NOTHROW;
793+
EP_ASSERT (activity_id != NULL);
794+
EP_ASSERT (activity_id_len == EP_ACTIVITY_ID_SIZE);
795+
#ifdef HOST_WIN32
796+
CoCreateGuid (reinterpret_cast<GUID *>(activity_id));
797+
#else
798+
if(minipal_get_cryptographically_secure_random_bytes(activity_id, activity_id_len)==-1)
799+
{
800+
*activity_id=0;
801+
return;
802+
}
803+
804+
const uint16_t version_mask = 0xF000;
805+
const uint16_t random_guid_version = 0x4000;
806+
const uint8_t clock_seq_hi_and_reserved_mask = 0xC0;
807+
const uint8_t clock_seq_hi_and_reserved_value = 0x80;
808+
809+
// Modify bits indicating the type of the GUID
810+
uint8_t *activity_id_c = activity_id + sizeof (uint32_t) + sizeof (uint16_t);
811+
uint8_t *activity_id_d = activity_id + sizeof (uint32_t) + sizeof (uint16_t) + sizeof (uint16_t);
812+
813+
uint16_t c;
814+
memcpy (&c, activity_id_c, sizeof (c));
815+
816+
uint8_t d;
817+
memcpy (&d, activity_id_d, sizeof (d));
818+
819+
// time_hi_and_version
820+
c = ((c & ~version_mask) | random_guid_version);
821+
// clock_seq_hi_and_reserved
822+
d = ((d & ~clock_seq_hi_and_reserved_mask) | clock_seq_hi_and_reserved_value);
823+
824+
memcpy (activity_id_c, &c, sizeof (c));
825+
memcpy (activity_id_d, &d, sizeof (d));
826+
#endif
827+
}
828+
829+
792830
#ifdef EP_CHECKED_BUILD
793831

794832
void ep_rt_aot_lock_requires_lock_held (const ep_rt_lock_handle_t *lock)

src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h

+13-66
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454

5555
extern void ep_rt_aot_thread_exited (void);
5656

57-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
58-
// TODO: The NativeAOT ALIGN_UP is defined in a tangled manner that generates linker errors if
57+
// The NativeAOT ALIGN_UP is defined in a tangled manner that generates linker errors if
5958
// it is used here; instead, define a version tailored to the existing usage in the shared
6059
// EventPipe code.
6160
static inline uint8_t* _rt_aot_align_up(uint8_t* val, uintptr_t alignment)
@@ -324,10 +323,7 @@ ep_rt_method_get_simple_assembly_name (
324323
{
325324
STATIC_CONTRACT_NOTHROW;
326325

327-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
328-
// TODO: Design MethodDesc and method name services if/when needed
329-
//PalDebugBreak();
330-
326+
// NativeAOT does not support method_desc operations
331327
return false;
332328

333329
}
@@ -339,10 +335,7 @@ ep_rt_method_get_full_name (
339335
ep_char8_t *name,
340336
size_t name_len)
341337
{
342-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
343-
// TODO: Design MethodDesc and method name services if/when needed
344-
//PalDebugBreak();
345-
338+
// NativeAOT does not support method_desc operations
346339
return false;
347340
}
348341

@@ -613,11 +606,9 @@ EventPipeWaitHandle
613606
ep_rt_wait_event_get_wait_handle (ep_rt_wait_event_handle_t *wait_event)
614607
{
615608
STATIC_CONTRACT_NOTHROW;
616-
// EP_ASSERT (wait_event != NULL && wait_event->event != NULL);
617609

618-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
619-
// TODO: NativeAOT CLREventStatic doesn't have GetHandleUNHOSTED
620-
// PalDebugBreak();
610+
// This is not reached in the current product
611+
abort();
621612
return 0;
622613
}
623614

@@ -675,41 +666,8 @@ ep_rt_create_activity_id (
675666
uint8_t *activity_id,
676667
uint32_t activity_id_len)
677668
{
678-
STATIC_CONTRACT_NOTHROW;
679-
EP_ASSERT (activity_id != NULL);
680-
EP_ASSERT (activity_id_len == EP_ACTIVITY_ID_SIZE);
681-
682-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
683-
// TODO: Implement a way to generate a real Guid
684-
// CoCreateGuid (reinterpret_cast<GUID *>(activity_id));
685-
686-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
687-
// TODO: Using roughly Mono's implementation but Mono randomly generates this, hardcoding for now
688-
uint8_t data1[] = {0x67,0xac,0x33,0xf1,0x8d,0xed,0x41,0x01,0xb4,0x26,0xc9,0xb7,0x94,0x35,0xf7,0x8a};
689-
memcpy (activity_id, data1, EP_ACTIVITY_ID_SIZE);
690-
691-
const uint16_t version_mask = 0xF000;
692-
const uint16_t random_guid_version = 0x4000;
693-
const uint8_t clock_seq_hi_and_reserved_mask = 0xC0;
694-
const uint8_t clock_seq_hi_and_reserved_value = 0x80;
695-
696-
// Modify bits indicating the type of the GUID
697-
uint8_t *activity_id_c = activity_id + sizeof (uint32_t) + sizeof (uint16_t);
698-
uint8_t *activity_id_d = activity_id + sizeof (uint32_t) + sizeof (uint16_t) + sizeof (uint16_t);
699-
700-
uint16_t c;
701-
memcpy (&c, activity_id_c, sizeof (c));
702-
703-
uint8_t d;
704-
memcpy (&d, activity_id_d, sizeof (d));
705-
706-
// time_hi_and_version
707-
c = ((c & ~version_mask) | random_guid_version);
708-
// clock_seq_hi_and_reserved
709-
d = ((d & ~clock_seq_hi_and_reserved_mask) | clock_seq_hi_and_reserved_value);
710-
711-
memcpy (activity_id_c, &c, sizeof (c));
712-
memcpy (activity_id_d, &d, sizeof (d));
669+
extern void ep_rt_aot_create_activity_id (uint8_t *activity_id, uint32_t activity_id_len);
670+
ep_rt_aot_create_activity_id(activity_id, activity_id_len);
713671
}
714672

715673
static
@@ -718,9 +676,9 @@ bool
718676
ep_rt_is_running (void)
719677
{
720678
STATIC_CONTRACT_NOTHROW;
721-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
722-
// TODO: Does NativeAot have the concept of EEStarted
723-
// PalDebugBreak();
679+
680+
// This is only used to check if the profiler can be attached
681+
// Profiler attach is not supported in NativeAOT
724682

725683
return false;
726684
}
@@ -732,11 +690,7 @@ ep_rt_execute_rundown (dn_vector_ptr_t *execution_checkpoints)
732690
{
733691
STATIC_CONTRACT_NOTHROW;
734692

735-
//TODO: Write execution checkpoint rundown events.
736-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
737-
// TODO: EventPipe Configuration values - RhConfig?
738-
// (CLRConfig::INTERNAL_EventPipeCircularMB)
739-
// PalDebugBreak();
693+
// NativeAOT does not currently support rundown
740694
}
741695

742696
/*
@@ -772,8 +726,6 @@ EP_RT_DEFINE_THREAD_FUNC (ep_rt_thread_aot_start_session_or_sampling_thread)
772726

773727
ep_rt_thread_params_t* thread_params = reinterpret_cast<ep_rt_thread_params_t *>(data);
774728

775-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
776-
// TODO: Implement thread creation/management if needed.
777729
// The session and sampling threads both assert that the incoming thread handle is
778730
// non-null, but do not necessarily rely on it otherwise; just pass a meaningless non-null
779731
// value until testing shows that a meaningful value is needed.
@@ -808,9 +760,7 @@ inline
808760
void
809761
ep_rt_set_server_name(void)
810762
{
811-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
812-
// TODO: Need to set name for the thread
813-
// ::SetThreadName(GetCurrentThread(), W(".NET EventPipe"));
763+
// This is optional, decorates the thread name with EventPipe specific information
814764
}
815765

816766

@@ -1569,10 +1519,7 @@ ep_rt_thread_setup (void)
15691519
{
15701520
STATIC_CONTRACT_NOTHROW;
15711521

1572-
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
1573-
// TODO: Implement thread creation/management if needed
1574-
// Thread* thread_handle = SetupThreadNoThrow ();
1575-
// EP_ASSERT (thread_handle != NULL);
1522+
// Likely not needed and do nothing until testing shows to be required
15761523
}
15771524

15781525
static

src/native/libs/Common/pal_config.h.in

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#cmakedefine01 HAVE_SCHED_GETCPU
5252
#cmakedefine01 HAVE_PTHREAD_SETCANCELSTATE
5353
#cmakedefine01 HAVE_GNU_LIBNAMES_H
54-
#cmakedefine01 HAVE_ARC4RANDOM_BUF
5554
#cmakedefine01 KEVENT_HAS_VOID_UDATA
5655
#cmakedefine01 HAVE_FDS_BITS
5756
#cmakedefine01 HAVE_PRIVATE_FDS_BITS

src/native/libs/System.Native/CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ if (CLR_CMAKE_TARGET_OSX)
1414
add_definitions(-D_DARWIN_C_SOURCE)
1515
endif ()
1616

17+
set (MINIPAL_SOURCES "")
18+
set (SHARED_MINIPAL_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/minipal")
19+
20+
include(${CLR_SRC_NATIVE_DIR}/minipal/configure.cmake)
21+
list(APPEND MINIPAL_SOURCES
22+
random.c
23+
)
24+
25+
addprefix(MINIPAL_SOURCES ${SHARED_MINIPAL_SOURCE_PATH} "${MINIPAL_SOURCES}")
26+
1727
set(NATIVE_SOURCES
1828
pal_errno.c
1929
pal_interfaceaddresses.c
@@ -30,6 +40,10 @@ set(NATIVE_SOURCES
3040
pal_sysctl.c
3141
)
3242

43+
list(APPEND NATIVE_SOURCES
44+
${MINIPAL_SOURCES}
45+
)
46+
3347
if (NOT CLR_CMAKE_TARGET_WASI)
3448
list (APPEND NATIVE_SOURCES
3549
pal_dynamicload.c

0 commit comments

Comments
 (0)