1 /* src/native/native.c - table of native functions
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
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 $Id: native.c 8056 2007-06-10 14:49:57Z michi $
35 #if !defined(WITH_STATIC_CLASSPATH)
41 #include "mm/memory.h"
43 #include "native/jni.h"
44 #include "native/native.h"
46 #include "native/vm/nativevm.h"
48 #include "threads/lock-common.h"
50 #include "toolbox/avl.h"
51 #include "toolbox/hashtable.h"
52 #include "toolbox/logging.h"
54 #include "vm/builtin.h"
55 #include "vm/exceptions.h"
56 #include "vm/global.h"
57 #include "vm/stringlocal.h"
60 #include "vm/jit/asmpart.h"
61 #include "vm/jit/jit.h"
63 #include "vmcore/loader.h"
64 #include "vmcore/options.h"
65 #include "vm/resolve.h"
67 #if defined(ENABLE_JVMTI)
68 #include "native/jvmti/cacaodbg.h"
72 /* include table of native functions ******************************************/
74 #if defined(WITH_STATIC_CLASSPATH)
75 # include "native/nativetable.inc"
79 /* tables for methods *********************************************************/
81 #if defined(WITH_STATIC_CLASSPATH)
82 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
84 /* table for fast string comparison */
85 static nativecompref nativecomptable[NATIVETABLESIZE];
87 /* string comparsion table initialized */
88 static bool nativecompdone = false;
92 /* global variables ***********************************************************/
94 static avl_tree_t *tree_native_methods;
95 static hashtable *hashtable_library;
98 /* prototypes *****************************************************************/
100 static s4 native_tree_native_methods_comparator(const void *treenode, const void *node);
103 /* native_init *****************************************************************
105 Initializes the native subsystem.
107 *******************************************************************************/
109 bool native_init(void)
111 #if !defined(WITH_STATIC_CLASSPATH)
112 /* initialize libltdl */
115 vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
117 /* initialize library hashtable, 10 entries should be enough */
119 hashtable_library = NEW(hashtable);
121 hashtable_create(hashtable_library, 10);
124 /* initialize the native methods table */
126 tree_native_methods = avl_create(&native_tree_native_methods_comparator);
128 /* register the intern native functions */
130 if (!nativevm_init())
133 /* everything's ok */
139 /* native_tree_native_methods_comparator ***************************************
141 Comparison function for AVL tree of native methods.
144 treenode....node in the tree
145 node........node to compare with tree-node
150 *******************************************************************************/
152 static s4 native_tree_native_methods_comparator(const void *treenode, const void *node)
154 const native_methods_node_t *treenmn;
155 const native_methods_node_t *nmn;
160 /* these are for walking the tree */
162 if (treenmn->classname < nmn->classname)
164 else if (treenmn->classname > nmn->classname)
167 if (treenmn->name < nmn->name)
169 else if (treenmn->name > nmn->name)
172 if (treenmn->descriptor < nmn->descriptor)
174 else if (treenmn->descriptor > nmn->descriptor)
177 /* all pointers are equal, we have found the entry */
183 /* native_make_overloaded_function *********************************************
187 *******************************************************************************/
189 #if !defined(WITH_STATIC_CLASSPATH)
190 static utf *native_make_overloaded_function(utf *name, utf *descriptor)
202 dumpsize = dump_size();
204 utf_ptr = descriptor->text;
205 namelen = strlen(name->text) + strlen("__") + strlen("0");
207 /* calculate additional length */
209 while ((c = utf_nextu2(&utf_ptr)) != ')') {
226 while (utf_nextu2(&utf_ptr) != ';')
237 /* reallocate memory */
239 i = strlen(name->text);
241 newname = DMNEW(char, namelen);
242 MCOPY(newname, name->text, char, i);
244 utf_ptr = descriptor->text;
249 while ((c = utf_nextu2(&utf_ptr)) != ')') {
267 while ((c = utf_nextu2(&utf_ptr)) != ';')
268 if (((c >= 'a') && (c <= 'z')) ||
269 ((c >= 'A') && (c <= 'Z')) ||
270 ((c >= '0') && (c <= '9')))
288 /* make a utf-string */
290 u = utf_new_char(newname);
294 dump_release(dumpsize);
300 /* native_insert_char **********************************************************
302 Inserts the passed UTF character into the native method name. If
303 necessary it is escaped properly.
305 *******************************************************************************/
307 static s4 native_insert_char(char *name, u4 pos, u2 c)
315 /* replace '/' or '.' with '_' */
320 /* escape sequence for '_' is '_1' */
326 /* escape sequence for ';' is '_2' */
332 /* escape sequence for '[' is '_1' */
341 /* unicode character */
345 for (i = 0; i < 4; ++i) {
347 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
356 /* return the new buffer index */
362 /* native_method_symbol ********************************************************
364 Generate a method-symbol string out of the class name and the
367 *******************************************************************************/
369 static utf *native_method_symbol(utf *classname, utf *methodname)
382 dumpsize = dump_size();
384 /* Calculate length of native function name. We multiply the
385 class and method name length by 6 as this is the maxium
386 escape-sequence that can be generated (unicode). */
390 utf_get_number_of_u2s(classname) * 6 +
392 utf_get_number_of_u2s(methodname) * 6 +
395 /* allocate memory */
397 name = DMNEW(char, namelen);
399 /* generate name of native functions */
401 strcpy(name, "Java_");
402 pos = strlen("Java_");
404 utf_ptr = classname->text;
405 utf_endptr = UTF_END(classname);
407 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
409 pos = native_insert_char(name, pos, c);
412 /* seperator between class and method */
416 utf_ptr = methodname->text;
417 utf_endptr = UTF_END(methodname);
419 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
421 pos = native_insert_char(name, pos, c);
428 /* check for an buffer overflow */
430 assert(pos <= namelen);
432 /* make a utf-string */
434 u = utf_new_char(name);
438 dump_release(dumpsize);
444 /* native_method_register ******************************************************
446 Register a native method in the native method table.
448 *******************************************************************************/
450 void native_method_register(utf *classname, JNINativeMethod *methods, s4 count)
452 native_methods_node_t *nmn;
457 /* insert all methods passed */
459 for (i = 0; i < count; i++) {
460 if (opt_verbosejni) {
461 printf("[Registering JNI native method ");
462 utf_display_printable_ascii_classname(classname);
463 printf(".%s]\n", methods[i].name);
466 /* generate the utf8 names */
468 name = utf_new_char(methods[i].name);
469 descriptor = utf_new_char(methods[i].signature);
471 /* allocate a new tree node */
473 nmn = NEW(native_methods_node_t);
475 nmn->classname = classname;
477 nmn->descriptor = descriptor;
478 nmn->function = (functionptr) (ptrint) methods[i].fnPtr;
480 /* insert the method into the tree */
482 avl_insert(tree_native_methods, nmn);
487 /* native_method_find **********************************************************
489 Find a native method in the native method table.
491 *******************************************************************************/
493 static functionptr native_method_find(methodinfo *m)
495 native_methods_node_t tmpnmn;
496 native_methods_node_t *nmn;
498 /* fill the temporary structure used for searching the tree */
500 tmpnmn.classname = m->class->name;
501 tmpnmn.name = m->name;
502 tmpnmn.descriptor = m->descriptor;
504 /* find the entry in the AVL-tree */
506 nmn = avl_find(tree_native_methods, &tmpnmn);
511 return nmn->function;
515 /* native_library_open *********************************************************
517 Open a native library with the given utf8 name.
519 *******************************************************************************/
521 #if !defined(WITH_STATIC_CLASSPATH)
522 lt_dlhandle native_library_open(utf *filename)
526 /* try to open the library */
528 handle = lt_dlopen(filename->text);
530 if (handle == NULL) {
533 log_print("native_library_load: lt_dlopen failed: ");
534 log_print(lt_dlerror());
546 /* native_library_add **********************************************************
548 Adds an entry to the native library hashtable.
550 *******************************************************************************/
552 #if !defined(WITH_STATIC_CLASSPATH)
553 void native_library_add(utf *filename, java_objectheader *loader,
556 hashtable_classloader_entry *cle;
557 hashtable_library_loader_entry *le;
558 hashtable_library_name_entry *ne; /* library name */
559 u4 key; /* hashkey */
560 u4 slot; /* slot in hashtable */
562 LOCK_MONITOR_ENTER(hashtable_library->header);
564 /* insert loader into the classloader hashtable */
566 cle = loader_hashtable_classloader_add(loader);
568 /* normally addresses are aligned to 4, 8 or 16 bytes */
570 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
571 slot = key & (hashtable_library->size - 1);
572 le = hashtable_library->ptr[slot];
574 /* search external hash chain for the entry */
580 le = le->hashlink; /* next element in external chain */
583 /* no loader found? create a new entry */
586 le = NEW(hashtable_library_loader_entry);
591 /* insert entry into hashtable */
594 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
595 hashtable_library->ptr[slot] = le;
597 /* update number of hashtable-entries */
599 hashtable_library->entries++;
603 /* search for library name */
608 if (ne->name == filename) {
609 LOCK_MONITOR_EXIT(hashtable_library->header);
614 ne = ne->hashlink; /* next element in external chain */
617 /* not found? add the library name to the classloader */
619 ne = NEW(hashtable_library_name_entry);
624 /* insert entry into external chain */
626 ne->hashlink = le->namelink;
629 LOCK_MONITOR_EXIT(hashtable_library->header);
631 #endif /* !defined(WITH_STATIC_CLASSPATH) */
634 /* native_library_find *********************************************************
636 Find an entry in the native library hashtable.
638 *******************************************************************************/
640 #if !defined(WITH_STATIC_CLASSPATH)
641 hashtable_library_name_entry *native_library_find(utf *filename,
642 java_objectheader *loader)
644 hashtable_classloader_entry *cle;
645 hashtable_library_loader_entry *le;
646 hashtable_library_name_entry *ne; /* library name */
647 u4 key; /* hashkey */
648 u4 slot; /* slot in hashtable */
650 /* search loader in the classloader hashtable */
652 cle = loader_hashtable_classloader_find(loader);
657 /* normally addresses are aligned to 4, 8 or 16 bytes */
659 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
660 slot = key & (hashtable_library->size - 1);
661 le = hashtable_library->ptr[slot];
663 /* search external hash chain for the entry */
669 le = le->hashlink; /* next element in external chain */
672 /* no loader found? return NULL */
677 /* search for library name */
682 if (ne->name == filename)
685 ne = ne->hashlink; /* next element in external chain */
688 /* return entry, if no entry was found, ne is NULL */
692 #endif /* !defined(WITH_STATIC_CLASSPATH) */
695 /* native_findfunction *********************************************************
697 Looks up a method (must have the same class name, method name,
698 descriptor and 'static'ness) and returns a function pointer to it.
699 Returns: function pointer or NULL (if there is no such method)
701 Remark: For faster operation, the names/descriptors are converted
702 from C strings to Unicode the first time this function is called.
704 *******************************************************************************/
706 #if defined(WITH_STATIC_CLASSPATH)
707 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
710 /* entry of table for fast string comparison */
711 struct nativecompref *n;
714 isstatic = isstatic ? true : false;
716 if (!nativecompdone) {
717 for (i = 0; i < NATIVETABLESIZE; i++) {
718 nativecomptable[i].classname =
719 utf_new_char(nativetable[i].classname);
721 nativecomptable[i].methodname =
722 utf_new_char(nativetable[i].methodname);
724 nativecomptable[i].descriptor =
725 utf_new_char(nativetable[i].descriptor);
727 nativecomptable[i].isstatic = nativetable[i].isstatic;
728 nativecomptable[i].func = nativetable[i].func;
731 nativecompdone = true;
734 for (i = 0; i < NATIVETABLESIZE; i++) {
735 n = &(nativecomptable[i]);
737 if (cname == n->classname && mname == n->methodname &&
738 desc == n->descriptor && isstatic == n->isstatic)
742 /* no function was found, throw exception */
745 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
750 #endif /* defined(WITH_STATIC_CLASSPATH) */
753 /* native_resolve_function *****************************************************
755 Resolves a native function, maybe from a dynamic library.
757 *******************************************************************************/
759 functionptr native_resolve_function(methodinfo *m)
764 hashtable_library_loader_entry *le;
765 hashtable_library_name_entry *ne;
766 u4 key; /* hashkey */
767 u4 slot; /* slot in hashtable */
771 if (opt_verbosejni) {
772 printf("[Dynamic-linking native method ");
773 utf_display_printable_ascii_classname(m->class->name);
775 utf_display_printable_ascii(m->name);
779 /* generate method symbol string */
781 name = native_method_symbol(m->class->name, m->name);
783 /* generate overloaded function (having the types in it's name) */
785 newname = native_make_overloaded_function(name, m->descriptor);
787 /* check the library hash entries of the classloader of the
792 /* normally addresses are aligned to 4, 8 or 16 bytes */
794 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
795 slot = key & (hashtable_library->size - 1);
796 le = hashtable_library->ptr[slot];
798 /* iterate through loaders in this hash slot */
800 while ((le != NULL) && (f == NULL)) {
801 /* iterate through names in this loader */
805 while ((ne != NULL) && (f == NULL)) {
806 f = (functionptr) (ptrint) lt_dlsym(ne->handle, name->text);
809 f = (functionptr) (ptrint) lt_dlsym(ne->handle, newname->text);
821 /* If not found, try to find the native function symbol in the
825 f = native_method_find(m);
829 printf("internal ]\n");
832 #if defined(ENABLE_JVMTI)
833 /* fire Native Method Bind event */
834 if (jvmti) jvmti_NativeMethodBind(m, f, &f);
837 /* no symbol found? throw exception */
841 printf("failed ]\n");
843 exceptions_throw_unsatisfiedlinkerror(m->name);
848 #endif /* !defined(WITH_STATIC_CLASSPATH) */
851 /* native_new_and_init *********************************************************
853 Creates a new object on the heap and calls the initializer.
854 Returns the object pointer or NULL if memory is exhausted.
856 *******************************************************************************/
858 java_objectheader *native_new_and_init(classinfo *c)
861 java_objectheader *o;
864 vm_abort("native_new_and_init: c == NULL");
873 /* try to find the initializer */
875 m = class_findmethod(c, utf_init, utf_void__void);
877 /* ATTENTION: returning the object here is ok, since the class may
878 not have an initializer */
883 /* call initializer */
885 (void) vm_call_method(m, o);
891 java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
894 java_objectheader *o;
897 vm_abort("native_new_and_init_string: c == NULL");
906 /* find initializer */
908 m = class_resolveclassmethod(c,
910 utf_java_lang_String__void,
914 /* initializer not found */
919 /* call initializer */
921 (void) vm_call_method(m, o, s);
927 java_objectheader *native_new_and_init_throwable(classinfo *c, java_objectheader *t)
929 java_objectheader *o;
933 vm_abort("native_new_and_init_throwable: c == NULL");
942 /* find initializer */
944 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
946 /* initializer not found */
951 /* call initializer */
953 (void) vm_call_method(m, o, t);
959 java_objectheader *native_new_and_init_exception(classinfo *c, java_objectheader *e)
961 java_objectheader *o;
965 vm_abort("native_new_and_init_exception: c == NULL");
974 /* find initializer */
976 m = class_findmethod(c, utf_init, utf_java_lang_Exception__V);
978 /* initializer not found */
983 /* call initializer */
985 (void) vm_call_method(m, o, e);
992 * These are local overrides for various environment variables in Emacs.
993 * Please do not remove this and leave it at the end of the file, where
994 * Emacs will automagically detect them.
995 * ---------------------------------------------------------------------
998 * indent-tabs-mode: t