From 8a13147d2fca4c539f7254f4c9c17fcbb8f01d7b Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 16 Oct 2024 15:05:07 -0700 Subject: [PATCH 01/15] Remove PAL_Random --- .../dlls/mscordac/mscordac_unixexports.src | 1 - src/coreclr/pal/inc/pal.h | 7 - src/coreclr/pal/src/CMakeLists.txt | 1 - src/coreclr/pal/src/misc/miscpalapi.cpp | 131 ------------------ src/coreclr/palrt/guid.cpp | 6 +- .../superpmi/superpmi-shared/spmiutil.cpp | 10 +- .../superpmi/superpmi/parallelsuperpmi.cpp | 7 +- 7 files changed, 10 insertions(+), 153 deletions(-) delete mode 100644 src/coreclr/pal/src/misc/miscpalapi.cpp diff --git a/src/coreclr/dlls/mscordac/mscordac_unixexports.src b/src/coreclr/dlls/mscordac/mscordac_unixexports.src index b7fe8d95f5b79..f40151ef8d9bd 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 diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index 8d768044f119a..87198582666a6 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -359,13 +359,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/src/CMakeLists.txt b/src/coreclr/pal/src/CMakeLists.txt index db5d430ed011b..680332b0843ab 100644 --- a/src/coreclr/pal/src/CMakeLists.txt +++ b/src/coreclr/pal/src/CMakeLists.txt @@ -158,7 +158,6 @@ set(SOURCES misc/error.cpp misc/errorstrings.cpp misc/fmtmessage.cpp - misc/miscpalapi.cpp misc/perfjitdump.cpp misc/strutil.cpp misc/sysinfo.cpp 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/guid.cpp b/src/coreclr/palrt/guid.cpp index 68cc157d91ca7..d1de2a4e49a4d 100644 --- a/src/coreclr/palrt/guid.cpp +++ b/src/coreclr/palrt/guid.cpp @@ -10,6 +10,7 @@ // =========================================================================== #define INITGUID +#include #include // These are GUIDs and IIDs that would normally be provided by the system via uuid.lib, @@ -28,7 +29,10 @@ DEFINE_GUID(IID_IStream, 0x0000000c, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x0 STDAPI CoCreateGuid(OUT GUID * pguid) { - PAL_Random(pguid, sizeof(GUID)); + if (!minipal_get_cryptographically_secure_random_bytes(pguid, sizeof(GUID))) + { + return E_FAIL; + } static const USHORT VersionMask = 0xF000; static const USHORT RandomGuidVersion = 0x4000; 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/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++) { From f96014a4378d26e8a5f4591af74074d3b4975190 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 16 Oct 2024 11:58:15 -0700 Subject: [PATCH 02/15] Move the "COM minipal" APIs into the regular repo minipal and update the CoreCLR PAL to also use it. Also remove dangling references to DNCP and remove dead standalone-DNMD infra --- src/coreclr/minipal/Unix/dn-u16.cpp | 8 ++- src/coreclr/palrt/CMakeLists.txt | 3 +- src/coreclr/palrt/comem.cpp | 5 +- src/coreclr/palrt/guid.cpp | 33 ++----------- src/native/minipal/CMakeLists.txt | 3 ++ src/native/minipal/guid.c | 77 +++++++++++++++++++++++++++++ src/native/minipal/guid.h | 43 ++++++++++++++++ src/native/minipal/memory.c | 40 +++++++++++++++ src/native/minipal/memory.h | 15 ++++++ src/native/minipal/strings.c | 14 ++++++ src/native/minipal/strings.h | 8 +++ 11 files changed, 212 insertions(+), 37 deletions(-) create mode 100644 src/native/minipal/guid.c create mode 100644 src/native/minipal/guid.h create mode 100644 src/native/minipal/memory.c create mode 100644 src/native/minipal/memory.h create mode 100644 src/native/minipal/strings.c 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/palrt/CMakeLists.txt b/src/coreclr/palrt/CMakeLists.txt index eda33fe9e0592..05a604fc3fa9d 100644 --- a/src/coreclr/palrt/CMakeLists.txt +++ b/src/coreclr/palrt/CMakeLists.txt @@ -11,5 +11,4 @@ add_library_clr(palrt ${PALRT_SOURCES} ) -# Install the static PAL library for VS -install_clr(TARGETS palrt DESTINATIONS lib) +target_link_libraries(palrt PUBLIC minipal) diff --git a/src/coreclr/palrt/comem.cpp b/src/coreclr/palrt/comem.cpp index e56e720cc80c2..321dd19a3469b 100644 --- a/src/coreclr/palrt/comem.cpp +++ b/src/coreclr/palrt/comem.cpp @@ -9,13 +9,14 @@ // =========================================================================== #include "common.h" +#include STDAPI_(LPVOID) CoTaskMemAlloc(SIZE_T cb) { - return malloc(cb); + return minipal_co_task_mem_alloc(cb); } STDAPI_(void) CoTaskMemFree(LPVOID pv) { - free(pv); + minipal_co_task_mem_free(pv); } diff --git a/src/coreclr/palrt/guid.cpp b/src/coreclr/palrt/guid.cpp index d1de2a4e49a4d..4f1ad4fefd33b 100644 --- a/src/coreclr/palrt/guid.cpp +++ b/src/coreclr/palrt/guid.cpp @@ -9,43 +9,20 @@ // PALRT guids // =========================================================================== -#define INITGUID -#include #include +#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) { - if (!minipal_get_cryptographically_secure_random_bytes(pguid, sizeof(GUID))) + minipal_guid_t guid; + if (!minipal_guid_v4_create(&guid)) { return E_FAIL; } - 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; + static_assert(sizeof(GUID) == sizeof(minipal_guid_t), "GUID and minipal_guid_t must be the same size"); + memcpy(pguid, &guid, sizeof(GUID)); return S_OK; } diff --git a/src/native/minipal/CMakeLists.txt b/src/native/minipal/CMakeLists.txt index 56a6c8de10cb4..9af402eb34e46 100644 --- a/src/native/minipal/CMakeLists.txt +++ b/src/native/minipal/CMakeLists.txt @@ -2,8 +2,11 @@ include(configure.cmake) set(SOURCES cpufeatures.c + guid.c + memory.c random.c debugger.c + strings.c time.c unicodedata.c utf8.c diff --git a/src/native/minipal/guid.c b/src/native/minipal/guid.c new file mode 100644 index 0000000000000..5e1237c4d96a9 --- /dev/null +++ b/src/native/minipal/guid.c @@ -0,0 +1,77 @@ +// 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 + +// Define some well-known GUIDs on non-Windows platforms. +#ifndef HOST_WINDOWS +// 00000000-0000-0000-0000-000000000000 +minipal_guid_t const GUID_NULL = { 0x0, 0x0, 0x0, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }; + +// 00000000-0000-0000-C000-000000000046 +minipal_guid_t const IID_IUnknown = { 0x0, 0x0, 0x0, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } }; + +// 00000001-0000-0000-C000-000000000046 +minipal_guid_t const IID_IClassFactory = { 0x1, 0x0, 0x0, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } }; + +// 0c733a30-2a1c-11ce-ade5-00aa0044773d +minipal_guid_t const IID_ISequentialStream = { 0x0c733a30, 0x2a1c, 0x11ce, { 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d } }; + +// 0000000C-0000-0000-C000-000000000046 +minipal_guid_t const IID_IStream = { 0xC, 0x0, 0x0, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } }; +#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 + 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..77692aef9ff30 --- /dev/null +++ b/src/native/minipal/guid.h @@ -0,0 +1,43 @@ +// 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); +} +#endif + +#endif // MINIPAL_GUID_H diff --git a/src/native/minipal/memory.c b/src/native/minipal/memory.c new file mode 100644 index 0000000000000..5e147de558f8b --- /dev/null +++ b/src/native/minipal/memory.c @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include "memory.h" + +#ifdef HOST_WINDOWS +#include + +void* minipal_co_task_mem_alloc(size_t cb) +{ + return CoTaskMemAlloc(cb); +} + +void minipal_co_task_mem_free(void* pv) +{ + CoTaskMemFree(pv); +} +#else +// CoTaskMemAlloc always aligns on an 8-byte boundary. +#define ALIGN 8 + +void* minipal_co_task_mem_alloc(size_t cb) +{ + // Ensure malloc always allocates. + if (cb == 0) + cb = ALIGN; + + // Align the allocation size. + size_t cb_safe = (cb + (ALIGN - 1)) & ~(ALIGN - 1); + if (cb_safe < cb) // Overflow + return NULL; + + return aligned_alloc(ALIGN, cb_safe); +} + +void minipal_co_task_mem_free(void* pv) +{ + free(pv); +} +#endif diff --git a/src/native/minipal/memory.h b/src/native/minipal/memory.h new file mode 100644 index 0000000000000..aac7773c74270 --- /dev/null +++ b/src/native/minipal/memory.h @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#ifndef MINIPAL_MEMORY_H +#define MINIPAL_MEMORY_H + +#include + +// Allocate memory on the platform equivalent of the CoTaskMem heap. +void* minipal_co_task_mem_alloc(size_t cb); + +// Free memory allocated on the platform equivalent of the CoTaskMem heap. +void minipal_co_task_mem_free(void* pv); + +#endif // MINIPAL_MEMORY_H diff --git a/src/native/minipal/strings.c b/src/native/minipal/strings.c new file mode 100644 index 0000000000000..1711a9d07cae9 --- /dev/null +++ b/src/native/minipal/strings.c @@ -0,0 +1,14 @@ +// 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" + +size_t minipal_u16_strlen(const CHAR16_T* str) +{ + size_t len = 0; + while (*str++) + { + len++; + } + return len; +} 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 From 5ccbb532c9a32e46f61a4a90766b9a2ebbf3694d Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 16 Oct 2024 13:43:02 -0700 Subject: [PATCH 03/15] Remove direct CoCreateGuid usage from CoreCLR and instead use the minipal --- src/coreclr/debug/inc/dbgtransportsession.h | 6 ++-- .../debug/shared/dbgtransportsession.cpp | 5 ++-- src/coreclr/ilasm/assem.cpp | 9 ------ src/coreclr/ilasm/portable_pdb.cpp | 10 ++++--- src/coreclr/md/compiler/regmeta.cpp | 3 +- src/coreclr/md/enc/metamodelrw.cpp | 3 +- src/coreclr/md/inc/portablepdbmdds.h | 1 + src/coreclr/pal/inc/rt/palrt.h | 3 -- src/coreclr/palrt/CMakeLists.txt | 1 - src/coreclr/palrt/guid.cpp | 28 ------------------- .../vm/eventing/eventpipe/ep-rt-coreclr.h | 3 +- src/coreclr/vm/eventpipeinternal.cpp | 6 ++-- 12 files changed, 23 insertions(+), 55 deletions(-) delete mode 100644 src/coreclr/palrt/guid.cpp 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/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/md/compiler/regmeta.cpp b/src/coreclr/md/compiler/regmeta.cpp index 85130f4cd4019..eee9a527f04c8 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(reinterpret_cast(&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..50f5c95a0dcd2 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(reinterpret_cast(&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/pal/inc/rt/palrt.h b/src/coreclr/pal/inc/rt/palrt.h index 05c04a21ca671..f0d1af00100ed 100644 --- a/src/coreclr/pal/inc/rt/palrt.h +++ b/src/coreclr/pal/inc/rt/palrt.h @@ -1093,9 +1093,6 @@ EXTERN_C HRESULT PALAPI PAL_CoCreateInstance(REFCLSID rclsid, // 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/palrt/CMakeLists.txt b/src/coreclr/palrt/CMakeLists.txt index 05a604fc3fa9d..bd28f446d710c 100644 --- a/src/coreclr/palrt/CMakeLists.txt +++ b/src/coreclr/palrt/CMakeLists.txt @@ -3,7 +3,6 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) set(PALRT_SOURCES comem.cpp - guid.cpp ) add_library_clr(palrt diff --git a/src/coreclr/palrt/guid.cpp b/src/coreclr/palrt/guid.cpp deleted file mode 100644 index 4f1ad4fefd33b..0000000000000 --- a/src/coreclr/palrt/guid.cpp +++ /dev/null @@ -1,28 +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 -// =========================================================================== - -#include -#include - -STDAPI -CoCreateGuid(OUT GUID * pguid) -{ - minipal_guid_t guid; - if (!minipal_guid_v4_create(&guid)) - { - return E_FAIL; - } - - static_assert(sizeof(GUID) == sizeof(minipal_guid_t), "GUID and minipal_guid_t must be the same size"); - memcpy(pguid, &guid, sizeof(GUID)); - - return S_OK; -} diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index 7f66adf7df631..767e23adfd0d7 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -17,6 +17,7 @@ #include "typestring.h" #include "clrversion.h" #include "hostinformation.h" +#include #undef EP_INFINITE_WAIT #define EP_INFINITE_WAIT INFINITE @@ -748,7 +749,7 @@ ep_rt_create_activity_id ( EP_ASSERT (activity_id != NULL); EP_ASSERT (activity_id_len == EP_ACTIVITY_ID_SIZE); - CoCreateGuid (reinterpret_cast(activity_id)); + minipal_guid_v4_create (reinterpret_cast(activity_id)); } static diff --git a/src/coreclr/vm/eventpipeinternal.cpp b/src/coreclr/vm/eventpipeinternal.cpp index 9d00e84800151..be22b72be47a3 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(reinterpret_cast(¤tActivityId)); pThread->SetActivityId(¤tActivityId); break; From cbb3c4a9022e61d82fa7dd4074dfc6ae303e2aff Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 16 Oct 2024 14:48:03 -0700 Subject: [PATCH 04/15] Remove CoTaskMemAlloc/Free usage from xplat code (and instead use the minipal implementation) --- src/coreclr/debug/di/module.cpp | 2 +- src/coreclr/dlls/mscorpe/pewriter.cpp | 4 ++-- src/coreclr/inc/holder.h | 3 ++- src/coreclr/md/enc/stgio.cpp | 2 +- src/coreclr/pal/inc/rt/palrt.h | 4 ---- src/coreclr/palrt/CMakeLists.txt | 13 ------------- src/coreclr/palrt/comem.cpp | 22 ---------------------- src/coreclr/vm/gdbjit.cpp | 14 +++++++------- src/coreclr/vm/ilmarshalers.cpp | 2 +- src/coreclr/vm/olevariant.cpp | 10 +++++----- src/native/minipal/memory.h | 9 +++++++++ 11 files changed, 28 insertions(+), 57 deletions(-) delete mode 100644 src/coreclr/palrt/comem.cpp diff --git a/src/coreclr/debug/di/module.cpp b/src/coreclr/debug/di/module.cpp index 02986839e454b..d3ab476877b6e 100644 --- a/src/coreclr/debug/di/module.cpp +++ b/src/coreclr/debug/di/module.cpp @@ -1101,7 +1101,7 @@ void CordbModule::CopyRemoteMetaData( // Allocate space for the local copy of the metadata // No need to zero out the memory since we'll fill it all here. - LPVOID pRawBuffer = CoTaskMemAlloc(buffer.cbSize); + LPVOID pRawBuffer = minipal_co_task_mem_alloc(buffer.cbSize); if (pRawBuffer == NULL) { ThrowOutOfMemory(); diff --git a/src/coreclr/dlls/mscorpe/pewriter.cpp b/src/coreclr/dlls/mscorpe/pewriter.cpp index 5e22e628b368c..5de2dd6685dc3 100644 --- a/src/coreclr/dlls/mscorpe/pewriter.cpp +++ b/src/coreclr/dlls/mscorpe/pewriter.cpp @@ -1593,7 +1593,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 *) ::minipal_co_task_mem_alloc(lSize); if (NULL == pImage) { return E_OUTOFMEMORY; @@ -1626,7 +1626,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 ::minipal_co_task_mem_free()) *ppImage = pImage; // all done diff --git a/src/coreclr/inc/holder.h b/src/coreclr/inc/holder.h index 353d7f39d5004..7afd527bb1281 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 @@ -969,7 +970,7 @@ template FORCEINLINE void DeleteCoTaskMem(TYPE *value) { if (value) - CoTaskMemFree(value); + minipal_co_task_mem_free(value); } template diff --git a/src/coreclr/md/enc/stgio.cpp b/src/coreclr/md/enc/stgio.cpp index aeba5e59f4735..0297478cfb69f 100644 --- a/src/coreclr/md/enc/stgio.cpp +++ b/src/coreclr/md/enc/stgio.cpp @@ -339,7 +339,7 @@ void StgIO::Close() case STGIO_SHAREDMEM: if (m_pBaseData != NULL) { - CoTaskMemFree(m_pBaseData); + minipal_co_task_mem_free(m_pBaseData); m_pBaseData = NULL; break; } diff --git a/src/coreclr/pal/inc/rt/palrt.h b/src/coreclr/pal/inc/rt/palrt.h index f0d1af00100ed..61a4ab379666b 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) diff --git a/src/coreclr/palrt/CMakeLists.txt b/src/coreclr/palrt/CMakeLists.txt index bd28f446d710c..e69de29bb2d1d 100644 --- a/src/coreclr/palrt/CMakeLists.txt +++ b/src/coreclr/palrt/CMakeLists.txt @@ -1,13 +0,0 @@ - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(PALRT_SOURCES - comem.cpp -) - -add_library_clr(palrt - STATIC - ${PALRT_SOURCES} -) - -target_link_libraries(palrt PUBLIC minipal) diff --git a/src/coreclr/palrt/comem.cpp b/src/coreclr/palrt/comem.cpp deleted file mode 100644 index 321dd19a3469b..0000000000000 --- a/src/coreclr/palrt/comem.cpp +++ /dev/null @@ -1,22 +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" -#include - -STDAPI_(LPVOID) CoTaskMemAlloc(SIZE_T cb) -{ - return minipal_co_task_mem_alloc(cb); -} - -STDAPI_(void) CoTaskMemFree(LPVOID pv) -{ - minipal_co_task_mem_free(pv); -} diff --git a/src/coreclr/vm/gdbjit.cpp b/src/coreclr/vm/gdbjit.cpp index 2a376726e5285..672cbdb030093 100644 --- a/src/coreclr/vm/gdbjit.cpp +++ b/src/coreclr/vm/gdbjit.cpp @@ -452,7 +452,7 @@ HRESULT FunctionMember::GetLocalsDebugInfo(NotifyGdb::PTK_TypeInfoMap pTypeMap, MethodDebugInfo::MethodDebugInfo(int numPoints, int numLocals) { - points = (SequencePointInfo*) CoTaskMemAlloc(sizeof(SequencePointInfo) * numPoints); + points = (SequencePointInfo*) minipal_co_task_mem_alloc(sizeof(SequencePointInfo) * numPoints); if (points == nullptr) { COMPlusThrowOM(); @@ -467,10 +467,10 @@ MethodDebugInfo::MethodDebugInfo(int numPoints, int numLocals) return; } - locals = (LocalVarInfo*) CoTaskMemAlloc(sizeof(LocalVarInfo) * numLocals); + locals = (LocalVarInfo*) minipal_co_task_mem_alloc(sizeof(LocalVarInfo) * numLocals); if (locals == nullptr) { - CoTaskMemFree(points); + minipal_co_task_mem_free(points); COMPlusThrowOM(); } memset(locals, 0, sizeof(LocalVarInfo) * numLocals); @@ -482,13 +482,13 @@ MethodDebugInfo::~MethodDebugInfo() if (locals) { for (int i = 0; i < localsSize; i++) - CoTaskMemFree(locals[i].name); - CoTaskMemFree(locals); + minipal_co_task_mem_free(locals[i].name); + minipal_co_task_mem_free(locals); } for (int i = 0; i < size; i++) - CoTaskMemFree(points[i].fileName); - CoTaskMemFree(points); + minipal_co_task_mem_free(points[i].fileName); + minipal_co_task_mem_free(points); } /* Get mapping of IL offsets to source line numbers */ diff --git a/src/coreclr/vm/ilmarshalers.cpp b/src/coreclr/vm/ilmarshalers.cpp index aa9b3d6531553..1726c63ece42a 100644 --- a/src/coreclr/vm/ilmarshalers.cpp +++ b/src/coreclr/vm/ilmarshalers.cpp @@ -4302,7 +4302,7 @@ extern "C" void QCALLTYPE MngdNativeArrayMarshaler_ConvertSpaceToNative(MngdNati if ( (!ClrSafeInt::multiply(cElements, cbElement, cbArray)) || cbArray > MAX_SIZE_FOR_INTEROP) COMPlusThrow(kArgumentException, IDS_EE_STRUCTARRAYTOOLARGE); - *pNativeHome = CoTaskMemAlloc(cbArray); + *pNativeHome = minipal_co_task_mem_alloc(cbArray); if (*pNativeHome == NULL) ThrowOutOfMemory(); diff --git a/src/coreclr/vm/olevariant.cpp b/src/coreclr/vm/olevariant.cpp index ab42391080e03..82072d170e5fb 100644 --- a/src/coreclr/vm/olevariant.cpp +++ b/src/coreclr/vm/olevariant.cpp @@ -1657,7 +1657,7 @@ void OleVariant::MarshalLPWSTRRArrayComToOle(BASEARRAYREF *pComArray, void *oleA // Allocate the string using CoTaskMemAlloc. { GCX_PREEMP(); - lpwstr = (LPWSTR)CoTaskMemAlloc(allocLength); + lpwstr = (LPWSTR)minipal_co_task_mem_alloc(allocLength); } if (lpwstr == NULL) ThrowOutOfMemory(); @@ -1694,7 +1694,7 @@ void OleVariant::ClearLPWSTRArray(void *oleArray, SIZE_T cElements, MethodTable LPWSTR lpwstr = *pOle++; if (lpwstr != NULL) - CoTaskMemFree(lpwstr); + minipal_co_task_mem_free(lpwstr); } } @@ -1801,7 +1801,7 @@ void OleVariant::MarshalLPSTRRArrayComToOle(BASEARRAYREF *pComArray, void *oleAr // Allocate the string using CoTaskMemAlloc. { GCX_PREEMP(); - lpstr = (LPSTR)CoTaskMemAlloc(allocLength); + lpstr = (LPSTR)minipal_co_task_mem_alloc(allocLength); } if (lpstr == NULL) ThrowOutOfMemory(); @@ -1840,7 +1840,7 @@ void OleVariant::ClearLPSTRArray(void *oleArray, SIZE_T cElements, MethodTable * LPSTR lpstr = *pOle++; if (lpstr != NULL) - CoTaskMemFree(lpstr); + minipal_co_task_mem_free(lpstr); } } @@ -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/native/minipal/memory.h b/src/native/minipal/memory.h index aac7773c74270..258a3938a8eb6 100644 --- a/src/native/minipal/memory.h +++ b/src/native/minipal/memory.h @@ -6,10 +6,19 @@ #include +#ifdef __cplusplus + extern "C" + { +#endif // __cplusplus + // Allocate memory on the platform equivalent of the CoTaskMem heap. void* minipal_co_task_mem_alloc(size_t cb); // Free memory allocated on the platform equivalent of the CoTaskMem heap. void minipal_co_task_mem_free(void* pv); +#ifdef __cplusplus + } +#endif // __cplusplus + #endif // MINIPAL_MEMORY_H From 995c85889c0356f2add87184bebe6664a0af900b Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 11:11:50 -0700 Subject: [PATCH 05/15] palrt is empty, so remove it. --- src/coreclr/CMakeLists.txt | 4 ---- src/coreclr/dlls/mscordac/CMakeLists.txt | 1 - src/coreclr/dlls/mscordbi/CMakeLists.txt | 1 - .../dlls/mscoree/coreclr/CMakeLists.txt | 1 - src/coreclr/ilasm/CMakeLists.txt | 2 -- src/coreclr/ildasm/exe/CMakeLists.txt | 2 -- src/coreclr/pal/inc/rt/palrt.h | 24 ------------------- src/coreclr/palrt/CMakeLists.txt | 0 src/coreclr/palrt/common.h | 17 ------------- src/coreclr/tools/superpmi/mcs/CMakeLists.txt | 1 - .../superpmi-shim-collector/CMakeLists.txt | 1 - .../superpmi-shim-counter/CMakeLists.txt | 1 - .../superpmi-shim-simple/CMakeLists.txt | 1 - .../tools/superpmi/superpmi/CMakeLists.txt | 1 - .../corehost/apphost/static/CMakeLists.txt | 1 - 15 files changed, 58 deletions(-) delete mode 100644 src/coreclr/palrt/CMakeLists.txt delete mode 100644 src/coreclr/palrt/common.h 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/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/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/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/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/pal/inc/rt/palrt.h b/src/coreclr/pal/inc/rt/palrt.h index 61a4ab379666b..9317654958b63 100644 --- a/src/coreclr/pal/inc/rt/palrt.h +++ b/src/coreclr/pal/inc/rt/palrt.h @@ -1065,30 +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) - /************** Byte swapping & unaligned access ******************/ #include diff --git a/src/coreclr/palrt/CMakeLists.txt b/src/coreclr/palrt/CMakeLists.txt deleted file mode 100644 index e69de29bb2d1d..0000000000000 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/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-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/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 From 75c421d0a47cd4dea5282f0fe5540a5ecbf89d26 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 15:12:47 -0700 Subject: [PATCH 06/15] Use minipal_u16_strlen in more places. --- src/coreclr/debug/createdump/crashinfo.cpp | 2 +- src/coreclr/debug/createdump/createdumppal.cpp | 8 -------- src/coreclr/pal/src/locale/unicode.cpp | 2 +- src/mono/mono/eglib/giconv.c | 16 +++------------- 4 files changed, 5 insertions(+), 23 deletions(-) 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/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/pal/src/locale/unicode.cpp b/src/coreclr/pal/src/locale/unicode.cpp index e966e22821ba0..2c6cf3e55bba9 100644 --- a/src/coreclr/pal/src/locale/unicode.cpp +++ b/src/coreclr/pal/src/locale/unicode.cpp @@ -202,7 +202,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/mono/mono/eglib/giconv.c b/src/mono/mono/eglib/giconv.c index 76743ed5c47c1..fe4d522326bb0 100644 --- a/src/mono/mono/eglib/giconv.c +++ b/src/mono/mono/eglib/giconv.c @@ -304,11 +304,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 = minipal_u16_strlen (str) + 1; } glong ret = (glong)minipal_get_length_utf16_to_utf8 (str, len, flags); @@ -349,11 +345,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 = minipal_u16_strlen (str) + 1; } glong ret = (glong)minipal_get_length_utf16_to_utf8 (str, len, 0); @@ -391,9 +383,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 = minipal_u16_strlen (str); } inptr = (char *) str; From 4ab9fdf29bab5364556ebf7026f88b43b508b0de Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 15:20:26 -0700 Subject: [PATCH 07/15] Use minipal's random APIs instead of Mono having their own --- src/mono/CMakeLists.txt | 2 +- src/mono/mono/eventpipe/ep-rt-mono.c | 12 +- src/mono/mono/eventpipe/ep-rt-mono.h | 1 - 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 -- 8 files changed, 6 insertions(+), 539 deletions(-) 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 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/eventpipe/ep-rt-mono.c b/src/mono/mono/eventpipe/ep-rt-mono.c index c75bb43007630..b23ea3f5b5ea3 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.c +++ b/src/mono/mono/eventpipe/ep-rt-mono.c @@ -10,10 +10,10 @@ #include #include #include -#include #include #include #include +#include #include #include @@ -29,9 +29,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}; @@ -67,10 +64,7 @@ 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); + return minipal_get_cryptographically_secure_random_bytes(buffer, buffer_size); } char * @@ -816,8 +810,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..19238cc6bc858 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 diff --git a/src/mono/mono/mini/aot-compiler.c b/src/mono/mono/mini/aot-compiler.c index df45813a2e30d..d8eb65c76c5b4 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); + bool success = minipal_get_cryptographically_secure_random_bytes (aotid, 16); + g_assert (success); } static void diff --git a/src/mono/mono/utils/CMakeLists.txt b/src/mono/mono/utils/CMakeLists.txt index 2409e92ae5e8e..ca7f65b7b81c1 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_ */ From d44224df400a58db5f92c3608f801b8194a7d17b Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 15:28:55 -0700 Subject: [PATCH 08/15] fixup! Use minipal_u16_strlen in more places. --- src/coreclr/debug/createdump/createdump.h | 1 + src/coreclr/debug/createdump/datatarget.cpp | 2 +- src/coreclr/pal/src/locale/unicode.cpp | 1 + src/mono/mono/eglib/giconv.c | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) 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/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/pal/src/locale/unicode.cpp b/src/coreclr/pal/src/locale/unicode.cpp index 2c6cf3e55bba9..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" diff --git a/src/mono/mono/eglib/giconv.c b/src/mono/mono/eglib/giconv.c index fe4d522326bb0..95cf6ac3505dd 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 From 2b4779be9c758f5c22d8d9c660ae7c42c4a4a4da Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 15:38:14 -0700 Subject: [PATCH 09/15] Remove 'random' usage in eventpipe and unify all usages of the minipal guid API instead of having three separate implementation of the exact same function body --- .../nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 40 ------------------- .../nativeaot/Runtime/eventpipe/ep-rt-aot.h | 11 ----- .../nativeaot/Runtime/eventpipeinternal.cpp | 4 +- .../vm/eventing/eventpipe/ep-rt-coreclr.h | 14 ------- src/mono/mono/eventpipe/ep-rt-mono.c | 9 ----- src/mono/mono/eventpipe/ep-rt-mono.h | 37 ----------------- 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 + 10 files changed, 8 insertions(+), 121 deletions(-) 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 3febb68b73e78..b08e99e59ea96 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/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index 767e23adfd0d7..4efbb9f8964b2 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -738,20 +738,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); - - minipal_guid_v4_create (reinterpret_cast(activity_id)); -} - static inline bool diff --git a/src/mono/mono/eventpipe/ep-rt-mono.c b/src/mono/mono/eventpipe/ep-rt-mono.c index b23ea3f5b5ea3..f07f5c6d26341 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.c +++ b/src/mono/mono/eventpipe/ep-rt-mono.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include @@ -59,14 +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) -{ - return minipal_get_cryptographically_secure_random_bytes(buffer, buffer_size); -} - char * ep_rt_mono_get_managed_cmd_line (void) { diff --git a/src/mono/mono/eventpipe/ep-rt-mono.h b/src/mono/mono/eventpipe/ep-rt-mono.h index 19238cc6bc858..94ca5bb94f7e5 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.h +++ b/src/mono/mono/eventpipe/ep-rt-mono.h @@ -69,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); @@ -772,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/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..8a60db2e12ffc 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 (reinterpret_cast(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. From 40077d524747d92b3cd9ec358491633110a1e8e3 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 15:42:41 -0700 Subject: [PATCH 10/15] PR feedback --- src/coreclr/dlls/mscorpe/pewriter.cpp | 4 ++-- src/coreclr/md/compiler/regmeta.cpp | 2 +- src/coreclr/md/enc/metamodelrw.cpp | 2 +- src/coreclr/vm/eventpipeinternal.cpp | 2 +- src/native/minipal/guid.h | 7 +++++++ 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/coreclr/dlls/mscorpe/pewriter.cpp b/src/coreclr/dlls/mscorpe/pewriter.cpp index 5de2dd6685dc3..0e5c3526d7813 100644 --- a/src/coreclr/dlls/mscorpe/pewriter.cpp +++ b/src/coreclr/dlls/mscorpe/pewriter.cpp @@ -1593,7 +1593,7 @@ HRESULT PEWriter::write(void ** ppImage) size_t lSize = filePos; // allocate the block we are handing back to the caller - void * pImage = (void *) ::minipal_co_task_mem_alloc(lSize); + void * pImage = (void *) minipal_co_task_mem_alloc(lSize); if (NULL == pImage) { return E_OUTOFMEMORY; @@ -1626,7 +1626,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 ::minipal_co_task_mem_free()) + // give pointer to memory image back to caller (who must free with minipal_co_task_mem_free()) *ppImage = pImage; // all done diff --git a/src/coreclr/md/compiler/regmeta.cpp b/src/coreclr/md/compiler/regmeta.cpp index eee9a527f04c8..7022832ae99e4 100644 --- a/src/coreclr/md/compiler/regmeta.cpp +++ b/src/coreclr/md/compiler/regmeta.cpp @@ -247,7 +247,7 @@ RegMeta::CreateNewMD() ModuleRec *pModule; GUID mvid; IfFailGo(m_pStgdb->m_MiniMd.AddModuleRecord(&pModule, &iRecord)); - IfFailGo(minipal_guid_v4_create(reinterpret_cast(&mvid)) ? S_OK : E_FAIL); + 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 50f5c95a0dcd2..45b75bb09ba12 100644 --- a/src/coreclr/md/enc/metamodelrw.cpp +++ b/src/coreclr/md/enc/metamodelrw.cpp @@ -1197,7 +1197,7 @@ CMiniMdRW::SetOption( PutCol(TBL_Module, ModuleRec::COL_EncBaseId, pMod, uVal); */ // Allocate a new GUID for EncId. - IfFailGo(minipal_guid_v4_create(reinterpret_cast(&encid)) ? S_OK : E_FAIL); + 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/vm/eventpipeinternal.cpp b/src/coreclr/vm/eventpipeinternal.cpp index be22b72be47a3..0c89c706b3051 100644 --- a/src/coreclr/vm/eventpipeinternal.cpp +++ b/src/coreclr/vm/eventpipeinternal.cpp @@ -205,7 +205,7 @@ extern "C" int QCALLTYPE EventPipeInternal_EventActivityIdControl(uint32_t contr case ActivityControlCode::EVENT_ACTIVITY_CONTROL_CREATE_SET_ID: *pActivityId = *pThread->GetActivityId(); - minipal_guid_v4_create(reinterpret_cast(¤tActivityId)); + minipal_guid_v4_create(¤tActivityId); pThread->SetActivityId(¤tActivityId); break; diff --git a/src/native/minipal/guid.h b/src/native/minipal/guid.h index 77692aef9ff30..ef7dcaaadca7b 100644 --- a/src/native/minipal/guid.h +++ b/src/native/minipal/guid.h @@ -38,6 +38,13 @@ 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 From 9408aa68d857b0aa538cc28594085851d85120a1 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 15:43:19 -0700 Subject: [PATCH 11/15] Link minipal to bcrypt and flow it so superpmi picks it up --- src/native/minipal/CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/native/minipal/CMakeLists.txt b/src/native/minipal/CMakeLists.txt index 9af402eb34e46..957b05e81dd62 100644 --- a/src/native/minipal/CMakeLists.txt +++ b/src/native/minipal/CMakeLists.txt @@ -17,12 +17,20 @@ include_directories(${CLR_SRC_NATIVE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) add_library(minipal_objects OBJECT ${SOURCES}) +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(minipal_objects_no_lto OBJECT ${SOURCES}) set_target_properties(minipal_objects_no_lto PROPERTIES INTERPROCEDURAL_OPTIMIZATION OFF) +if (WIN32) + target_link_libraries(minipal_objects_no_lto PUBLIC bcrypt) +endif() + # Provide a static library for our shared library and executable scenarios # for easier usability. add_library(minipal STATIC) From aae4c867b25343c3adc07f12645d548aa2873945 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 15:47:59 -0700 Subject: [PATCH 12/15] Make the co-task-mem-alloc PAL just call malloc --- src/native/minipal/memory.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/native/minipal/memory.c b/src/native/minipal/memory.c index 5e147de558f8b..d72c7ed0f6ddc 100644 --- a/src/native/minipal/memory.c +++ b/src/native/minipal/memory.c @@ -16,21 +16,9 @@ void minipal_co_task_mem_free(void* pv) CoTaskMemFree(pv); } #else -// CoTaskMemAlloc always aligns on an 8-byte boundary. -#define ALIGN 8 - void* minipal_co_task_mem_alloc(size_t cb) { - // Ensure malloc always allocates. - if (cb == 0) - cb = ALIGN; - - // Align the allocation size. - size_t cb_safe = (cb + (ALIGN - 1)) & ~(ALIGN - 1); - if (cb_safe < cb) // Overflow - return NULL; - - return aligned_alloc(ALIGN, cb_safe); + return malloc(cb); } void minipal_co_task_mem_free(void* pv) From 9ccdb609dcc4395dd2134c5862d1501bd3e36238 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 16:10:24 -0700 Subject: [PATCH 13/15] We're in C --- src/native/eventpipe/ep-thread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/eventpipe/ep-thread.h b/src/native/eventpipe/ep-thread.h index 8a60db2e12ffc..a4e6e3b73e422 100644 --- a/src/native/eventpipe/ep-thread.h +++ b/src/native/eventpipe/ep-thread.h @@ -117,7 +117,7 @@ ep_thread_create_activity_id ( EP_ASSERT (activity_id != NULL); EP_ASSERT (activity_id_len == EP_ACTIVITY_ID_SIZE); - minipal_guid_v4_create (reinterpret_cast(activity_id)); + minipal_guid_v4_create ((minipal_guid_t *)(activity_id)); } static From 676c988e36fa00a2ff95cb5408d6ae853230ddf4 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 16:31:45 -0700 Subject: [PATCH 14/15] Add casts for Windows --- src/mono/mono/eglib/giconv.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mono/mono/eglib/giconv.c b/src/mono/mono/eglib/giconv.c index 95cf6ac3505dd..144d38a5de4c2 100644 --- a/src/mono/mono/eglib/giconv.c +++ b/src/mono/mono/eglib/giconv.c @@ -305,7 +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 = minipal_u16_strlen (str) + 1; + len = (glong)minipal_u16_strlen (str) + 1; } glong ret = (glong)minipal_get_length_utf16_to_utf8 (str, len, flags); @@ -346,7 +346,7 @@ g_utf16_to_utf8_custom_alloc (const gunichar2 *str, glong len, glong *items_read errno = 0; if (len < 0) { - len = minipal_u16_strlen (str) + 1; + len = (glong)minipal_u16_strlen (str) + 1; } glong ret = (glong)minipal_get_length_utf16_to_utf8 (str, len, 0); @@ -384,7 +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 = minipal_u16_strlen (str); + len = (glong)minipal_u16_strlen (str); } inptr = (char *) str; From 1be8d8adbc2627b96f9bd0d7303f86b5d0b88797 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Oct 2024 17:49:06 -0700 Subject: [PATCH 15/15] Fix assert --- src/mono/mono/mini/aot-compiler.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mono/mono/mini/aot-compiler.c b/src/mono/mono/mini/aot-compiler.c index d8eb65c76c5b4..51889fa514b9b 100644 --- a/src/mono/mono/mini/aot-compiler.c +++ b/src/mono/mono/mini/aot-compiler.c @@ -11567,8 +11567,8 @@ emit_extra_methods (MonoAotCompile *acfg) static void generate_aotid (guint8* aotid) { - bool success = minipal_get_cryptographically_secure_random_bytes (aotid, 16); - g_assert (success); + int status = minipal_get_cryptographically_secure_random_bytes (aotid, 16); + g_assert (status == 0); } static void