From e68b4c5e5d4ff7ab8e68d68c9d38eb7898b39566 Mon Sep 17 00:00:00 2001 From: sanjam panda Date: Wed, 20 May 2026 05:20:28 +0000 Subject: [PATCH 1/2] Fix utf8 conversions for s390x while adding eventpipe support s390x microsoft/perfview I came across some bugs where string utf8_to_utf16 convertsion file was incorrect in the .nettrace file for s390x. --- src/native/minipal/utf8.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/native/minipal/utf8.c b/src/native/minipal/utf8.c index 6580d20b201ea1..ad6fed5711614f 100644 --- a/src/native/minipal/utf8.c +++ b/src/native/minipal/utf8.c @@ -620,14 +620,14 @@ static size_t GetCharCount(UTF8Encoding* self, unsigned char* bytes, size_t coun LongCodeWithMask32 : #if BIGENDIAN // be careful about the sign extension - if (!self->treatAsLE) ch = (int)(((unsigned int)ch) >> 16); + if (self->treatAsLE) ch = (int)(((unsigned int)ch) >> 16); else #endif ch &= 0xFF; LongCodeWithMask16: #if BIGENDIAN - if (!self->treatAsLE) ch = (int)(((unsigned int)ch) >> 8); + if (self->treatAsLE) ch = (int)(((unsigned int)ch) >> 8); else #endif ch &= 0xFF; @@ -1061,7 +1061,7 @@ static size_t GetChars(UTF8Encoding* self, unsigned char* bytes, size_t byteCoun // Unfortunately, this is endianness sensitive #if BIGENDIAN - if (!self->treatAsLE) + if (self->treatAsLE) { *pTarget = (CHAR16_T)((ch >> 8) & 0x7F); pSrc += 2; @@ -1093,7 +1093,7 @@ static size_t GetChars(UTF8Encoding* self, unsigned char* bytes, size_t byteCoun // Unfortunately, this is endianness sensitive #if BIGENDIAN - if (!self->treatAsLE) + if (self->treatAsLE) { *pTarget = (CHAR16_T)((ch >> 24) & 0x7F); *(pTarget + 1) = (CHAR16_T)((ch >> 16) & 0x7F); @@ -1126,14 +1126,14 @@ static size_t GetChars(UTF8Encoding* self, unsigned char* bytes, size_t byteCoun LongCodeWithMask32 : #if BIGENDIAN // be careful about the sign extension - if (!self->treatAsLE) ch = (int)(((unsigned int)ch) >> 16); + if (self->treatAsLE) ch = (int)(((unsigned int)ch) >> 16); else #endif ch &= 0xFF; LongCodeWithMask16: #if BIGENDIAN - if (!self->treatAsLE) ch = (int)(((unsigned int)ch) >> 8); + if (self->treatAsLE) ch = (int)(((unsigned int)ch) >> 8); else #endif ch &= 0xFF; @@ -1599,7 +1599,7 @@ static size_t GetBytes(UTF8Encoding* self, CHAR16_T* chars, size_t charCount, un // Unfortunately, this is endianness sensitive #if BIGENDIAN - if (!self->treatAsLE) + if (self->treatAsLE) { *pTarget = (unsigned char)(ch >> 16); *(pTarget + 1) = (unsigned char)ch; @@ -1624,7 +1624,7 @@ static size_t GetBytes(UTF8Encoding* self, CHAR16_T* chars, size_t charCount, un LongCodeWithMask: #if BIGENDIAN // be careful about the sign extension - if (!self->treatAsLE) ch = (int)(((unsigned int)ch) >> 16); + if (self->treatAsLE) ch = (int)(((unsigned int)ch) >> 16); else #endif ch = (CHAR16_T)ch; @@ -2002,7 +2002,7 @@ static size_t GetByteCount(UTF8Encoding* self, CHAR16_T *chars, size_t count) LongCodeWithMask: #if BIGENDIAN // be careful about the sign extension - if (!self->treatAsLE) ch = (int)(((unsigned int)ch) >> 16); + if (self->treatAsLE) ch = (int)(((unsigned int)ch) >> 16); else #endif ch = (CHAR16_T)ch; From b7208bfc209685d6624abdde31c7c10266ba2913 Mon Sep 17 00:00:00 2001 From: sanjam panda Date: Thu, 21 May 2026 04:29:02 +0000 Subject: [PATCH 2/2] fix unicode.cpp to make utf8-to-utf16 conversions as little-endian --- src/coreclr/pal/src/locale/unicode.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/coreclr/pal/src/locale/unicode.cpp b/src/coreclr/pal/src/locale/unicode.cpp index a66a0edc14d488..d50b558344c924 100644 --- a/src/coreclr/pal/src/locale/unicode.cpp +++ b/src/coreclr/pal/src/locale/unicode.cpp @@ -61,6 +61,7 @@ MultiByteToWideChar( IN int cchWideChar) { INT retval =0; + dwFlags |= MINIPAL_TREAT_AS_LITTLE_ENDIAN; PERF_ENTRY(MultiByteToWideChar); ENTRY("MultiByteToWideChar(CodePage=%u, dwFlags=%#x, lpMultiByteStr=%p (%s)," @@ -68,7 +69,7 @@ MultiByteToWideChar( CodePage, dwFlags, lpMultiByteStr?lpMultiByteStr:"NULL", lpMultiByteStr?lpMultiByteStr:"NULL", cbMultiByte, lpWideCharStr, cchWideChar); - if (dwFlags & ~(MB_ERR_INVALID_CHARS | MB_PRECOMPOSED)) + if (dwFlags & ~(MB_ERR_INVALID_CHARS | MB_PRECOMPOSED | MINIPAL_TREAT_AS_LITTLE_ENDIAN)) { ASSERT("Error dwFlags(0x%x) parameter is invalid\n", dwFlags); SetLastError(ERROR_INVALID_FLAGS); @@ -137,6 +138,7 @@ WideCharToMultiByte( INT retval =0; char defaultChar = '?'; BOOL usedDefaultChar = FALSE; + dwFlags |= MINIPAL_TREAT_AS_LITTLE_ENDIAN; PERF_ENTRY(WideCharToMultiByte); ENTRY("WideCharToMultiByte(CodePage=%u, dwFlags=%#x, lpWideCharStr=%p (%S), " @@ -146,7 +148,7 @@ WideCharToMultiByte( cchWideChar, lpMultiByteStr, cbMultiByte, lpDefaultChar, lpUsedDefaultChar); - if (dwFlags & ~WC_NO_BEST_FIT_CHARS) + if (dwFlags & ~(WC_NO_BEST_FIT_CHARS | MINIPAL_TREAT_AS_LITTLE_ENDIAN)) { ERROR("dwFlags %d invalid\n", dwFlags); SetLastError(ERROR_INVALID_FLAGS);