From 29eebc12f43f103b3c010e03adc58bcbba3d7db9 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 22 Jan 2007 23:33:24 +0000 Subject: [PATCH] re PR java/29812 (env->klass value is not updated during the native calls) PR java/29812: * java/lang/natRuntime.cc (_load): Push a new system frame before calling JNI_OnLoad. * include/jvm.h (_Jv_JNI_PopSystemFrame): Declare. (_Jv_GetJNIEnvNewFrameWithLoader): Likewise. * jni.cc (struct _Jv_JNI_LocalFrame) : Now unsigned char. : Now bool. : New field. (_Jv_JNI_EnsureLocalCapacity): Updated. (_Jv_JNI_NewLocalRef): Likewise. (_Jv_JNI_NewLocalRef): Likewise. (_Jv_JNI_PopLocalFrame): Likewise. (_Jv_JNI_FindClass): Likewise. (_Jv_GetJNIEnvNewFrame): Likewise. (_Jv_JNI_AttachCurrentThread): Likewise. (_Jv_GetJNIEnvNewFrameWithLoader): New function. (_Jv_GetJNIEnvNewFrame): Use it. * include/jni_md.h (_CLASSPATH_JNIENV_CONTENTS): Removed 'klass'. From-SVN: r121066 --- libjava/ChangeLog | 21 ++++++++++++ libjava/include/jni_md.h | 5 +-- libjava/include/jvm.h | 4 +-- libjava/java/lang/natRuntime.cc | 7 ++++ libjava/jni.cc | 61 +++++++++++++++++++-------------- 5 files changed, 67 insertions(+), 31 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 08eeba240c08..0513dd605c0a 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,24 @@ +2007-01-22 Tom Tromey + + PR java/29812: + * java/lang/natRuntime.cc (_load): Push a new system frame before + calling JNI_OnLoad. + * include/jvm.h (_Jv_JNI_PopSystemFrame): Declare. + (_Jv_GetJNIEnvNewFrameWithLoader): Likewise. + * jni.cc (struct _Jv_JNI_LocalFrame) : Now unsigned char. + : Now bool. + : New field. + (_Jv_JNI_EnsureLocalCapacity): Updated. + (_Jv_JNI_NewLocalRef): Likewise. + (_Jv_JNI_NewLocalRef): Likewise. + (_Jv_JNI_PopLocalFrame): Likewise. + (_Jv_JNI_FindClass): Likewise. + (_Jv_GetJNIEnvNewFrame): Likewise. + (_Jv_JNI_AttachCurrentThread): Likewise. + (_Jv_GetJNIEnvNewFrameWithLoader): New function. + (_Jv_GetJNIEnvNewFrame): Use it. + * include/jni_md.h (_CLASSPATH_JNIENV_CONTENTS): Removed 'klass'. + 2007-01-16 Jack Howarth * configure.ac: Use multi.m4 from aclocal rather than custom diff --git a/libjava/include/jni_md.h b/libjava/include/jni_md.h index c550338acd70..6ea90ab4c36d 100644 --- a/libjava/include/jni_md.h +++ b/libjava/include/jni_md.h @@ -1,5 +1,5 @@ /* jni_md.h - Copyright (C) 2001, 2005 Free Software Foundation, Inc. + Copyright (C) 2001, 2005, 2007 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -58,9 +58,6 @@ typedef struct _Jv_JavaVM JavaVM; /* The current exception. */ \ jthrowable ex; \ \ - /* The class of the current native method. */ \ - jclass klass; \ - \ /* The chain of local frames. */ \ struct _Jv_JNI_LocalFrame *locals; \ \ diff --git a/libjava/include/jvm.h b/libjava/include/jvm.h index 88b45be983db..55cef27f0c36 100644 --- a/libjava/include/jvm.h +++ b/libjava/include/jvm.h @@ -567,8 +567,8 @@ void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *); /* Free a JNIEnv. */ void _Jv_FreeJNIEnv (_Jv_JNIEnv *); -/* Free a JNIEnv. */ -void _Jv_FreeJNIEnv (_Jv_JNIEnv *); +extern "C" void _Jv_JNI_PopSystemFrame (_Jv_JNIEnv *); +_Jv_JNIEnv *_Jv_GetJNIEnvNewFrameWithLoader (::java::lang::ClassLoader *); struct _Jv_JavaVM; _Jv_JavaVM *_Jv_GetJavaVM (); diff --git a/libjava/java/lang/natRuntime.cc b/libjava/java/lang/natRuntime.cc index 5cca1b072a3d..a59ec4bada4c 100644 --- a/libjava/java/lang/natRuntime.cc +++ b/libjava/java/lang/natRuntime.cc @@ -203,7 +203,14 @@ java::lang::Runtime::_load (jstring path, jboolean do_search) // FIXME: what? return; } + + // Push a new frame so that JNI_OnLoad will get the right class + // loader if it calls FindClass. + ::java::lang::ClassLoader *loader + = _Jv_StackTrace::GetFirstNonSystemClassLoader(); + JNIEnv *env = _Jv_GetJNIEnvNewFrameWithLoader (loader); jint vers = ((jint (JNICALL *) (JavaVM *, void *)) onload) (vm, NULL); + _Jv_JNI_PopSystemFrame (env); if (vers != JNI_VERSION_1_1 && vers != JNI_VERSION_1_2 && vers != JNI_VERSION_1_4) { diff --git a/libjava/jni.cc b/libjava/jni.cc index c5c2b0ff3cd9..1ad37353797d 100644 --- a/libjava/jni.cc +++ b/libjava/jni.cc @@ -85,16 +85,18 @@ extern struct JNIInvokeInterface _Jv_JNI_InvokeFunctions; // This structure is used to keep track of local references. struct _Jv_JNI_LocalFrame { - // This is true if this frame object represents a pushed frame (eg - // from PushLocalFrame). - int marker; + // This is one of the MARK_ constants. + unsigned char marker; // Flag to indicate some locals were allocated. - int allocated_p; + bool allocated_p; // Number of elements in frame. int size; + // The class loader of the JNI method that allocated this frame. + ::java::lang::ClassLoader *loader; + // Next frame in chain. _Jv_JNI_LocalFrame *next; @@ -311,8 +313,9 @@ _Jv_JNI_EnsureLocalCapacity (JNIEnv *env, jint size) frame->marker = MARK_NONE; frame->size = size; - frame->allocated_p = 0; + frame->allocated_p = false; memset (&frame->vec[0], 0, size * sizeof (jobject)); + frame->loader = env->locals->loader; frame->next = env->locals; env->locals = frame; @@ -350,7 +353,7 @@ _Jv_JNI_NewLocalRef (JNIEnv *env, jobject obj) set = true; done = true; frame->vec[i] = obj; - frame->allocated_p = 1; + frame->allocated_p = true; break; } } @@ -368,7 +371,7 @@ _Jv_JNI_NewLocalRef (JNIEnv *env, jobject obj) _Jv_JNI_EnsureLocalCapacity (env, 16); // We know the first element of the new frame will be ok. env->locals->vec[0] = obj; - env->locals->allocated_p = 1; + env->locals->allocated_p = true; } mark_for_gc (obj, local_ref_table); @@ -397,7 +400,7 @@ _Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result, int stop) { if (rf->allocated_p) memset (&rf->vec[0], 0, rf->size * sizeof (jobject)); - rf->allocated_p = 0; + rf->allocated_p = false; rf = NULL; break; } @@ -541,8 +544,8 @@ _Jv_JNI_FindClass (JNIEnv *env, const char *name) jstring n = JvNewStringUTF (s); java::lang::ClassLoader *loader = NULL; - if (env->klass != NULL) - loader = env->klass->getClassLoaderInternal (); + if (env->locals->loader != NULL) + loader = env->locals->loader; if (loader == NULL) { @@ -2086,18 +2089,14 @@ mangled_name (jclass klass, _Jv_Utf8Const *func_name, buf[here] = '\0'; } -// Return the current thread's JNIEnv; if one does not exist, create -// it. Also create a new system frame for use. This is `extern "C"' -// because the compiler calls it. -extern "C" JNIEnv * -_Jv_GetJNIEnvNewFrame (jclass klass) +JNIEnv * +_Jv_GetJNIEnvNewFrameWithLoader (::java::lang::ClassLoader *loader) { JNIEnv *env = _Jv_GetCurrentJNIEnv (); if (__builtin_expect (env == NULL, false)) { env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv)); env->p = &_Jv_JNIFunctions; - env->klass = klass; env->locals = NULL; // We set env->ex below. @@ -2106,11 +2105,12 @@ _Jv_GetJNIEnvNewFrame (jclass klass) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame) + (FRAME_SIZE * sizeof (jobject))); - + env->bottom_locals->marker = MARK_SYSTEM; env->bottom_locals->size = FRAME_SIZE; env->bottom_locals->next = NULL; - env->bottom_locals->allocated_p = 0; + env->bottom_locals->allocated_p = false; + // We set the klass field below. memset (&env->bottom_locals->vec[0], 0, env->bottom_locals->size * sizeof (jobject)); @@ -2122,23 +2122,25 @@ _Jv_GetJNIEnvNewFrame (jclass klass) // built, above. if (__builtin_expect (env->locals == NULL, true)) - env->locals = env->bottom_locals; - + { + env->locals = env->bottom_locals; + env->locals->loader = loader; + } else { // Alternatively, we might be re-entering JNI, in which case we can't // reuse the bottom_locals frame, because it is already underneath // us. So we need to make a new one. - _Jv_JNI_LocalFrame *frame = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame) + (FRAME_SIZE * sizeof (jobject))); - + frame->marker = MARK_SYSTEM; frame->size = FRAME_SIZE; - frame->allocated_p = 0; + frame->allocated_p = false; frame->next = env->locals; + frame->loader = loader; memset (&frame->vec[0], 0, frame->size * sizeof (jobject)); @@ -2151,6 +2153,15 @@ _Jv_GetJNIEnvNewFrame (jclass klass) return env; } +// Return the current thread's JNIEnv; if one does not exist, create +// it. Also create a new system frame for use. This is `extern "C"' +// because the compiler calls it. +extern "C" JNIEnv * +_Jv_GetJNIEnvNewFrame (jclass klass) +{ + return _Jv_GetJNIEnvNewFrameWithLoader (klass->getClassLoaderInternal()); +} + // Destroy the env's reusable resources. This is called from the thread // destructor "finalize_native" in natThread.cc void @@ -2391,7 +2402,6 @@ _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv, return JNI_ERR; env->p = &_Jv_JNIFunctions; env->ex = NULL; - env->klass = NULL; env->bottom_locals = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame) + (FRAME_SIZE @@ -2403,9 +2413,10 @@ _Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv, return JNI_ERR; } - env->locals->allocated_p = 0; + env->locals->allocated_p = false; env->locals->marker = MARK_SYSTEM; env->locals->size = FRAME_SIZE; + env->locals->loader = NULL; env->locals->next = NULL; for (int i = 0; i < env->locals->size; ++i)