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 4530 2006-02-21 09:11:53Z 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"
90 #if defined(WITH_STATIC_CLASSPATH)
92 /* these are required to prevent compiler warnings */
94 #include "native/include/java_net_DatagramPacket.h"
95 #include "native/include/java_net_InetAddress.h"
96 #include "native/include/java_net_SocketImpl.h"
98 #include "native/include/gnu_java_net_PlainDatagramSocketImpl.h"
99 #include "native/include/gnu_java_net_PlainSocketImpl.h"
100 #include "native/include/gnu_java_nio_PipeImpl.h"
101 #include "native/include/gnu_java_nio_channels_FileChannelImpl.h"
102 #include "native/include/gnu_java_nio_charset_iconv_IconvEncoder.h"
103 #include "native/include/gnu_java_nio_charset_iconv_IconvDecoder.h"
104 #include "native/include/java_lang_VMProcess.h"
105 #include "native/include/java_nio_MappedByteBufferImpl.h"
106 #include "native/include/java_nio_channels_spi_SelectorProvider.h"
108 /* now include the native table */
110 #include "native/nativetable.inc"
112 #else /* defined(WITH_STATIC_CLASSPATH) */
114 /* Ensure that symbols for functions implemented within CACAO are used
115 and exported to dlopen. */
117 static functionptr dummynativetable[] = {
118 (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
120 (functionptr) Java_gnu_classpath_VMSystemProperties_preInit,
122 (functionptr) Java_java_lang_VMClass_isInstance,
123 (functionptr) Java_java_lang_VMClass_isAssignableFrom,
124 (functionptr) Java_java_lang_VMClass_isInterface,
125 (functionptr) Java_java_lang_VMClass_isPrimitive,
126 (functionptr) Java_java_lang_VMClass_getName,
127 (functionptr) Java_java_lang_VMClass_getSuperclass,
128 (functionptr) Java_java_lang_VMClass_getInterfaces,
129 (functionptr) Java_java_lang_VMClass_getComponentType,
130 (functionptr) Java_java_lang_VMClass_getModifiers,
131 (functionptr) Java_java_lang_VMClass_getDeclaringClass,
132 (functionptr) Java_java_lang_VMClass_getDeclaredClasses,
133 (functionptr) Java_java_lang_VMClass_getDeclaredFields,
134 (functionptr) Java_java_lang_VMClass_getDeclaredMethods,
135 (functionptr) Java_java_lang_VMClass_getDeclaredConstructors,
136 (functionptr) Java_java_lang_VMClass_getClassLoader,
137 (functionptr) Java_java_lang_VMClass_forName,
138 (functionptr) Java_java_lang_VMClass_isArray,
139 (functionptr) Java_java_lang_VMClass_throwException,
141 (functionptr) Java_java_lang_VMClassLoader_defineClass,
142 (functionptr) Java_java_lang_VMClassLoader_resolveClass,
143 (functionptr) Java_java_lang_VMClassLoader_loadClass,
144 (functionptr) Java_java_lang_VMClassLoader_getPrimitiveClass,
145 (functionptr) Java_java_lang_VMClassLoader_nativeGetResources,
146 (functionptr) Java_java_lang_VMClassLoader_findLoadedClass,
148 (functionptr) Java_java_lang_VMObject_getClass,
149 (functionptr) Java_java_lang_VMObject_clone,
150 (functionptr) Java_java_lang_VMObject_notify,
151 (functionptr) Java_java_lang_VMObject_notifyAll,
152 (functionptr) Java_java_lang_VMObject_wait,
154 (functionptr) Java_java_lang_VMRuntime_availableProcessors,
155 (functionptr) Java_java_lang_VMRuntime_freeMemory,
156 (functionptr) Java_java_lang_VMRuntime_totalMemory,
157 (functionptr) Java_java_lang_VMRuntime_maxMemory,
158 (functionptr) Java_java_lang_VMRuntime_gc,
159 (functionptr) Java_java_lang_VMRuntime_runFinalization,
160 (functionptr) Java_java_lang_VMRuntime_runFinalizationForExit,
161 (functionptr) Java_java_lang_VMRuntime_traceInstructions,
162 (functionptr) Java_java_lang_VMRuntime_traceMethodCalls,
163 (functionptr) Java_java_lang_VMRuntime_runFinalizersOnExit,
164 (functionptr) Java_java_lang_VMRuntime_exit,
165 (functionptr) Java_java_lang_VMRuntime_nativeLoad,
166 (functionptr) Java_java_lang_VMRuntime_mapLibraryName,
168 (functionptr) Java_java_lang_VMString_intern,
170 (functionptr) Java_java_lang_VMSystem_arraycopy,
171 (functionptr) Java_java_lang_VMSystem_identityHashCode,
173 (functionptr) Java_java_lang_VMThread_start,
174 (functionptr) Java_java_lang_VMThread_interrupt,
175 (functionptr) Java_java_lang_VMThread_isInterrupted,
176 (functionptr) Java_java_lang_VMThread_suspend,
177 (functionptr) Java_java_lang_VMThread_resume,
178 (functionptr) Java_java_lang_VMThread_nativeSetPriority,
179 (functionptr) Java_java_lang_VMThread_nativeStop,
180 (functionptr) Java_java_lang_VMThread_currentThread,
181 (functionptr) Java_java_lang_VMThread_yield,
182 (functionptr) Java_java_lang_VMThread_interrupted,
183 (functionptr) Java_java_lang_VMThread_holdsLock,
185 (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
186 (functionptr) Java_java_lang_VMThrowable_getStackTrace,
188 (functionptr) Java_java_lang_reflect_Constructor_getModifiers,
189 (functionptr) Java_java_lang_reflect_Constructor_constructNative,
191 (functionptr) Java_java_lang_reflect_Field_getModifiers,
192 (functionptr) Java_java_lang_reflect_Field_getType,
193 (functionptr) Java_java_lang_reflect_Field_get,
194 (functionptr) Java_java_lang_reflect_Field_getBoolean,
195 (functionptr) Java_java_lang_reflect_Field_getByte,
196 (functionptr) Java_java_lang_reflect_Field_getChar,
197 (functionptr) Java_java_lang_reflect_Field_getShort,
198 (functionptr) Java_java_lang_reflect_Field_getInt,
199 (functionptr) Java_java_lang_reflect_Field_getLong,
200 (functionptr) Java_java_lang_reflect_Field_getFloat,
201 (functionptr) Java_java_lang_reflect_Field_getDouble,
202 (functionptr) Java_java_lang_reflect_Field_set,
203 (functionptr) Java_java_lang_reflect_Field_setBoolean,
204 (functionptr) Java_java_lang_reflect_Field_setByte,
205 (functionptr) Java_java_lang_reflect_Field_setChar,
206 (functionptr) Java_java_lang_reflect_Field_setShort,
207 (functionptr) Java_java_lang_reflect_Field_setInt,
208 (functionptr) Java_java_lang_reflect_Field_setLong,
209 (functionptr) Java_java_lang_reflect_Field_setFloat,
210 (functionptr) Java_java_lang_reflect_Field_setDouble,
212 (functionptr) Java_java_lang_reflect_Method_getModifiers,
213 (functionptr) Java_java_lang_reflect_Method_getReturnType,
214 (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
215 (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
216 (functionptr) Java_java_lang_reflect_Method_invokeNative,
218 (functionptr) Java_java_lang_reflect_VMProxy_getProxyClass,
219 (functionptr) Java_java_lang_reflect_VMProxy_getProxyData,
220 (functionptr) Java_java_lang_reflect_VMProxy_generateProxyClass,
222 (functionptr) Java_java_security_VMAccessController_getStack,
225 #endif /* defined(WITH_STATIC_CLASSPATH) */
228 /* tables for methods *********************************************************/
230 #ifdef WITH_STATIC_CLASSPATH
231 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
233 /* table for fast string comparison */
234 static nativecompref nativecomptable[NATIVETABLESIZE];
236 /* string comparsion table initialized */
237 static bool nativecompdone = false;
241 /* global variables ***********************************************************/
243 #if !defined(WITH_STATIC_CLASSPATH)
244 static hashtable hashtable_library;
245 static lt_dlhandle mainhandle;
249 /* native_loadclasses **********************************************************
251 Load classes required for native methods.
253 *******************************************************************************/
255 bool native_init(void)
257 #if !defined(WITH_STATIC_CLASSPATH)
260 /* We need to access the dummy native table, not only to remove a warning */
261 /* but to be sure that the table is not optimized away (gcc does this */
264 p = &dummynativetable;
266 /* initialize libltdl */
269 /* XXX how can we throw an exception here? */
270 log_text(lt_dlerror());
275 /* get the handle for the main program */
277 if (!(mainhandle = lt_dlopen(NULL)))
280 /* initialize library hashtable, 10 entries should be enough */
282 hashtable_create(&hashtable_library, 10);
285 /* everything's ok */
291 /* native_hashtable_library_add ************************************************
293 Adds an entry to the native library hashtable.
295 *******************************************************************************/
297 #if !defined(WITH_STATIC_CLASSPATH)
298 void native_hashtable_library_add(utf *filename, java_objectheader *loader,
301 hashtable_library_loader_entry *le;
302 hashtable_library_name_entry *ne; /* library name */
303 u4 key; /* hashkey */
304 u4 slot; /* slot in hashtable */
306 /* normally addresses are aligned to 4, 8 or 16 bytes */
308 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
309 slot = key & (hashtable_library.size - 1);
310 le = hashtable_library.ptr[slot];
312 /* search external hash chain for the entry */
315 if (le->loader == loader)
318 le = le->hashlink; /* next element in external chain */
321 /* no loader found? create a new entry */
324 le = NEW(hashtable_library_loader_entry);
329 /* insert entry into hashtable */
332 (hashtable_library_loader_entry *) hashtable_library.ptr[slot];
333 hashtable_library.ptr[slot] = le;
335 /* update number of hashtable-entries */
337 hashtable_library.entries++;
341 /* search for library name */
346 if (ne->name == filename)
349 ne = ne->hashlink; /* next element in external chain */
352 /* not found? add the library name to the classloader */
354 ne = NEW(hashtable_library_name_entry);
359 /* insert entry into external chain */
361 ne->hashlink = le->namelink;
364 #endif /* !defined(WITH_STATIC_CLASSPATH) */
367 /* native_hashtable_library_find ***********************************************
369 Find an entry in the native library hashtable.
371 *******************************************************************************/
373 #if !defined(WITH_STATIC_CLASSPATH)
374 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
375 java_objectheader *loader)
377 hashtable_library_loader_entry *le;
378 hashtable_library_name_entry *ne; /* library name */
379 u4 key; /* hashkey */
380 u4 slot; /* slot in hashtable */
382 /* normally addresses are aligned to 4, 8 or 16 bytes */
384 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
385 slot = key & (hashtable_library.size - 1);
386 le = hashtable_library.ptr[slot];
388 /* search external hash chain for the entry */
391 if (le->loader == loader)
394 le = le->hashlink; /* next element in external chain */
397 /* no loader found? return NULL */
402 /* search for library name */
407 if (ne->name == filename)
410 ne = ne->hashlink; /* next element in external chain */
413 /* return entry, if no entry was found, ne is NULL */
417 #endif /* !defined(WITH_STATIC_CLASSPATH) */
420 /* native_findfunction *********************************************************
422 Looks up a method (must have the same class name, method name,
423 descriptor and 'static'ness) and returns a function pointer to it.
424 Returns: function pointer or NULL (if there is no such method)
426 Remark: For faster operation, the names/descriptors are converted
427 from C strings to Unicode the first time this function is called.
429 *******************************************************************************/
431 #if defined(WITH_STATIC_CLASSPATH)
432 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
435 /* entry of table for fast string comparison */
436 struct nativecompref *n;
439 isstatic = isstatic ? true : false;
441 if (!nativecompdone) {
442 for (i = 0; i < NATIVETABLESIZE; i++) {
443 nativecomptable[i].classname =
444 utf_new_char(nativetable[i].classname);
446 nativecomptable[i].methodname =
447 utf_new_char(nativetable[i].methodname);
449 nativecomptable[i].descriptor =
450 utf_new_char(nativetable[i].descriptor);
452 nativecomptable[i].isstatic = nativetable[i].isstatic;
453 nativecomptable[i].func = nativetable[i].func;
456 nativecompdone = true;
459 for (i = 0; i < NATIVETABLESIZE; i++) {
460 n = &(nativecomptable[i]);
462 if (cname == n->classname && mname == n->methodname &&
463 desc == n->descriptor && isstatic == n->isstatic)
468 /* no function was found, throw exception */
471 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
476 #endif /* defined(WITH_STATIC_CLASSPATH) */
479 /* native_make_overloaded_function *********************************************
483 *******************************************************************************/
485 #if !defined(WITH_STATIC_CLASSPATH)
486 static char *native_make_overloaded_function(char *name, utf *desc)
494 utf_ptr = desc->text;
495 namelen = strlen(name) + strlen("__") + strlen("0");
497 /* calculate additional length */
499 while ((c = utf_nextu2(&utf_ptr)) != ')') {
516 while (utf_nextu2(&utf_ptr) != ';')
528 /* reallocate memory */
532 newname = DMNEW(char, namelen);
533 MCOPY(newname, name, char, i);
535 utf_ptr = desc->text;
540 while ((c = utf_nextu2(&utf_ptr)) != ')') {
558 while ((c = utf_nextu2(&utf_ptr)) != ';')
559 if (((c >= 'a') && (c <= 'z')) ||
560 ((c >= 'A') && (c <= 'Z')) ||
561 ((c >= '0') && (c <= '9')))
583 /* native_resolve_function *****************************************************
585 Resolves a native function, maybe from a dynamic library.
587 *******************************************************************************/
589 functionptr native_resolve_function(methodinfo *m)
598 hashtable_library_loader_entry *le;
599 hashtable_library_name_entry *ne;
600 u4 key; /* hashkey */
601 u4 slot; /* slot in hashtable */
607 if (opt_verbosejni) {
608 printf("[Dynamic-linking native method ");
609 utf_display_classname(m->class->name);
611 utf_display(m->name);
615 /* calculate length of native function name */
617 namelen = strlen("Java_") + utf_strlen(m->class->name) + strlen("_") +
618 utf_strlen(m->name) + strlen("0");
620 /* check for underscores in class name */
622 utf_ptr = m->class->name->text;
623 utf_endptr = UTF_END(m->class->name);
625 while (utf_ptr < utf_endptr)
626 if (utf_nextu2(&utf_ptr) == '_')
629 /* check for underscores in method name */
631 utf_ptr = m->name->text;
632 utf_endptr = UTF_END(m->name);
634 while (utf_ptr < utf_endptr)
635 if (utf_nextu2(&utf_ptr) == '_')
638 /* allocate memory */
640 dumpsize = dump_size();
642 name = DMNEW(char, namelen);
645 /* generate name of native functions */
647 strcpy(name, "Java_");
650 utf_ptr = m->class->name->text;
651 utf_endptr = UTF_END(m->class->name);
653 for (; utf_ptr < utf_endptr; utf_ptr++, i++) {
656 /* escape sequence for '_' is '_1' */
661 /* replace '/' with '_' */
667 /* seperator between class and method */
671 utf_ptr = m->name->text;
672 utf_endptr = UTF_END(m->name);
674 for (; utf_ptr < utf_endptr; utf_ptr++, i++) {
677 /* escape sequence for '_' is '_1' */
688 /* generate overloaded function (having the types in it's name) */
690 newname = native_make_overloaded_function(name, m->descriptor);
692 /* check the library hash entries of the classloader of the
697 /* normally addresses are aligned to 4, 8 or 16 bytes */
699 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
700 slot = key & (hashtable_library.size - 1);
701 le = hashtable_library.ptr[slot];
703 /* iterate through loaders in this hash slot */
705 while ((le != NULL) && (sym == NULL)) {
706 /* iterate through names in this loader */
710 while ((ne != NULL) && (sym == NULL)) {
711 sym = lt_dlsym(ne->handle, name);
714 sym = lt_dlsym(ne->handle, newname);
727 /* If not found, try to find the native function symbol in the
731 sym = lt_dlsym(mainhandle, name);
734 sym = lt_dlsym(mainhandle, newname);
738 printf("internal ]\n");
742 /* no symbol found? throw exception */
746 printf("failed ]\n");
749 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
755 dump_release(dumpsize);
757 return (functionptr) (ptrint) sym;
759 #endif /* !defined(WITH_STATIC_CLASSPATH) */
762 /* native_new_and_init *********************************************************
764 Creates a new object on the heap and calls the initializer.
765 Returns the object pointer or NULL if memory is exhausted.
767 *******************************************************************************/
769 java_objectheader *native_new_and_init(classinfo *c)
772 java_objectheader *o;
775 return *exceptionptr;
784 /* try to find the initializer */
786 m = class_findmethod(c, utf_init, utf_void__void);
788 /* ATTENTION: returning the object here is ok, since the class may
789 not have an initializer */
794 /* call initializer */
796 ASM_CALLJAVAFUNCTION(m, o, NULL, NULL, NULL);
802 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
805 java_objectheader *o;
808 return *exceptionptr;
817 /* find initializer */
819 m = class_resolveclassmethod(c,
821 utf_java_lang_String__void,
825 /* initializer not found */
830 /* call initializer */
832 ASM_CALLJAVAFUNCTION(m, o, s, NULL, NULL);
838 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
841 java_objectheader *o;
844 return *exceptionptr;
853 /* find initializer */
855 m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
857 /* initializer not found */
862 /* call initializer */
864 ASM_CALLJAVAFUNCTION(m, o, (void *) (ptrint) i, NULL, NULL);
870 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
873 java_objectheader *o;
876 return *exceptionptr;
885 /* find initializer */
887 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
889 /* initializer not found */
894 /* call initializer */
896 ASM_CALLJAVAFUNCTION(m, o, t, NULL, NULL);
902 /* native_get_parametertypes ***************************************************
904 Use the descriptor of a method to generate a java/lang/Class array
905 which contains the classes of the parametertypes of the method.
907 *******************************************************************************/
909 java_objectarray *native_get_parametertypes(methodinfo *m)
912 typedesc *paramtypes;
914 java_objectarray *oa;
919 /* is the descriptor fully parsed? */
921 if (!m->parseddesc->params)
922 if (!descriptor_params_from_paramtypes(md, m->flags))
925 paramtypes = md->paramtypes;
926 paramcount = md->paramcount;
928 /* skip `this' pointer */
930 if (!(m->flags & ACC_STATIC)) {
935 /* create class-array */
937 oa = builtin_anewarray(paramcount, class_java_lang_Class);
944 for (i = 0; i < paramcount; i++)
945 if (!resolve_class_from_typedesc(¶mtypes[i], true, false,
946 (classinfo **) &oa->data[i]))
953 /* native_get_exceptiontypes ***************************************************
955 Get the exceptions which can be thrown by a method.
957 *******************************************************************************/
959 java_objectarray *native_get_exceptiontypes(methodinfo *m)
961 java_objectarray *oa;
965 /* create class-array */
967 oa = builtin_anewarray(m->thrownexceptionscount, class_java_lang_Class);
972 for (i = 0; i < m->thrownexceptionscount; i++) {
973 if (!resolve_classref_or_classinfo(NULL, m->thrownexceptions[i],
974 resolveEager, true, false, &c))
977 oa->data[i] = (java_objectheader *) c;
984 /* native_get_returntype *******************************************************
986 Get the returntype class of a method.
988 *******************************************************************************/
990 classinfo *native_get_returntype(methodinfo *m)
994 if (!resolve_class_from_typedesc(&(m->parseddesc->returntype), true, false,
1003 * These are local overrides for various environment variables in Emacs.
1004 * Please do not remove this and leave it at the end of the file, where
1005 * Emacs will automagically detect them.
1006 * ---------------------------------------------------------------------
1009 * indent-tabs-mode: t