// jni.cc - JNI implementation, including the jump table.
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
Free Software Foundation
This file is part of libgcj.
#ifdef ENABLE_JVMPI
#include <jvmpi.h>
#endif
+#include <jvmti.h>
+#include "jvmti-int.h"
#include <java/lang/Class.h>
#include <java/lang/ClassLoader.h>
#include <java/nio/DirectByteBufferImpl$ReadWrite.h>
#include <java/util/IdentityHashMap.h>
#include <gnu/gcj/RawData.h>
+#include <java/lang/ClassNotFoundException.h>
#include <gcj/method.h>
#include <gcj/field.h>
// 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;
static java::util::IdentityHashMap *global_ref_table;
// The only VM.
-static JavaVM *the_vm;
+JavaVM *_Jv_the_vm;
#ifdef ENABLE_JVMPI
// The only JVMPI interface description.
return reinterpret_cast<T *> (wr->get ());
}
+jobject
+_Jv_UnwrapJNIweakReference (jobject obj)
+{
+ return unwrap (obj);
+}
+
\f
static jobject JNICALL
{
// This seems weird but I think it is correct.
obj = unwrap (obj);
+
+ // NULL is ok here -- the JNI specification doesn't say so, but this
+ // is a no-op.
+ if (! obj)
+ return;
+
unmark_for_gc (obj, global_ref_table);
}
// This seems weird but I think it is correct.
obj = unwrap (obj);
+ // NULL is ok here -- the JNI specification doesn't say so, but this
+ // is a no-op.
+ if (! obj)
+ return;
+
for (frame = env->locals; frame != NULL; frame = frame->next)
{
for (int i = 0; i < frame->size; ++i)
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;
set = true;
done = true;
frame->vec[i] = obj;
- frame->allocated_p = 1;
+ frame->allocated_p = true;
break;
}
}
_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);
{
if (rf->allocated_p)
memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
- rf->allocated_p = 0;
+ rf->allocated_p = false;
rf = NULL;
break;
}
{
jthrowable t = env->ex;
env->ex = NULL;
+ if (JVMTI_REQUESTED_EVENT (Exception))
+ _Jv_ReportJVMTIExceptionThrow (t);
throw t;
}
}
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)
{
}
r = loader->loadClass (n);
+ _Jv_InitClass (r);
}
catch (jthrowable t)
{
static jboolean JNICALL
_Jv_JNI_IsAssignableFrom (JNIEnv *, jclass clazz1, jclass clazz2)
{
- return unwrap (clazz1)->isAssignableFrom (unwrap (clazz2));
+ return unwrap (clazz2)->isAssignableFrom (unwrap (clazz1));
}
static jint JNICALL
java::lang::StringBuffer *name_sig =
new java::lang::StringBuffer (JvNewStringUTF (name));
- name_sig->append ((jchar) ' ')->append (JvNewStringUTF (s));
+ name_sig->append ((jchar) ' ');
+ name_sig->append (JvNewStringUTF (s));
env->ex = new java::lang::NoSuchMethodError (name_sig->toString ());
}
catch (jthrowable t)
jmethodID id, va_list args)
{
JvAssert (klass && ! klass->isArray ());
- JvAssert (! strcmp (id->name->data, "<init>")
- && id->signature->length > 2
- && id->signature->data[0] == '('
- && ! strcmp (&id->signature->data[id->signature->length - 2],
+ JvAssert (! strcmp (id->name->chars(), "<init>")
+ && id->signature->len() > 2
+ && id->signature->chars()[0] == '('
+ && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
")V"));
return _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
_Jv_JNI_NewObject (JNIEnv *env, jclass klass, jmethodID id, ...)
{
JvAssert (klass && ! klass->isArray ());
- JvAssert (! strcmp (id->name->data, "<init>")
- && id->signature->length > 2
- && id->signature->data[0] == '('
- && ! strcmp (&id->signature->data[id->signature->length - 2],
+ JvAssert (! strcmp (id->name->chars(), "<init>")
+ && id->signature->len() > 2
+ && id->signature->chars()[0] == '('
+ && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
")V"));
va_list args;
jvalue *args)
{
JvAssert (klass && ! klass->isArray ());
- JvAssert (! strcmp (id->name->data, "<init>")
- && id->signature->length > 2
- && id->signature->data[0] == '('
- && ! strcmp (&id->signature->data[id->signature->length - 2],
+ JvAssert (! strcmp (id->name->chars(), "<init>")
+ && id->signature->len() > 2
+ && id->signature->chars()[0] == '('
+ && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
")V"));
return _Jv_JNI_CallAnyMethodA<jobject, constructor> (env, NULL, klass,
char s[len + 1];
for (int i = 0; i <= len; ++i)
s[i] = (sig[i] == '/') ? '.' : sig[i];
- jclass field_class = _Jv_FindClassFromSignature ((char *) s, NULL);
-
- // FIXME: what if field_class == NULL?
-
java::lang::ClassLoader *loader = clazz->getClassLoaderInternal ();
+ jclass field_class = _Jv_FindClassFromSignature ((char *) s, loader);
+ if (! field_class)
+ throw new java::lang::ClassNotFoundException(JvNewStringUTF(s));
+
while (clazz != NULL)
{
// We acquire the class lock so that fields aren't resolved
void JNICALL
_Jv_JNI_DeleteWeakGlobalRef (JNIEnv *, jweak obj)
{
+ // JDK compatibility.
+ if (obj == NULL)
+ return;
+
using namespace gnu::gcj::runtime;
JNIWeakRef *ref = reinterpret_cast<JNIWeakRef *> (obj);
unmark_for_gc (ref, global_ref_table);
\f
+struct NativeMethodCacheEntry : public JNINativeMethod
+{
+ char *className;
+};
+
// Hash table of native methods.
-static JNINativeMethod *nathash;
+static NativeMethodCacheEntry *nathash;
// Number of slots used.
static int nathash_count = 0;
// Number of slots available. Must be power of 2.
// Compute a hash value for a native method descriptor.
static int
-hash (const JNINativeMethod *method)
+hash (const NativeMethodCacheEntry *method)
{
char *ptr;
int hash = 0;
+ ptr = method->className;
+ while (*ptr)
+ hash = (31 * hash) + *ptr++;
+
ptr = method->name;
while (*ptr)
hash = (31 * hash) + *ptr++;
}
// Find the slot where a native method goes.
-static JNINativeMethod *
-nathash_find_slot (const JNINativeMethod *method)
+static NativeMethodCacheEntry *
+nathash_find_slot (const NativeMethodCacheEntry *method)
{
jint h = hash (method);
int step = (h ^ (h >> 16)) | 1;
for (;;)
{
- JNINativeMethod *slotp = &nathash[w];
+ NativeMethodCacheEntry *slotp = &nathash[w];
if (slotp->name == NULL)
{
if (del >= 0)
else if (slotp->name == DELETED_ENTRY)
del = w;
else if (! strcmp (slotp->name, method->name)
- && ! strcmp (slotp->signature, method->signature))
+ && ! strcmp (slotp->signature, method->signature)
+ && ! strcmp (slotp->className, method->className))
return slotp;
w = (w + step) & (nathash_size - 1);
}
// Find a method. Return NULL if it isn't in the hash table.
static void *
-nathash_find (JNINativeMethod *method)
+nathash_find (NativeMethodCacheEntry *method)
{
if (nathash == NULL)
return NULL;
- JNINativeMethod *slot = nathash_find_slot (method);
+ NativeMethodCacheEntry *slot = nathash_find_slot (method);
if (slot->name == NULL || slot->name == DELETED_ENTRY)
return NULL;
return slot->fnPtr;
{
nathash_size = 1024;
nathash =
- (JNINativeMethod *) _Jv_AllocBytes (nathash_size
- * sizeof (JNINativeMethod));
- memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
+ (NativeMethodCacheEntry *) _Jv_AllocBytes (nathash_size
+ * sizeof (NativeMethodCacheEntry));
}
else
{
int savesize = nathash_size;
- JNINativeMethod *savehash = nathash;
+ NativeMethodCacheEntry *savehash = nathash;
nathash_size *= 2;
nathash =
- (JNINativeMethod *) _Jv_AllocBytes (nathash_size
- * sizeof (JNINativeMethod));
- memset (nathash, 0, nathash_size * sizeof (JNINativeMethod));
+ (NativeMethodCacheEntry *) _Jv_AllocBytes (nathash_size
+ * sizeof (NativeMethodCacheEntry));
for (int i = 0; i < savesize; ++i)
{
if (savehash[i].name != NULL && savehash[i].name != DELETED_ENTRY)
{
- JNINativeMethod *slot = nathash_find_slot (&savehash[i]);
+ NativeMethodCacheEntry *slot = nathash_find_slot (&savehash[i]);
*slot = savehash[i];
}
}
}
static void
-nathash_add (const JNINativeMethod *method)
+nathash_add (const NativeMethodCacheEntry *method)
{
if (3 * nathash_count >= 2 * nathash_size)
natrehash ();
- JNINativeMethod *slot = nathash_find_slot (method);
+ NativeMethodCacheEntry *slot = nathash_find_slot (method);
// If the slot has a real entry in it, then there is no work to do.
if (slot->name != NULL && slot->name != DELETED_ENTRY)
return;
- // FIXME
+ // FIXME: memory leak?
slot->name = strdup (method->name);
- slot->signature = strdup (method->signature);
+ slot->className = strdup (method->className);
+ // This was already strduped in _Jv_JNI_RegisterNatives.
+ slot->signature = method->signature;
slot->fnPtr = method->fnPtr;
}
// the nathash table.
JvSynchronize sync (global_ref_table);
+ NativeMethodCacheEntry dottedMethod;
+
// Look at each descriptor given us, and find the corresponding
// method in the class.
for (int j = 0; j < nMethods; ++j)
{
_Jv_Method *self = &imeths[i];
- if (! strcmp (self->name->chars (), methods[j].name)
- && ! strcmp (self->signature->chars (), methods[j].signature))
+ // Copy this JNINativeMethod and do a slash to dot
+ // conversion on the signature.
+ dottedMethod.name = methods[j].name;
+ // FIXME: we leak a little memory here if the method
+ // is not found.
+ dottedMethod.signature = strdup (methods[j].signature);
+ dottedMethod.fnPtr = methods[j].fnPtr;
+ dottedMethod.className = _Jv_GetClassNameUtf8 (klass)->chars();
+ char *c = dottedMethod.signature;
+ while (*c)
+ {
+ if (*c == '/')
+ *c = '.';
+ c++;
+ }
+
+ if (! strcmp (self->name->chars (), dottedMethod.name)
+ && ! strcmp (self->signature->chars (), dottedMethod.signature))
{
if (! (self->accflags & java::lang::reflect::Modifier::NATIVE))
break;
// Found a match that is native.
found = true;
- nathash_add (&methods[j]);
+ nathash_add (&dottedMethod);
break;
}
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.
_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));
// 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));
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
buf[name_length] = '\0';
strncpy (buf + name_length + 1, signature->chars (), sig_length);
buf[name_length + sig_length + 1] = '\0';
- JNINativeMethod meth;
+ NativeMethodCacheEntry meth;
meth.name = buf;
meth.signature = buf + name_length + 1;
+ meth.className = _Jv_GetClassNameUtf8(klass)->chars();
function = nathash_find (&meth);
if (function != NULL)
return function;
// Copy over passed-in arguments.
memcpy (&real_args[offset], args, _this->args_raw_size);
+
+ // Add a frame to the composite (interpreted + JNI) call stack
+ java::lang::Thread *thread = java::lang::Thread::currentThread();
+ _Jv_NativeFrame nat_frame (_this, thread);
// The actual call to the JNI function.
#if FFI_NATIVE_RAW_API
ret, real_args);
#endif
+ // We might need to unwrap a JNI weak reference here.
+ if (_this->jni_cif.rtype == &ffi_type_pointer)
+ {
+ _Jv_value *val = (_Jv_value *) ret;
+ val->object_value = unwrap (val->object_value);
+ }
+
if (sync != NULL)
_Jv_MonitorExit (sync);
}
// Attaching an already-attached thread is a no-op.
- if (_Jv_GetCurrentJNIEnv () != NULL)
- return 0;
+ JNIEnv *env = _Jv_GetCurrentJNIEnv ();
+ if (env != NULL)
+ {
+ *penv = reinterpret_cast<void *> (env);
+ return 0;
+ }
- JNIEnv *env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
+ env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
if (env == NULL)
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
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)
}
// This is the one actually used by JNI.
-static jint JNICALL
+jint JNICALL
_Jv_JNI_AttachCurrentThread (JavaVM *vm, void **penv, void *args)
{
return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
static jint JNICALL
_Jv_JNI_DestroyJavaVM (JavaVM *vm)
{
- JvAssert (the_vm && vm == the_vm);
+ JvAssert (_Jv_the_vm && vm == _Jv_the_vm);
+
+ union
+ {
+ JNIEnv *env;
+ void *env_p;
+ };
- JNIEnv *env;
if (_Jv_ThreadCurrent () != NULL)
{
jstring main_name;
return JNI_ERR;
}
- jint r = _Jv_JNI_AttachCurrentThread (vm, main_name,
- reinterpret_cast<void **> (&env),
+ jint r = _Jv_JNI_AttachCurrentThread (vm, main_name, &env_p,
NULL, false);
if (r < 0)
return r;
}
#endif
+ // Handle JVMTI requests
+ if (version == JVMTI_VERSION_1_0)
+ {
+ *penv = (void *) _Jv_GetJVMTIEnv ();
+ return 0;
+ }
+
// FIXME: do we really want to support 1.1?
if (version != JNI_VERSION_1_4 && version != JNI_VERSION_1_2
&& version != JNI_VERSION_1_1)
return 0;
}
-jint JNICALL
-JNI_GetDefaultJavaVMInitArgs (void *args)
-{
- jint version = * (jint *) args;
- // Here we only support 1.2 and 1.4.
- if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
- return JNI_EVERSION;
-
- JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
- ia->version = JNI_VERSION_1_4;
- ia->nOptions = 0;
- ia->options = NULL;
- ia->ignoreUnrecognized = true;
-
- return 0;
-}
-
-jint JNICALL
-JNI_CreateJavaVM (JavaVM **vm, void **penv, void *args)
-{
- JvAssert (! the_vm);
-
- _Jv_CreateJavaVM (NULL);
-
- // FIXME: synchronize
- JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
- if (nvm == NULL)
- return JNI_ERR;
- nvm->functions = &_Jv_JNI_InvokeFunctions;
-
- // Parse the arguments.
- if (args != NULL)
- {
- jint version = * (jint *) args;
- // We only support 1.2 and 1.4.
- if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4)
- return JNI_EVERSION;
- JavaVMInitArgs *ia = reinterpret_cast<JavaVMInitArgs *> (args);
- for (int i = 0; i < ia->nOptions; ++i)
- {
- if (! strcmp (ia->options[i].optionString, "vfprintf")
- || ! strcmp (ia->options[i].optionString, "exit")
- || ! strcmp (ia->options[i].optionString, "abort"))
- {
- // We are required to recognize these, but for now we
- // don't handle them in any way. FIXME.
- continue;
- }
- else if (! strncmp (ia->options[i].optionString,
- "-verbose", sizeof ("-verbose") - 1))
- {
- // We don't do anything with this option either. We
- // might want to make sure the argument is valid, but we
- // don't really care all that much for now.
- continue;
- }
- else if (! strncmp (ia->options[i].optionString, "-D", 2))
- {
- // FIXME.
- continue;
- }
- else if (ia->ignoreUnrecognized)
- {
- if (ia->options[i].optionString[0] == '_'
- || ! strncmp (ia->options[i].optionString, "-X", 2))
- continue;
- }
-
- return JNI_ERR;
- }
- }
-
- jint r =_Jv_JNI_AttachCurrentThread (nvm, penv, NULL);
- if (r < 0)
- return r;
-
- the_vm = nvm;
- *vm = the_vm;
-
- return 0;
-}
-
-jint JNICALL
-JNI_GetCreatedJavaVMs (JavaVM **vm_buffer, jsize buf_len, jsize *n_vms)
-{
- if (buf_len <= 0)
- return JNI_ERR;
-
- // We only support a single VM.
- if (the_vm != NULL)
- {
- vm_buffer[0] = the_vm;
- *n_vms = 1;
- }
- else
- *n_vms = 0;
- return 0;
-}
-
JavaVM *
_Jv_GetJavaVM ()
{
// FIXME: synchronize
- if (! the_vm)
+ if (! _Jv_the_vm)
{
JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
if (nvm != NULL)
nvm->functions = &_Jv_JNI_InvokeFunctions;
- the_vm = nvm;
+ _Jv_the_vm = nvm;
}
// If this is a Java thread, we want to make sure it has an
if (_Jv_ThreadCurrent () != NULL)
{
void *ignore;
- _Jv_JNI_AttachCurrentThread (the_vm, &ignore, NULL);
+ _Jv_JNI_AttachCurrentThread (_Jv_the_vm, &ignore, NULL);
}
- return the_vm;
+ return _Jv_the_vm;
}
static jint JNICALL