From 1b27ee9197251edcc9167a48b87786d3a6b5c163 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 30 Oct 2024 19:22:54 -0700 Subject: [PATCH] Remove PAL_Random and move palrt APIs into the minipal (#108999) --- src/coreclr/CMakeLists.txt | 4 - src/coreclr/debug/createdump/crashinfo.cpp | 2 +- src/coreclr/debug/createdump/createdump.h | 1 + .../debug/createdump/createdumppal.cpp | 8 - src/coreclr/debug/createdump/datatarget.cpp | 2 +- src/coreclr/debug/inc/dbgtransportsession.h | 6 +- .../debug/shared/dbgtransportsession.cpp | 5 +- src/coreclr/dlls/mscordac/CMakeLists.txt | 1 - .../dlls/mscordac/mscordac_unixexports.src | 3 +- src/coreclr/dlls/mscordbi/CMakeLists.txt | 1 - .../dlls/mscoree/coreclr/CMakeLists.txt | 1 - src/coreclr/dlls/mscorpe/pewriter.cpp | 4 +- src/coreclr/ilasm/CMakeLists.txt | 2 - src/coreclr/ilasm/assem.cpp | 9 - src/coreclr/ilasm/portable_pdb.cpp | 10 +- src/coreclr/ildasm/exe/CMakeLists.txt | 2 - src/coreclr/inc/holder.h | 1 + src/coreclr/md/compiler/regmeta.cpp | 3 +- src/coreclr/md/enc/metamodelrw.cpp | 3 +- src/coreclr/md/inc/portablepdbmdds.h | 1 + src/coreclr/minipal/Unix/dn-u16.cpp | 8 +- .../nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 40 -- .../nativeaot/Runtime/eventpipe/ep-rt-aot.h | 11 - .../nativeaot/Runtime/eventpipeinternal.cpp | 4 +- src/coreclr/pal/inc/pal.h | 7 - src/coreclr/pal/inc/rt/palrt.h | 31 -- src/coreclr/pal/src/CMakeLists.txt | 8 +- src/coreclr/pal/src/com/guid.cpp | 18 + src/coreclr/pal/src/locale/unicode.cpp | 3 +- src/coreclr/pal/src/misc/miscpalapi.cpp | 131 ------ src/coreclr/palrt/CMakeLists.txt | 15 - src/coreclr/palrt/comem.cpp | 21 - src/coreclr/palrt/common.h | 17 - src/coreclr/palrt/guid.cpp | 47 --- src/coreclr/tools/superpmi/mcs/CMakeLists.txt | 1 - .../superpmi/superpmi-shared/spmiutil.cpp | 10 +- .../superpmi-shim-collector/CMakeLists.txt | 1 - .../superpmi-shim-counter/CMakeLists.txt | 1 - .../superpmi-shim-simple/CMakeLists.txt | 1 - .../tools/superpmi/superpmi/CMakeLists.txt | 1 - .../superpmi/superpmi/parallelsuperpmi.cpp | 7 +- .../vm/eventing/eventpipe/ep-rt-coreclr.h | 18 +- src/coreclr/vm/eventpipeinternal.cpp | 6 +- src/coreclr/vm/olevariant.cpp | 2 +- src/mono/CMakeLists.txt | 2 +- src/mono/mono/eglib/giconv.c | 17 +- src/mono/mono/eventpipe/ep-rt-mono.c | 17 - src/mono/mono/eventpipe/ep-rt-mono.h | 38 -- src/mono/mono/mini/aot-compiler.c | 14 +- src/mono/mono/utils/CMakeLists.txt | 3 - src/mono/mono/utils/mono-rand-windows.c | 101 ----- src/mono/mono/utils/mono-rand.c | 380 ------------------ src/mono/mono/utils/mono-rand.h | 32 -- .../corehost/apphost/static/CMakeLists.txt | 1 - src/native/eventpipe/ds-protocol.c | 2 +- src/native/eventpipe/ep-rt.h | 6 - src/native/eventpipe/ep-thread.h | 5 +- src/native/eventpipe/ep-types.h | 1 + src/native/minipal/CMakeLists.txt | 12 +- src/native/minipal/com/memory.h | 30 ++ src/native/minipal/guid.c | 62 +++ src/native/minipal/guid.h | 50 +++ src/native/minipal/strings.c | 22 + src/native/minipal/strings.h | 8 + 64 files changed, 267 insertions(+), 1014 deletions(-) create mode 100644 src/coreclr/pal/src/com/guid.cpp delete mode 100644 src/coreclr/pal/src/misc/miscpalapi.cpp delete mode 100644 src/coreclr/palrt/CMakeLists.txt delete mode 100644 src/coreclr/palrt/comem.cpp delete mode 100644 src/coreclr/palrt/common.h delete mode 100644 src/coreclr/palrt/guid.cpp delete mode 100644 src/mono/mono/utils/mono-rand-windows.c delete mode 100644 src/mono/mono/utils/mono-rand.c delete mode 100644 src/mono/mono/utils/mono-rand.h create mode 100644 src/native/minipal/com/memory.h create mode 100644 src/native/minipal/guid.c create mode 100644 src/native/minipal/guid.h create mode 100644 src/native/minipal/strings.c diff --git a/src/coreclr/CMakeLists.txt b/src/coreclr/CMakeLists.txt index 732c3b765379e..107859ec58dc5 100644 --- a/src/coreclr/CMakeLists.txt +++ b/src/coreclr/CMakeLists.txt @@ -250,10 +250,6 @@ if(NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_H add_subdirectory(utilcode) add_subdirectory(inc) - if(CLR_CMAKE_HOST_UNIX) - add_subdirectory(palrt) - endif(CLR_CMAKE_HOST_UNIX) - add_subdirectory(ilasm) add_subdirectory(ildasm) add_subdirectory(gcinfo) diff --git a/src/coreclr/debug/createdump/crashinfo.cpp b/src/coreclr/debug/createdump/crashinfo.cpp index 4026903f289e0..8958cfc05c54f 100644 --- a/src/coreclr/debug/createdump/crashinfo.cpp +++ b/src/coreclr/debug/createdump/crashinfo.cpp @@ -1026,7 +1026,7 @@ ConvertString(const WCHAR* str) if (str == nullptr) return { }; - size_t cch = u16_strlen(str) + 1; + size_t cch = minipal_u16_strlen((CHAR16_T*)str) + 1; int len = minipal_get_length_utf16_to_utf8((CHAR16_T*)str, cch, 0); if (len == 0) return { }; diff --git a/src/coreclr/debug/createdump/createdump.h b/src/coreclr/debug/createdump/createdump.h index 804575c10fed7..551e3da4c9984 100644 --- a/src/coreclr/debug/createdump/createdump.h +++ b/src/coreclr/debug/createdump/createdump.h @@ -51,6 +51,7 @@ typedef int T_CONTEXT; #include #include #ifdef HOST_UNIX +#include #include #include #include diff --git a/src/coreclr/debug/createdump/createdumppal.cpp b/src/coreclr/debug/createdump/createdumppal.cpp index 03b06a84a4613..88e57ef7e8541 100644 --- a/src/coreclr/debug/createdump/createdumppal.cpp +++ b/src/coreclr/debug/createdump/createdumppal.cpp @@ -217,14 +217,6 @@ RaiseException( throw; } -size_t u16_strlen(const WCHAR* str) -{ - size_t nChar = 0; - while (*str++) - nChar++; - return nChar; -} - // // Used by _ASSERTE // diff --git a/src/coreclr/debug/createdump/datatarget.cpp b/src/coreclr/debug/createdump/datatarget.cpp index 243d8458bda53..7867967e23777 100644 --- a/src/coreclr/debug/createdump/datatarget.cpp +++ b/src/coreclr/debug/createdump/datatarget.cpp @@ -102,7 +102,7 @@ DumpDataTarget::GetImageBase( *baseAddress = 0; char tempModuleName[MAX_PATH]; - size_t cch = u16_strlen(moduleName) + 1; + size_t cch = minipal_u16_strlen((CHAR16_T*)moduleName) + 1; int length = minipal_convert_utf16_to_utf8((CHAR16_T*)moduleName, cch, tempModuleName, sizeof(tempModuleName), 0); if (length > 0) { diff --git a/src/coreclr/debug/inc/dbgtransportsession.h b/src/coreclr/debug/inc/dbgtransportsession.h index 528590e21d51c..7a5df64d41eec 100644 --- a/src/coreclr/debug/inc/dbgtransportsession.h +++ b/src/coreclr/debug/inc/dbgtransportsession.h @@ -11,6 +11,8 @@ #endif // !RIGHT_SIDE_COMPILE +#include + #if defined(FEATURE_DBGIPC_TRANSPORT_VM) || defined(FEATURE_DBGIPC_TRANSPORT_DI) #include @@ -519,7 +521,7 @@ class DbgTransportSession // Struct defining the format of the data block sent with a SessionRequest. struct SessionRequestData { - GUID m_sSessionID; // Unique session ID. Treated as byte blob so no endian-ness + minipal_guid_t m_sSessionID; // Unique session ID. Treated as byte blob so no endian-ness }; // Struct used to track a message that is being (or will soon be) sent but has not yet been acknowledged. @@ -674,7 +676,7 @@ class DbgTransportSession // Session ID randomly allocated by the right side and sent over in the SessionRequest message. This // serves to disambiguate a re-send of the SessionRequest due to a network error versus a SessionRequest // from a different debugger. - GUID m_sSessionID; + minipal_guid_t m_sSessionID; // Lock used to synchronize sending messages and updating the session state. This ensures message bytes // don't become interleaved on the transport connection, the send queue is updated consistently across diff --git a/src/coreclr/debug/shared/dbgtransportsession.cpp b/src/coreclr/debug/shared/dbgtransportsession.cpp index 74692f40e85f7..2df265f22b869 100644 --- a/src/coreclr/debug/shared/dbgtransportsession.cpp +++ b/src/coreclr/debug/shared/dbgtransportsession.cpp @@ -89,9 +89,8 @@ HRESULT DbgTransportSession::Init(DebuggerIPCControlBlock *pDCB, AppDomainEnumer // The RS randomly allocates a session ID which is sent to the LS in the SessionRequest message. In the // case of network errors during session formation this allows the LS to tell SessionRequest re-sends from // a new request from a different RS. - HRESULT hr = CoCreateGuid(&m_sSessionID); - if (FAILED(hr)) - return hr; + if (!minipal_guid_v4_create(&m_sSessionID)) + return E_FAIL; #endif // RIGHT_SIDE_COMPILE diff --git a/src/coreclr/dlls/mscordac/CMakeLists.txt b/src/coreclr/dlls/mscordac/CMakeLists.txt index f031e3fc4b60f..ef91243448c51 100644 --- a/src/coreclr/dlls/mscordac/CMakeLists.txt +++ b/src/coreclr/dlls/mscordac/CMakeLists.txt @@ -175,7 +175,6 @@ else(CLR_CMAKE_HOST_WIN32) mscorrc ${START_WHOLE_ARCHIVE} # force all PAL objects to be included so all exports are available coreclrpal - palrt coreclrminipal ${END_WHOLE_ARCHIVE} ) diff --git a/src/coreclr/dlls/mscordac/mscordac_unixexports.src b/src/coreclr/dlls/mscordac/mscordac_unixexports.src index b7fe8d95f5b79..05bfd886a7552 100644 --- a/src/coreclr/dlls/mscordac/mscordac_unixexports.src +++ b/src/coreclr/dlls/mscordac/mscordac_unixexports.src @@ -40,7 +40,6 @@ nativeStringResourceTable_mscorrc #PAL_CloseProcessMemory #PAL_ReadProcessMemory #PAL_ProbeMemory -#PAL_Random #PAL__wcstoui64 #PAL_wcstoul #PAL_wcstod @@ -147,3 +146,5 @@ nativeStringResourceTable_mscorrc #WaitForSingleObjectEx #WideCharToMultiByte #WriteFile + +#GUID_NULL diff --git a/src/coreclr/dlls/mscordbi/CMakeLists.txt b/src/coreclr/dlls/mscordbi/CMakeLists.txt index fce244c50d717..a92f844b0f9d9 100644 --- a/src/coreclr/dlls/mscordbi/CMakeLists.txt +++ b/src/coreclr/dlls/mscordbi/CMakeLists.txt @@ -96,7 +96,6 @@ if(CLR_CMAKE_HOST_WIN32) elseif(CLR_CMAKE_HOST_UNIX) list(APPEND COREDBI_LIBRARIES - palrt # share the PAL in the dac module mscordaccore ) diff --git a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt index 7e941521bd51b..6dd42fe2b2538 100644 --- a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt +++ b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt @@ -140,7 +140,6 @@ else() coreclrpal ${END_WHOLE_ARCHIVE} mscorrc - palrt ) endif(CLR_CMAKE_TARGET_WIN32) diff --git a/src/coreclr/dlls/mscorpe/pewriter.cpp b/src/coreclr/dlls/mscorpe/pewriter.cpp index b43801c9f5a5d..efbf147e55171 100644 --- a/src/coreclr/dlls/mscorpe/pewriter.cpp +++ b/src/coreclr/dlls/mscorpe/pewriter.cpp @@ -1589,7 +1589,7 @@ HRESULT PEWriter::write(void ** ppImage) size_t lSize = filePos; // allocate the block we are handing back to the caller - void * pImage = (void *) ::CoTaskMemAlloc(lSize); + void * pImage = (void *) CoTaskMemAlloc(lSize); if (NULL == pImage) { return E_OUTOFMEMORY; @@ -1622,7 +1622,7 @@ HRESULT PEWriter::write(void ** ppImage) // make sure we wrote the exact numbmer of bytes expected _ASSERTE(lSize == (size_t) (pCur - (char *)pImage)); - // give pointer to memory image back to caller (who must free with ::CoTaskMemFree()) + // give pointer to memory image back to caller (who must free with CoTaskMemFree()) *ppImage = pImage; // all done diff --git a/src/coreclr/ilasm/CMakeLists.txt b/src/coreclr/ilasm/CMakeLists.txt index cca2c6da1858e..818171c25c749 100644 --- a/src/coreclr/ilasm/CMakeLists.txt +++ b/src/coreclr/ilasm/CMakeLists.txt @@ -103,7 +103,6 @@ else() list(APPEND ILASM_LINK_LIBRARIES coreclrpal mscorrc - palrt coreclrminipal ) endif(CLR_CMAKE_TARGET_WIN32) @@ -116,7 +115,6 @@ if(CLR_CMAKE_HOST_UNIX) utilcodestaticnohost mscorrc coreclrpal - palrt ${CMAKE_DL_LIBS} ) else() diff --git a/src/coreclr/ilasm/assem.cpp b/src/coreclr/ilasm/assem.cpp index 2cbf02c0eab07..0100feef79a12 100644 --- a/src/coreclr/ilasm/assem.cpp +++ b/src/coreclr/ilasm/assem.cpp @@ -1064,20 +1064,11 @@ BOOL Assembler::EmitClass(Class *pClass) LPCUTF8 szFullName; WCHAR* wzFullName=&wzUniBuf[0]; HRESULT hr = E_FAIL; - GUID guid; size_t L; mdToken tok; if(pClass == NULL) return FALSE; - hr = CoCreateGuid(&guid); - if (FAILED(hr)) - { - printf("Unable to create GUID\n"); - m_State = STATE_FAIL; - return FALSE; - } - if(pClass->m_pEncloser) szFullName = strrchr(pClass->m_szFQN,NESTING_SEP) + 1; else diff --git a/src/coreclr/ilasm/portable_pdb.cpp b/src/coreclr/ilasm/portable_pdb.cpp index 68bd1bd72b950..83260f107de50 100644 --- a/src/coreclr/ilasm/portable_pdb.cpp +++ b/src/coreclr/ilasm/portable_pdb.cpp @@ -92,9 +92,11 @@ HRESULT PortablePdbWriter::Init(IMetaDataDispenserEx2* mdDispenser) time_t now; time(&now); m_pdbStream.id.pdbTimeStamp = (ULONG)now; - hr = CoCreateGuid(&m_pdbStream.id.pdbGuid); - - if (FAILED(hr)) goto exit; + if (!minipal_guid_v4_create(reinterpret_cast(&m_pdbStream.id.pdbGuid))) + { + hr = E_FAIL; + goto exit; + } hr = mdDispenser->DefinePortablePdbScope( CLSID_CorMetaDataRuntime, @@ -176,7 +178,7 @@ HRESULT PortablePdbWriter::DefineDocument(char* name, GUID* language) mdDocument docToken = mdDocumentNil; if (FAILED(hr = m_pdbEmitter->DefineDocument( - name, // will be tokenized + name, // will be tokenized &hashAlgorithmUnknown, hashValue, cbHashValue, diff --git a/src/coreclr/ildasm/exe/CMakeLists.txt b/src/coreclr/ildasm/exe/CMakeLists.txt index fd5daaaa1525b..c16fbed72c09f 100644 --- a/src/coreclr/ildasm/exe/CMakeLists.txt +++ b/src/coreclr/ildasm/exe/CMakeLists.txt @@ -94,7 +94,6 @@ else() list(APPEND ILDASM_LINK_LIBRARIES coreclrpal mscorrc - palrt coreclrminipal ) endif(CLR_CMAKE_HOST_WIN32) @@ -105,7 +104,6 @@ if(CLR_CMAKE_HOST_UNIX) ${ILDASM_LINK_LIBRARIES} mscorrc coreclrpal - palrt ${CMAKE_DL_LIBS} ) else() diff --git a/src/coreclr/inc/holder.h b/src/coreclr/inc/holder.h index 353d7f39d5004..ea0466df2e16d 100644 --- a/src/coreclr/inc/holder.h +++ b/src/coreclr/inc/holder.h @@ -10,6 +10,7 @@ #include "staticcontract.h" #include "volatile.h" #include "palclr.h" +#include #include #include diff --git a/src/coreclr/md/compiler/regmeta.cpp b/src/coreclr/md/compiler/regmeta.cpp index 85130f4cd4019..7022832ae99e4 100644 --- a/src/coreclr/md/compiler/regmeta.cpp +++ b/src/coreclr/md/compiler/regmeta.cpp @@ -21,6 +21,7 @@ #include "posterror.h" #include "stgio.h" #include "sstring.h" +#include #include "mdinternalrw.h" @@ -246,7 +247,7 @@ RegMeta::CreateNewMD() ModuleRec *pModule; GUID mvid; IfFailGo(m_pStgdb->m_MiniMd.AddModuleRecord(&pModule, &iRecord)); - IfFailGo(CoCreateGuid(&mvid)); + IfFailGo(minipal_guid_v4_create(&mvid) ? S_OK : E_FAIL); IfFailGo(m_pStgdb->m_MiniMd.PutGuid(TBL_Module, ModuleRec::COL_Mvid, pModule, mvid)); // Add the dummy module typedef which we are using to parent global items. diff --git a/src/coreclr/md/enc/metamodelrw.cpp b/src/coreclr/md/enc/metamodelrw.cpp index 930a5c98a9446..45b75bb09ba12 100644 --- a/src/coreclr/md/enc/metamodelrw.cpp +++ b/src/coreclr/md/enc/metamodelrw.cpp @@ -19,6 +19,7 @@ #include "../compiler/importhelper.h" #include "metadata.h" #include "streamutil.h" +#include #ifdef _MSC_VER #pragma intrinsic(memcpy) @@ -1196,7 +1197,7 @@ CMiniMdRW::SetOption( PutCol(TBL_Module, ModuleRec::COL_EncBaseId, pMod, uVal); */ // Allocate a new GUID for EncId. - IfFailGo(CoCreateGuid(&encid)); + IfFailGo(minipal_guid_v4_create(&encid) ? S_OK : E_FAIL); IfFailGo(PutGuid(TBL_Module, ModuleRec::COL_EncId, pMod, encid)); #else //!FEATURE_METADATA_EMIT IfFailGo(E_INVALIDARG); diff --git a/src/coreclr/md/inc/portablepdbmdds.h b/src/coreclr/md/inc/portablepdbmdds.h index 10c82d3dba318..9caed6cf2eb3e 100644 --- a/src/coreclr/md/inc/portablepdbmdds.h +++ b/src/coreclr/md/inc/portablepdbmdds.h @@ -16,6 +16,7 @@ #endif #include "corhdr.h" +#include //------------------------------------- //--- PDB stream data structure diff --git a/src/coreclr/minipal/Unix/dn-u16.cpp b/src/coreclr/minipal/Unix/dn-u16.cpp index 7832e501023d3..aec3fb1ae6638 100644 --- a/src/coreclr/minipal/Unix/dn-u16.cpp +++ b/src/coreclr/minipal/Unix/dn-u16.cpp @@ -5,13 +5,11 @@ typedef char16_t WCHAR; #include #include +#include size_t u16_strlen(const WCHAR* str) { - size_t nChar = 0; - while (*str++) - nChar++; - return nChar; + return minipal_u16_strlen((CHAR16_T*)str); } int u16_strcmp(const WCHAR* str1, const WCHAR* str2) @@ -192,4 +190,4 @@ uint64_t u16_strtoui64(const WCHAR* nptr, WCHAR** endptr, int base) double u16_strtod(const WCHAR* nptr, WCHAR** endptr) { return PAL_wcstod(nptr, endptr); -} \ No newline at end of file +} diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index f7ca6d565b4ed..9b2bb85223964 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -783,46 +783,6 @@ void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array) #endif } -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(minipal_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 -} - ep_rt_thread_handle_t ep_rt_aot_thread_get_handle (void) { return ThreadStore::GetCurrentThreadIfAvailable(); diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index 0513409886ebb..9c0a120531003 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -660,17 +660,6 @@ ep_rt_process_shutdown (void) return false; } -static -inline -void -ep_rt_create_activity_id ( - uint8_t *activity_id, - uint32_t activity_id_len) -{ - 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 inline bool diff --git a/src/coreclr/nativeaot/Runtime/eventpipeinternal.cpp b/src/coreclr/nativeaot/Runtime/eventpipeinternal.cpp index dca86d0138eec..01d251c46fc07 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipeinternal.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipeinternal.cpp @@ -198,7 +198,7 @@ EXTERN_C int QCALLTYPE EventPipeInternal_EventActivityIdControl(uint32_t control case ActivityControlCode::EVENT_ACTIVITY_CONTROL_CREATE_ID: - ep_rt_create_activity_id(reinterpret_cast(pActivityId), EP_ACTIVITY_ID_SIZE); + ep_thread_create_activity_id(reinterpret_cast(pActivityId), EP_ACTIVITY_ID_SIZE); break; case ActivityControlCode::EVENT_ACTIVITY_CONTROL_GET_SET_ID: @@ -212,7 +212,7 @@ EXTERN_C int QCALLTYPE EventPipeInternal_EventActivityIdControl(uint32_t control case ActivityControlCode::EVENT_ACTIVITY_CONTROL_CREATE_SET_ID: ep_rt_thread_get_activity_id (activityIdHandle, reinterpret_cast(pActivityId), EP_ACTIVITY_ID_SIZE); - ep_rt_create_activity_id(reinterpret_cast(¤tActivityId), EP_ACTIVITY_ID_SIZE); + ep_thread_create_activity_id(reinterpret_cast(¤tActivityId), EP_ACTIVITY_ID_SIZE); ep_rt_thread_set_activity_id (activityIdHandle, reinterpret_cast(¤tActivityId), EP_ACTIVITY_ID_SIZE); break; diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index 9ba3edc18cb59..9a09b79ffce48 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -363,13 +363,6 @@ PALAPI PAL_UnregisterModule( IN HINSTANCE hInstance); -PALIMPORT -VOID -PALAPI -PAL_Random( - IN OUT LPVOID lpBuffer, - IN DWORD dwLength); - PALIMPORT BOOL PALAPI diff --git a/src/coreclr/pal/inc/rt/palrt.h b/src/coreclr/pal/inc/rt/palrt.h index 05c04a21ca671..9317654958b63 100644 --- a/src/coreclr/pal/inc/rt/palrt.h +++ b/src/coreclr/pal/inc/rt/palrt.h @@ -274,10 +274,6 @@ typedef union _ULARGE_INTEGER { } ULARGE_INTEGER, *PULARGE_INTEGER; /******************* OLE, BSTR, VARIANT *************************/ - -STDAPI_VIS(DLLEXPORT, LPVOID) CoTaskMemAlloc(SIZE_T cb); -STDAPI_VIS(DLLEXPORT, void) CoTaskMemFree(LPVOID pv); - typedef SHORT VARIANT_BOOL; #define VARIANT_TRUE ((VARIANT_BOOL)-1) #define VARIANT_FALSE ((VARIANT_BOOL)0) @@ -1069,33 +1065,6 @@ typedef LONG (WINAPI *PTOP_LEVEL_EXCEPTION_FILTER)( ); typedef PTOP_LEVEL_EXCEPTION_FILTER LPTOP_LEVEL_EXCEPTION_FILTER; -/******************** PAL RT APIs *******************************/ - -typedef struct _HSATELLITE *HSATELLITE; - -EXTERN_C HSATELLITE PALAPI PAL_LoadSatelliteResourceW(LPCWSTR SatelliteResourceFileName); -EXTERN_C HSATELLITE PALAPI PAL_LoadSatelliteResourceA(LPCSTR SatelliteResourceFileName); -EXTERN_C BOOL PALAPI PAL_FreeSatelliteResource(HSATELLITE SatelliteResource); -EXTERN_C UINT PALAPI PAL_LoadSatelliteStringW(HSATELLITE SatelliteResource, - UINT uID, - LPWSTR lpBuffer, - UINT nBufferMax); -EXTERN_C UINT PALAPI PAL_LoadSatelliteStringA(HSATELLITE SatelliteResource, - UINT uID, - LPSTR lpBuffer, - UINT nBufferMax); - -EXTERN_C HRESULT PALAPI PAL_CoCreateInstance(REFCLSID rclsid, - REFIID riid, - void **ppv); - -// So we can have CoCreateInstance in most of the code base, -// instead of spreading around of if'def FEATURE_PALs for PAL_CoCreateInstance. -#define CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv) PAL_CoCreateInstance(rclsid, riid, ppv) - -STDAPI -CoCreateGuid(OUT GUID * pguid); - /************** Byte swapping & unaligned access ******************/ #include diff --git a/src/coreclr/pal/src/CMakeLists.txt b/src/coreclr/pal/src/CMakeLists.txt index db5d430ed011b..0a3ed749f29fb 100644 --- a/src/coreclr/pal/src/CMakeLists.txt +++ b/src/coreclr/pal/src/CMakeLists.txt @@ -134,6 +134,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND (CLR_CMAKE_HOST_ARCH_AMD64 OR CLR_CM endif() set(SOURCES + com/guid.cpp cruntime/wchar.cpp debug/debug.cpp exception/seh.cpp @@ -158,7 +159,6 @@ set(SOURCES misc/error.cpp misc/errorstrings.cpp misc/fmtmessage.cpp - misc/miscpalapi.cpp misc/perfjitdump.cpp misc/strutil.cpp misc/sysinfo.cpp @@ -203,6 +203,12 @@ set(SOURCES thread/threadsusp.cpp ) +set_source_files_properties( + com/guid.cpp + PROPERTIES + INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/../inc/rt +) + if(NOT CLR_CMAKE_USE_SYSTEM_LIBUNWIND) set(LIBUNWIND_OBJECTS $) endif(NOT CLR_CMAKE_USE_SYSTEM_LIBUNWIND) diff --git a/src/coreclr/pal/src/com/guid.cpp b/src/coreclr/pal/src/com/guid.cpp new file mode 100644 index 0000000000000..57b632d86ca68 --- /dev/null +++ b/src/coreclr/pal/src/com/guid.cpp @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// + +#define INITGUID +#include + +// These are GUIDs and IIDs that would normally be provided by the system via uuid.lib, +// and that the PALRT exposes through headers. + +DEFINE_GUID(GUID_NULL, 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); +DEFINE_GUID(IID_IUnknown, 0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); +DEFINE_GUID(IID_IClassFactory, 0x00000001, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); + + +// objidl.idl +DEFINE_GUID(IID_ISequentialStream, 0x0c733a30, 0x2a1c, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d); +DEFINE_GUID(IID_IStream, 0x0000000c, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); diff --git a/src/coreclr/pal/src/locale/unicode.cpp b/src/coreclr/pal/src/locale/unicode.cpp index e966e22821ba0..a14b414c32348 100644 --- a/src/coreclr/pal/src/locale/unicode.cpp +++ b/src/coreclr/pal/src/locale/unicode.cpp @@ -14,6 +14,7 @@ Implementation of all functions related to Unicode support #include "pal/palinternal.h" #include "pal/dbgmsg.h" #include "pal/file.h" +#include #include #include "pal/cruntime.h" #include "pal/stackstring.hpp" @@ -202,7 +203,7 @@ WideCharToMultiByte( if (CodePage == CP_UTF8 || CodePage == CP_ACP) { if (cchWideChar < 0) - cchWideChar = PAL_wcslen(lpWideCharStr) + 1; + cchWideChar = minipal_u16_strlen((CHAR16_T*)lpWideCharStr) + 1; if (!lpMultiByteStr || cbMultiByte == 0) retval = minipal_get_length_utf16_to_utf8((CHAR16_T*)lpWideCharStr, cchWideChar, dwFlags); diff --git a/src/coreclr/pal/src/misc/miscpalapi.cpp b/src/coreclr/pal/src/misc/miscpalapi.cpp deleted file mode 100644 index f0d32f0388e86..0000000000000 --- a/src/coreclr/pal/src/misc/miscpalapi.cpp +++ /dev/null @@ -1,131 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*++ - - - -Module Name: - - miscpalapi.c - -Abstract: - - Implementation misc PAL APIs - -Revision History: - - - ---*/ - -#include "pal/palinternal.h" -#include "pal/dbgmsg.h" -#include "pal/file.h" -#include "pal/process.h" -#include "pal/module.h" -#include "pal/malloc.hpp" -#include "pal/stackstring.hpp" - -#include -#include -#include -#include -#include -#include - -#include - -#ifdef __APPLE__ -#include -#endif // __APPLE__ - -SET_DEFAULT_DEBUG_CHANNEL(MISC); - -static const char URANDOM_DEVICE_NAME[]="/dev/urandom"; - -VOID -PALAPI -PAL_Random( - IN OUT LPVOID lpBuffer, - IN DWORD dwLength) -{ - int rand_des = -1; - DWORD i; - long num = 0; - static BOOL sMissingDevURandom; - static BOOL sInitializedMRand; - - PERF_ENTRY(PAL_Random); - ENTRY("PAL_Random(lpBuffer=%p, dwLength=%d)\n", lpBuffer, dwLength); - - if (!sMissingDevURandom) - { - do - { - rand_des = open("/dev/urandom", O_RDONLY | O_CLOEXEC); - } - while ((rand_des == -1) && (errno == EINTR)); - - if (rand_des == -1) - { - if (errno == ENOENT) - { - sMissingDevURandom = TRUE; - } - else - { - ASSERT("PAL__open() failed, errno:%d (%s)\n", errno, strerror(errno)); - } - - // Back off and try mrand48. - } - else - { - DWORD offset = 0; - do - { - ssize_t n = read(rand_des, (BYTE*)lpBuffer + offset , dwLength - offset); - if (n == -1) - { - if (errno == EINTR) - { - continue; - } - ASSERT("read() failed, errno:%d (%s)\n", errno, strerror(errno)); - - break; - } - - offset += n; - } - while (offset != dwLength); - - _ASSERTE(offset == dwLength); - - close(rand_des); - } - } - - if (!sInitializedMRand) - { - srand48(time(NULL)); - sInitializedMRand = TRUE; - } - - // always xor srand48 over the whole buffer to get some randomness - // in case /dev/urandom is not really random - - for (i = 0; i < dwLength; i++) - { - if (i % sizeof(long) == 0) { - num = mrand48(); - } - - *(((BYTE*)lpBuffer) + i) ^= num; - num >>= 8; - } - - LOGEXIT("PAL_Random\n"); - PERF_EXIT(PAL_Random); -} diff --git a/src/coreclr/palrt/CMakeLists.txt b/src/coreclr/palrt/CMakeLists.txt deleted file mode 100644 index eda33fe9e0592..0000000000000 --- a/src/coreclr/palrt/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(PALRT_SOURCES - comem.cpp - guid.cpp -) - -add_library_clr(palrt - STATIC - ${PALRT_SOURCES} -) - -# Install the static PAL library for VS -install_clr(TARGETS palrt DESTINATIONS lib) diff --git a/src/coreclr/palrt/comem.cpp b/src/coreclr/palrt/comem.cpp deleted file mode 100644 index e56e720cc80c2..0000000000000 --- a/src/coreclr/palrt/comem.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// - -// -// =========================================================================== -// File: comem.cpp -// -// =========================================================================== - -#include "common.h" - -STDAPI_(LPVOID) CoTaskMemAlloc(SIZE_T cb) -{ - return malloc(cb); -} - -STDAPI_(void) CoTaskMemFree(LPVOID pv) -{ - free(pv); -} diff --git a/src/coreclr/palrt/common.h b/src/coreclr/palrt/common.h deleted file mode 100644 index 1040b7c13cec4..0000000000000 --- a/src/coreclr/palrt/common.h +++ /dev/null @@ -1,17 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -//***************************************************************************** -// common.h -// - -// -// Common include file for the palrt code. -//***************************************************************************** - -#ifndef _COMMON_H_ -#define _COMMON_H_ - -#include -#include -#include -#endif // _COMMON_H_ diff --git a/src/coreclr/palrt/guid.cpp b/src/coreclr/palrt/guid.cpp deleted file mode 100644 index 68cc157d91ca7..0000000000000 --- a/src/coreclr/palrt/guid.cpp +++ /dev/null @@ -1,47 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// - -// -// =========================================================================== -// File: guid.cpp -// -// PALRT guids -// =========================================================================== - -#define INITGUID -#include - -// These are GUIDs and IIDs that would normally be provided by the system via uuid.lib, -// and that the PALRT exposes through headers. - -DEFINE_GUID(GUID_NULL, 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); -DEFINE_GUID(IID_IUnknown, 0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); -DEFINE_GUID(IID_IClassFactory, 0x00000001, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); - - -// objidl.idl -DEFINE_GUID(IID_ISequentialStream, 0x0c733a30, 0x2a1c, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d); -DEFINE_GUID(IID_IStream, 0x0000000c, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); - -// Create a random guid based on the https://www.ietf.org/rfc/rfc4122.txt -STDAPI -CoCreateGuid(OUT GUID * pguid) -{ - PAL_Random(pguid, sizeof(GUID)); - - static const USHORT VersionMask = 0xF000; - static const USHORT RandomGuidVersion = 0x4000; - - static const BYTE ClockSeqHiAndReservedMask = 0xC0; - static const BYTE ClockSeqHiAndReservedValue = 0x80; - - // Modify bits indicating the type of the GUID - - // time_hi_and_version - pguid->Data3 = (pguid->Data3 & ~VersionMask) | RandomGuidVersion; - // clock_seq_hi_and_reserved - pguid->Data4[0] = (pguid->Data4[0] & ~ClockSeqHiAndReservedMask) | ClockSeqHiAndReservedValue; - - return S_OK; -} diff --git a/src/coreclr/tools/superpmi/mcs/CMakeLists.txt b/src/coreclr/tools/superpmi/mcs/CMakeLists.txt index 254ec92cb0488..b11fed72e32e6 100644 --- a/src/coreclr/tools/superpmi/mcs/CMakeLists.txt +++ b/src/coreclr/tools/superpmi/mcs/CMakeLists.txt @@ -58,7 +58,6 @@ if(CLR_CMAKE_HOST_UNIX) utilcodestaticnohost mscorrc coreclrpal - palrt coreclrminipal ) else() diff --git a/src/coreclr/tools/superpmi/superpmi-shared/spmiutil.cpp b/src/coreclr/tools/superpmi/superpmi-shared/spmiutil.cpp index fae4cea68159b..9bba21b1bfd8e 100644 --- a/src/coreclr/tools/superpmi/superpmi-shared/spmiutil.cpp +++ b/src/coreclr/tools/superpmi/superpmi-shared/spmiutil.cpp @@ -10,6 +10,7 @@ #include "spmiutil.h" #include +#include static bool breakOnDebugBreakorAV = false; @@ -235,13 +236,8 @@ WCHAR* GetResultFileName(const WCHAR* folderPath, const WCHAR* fileName, const W // Append a random string to improve uniqueness. // - unsigned randomNumber = 0; - -#ifdef TARGET_UNIX - PAL_Random(&randomNumber, sizeof(randomNumber)); -#else // !TARGET_UNIX - rand_s(&randomNumber); -#endif // !TARGET_UNIX + unsigned int randomNumber = 0; + minipal_get_non_cryptographically_secure_random_bytes((uint8_t*)&randomNumber, sizeof(randomNumber)); WCHAR randomString[randomStringLength + 1]; FormatInteger(randomString, randomStringLength + 1, "%08X", randomNumber); diff --git a/src/coreclr/tools/superpmi/superpmi-shim-collector/CMakeLists.txt b/src/coreclr/tools/superpmi/superpmi-shim-collector/CMakeLists.txt index 5ddfc257abf74..61ea6207c8b29 100644 --- a/src/coreclr/tools/superpmi/superpmi-shim-collector/CMakeLists.txt +++ b/src/coreclr/tools/superpmi/superpmi-shim-collector/CMakeLists.txt @@ -73,7 +73,6 @@ if(CLR_CMAKE_HOST_UNIX) utilcodestaticnohost mscorrc coreclrpal - palrt coreclrminipal ) else() diff --git a/src/coreclr/tools/superpmi/superpmi-shim-counter/CMakeLists.txt b/src/coreclr/tools/superpmi/superpmi-shim-counter/CMakeLists.txt index 231b5d3253e97..107425a882974 100644 --- a/src/coreclr/tools/superpmi/superpmi-shim-counter/CMakeLists.txt +++ b/src/coreclr/tools/superpmi/superpmi-shim-counter/CMakeLists.txt @@ -54,7 +54,6 @@ if(CLR_CMAKE_HOST_UNIX) utilcodestaticnohost mscorrc coreclrpal - palrt coreclrminipal ) else() diff --git a/src/coreclr/tools/superpmi/superpmi-shim-simple/CMakeLists.txt b/src/coreclr/tools/superpmi/superpmi-shim-simple/CMakeLists.txt index 96ac56a7e8523..f6be551299c16 100644 --- a/src/coreclr/tools/superpmi/superpmi-shim-simple/CMakeLists.txt +++ b/src/coreclr/tools/superpmi/superpmi-shim-simple/CMakeLists.txt @@ -53,7 +53,6 @@ if(CLR_CMAKE_HOST_UNIX) utilcodestaticnohost mscorrc coreclrpal - palrt coreclrminipal ) else() diff --git a/src/coreclr/tools/superpmi/superpmi/CMakeLists.txt b/src/coreclr/tools/superpmi/superpmi/CMakeLists.txt index ccb766f0d0788..2b93a45c7c9eb 100644 --- a/src/coreclr/tools/superpmi/superpmi/CMakeLists.txt +++ b/src/coreclr/tools/superpmi/superpmi/CMakeLists.txt @@ -64,7 +64,6 @@ if(CLR_CMAKE_HOST_UNIX) utilcodestaticnohost mscorrc coreclrpal - palrt coreclrminipal ) else() diff --git a/src/coreclr/tools/superpmi/superpmi/parallelsuperpmi.cpp b/src/coreclr/tools/superpmi/superpmi/parallelsuperpmi.cpp index 11af679d6d06b..756664d7a88e5 100644 --- a/src/coreclr/tools/superpmi/superpmi/parallelsuperpmi.cpp +++ b/src/coreclr/tools/superpmi/superpmi/parallelsuperpmi.cpp @@ -9,6 +9,7 @@ #include "commandline.h" #include "errorhandling.h" #include "fileio.h" +#include // Forward declare the conversion method. Including spmiutil.h pulls in other headers // that cause build breaks. @@ -557,11 +558,7 @@ int doParallelSuperPMI(CommandLine::Options& o) // Add a random number to the temporary file names to allow multiple parallel SuperPMI to happen at once. unsigned int randNumber = 0; -#ifdef TARGET_UNIX - PAL_Random(&randNumber, sizeof(randNumber)); -#else // !TARGET_UNIX - rand_s(&randNumber); -#endif // !TARGET_UNIX + minipal_get_non_cryptographically_secure_random_bytes((uint8_t*)&randNumber, sizeof(randNumber)); for (int i = 0; i < o.workerCount; i++) { diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index 7f66adf7df631..b45e9770a1c4e 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -17,6 +17,8 @@ #include "typestring.h" #include "clrversion.h" #include "hostinformation.h" +#include +#include #undef EP_INFINITE_WAIT #define EP_INFINITE_WAIT INFINITE @@ -737,20 +739,6 @@ ep_rt_process_shutdown (void) return (bool)g_fEEShutDown; } -static -inline -void -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); - - CoCreateGuid (reinterpret_cast(activity_id)); -} - static inline bool @@ -1460,7 +1448,7 @@ ep_rt_utf16_string_len (const ep_char16_t *str) STATIC_CONTRACT_NOTHROW; EP_ASSERT (str != NULL); - return u16_strlen (reinterpret_cast(str)); + return minipal_u16_strlen (reinterpret_cast (str)); } static diff --git a/src/coreclr/vm/eventpipeinternal.cpp b/src/coreclr/vm/eventpipeinternal.cpp index 9d00e84800151..0c89c706b3051 100644 --- a/src/coreclr/vm/eventpipeinternal.cpp +++ b/src/coreclr/vm/eventpipeinternal.cpp @@ -9,6 +9,8 @@ #include "pal.h" #endif // TARGET_UNIX +#include + #ifdef FEATURE_PERFTRACING extern "C" UINT64 QCALLTYPE EventPipeInternal_Enable( @@ -190,7 +192,7 @@ extern "C" int QCALLTYPE EventPipeInternal_EventActivityIdControl(uint32_t contr case ActivityControlCode::EVENT_ACTIVITY_CONTROL_CREATE_ID: - CoCreateGuid(pActivityId); + minipal_guid_v4_create(reinterpret_cast(pActivityId)); break; case ActivityControlCode::EVENT_ACTIVITY_CONTROL_GET_SET_ID: @@ -203,7 +205,7 @@ extern "C" int QCALLTYPE EventPipeInternal_EventActivityIdControl(uint32_t contr case ActivityControlCode::EVENT_ACTIVITY_CONTROL_CREATE_SET_ID: *pActivityId = *pThread->GetActivityId(); - CoCreateGuid(¤tActivityId); + minipal_guid_v4_create(¤tActivityId); pThread->SetActivityId(¤tActivityId); break; diff --git a/src/coreclr/vm/olevariant.cpp b/src/coreclr/vm/olevariant.cpp index ab42391080e03..b46c5027d097b 100644 --- a/src/coreclr/vm/olevariant.cpp +++ b/src/coreclr/vm/olevariant.cpp @@ -2468,7 +2468,7 @@ void OleVariant::MarshalObjectForOleVariant(const VARIANT * pOle, OBJECTREF * co AllocateObject(CoreLibBinder::GetElementType(ELEMENT_TYPE_U1)) ); *(BYTE*)((*pObj)->GetData()) = *(V_UI1REF(pOle)); break; - + case VT_I8: SetObjectReference( pObj, AllocateObject(CoreLibBinder::GetElementType(ELEMENT_TYPE_I8)) ); diff --git a/src/mono/CMakeLists.txt b/src/mono/CMakeLists.txt index 70f03a4a9f4dd..80d82971843e4 100644 --- a/src/mono/CMakeLists.txt +++ b/src/mono/CMakeLists.txt @@ -565,7 +565,7 @@ if(GCC) if(HOST_WASI) # When building under WASI SDK, it's stricter about discarding 'const' qualifiers, causing some existing - # code (e.g., mono-rand.c:315) to be rejected + # code to be rejected set(WERROR_C "${WERROR_C} -Wno-incompatible-pointer-types-discards-qualifiers") endif() diff --git a/src/mono/mono/eglib/giconv.c b/src/mono/mono/eglib/giconv.c index 76743ed5c47c1..144d38a5de4c2 100644 --- a/src/mono/mono/eglib/giconv.c +++ b/src/mono/mono/eglib/giconv.c @@ -29,6 +29,7 @@ #include "../utils/mono-errno.h" #include +#include #ifdef _MSC_VER #define FORCE_INLINE(RET_TYPE) __forceinline RET_TYPE @@ -304,11 +305,7 @@ g_utf16_to_utf8_impl (const gunichar2 *str, glong len, glong *items_read, glong flags |= MINIPAL_TREAT_AS_LITTLE_ENDIAN; #endif if (len < 0) { - len = 0; - while (str[len]) - len++; - - len++; + len = (glong)minipal_u16_strlen (str) + 1; } glong ret = (glong)minipal_get_length_utf16_to_utf8 (str, len, flags); @@ -349,11 +346,7 @@ g_utf16_to_utf8_custom_alloc (const gunichar2 *str, glong len, glong *items_read errno = 0; if (len < 0) { - len = 0; - while (str[len]) - len++; - - len++; + len = (glong)minipal_u16_strlen (str) + 1; } glong ret = (glong)minipal_get_length_utf16_to_utf8 (str, len, 0); @@ -391,9 +384,7 @@ g_utf16_to_ucs4 (const gunichar2 *str, glong len, glong *items_read, glong *item g_return_val_if_fail (str != NULL, NULL); if (len < 0) { - len = 0; - while (str[len]) - len++; + len = (glong)minipal_u16_strlen (str); } inptr = (char *) str; diff --git a/src/mono/mono/eventpipe/ep-rt-mono.c b/src/mono/mono/eventpipe/ep-rt-mono.c index c75bb43007630..f07f5c6d26341 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.c +++ b/src/mono/mono/eventpipe/ep-rt-mono.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -29,9 +28,6 @@ gboolean _ep_rt_mono_runtime_initialized; MonoNativeTlsKey _ep_rt_mono_thread_holder_tls_id; static MonoNativeTlsKey _thread_data_tls_id; -// Random byte provider. -static gpointer _rand_provider; - // EventPipe global config lock. ep_rt_spin_lock_handle_t _ep_rt_mono_config_lock = {0}; @@ -62,17 +58,6 @@ EVENTPIPE_TRACE_CONTEXT MICROSOFT_DOTNETRUNTIME_MONO_PROFILER_PROVIDER_DOTNET_Co void ep_rt_mono_thread_exited (void); -bool -ep_rt_mono_rand_try_get_bytes ( - uint8_t *buffer, - size_t buffer_size) -{ - EP_ASSERT (_rand_provider != NULL); - - ERROR_DECL (error); - return mono_rand_try_get_bytes (&_rand_provider, (guchar *)buffer, (gssize)buffer_size, error); -} - char * ep_rt_mono_get_managed_cmd_line (void) { @@ -816,8 +801,6 @@ ep_rt_mono_init (void) mono_native_tls_alloc (&_thread_data_tls_id, NULL); mono_100ns_ticks (); - mono_rand_open (); - _rand_provider = mono_rand_init (NULL, 0); ep_rt_mono_runtime_provider_init (); ep_rt_mono_profiler_provider_init (); diff --git a/src/mono/mono/eventpipe/ep-rt-mono.h b/src/mono/mono/eventpipe/ep-rt-mono.h index 418f3b8e1e93d..94ca5bb94f7e5 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.h +++ b/src/mono/mono/eventpipe/ep-rt-mono.h @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -70,7 +69,6 @@ extern void ep_rt_mono_provider_config_init (EventPipeProviderConfiguration *pro extern void ep_rt_mono_init_providers_and_events (void); extern bool ep_rt_mono_providers_validate_all_disabled (void); extern bool ep_rt_mono_sample_profiler_write_sampling_event_for_threads (ep_rt_thread_handle_t sampling_thread, EventPipeEvent *sampling_event); -extern bool ep_rt_mono_rand_try_get_bytes (uint8_t *buffer,size_t buffer_size); extern void ep_rt_mono_execute_rundown (dn_vector_ptr_t *execution_checkpoints); extern int64_t ep_rt_mono_perf_counter_query (void); extern int64_t ep_rt_mono_perf_frequency_query (void); @@ -773,42 +771,6 @@ ep_rt_process_shutdown (void) return ep_rt_process_detach (); } -static -inline -void -ep_rt_create_activity_id ( - uint8_t *activity_id, - uint32_t activity_id_len) -{ - EP_ASSERT (activity_id != NULL); - EP_ASSERT (activity_id_len == EP_ACTIVITY_ID_SIZE); - - ep_rt_mono_rand_try_get_bytes ((guchar *)activity_id, 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)); -} - static inline bool diff --git a/src/mono/mono/mini/aot-compiler.c b/src/mono/mono/mini/aot-compiler.c index df45813a2e30d..51889fa514b9b 100644 --- a/src/mono/mono/mini/aot-compiler.c +++ b/src/mono/mono/mini/aot-compiler.c @@ -58,11 +58,11 @@ #include #include #include -#include #include #include #include #include +#include #include "aot-compiler.h" #include "aot-runtime.h" @@ -11567,16 +11567,8 @@ emit_extra_methods (MonoAotCompile *acfg) static void generate_aotid (guint8* aotid) { - gpointer rand_handle; - ERROR_DECL (error); - - mono_rand_open (); - rand_handle = mono_rand_init (NULL, 0); - - mono_rand_try_get_bytes (&rand_handle, aotid, 16, error); - mono_error_assert_ok (error); - - mono_rand_close (rand_handle); + int status = minipal_get_cryptographically_secure_random_bytes (aotid, 16); + g_assert (status == 0); } static void diff --git a/src/mono/mono/utils/CMakeLists.txt b/src/mono/mono/utils/CMakeLists.txt index 05a670692edb3..8c124a1aaa952 100644 --- a/src/mono/mono/utils/CMakeLists.txt +++ b/src/mono/mono/utils/CMakeLists.txt @@ -155,9 +155,6 @@ set(utils_common_sources mono-conc-hashtable.c json.h json.c - mono-rand.c - mono-rand-windows.c - mono-rand.h memfuncs.c memfuncs.h parse.c diff --git a/src/mono/mono/utils/mono-rand-windows.c b/src/mono/mono/utils/mono-rand-windows.c deleted file mode 100644 index ff856bb61bf29..0000000000000 --- a/src/mono/mono/utils/mono-rand-windows.c +++ /dev/null @@ -1,101 +0,0 @@ -/** - * \file - * Windows rand support for Mono. - * - * Copyright 2016 Microsoft - * Licensed under the MIT license. See LICENSE file in the project root for full license information. -*/ -#include -#include -#ifdef HOST_WIN32 -#include -#include "mono-error-internals.h" -#include "mono-rand.h" -#include -#include -#include - -// This implementation requires Windows 7 or newer. - -#define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002 -const static char mono_rand_provider [ ] = "BCryptGenRandom"; - -/** - * mono_rand_open: - * - * Returns: True if random source is global, false if mono_rand_init can be called repeatedly to get randomness instances. - * - * Initializes entire RNG system. Must be called once per process before calling mono_rand_init. - */ -gboolean -mono_rand_open (void) -{ - return TRUE; -} - -/** - * mono_rand_init: - * \param seed A string containing seed data - * \param seed_size Length of seed string - * Initializes an RNG client. - * \returns On success, a non-NULL handle which can be used to fetch random data from \c mono_rand_try_get_bytes. On failure, NULL. - */ -gpointer -mono_rand_init (const guchar *seed, gssize seed_size) -{ - // NULL will be interpreted as failure; return arbitrary nonzero pointer - return (gpointer)mono_rand_provider; -} - -/** - * mono_rand_try_get_bytes: - * \param handle A pointer to an RNG handle. Handle is set to NULL on failure. - * \param buffer A buffer into which to write random data. - * \param buffer_size Number of bytes to write into buffer. - * \param error Set on error. - * Extracts bytes from an RNG handle. - * \returns FALSE on failure and sets \p error, TRUE on success. - */ -gboolean -mono_rand_try_get_bytes (gpointer *handle, guchar *buffer, gssize buffer_size, MonoError *error) -{ - g_assert (buffer || !buffer_size); - error_init (error); - g_assert (handle); - gpointer const handle_value = *handle; - g_assert (handle_value == 0 || handle_value == mono_rand_provider); - if (!handle_value) - return FALSE; - while (buffer_size > 0) { - ULONG const size = (ULONG)MIN (buffer_size, ULONG_MAX); - NTSTATUS const status = BCryptGenRandom (0, buffer, size, BCRYPT_USE_SYSTEM_PREFERRED_RNG); - if (!BCRYPT_SUCCESS (status)) { - mono_error_set_execution_engine (error, "Failed to gen random bytes (%ld)", status); - // failure, clear provider for future attempts - *handle = 0; - return FALSE; - } - buffer += size; - buffer_size -= size; - } - return TRUE; -} - -/** - * mono_rand_close: - * \param handle An RNG handle. - * Releases an RNG handle. - */ -void -mono_rand_close (gpointer handle) -{ - g_assert (handle == 0 || handle == mono_rand_provider); -} - -#else - -#include - -MONO_EMPTY_SOURCE_FILE (mono_rand_windows); - -#endif /* HOST_WIN32 */ diff --git a/src/mono/mono/utils/mono-rand.c b/src/mono/mono/utils/mono-rand.c deleted file mode 100644 index 0f64d3b9eacdb..0000000000000 --- a/src/mono/mono/utils/mono-rand.c +++ /dev/null @@ -1,380 +0,0 @@ -/** - * \file - * - * Authors: - * Mark Crichton (crichton@gimp.org) - * Patrik Torstensson (p@rxc.se) - * Sebastien Pouliot (sebastien@ximian.com) - * Ludovic Henry (ludovic.henry@xamarin.com) - * - * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com) - * Copyright 2004-2009 Novell, Inc (http://www.novell.com) - * Copyright 2001 Xamarin, Inc (http://www.novell.com) - * Licensed under the MIT license. See LICENSE file in the project root for full license information. - */ - -#include -#include - -#include "atomic.h" -#include -#include "mono-error-internals.h" -#include "mono-rand.h" -#include "mono-threads.h" -#include -#include - -#ifdef HOST_WIN32 -// Windows specific implementation in mono-rand-windows.c -#elif defined (HAVE_SYS_UN_H) - -#include -#include -#include -#include -#include -#include - -#ifdef HAVE_SYS_RANDOM_H -#include -#endif - -#ifndef NAME_DEV_URANDOM -#define NAME_DEV_URANDOM "/dev/urandom" -#endif - -static gboolean use_egd = FALSE; -static gint file = -1; - -#ifdef HAVE_GETRANDOM -/* Fill buffer with buffer_size random bytes generated by getrandom(): - - Return 1 on success - - Return 0 if getrandom() is not available (failed with ENOSYS or EPERM) - - Return -1 on error - getrandom() is retried if it failed with EINTR: interrupted by a signal. */ -static gint -mono_getrandom (guchar *buffer, gssize buffer_size, gint flags, MonoError *error) -{ - g_assert (buffer || !buffer_size); - - static gboolean getrandom_fail; - - if (getrandom_fail) - return 0; - - /* Read until the buffer is filled. This may block if random pool isn't initialized. */ - while (buffer_size > 0) { - gssize const err = getrandom (buffer, buffer_size, flags); - if (err < 0) { - if (errno == EINTR) - continue; - if (errno == ENOSYS || errno == EPERM) { - getrandom_fail = TRUE; - return 0; - } - g_warning ("Entropy error! Error in getrandom (%s).", strerror (errno)); - /* exception will be thrown in managed code */ - mono_error_set_execution_engine (error, "Entropy error! Error in getrandom (%s).", strerror (errno)); - return -1; - } - buffer_size -= err; - buffer += err; - } - return 1; -} -#elif HAVE_GETENTROPY -/* Fill buffer with buffer_size random bytes generated by getentropy(): - - Return 1 on success - - Return 0 if getentropy() is not available (failed with ENOSYS or EPERM) - - Return -1 on error - getentropy() is retried if it failed with EINTR: interrupted by a signal. */ -static gint -mono_getentropy (guchar *buffer, gssize buffer_size, MonoError *error) -{ - g_assert (buffer || !buffer_size); - - static gboolean getentropy_fail; - - if (getentropy_fail) - return 0; - - /* Read until the buffer is filled. This may block if random pool isn't initialized. */ - while (buffer_size > 0) { - gssize const len = MIN (buffer_size, 256); - gint const err = getentropy (buffer, len); - if (err < 0) { - if (errno == EINTR) - continue; - if (errno == ENOSYS || errno == EPERM) { - getentropy_fail = TRUE; - return 0; - } - g_warning ("Entropy error! Error in getentropy (%s).", strerror (errno)); - /* exception will be thrown in managed code */ - mono_error_set_execution_engine (error, "Entropy error! Error in getentropy (%s).", strerror (errno)); - return -1; - } - buffer += len; - buffer_size -= len; - } - return 1; -} -#endif /* HAVE_GETENTROPY */ - -#if !defined(DISABLE_EGD_SOCKET) -static void -get_entropy_from_egd (const char *path, guchar *buffer, gssize buffer_size, MonoError *error) -{ - g_assert (buffer || !buffer_size); - - struct sockaddr_un egd_addr; - gint socket_fd; - gint ret; - guint offset = 0; - int err = 0; - - socket_fd = socket (PF_UNIX, SOCK_STREAM, 0); - if (socket_fd < 0) { - ret = -1; - err = errno; - } else { - egd_addr.sun_family = AF_UNIX; - memcpy (egd_addr.sun_path, path, sizeof (egd_addr.sun_path) - 1); - egd_addr.sun_path [sizeof (egd_addr.sun_path) - 1] = '\0'; - ret = connect (socket_fd, (struct sockaddr*) &egd_addr, sizeof (egd_addr)); - err = errno; - } - if (ret == -1) { - if (socket_fd >= 0) - close (socket_fd); - g_warning ("Entropy problem! Can't create or connect to egd socket %s", path); - mono_error_set_execution_engine (error, "Failed to open egd socket %s: %s", path, strerror (err)); - return; - } - - while (buffer_size > 0) { - guchar request [2]; - gint count = 0; - - /* block until daemon can return enough entropy */ - request [0] = 2; - request [1] = buffer_size < 255 ? (guchar)buffer_size : 255; - while (count < 2) { - int sent = write (socket_fd, request + count, 2 - count); - err = errno; - if (sent >= 0) { - count += sent; - } else if (err == EINTR) { - continue; - } else { - close (socket_fd); - g_warning ("Send egd request failed %d", err); - mono_error_set_execution_engine (error, "Failed to send request to egd socket: %s", strerror (err)); - return; - } - } - - count = 0; - while (count != request [1]) { - int received; - received = read (socket_fd, buffer + offset, request [1] - count); - err = errno; - if (received > 0) { - count += received; - offset += received; - } else if (received < 0 && err == EINTR) { - continue; - } else { - close (socket_fd); - g_warning ("Receive egd request failed %d", err); - mono_error_set_execution_engine (error, "Failed to get response from egd socket: %s", strerror(err)); - return; - } - } - - buffer_size -= request [1]; - } - - close (socket_fd); -} - -gboolean -mono_rand_open (void) -{ - static gint32 status; - if (status != 0 || mono_atomic_cas_i32 (&status, 1, 0) != 0) { - while (status != 2) - mono_thread_info_yield (); - return TRUE; - } - -#ifdef NAME_DEV_URANDOM - if (file < 0) - file = open (NAME_DEV_URANDOM, O_RDONLY); -#endif -#ifdef NAME_DEV_RANDOM - if (file < 0) - file = open (NAME_DEV_RANDOM, O_RDONLY); -#endif - if (file < 0) - use_egd = g_hasenv ("MONO_EGD_SOCKET"); - - status = 2; - - return TRUE; -} -#endif /* !DISABLE_EGD_SOCKET */ - -gpointer -mono_rand_init (const guchar *seed, gssize seed_size) -{ - // file < 0 is expected in the egd case - return (!use_egd && file < 0) ? (gpointer)NULL : GINT_TO_POINTER (file); -} - -gboolean -mono_rand_try_get_bytes (gpointer *handle, guchar *buffer, gssize buffer_size, MonoError *error) -{ - g_assert (buffer || !buffer_size); - g_assert (handle); - - error_init (error); - -#if defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY) -#ifdef HAVE_GETRANDOM - gint const res = mono_getrandom (buffer, buffer_size, 0, error); -#else - gint const res = mono_getentropy (buffer, buffer_size, error); -#endif - if (res < 0) - return FALSE; - - if (res == 1) - return TRUE; - -#elif !defined(DISABLE_EGD_SOCKET) - /* getrandom() or getentropy() function is not available: failed with - ENOSYS or EPERM. Fall back on reading from /dev/urandom. */ - - if (use_egd) { - char *socket_path = g_getenv ("MONO_EGD_SOCKET"); - /* exception will be thrown in managed code */ - if (socket_path == NULL) { - *handle = NULL; - return FALSE; - } - get_entropy_from_egd (socket_path, buffer, buffer_size, error); - g_free (socket_path); - return TRUE; - } -#endif - /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */ - while (buffer_size > 0) { - gssize const err = read (file, buffer, buffer_size); - if (err < 0) { - if (errno == EINTR) - continue; - g_warning("Entropy error! Error in read (%s).", strerror (errno)); - /* exception will be thrown in managed code */ - mono_error_set_execution_engine (error, "Entropy error! Error in read (%s).", strerror (errno)); - return FALSE; - } - buffer += err; - buffer_size -= err; - } - return TRUE; -} - -void -mono_rand_close (gpointer provider) -{ -} - -#else - -#include -#include - -gboolean -mono_rand_open (void) -{ - static gint32 status; - if (status != 0 || mono_atomic_cas_i32 (&status, 1, 0) != 0) { - while (status != 2) - mono_thread_info_yield (); - return TRUE; - } - - srand (time (NULL)); - - status = 2; - - return TRUE; -} - -gpointer -mono_rand_init (const guchar *seed, gssize seed_size) -{ - return "srand"; // NULL will be interpreted as failure; return arbitrary nonzero pointer -} - -gboolean -mono_rand_try_get_bytes (gpointer *handle, guchar *buffer, gssize buffer_size, MonoError *error) -{ - // This functions is not used on any mainstream platform, perhaps not at all. - - g_assert (buffer || !buffer_size); - - error_init (error); - - g_static_assert (RAND_MAX >= 0xFF); - - while (buffer_size > 0) { - int const i = rand (); - int j; - if (buffer_size >= (j = 4) && RAND_MAX >= 0xFFFFFFFF) - *(gint32*) buffer = i; - else if (buffer_size >= (j = 2) && RAND_MAX >= 0xFFFF) - *(gint16*) buffer = i; - else { - j = 1; - *(gint8*) buffer = i; - } - buffer += j; - buffer_size -= j; - } - - return TRUE; -} - -void -mono_rand_close (gpointer provider) -{ -} - -#endif - -/** - * mono_rand_try_get_uint32: - * \param handle A pointer to an RNG handle. Handle is set to NULL on failure. - * \param val A pointer to a 32-bit unsigned int, to which the result will be written. - * \param min Result will be greater than or equal to this value. - * \param max Result will be less than or equal to this value. - * Extracts one 32-bit unsigned int from an RNG handle. - * \returns FALSE on failure, TRUE on success. - */ -gboolean -mono_rand_try_get_uint32 (gpointer *handle, guint32 *val, guint32 min, guint32 max, MonoError *error) -{ - g_assert (val); - if (!mono_rand_try_get_bytes (handle, (guchar*) val, sizeof (guint32), error)) - return FALSE; - - double randomDouble = ((gdouble) *val) / ( ((double)G_MAXUINT32) + 1 ); // Range is [0,1) - *val = (guint32) (randomDouble * (max - min + 1) + min); - - g_assert (*val >= min); - g_assert (*val <= max); - - return TRUE; -} diff --git a/src/mono/mono/utils/mono-rand.h b/src/mono/mono/utils/mono-rand.h deleted file mode 100644 index ea1ff7f9d6762..0000000000000 --- a/src/mono/mono/utils/mono-rand.h +++ /dev/null @@ -1,32 +0,0 @@ -/** - * \file - */ - -#ifndef _MONO_UTILS_RAND_H_ -#define _MONO_UTILS_RAND_H_ - -#include - -#include "mono-compiler.h" -#include - -MONO_COMPONENT_API -gboolean -mono_rand_open (void); - -MONO_COMPONENT_API -gpointer -mono_rand_init (const guchar *seed, gssize seed_size); - -MONO_COMPONENT_API -gboolean -mono_rand_try_get_bytes (gpointer *handle, guchar *buffer, gssize buffer_size, MonoError *error); - -gboolean -mono_rand_try_get_uint32 (gpointer *handle, guint32 *val, guint32 min, guint32 max, MonoError *error); - -MONO_COMPONENT_API -void -mono_rand_close (gpointer handle); - -#endif /* _MONO_UTILS_RAND_H_ */ diff --git a/src/native/corehost/apphost/static/CMakeLists.txt b/src/native/corehost/apphost/static/CMakeLists.txt index 3ea4d5d043ca3..c013b3fcce0d7 100644 --- a/src/native/corehost/apphost/static/CMakeLists.txt +++ b/src/native/corehost/apphost/static/CMakeLists.txt @@ -169,7 +169,6 @@ else() System.Native-Static System.Security.Cryptography.Native.OpenSsl-Static - palrt coreclrpal_dac corguids dbgutil diff --git a/src/native/eventpipe/ds-protocol.c b/src/native/eventpipe/ds-protocol.c index 8232d7d9b9db3..d1cbee0ac3578 100644 --- a/src/native/eventpipe/ds-protocol.c +++ b/src/native/eventpipe/ds-protocol.c @@ -80,7 +80,7 @@ ds_ipc_advertise_cookie_v1_get (void) void ds_ipc_advertise_cookie_v1_init (void) { - ep_rt_create_activity_id ((uint8_t *)&_ds_ipc_advertise_cooike_v1, EP_GUID_SIZE); + ep_thread_create_activity_id ((uint8_t *)&_ds_ipc_advertise_cooike_v1, EP_GUID_SIZE); } /** diff --git a/src/native/eventpipe/ep-rt.h b/src/native/eventpipe/ep-rt.h index 2f1aef5047aab..060bb71503bef 100644 --- a/src/native/eventpipe/ep-rt.h +++ b/src/native/eventpipe/ep-rt.h @@ -270,12 +270,6 @@ static bool ep_rt_process_shutdown (void); -static -void -ep_rt_create_activity_id ( - uint8_t *activity_id, - uint32_t activity_id_len); - static bool ep_rt_is_running (void); diff --git a/src/native/eventpipe/ep-thread.h b/src/native/eventpipe/ep-thread.h index 44c683b52d57f..a4e6e3b73e422 100644 --- a/src/native/eventpipe/ep-thread.h +++ b/src/native/eventpipe/ep-thread.h @@ -114,7 +114,10 @@ ep_thread_create_activity_id ( uint8_t *activity_id, uint32_t activity_id_len) { - ep_rt_create_activity_id (activity_id, activity_id_len); + EP_ASSERT (activity_id != NULL); + EP_ASSERT (activity_id_len == EP_ACTIVITY_ID_SIZE); + + minipal_guid_v4_create ((minipal_guid_t *)(activity_id)); } static diff --git a/src/native/eventpipe/ep-types.h b/src/native/eventpipe/ep-types.h index 79b05ea8bc671..7a16bc77a3466 100644 --- a/src/native/eventpipe/ep-types.h +++ b/src/native/eventpipe/ep-types.h @@ -19,6 +19,7 @@ #include #include #include +#include /* * EventFilterDescriptor. diff --git a/src/native/minipal/CMakeLists.txt b/src/native/minipal/CMakeLists.txt index b1697004db343..1504424f3d8a7 100644 --- a/src/native/minipal/CMakeLists.txt +++ b/src/native/minipal/CMakeLists.txt @@ -2,8 +2,10 @@ include(configure.cmake) set(SOURCES cpufeatures.c + guid.c random.c debugger.c + strings.c time.c unicodedata.c utf8.c @@ -14,12 +16,20 @@ include_directories(${CLR_SRC_NATIVE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) add_library(minipal_objects OBJECT ${SOURCES}) -# Add a copy of the minipal static library with interprocedural optimization disabled +if (WIN32) + target_link_libraries(minipal_objects PUBLIC bcrypt) +endif() + +# Add a copy of the minipal object library with interprocedural optimization disabled # for NativeAOT scenarios, where we ship static libraries that need to be able to be run with # a variety of toolchain versions, not only the exact one we built with. add_library(aotminipal STATIC ${SOURCES}) set_target_properties(aotminipal PROPERTIES INTERPROCEDURAL_OPTIMIZATION OFF) +if (WIN32) + target_link_libraries(aotminipal PUBLIC bcrypt) +endif() + # Provide a static library for our shared library and executable scenarios # for easier usability. add_library(minipal STATIC) diff --git a/src/native/minipal/com/memory.h b/src/native/minipal/com/memory.h new file mode 100644 index 0000000000000..1a869f50acec4 --- /dev/null +++ b/src/native/minipal/com/memory.h @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#ifndef MINIPAL_COM_MEMORY_H +#define MINIPAL_COM_MEMORY_H + +#include + +#ifdef __cplusplus + extern "C" + { +#endif // __cplusplus + +#ifndef HOST_WINDOWS +inline void* CoTaskMemAlloc(size_t cb) +{ + return malloc(cb); +} + +inline void CoTaskMemFree(void* pv) +{ + free(pv); +} +#endif + +#ifdef __cplusplus + } +#endif // __cplusplus + +#endif // MINIPAL_COM_MEMORY_H diff --git a/src/native/minipal/guid.c b/src/native/minipal/guid.c new file mode 100644 index 0000000000000..8002e323c4078 --- /dev/null +++ b/src/native/minipal/guid.c @@ -0,0 +1,62 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include "guid.h" +#include "random.h" +#include +#ifdef HOST_WINDOWS +#include +#endif + +// See RFC-4122 section 4.4 on creation of random GUID. +// https://www.ietf.org/rfc/rfc4122.txt +// +// The version 4 UUID is meant for generating UUIDs from truly-random or +// pseudo-random numbers. +// +// The algorithm is as follows: +// +// o Set the two most significant bits (bits 6 and 7) of the +// clock_seq_hi_and_reserved to zero and one, respectively. +// +// o Set the four most significant bits (bits 12 through 15) of the +// time_hi_and_version field to the 4-bit version number from +// Section 4.1.3. +// +// o Set all the other bits to randomly (or pseudo-randomly) chosen +// values. +// +bool minipal_guid_v4_create(minipal_guid_t* guid) +{ +#ifdef HOST_WINDOWS + // Windows has a built-in function for creating v4 GUIDs. + return SUCCEEDED(CoCreateGuid((GUID*)guid)); +#else + // Technically, v4 GUIDs don't require cryptographically secure random bytes; + // however, CoCreateGuid provides that guarantee and we want to ensure + // that guarantee is maintained as customers have taken a dependency on it. + if (minipal_get_cryptographically_secure_random_bytes((uint8_t*)guid, sizeof(*guid)) != 0) + return false; + + { + // time_hi_and_version + const uint16_t mask = 0xf000; // b1111000000000000 + const uint16_t value = 0x4000; // b0100000000000000 + guid->data3 = (guid->data3 & ~mask) | value; + } + + { + // clock_seq_hi_and_reserved + const uint8_t mask = 0xc0; // b11000000 + const uint8_t value = 0x80; // b10000000 + guid->data4[0] = (guid->data4[0] & ~mask) | value; + } + + return true; +#endif +} + +bool minipal_guid_equals(minipal_guid_t const* g1, minipal_guid_t const* g2) +{ + return memcmp(g1, g2, sizeof(minipal_guid_t)) == 0; +} diff --git a/src/native/minipal/guid.h b/src/native/minipal/guid.h new file mode 100644 index 0000000000000..ef7dcaaadca7b --- /dev/null +++ b/src/native/minipal/guid.h @@ -0,0 +1,50 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#ifndef MINIPAL_GUID_H +#define MINIPAL_GUID_H + +#include +#include + +#ifdef __cplusplus + extern "C" + { +#endif // __cplusplus + +typedef struct minipal_guid__ +{ + uint32_t data1; + uint16_t data2; + uint16_t data3; + uint8_t data4[8]; +} minipal_guid_t; + +bool minipal_guid_v4_create(minipal_guid_t* guid); + +bool minipal_guid_equals(minipal_guid_t const* g1, minipal_guid_t const* g2); + +#ifdef __cplusplus + } +#endif // __cplusplus + +#ifdef __cplusplus +inline bool operator==(minipal_guid_t const& a, minipal_guid_t const& b) +{ + return minipal_guid_equals(&a, &b); +} + +inline bool operator!=(minipal_guid_t const& a, minipal_guid_t const& b) +{ + return !(a == b); +} + +template +bool minipal_guid_v4_create(T* guid) +{ + static_assert(sizeof(T) == sizeof(minipal_guid_t), "minipal_guid_t size mismatch"); + return minipal_guid_v4_create(reinterpret_cast(guid)); +} +#endif + +#endif // MINIPAL_GUID_H diff --git a/src/native/minipal/strings.c b/src/native/minipal/strings.c new file mode 100644 index 0000000000000..528c483c2beb1 --- /dev/null +++ b/src/native/minipal/strings.c @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include "strings.h" + +#ifdef HOST_WINDOWS +#include +#endif + +size_t minipal_u16_strlen(const CHAR16_T* str) +{ +#ifdef HOST_WINDOWS + return wcslen((const wchar_t*)str); +#else + size_t len = 0; + while (*str++) + { + len++; + } + return len; +#endif +} diff --git a/src/native/minipal/strings.h b/src/native/minipal/strings.h index 250dada9f539c..ebeea097d6f07 100644 --- a/src/native/minipal/strings.h +++ b/src/native/minipal/strings.h @@ -27,6 +27,14 @@ CHAR16_T minipal_toupper_invariant(CHAR16_T code); */ CHAR16_T minipal_tolower_invariant(CHAR16_T code); +/** + * @brief Get the length of a null-terminated UTF-16 string. + * + * @param str The null-terminated UTF-16 string. + * @return The length of the string. + */ +size_t minipal_u16_strlen(const CHAR16_T* str); + #ifdef __cplusplus } #endif // __cplusplus