Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8361725

Do not load Java agent with "-Xshare:dump -XX:+AOTClassLinking"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 26
    • 25
    • hotspot
    • master

      When -XX:+AOTClassLinking is enabled during "java -Xshare:dump", we have a more stringent restriction of what Java code can be executed -- if arbitrary Java code is execute, it may produce side effects that cannot be handled when archiving Java heap objects. In this case, cdsHeapVerifier.cpp reports a suspicious reference to a static field that is not known to be safe:

      $ java -javaagent:holytime/holy-agent.jar -Xshare:dump -XX:+UnlockDiagnosticVMOptions -XX:+AllowArchivingWithJavaAgent -XX:+AOTClassLinking
      Jul 09, 2025 10:44:46 AM dev.thihup.holytime.holy.agent.Premain premain
      INFO: [Holyrics Patcher] Agent loaded
      Jul 09, 2025 10:44:46 AM dev.thihup.holytime.holy.agent.Premain premain
      WARNING: [Holyrics Patcher] Failed to open packages for modules. Probably during -Xshare:dump. Disabling agent.
      [0.215s][warning][cds] Skipping dev/thihup/holytime/holy/agent/Premain: Unsupported location
      [0.215s][warning][cds] Skipping dev/thihup/holytime/holy/agent/Premain$$Lambda+0x80000003d: nest_host class dev/thihup/holytime/holy/agent/Premain is excluded
      [0.215s][warning][cds] Skipping dev/thihup/holytime/holy/agent/Premain$$Lambda+0x800000040: nest_host class dev/thihup/holytime/holy/agent/Premain is excluded
      [1.367s][warning][aot,heap] Archive heap points to a static field that may hold a different value at runtime:
      [1.367s][warning][aot,heap] Field: sun/util/locale/LanguageTag::EMPTY_SUBTAGS
      [1.368s][warning][aot,heap] Value: java.util.Collections$EmptyList
      [1.369s][warning][aot,heap] {0x00000000cb006f68} - klass: 'java/util/Collections$EmptyList' - flags:
      [1.369s][warning][aot,heap]
      [1.370s][warning][aot,heap] - ---- fields (total size 2 words):
      [1.371s][warning][aot,heap] - protected transient 'modCount' 'I' @8 0 (0x00000000)
      [1.371s][warning][aot,heap] --- trace begin ---
      [1.371s][warning][aot,heap] [ 0] {0x00000000cb006f68} java.util.Collections$EmptyList
      [1.371s][warning][aot,heap] --- trace end ---
      [1.371s][warning][aot,heap]
      [1.372s][error ][aot,heap] Scanned 38492 objects. Found 1 case(s) where an object points to a static field that may hold a different value at runtime.
      [1.372s][error ][aot,heap] Please see cdsHeapVerifier.cpp and aotClassInitializer.cpp for details
      [1.372s][error ][cds ] An error has occurred while writing the shared archive file.

      For original reproduction steps, see comments

      ==================
      Work around
      ===================
      [1] Do not use -javaagent when running "java -Xshare:dump -XX:+AOTClassLinking", or
      [2] Avoid doing any work inside the agent -- exit the premain() method without doing anything. Especially, do not use Java logging.

      E.g., in the case of the "holy-agent.jar", we can work around the problem by disabling logging and exiting quickly:

      https://github.com/Thihup/holytime/blob/569b047ccbd52fd3585208645c7ed416e3aeb94c/holy-agent/src/main/java/dev/thihup/holytime/holy/agent/Premain.java#L30-L32

              if (Boolean.getBoolean("holyagent.disable")) {
                  //LOGGER.log(Level.WARNING,
                  // "[Holyrics Patcher] Agent disabled by system property 'holyagent.disable'.");
                  return;
              }

      $ java -javaagent:holytime/holy-agent.jar -Xshare:dump -XX:+UnlockDiagnosticVMOptions -XX:+AllowArchivingWithJavaAgent -XX:+AOTClassLinking -Dholyagent.disable=true
      [0.201s][warning][cds] Skipping dev/thihup/holytime/holy/agent/Premain: Unsupported location
      [0.376s][warning][cds] This shared archive file was created with AllowArchivingWithJavaAgent. It should be used for testing purposes only and should not be used in a production environment
      $ echo $?
      0

            iklam Ioi Lam
            iklam Ioi Lam
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: