1 // natClassLoader.cc - Implementation of java.lang.ClassLoader native methods.
3 /* Copyright (C) 1999, 2000 Cygnus Solutions
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
21 #include <java-threads.h>
22 #include <java-interp.h>
24 #include <java/lang/Character.h>
25 #include <java/lang/Thread.h>
26 #include <java/lang/ClassLoader.h>
27 #include <gnu/gcj/runtime/VMClassLoader.h>
28 #include <java/lang/InternalError.h>
29 #include <java/lang/IllegalAccessError.h>
30 #include <java/lang/LinkageError.h>
31 #include <java/lang/ClassFormatError.h>
32 #include <java/lang/NoClassDefFoundError.h>
33 #include <java/lang/ClassNotFoundException.h>
34 #include <java/lang/ClassCircularityError.h>
35 #include <java/lang/IncompatibleClassChangeError.h>
36 #include <java/lang/reflect/Modifier.h>
37 #include <java/lang/Runtime.h>
39 #define CloneableClass _CL_Q34java4lang9Cloneable
40 extern java::lang::Class CloneableClass;
41 #define ObjectClass _CL_Q34java4lang6Object
42 extern java::lang::Class ObjectClass;
43 #define ClassClass _CL_Q34java4lang5Class
44 extern java::lang::Class ClassClass;
45 #define VMClassLoaderClass _CL_Q34java4lang17VMClassLoader
46 extern java::lang::Class VMClassLoader;
47 #define ClassLoaderClass _CL_Q34java4lang11ClassLoader
48 extern java::lang::Class ClassLoaderClass;
50 /////////// java.lang.ClassLoader native methods ////////////
52 java::lang::ClassLoader *
53 java::lang::ClassLoader::getSystemClassLoader (void)
55 JvSynchronize sync (&ClassLoaderClass);
57 system = gnu::gcj::runtime::VMClassLoader::getVMClassLoader ();
62 java::lang::ClassLoader::defineClass2 (jclass klass, jbyteArray data,
63 jint offset, jint length)
66 _Jv_DefineClass (klass, data, offset, length);
71 java::lang::ClassLoader::defineClass0 (jstring name,
78 klass = (jclass) JvAllocObject (&ClassClass, sizeof (_Jv_InterpClass));
80 // synchronize on the class, so that it is not
81 // attempted initialized until we're done loading.
82 _Jv_MonitorEnter (klass);
84 // record which is the defining loader
87 // register that we are the initiating loader...
90 _Jv_Utf8Const * name2 = _Jv_makeUtf8Const (name);
92 _Jv_VerifyClassName (name2);
97 // this will do the magic. loadInto also operates
98 // as an exception trampoline for now...
99 Throwable *ex = defineClass1 (klass, data, offset, length);
101 if (ex) // we failed to load it
103 klass->state = JV_STATE_ERROR;
106 _Jv_UnregisterClass (klass);
108 _Jv_MonitorExit (klass);
110 // FIXME: Here we may want to test that EX does
111 // indeed represent a valid exception. That is,
112 // anything but ClassNotFoundException,
113 // or some kind of Error.
118 // if everything proceeded sucessfully, we're loaded.
119 JvAssert (klass->state == JV_STATE_LOADED);
121 // if an exception is generated, this is initially missed.
122 // however, we come back here in handleException0 below...
123 _Jv_MonitorExit (klass);
134 _Jv_WaitForState (jclass klass, int state)
136 if (klass->state >= state)
139 _Jv_MonitorEnter (klass) ;
141 if (state == JV_STATE_LINKED)
143 // Must call _Jv_PrepareCompiledClass while holding the class
145 _Jv_PrepareCompiledClass (klass);
146 _Jv_MonitorExit (klass);
150 java::lang::Thread *self = java::lang::Thread::currentThread();
152 // this is similar to the strategy for class initialization.
153 // if we already hold the lock, just leave.
154 while (klass->state <= state
156 && klass->thread != self)
159 _Jv_MonitorExit (klass);
161 if (klass->state == JV_STATE_ERROR)
163 _Jv_Throw (new java::lang::LinkageError ());
167 // Finish linking a class. Only called from ClassLoader::resolveClass.
169 java::lang::ClassLoader::linkClass0 (java::lang::Class *klass)
171 if (klass->state >= JV_STATE_LINKED)
175 if (_Jv_IsInterpretedClass (klass))
177 _Jv_PrepareClass (klass);
181 _Jv_PrepareCompiledClass (klass);
185 java::lang::ClassLoader::markClassErrorState0 (java::lang::Class *klass)
187 klass->state = JV_STATE_ERROR;
192 /** this is the only native method in VMClassLoader, so
193 we define it here. */
195 gnu::gcj::runtime::VMClassLoader::findSystemClass (jstring name)
197 _Jv_Utf8Const *name_u = _Jv_makeUtf8Const (name);
198 jclass klass = _Jv_FindClassInCache (name_u, 0);
202 // Turn `gnu.pkg.quux' into `gnu-pkg-quux'. Then search for a
203 // module named (eg, on Linux) `gnu-pkg-quux.so', followed by
204 // `gnu-pkg.so' and `gnu.so'. If loading one of these causes
205 // the class to appear in the cache, then use it.
206 jstring so_base_name = name->replace ('.', '-');
208 while (! klass && so_base_name && so_base_name->length() > 0)
210 using namespace ::java::lang;
211 Runtime *rt = Runtime::getRuntime();
212 jboolean loaded = rt->loadLibraryInternal (so_base_name);
214 jint nd = so_base_name->lastIndexOf ('-');
218 so_base_name = so_base_name->substring (0, nd);
221 klass = _Jv_FindClassInCache (name_u, 0);
229 java::lang::ClassLoader::findLoadedClass (jstring name)
231 return _Jv_FindClassInCache (_Jv_makeUtf8Const (name), this);
235 /** This function does class-preparation for compiled classes.
236 NOTE: It contains replicated functionality from
237 _Jv_ResolvePoolEntry, and this is intentional, since that function
238 lives in resolve.cc which is entirely conditionally compiled.
241 _Jv_PrepareCompiledClass(jclass klass)
243 if (klass->state >= JV_STATE_LINKED)
246 // Short-circuit, so that mutually dependent classes are ok.
247 klass->state = JV_STATE_LINKED;
249 _Jv_Constants *pool = &klass->constants;
250 for (int index = 1; index < pool->size; ++index)
252 if (pool->tags[index] == JV_CONSTANT_Class)
254 _Jv_Utf8Const *name = pool->data[index].utf8;
257 if (name->data[0] == '[')
258 found = _Jv_FindClassFromSignature (&name->data[0],
261 found = _Jv_FindClass (name, klass->loader);
265 jstring str = _Jv_NewStringUTF (name->data);
266 JvThrow (new java::lang::ClassNotFoundException (str));
269 pool->data[index].clazz = found;
270 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
273 else if (pool->tags[index] == JV_CONSTANT_String)
276 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
277 pool->data[index].o = str;
278 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
287 // A single class can have many "initiating" class loaders,
288 // and a single "defining" class loader. The Defining
289 // class loader is what is returned from Class.getClassLoader()
290 // and is used when loading dependent classes during resolution.
291 // The set of initiating class loaders are used to ensure
292 // safety of linking, and is maintained in the hash table
293 // "initiated_classes". A defining classloader is by definition also
294 // initiating, so we only store classes in this table, if they have more
295 // than one class loader associated.
299 // Size of local hash table.
300 #define HASH_LEN 1013
302 // Hash function for Utf8Consts.
303 #define HASH_UTF(Utf) (((Utf)->hash) % HASH_LEN)
305 struct _Jv_LoaderInfo {
306 _Jv_LoaderInfo *next;
307 java::lang::Class *klass;
308 java::lang::ClassLoader *loader;
311 _Jv_LoaderInfo *initiated_classes[HASH_LEN];
312 jclass loaded_classes[HASH_LEN];
315 _Jv_FindClassInCache (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
317 _Jv_MonitorEnter (&ClassClass);
318 jint hash = HASH_UTF (name);
320 // first, if LOADER is a defining loader, then it is also initiating
322 for (klass = loaded_classes[hash]; klass; klass = klass->next)
324 if (loader == klass->loader && _Jv_equalUtf8Consts (name, klass->name))
328 // otherwise, it may be that the class in question was defined
329 // by some other loader, but that the loading was initiated by
330 // the loader in question.
333 _Jv_LoaderInfo *info;
334 for (info = initiated_classes[hash]; info; info = info->next)
336 if (loader == info->loader
337 && _Jv_equalUtf8Consts (name, info->klass->name))
345 _Jv_MonitorExit (&ClassClass);
351 _Jv_UnregisterClass (jclass the_class)
353 _Jv_MonitorEnter (&ClassClass);
354 jint hash = HASH_UTF(the_class->name);
356 jclass *klass = &(loaded_classes[hash]);
357 for ( ; *klass; klass = &((*klass)->next))
359 if (*klass == the_class)
361 *klass = (*klass)->next;
366 _Jv_LoaderInfo **info = &(initiated_classes[hash]);
367 for ( ; *info; info = &((*info)->next))
369 while ((*info)->klass == the_class)
371 *info = (*info)->next;
375 _Jv_MonitorExit (&ClassClass);
379 _Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
381 _Jv_LoaderInfo *info = new _Jv_LoaderInfo; // non-gc alloc!
382 jint hash = HASH_UTF(klass->name);
384 _Jv_MonitorEnter (&ClassClass);
385 info->loader = loader;
387 info->next = initiated_classes[hash];
388 initiated_classes[hash] = info;
389 _Jv_MonitorExit (&ClassClass);
393 // This function is called many times during startup, before main() is
394 // run. We do our runtime initialization here the very first time we
395 // are called. At that point in time we know for certain we are
396 // running single-threaded, so we don't need to lock when modifying
397 // `init'. CLASSES is NULL-terminated.
399 _Jv_RegisterClasses (jclass *classes)
401 static bool init = false;
408 _Jv_InitializeSyncMutex ();
411 JvSynchronize sync (&ClassClass);
412 for (; *classes; ++classes)
414 jclass klass = *classes;
415 jint hash = HASH_UTF (klass->name);
416 klass->next = loaded_classes[hash];
417 loaded_classes[hash] = klass;
419 // registering a compiled class causes
420 // it to be immediately "prepared".
421 if (klass->state == JV_STATE_NOTHING)
422 klass->state = JV_STATE_COMPILED;
427 _Jv_RegisterClass (jclass klass)
432 _Jv_RegisterClasses (classes);
436 _Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
438 jclass klass = _Jv_FindClassInCache (name, loader);
442 jstring sname = _Jv_NewStringUTF (name->data);
446 // Load using a user-defined loader, jvmspec 5.3.2
447 klass = loader->loadClass(sname, false);
449 // If "loader" delegated the loadClass operation to another
450 // loader, explicitly register that it is also an initiating
451 // loader of the given class.
452 if (klass && (klass->getClassLoader () != loader))
453 _Jv_RegisterInitiatingLoader (klass, loader);
457 java::lang::ClassLoader *sys = java::lang::ClassLoader::system;
460 _Jv_InitClass (&ClassLoaderClass);
461 sys = java::lang::ClassLoader::getSystemClassLoader ();
464 // Load using the bootstrap loader jvmspec 5.3.1.
465 klass = sys->loadClass (sname, false);
467 // Register that we're an initiating loader.
469 _Jv_RegisterInitiatingLoader (klass, 0);
474 // we need classes to be in the hash while
475 // we're loading, so that they can refer to themselves.
476 _Jv_WaitForState (klass, JV_STATE_LOADED);
483 _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
484 java::lang::ClassLoader *loader)
486 jclass ret = (jclass) JvAllocObject (&ClassClass);
491 ret->superclass = superclass;
492 ret->constants.size = 0;
493 ret->constants.tags = NULL;
494 ret->constants.data = NULL;
496 ret->method_count = 0;
497 ret->vtable_method_count = 0;
499 ret->size_in_bytes = 0;
500 ret->field_count = 0;
501 ret->static_field_count = 0;
503 ret->interfaces = NULL;
504 ret->loader = loader;
505 ret->interface_count = 0;
506 ret->state = JV_STATE_NOTHING;
509 _Jv_RegisterClass (ret);
515 _Jv_FindArrayClass (jclass element, java::lang::ClassLoader *loader)
517 _Jv_Utf8Const *array_name;
519 if (element->isPrimitive())
521 // For primitive types the array is cached in the class.
522 jclass ret = (jclass) element->methods;
528 len = element->name->length + 5;
533 signature[index++] = '[';
534 // Compute name of array class to see if we've already cached it.
535 if (element->isPrimitive())
537 signature[index++] = (char) element->method_count;
541 size_t length = element->name->length;
542 const char *const name = element->name->data;
544 signature[index++] = 'L';
545 memcpy (&signature[index], name, length);
548 signature[index++] = ';';
550 array_name = _Jv_makeUtf8Const (signature, index);
553 jclass array_class = _Jv_FindClassInCache (array_name, element->loader);
557 // Create new array class.
558 array_class = _Jv_NewClass (array_name, &ObjectClass, element->loader);
560 // Note that `vtable_method_count' doesn't include the initial
562 int dm_count = ObjectClass.vtable_method_count + 1;
564 // Create a new vtable by copying Object's vtable (except the
565 // class pointer, of course). Note that we allocate this as
566 // unscanned memory -- the vtables are handled specially by the
568 int size = (sizeof (_Jv_VTable) +
569 ((dm_count - 1) * sizeof (void *)));
570 _Jv_VTable *vtable = (_Jv_VTable *) _Jv_AllocBytes (size);
571 vtable->clas = array_class;
572 memcpy (vtable->method, ObjectClass.vtable->method,
573 dm_count * sizeof (void *));
574 array_class->vtable = vtable;
575 array_class->vtable_method_count = ObjectClass.vtable_method_count;
577 // Stash the pointer to the element type.
578 array_class->methods = (_Jv_Method *) element;
580 // Register our interfaces.
581 // FIXME: for JDK 1.2 we need Serializable.
582 static jclass interfaces[] = { &CloneableClass };
583 array_class->interfaces = interfaces;
584 array_class->interface_count = 1;
586 // as per vmspec 5.3.3.2
587 array_class->accflags = element->accflags;
589 // FIXME: initialize other Class instance variables,
592 // say this class is initialized and ready to go!
593 array_class->state = JV_STATE_DONE;
595 // vmspec, section 5.3.3 describes this
596 if (element->loader != loader)
597 _Jv_RegisterInitiatingLoader (array_class, loader);
600 // For primitive types, point back at this array.
601 if (element->isPrimitive())
602 element->methods = (_Jv_Method *) array_class;