From 3b09e422ff63b1910e49d751b45cce50d4c6c868 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Mon, 17 Jul 2023 11:15:44 -0700 Subject: [PATCH 1/9] Support for utf conversion --- .../Runtime/eventpipe/CMakeLists.txt | 15 +++++ .../nativeaot/Runtime/eventpipe/ep-rt-aot.h | 62 ++++++++++--------- src/native/minipal/utf8.c | 2 + 3 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt b/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt index a6bedd8e2254d9..0685ffdfd393e7 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt +++ b/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt @@ -9,15 +9,26 @@ set(AOT_EVENTPIPE_SHIM_DIR "${CMAKE_CURRENT_SOURCE_DIR}") set (CONTAINER_SOURCES "") set (CONTAINER_HEADERS "") +set (MINIPAL_SOURCES "") +set (MINIPAL_HEADERS "") set (EVENTPIPE_SOURCES "") set (EVENTPIPE_HEADERS "") set (GEN_EVENTPIPE_SOURCES "") set (SHARED_CONTAINERS_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/containers") set (SHARED_EVENTPIPE_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/eventpipe") +set (SHARED_MINIPAL_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/minipal") include (${SHARED_EVENTPIPE_SOURCE_PATH}/eventpipe.cmake) include (${SHARED_CONTAINERS_SOURCE_PATH}/containers.cmake) +list(APPEND MINIPAL_SOURCES + utf8.c +) + +list(APPEND MINIPAL_HEADERS + utf8.h +) + if(CLR_CMAKE_HOST_WIN32) list(APPEND SHARED_DIAGNOSTIC_SERVER_SOURCES ds-ipc-pal-namedpipe.c @@ -50,6 +61,8 @@ list(APPEND EVENTPIPE_HEADERS addprefix(CONTAINER_SOURCES ${SHARED_CONTAINERS_SOURCE_PATH} "${SHARED_CONTAINER_SOURCES}") addprefix(CONTAINER_HEADERS ${SHARED_CONTAINERS_SOURCE_PATH} "${SHARED_CONTAINER_HEADERS}") +addprefix(MINIPAL_SOURCES ${SHARED_MINIPAL_SOURCE_PATH} "${MINIPAL_SOURCES}") +addprefix(MINIPAL_HEADERS ${SHARED_MINIPAL_SOURCE_PATH} "${MINIPAL_HEADERS}") addprefix(EVENTPIPE_SOURCES ${SHARED_EVENTPIPE_SOURCE_PATH} "${EVENTPIPE_SOURCES}") addprefix(EVENTPIPE_HEADERS ${SHARED_EVENTPIPE_SOURCE_PATH} "${EVENTPIPE_HEADERS}") @@ -125,6 +138,8 @@ list(APPEND EVENTPIPE_SOURCES ${GEN_EVENTPIPE_SOURCES} ${CONTAINER_SOURCES} ${CONTAINER_HEADERS} + ${MINIPAL_SOURCES} + ${MINIPAL_HEADERS} ) list(APPEND AOT_EVENTPIPE_DISABLED_SOURCES diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index 25c9666b78c017..15bd54098baf47 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -11,6 +11,8 @@ #include #endif +#include + #include #ifdef ENABLE_PERFTRACING #include @@ -1375,6 +1377,8 @@ ep_rt_utf8_string_replace ( return false; } +#define MINIPAL_MB_NO_REPLACE_INVALID_CHARS 0x00000008 + static ep_char16_t * ep_rt_utf8_to_utf16le_string ( @@ -1386,22 +1390,23 @@ ep_rt_utf8_to_utf16le_string ( if (!str) return NULL; - // Shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // Implementation would just use strlen and malloc to make a new buffer, and would then copy the string chars one by one. - // Assumes that only ASCII is used for ep_char8_t - size_t len_utf8 = strlen(str); - ep_char16_t *str_utf16 = reinterpret_cast(malloc ((len_utf8 + 1) * sizeof (ep_char16_t))); - if (!str_utf16) + int32_t flags = MINIPAL_MB_NO_REPLACE_INVALID_CHARS; + + ep_char16_t* lpDestStr = NULL; + + if (static_cast(len) < 0) + len = (size_t)strlen(str) + 1; + + size_t ret = (size_t)minipal_get_length_utf8_to_utf16 (str, len, flags); + + if (ret <= 0) return NULL; - for (size_t i = 0; i < len_utf8; i++) - { - EP_ASSERT(isascii(str[i])); - str_utf16[i] = str[i]; - } + lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(ep_char16_t))); + ret = (size_t)minipal_convert_utf8_to_utf16 (str, len, reinterpret_cast(lpDestStr), ret, flags); + lpDestStr[ret] = '\0'; - str_utf16[len_utf8] = 0; - return str_utf16; + return lpDestStr; } static @@ -1450,27 +1455,28 @@ ep_rt_utf16_to_utf8_string ( size_t len) { STATIC_CONTRACT_NOTHROW; - if (!str) return NULL; - - // shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase - // Simple implementation to create a utf8 string from a utf16 one - size_t len_utf16 = len; - if(len_utf16 == (size_t)-1) - len_utf16 = ep_rt_utf16_string_len (str); - ep_char8_t *str_utf8 = reinterpret_cast(malloc ((len_utf16 + 1) * sizeof (ep_char8_t))); - if (!str_utf8) - return NULL; + ep_char8_t* lpDestStr = NULL; + if (static_cast(len) < 0) { + len = 0; + while (str[len]) + len++; - for (size_t i = 0; i < len_utf16; i++) - { - str_utf8[i] = (char)str[i]; + len++; } - str_utf8[len_utf16] = 0; - return str_utf8; + size_t ret = (size_t)minipal_get_length_utf16_to_utf8 (reinterpret_cast(str), len, 0); + + if (ret <= 0) + return NULL; + + lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(ep_char8_t))); + ret = (size_t)minipal_convert_utf16_to_utf8 (reinterpret_cast(str), len, lpDestStr, ret, 0); + lpDestStr[ret] = '\0'; + + return lpDestStr; } static diff --git a/src/native/minipal/utf8.c b/src/native/minipal/utf8.c index a54b805540f897..c0bf227919f256 100644 --- a/src/native/minipal/utf8.c +++ b/src/native/minipal/utf8.c @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning(disable:4244) + #include #include From 86b5325988f5385e1ef6dc227167176c0c6488cd Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Mon, 17 Jul 2023 12:06:12 -0700 Subject: [PATCH 2/9] cast fix --- src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index 15bd54098baf47..627e99f341a408 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -1403,7 +1403,7 @@ ep_rt_utf8_to_utf16le_string ( return NULL; lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(ep_char16_t))); - ret = (size_t)minipal_convert_utf8_to_utf16 (str, len, reinterpret_cast(lpDestStr), ret, flags); + ret = (size_t)minipal_convert_utf8_to_utf16 (str, len, reinterpret_cast(lpDestStr), ret, flags); lpDestStr[ret] = '\0'; return lpDestStr; @@ -1467,7 +1467,7 @@ ep_rt_utf16_to_utf8_string ( len++; } - size_t ret = (size_t)minipal_get_length_utf16_to_utf8 (reinterpret_cast(str), len, 0); + size_t ret = (size_t)minipal_get_length_utf16_to_utf8 (reinterpret_cast(str), len, 0); if (ret <= 0) return NULL; From 424473b7ab89b707687487df026e363acc274486 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Mon, 17 Jul 2023 14:05:01 -0700 Subject: [PATCH 3/9] FB --- .../nativeaot/Runtime/eventpipe/CMakeLists.txt | 7 ------- src/native/minipal/utf8.c | 16 +++++++--------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt b/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt index 0685ffdfd393e7..3c61f341b328f7 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt +++ b/src/coreclr/nativeaot/Runtime/eventpipe/CMakeLists.txt @@ -10,7 +10,6 @@ set(AOT_EVENTPIPE_SHIM_DIR "${CMAKE_CURRENT_SOURCE_DIR}") set (CONTAINER_SOURCES "") set (CONTAINER_HEADERS "") set (MINIPAL_SOURCES "") -set (MINIPAL_HEADERS "") set (EVENTPIPE_SOURCES "") set (EVENTPIPE_HEADERS "") set (GEN_EVENTPIPE_SOURCES "") @@ -25,10 +24,6 @@ list(APPEND MINIPAL_SOURCES utf8.c ) -list(APPEND MINIPAL_HEADERS - utf8.h -) - if(CLR_CMAKE_HOST_WIN32) list(APPEND SHARED_DIAGNOSTIC_SERVER_SOURCES ds-ipc-pal-namedpipe.c @@ -62,7 +57,6 @@ list(APPEND EVENTPIPE_HEADERS addprefix(CONTAINER_SOURCES ${SHARED_CONTAINERS_SOURCE_PATH} "${SHARED_CONTAINER_SOURCES}") addprefix(CONTAINER_HEADERS ${SHARED_CONTAINERS_SOURCE_PATH} "${SHARED_CONTAINER_HEADERS}") addprefix(MINIPAL_SOURCES ${SHARED_MINIPAL_SOURCE_PATH} "${MINIPAL_SOURCES}") -addprefix(MINIPAL_HEADERS ${SHARED_MINIPAL_SOURCE_PATH} "${MINIPAL_HEADERS}") addprefix(EVENTPIPE_SOURCES ${SHARED_EVENTPIPE_SOURCE_PATH} "${EVENTPIPE_SOURCES}") addprefix(EVENTPIPE_HEADERS ${SHARED_EVENTPIPE_SOURCE_PATH} "${EVENTPIPE_HEADERS}") @@ -139,7 +133,6 @@ list(APPEND EVENTPIPE_SOURCES ${CONTAINER_SOURCES} ${CONTAINER_HEADERS} ${MINIPAL_SOURCES} - ${MINIPAL_HEADERS} ) list(APPEND AOT_EVENTPIPE_DISABLED_SOURCES diff --git a/src/native/minipal/utf8.c b/src/native/minipal/utf8.c index c0bf227919f256..b9fdff27a407df 100644 --- a/src/native/minipal/utf8.c +++ b/src/native/minipal/utf8.c @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning(disable:4244) - #include #include @@ -534,7 +532,7 @@ static size_t GetCharCount(UTF8Encoding* self, unsigned char* bytes, size_t coun EncodeChar: - availableBytes = pEnd - pSrc; + availableBytes = (int)(pEnd - pSrc); // don't fall into the fast decoding loop if we don't have enough bytes if (availableBytes <= 13) @@ -984,8 +982,8 @@ static int GetChars(UTF8Encoding* self, unsigned char* bytes, size_t byteCount, *pTarget = (CHAR16_T)ch; ENSURE_BUFFER_INC - int availableChars = pAllocatedBufferEnd - pTarget; - int availableBytes = pEnd - pSrc; + int availableChars = (int)(pAllocatedBufferEnd - pTarget); + int availableBytes = (int)(pEnd - pSrc); // don't fall into the fast decoding loop if we don't have enough bytes // Test for availableChars is done because pStop would be <= pTarget. @@ -1291,7 +1289,7 @@ static int GetChars(UTF8Encoding* self, unsigned char* bytes, size_t byteCount, return 0; } - return pTarget - chars; + return (int)(pTarget - chars); } static size_t GetBytes(UTF8Encoding* self, CHAR16_T* chars, size_t charCount, unsigned char* bytes, size_t byteCount) @@ -1512,8 +1510,8 @@ static size_t GetBytes(UTF8Encoding* self, CHAR16_T* chars, size_t charCount, un if (fallbackUsed && (ch = EncoderReplacementFallbackBuffer_InternalGetNextChar(&self->buffer.encoder)) != 0) goto ProcessChar; - int availableChars = pEnd - pSrc; - int availableBytes = pAllocatedBufferEnd - pTarget; + int availableChars = (int)(pEnd - pSrc); + int availableBytes = (int)(pAllocatedBufferEnd - pTarget); // don't fall into the fast decoding loop if we don't have enough characters // Note that if we don't have enough bytes, pStop will prevent us from entering the fast loop. @@ -1891,7 +1889,7 @@ static size_t GetByteCount(UTF8Encoding* self, CHAR16_T *chars, size_t count) goto ProcessChar; } - int availableChars = pEnd - pSrc; + int availableChars = (int)(pEnd - pSrc); // don't fall into the fast decoding loop if we don't have enough characters if (availableChars <= 13) From 68b5a5dd672d66ea70908cea3a9c26fafead0b7b Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Mon, 17 Jul 2023 16:22:39 -0700 Subject: [PATCH 4/9] FB --- src/native/minipal/utf8.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/native/minipal/utf8.c b/src/native/minipal/utf8.c index b9fdff27a407df..77a4e1407bcd9a 100644 --- a/src/native/minipal/utf8.c +++ b/src/native/minipal/utf8.c @@ -365,7 +365,8 @@ static size_t GetCharCount(UTF8Encoding* self, unsigned char* bytes, size_t coun // Initialize stuff unsigned char *pSrc = bytes; unsigned char *pEnd = pSrc + count; - int availableBytes, chc; + size_t availableBytes; + int chc; // Start by assuming we have as many as count, charCount always includes the adjustment // for the character being decoded @@ -532,7 +533,7 @@ static size_t GetCharCount(UTF8Encoding* self, unsigned char* bytes, size_t coun EncodeChar: - availableBytes = (int)(pEnd - pSrc); + availableBytes = (size_t)(pEnd - pSrc); // don't fall into the fast decoding loop if we don't have enough bytes if (availableBytes <= 13) @@ -749,7 +750,7 @@ static size_t GetCharCount(UTF8Encoding* self, unsigned char* bytes, size_t coun return 0; \ } -static int GetChars(UTF8Encoding* self, unsigned char* bytes, size_t byteCount, CHAR16_T* chars, size_t charCount) +static size_t GetChars(UTF8Encoding* self, unsigned char* bytes, size_t byteCount, CHAR16_T* chars, size_t charCount) { assert(chars != NULL); assert(byteCount >= 0); @@ -982,8 +983,8 @@ static int GetChars(UTF8Encoding* self, unsigned char* bytes, size_t byteCount, *pTarget = (CHAR16_T)ch; ENSURE_BUFFER_INC - int availableChars = (int)(pAllocatedBufferEnd - pTarget); - int availableBytes = (int)(pEnd - pSrc); + size_t availableChars = (size_t)(pAllocatedBufferEnd - pTarget); + size_t availableBytes = (size_t)(pEnd - pSrc); // don't fall into the fast decoding loop if we don't have enough bytes // Test for availableChars is done because pStop would be <= pTarget. @@ -1289,7 +1290,7 @@ static int GetChars(UTF8Encoding* self, unsigned char* bytes, size_t byteCount, return 0; } - return (int)(pTarget - chars); + return (size_t)(pTarget - chars); } static size_t GetBytes(UTF8Encoding* self, CHAR16_T* chars, size_t charCount, unsigned char* bytes, size_t byteCount) @@ -1510,8 +1511,8 @@ static size_t GetBytes(UTF8Encoding* self, CHAR16_T* chars, size_t charCount, un if (fallbackUsed && (ch = EncoderReplacementFallbackBuffer_InternalGetNextChar(&self->buffer.encoder)) != 0) goto ProcessChar; - int availableChars = (int)(pEnd - pSrc); - int availableBytes = (int)(pAllocatedBufferEnd - pTarget); + size_t availableChars = (size_t)(pEnd - pSrc); + size_t availableBytes = (size_t)(pAllocatedBufferEnd - pTarget); // don't fall into the fast decoding loop if we don't have enough characters // Note that if we don't have enough bytes, pStop will prevent us from entering the fast loop. @@ -1709,7 +1710,7 @@ static size_t GetBytes(UTF8Encoding* self, CHAR16_T* chars, size_t charCount, un return 0; } - return (int)(pTarget - bytes); + return (size_t)(pTarget - bytes); } static size_t GetByteCount(UTF8Encoding* self, CHAR16_T *chars, size_t count) @@ -1889,7 +1890,7 @@ static size_t GetByteCount(UTF8Encoding* self, CHAR16_T *chars, size_t count) goto ProcessChar; } - int availableChars = (int)(pEnd - pSrc); + size_t availableChars = (size_t)(pEnd - pSrc); // don't fall into the fast decoding loop if we don't have enough characters if (availableChars <= 13) From fd54d9dd7611913c4f08b35c2b7523d8c69d07f7 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Tue, 18 Jul 2023 03:55:29 -0700 Subject: [PATCH 5/9] Update src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h Co-authored-by: Jan Kotas --- src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index 627e99f341a408..bcba45a3bd50f8 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -1397,7 +1397,7 @@ ep_rt_utf8_to_utf16le_string ( if (static_cast(len) < 0) len = (size_t)strlen(str) + 1; - size_t ret = (size_t)minipal_get_length_utf8_to_utf16 (str, len, flags); + size_t ret = minipal_get_length_utf8_to_utf16 (str, len, flags); if (ret <= 0) return NULL; From d90a836947d0e6f81090cb742dc25a887b94bf96 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Tue, 18 Jul 2023 03:56:34 -0700 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Jan Kotas --- src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index bcba45a3bd50f8..210a98bd18beeb 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -1395,7 +1395,7 @@ ep_rt_utf8_to_utf16le_string ( ep_char16_t* lpDestStr = NULL; if (static_cast(len) < 0) - len = (size_t)strlen(str) + 1; + len = strlen(str) + 1; size_t ret = minipal_get_length_utf8_to_utf16 (str, len, flags); @@ -1403,7 +1403,7 @@ ep_rt_utf8_to_utf16le_string ( return NULL; lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(ep_char16_t))); - ret = (size_t)minipal_convert_utf8_to_utf16 (str, len, reinterpret_cast(lpDestStr), ret, flags); + ret = minipal_convert_utf8_to_utf16 (str, len, reinterpret_cast(lpDestStr), ret, flags); lpDestStr[ret] = '\0'; return lpDestStr; @@ -1464,16 +1464,15 @@ ep_rt_utf16_to_utf8_string ( while (str[len]) len++; - len++; } - size_t ret = (size_t)minipal_get_length_utf16_to_utf8 (reinterpret_cast(str), len, 0); + size_t ret = minipal_get_length_utf16_to_utf8 (reinterpret_cast(str), len, 0); if (ret <= 0) return NULL; lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(ep_char8_t))); - ret = (size_t)minipal_convert_utf16_to_utf8 (reinterpret_cast(str), len, lpDestStr, ret, 0); + ret = minipal_convert_utf16_to_utf8 (reinterpret_cast(str), len, lpDestStr, ret, 0); lpDestStr[ret] = '\0'; return lpDestStr; From dfcaabba3ebdbf642228e4e6c1b294b2ebca611c Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Tue, 18 Jul 2023 12:57:34 -0700 Subject: [PATCH 7/9] FB --- src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index 210a98bd18beeb..c58bde566d11a8 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -1394,7 +1394,7 @@ ep_rt_utf8_to_utf16le_string ( ep_char16_t* lpDestStr = NULL; - if (static_cast(len) < 0) + if (len == (size_t) -1) len = strlen(str) + 1; size_t ret = minipal_get_length_utf8_to_utf16 (str, len, flags); @@ -1459,12 +1459,8 @@ ep_rt_utf16_to_utf8_string ( return NULL; ep_char8_t* lpDestStr = NULL; - if (static_cast(len) < 0) { - len = 0; - while (str[len]) - len++; - - } + if (len == (size_t) -1) + len = ep_rt_utf16_string_len (str) + 1; size_t ret = minipal_get_length_utf16_to_utf8 (reinterpret_cast(str), len, 0); From 3af74a0b53065257c56177acd2af797e016ed596 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 20 Jul 2023 10:09:08 -0700 Subject: [PATCH 8/9] FB --- .../nativeaot/Runtime/eventpipe/ep-rt-aot.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index c58bde566d11a8..a4ff7f213f2224 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -1377,7 +1377,6 @@ ep_rt_utf8_string_replace ( return false; } -#define MINIPAL_MB_NO_REPLACE_INVALID_CHARS 0x00000008 static ep_char16_t * @@ -1392,8 +1391,6 @@ ep_rt_utf8_to_utf16le_string ( int32_t flags = MINIPAL_MB_NO_REPLACE_INVALID_CHARS; - ep_char16_t* lpDestStr = NULL; - if (len == (size_t) -1) len = strlen(str) + 1; @@ -1402,11 +1399,11 @@ ep_rt_utf8_to_utf16le_string ( if (ret <= 0) return NULL; - lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(ep_char16_t))); - ret = minipal_convert_utf8_to_utf16 (str, len, reinterpret_cast(lpDestStr), ret, flags); + CHAR16_T * lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(CHAR16_T))); + ret = minipal_convert_utf8_to_utf16 (str, len, lpDestStr, ret, flags); lpDestStr[ret] = '\0'; - return lpDestStr; + return reinterpret_cast(lpDestStr); } static @@ -1458,7 +1455,6 @@ ep_rt_utf16_to_utf8_string ( if (!str) return NULL; - ep_char8_t* lpDestStr = NULL; if (len == (size_t) -1) len = ep_rt_utf16_string_len (str) + 1; @@ -1467,11 +1463,11 @@ ep_rt_utf16_to_utf8_string ( if (ret <= 0) return NULL; - lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(ep_char8_t))); + char* lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(char))); ret = minipal_convert_utf16_to_utf8 (reinterpret_cast(str), len, lpDestStr, ret, 0); lpDestStr[ret] = '\0'; - return lpDestStr; + return reinterpret_cast(lpDestStr); } static From fb234b8b8baa7ad0e1d5c4e0844e28b15c2c7313 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 21 Jul 2023 06:48:00 -0700 Subject: [PATCH 9/9] FB --- .../nativeaot/Runtime/eventpipe/ep-rt-aot.h | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index a4ff7f213f2224..4753bef82d376d 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -1389,10 +1389,22 @@ ep_rt_utf8_to_utf16le_string ( if (!str) return NULL; - int32_t flags = MINIPAL_MB_NO_REPLACE_INVALID_CHARS; + if (len == 0) { + // Return an empty string if the length is 0 + CHAR16_T * lpDestEmptyStr = reinterpret_cast(malloc(1 * sizeof(CHAR16_T))); + if(lpDestEmptyStr==NULL) { + return NULL; + } + *lpDestEmptyStr = '\0'; + return reinterpret_cast(lpDestEmptyStr); + } - if (len == (size_t) -1) + if (len == (size_t) -1) { + // Following the pattern used in EventPipe library where it allocates 1 extra character len = strlen(str) + 1; + } + + int32_t flags = MINIPAL_MB_NO_REPLACE_INVALID_CHARS | MINIPAL_TREAT_AS_LITTLE_ENDIAN; size_t ret = minipal_get_length_utf8_to_utf16 (str, len, flags); @@ -1400,6 +1412,9 @@ ep_rt_utf8_to_utf16le_string ( return NULL; CHAR16_T * lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(CHAR16_T))); + if(lpDestStr==NULL) { + return NULL; + } ret = minipal_convert_utf8_to_utf16 (str, len, lpDestStr, ret, flags); lpDestStr[ret] = '\0'; @@ -1455,8 +1470,20 @@ ep_rt_utf16_to_utf8_string ( if (!str) return NULL; - if (len == (size_t) -1) + if (len == 0) { + // Return an empty string if the length is 0 + char * lpDestEmptyStr = reinterpret_cast(malloc(1 * sizeof(char))); + if(lpDestEmptyStr==NULL) { + return NULL; + } + *lpDestEmptyStr = '\0'; + return reinterpret_cast(lpDestEmptyStr); + } + + if (len == (size_t) -1) { + // Following the pattern used in EventPipe library where it allocates 1 extra character len = ep_rt_utf16_string_len (str) + 1; + } size_t ret = minipal_get_length_utf16_to_utf8 (reinterpret_cast(str), len, 0); @@ -1464,6 +1491,9 @@ ep_rt_utf16_to_utf8_string ( return NULL; char* lpDestStr = reinterpret_cast(malloc((ret + 1) * sizeof(char))); + if(lpDestStr==NULL) { + return NULL; + } ret = minipal_convert_utf16_to_utf8 (reinterpret_cast(str), len, lpDestStr, ret, 0); lpDestStr[ret] = '\0';