Merged revisions 7707-7722 via svnmerge from
[cacao.git] / src / native / vm / java_lang_ClassLoader.c
1 /* src/native/vm/java_lang_ClassLoader.c - java/lang/ClassLoader
2
3    Copyright (C) 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_VMClass.c 6131 2006-12-06 22:15:57Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <string.h>
34
35 #include "vm/types.h"
36
37 #include "mm/memory.h"
38
39 #include "vm/global.h"                          /* required by native headers */
40
41 #include "native/jni.h"
42
43 #include "native/include/java_lang_Object.h"
44
45 #if defined(ENABLE_JAVASE)
46 # include "native/include/java_lang_String.h"/* required by java_lang_Class.h */
47 # include "native/include/java_lang_Class.h"
48 # include "native/include/java_lang_ClassLoader.h"
49 # include "native/include/java_security_ProtectionDomain.h"
50 #endif
51
52 #include "vm/exceptions.h"
53 #include "vm/stringlocal.h"
54
55 #include "vmcore/class.h"
56 #include "vmcore/classcache.h"
57 #include "vmcore/options.h"
58
59
60 /*
61  * Class:     java/lang/ClassLoader
62  * Method:    defineClass
63  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
64  */
65 java_lang_Class *_Jv_java_lang_ClassLoader_defineClass(java_lang_ClassLoader *cl, java_lang_String *name, java_bytearray *data, s4 offset, s4 len, java_security_ProtectionDomain *pd)
66 {
67         utf               *utfname;
68         classinfo         *c;
69         classinfo         *r;
70         classbuffer       *cb;
71         classloader       *loader;
72         java_lang_Class   *co;
73 #if defined(ENABLE_JVMTI)
74         jint new_class_data_len = 0;
75         unsigned char* new_class_data = NULL;
76 #endif
77
78         /* check if data was passed */
79
80         if (data == NULL) {
81                 exceptions_throw_nullpointerexception();
82                 return NULL;
83         }
84
85         /* check the indexes passed */
86
87         if ((offset < 0) || (len < 0) || ((offset + len) > data->header.size)) {
88                 exceptions_throw_arrayindexoutofboundsexception();
89                 return NULL;
90         }
91
92         /* add classloader to classloader hashtable */
93
94         assert(cl);
95         loader = loader_hashtable_classloader_add((java_objectheader *) cl);
96
97         if (name != NULL) {
98                 /* convert '.' to '/' in java string */
99
100                 utfname = javastring_toutf((java_objectheader *) name, true);
101                 
102                 /* check if this class has already been defined */
103
104                 c = classcache_lookup_defined_or_initiated(cl, utfname);
105
106                 if (c != NULL) {
107                         exceptions_throw_linkageerror("duplicate class definition: ", c);
108                         return NULL;
109                 }
110         } 
111         else {
112                 utfname = NULL;
113         }
114
115 #if defined(ENABLE_JVMTI)
116         /* XXX again this will not work because of the indirection cell for classloaders */
117         assert(0);
118         /* fire Class File Load Hook JVMTI event */
119
120         if (jvmti)
121                 jvmti_ClassFileLoadHook(utfname, len, (unsigned char *) data->data, 
122                                                                 loader, (java_objectheader *) pd, 
123                                                                 &new_class_data_len, &new_class_data);
124 #endif
125
126         /* create a new classinfo struct */
127
128         c = class_create_classinfo(utfname);
129
130 #if defined(ENABLE_STATISTICS)
131         /* measure time */
132
133         if (opt_getloadingtime)
134                 loadingtime_start();
135 #endif
136
137         /* build a classbuffer with the given data */
138
139         cb = NEW(classbuffer);
140         cb->class = c;
141 #if defined(ENABLE_JVMTI)
142         /* check if the JVMTI wants to modify the class */
143         if (new_class_data == NULL) {
144 #endif
145         cb->size  = len;
146         cb->data  = (u1 *) &data->data[offset];
147 #if defined(ENABLE_JVMTI)
148         } else {
149                 cb->size  = new_class_data_len;
150                 cb->data  = (u1 *) new_class_data;
151         }
152 #endif
153         cb->pos   = cb->data;
154
155         /* preset the defining classloader */
156
157         c->classloader = loader;
158
159         /* load the class from this buffer */
160
161         r = load_class_from_classbuffer(cb);
162
163         /* free memory */
164
165         FREE(cb, classbuffer);
166
167 #if defined(ENABLE_STATISTICS)
168         /* measure time */
169
170         if (opt_getloadingtime)
171                 loadingtime_stop();
172 #endif
173
174         if (r == NULL) {
175                 /* If return value is NULL, we had a problem and the class is
176                    not loaded.  Now free the allocated memory, otherwise we
177                    could run into a DOS. */
178
179                 class_free(c);
180
181                 return NULL;
182         }
183
184         /* set ProtectionDomain */
185
186         co = (java_lang_Class *) c;
187
188         co->pd = pd;
189
190         /* Store the newly defined class in the class cache. This call also       */
191         /* checks whether a class of the same name has already been defined by    */
192         /* the same defining loader, and if so, replaces the newly created class  */
193         /* by the one defined earlier.                                            */
194         /* Important: The classinfo given to classcache_store must be             */
195         /*            fully prepared because another thread may return this       */
196         /*            pointer after the lookup at to top of this function         */
197         /*            directly after the class cache lock has been released.      */
198
199         c = classcache_store(loader, c, true);
200
201         return (java_lang_Class *) c;
202 }
203
204
205 /*
206  * These are local overrides for various environment variables in Emacs.
207  * Please do not remove this and leave it at the end of the file, where
208  * Emacs will automagically detect them.
209  * ---------------------------------------------------------------------
210  * Local variables:
211  * mode: c
212  * indent-tabs-mode: t
213  * c-basic-offset: 4
214  * tab-width: 4
215  * End:
216  * vim:noexpandtab:sw=4:ts=4:
217  */