Question regarding JVM crashes in GC

Michael Rasmussen michael.rasmussen at zeroturnaround.com
Mon Apr 10 15:37:23 UTC 2017


On 10 April 2017 at 17:48, Stefan Johansson <stefan.johansson at oracle.com> wrote:
> This sounds great. Could you attach the test case to the mailing list in
> case we want to add this to the OpenJDK testing going forward?

Attached inline below

/Michael

--- agent.c ---

#include <string.h>

#include "jni.h"
#include "jvmti.h"

/*
  public class java.lang.ref.MyWeakReference
    extends java.lang.ref.WeakReference {};
*/
jbyte MyWeakReference_bytes[] = {
  -54,  -2, -70, -66,   0,   0,   0,  49,
    0,   5,   1,   0,  29, 106,  97, 118,
   97,  47, 108,  97, 110, 103,  47, 114,
  101, 102,  47,  77, 121,  87, 101,  97,
  107,  82, 101, 102, 101, 114, 101, 110,
   99, 101,   7,   0,   1,   1,   0,  27,
  106,  97, 118,  97,  47, 108,  97, 110,
  103,  47, 114, 101, 102,  47,  87, 101,
   97, 107,  82, 101, 102, 101, 114, 101,
  110,  99, 101,   7,   0,   3,   0,   1,
    0,   2,   0,   4,   0,   0,   0,   0,
    0,   0,   0,   0
};
jsize MyWeakReference_len = 0x5C;

void JNICALL callback_ClassFileLoadHook(jvmtiEnv *jvmti, JNIEnv* env,
  jclass class_being_redefined, jobject loader, const char* name,
  jobject protection_domain,
  jint class_data_len, const unsigned char* class_data,
  jint* new_class_data_len, unsigned char** new_class_data) {

  if (name && !strcmp(name, "java/lang/System")) {
    (*env)->DefineClass(env, "java/lang/ref/MyWeakReference", NULL,
      MyWeakReference_bytes, MyWeakReference_len);
  }
}

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm,
  char *options, void *reserved) {

  jvmtiEventCallbacks callbacks;
  jvmtiCapabilities caps;
  jvmtiEnv *jvmti;

  (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION_9);

  memset(&caps, 0, sizeof(caps));
  memset(&callbacks, 0, sizeof(callbacks));

  caps.can_generate_early_vmstart = 1;
  caps.can_generate_all_class_hook_events = 1;
  caps.can_generate_early_class_hook_events = 1;

  callbacks.ClassFileLoadHook = &callback_ClassFileLoadHook;

  (*jvmti)->AddCapabilities(jvmti, &caps);
  (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
  (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
    JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);

  return 0;
}

--- MiscTest9.java ---

package app1;

import java.lang.ref.WeakReference;

public class MiscTest9 {
  public static void main(String[] args) throws Exception {
    WeakReference<?> weak = new WeakReference<>(new MiscTest9());

    boolean present = true;
    while (present) {
      System.out.println("Running GC");
      System.gc();
      present = weak.get() != null;
    }
  }
}


More information about the hotspot-dev mailing list