1 /* src/native/native.c - table of native functions
3 Copyright (C) 1996-2005, 2006 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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
31 Changes: Christian Thalinger
33 $Id: native.c 4541 2006-02-21 13:37:48Z twisti $
42 #if !defined(WITH_STATIC_CLASSPATH)
48 #include "mm/memory.h"
49 #include "native/jni.h"
50 #include "native/native.h"
51 #include "native/include/java_lang_Throwable.h"
52 #include "toolbox/logging.h"
53 #include "vm/builtin.h"
54 #include "vm/exceptions.h"
55 #include "vm/global.h"
56 #include "vm/hashtable.h"
57 #include "vm/loader.h"
58 #include "vm/options.h"
59 #include "vm/resolve.h"
60 #include "vm/stringlocal.h"
61 #include "vm/jit/asmpart.h"
62 #include "vm/jit/jit.h"
65 /* include table of native functions ******************************************/
67 #include "native/include/java_lang_Cloneable.h"
68 #include "native/include/java_util_Properties.h"
69 #include "native/include/java_io_InputStream.h"
70 #include "native/include/java_io_PrintStream.h"
72 #include "native/include/gnu_classpath_VMStackWalker.h"
73 #include "native/include/gnu_classpath_VMSystemProperties.h"
74 #include "native/include/java_lang_Class.h"
75 #include "native/include/java_lang_Object.h"
76 #include "native/include/java_lang_VMClass.h"
77 #include "native/include/java_lang_VMClassLoader.h"
78 #include "native/include/java_lang_VMObject.h"
79 #include "native/include/java_lang_VMRuntime.h"
80 #include "native/include/java_lang_VMString.h"
81 #include "native/include/java_lang_VMSystem.h"
82 #include "native/include/java_lang_VMThread.h"
83 #include "native/include/java_lang_VMThrowable.h"
84 #include "native/include/java_lang_reflect_Constructor.h"
85 #include "native/include/java_lang_reflect_Field.h"
86 #include "native/include/java_lang_reflect_Method.h"
87 #include "native/include/java_lang_reflect_VMProxy.h"
88 #include "native/include/java_security_VMAccessController.h"
91 #if defined(WITH_STATIC_CLASSPATH)
93 /* these are required to prevent compiler warnings */
95 #include "native/include/java_net_DatagramPacket.h"
96 #include "native/include/java_net_InetAddress.h"
97 #include "native/include/java_net_SocketImpl.h"
99 #include "native/include/gnu_java_net_PlainDatagramSocketImpl.h"
100 #include "native/include/gnu_java_net_PlainSocketImpl.h"
101 #include "native/include/gnu_java_nio_PipeImpl.h"
102 #include "native/include/gnu_java_nio_channels_FileChannelImpl.h"
103 #include "native/include/gnu_java_nio_charset_iconv_IconvEncoder.h"
104 #include "native/include/gnu_java_nio_charset_iconv_IconvDecoder.h"
105 #include "native/include/java_lang_VMProcess.h"
106 #include "native/include/java_nio_MappedByteBufferImpl.h"
107 #include "native/include/java_nio_channels_spi_SelectorProvider.h"
109 /* now include the native table */
111 #include "native/nativetable.inc"
113 #else /* defined(WITH_STATIC_CLASSPATH) */
115 /* Ensure that symbols for functions implemented within CACAO are used
116 and exported to dlopen. */
118 static functionptr dummynativetable[] = {
119 (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
121 (functionptr) Java_gnu_classpath_VMSystemProperties_preInit,
123 (functionptr) Java_java_lang_VMClass_isInstance,
124 (functionptr) Java_java_lang_VMClass_isAssignableFrom,
125 (functionptr) Java_java_lang_VMClass_isInterface,
126 (functionptr) Java_java_lang_VMClass_isPrimitive,
127 (functionptr) Java_java_lang_VMClass_getName,
128 (functionptr) Java_java_lang_VMClass_getSuperclass,
129 (functionptr) Java_java_lang_VMClass_getInterfaces,
130 (functionptr) Java_java_lang_VMClass_getComponentType,
131 (functionptr) Java_java_lang_VMClass_getModifiers,
132 (functionptr) Java_java_lang_VMClass_getDeclaringClass,
133 (functionptr) Java_java_lang_VMClass_getDeclaredClasses,
134 (functionptr) Java_java_lang_VMClass_getDeclaredFields,
135 (functionptr) Java_java_lang_VMClass_getDeclaredMethods,
136 (functionptr) Java_java_lang_VMClass_getDeclaredConstructors,
137 (functionptr) Java_java_lang_VMClass_getClassLoader,
138 (functionptr) Java_java_lang_VMClass_forName,
139 (functionptr) Java_java_lang_VMClass_isArray,
140 (functionptr) Java_java_lang_VMClass_throwException,
142 (functionptr) Java_java_lang_VMClassLoader_defineClass,
143 (functionptr) Java_java_lang_VMClassLoader_resolveClass,
144 (functionptr) Java_java_lang_VMClassLoader_loadClass,
145 (functionptr) Java_java_lang_VMClassLoader_getPrimitiveClass,
146 (functionptr) Java_java_lang_VMClassLoader_nativeGetResources,
147 (functionptr) Java_java_lang_VMClassLoader_findLoadedClass,
149 (functionptr) Java_java_lang_VMObject_getClass,
150 (functionptr) Java_java_lang_VMObject_clone,
151 (functionptr) Java_java_lang_VMObject_notify,
152 (functionptr) Java_java_lang_VMObject_notifyAll,
153 (functionptr) Java_java_lang_VMObject_wait,
155 (functionptr) Java_java_lang_VMRuntime_availableProcessors,
156 (functionptr) Java_java_lang_VMRuntime_freeMemory,
157 (functionptr) Java_java_lang_VMRuntime_totalMemory,
158 (functionptr) Java_java_lang_VMRuntime_maxMemory,
159 (functionptr) Java_java_lang_VMRuntime_gc,
160 (functionptr) Java_java_lang_VMRuntime_runFinalization,
161 (functionptr) Java_java_lang_VMRuntime_runFinalizationForExit,
162 (functionptr) Java_java_lang_VMRuntime_traceInstructions,
163 (functionptr) Java_java_lang_VMRuntime_traceMethodCalls,
164 (functionptr) Java_java_lang_VMRuntime_runFinalizersOnExit,
165 (functionptr) Java_java_lang_VMRuntime_exit,
166 (functionptr) Java_java_lang_VMRuntime_nativeLoad,
167 (functionptr) Java_java_lang_VMRuntime_mapLibraryName,
169 (functionptr) Java_java_lang_VMString_intern,
171 (functionptr) Java_java_lang_VMSystem_arraycopy,
172 (functionptr) Java_java_lang_VMSystem_identityHashCode,
174 (functionptr) Java_java_lang_VMThread_start,
175 (functionptr) Java_java_lang_VMThread_interrupt,
176 (functionptr) Java_java_lang_VMThread_isInterrupted,
177 (functionptr) Java_java_lang_VMThread_suspend,
178 (functionptr) Java_java_lang_VMThread_resume,
179 (functionptr) Java_java_lang_VMThread_nativeSetPriority,
180 (functionptr) Java_java_lang_VMThread_nativeStop,
181 (functionptr) Java_java_lang_VMThread_currentThread,
182 (functionptr) Java_java_lang_VMThread_yield,
183 (functionptr) Java_java_lang_VMThread_interrupted,
184 (functionptr) Java_java_lang_VMThread_holdsLock,
186 (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
187 (functionptr) Java_java_lang_VMThrowable_getStackTrace,
189 (functionptr) Java_java_lang_reflect_Constructor_getModifiers,
190 (functionptr) Java_java_lang_reflect_Constructor_constructNative,
192 (functionptr) Java_java_lang_reflect_Field_getModifiers,
193 (functionptr) Java_java_lang_reflect_Field_getType,
194 (functionptr) Java_java_lang_reflect_Field_get,
195 (functionptr) Java_java_lang_reflect_Field_getBoolean,
196 (functionptr) Java_java_lang_reflect_Field_getByte,
197 (functionptr) Java_java_lang_reflect_Field_getChar,
198 (functionptr) Java_java_lang_reflect_Field_getShort,
199 (functionptr) Java_java_lang_reflect_Field_getInt,
200 (functionptr) Java_java_lang_reflect_Field_getLong,
201 (functionptr) Java_java_lang_reflect_Field_getFloat,
202 (functionptr) Java_java_lang_reflect_Field_getDouble,
203 (functionptr) Java_java_lang_reflect_Field_set,
204 (functionptr) Java_java_lang_reflect_Field_setBoolean,
205 (functionptr) Java_java_lang_reflect_Field_setByte,
206 (functionptr) Java_java_lang_reflect_Field_setChar,
207 (functionptr) Java_java_lang_reflect_Field_setShort,
208 (functionptr) Java_java_lang_reflect_Field_setInt,
209 (functionptr) Java_java_lang_reflect_Field_setLong,
210 (functionptr) Java_java_lang_reflect_Field_setFloat,
211 (functionptr) Java_java_lang_reflect_Field_setDouble,
213 (functionptr) Java_java_lang_reflect_Method_getModifiers,
214 (functionptr) Java_java_lang_reflect_Method_getReturnType,
215 (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
216 (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
217 (functionptr) Java_java_lang_reflect_Method_invokeNative,
219 (functionptr) Java_java_lang_reflect_VMProxy_getProxyClass,
220 (functionptr) Java_java_lang_reflect_VMProxy_getProxyData,
221 (functionptr) Java_java_lang_reflect_VMProxy_generateProxyClass,
223 (functionptr) Java_java_security_VMAccessController_getStack,
226 #endif /* defined(WITH_STATIC_CLASSPATH) */
229 /* tables for methods *********************************************************/
231 #if defined(WITH_STATIC_CLASSPATH)
232 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
234 /* table for fast string comparison */
235 static nativecompref nativecomptable[NATIVETABLESIZE];
237 /* string comparsion table initialized */
238 static bool nativecompdone = false;
242 /* global variables ***********************************************************/
244 #if !defined(WITH_STATIC_CLASSPATH)
245 static hashtable hashtable_library;
246 static lt_dlhandle mainhandle;
250 /* native_loadclasses **********************************************************
252 Load classes required for native methods.
254 *******************************************************************************/
256 bool native_init(void)
258 #if !defined(WITH_STATIC_CLASSPATH)
261 /* We need to access the dummy native table, not only to remove a
262 warning but to be sure that the table is not optimized away
263 (gcc does this since 3.4). */
265 p = &dummynativetable;
267 /* initialize libltdl */
270 /* XXX how can we throw an exception here? */
271 log_text(lt_dlerror());
276 /* get the handle for the main program */
278 if (!(mainhandle = lt_dlopen(NULL)))
281 /* initialize library hashtable, 10 entries should be enough */
283 hashtable_create(&hashtable_library, 10);
286 /* everything's ok */
292 /* native_hashtable_library_add ************************************************
294 Adds an entry to the native library hashtable.
296 *******************************************************************************/
298 #if !defined(WITH_STATIC_CLASSPATH)
299 void native_hashtable_library_add(utf *filename, java_objectheader *loader,
302 hashtable_library_loader_entry *le;
303 hashtable_library_name_entry *ne; /* library name */
304 u4 key; /* hashkey */
305 u4 slot; /* slot in hashtable */
307 /* normally addresses are aligned to 4, 8 or 16 bytes */
309 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
310 slot = key & (hashtable_library.size - 1);
311 le = hashtable_library.ptr[slot];
313 /* search external hash chain for the entry */
316 if (le->loader == loader)
319 le = le->hashlink; /* next element in external chain */
322 /* no loader found? create a new entry */
325 le = NEW(hashtable_library_loader_entry);
330 /* insert entry into hashtable */
333 (hashtable_library_loader_entry *) hashtable_library.ptr[slot];
334 hashtable_library.ptr[slot] = le;
336 /* update number of hashtable-entries */
338 hashtable_library.entries++;
342 /* search for library name */
347 if (ne->name == filename)
350 ne = ne->hashlink; /* next element in external chain */
353 /* not found? add the library name to the classloader */
355 ne = NEW(hashtable_library_name_entry);
360 /* insert entry into external chain */
362 ne->hashlink = le->namelink;
365 #endif /* !defined(WITH_STATIC_CLASSPATH) */
368 /* native_hashtable_library_find ***********************************************
370 Find an entry in the native library hashtable.
372 *******************************************************************************/
374 #if !defined(WITH_STATIC_CLASSPATH)
375 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
376 java_objectheader *loader)
378 hashtable_library_loader_entry *le;
379 hashtable_library_name_entry *ne; /* library name */
380 u4 key; /* hashkey */
381 u4 slot; /* slot in hashtable */
383 /* normally addresses are aligned to 4, 8 or 16 bytes */
385 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
386 slot = key & (hashtable_library.size - 1);
387 le = hashtable_library.ptr[slot];
389 /* search external hash chain for the entry */
392 if (le->loader == loader)
395 le = le->hashlink; /* next element in external chain */
398 /* no loader found? return NULL */
403 /* search for library name */
408 if (ne->name == filename)
411 ne = ne->hashlink; /* next element in external chain */
414 /* return entry, if no entry was found, ne is NULL */
418 #endif /* !defined(WITH_STATIC_CLASSPATH) */
421 /* native_findfunction *********************************************************
423 Looks up a method (must have the same class name, method name,
424 descriptor and 'static'ness) and returns a function pointer to it.
425 Returns: function pointer or NULL (if there is no such method)
427 Remark: For faster operation, the names/descriptors are converted
428 from C strings to Unicode the first time this function is called.
430 *******************************************************************************/
432 #if defined(WITH_STATIC_CLASSPATH)
433 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
436 /* entry of table for fast string comparison */
437 struct nativecompref *n;
440 isstatic = isstatic ? true : false;
442 if (!nativecompdone) {
443 for (i = 0; i < NATIVETABLESIZE; i++) {
444 nativecomptable[i].classname =
445 utf_new_char(nativetable[i].classname);
447 nativecomptable[i].methodname =
448 utf_new_char(nativetable[i].methodname);
450 nativecomptable[i].descriptor =
451 utf_new_char(nativetable[i].descriptor);
453 nativecomptable[i].isstatic = nativetable[i].isstatic;
454 nativecomptable[i].func = nativetable[i].func;
457 nativecompdone = true;
460 for (i = 0; i < NATIVETABLESIZE; i++) {
461 n = &(nativecomptable[i]);
463 if (cname == n->classname && mname == n->methodname &&
464 desc == n->descriptor && isstatic == n->isstatic)
469 /* no function was found, throw exception */
472 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
477 #endif /* defined(WITH_STATIC_CLASSPATH) */
480 /* native_make_overloaded_function *********************************************
484 *******************************************************************************/
486 #if !defined(WITH_STATIC_CLASSPATH)
487 static char *native_make_overloaded_function(char *name, utf *desc)
495 utf_ptr = desc->text;
496 namelen = strlen(name) + strlen("__") + strlen("0");
498 /* calculate additional length */
500 while ((c = utf_nextu2(&utf_ptr)) != ')') {
517 while (utf_nextu2(&utf_ptr) != ';')
529 /* reallocate memory */
533 newname = DMNEW(char, namelen);
534 MCOPY(newname, name, char, i);
536 utf_ptr = desc->text;
541 while ((c = utf_nextu2(&utf_ptr)) != ')') {
559 while ((c = utf_nextu2(&utf_ptr)) != ';')
560 if (((c >= 'a') && (c <= 'z')) ||
561 ((c >= 'A') && (c <= 'Z')) ||
562 ((c >= '0') && (c <= '9')))
584 /* native_resolve_function *****************************************************
586 Resolves a native function, maybe from a dynamic library.
588 *******************************************************************************/
590 functionptr native_resolve_function(methodinfo *m)
599 hashtable_library_loader_entry *le;
600 hashtable_library_name_entry *ne;
601 u4 key; /* hashkey */
602 u4 slot; /* slot in hashtable */
608 if (opt_verbosejni) {
609 printf("[Dynamic-linking native method ");
610 utf_display_classname(m->class->name);
612 utf_display(m->name);
616 /* calculate length of native function name */
618 namelen = strlen("Java_") + utf_strlen(m->class->name) + strlen("_") +
619 utf_strlen(m->name) + strlen("0");
621 /* check for underscores in class name */
623 utf_ptr = m->class->name->text;
624 utf_endptr = UTF_END(m->class->name);
626 while (utf_ptr < utf_endptr)
627 if (utf_nextu2(&utf_ptr) == '_')
630 /* check for underscores in method name */
632 utf_ptr = m->name->text;
633 utf_endptr = UTF_END(m->name);
635 while (utf_ptr < utf_endptr)
636 if (utf_nextu2(&utf_ptr) == '_')
639 /* allocate memory */
641 dumpsize = dump_size();
643 name = DMNEW(char, namelen);
646 /* generate name of native functions */
648 strcpy(name, "Java_");
651 utf_ptr = m->class->name->text;
652 utf_endptr = UTF_END(m->class->name);
654 for (; utf_ptr < utf_endptr; utf_ptr++, i++) {
657 /* escape sequence for '_' is '_1' */
662 /* replace '/' with '_' */
668 /* seperator between class and method */
672 utf_ptr = m->name->text;
673 utf_endptr = UTF_END(m->name);
675 for (; utf_ptr < utf_endptr; utf_ptr++, i++) {
678 /* escape sequence for '_' is '_1' */
689 /* generate overloaded function (having the types in it's name) */
691 newname = native_make_overloaded_function(name, m->descriptor);
693 /* check the library hash entries of the classloader of the
698 /* normally addresses are aligned to 4, 8 or 16 bytes */
700 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
701 slot = key & (hashtable_library.size - 1);
702 le = hashtable_library.ptr[slot];
704 /* iterate through loaders in this hash slot */
706 while ((le != NULL) && (sym == NULL)) {
707 /* iterate through names in this loader */
711 while ((ne != NULL) && (sym == NULL)) {
712 sym = lt_dlsym(ne->handle, name);
715 sym = lt_dlsym(ne->handle, newname);
728 /* If not found, try to find the native function symbol in the
732 sym = lt_dlsym(mainhandle, name);
735 sym = lt_dlsym(mainhandle, newname);
739 printf("internal ]\n");
743 /* no symbol found? throw exception */
747 printf("failed ]\n");
750 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
756 dump_release(dumpsize);
758 return (functionptr) (ptrint) sym;
760 #endif /* !defined(WITH_STATIC_CLASSPATH) */
763 /* native_new_and_init *********************************************************
765 Creates a new object on the heap and calls the initializer.
766 Returns the object pointer or NULL if memory is exhausted.
768 *******************************************************************************/
770 java_objectheader *native_new_and_init(classinfo *c)
773 java_objectheader *o;
776 return *exceptionptr;
785 /* try to find the initializer */
787 m = class_findmethod(c, utf_init, utf_void__void);
789 /* ATTENTION: returning the object here is ok, since the class may
790 not have an initializer */
795 /* call initializer */
797 ASM_CALLJAVAFUNCTION(m, o, NULL, NULL, NULL);
803 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
806 java_objectheader *o;
809 return *exceptionptr;
818 /* find initializer */
820 m = class_resolveclassmethod(c,
822 utf_java_lang_String__void,
826 /* initializer not found */
831 /* call initializer */
833 ASM_CALLJAVAFUNCTION(m, o, s, NULL, NULL);
839 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
842 java_objectheader *o;
845 return *exceptionptr;
854 /* find initializer */
856 m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
858 /* initializer not found */
863 /* call initializer */
865 ASM_CALLJAVAFUNCTION(m, o, (void *) (ptrint) i, NULL, NULL);
871 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
874 java_objectheader *o;
877 return *exceptionptr;
886 /* find initializer */
888 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
890 /* initializer not found */
895 /* call initializer */
897 ASM_CALLJAVAFUNCTION(m, o, t, NULL, NULL);
903 /* native_get_parametertypes ***************************************************
905 Use the descriptor of a method to generate a java/lang/Class array
906 which contains the classes of the parametertypes of the method.
908 *******************************************************************************/
910 java_objectarray *native_get_parametertypes(methodinfo *m)
913 typedesc *paramtypes;
915 java_objectarray *oa;
920 /* is the descriptor fully parsed? */
922 if (!m->parseddesc->params)
923 if (!descriptor_params_from_paramtypes(md, m->flags))
926 paramtypes = md->paramtypes;
927 paramcount = md->paramcount;
929 /* skip `this' pointer */
931 if (!(m->flags & ACC_STATIC)) {
936 /* create class-array */
938 oa = builtin_anewarray(paramcount, class_java_lang_Class);
945 for (i = 0; i < paramcount; i++)
946 if (!resolve_class_from_typedesc(¶mtypes[i], true, false,
947 (classinfo **) &oa->data[i]))
954 /* native_get_exceptiontypes ***************************************************
956 Get the exceptions which can be thrown by a method.
958 *******************************************************************************/
960 java_objectarray *native_get_exceptiontypes(methodinfo *m)
962 java_objectarray *oa;
966 /* create class-array */
968 oa = builtin_anewarray(m->thrownexceptionscount, class_java_lang_Class);
973 for (i = 0; i < m->thrownexceptionscount; i++) {
974 if (!resolve_classref_or_classinfo(NULL, m->thrownexceptions[i],
975 resolveEager, true, false, &c))
978 oa->data[i] = (java_objectheader *) c;
985 /* native_get_returntype *******************************************************
987 Get the returntype class of a method.
989 *******************************************************************************/
991 classinfo *native_get_returntype(methodinfo *m)
995 if (!resolve_class_from_typedesc(&(m->parseddesc->returntype), true, false,
1004 * These are local overrides for various environment variables in Emacs.
1005 * Please do not remove this and leave it at the end of the file, where
1006 * Emacs will automagically detect them.
1007 * ---------------------------------------------------------------------
1010 * indent-tabs-mode: t