source: trunk/src/gcc/libjava/java/lang/natClassLoader.cc@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 18.9 KB
Line 
1// natClassLoader.cc - Implementation of java.lang.ClassLoader native methods.
2
3/* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11/* Author: Kresten Krab Thorup <[email protected]> */
12
13#include <config.h>
14
15#include <stdlib.h>
16#include <string.h>
17
18#include <gcj/cni.h>
19#include <jvm.h>
20
21#include <java-threads.h>
22#include <java-interp.h>
23
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/VirtualMachineError.h>
37#include <java/lang/VMClassLoader.h>
38#include <java/lang/reflect/Modifier.h>
39#include <java/lang/Runtime.h>
40#include <java/lang/StringBuffer.h>
41#include <java/io/Serializable.h>
42#include <java/lang/Cloneable.h>
43
44// FIXME: remove these.
45#define CloneableClass java::lang::Cloneable::class$
46#define ObjectClass java::lang::Object::class$
47#define ClassClass java::lang::Class::class$
48#define VMClassLoaderClass gnu::gcj::runtime::VMClassLoader::class$
49#define ClassLoaderClass java::lang::ClassLoader::class$
50#define SerializableClass java::io::Serializable::class$
51
52/////////// java.lang.ClassLoader native methods ////////////
53
54java::lang::Class *
55java::lang::ClassLoader::defineClass0 (jstring name,
56 jbyteArray data,
57 jint offset,
58 jint length,
59 java::security::ProtectionDomain *pd)
60{
61#ifdef INTERPRETER
62 jclass klass;
63 klass = (jclass) JvAllocObject (&ClassClass, sizeof (_Jv_InterpClass));
64 _Jv_InitNewClassFields (klass);
65
66 // synchronize on the class, so that it is not
67 // attempted initialized until we're done loading.
68 _Jv_MonitorEnter (klass);
69
70 // record which is the defining loader
71 klass->loader = this;
72
73 // register that we are the initiating loader...
74 if (name != 0)
75 {
76 _Jv_Utf8Const * name2 = _Jv_makeUtf8Const (name);
77
78 if (! _Jv_VerifyClassName (name2))
79 throw new java::lang::ClassFormatError
80 (JvNewStringLatin1 ("erroneous class name"));
81
82 klass->name = name2;
83 }
84
85 try
86 {
87 _Jv_DefineClass (klass, data, offset, length);
88 }
89 catch (java::lang::Throwable *ex)
90 {
91 klass->state = JV_STATE_ERROR;
92 klass->notifyAll ();
93
94 _Jv_UnregisterClass (klass);
95
96 _Jv_MonitorExit (klass);
97
98 // FIXME: Here we may want to test that EX does
99 // indeed represent a valid exception. That is,
100 // anything but ClassNotFoundException,
101 // or some kind of Error.
102
103 // FIXME: Rewrite this as a cleanup instead of
104 // as a catch handler.
105
106 throw ex;
107 }
108
109 klass->protectionDomain = pd;
110
111 // if everything proceeded sucessfully, we're loaded.
112 JvAssert (klass->state == JV_STATE_LOADED);
113
114 // if an exception is generated, this is initially missed.
115 // however, we come back here in handleException0 below...
116 _Jv_MonitorExit (klass);
117
118 return klass;
119
120#else // INTERPRETER
121
122 return 0;
123#endif
124}
125
126void
127_Jv_WaitForState (jclass klass, int state)
128{
129 if (klass->state >= state)
130 return;
131
132 _Jv_MonitorEnter (klass) ;
133
134 if (state == JV_STATE_LINKED)
135 {
136 // Must call _Jv_PrepareCompiledClass while holding the class
137 // mutex.
138 _Jv_PrepareCompiledClass (klass);
139 _Jv_MonitorExit (klass);
140 return;
141 }
142
143 java::lang::Thread *self = java::lang::Thread::currentThread();
144
145 // this is similar to the strategy for class initialization.
146 // if we already hold the lock, just leave.
147 while (klass->state <= state
148 && klass->thread
149 && klass->thread != self)
150 klass->wait ();
151
152 _Jv_MonitorExit (klass);
153
154 if (klass->state == JV_STATE_ERROR)
155 throw new java::lang::LinkageError;
156}
157
158// Finish linking a class. Only called from ClassLoader::resolveClass.
159void
160java::lang::ClassLoader::linkClass0 (java::lang::Class *klass)
161{
162 if (klass->state >= JV_STATE_LINKED)
163 return;
164
165#ifdef INTERPRETER
166 if (_Jv_IsInterpretedClass (klass))
167 _Jv_PrepareClass (klass);
168#endif
169
170 _Jv_PrepareCompiledClass (klass);
171}
172
173void
174java::lang::ClassLoader::markClassErrorState0 (java::lang::Class *klass)
175{
176 klass->state = JV_STATE_ERROR;
177 klass->notifyAll ();
178}
179
180jclass
181java::lang::VMClassLoader::getPrimitiveClass (jchar type)
182{
183 char sig[2];
184 sig[0] = (char) type;
185 sig[1] = '\0';
186 return _Jv_FindClassFromSignature (sig, NULL);
187}
188
189// This is the findClass() implementation for the System classloader. It is
190// the only native method in VMClassLoader, so we define it here.
191jclass
192gnu::gcj::runtime::VMClassLoader::findClass (jstring name)
193{
194 _Jv_Utf8Const *name_u = _Jv_makeUtf8Const (name);
195 jclass klass = _Jv_FindClassInCache (name_u, 0);
196
197 if (! klass)
198 {
199 // Turn `gnu.pkg.quux' into `lib-gnu-pkg-quux'. Then search for
200 // a module named (eg, on Linux) `lib-gnu-pkg-quux.so', followed
201 // by `lib-gnu-pkg.so' and `lib-gnu.so'. If loading one of
202 // these causes the class to appear in the cache, then use it.
203 java::lang::StringBuffer *sb = new java::lang::StringBuffer (JvNewStringLatin1("lib-"));
204 jstring so_base_name = (sb->append (name)->toString ())->replace ('.', '-');
205
206 // Compare against `3' because that is the length of "lib".
207 while (! klass && so_base_name && so_base_name->length() > 3)
208 {
209 using namespace ::java::lang;
210 Runtime *rt = Runtime::getRuntime();
211 jboolean loaded = rt->loadLibraryInternal (so_base_name);
212
213 jint nd = so_base_name->lastIndexOf ('-');
214 if (nd == -1)
215 so_base_name = NULL;
216 else
217 so_base_name = so_base_name->substring (0, nd);
218
219 if (loaded)
220 klass = _Jv_FindClassInCache (name_u, 0);
221 }
222 }
223
224 // Now try loading using the interpreter.
225 if (! klass)
226 {
227 klass = java::net::URLClassLoader::findClass (name);
228 }
229
230 return klass;
231}
232
233jclass
234java::lang::ClassLoader::findLoadedClass (jstring name)
235{
236 return _Jv_FindClassInCache (_Jv_makeUtf8Const (name), this);
237}
238
239/** This function does class-preparation for compiled classes.
240 NOTE: It contains replicated functionality from
241 _Jv_ResolvePoolEntry, and this is intentional, since that function
242 lives in resolve.cc which is entirely conditionally compiled.
243 */
244void
245_Jv_PrepareCompiledClass (jclass klass)
246{
247 if (klass->state >= JV_STATE_LINKED)
248 return;
249
250 // Short-circuit, so that mutually dependent classes are ok.
251 klass->state = JV_STATE_LINKED;
252
253 _Jv_Constants *pool = &klass->constants;
254 for (int index = 1; index < pool->size; ++index)
255 {
256 if (pool->tags[index] == JV_CONSTANT_Class)
257 {
258 _Jv_Utf8Const *name = pool->data[index].utf8;
259
260 jclass found;
261 if (name->data[0] == '[')
262 found = _Jv_FindClassFromSignature (&name->data[0],
263 klass->loader);
264 else
265 found = _Jv_FindClass (name, klass->loader);
266
267 if (! found)
268 {
269 jstring str = _Jv_NewStringUTF (name->data);
270 throw new java::lang::ClassNotFoundException (str);
271 }
272
273 pool->data[index].clazz = found;
274 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
275 }
276 else if (pool->tags[index] == JV_CONSTANT_String)
277 {
278 jstring str;
279 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
280 pool->data[index].o = str;
281 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
282 }
283 }
284
285#ifdef INTERPRETER
286 // FIXME: although the comment up top says that this function is
287 // only called for compiled classes, it is actually called for every
288 // class.
289 if (! _Jv_IsInterpretedClass (klass))
290 {
291#endif /* INTERPRETER */
292 jfieldID f = JvGetFirstStaticField (klass);
293 for (int n = JvNumStaticFields (klass); n > 0; --n)
294 {
295 int mod = f->getModifiers ();
296 // If we have a static String field with a non-null initial
297 // value, we know it points to a Utf8Const.
298 if (f->getClass () == &StringClass
299 && java::lang::reflect::Modifier::isStatic (mod))
300 {
301 jstring *strp = (jstring *) f->u.addr;
302 if (*strp)
303 *strp = _Jv_NewStringUtf8Const ((_Jv_Utf8Const *) *strp);
304 }
305 f = f->getNextField ();
306 }
307#ifdef INTERPRETER
308 }
309#endif /* INTERPRETER */
310
311 if (klass->vtable == NULL)
312 _Jv_MakeVTable(klass);
313
314 if (klass->otable != NULL && klass->otable->state == 0)
315 _Jv_LinkOffsetTable(klass);
316
317 klass->notifyAll ();
318}
319
320
321//
322// A single class can have many "initiating" class loaders,
323// and a single "defining" class loader. The Defining
324// class loader is what is returned from Class.getClassLoader()
325// and is used when loading dependent classes during resolution.
326// The set of initiating class loaders are used to ensure
327// safety of linking, and is maintained in the hash table
328// "initiated_classes". A defining classloader is by definition also
329// initiating, so we only store classes in this table, if they have more
330// than one class loader associated.
331//
332
333
334// Size of local hash table.
335#define HASH_LEN 1013
336
337// Hash function for Utf8Consts.
338#define HASH_UTF(Utf) (((Utf)->hash) % HASH_LEN)
339
340struct _Jv_LoaderInfo {
341 _Jv_LoaderInfo *next;
342 java::lang::Class *klass;
343 java::lang::ClassLoader *loader;
344};
345
346static _Jv_LoaderInfo *initiated_classes[HASH_LEN];
347static jclass loaded_classes[HASH_LEN];
348
349// This is the root of a linked list of classes
350
351
352
353
354jclass
355_Jv_FindClassInCache (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
356{
357 _Jv_MonitorEnter (&ClassClass);
358 jint hash = HASH_UTF (name);
359
360 // first, if LOADER is a defining loader, then it is also initiating
361 jclass klass;
362 for (klass = loaded_classes[hash]; klass; klass = klass->next)
363 {
364 if (loader == klass->loader && _Jv_equalUtf8Consts (name, klass->name))
365 break;
366 }
367
368 // otherwise, it may be that the class in question was defined
369 // by some other loader, but that the loading was initiated by
370 // the loader in question.
371 if (!klass)
372 {
373 _Jv_LoaderInfo *info;
374 for (info = initiated_classes[hash]; info; info = info->next)
375 {
376 if (loader == info->loader
377 && _Jv_equalUtf8Consts (name, info->klass->name))
378 {
379 klass = info->klass;
380 break;
381 }
382 }
383 }
384
385 _Jv_MonitorExit (&ClassClass);
386
387 return klass;
388}
389
390void
391_Jv_UnregisterClass (jclass the_class)
392{
393 _Jv_MonitorEnter (&ClassClass);
394 jint hash = HASH_UTF(the_class->name);
395
396 jclass *klass = &(loaded_classes[hash]);
397 for ( ; *klass; klass = &((*klass)->next))
398 {
399 if (*klass == the_class)
400 {
401 *klass = (*klass)->next;
402 break;
403 }
404 }
405
406 _Jv_LoaderInfo **info = &(initiated_classes[hash]);
407 for ( ; ; info = &((*info)->next))
408 {
409 while (*info && (*info)->klass == the_class)
410 {
411 *info = (*info)->next;
412 }
413
414 if (*info == NULL)
415 break;
416 }
417
418 _Jv_MonitorExit (&ClassClass);
419}
420
421void
422_Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
423{
424 // non-gc alloc!
425 _Jv_LoaderInfo *info = (_Jv_LoaderInfo *) _Jv_Malloc (sizeof(_Jv_LoaderInfo));
426 jint hash = HASH_UTF(klass->name);
427
428 _Jv_MonitorEnter (&ClassClass);
429 info->loader = loader;
430 info->klass = klass;
431 info->next = initiated_classes[hash];
432 initiated_classes[hash] = info;
433 _Jv_MonitorExit (&ClassClass);
434}
435
436// This function is called many times during startup, before main() is
437// run. At that point in time we know for certain we are running
438// single-threaded, so we don't need to lock when adding classes to the
439// class chain. At all other times, the caller should synchronize on
440// Class::class$.
441void
442_Jv_RegisterClasses (jclass *classes)
443{
444 for (; *classes; ++classes)
445 {
446 jclass klass = *classes;
447
448 (*_Jv_RegisterClassHook) (klass);
449
450 // registering a compiled class causes
451 // it to be immediately "prepared".
452 if (klass->state == JV_STATE_NOTHING)
453 klass->state = JV_STATE_COMPILED;
454 }
455}
456
457void
458_Jv_RegisterClassHookDefault (jclass klass)
459{
460 jint hash = HASH_UTF (klass->name);
461
462 jclass check_class = loaded_classes[hash];
463
464 // If the class is already registered, don't re-register it.
465 while (check_class != NULL)
466 {
467 if (check_class == klass)
468 {
469 // If you get this, it means you have the same class in two
470 // different libraries.
471#define TEXT "Duplicate class registration: "
472 // We size-limit MESSAGE so that you can't trash the stack.
473 char message[200];
474 strcpy (message, TEXT);
475 strncpy (message + sizeof (TEXT) - 1, klass->name->data,
476 sizeof (message) - sizeof (TEXT));
477 message[sizeof (message) - 1] = '\0';
478 if (! gcj::runtimeInitialized)
479 JvFail (message);
480 else
481 {
482 java::lang::String *str = JvNewStringLatin1 (message);
483 throw new java::lang::VirtualMachineError (str);
484 }
485 }
486
487 check_class = check_class->next;
488 }
489
490 klass->next = loaded_classes[hash];
491 loaded_classes[hash] = klass;
492}
493
494// A pointer to a function that actually registers a class.
495// Normally _Jv_RegisterClassHookDefault, but could be some other function
496// that registers the class in e.g. a ClassLoader-local table.
497// Should synchronize on Class:class$ while setting/restore this variable.
498
499void (*_Jv_RegisterClassHook) (jclass cl) = _Jv_RegisterClassHookDefault;
500
501void
502_Jv_RegisterClass (jclass klass)
503{
504 jclass classes[2];
505 classes[0] = klass;
506 classes[1] = NULL;
507 _Jv_RegisterClasses (classes);
508}
509
510jclass
511_Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
512{
513 jclass klass = _Jv_FindClassInCache (name, loader);
514
515 if (! klass)
516 {
517 jstring sname = _Jv_NewStringUTF (name->data);
518
519 if (loader)
520 {
521 // Load using a user-defined loader, jvmspec 5.3.2
522 klass = loader->loadClass(sname, false);
523
524 // If "loader" delegated the loadClass operation to another
525 // loader, explicitly register that it is also an initiating
526 // loader of the given class.
527 if (klass && (klass->getClassLoader () != loader))
528 _Jv_RegisterInitiatingLoader (klass, loader);
529 }
530 else
531 {
532 java::lang::ClassLoader *sys
533 = java::lang::ClassLoader::getSystemClassLoader ();
534
535 // Load using the bootstrap loader jvmspec 5.3.1.
536 klass = sys->loadClass (sname, false);
537
538 // Register that we're an initiating loader.
539 if (klass)
540 _Jv_RegisterInitiatingLoader (klass, 0);
541 }
542 }
543 else
544 {
545 // we need classes to be in the hash while
546 // we're loading, so that they can refer to themselves.
547 _Jv_WaitForState (klass, JV_STATE_LOADED);
548 }
549
550 return klass;
551}
552
553void
554_Jv_InitNewClassFields (jclass ret)
555{
556 ret->next = NULL;
557 ret->name = NULL;
558 ret->accflags = 0;
559 ret->superclass = NULL;
560 ret->constants.size = 0;
561 ret->constants.tags = NULL;
562 ret->constants.data = NULL;
563 ret->methods = NULL;
564 ret->method_count = 0;
565 ret->vtable_method_count = 0;
566 ret->fields = NULL;
567 ret->size_in_bytes = 0;
568 ret->field_count = 0;
569 ret->static_field_count = 0;
570 ret->vtable = NULL;
571 ret->interfaces = NULL;
572 ret->loader = NULL;
573 ret->interface_count = 0;
574 ret->state = JV_STATE_NOTHING;
575 ret->thread = NULL;
576 ret->depth = 0;
577 ret->ancestors = NULL;
578 ret->idt = NULL;
579 ret->arrayclass = NULL;
580}
581
582jclass
583_Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
584 java::lang::ClassLoader *loader)
585{
586 jclass ret = (jclass) JvAllocObject (&ClassClass);
587 _Jv_InitNewClassFields (ret);
588 ret->name = name;
589 ret->superclass = superclass;
590 ret->loader = loader;
591
592 _Jv_RegisterClass (ret);
593
594 return ret;
595}
596
597static _Jv_IDispatchTable *array_idt = NULL;
598static jshort array_depth = 0;
599static jclass *array_ancestors = NULL;
600
601// Create a class representing an array of ELEMENT and store a pointer to it
602// in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
603// instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
604// array class. This parameter is optional.
605void
606_Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader,
607 _Jv_VTable *array_vtable)
608{
609 JvSynchronize sync (element);
610
611 _Jv_Utf8Const *array_name;
612 int len;
613
614 if (element->arrayclass)
615 return;
616
617 if (element->isPrimitive())
618 {
619 if (element == JvPrimClass (void))
620 throw new java::lang::ClassNotFoundException ();
621 len = 3;
622 }
623 else
624 len = element->name->length + 5;
625
626 {
627 char signature[len];
628 int index = 0;
629 signature[index++] = '[';
630 // Compute name of array class.
631 if (element->isPrimitive())
632 {
633 signature[index++] = (char) element->method_count;
634 }
635 else
636 {
637 size_t length = element->name->length;
638 const char *const name = element->name->data;
639 if (name[0] != '[')
640 signature[index++] = 'L';
641 memcpy (&signature[index], name, length);
642 index += length;
643 if (name[0] != '[')
644 signature[index++] = ';';
645 }
646 array_name = _Jv_makeUtf8Const (signature, index);
647 }
648
649 // Create new array class.
650 jclass array_class = _Jv_NewClass (array_name, &ObjectClass,
651 element->loader);
652
653 // Note that `vtable_method_count' doesn't include the initial
654 // gc_descr slot.
655 JvAssert (ObjectClass.vtable_method_count == NUM_OBJECT_METHODS);
656 int dm_count = ObjectClass.vtable_method_count;
657
658 // Create a new vtable by copying Object's vtable.
659 _Jv_VTable *vtable;
660 if (array_vtable)
661 vtable = array_vtable;
662 else
663 vtable = _Jv_VTable::new_vtable (dm_count);
664 vtable->clas = array_class;
665 vtable->gc_descr = ObjectClass.vtable->gc_descr;
666 for (int i = 0; i < dm_count; ++i)
667 vtable->set_method (i, ObjectClass.vtable->get_method (i));
668
669 array_class->vtable = vtable;
670 array_class->vtable_method_count = ObjectClass.vtable_method_count;
671
672 // Stash the pointer to the element type.
673 array_class->methods = (_Jv_Method *) element;
674
675 // Register our interfaces.
676 static jclass interfaces[] = { &CloneableClass, &SerializableClass };
677 array_class->interfaces = interfaces;
678 array_class->interface_count = sizeof interfaces / sizeof interfaces[0];
679
680 // Since all array classes have the same interface dispatch table, we can
681 // cache one and reuse it. It is not necessary to synchronize this.
682 if (!array_idt)
683 {
684 _Jv_PrepareConstantTimeTables (array_class);
685 array_idt = array_class->idt;
686 array_depth = array_class->depth;
687 array_ancestors = array_class->ancestors;
688 }
689 else
690 {
691 array_class->idt = array_idt;
692 array_class->depth = array_depth;
693 array_class->ancestors = array_ancestors;
694 }
695
696 using namespace java::lang::reflect;
697 {
698 // Array classes are "abstract final"...
699 _Jv_ushort accflags = Modifier::FINAL | Modifier::ABSTRACT;
700 // ... and inherit accessibility from element type, per vmspec 5.3.3.2
701 accflags |= (element->accflags & Modifier::PUBLIC);
702 accflags |= (element->accflags & Modifier::PROTECTED);
703 accflags |= (element->accflags & Modifier::PRIVATE);
704 array_class->accflags = accflags;
705 }
706
707 // An array class has no visible instance fields. "length" is invisible to
708 // reflection.
709
710 // say this class is initialized and ready to go!
711 array_class->state = JV_STATE_DONE;
712
713 // vmspec, section 5.3.3 describes this
714 if (element->loader != loader)
715 _Jv_RegisterInitiatingLoader (array_class, loader);
716
717 element->arrayclass = array_class;
718}
Note: See TracBrowser for help on using the repository browser.