Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change return type of getStringUTF8Length #21005

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hzongaro
Copy link
Member

The return type of the JVM's getStringUTF8Length method has changed from IDATA to UDATA. This change adjusts the JIT compiler's uses of that method. In particular, the return type of TR_FrontEnd::getStringUTF8Length and its overriding methods change from intptr_t to int32_t. Similarly, the bufferSize argument of TR_FrontEnd::getStringUTF8 becomes uintptr_t where it was int32_t.

The motivation was that the length of a UTF-8 encoded String could be greater than 232 bytes, so the length could overflow intptr_t on a 32-bit platform. All uses of getStringUTF8Length in the JIT involve signatures, descriptors, method names and class names, which will never be large enough to exceed the range of the int32_t type. This was something that @jpmpapin was good enough to verify. Just to be cautious, however, this change includes an assertion test that the computed length is in range for the int32_t type.

This change also introduces a method, getStringUTF8UnabbreviatedLength, that returns a length value of type uint64_t if the JIT compiler ever needs to determine the UTF-8 encoded length of an arbitrary String. The method is currently unused.

In addition, String Builder Transformer uses the result of getStringUTF8Length to estimate the StringBuilder buffer size needed to accommodate appending a constant String to a StringBuilder. That could overestimate the space required. This has been changed to use getStringLength instead, to use the actual lengths of constant String objects. A test has also been added to detect integer overflow of the capacity estimate, aborting the transformation, as StringBuilder.<init>
will throw a NegativeArraySizeException if the specified capacity is negative.

This pull request requires a coordinated merge with OMR pull request eclipse-omr/omr#7620

@hzongaro hzongaro added comp:jit depends:omr Pull request is dependent on a corresponding change in OMR labels Jan 24, 2025
@hzongaro hzongaro requested a review from jdmpapin January 24, 2025 14:58
@hzongaro hzongaro requested a review from dsouzai as a code owner January 24, 2025 14:58
@hzongaro
Copy link
Member Author

@jdmpapin, thank you for your off-line review of an earlier version of these changes. May I ask you to review this updated version?

runtime/compiler/optimizer/StringBuilderTransformer.cpp Outdated Show resolved Hide resolved
runtime/compiler/net/MessageTypes.hpp Outdated Show resolved Hide resolved
runtime/compiler/env/VMJ9.cpp Outdated Show resolved Hide resolved
runtime/compiler/env/VMJ9.h Outdated Show resolved Hide resolved
runtime/compiler/env/j9method.cpp Show resolved Hide resolved
@jdmpapin jdmpapin changed the title Change return type of Change return type of getStringUTF8Length Jan 24, 2025
@hzongaro
Copy link
Member Author

hzongaro commented Feb 3, 2025

Thanks for your review, @jdmpapin. I have addressed your review comments. May I ask you to rereview? Once you are OK with the changes, I will squash the extra commits.

Copy link
Contributor

@jdmpapin jdmpapin left a comment

Choose a reason for hiding this comment

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

LGTM, please squash

String Builder Transformer uses the result of getStringUTF8Length
to estimate the StringBuilder buffer size needed to accommodate
appending a constant String to a StringBuilder.  That could
overestimate the space required.  This has been changed to use
getStringLength instead, to use the actual lengths of constant String
objects.  A test has also been added to detect integer overflow of the
capacity estimate, aborting the transformation, as StringBuilder.<init>
will throw a NegativeArraySizeException if the specified capacity is
negative.

Signed-off-by:  Henry Zongaro <[email protected]>
The return type of the JVM's getStringUTF8Length method has changed from
IDATA to UDATA.  This change adjusts the JIT compiler's uses of that
method.  In particular, the return type of
TR_FrontEnd::getStringUTF8Length and its overriding methods changes from
intptr_t to int32_t.  Similarly, the bufferSize argument of
TR_FrontEnd::getStringUTF8 becomes uintptr_t where it was int32_t.

The motivation was that the length of a UTF-8 encoded String could be
greater than 2^32 bytes, so the length could overflow on a 32-bit
platform.  All uses of getStringUTF8Length in the JIT involve
signatures, descriptors, method names and class names, which will never
be large enough to exceed the range of the int32_t type.  Just to be
cautious, however, this change includes an assertion test that the
computed length is in range for the int32_t type, allowing for a
maximum length of 2^31-2.  That ensures that any code that then uses
that length to allocate a buffer to contain the encoded String with a
NUL terminator will not overflow a 32-bit signed integer representation
for the length plus the NUL byte.

This change also introduces a method, getStringUTF8UnabbreviatedLength,
that returns a length value of type uint64_t if the JIT compiler ever
needs to determine the UTF-8 encoded length of an arbitrary String.  The
method is currently unused.

Finally, this change removes the VM_getStringUTF8Length JITServer
message type, which is never used.

Signed-off-by:  Henry Zongaro <[email protected]>
@hzongaro hzongaro force-pushed the long-utf8-string-length branch from a4974d4 to fb3e0f2 Compare February 5, 2025 14:40
@hzongaro
Copy link
Member Author

hzongaro commented Feb 5, 2025

Thanks, @jdmpapin! I have squashed the commits.

@jdmpapin
Copy link
Contributor

jdmpapin commented Feb 5, 2025

Jenkins test sanity.functional,sanity.openjdk all jdk8,jdk11,jdk17,jdk21 depends eclipse-omr/omr#7620

@hzongaro
Copy link
Member Author

hzongaro commented Feb 7, 2025

It looks like there is a known problem — issue #21066 — affecting JITServer tests. I will rerun PR testing once that problem has been resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:jit depends:omr Pull request is dependent on a corresponding change in OMR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants