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

Spec states to invoke the premain method in an agent class if it's public but implementation differs

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: 17
    • Component/s: core-svc
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      Hide
      This change will break any existing java agent with a non-public static premain or agentmain method that has to be changed to be public. Also, this change will search for a public premain or agentmain method declared in the agent class, but not in its superclasses. The compatibility impact of fixing the bug is probably not too significant. There was no popular Java agent found that defines a non-public premain or agentmain method. All these agents define public premain method. Most of them also define public agentmain method but a couple of them (e.g. Mockit and Zeroturnaround CallSpy) define no agentmain method. In addition, premain and agentmain methods are defined in the agent class (not superclass).
      Show
      This change will break any existing java agent with a non-public static premain or agentmain method that has to be changed to be public. Also, this change will search for a public premain or agentmain method declared in the agent class, but not in its superclasses. The compatibility impact of fixing the bug is probably not too significant. There was no popular Java agent found that defines a non-public premain or agentmain method. All these agents define public premain method. Most of them also define public agentmain method but a couple of them (e.g. Mockit and Zeroturnaround CallSpy) define no agentmain method. In addition, premain and agentmain methods are defined in the agent class (not superclass).
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Align the Instrumentation implementation with the java.lang.instrument spec which requires the premain and agentmain methods to be defined as public.

      Problem

      The java.lang.instrument spec clearly states:

      The agent class must implement a public static premain method
      similar in principle to the main application entry point. 
      . . .
      The agent class must implement a public static agentmain method.

      The current implementation of sun/instrument/InstrumentationImpl.java violates the specification as follows:

      • the implementation allows premain and agentmain methods to be non-public but the spec clearly states "public static premain/agentmain" method.
      • if the agent class does not define the premain/ agentmain method, the implementation would invoke the premain/ agentmain from its superclasses if present but the spec clearly states that the agent class must implement "public static premain/agentmain" method.

      Solution

      The solution is to align implementation with the java.lang.instrument spec:

      • the agent class must implement a public static premain method if the agent is started from the command-line
      • the agent class must implement a public static agentmain method if the agent is started after VM startup.

      It will find the premain and agentmain method from the agent class but not its superclasses.

      This change will break any existing java agent with a non-public static premain or agentmain methods.

      Such agents will fail to load with an exception like below:

      java.lang.IllegalAccessException: method <fully-qualified-class-name>.agentmain must be declared public

      In addition, this change will break any existing agent which does not implement premain/agentmain but its superclass does.

      The agent class in an unnamed module can still be non-public same as the Main class declaring public static void main allows. Support for modular java agent is a future enhancement ( https://bugs.openjdk.java.net/browse/JDK-6932391)

      Also, see the GitHub Pull Request: https://github.com/openjdk/jdk/pull/1694

      The latest webrev is: https://openjdk.github.io/cr/?repo=jdk&pr=1694&range=08

      Specification

      The implementation is being aligned with the java.lang.instrument spec which remains unchanged:

      https://docs.oracle.com/en/java/javase/15/docs/api/java.instrument/java/lang/instrument/package-summary.html

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              sspitsyn Serguei Spitsyn
              Reporter:
              mchung Mandy Chung
              Reviewed By:
              Alan Bateman, Daniel Daugherty, David Holmes, Mandy Chung
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: