RFR: 8349241: Fix the concurrent execution JVM crash of StringBuilder::append(int/long) [v4]

Shaojin Wen swen at openjdk.org
Tue Feb 4 13:46:11 UTC 2025


On Tue, 4 Feb 2025 00:59:57 GMT, Shaojin Wen <swen at openjdk.org> wrote:

>> The following code can reproduce the problem, writing out of bounds causes JVM Crash
>> 
>> 
>>    StringBuilder buf = new StringBuilder();
>>     buf.append('中');
>> 
>>     final CountDownLatch latch = new CountDownLatch(10);
>>     Runnable r = () -> {
>>         for (int i = 0; i < 10000; i++) {
>>             buf.setLength(0);
>>             buf.trimToSize();
>>             buf.append(123456789);
>>         }
>>         latch.countDown();
>>     };
>>     Thread[] threads = new Thread[10];
>>     for (int i = 0; i < threads.length; i++) {
>>         threads[i] = new Thread(r);
>>     }
>>     for (Thread t : threads) {
>>         t.start();
>>     }
>>     latch.await();
>> }
>> 
>> 
>> This problem can be avoided by using the value of ensureCapacityInternal directly.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   skip coder change

> ```java
>  StringBuilder buf = new StringBuilder();
>     buf.append('中');
> 
>     final CountDownLatch latch = new CountDownLatch(10);
>     Runnable r = () -> {
>         for (int i = 0; i < 10000; i++) {
>             buf.setLength(0);
>             buf.trimToSize();
>             buf.append(123456789);
>         }
>         latch.countDown();
>     };
>     Thread[] threads = new Thread[10];
>     for (int i = 0; i < threads.length; i++) {
>         threads[i] = new Thread(r);
>     }
>     for (Thread t : threads) {
>         t.start();
>     }
>     latch.await();
> ```

Will cause the JVM to exit directly, the error message is as follows

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGBUS (0xa) at pc=0x0000000103b9d23c, pid=23348, tid=33539
#
# JRE version: OpenJDK Runtime Environment (25.0) (build 25-internal-adhoc.wenshao.jdkx)
# Java VM: OpenJDK 64-Bit Server VM (25-internal-adhoc.wenshao.jdkx, mixed mode, tiered, compressed oops, compressed class ptrs, g1 gc, bsd-aarch64)
# Problematic frame:
# [thread 41731 also had an error]
V  [libjvm.dylib+0x41523c]  G1ParScanThreadState::copy_to_survivor_space(G1HeapRegionAttr, oopDesc*, markWord)+0x64
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
[thread 39683 also had an error]
# An error report file with more information is saved as:
# /Users/wenshao/Work/git/jdk_mico_bench/hs_err_pid23348.log
#
# If you would like to submit a bug report, please visit:
#   https://bugreport.java.com/bugreport/crash.jsp
#

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

PR Comment: https://git.openjdk.org/jdk/pull/23427#issuecomment-2634006503


More information about the core-libs-dev mailing list