* m4/classpath.m4: Renamed --with-classpath* options to
[cacao.git] / src / native / vm / gnu / java_lang_VMClassLoader.c
1 /* src/native/vm/gnu/java_lang_VMClassLoader.c
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30 #include <sys/stat.h>
31
32 #include "mm/memory.h"
33
34 #include "native/jni.h"
35 #include "native/llni.h"
36 #include "native/native.h"
37 #include "native/include/java_lang_Class.h"
38 #include "native/include/java_lang_String.h"
39 #include "native/include/java_security_ProtectionDomain.h"  /* required by... */
40 #include "native/include/java_lang_ClassLoader.h"
41 #include "native/include/java_util_Vector.h"
42 #include "native/include/java_util_HashMap.h"
43 #include "native/include/java_util_Map.h"
44 #include "native/include/java_lang_Boolean.h"
45
46 #include "native/include/java_lang_VMClassLoader.h"
47
48 #include "toolbox/logging.h"
49 #include "toolbox/list.h"
50
51 #if defined(ENABLE_ASSERTION)
52 #include "vm/assertion.h"
53 #endif
54
55 #include "vm/builtin.h"
56 #include "vm/exceptions.h"
57 #include "vm/initialize.h"
58 #include "vm/primitive.h"
59 #include "vm/stringlocal.h"
60 #include "vm/vm.h"
61
62 #include "vm/jit/asmpart.h"
63
64 #include "vmcore/class.h"
65 #include "vmcore/classcache.h"
66 #include "vmcore/linker.h"
67 #include "vmcore/loader.h"
68 #include "vmcore/options.h"
69 #include "vmcore/statistics.h"
70 #include "vmcore/suck.h"
71 #include "vmcore/zip.h"
72
73 #if defined(ENABLE_JVMTI)
74 #include "native/jvmti/cacaodbg.h"
75 #endif
76
77 /* native methods implemented by this file ************************************/
78
79 static JNINativeMethod methods[] = {
80         { "defineClass",                "(Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;", (void *) (uintptr_t) &Java_java_lang_VMClassLoader_defineClass                },
81         { "getPrimitiveClass",          "(C)Ljava/lang/Class;",                                                                             (void *) (uintptr_t) &Java_java_lang_VMClassLoader_getPrimitiveClass          },
82         { "resolveClass",               "(Ljava/lang/Class;)V",                                                                             (void *) (uintptr_t) &Java_java_lang_VMClassLoader_resolveClass               },
83         { "loadClass",                  "(Ljava/lang/String;Z)Ljava/lang/Class;",                                                           (void *) (uintptr_t) &Java_java_lang_VMClassLoader_loadClass                  },
84         { "nativeGetResources",         "(Ljava/lang/String;)Ljava/util/Vector;",                                                           (void *) (uintptr_t) &Java_java_lang_VMClassLoader_nativeGetResources         },
85         { "defaultAssertionStatus",     "()Z",                                                                                              (void *) (uintptr_t) &Java_java_lang_VMClassLoader_defaultAssertionStatus     },
86         { "defaultUserAssertionStatus", "()Z",                                                                                              (void *) (uintptr_t) &Java_java_lang_VMClassLoader_defaultUserAssertionStatus },
87         { "packageAssertionStatus0",    "(Ljava/lang/Boolean;Ljava/lang/Boolean;)Ljava/util/Map;",                                          (void *) (uintptr_t) &Java_java_lang_VMClassLoader_packageAssertionStatus0    },
88         { "classAssertionStatus0",      "(Ljava/lang/Boolean;Ljava/lang/Boolean;)Ljava/util/Map;",                                          (void *) (uintptr_t) &Java_java_lang_VMClassLoader_classAssertionStatus0      },
89         { "findLoadedClass",            "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;",                                     (void *) (uintptr_t) &Java_java_lang_VMClassLoader_findLoadedClass            },
90 };
91
92
93 /* _Jv_java_lang_VMClassLoader_init ********************************************
94
95    Register native functions.
96
97 *******************************************************************************/
98
99 void _Jv_java_lang_VMClassLoader_init(void)
100 {
101         utf *u;
102
103         u = utf_new_char("java/lang/VMClassLoader");
104
105         native_method_register(u, methods, NATIVE_METHODS_COUNT);
106 }
107
108
109 /*
110  * Class:     java/lang/VMClassLoader
111  * Method:    defineClass
112  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
113  */
114 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_defineClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *cl, java_lang_String *name, java_handle_bytearray_t *data, int32_t offset, int32_t len, java_security_ProtectionDomain *pd)
115 {
116         utf             *utfname;
117         classinfo       *c;
118         classloader_t   *loader;
119         java_lang_Class *o;
120
121 #if defined(ENABLE_JVMTI)
122         jint new_class_data_len = 0;
123         unsigned char* new_class_data = NULL;
124 #endif
125
126         /* check if data was passed */
127
128         if (data == NULL) {
129                 exceptions_throw_nullpointerexception();
130                 return NULL;
131         }
132
133         /* check the indexes passed */
134
135         if ((offset < 0) || (len < 0) || ((offset + len) > LLNI_array_size(data))) {
136                 exceptions_throw_arrayindexoutofboundsexception();
137                 return NULL;
138         }
139
140         /* add classloader to classloader hashtable */
141
142         loader = loader_hashtable_classloader_add((java_handle_t *) cl);
143
144         if (name != NULL) {
145                 /* convert '.' to '/' in java string */
146
147                 utfname = javastring_toutf((java_handle_t *) name, true);
148         } 
149         else {
150                 utfname = NULL;
151         }
152
153 #if defined(ENABLE_JVMTI)
154         /* XXX again this will not work because of the indirection cell for classloaders */
155         assert(0);
156         /* fire Class File Load Hook JVMTI event */
157
158         if (jvmti)
159                 jvmti_ClassFileLoadHook(utfname, len, (unsigned char *) data->data, 
160                                                                 loader, (java_handle_t *) pd, 
161                                                                 &new_class_data_len, &new_class_data);
162 #endif
163
164         /* define the class */
165
166 #if defined(ENABLE_JVMTI)
167         /* check if the JVMTI wants to modify the class */
168
169         if (new_class_data == NULL)
170                 c = class_define(utfname, loader, new_class_data_len, new_class_data, pd); 
171         else
172 #endif
173                 c = class_define(utfname, loader, len, (uint8_t *) &LLNI_array_direct(data, offset), (java_handle_t *) pd);
174
175         if (c == NULL)
176                 return NULL;
177
178         /* for convenience */
179
180         o = LLNI_classinfo_wrap(c);
181
182         /* set ProtectionDomain */
183
184         LLNI_field_set_ref(o, pd, pd);
185
186         return o;
187 }
188
189
190 /*
191  * Class:     java/lang/VMClassLoader
192  * Method:    getPrimitiveClass
193  * Signature: (C)Ljava/lang/Class;
194  */
195 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, int32_t type)
196 {
197         classinfo *c;
198
199         c = primitive_class_get_by_char(type);
200
201         if (c == NULL) {
202                 exceptions_throw_classnotfoundexception(utf_null);
203                 return NULL;
204         }
205
206         return LLNI_classinfo_wrap(c);
207 }
208
209
210 /*
211  * Class:     java/lang/VMClassLoader
212  * Method:    resolveClass
213  * Signature: (Ljava/lang/Class;)V
214  */
215 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
216 {
217         classinfo *ci;
218
219         ci = LLNI_classinfo_unwrap(c);
220
221         if (!ci) {
222                 exceptions_throw_nullpointerexception();
223                 return;
224         }
225
226         /* link the class */
227
228         if (!(ci->state & CLASS_LINKED))
229                 (void) link_class(ci);
230
231         return;
232 }
233
234
235 /*
236  * Class:     java/lang/VMClassLoader
237  * Method:    loadClass
238  * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
239  */
240 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, int32_t resolve)
241 {
242         classinfo *c;
243         utf       *u;
244
245         if (name == NULL) {
246                 exceptions_throw_nullpointerexception();
247                 return NULL;
248         }
249
250         /* create utf string in which '.' is replaced by '/' */
251
252         u = javastring_toutf((java_handle_t *) name, true);
253
254         /* load class */
255
256         c = load_class_bootstrap(u);
257
258         if (c == NULL)
259                 return NULL;
260
261         /* resolve class -- if requested */
262
263 /*      if (resolve) */
264                 if (!link_class(c))
265                         return NULL;
266
267         return LLNI_classinfo_wrap(c);
268 }
269
270
271 /*
272  * Class:     java/lang/VMClassLoader
273  * Method:    nativeGetResources
274  * Signature: (Ljava/lang/String;)Ljava/util/Vector;
275  */
276 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
277 {
278         java_handle_t        *o;         /* vector being created     */
279         methodinfo           *m;         /* "add" method of vector   */
280         java_handle_t        *path;      /* path to be added         */
281         list_classpath_entry *lce;       /* classpath entry          */
282         utf                  *utfname;   /* utf to look for          */
283         char                 *buffer;    /* char buffer              */
284         char                 *namestart; /* start of name to use     */
285         char                 *tmppath;   /* temporary buffer         */
286         int32_t               namelen;   /* length of name to use    */
287         int32_t               searchlen; /* length of name to search */
288         int32_t               bufsize;   /* size of buffer allocated */
289         int32_t               pathlen;   /* name of path to assemble */
290         struct stat           buf;       /* buffer for stat          */
291         jboolean              ret;       /* return value of "add"    */
292
293         /* get the resource name as utf string */
294
295         utfname = javastring_toutf((java_handle_t *) name, false);
296
297         if (utfname == NULL)
298                 return NULL;
299
300         /* copy it to a char buffer */
301
302         namelen   = utf_bytes(utfname);
303         searchlen = namelen;
304         bufsize   = namelen + strlen("0");
305         buffer    = MNEW(char, bufsize);
306
307         utf_copy(buffer, utfname);
308         namestart = buffer;
309
310         /* skip leading '/' */
311
312         if (namestart[0] == '/') {
313                 namestart++;
314                 namelen--;
315                 searchlen--;
316         }
317
318         /* remove trailing `.class' */
319
320         if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
321                 searchlen -= 6;
322         }
323
324         /* create a new needle to look for, if necessary */
325
326         if (searchlen != bufsize-1) {
327                 utfname = utf_new(namestart, searchlen);
328                 if (utfname == NULL)
329                         goto return_NULL;
330         }
331                         
332         /* new Vector() */
333
334         o = native_new_and_init(class_java_util_Vector);
335
336         if (o == NULL)
337                 goto return_NULL;
338
339         /* get Vector.add() method */
340
341         m = class_resolveclassmethod(class_java_util_Vector,
342                                                                  utf_add,
343                                                                  utf_new_char("(Ljava/lang/Object;)Z"),
344                                                                  NULL,
345                                                                  true);
346
347         if (m == NULL)
348                 goto return_NULL;
349
350         /* iterate over all classpath entries */
351
352         for (lce = list_first(list_classpath_entries); lce != NULL;
353                  lce = list_next(list_classpath_entries, lce)) {
354                 /* clear path pointer */
355                 path = NULL;
356
357 #if defined(ENABLE_ZLIB)
358                 if (lce->type == CLASSPATH_ARCHIVE) {
359
360                         if (zip_find(lce, utfname)) {
361                                 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
362                                         namelen + strlen("0");
363
364                                 tmppath = MNEW(char, pathlen);
365
366                                 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
367                                 path = javastring_new_from_utf_string(tmppath),
368
369                                 MFREE(tmppath, char, pathlen);
370                         }
371
372                 } else {
373 #endif /* defined(ENABLE_ZLIB) */
374                         pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
375
376                         tmppath = MNEW(char, pathlen);
377
378                         sprintf(tmppath, "file://%s%s", lce->path, namestart);
379
380                         /* Does this file exist? */
381
382                         if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
383                                 if (!S_ISDIR(buf.st_mode))
384                                         path = javastring_new_from_utf_string(tmppath);
385
386                         MFREE(tmppath, char, pathlen);
387 #if defined(ENABLE_ZLIB)
388                 }
389 #endif
390
391                 /* if a resource was found, add it to the vector */
392
393                 if (path != NULL) {
394                         ret = vm_call_method_int(m, o, path);
395
396                         if (exceptions_get_exception() != NULL)
397                                 goto return_NULL;
398
399                         if (ret == 0) 
400                                 goto return_NULL;
401                 }
402         }
403
404         MFREE(buffer, char, bufsize);
405
406         return (java_util_Vector *) o;
407
408 return_NULL:
409         MFREE(buffer, char, bufsize);
410
411         return NULL;
412 }
413
414
415 /*
416  * Class:     java/lang/VMClassLoader
417  * Method:    defaultAssertionStatus
418  * Signature: ()Z
419  */
420 JNIEXPORT int32_t JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
421 {
422 #if defined(ENABLE_ASSERTION)
423         return assertion_system_enabled;
424 #else
425         return false;
426 #endif
427 }
428
429 /*
430  * Class:     java/lang/VMClassLoader
431  * Method:    userAssertionStatus
432  * Signature: ()Z
433  */
434 JNIEXPORT int32_t JNICALL Java_java_lang_VMClassLoader_defaultUserAssertionStatus(JNIEnv *env, jclass clazz)
435 {
436 #if defined(ENABLE_ASSERTION)
437         return assertion_user_enabled;
438 #else
439         return false;
440 #endif
441 }
442
443 /*
444  * Class:     java/lang/VMClassLoader
445  * Method:    packageAssertionStatus
446  * Signature: ()Ljava_util_Map;
447  */
448 JNIEXPORT java_util_Map* JNICALL Java_java_lang_VMClassLoader_packageAssertionStatus0(JNIEnv *env, jclass clazz, java_lang_Boolean *jtrue, java_lang_Boolean *jfalse)
449 {
450         java_handle_t     *hm;
451 #if defined(ENABLE_ASSERTION)
452         java_handle_t     *js;
453         methodinfo        *m;
454         assertion_name_t  *item;
455 #endif
456
457         /* new HashMap() */
458
459         hm = native_new_and_init(class_java_util_HashMap);
460         if (hm == NULL) {
461                 return NULL;
462         }
463
464 #if defined(ENABLE_ASSERTION)
465         /* if nothing todo, return now */
466
467         if (assertion_package_count == 0) {
468                 return (java_util_Map *) hm;
469         }
470
471         /* get HashMap.put method */
472
473         m = class_resolveclassmethod(class_java_util_HashMap,
474                                  utf_put,
475                                  utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
476                                  NULL,
477                                  true);
478
479         if (m == NULL) {
480                 return NULL;
481         }
482
483         item = (assertion_name_t *)list_first(list_assertion_names);
484
485         while (item != NULL) {
486                 if (item->package == false) {
487                         item = (assertion_name_t *)list_next(list_assertion_names, item);
488                         continue;
489                 }
490                 
491                 if (strcmp(item->name, "") == 0) {
492                         /* unnamed package wanted */
493                         js = NULL;
494                 }
495                 else {
496                         js = javastring_new_from_ascii(item->name);
497                         if (js == NULL) {
498                                 return NULL;
499                         }
500                 }
501
502                 if (item->enabled == true) {
503                         vm_call_method(m, hm, js, jtrue);
504                 }
505                 else {
506                         vm_call_method(m, hm, js, jfalse);
507                 }
508
509                 item = (assertion_name_t *)list_next(list_assertion_names, item);
510         }
511 #endif
512
513         return (java_util_Map *) hm;
514 }
515
516 /*
517  * Class:     java/lang/VMClassLoader
518  * Method:    classAssertionStatus
519  * Signature: ()Ljava_util_Map;
520  */
521 JNIEXPORT java_util_Map* JNICALL Java_java_lang_VMClassLoader_classAssertionStatus0(JNIEnv *env, jclass clazz, java_lang_Boolean *jtrue, java_lang_Boolean *jfalse)
522 {
523         java_handle_t     *hm;
524 #if defined(ENABLE_ASSERTION)
525         java_handle_t     *js;
526         methodinfo        *m;
527         assertion_name_t  *item;
528 #endif
529
530         /* new HashMap() */
531
532         hm = native_new_and_init(class_java_util_HashMap);
533         if (hm == NULL) {
534                 return NULL;
535         }
536
537 #if defined(ENABLE_ASSERTION)
538         /* if nothing todo, return now */
539
540         if (assertion_class_count == 0) {
541                 return (java_util_Map *) hm;
542         }
543
544         /* get HashMap.put method */
545
546         m = class_resolveclassmethod(class_java_util_HashMap,
547                                  utf_put,
548                                  utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
549                                  NULL,
550                                  true);
551
552         if (m == NULL) {
553                 return NULL;
554         }
555
556         item = (assertion_name_t *)list_first(list_assertion_names);
557
558         while (item != NULL) {
559                 if (item->package == true) {
560                         item = (assertion_name_t *)list_next(list_assertion_names, item);
561                         continue;
562                 }
563
564                 js = javastring_new_from_ascii(item->name);
565                 if (js == NULL) {
566                         return NULL;
567                 }
568
569                 if (item->enabled == true) {
570                         vm_call_method(m, hm, js, jtrue);
571                 }
572                 else {
573                         vm_call_method(m, hm, js, jfalse);
574                 }
575
576                 item = (assertion_name_t *)list_next(list_assertion_names, item);
577         }
578 #endif
579
580         return (java_util_Map *) hm;
581 }
582
583
584 /*
585  * Class:     java/lang/VMClassLoader
586  * Method:    findLoadedClass
587  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
588  */
589 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *loader, java_lang_String *name)
590 {
591         classloader_t *cl;
592         classinfo     *c;
593         utf           *u;
594
595         /* XXX is it correct to add the classloader to the hashtable here? */
596
597         cl = loader_hashtable_classloader_add((java_handle_t *) loader);
598
599         /* replace `.' by `/', this is required by the classcache */
600
601         u = javastring_toutf((java_handle_t *) name, true);
602
603         /* lookup for defining classloader */
604
605         c = classcache_lookup_defined(cl, u);
606
607         /* if not found, lookup for initiating classloader */
608
609         if (c == NULL)
610                 c = classcache_lookup(cl, u);
611
612         return LLNI_classinfo_wrap(c);
613 }
614
615
616 /*
617  * These are local overrides for various environment variables in Emacs.
618  * Please do not remove this and leave it at the end of the file, where
619  * Emacs will automagically detect them.
620  * ---------------------------------------------------------------------
621  * Local variables:
622  * mode: c
623  * indent-tabs-mode: t
624  * c-basic-offset: 4
625  * tab-width: 4
626  * End:
627  * vim:noexpandtab:sw=4:ts=4:
628  */