RFR: 8328877: [JNI] The JNI Specification needs to address the limitations of integer UTF-8 String lengths [v2]

David Holmes dholmes at openjdk.org
Fri Aug 30 05:21:54 UTC 2024


> This is the implementation of a new method added to the JNI specification.
> 
> From the CSR request:
> 
> The `GetStringUTFLength` function returns the length as a `jint` (`jsize`) value and so is limited to returning at most `Integer.MAX_VALUE`. But a Java string can itself consist of `Integer.MAX_VALUE` characters, each of which may require more than one byte to represent them in modified UTF-8 format.** It follows then that this function cannot return the correct answer for all String values and yet the specification makes no mention of this, nor of any possible error to report if this situation is encountered.
> 
> **The modified UTF-8 format used by the VM can require up to six bytes to represent one unicode character, but six byte characters are stored as UTF16 surrogate pairs. Hence the most bytes per character is 3, and so the maximum length is 3*`Integer.MAX_VALUE`. With compact strings this reduces to 2*`Integer.MAX_VALUE`.
> 
> Solution
> 
> Deprecate the existing JNI `GetStringUTFLength` method noting that it may return a truncated length, and add a new method, JNI `GetStringUTFLengthAsLong` that returns the string length as a `jlong` value.
> 
> ---
> 
> We also add a truncation warning to `GetStringUTFLength` under -Xcheck:jni
> 
> There are some incidental whitespace changes in `src/hotspot/os/posix/dtrace/hotspot_jni.d` along with the new method entries.
> 
> Testing:
>  - new test added
>  - tiers 1-3 sanity
> 
> Thanks

David Holmes has updated the pull request incrementally with one additional commit since the last revision:

  Exclude test on 32-bit

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/20784/files
  - new: https://git.openjdk.org/jdk/pull/20784/files/9a8964b8..73174e64

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=20784&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=20784&range=00-01

  Stats: 1 line in 1 file changed: 1 ins; 0 del; 0 mod
  Patch: https://git.openjdk.org/jdk/pull/20784.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/20784/head:pull/20784

PR: https://git.openjdk.org/jdk/pull/20784


More information about the core-libs-dev mailing list