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

Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing

    Details

    • Subcomponent:
      jfr
    • Resolved In Build:
      b25

      Backports

        Description

        Threads::create_vm()...
         
          // Notify JVMTI agents that VM initialization is complete - nop if no agents.
        4053 JvmtiExport::post_vm_initialized();

        4055 JFR_ONLY(Jfr::on_vm_start();)
        ...

        Runs into the problematic situation that an agent can initialize JFR (using the JFR default options) in Premain in response to the JVMTI VMInit event before the command-line options specified for JFR is processed and applied.

        There is also a less common issue in that an agent can initialize JFR in response to the JVMTI VMStart event. An agent is allowed to invoke JNI code at this point and could attempt to create and start a recording, for example:

        void JNICALL on_vm_start(jvmtiEnv *jvmti_env, JNIEnv* jni_env) {
          printf("On VM Start Event - JNI is up\n");
          jclass recording_clz = (*jni_env)->FindClass(jni_env, "jdk/jfr/Recording");
          jmethodID init_method = (*jni_env)->GetMethodID(jni_env, recording_clz, "<init>", "()V");
          jobject recording = (*jni_env)->NewObject(jni_env, recording_clz, init_method); <<-- (attempts to) initialize JFR
          jmethodID start_method = (*jni_env)->GetMethodID(jni_env, recording_clz, "start", "()V");
          (*jni_env)->CallVoidMethod(jni_env, recording, start_method);
        }

        Besides running into the issue with not using the options specified on the command-line, the attempt will fail because JVMTI has not yet entered phase JVMTI_PHASE_LIVE which is required if Retransform is to be used (JFR uses it if JVMTI is available).

        Today, the start attempt continues even though the Retransform failed (JVMTI_ERROR_WRONG_PHASE). This is not correct.

        A proper error message should explain that attempting to start JFR in response to JVMTI event VMStart is disallowed because the VM has not completed initialization yet. A recommendation to use JVMTI event VMInit instead of VMStart can also be printed before fail fast with vm_exit_during_initialization().

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  mgronlun Markus Grönlund
                  Reporter:
                  mgronlun Markus Grönlund
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  5 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: