Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

The JIT crashes with an access violation when dumping debug output for string literals longer than the buffer size. getStringLiteral returns the actual string length but only copies up to bufferSize characters. The code was passing the full length to convertUtf16ToUtf8ForPrinting, causing it to read beyond the buffer.

Changes:

  • Truncate length to MAX_LITERAL_LENGTH before calling convertUtf16ToUtf8ForPrinting in eePrintStringLiteral
  • Document getStringLiteral return value semantics: returns actual length, may exceed bufferSize
  • Reduce MAX_LITERAL_LENGTH from 256 to 50 characters (since only 50 characters are printed)
  • Increase dst buffer to MAX_LITERAL_LENGTH * 3 + 1 (151 bytes) to accommodate UTF-8 encoding where each UTF-16 character can expand to up to 3 UTF-8 bytes
// Before: crashes when length > MAX_LITERAL_LENGTH
const int MAX_LITERAL_LENGTH = 256;
char dst[MAX_LITERAL_LENGTH];
convertUtf16ToUtf8ForPrinting(str, length, dst, MAX_LITERAL_LENGTH);
printf("\"%.50s%s\"", dst, length > 50 ? "..." : "");

// After: safe truncation with optimized buffer sizes
const int MAX_LITERAL_LENGTH = 50;
char dst[MAX_LITERAL_LENGTH * 3 + 1];
int truncatedLength = min(length, MAX_LITERAL_LENGTH);
convertUtf16ToUtf8ForPrinting(str, truncatedLength, dst, sizeof(dst));
printf("\"%s%s\"", dst, length > MAX_LITERAL_LENGTH ? "..." : "");

This is a debug-only issue with no customer impact.

Original prompt

This section details on the original issue you should resolve

<issue_title>Access violation in the JIT while trying to print a long string</issue_title>
<issue_description>When the JIT trying to print a constant string to the jit dump, it can crash with an access violation at GetByteCount if the string is long.

Repro: https://godbolt.org/z/P7Mba6eW8

clrjit.dll!GetByteCount(UTF8Encoding * self, wchar_t * chars, unsigned __int64 count) line 1959
	: D:\runtime\src\native\minipal\utf8.c(1959)
clrjit.dll!minipal_get_length_utf16_to_utf8(const wchar_t * source, unsigned __int64 sourceLength, unsigned int flags) line 2100
	: D:\runtime\src\native\minipal\utf8.c(2100)
clrjit.dll!Compiler::convertUtf16ToUtf8ForPrinting(const char16_t * utf16Src, unsigned __int64 utf16SrcLen, char * utf8Dst, unsigned __int64 utf8DstLen) line 10654
	: D:\runtime\src\coreclr\jit\compiler.cpp(10654)
clrjit.dll!Compiler::eePrintStringLiteral(CORINFO_MODULE_STRUCT_ * module, unsigned int token) line 723
	: D:\runtime\src\coreclr\jit\eeinterface.cpp(723)
clrjit.dll!Compiler::gtDispConst(GenTree * tree) line 12311
	: D:\runtime\src\coreclr\jit\gentree.cpp(12311)
clrjit.dll!Compiler::gtDispLeaf(GenTree * tree, IndentStack * indentStack) line 12425
	: D:\runtime\src\coreclr\jit\gentree.cpp(12425)
clrjit.dll!Compiler::gtDispTree(GenTree * tree, IndentStack * indentStack, const char * msg, bool topOnly, bool isLIR) line 13216
	: D:\runtime\src\coreclr\jit\gentree.cpp(13216)

Not prioritized as it's debug only.</issue_description>

<agent_instructions>- Truncate the length passed into convertUtf16ToUtf8ForPrinting in eePrintStringLiteral to MAX_LITERAL_LENGTH

  • Add comment to getStringLiteral in corinfo.h that clarifies the return value of the method. The method returns the actual length of the string literal. </agent_instructions>

Comments on the Issue (you are @copilot in this section)

@JulieLeeMSFT It's not a customer facing issue. We will fix when we can.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Dec 11, 2025
@jkotas jkotas added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Dec 11, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix access violation in JIT for long strings Fix access violation in JIT when printing long string literals Dec 11, 2025
Copilot AI requested a review from jkotas December 11, 2025 19:18
@jkotas jkotas requested a review from a team December 11, 2025 19:21
@jkotas jkotas marked this pull request as ready for review December 11, 2025 19:21
Copilot AI review requested due to automatic review settings December 11, 2025 19:21
@jkotas
Copy link
Member

jkotas commented Dec 11, 2025

@dotnet/jit-contrib PTLA

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an access violation in the JIT when dumping debug output for string literals longer than 256 characters. The crash occurred because getStringLiteral returns the actual string length but only copies up to bufferSize characters into the buffer, and the code was passing the full (potentially truncated) length to convertUtf16ToUtf8ForPrinting, causing it to read beyond the buffer.

Key changes:

  • Truncate the length parameter to MAX_LITERAL_LENGTH before calling convertUtf16ToUtf8ForPrinting in eePrintStringLiteral
  • Document the return value semantics of getStringLiteral to clarify it returns the actual string length (which may exceed bufferSize)

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/coreclr/jit/eeinterface.cpp Adds truncation of length to MAX_LITERAL_LENGTH before passing to convertUtf16ToUtf8ForPrinting to prevent buffer overrun
src/coreclr/inc/corinfo.h Documents that getStringLiteral returns the actual string length, which may be larger than bufferSize

Copilot AI and others added 2 commits December 12, 2025 04:58
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested a review from jkotas December 12, 2025 05:07
@jkotas jkotas requested review from AndyAyersMS and EgorBo December 12, 2025 05:11
@jkotas jkotas merged commit 5dadd0f into main Dec 12, 2025
127 checks passed
@jkotas jkotas deleted the copilot/fix-jit-access-violation branch December 12, 2025 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Access violation in the JIT while trying to print a long string

4 participants