This PR fixes an issue when archiving `sun.util.locale.BaseLocale.CACHE` in 
AOTCache or cds archive. `CACHE` is a `LazyConstant` holder for a 
`ReferenceKeySet`. If the CACHE has any element added during the assembly 
phase, then the `ReferenceKeySet` needs to be prepared for archiving, just like 
it is done for `MethodType.internTable` [0].

An element is added to the CACHE in `BaseLocale.getInstance` if 
`BaseLocale.script` is not empty. Depending on the locale settings, it may or 
may not be empty. For example, on my Linux system with locale `en_US`, `script` 
is empty. To force script to be non-empty set these system properties:
`-Duser.language=en -Duser.script=Latn -Duser.country=US`

Testing: 
To reproduce the issue on Linux, run 
`runtime/cds/appcds/aotClassLinking/AOTClassLinkingVMOptions.java` with the 
above mentioned system properties, as:

make test 
TEST=test/hotspot/jtreg/runtime/cds/appcds/aotClassLinking/AOTClassLinkingVMOptions.java
 CONF=base-fd TEST_OPTS_JAVA_OPTIONS="-Duser.language=en -Duser.script=Latn 
-Duser.country=US"


It results in following error:

 stdout: [[0.004s][info][cds] Core region alignment: 4096
[0.131s][info][cds] Loading classes to share ...
[0.131s][info][cds] Parsing 
/home/asmehra/data/ashu-mehra/jdk/build/base-fd/images/jdk/lib/classlist
[0.198s][info][cds] Parsing 
/home/asmehra/data/ashu-mehra/jdk/build/base-fd/test-support/jtreg_test_hotspot_jtreg_runtime_cds_appcds_aotClassLinking_AOTClassLinkingVMOptions_java/scratch/0/runtime.cds.appcds.aotClassLinking.AOTClassLinkingVMOptions.java-test.classlist
[0.199s][info][cds] Loading classes to share: done.
[0.461s][info][cds] Dumping shared data to file: 
/home/asmehra/data/ashu-mehra/jdk/build/base-fd/test-support/jtreg_test_hotspot_jtreg_runtime_cds_appcds_aotClassLinking_AOTClassLinkingVMOptions_java/scratch/0/appcds-14h41m37s070.jsa
[0.461s][info][cds] Gathering all archivable objects ... 
[0.478s][info][cds] Skipping jdk/internal/misc/CDS$UnregisteredClassLoader: 
used only when dumping CDS archive
[0.478s][info][cds] Skipping 
jdk/internal/misc/CDS$UnregisteredClassLoader$Source: used only when dumping 
CDS archive
[0.479s][info][cds] Heap range = [0x000000009c000000 - 0x0000000100000000]
[0.535s][error][aot,heap] Cannot archive reference object 0x000000009c012418 of 
class jdk.internal.util.SoftReferenceKey
[0.535s][error][aot,heap] referent = 0x000000009c012440, queue = 
0x000000009c012488, next = 0x0000000000000000, discovered = 0x0000000000000000
[0.535s][error][aot,heap] This object requires special clean up as its queue is 
not ReferenceQueue::NULL (0x000000009c12d398)
[0.535s][error][aot,heap] referent is not registered with CDS.keepAlive()
[0.535s][error][aot,heap] Reference trace
[0.535s][error][aot,heap] [ 0] {0x000000009c012338} 
jdk.internal.lang.LazyConstantImpl::constant (offset = 8)
[0.535s][error][aot,heap] [ 1] {0x000000009c012348} 
jdk.internal.util.ReferencedKeySet::map (offset = 8)
[0.535s][error][aot,heap] [ 2] {0x000000009c012358} 
jdk.internal.util.ReferencedKeyMap::map (offset = 12)
[0.535s][error][aot,heap] [ 3] {0x000000009c012370} 
java.util.concurrent.ConcurrentHashMap::table (offset = 16)
[0.535s][error][aot,heap] [ 4] {0x000000009c0123b0} 
[Ljava.util.concurrent.ConcurrentHashMap$Node; @[10]
[0.535s][error][aot,heap] [ 5] {0x000000009c012400} 
java.util.concurrent.ConcurrentHashMap$Node
[0.535s][error][cds     ] An error has occurred while writing the shared 
archive file.
];


This problem also happens during the assembly phase of generating the AOTCache.

With this patch, the error is gone and the test passes.

[0] 
https://github.com/openjdk/jdk/blob/fe430ad121464d731402ef4add6d57dadffe723e/src/java.base/share/classes/java/lang/invoke/MethodType.java#L1384
 

---------
- [x] I confirm that I make this contribution in accordance with the [OpenJDK 
Interim AI Policy](https://openjdk.org/legal/ai).

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

Commit messages:
 - 8387745: Call prepreForAOTCache() on BaseLocale::CACHE

Changes: https://git.openjdk.org/jdk/pull/31902/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=31902&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8387745
  Stats: 17 lines in 3 files changed: 17 ins; 0 del; 0 mod
  Patch: https://git.openjdk.org/jdk/pull/31902.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/31902/head:pull/31902

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

Reply via email to