* This is a rather huge commit, which changes the build order of
[cacao.git] / src / native / vm / gnu / java_lang_VMClassLoader.c
1 /* src/native/vm/gnu/VMClassLoader.c
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: java_lang_VMClassLoader.c 7246 2007-01-29 18:49:05Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <sys/stat.h>
33
34 #include "vm/types.h"
35
36 #include "mm/memory.h"
37
38 #include "native/jni.h"
39 #include "native/native.h"
40 #include "native/include/java_lang_Class.h"
41 #include "native/include/java_lang_String.h"
42 #include "native/include/java_security_ProtectionDomain.h"  /* required by... */
43 #include "native/include/java_lang_ClassLoader.h"
44 #include "native/include/java_util_Vector.h"
45
46 #include "toolbox/logging.h"
47
48 #include "vm/builtin.h"
49 #include "vm/exceptions.h"
50 #include "vm/initialize.h"
51 #include "vm/stringlocal.h"
52 #include "vm/vm.h"
53
54 #include "vm/jit/asmpart.h"
55
56 #include "vmcore/class.h"
57 #include "vmcore/classcache.h"
58 #include "vmcore/linker.h"
59 #include "vmcore/loader.h"
60 #include "vmcore/options.h"
61 #include "vmcore/statistics.h"
62 #include "vmcore/suck.h"
63 #include "vmcore/zip.h"
64
65 #if defined(ENABLE_JVMTI)
66 #include "native/jvmti/cacaodbg.h"
67 #endif
68
69
70 /*
71  * Class:     java/lang/VMClassLoader
72  * Method:    defineClass
73  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
74  */
75 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_defineClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *cl, java_lang_String *name, java_bytearray *data, s4 offset, s4 len, java_security_ProtectionDomain *pd)
76 {
77         classinfo       *c;
78         classinfo       *r;
79         classbuffer     *cb;
80         utf             *utfname;
81         java_lang_Class *co;
82 #if defined(ENABLE_JVMTI)
83         jint new_class_data_len = 0;
84         unsigned char* new_class_data = NULL;
85 #endif
86
87         /* check if data was passed */
88
89         if (data == NULL) {
90                 exceptions_throw_nullpointerexception();
91                 return NULL;
92         }
93
94         /* check the indexes passed */
95
96         if ((offset < 0) || (len < 0) || ((offset + len) > data->header.size)) {
97                 exceptions_throw_arrayindexoutofboundsexception();
98                 return NULL;
99         }
100
101         if (name != NULL) {
102                 /* convert '.' to '/' in java string */
103
104                 utfname = javastring_toutf(name, true);
105                 
106                 /* check if this class has already been defined */
107
108                 c = classcache_lookup_defined_or_initiated((java_objectheader *) cl, utfname);
109                 if (c != NULL) {
110                         exceptions_throw_linkageerror("duplicate class definition: ", c);
111                         return NULL;
112                 }
113         } 
114         else {
115                 utfname = NULL;
116         }
117
118
119 #if defined(ENABLE_JVMTI)
120         /* fire Class File Load Hook JVMTI event */
121         if (jvmti) jvmti_ClassFileLoadHook(utfname, len, (unsigned char*)data->data, 
122                                                         (java_objectheader *)cl, (java_objectheader *)pd, 
123                                                         &new_class_data_len, &new_class_data);
124 #endif
125
126
127         /* create a new classinfo struct */
128
129         c = class_create_classinfo(utfname);
130
131 #if defined(ENABLE_STATISTICS)
132         /* measure time */
133
134         if (opt_getloadingtime)
135                 loadingtime_start();
136 #endif
137
138         /* build a classbuffer with the given data */
139
140         cb = NEW(classbuffer);
141         cb->class = c;
142 #if defined(ENABLE_JVMTI)
143         /* check if the JVMTI wants to modify the class */
144         if (new_class_data == NULL) {
145 #endif
146         cb->size  = len;
147         cb->data  = (u1 *) &data->data[offset];
148 #if defined(ENABLE_JVMTI)
149         } else {
150                 cb->size  = new_class_data_len;
151                 cb->data  = (u1 *) new_class_data;
152         }
153 #endif
154         cb->pos   = cb->data;
155
156         /* preset the defining classloader */
157
158         c->classloader = (java_objectheader *) cl;
159
160         /* load the class from this buffer */
161
162         r = load_class_from_classbuffer(cb);
163
164         /* free memory */
165
166         FREE(cb, classbuffer);
167
168 #if defined(ENABLE_STATISTICS)
169         /* measure time */
170
171         if (opt_getloadingtime)
172                 loadingtime_stop();
173 #endif
174
175         if (r == NULL) {
176                 /* If return value is NULL, we had a problem and the class is
177                    not loaded.  Now free the allocated memory, otherwise we
178                    could run into a DOS. */
179
180                 class_free(c);
181
182                 return NULL;
183         }
184
185         /* set ProtectionDomain */
186
187         co = (java_lang_Class *) c;
188
189         co->pd = pd;
190
191         /* Store the newly defined class in the class cache. This call also       */
192         /* checks whether a class of the same name has already been defined by    */
193         /* the same defining loader, and if so, replaces the newly created class  */
194         /* by the one defined earlier.                                            */
195         /* Important: The classinfo given to classcache_store must be             */
196         /*            fully prepared because another thread may return this       */
197         /*            pointer after the lookup at to top of this function         */
198         /*            directly after the class cache lock has been released.      */
199
200         c = classcache_store((java_objectheader *) cl, c, true);
201
202         return (java_lang_Class *) c;
203 }
204
205
206 /*
207  * Class:     java/lang/VMClassLoader
208  * Method:    getPrimitiveClass
209  * Signature: (C)Ljava/lang/Class;
210  */
211 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
212 {
213         classinfo *c;
214         s4         index;
215
216         /* get primitive class */
217
218         switch (type) {
219         case 'I':
220                 index = PRIMITIVETYPE_INT;
221                 break;
222         case 'J':
223                 index = PRIMITIVETYPE_LONG;
224                 break;
225         case 'F':
226                 index = PRIMITIVETYPE_FLOAT;
227                 break;
228         case 'D':
229                 index = PRIMITIVETYPE_DOUBLE;
230                 break;
231         case 'B':
232                 index = PRIMITIVETYPE_BYTE;
233                 break;
234         case 'C':
235                 index = PRIMITIVETYPE_CHAR;
236                 break;
237         case 'S':
238                 index = PRIMITIVETYPE_SHORT;
239                 break;
240         case 'Z':
241                 index = PRIMITIVETYPE_BOOLEAN;
242                 break;
243         case 'V':
244                 index = PRIMITIVETYPE_VOID;
245                 break;
246         default:
247                 exceptions_throw_noclassdeffounderror(utf_null);
248                 c = NULL;
249         }
250
251         c = primitivetype_table[index].class_primitive;
252
253         return (java_lang_Class *) c;
254 }
255
256
257 /*
258  * Class:     java/lang/VMClassLoader
259  * Method:    resolveClass
260  * Signature: (Ljava/lang/Class;)V
261  */
262 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
263 {
264         classinfo *ci;
265
266         ci = (classinfo *) c;
267
268         if (!ci) {
269                 exceptions_throw_nullpointerexception();
270                 return;
271         }
272
273         /* link the class */
274
275         if (!(ci->state & CLASS_LINKED))
276                 (void) link_class(ci);
277
278         return;
279 }
280
281
282 /*
283  * Class:     java/lang/VMClassLoader
284  * Method:    loadClass
285  * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
286  */
287 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, jboolean resolve)
288 {
289         classinfo         *c;
290         utf               *u;
291         java_objectheader *xptr;
292
293         if (name == NULL) {
294                 exceptions_throw_nullpointerexception();
295                 return NULL;
296         }
297
298         /* create utf string in which '.' is replaced by '/' */
299
300         u = javastring_toutf(name, true);
301
302         /* load class */
303
304         c = load_class_bootstrap(u);
305
306         if (c == NULL)
307                 goto exception;
308
309         /* resolve class -- if requested */
310
311 /*      if (resolve) */
312                 if (!link_class(c))
313                         goto exception;
314
315         return (java_lang_Class *) c;
316
317  exception:
318         xptr = exceptions_get_exception();
319
320         c = xptr->vftbl->class;
321         
322         /* if the exception is a NoClassDefFoundError, we replace it with a
323            ClassNotFoundException, otherwise return the exception */
324
325         if (c == class_java_lang_NoClassDefFoundError) {
326                 /* clear exceptionptr, because builtin_new checks for 
327                    ExceptionInInitializerError */
328                 exceptions_clear_exception();
329
330                 exceptions_throw_classnotfoundexception(u);
331         }
332
333         return NULL;
334 }
335
336
337 /*
338  * Class:     java/lang/VMClassLoader
339  * Method:    nativeGetResources
340  * Signature: (Ljava/lang/String;)Ljava/util/Vector;
341  */
342 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
343 {
344         jobject               o;         /* vector being created     */
345         methodinfo           *m;         /* "add" method of vector   */
346         java_lang_String     *path;      /* path to be added         */
347         list_classpath_entry *lce;       /* classpath entry          */
348         utf                  *utfname;   /* utf to look for          */
349         char                 *buffer;    /* char buffer              */
350         char                 *namestart; /* start of name to use     */
351         char                 *tmppath;   /* temporary buffer         */
352         s4                    namelen;   /* length of name to use    */
353         s4                    searchlen; /* length of name to search */
354         s4                    bufsize;   /* size of buffer allocated */
355         s4                    pathlen;   /* name of path to assemble */
356         struct stat           buf;       /* buffer for stat          */
357         jboolean              ret;       /* return value of "add"    */
358
359         /* get the resource name as utf string */
360
361         utfname = javastring_toutf(name, false);
362         if (!utfname)
363                 return NULL;
364
365         /* copy it to a char buffer */
366
367         namelen = utf_bytes(utfname);
368         searchlen = namelen;
369         bufsize = namelen + strlen("0");
370         buffer = MNEW(char, bufsize);
371
372         utf_copy(buffer, utfname);
373         namestart = buffer;
374
375         /* skip leading '/' */
376
377         if (namestart[0] == '/') {
378                 namestart++;
379                 namelen--;
380                 searchlen--;
381         }
382
383         /* remove trailing `.class' */
384
385         if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
386                 searchlen -= 6;
387         }
388
389         /* create a new needle to look for, if necessary */
390
391         if (searchlen != bufsize-1) {
392                 utfname = utf_new(namestart, searchlen);
393                 if (utfname == NULL)
394                         goto return_NULL;
395         }
396                         
397         /* new Vector() */
398
399         o = native_new_and_init(class_java_util_Vector);
400
401         if (!o)
402                 goto return_NULL;
403
404         /* get Vector.add() method */
405
406         m = class_resolveclassmethod(class_java_util_Vector,
407                                                                  utf_add,
408                                                                  utf_new_char("(Ljava/lang/Object;)Z"),
409                                                                  NULL,
410                                                                  true);
411
412         if (!m)
413                 goto return_NULL;
414
415         /* iterate over all classpath entries */
416
417         for (lce = list_first(list_classpath_entries); lce != NULL;
418                  lce = list_next(list_classpath_entries, lce)) {
419                 /* clear path pointer */
420                 path = NULL;
421
422 #if defined(ENABLE_ZLIB)
423                 if (lce->type == CLASSPATH_ARCHIVE) {
424
425                         if (zip_find(lce, utfname)) {
426                                 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
427                                         namelen + strlen("0");
428
429                                 tmppath = MNEW(char, pathlen);
430
431                                 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
432                                 path = javastring_new_from_utf_string(tmppath),
433
434                                 MFREE(tmppath, char, pathlen);
435                         }
436
437                 } else {
438 #endif /* defined(ENABLE_ZLIB) */
439                         pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
440
441                         tmppath = MNEW(char, pathlen);
442
443                         sprintf(tmppath, "file://%s%s", lce->path, namestart);
444
445                         /* Does this file exist? */
446
447                         if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
448                                 if (!S_ISDIR(buf.st_mode))
449                                         path = javastring_new_from_utf_string(tmppath);
450
451                         MFREE(tmppath, char, pathlen);
452 #if defined(ENABLE_ZLIB)
453                 }
454 #endif
455
456                 /* if a resource was found, add it to the vector */
457
458                 if (path) {
459                         ret = vm_call_method_int(m, o, path);
460
461                         if (exceptions_get_exception() != NULL)
462                                 goto return_NULL;
463
464                         if (ret == 0) 
465                                 goto return_NULL;
466                 }
467         }
468
469         MFREE(buffer, char, bufsize);
470
471         return (java_util_Vector *) o;
472
473 return_NULL:
474         MFREE(buffer, char, bufsize);
475
476         return NULL;
477 }
478
479
480 /*
481  * Class:     java/lang/VMClassLoader
482  * Method:    defaultAssertionStatus
483  * Signature: ()Z
484  */
485 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
486 {
487         return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
488 }
489
490
491 /*
492  * Class:     java/lang/VMClassLoader
493  * Method:    findLoadedClass
494  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
495  */
496 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *cl, java_lang_String *name)
497 {
498         classinfo *c;
499         utf       *u;
500
501         /* replace `.' by `/', this is required by the classcache */
502
503         u = javastring_toutf(name, true);
504
505         /* lookup for defining classloader */
506
507         c = classcache_lookup_defined((classloader *) cl, u);
508
509         /* if not found, lookup for initiating classloader */
510
511         if (c == NULL)
512                 c = classcache_lookup((classloader *) cl, u);
513
514         return (java_lang_Class *) c;
515 }
516
517
518 /*
519  * These are local overrides for various environment variables in Emacs.
520  * Please do not remove this and leave it at the end of the file, where
521  * Emacs will automagically detect them.
522  * ---------------------------------------------------------------------
523  * Local variables:
524  * mode: c
525  * indent-tabs-mode: t
526  * c-basic-offset: 4
527  * tab-width: 4
528  * End:
529  * vim:noexpandtab:sw=4:ts=4:
530  */