JVM_ENTRY 关于 OpenJDK 源代码

JVM_ENTRY on OpenJDK source code

我阅读了在热点 OpenJDK 7 中抛出异常的代码,并跟踪哪些 类 使用它们。它们最终出现在实现 JVM_ENTRY(result_type, header) 的 类 中,例如 jvm.cppjava.cpp 等。我的问题是 JVM_ENTRY 的功能是什么以及如何实现有用吗?

下面是jvm.cpp

中的例子
JVM_ENTRY(jobject, JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass))
  JVMWrapper("JVM_AllocateNewObject");
  JvmtiVMObjectAllocEventCollector oam;
  // Receiver is not used
  oop curr_mirror = JNIHandles::resolve_non_null(currClass);
  oop init_mirror = JNIHandles::resolve_non_null(initClass);

  // Cannot instantiate primitive types
  if (java_lang_Class::is_primitive(curr_mirror) || java_lang_Class::is_primitive(init_mirror)) {
    ResourceMark rm(THREAD);
    THROW_0(vmSymbols::java_lang_InvalidClassException());
  }

  // Arrays not allowed here, must use JVM_AllocateNewArray
  if (Klass::cast(java_lang_Class::as_klassOop(curr_mirror))->oop_is_javaArray() ||
      Klass::cast(java_lang_Class::as_klassOop(init_mirror))->oop_is_javaArray()) {
    ResourceMark rm(THREAD);
    THROW_0(vmSymbols::java_lang_InvalidClassException());
  }

  instanceKlassHandle curr_klass (THREAD, java_lang_Class::as_klassOop(curr_mirror));
  instanceKlassHandle init_klass (THREAD, java_lang_Class::as_klassOop(init_mirror));

  assert(curr_klass->is_subclass_of(init_klass()), "just checking");

  // Interfaces, abstract classes, and java.lang.Class classes cannot be instantiated directly.
  curr_klass->check_valid_for_instantiation(false, CHECK_NULL);

  // Make sure klass is initialized, since we are about to instantiate one of them.
  curr_klass->initialize(CHECK_NULL);

 methodHandle m (THREAD,
                 init_klass->find_method(vmSymbols::object_initializer_name(),
                                         vmSymbols::void_method_signature()));
  if (m.is_null()) {
    ResourceMark rm(THREAD);
    THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(),
                methodOopDesc::name_and_sig_as_C_string(Klass::cast(init_klass()),
                                          vmSymbols::object_initializer_name(),
                                          vmSymbols::void_method_signature()));
  }

  if (curr_klass ==  init_klass && !m->is_public()) {
    // Calling the constructor for class 'curr_klass'.
    // Only allow calls to a public no-arg constructor.
    // This path corresponds to creating an Externalizable object.
    THROW_0(vmSymbols::java_lang_IllegalAccessException());
  }

  if (!force_verify_field_access(curr_klass(), init_klass(), m->access_flags(), false)) {
    // subclass 'curr_klass' does not have access to no-arg constructor of 'initcb'
    THROW_0(vmSymbols::java_lang_IllegalAccessException());
  }

  Handle obj = curr_klass->allocate_instance_handle(CHECK_NULL);
  // Call constructor m. This might call a constructor higher up in the hierachy
  JavaCalls::call_default_constructor(thread, m, obj, CHECK_NULL);

  return JNIHandles::make_local(obj());
JVM_END

我在 hotspot/src/share/vm/runtime/interfaceSupport.hpp

找到了 JVM_ENTRY 的定义
#define JVM_ENTRY(result_type, header)                               \
extern "C" {                                                         \
  result_type JNICALL header {                                       \
    JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
    ThreadInVMfromNative __tiv(thread);                              \
    debug_only(VMNativeEntryWrapper __vew;)                          \
    VM_ENTRY_BASE(result_type, header, thread)

JVM_ENTRY 是一个预处理器宏,它添加了一些样板代码,这些代码对于 HotSpot JVM API 的所有功能都是通用的。 API 是 JDK class 库的本机代码和 JVM 之间的连接层。

JVM_ENTRY 宏的作用:

  1. JNIEnv* 参数获取指向当前 JavaThread 的指针。
  2. 执行从 _thread_in_native_thread_in_vm 的线程状态转换,必要时在安全点阻塞..
  3. 在函数退出时清除 JNI 本地引用。
  4. 负责 JVM 调试版本中的调试跟踪和验证。