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
33 #if defined(ENABLE_LTDL) && defined(HAVE_LTDL_H)
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;
96 #if defined(ENABLE_LTDL)
97 static hashtable *hashtable_library;
101 /* prototypes *****************************************************************/
103 static s4 native_tree_native_methods_comparator(const void *treenode, const void *node);
106 /* native_init *****************************************************************
108 Initializes the native subsystem.
110 *******************************************************************************/
112 bool native_init(void)
114 #if defined(ENABLE_LTDL)
115 /* initialize libltdl */
118 vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
120 /* initialize library hashtable, 10 entries should be enough */
122 hashtable_library = NEW(hashtable);
124 hashtable_create(hashtable_library, 10);
127 /* initialize the native methods table */
129 tree_native_methods = avl_create(&native_tree_native_methods_comparator);
131 /* everything's ok */
137 /* native_tree_native_methods_comparator ***************************************
139 Comparison function for AVL tree of native methods.
142 treenode....node in the tree
143 node........node to compare with tree-node
148 *******************************************************************************/
150 static s4 native_tree_native_methods_comparator(const void *treenode, const void *node)
152 const native_methods_node_t *treenmn;
153 const native_methods_node_t *nmn;
158 /* these are for walking the tree */
160 if (treenmn->classname < nmn->classname)
162 else if (treenmn->classname > nmn->classname)
165 if (treenmn->name < nmn->name)
167 else if (treenmn->name > nmn->name)
170 if (treenmn->descriptor < nmn->descriptor)
172 else if (treenmn->descriptor > nmn->descriptor)
175 /* all pointers are equal, we have found the entry */
181 /* native_make_overloaded_function *********************************************
185 *******************************************************************************/
187 #if !defined(WITH_STATIC_CLASSPATH)
188 static utf *native_make_overloaded_function(utf *name, utf *descriptor)
200 dumpsize = dump_size();
202 utf_ptr = descriptor->text;
203 namelen = strlen(name->text) + strlen("__") + strlen("0");
205 /* calculate additional length */
207 while ((c = utf_nextu2(&utf_ptr)) != ')') {
224 while (utf_nextu2(&utf_ptr) != ';')
235 /* reallocate memory */
237 i = strlen(name->text);
239 newname = DMNEW(char, namelen);
240 MCOPY(newname, name->text, char, i);
242 utf_ptr = descriptor->text;
247 while ((c = utf_nextu2(&utf_ptr)) != ')') {
265 while ((c = utf_nextu2(&utf_ptr)) != ';')
266 if (((c >= 'a') && (c <= 'z')) ||
267 ((c >= 'A') && (c <= 'Z')) ||
268 ((c >= '0') && (c <= '9')))
286 /* make a utf-string */
288 u = utf_new_char(newname);
292 dump_release(dumpsize);
298 /* native_insert_char **********************************************************
300 Inserts the passed UTF character into the native method name. If
301 necessary it is escaped properly.
303 *******************************************************************************/
305 static s4 native_insert_char(char *name, u4 pos, u2 c)
313 /* replace '/' or '.' with '_' */
318 /* escape sequence for '_' is '_1' */
324 /* escape sequence for ';' is '_2' */
330 /* escape sequence for '[' is '_1' */
339 /* unicode character */
343 for (i = 0; i < 4; ++i) {
345 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
354 /* return the new buffer index */
360 /* native_method_symbol ********************************************************
362 Generate a method-symbol string out of the class name and the
365 *******************************************************************************/
367 static utf *native_method_symbol(utf *classname, utf *methodname)
380 dumpsize = dump_size();
382 /* Calculate length of native function name. We multiply the
383 class and method name length by 6 as this is the maxium
384 escape-sequence that can be generated (unicode). */
388 utf_get_number_of_u2s(classname) * 6 +
390 utf_get_number_of_u2s(methodname) * 6 +
393 /* allocate memory */
395 name = DMNEW(char, namelen);
397 /* generate name of native functions */
399 strcpy(name, "Java_");
400 pos = strlen("Java_");
402 utf_ptr = classname->text;
403 utf_endptr = UTF_END(classname);
405 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
407 pos = native_insert_char(name, pos, c);
410 /* seperator between class and method */
414 utf_ptr = methodname->text;
415 utf_endptr = UTF_END(methodname);
417 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
419 pos = native_insert_char(name, pos, c);
426 /* check for an buffer overflow */
428 assert(pos <= namelen);
430 /* make a utf-string */
432 u = utf_new_char(name);
436 dump_release(dumpsize);
442 /* native_method_register ******************************************************
444 Register a native method in the native method table.
446 *******************************************************************************/
448 void native_method_register(utf *classname, const JNINativeMethod *methods,
451 native_methods_node_t *nmn;
456 /* insert all methods passed */
458 for (i = 0; i < count; i++) {
459 if (opt_verbosejni) {
460 printf("[Registering JNI native method ");
461 utf_display_printable_ascii_classname(classname);
462 printf(".%s]\n", methods[i].name);
465 /* generate the utf8 names */
467 name = utf_new_char(methods[i].name);
468 descriptor = utf_new_char(methods[i].signature);
470 /* allocate a new tree node */
472 nmn = NEW(native_methods_node_t);
474 nmn->classname = classname;
476 nmn->descriptor = descriptor;
477 nmn->function = (functionptr) (ptrint) methods[i].fnPtr;
479 /* insert the method into the tree */
481 avl_insert(tree_native_methods, nmn);
486 /* native_method_find **********************************************************
488 Find a native method in the native method table.
490 *******************************************************************************/
492 static functionptr native_method_find(methodinfo *m)
494 native_methods_node_t tmpnmn;
495 native_methods_node_t *nmn;
497 /* fill the temporary structure used for searching the tree */
499 tmpnmn.classname = m->class->name;
500 tmpnmn.name = m->name;
501 tmpnmn.descriptor = m->descriptor;
503 /* find the entry in the AVL-tree */
505 nmn = avl_find(tree_native_methods, &tmpnmn);
510 return nmn->function;
514 /* native_library_open *********************************************************
516 Open a native library with the given utf8 name.
518 *******************************************************************************/
520 #if defined(ENABLE_LTDL)
521 lt_dlhandle native_library_open(utf *filename)
525 if (opt_verbosejni) {
526 printf("[Loading native library ");
527 utf_display_printable_ascii(filename);
531 /* try to open the library */
533 handle = lt_dlopen(filename->text);
535 if (handle == NULL) {
538 log_print("native_library_load: lt_dlopen failed: ");
539 log_print(lt_dlerror());
551 /* native_library_add **********************************************************
553 Adds an entry to the native library hashtable.
555 *******************************************************************************/
557 #if defined(ENABLE_LTDL)
558 void native_library_add(utf *filename, classloader *loader, lt_dlhandle handle)
560 hashtable_library_loader_entry *le;
561 hashtable_library_name_entry *ne; /* library name */
562 u4 key; /* hashkey */
563 u4 slot; /* slot in hashtable */
565 LOCK_MONITOR_ENTER(hashtable_library->header);
567 /* normally addresses are aligned to 4, 8 or 16 bytes */
569 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
570 slot = key & (hashtable_library->size - 1);
571 le = hashtable_library->ptr[slot];
573 /* search external hash chain for the entry */
576 if (le->loader == loader)
579 le = le->hashlink; /* next element in external chain */
582 /* no loader found? create a new entry */
585 le = NEW(hashtable_library_loader_entry);
590 /* insert entry into hashtable */
593 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
594 hashtable_library->ptr[slot] = le;
596 /* update number of hashtable-entries */
598 hashtable_library->entries++;
602 /* search for library name */
607 if (ne->name == filename) {
608 LOCK_MONITOR_EXIT(hashtable_library->header);
613 ne = ne->hashlink; /* next element in external chain */
616 /* not found? add the library name to the classloader */
618 ne = NEW(hashtable_library_name_entry);
623 /* insert entry into external chain */
625 ne->hashlink = le->namelink;
628 LOCK_MONITOR_EXIT(hashtable_library->header);
633 /* native_library_find *********************************************************
635 Find an entry in the native library hashtable.
637 *******************************************************************************/
639 #if defined(ENABLE_LTDL)
640 hashtable_library_name_entry *native_library_find(utf *filename,
643 hashtable_library_loader_entry *le;
644 hashtable_library_name_entry *ne; /* library name */
645 u4 key; /* hashkey */
646 u4 slot; /* slot in hashtable */
648 /* normally addresses are aligned to 4, 8 or 16 bytes */
650 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
651 slot = key & (hashtable_library->size - 1);
652 le = hashtable_library->ptr[slot];
654 /* search external hash chain for the entry */
657 if (le->loader == loader)
660 le = le->hashlink; /* next element in external chain */
663 /* no loader found? return NULL */
668 /* search for library name */
673 if (ne->name == filename)
676 ne = ne->hashlink; /* next element in external chain */
679 /* return entry, if no entry was found, ne is NULL */
683 #endif /* !defined(WITH_STATIC_CLASSPATH) */
686 /* native_findfunction *********************************************************
688 Looks up a method (must have the same class name, method name,
689 descriptor and 'static'ness) and returns a function pointer to it.
690 Returns: function pointer or NULL (if there is no such method)
692 Remark: For faster operation, the names/descriptors are converted
693 from C strings to Unicode the first time this function is called.
695 *******************************************************************************/
697 #if defined(WITH_STATIC_CLASSPATH)
698 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
701 /* entry of table for fast string comparison */
702 struct nativecompref *n;
705 isstatic = isstatic ? true : false;
707 if (!nativecompdone) {
708 for (i = 0; i < NATIVETABLESIZE; i++) {
709 nativecomptable[i].classname =
710 utf_new_char(nativetable[i].classname);
712 nativecomptable[i].methodname =
713 utf_new_char(nativetable[i].methodname);
715 nativecomptable[i].descriptor =
716 utf_new_char(nativetable[i].descriptor);
718 nativecomptable[i].isstatic = nativetable[i].isstatic;
719 nativecomptable[i].func = nativetable[i].func;
722 nativecompdone = true;
725 for (i = 0; i < NATIVETABLESIZE; i++) {
726 n = &(nativecomptable[i]);
728 if (cname == n->classname && mname == n->methodname &&
729 desc == n->descriptor && isstatic == n->isstatic)
733 /* no function was found, throw exception */
735 exceptions_throw_unsatisfiedlinkerror(mname);
739 #endif /* defined(WITH_STATIC_CLASSPATH) */
742 /* native_resolve_function *****************************************************
744 Resolves a native function, maybe from a dynamic library.
746 *******************************************************************************/
748 functionptr native_resolve_function(methodinfo *m)
754 #if defined(ENABLE_LTDL)
755 hashtable_library_loader_entry *le;
756 hashtable_library_name_entry *ne;
757 u4 key; /* hashkey */
758 u4 slot; /* slot in hashtable */
760 #if defined(WITH_CLASSPATH_SUN)
761 methodinfo *method_findNative;
765 cl = m->class->classloader;
769 if (opt_verbosejni) {
770 printf("[Dynamic-linking native method ");
771 utf_display_printable_ascii_classname(m->class->name);
773 utf_display_printable_ascii(m->name);
777 /* generate method symbol string */
779 name = native_method_symbol(m->class->name, m->name);
781 /* generate overloaded function (having the types in it's name) */
783 newname = native_make_overloaded_function(name, m->descriptor);
785 /* check the library hash entries of the classloader of the
790 #if defined(ENABLE_LTDL)
791 /* normally addresses are aligned to 4, 8 or 16 bytes */
793 key = ((u4) (ptrint) cl) >> 4; /* align to 16-byte */
794 slot = key & (hashtable_library->size - 1);
795 le = hashtable_library->ptr[slot];
797 /* iterate through loaders in this hash slot */
799 while ((le != NULL) && (f == NULL)) {
800 /* iterate through names in this loader */
804 while ((ne != NULL) && (f == NULL)) {
805 f = (functionptr) (ptrint) lt_dlsym(ne->handle, name->text);
808 f = (functionptr) (ptrint) lt_dlsym(ne->handle, newname->text);
816 # if defined(WITH_CLASSPATH_SUN)
818 /* We can resolve the function directly from
819 java.lang.ClassLoader as it's a static function. */
820 /* XXX should be done in native_init */
823 class_resolveclassmethod(class_java_lang_ClassLoader,
825 utf_java_lang_ClassLoader_java_lang_String__J,
826 class_java_lang_ClassLoader,
829 if (method_findNative == NULL)
832 /* try the normal name */
834 s = javastring_new(name);
836 f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
839 /* if not found, try the mangled name */
842 s = javastring_new(newname);
844 f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
855 /* If not found, try to find the native function symbol in the
859 f = native_method_find(m);
863 printf("internal ]\n");
866 #if defined(ENABLE_JVMTI)
867 /* fire Native Method Bind event */
868 if (jvmti) jvmti_NativeMethodBind(m, f, &f);
871 /* no symbol found? throw exception */
875 printf("failed ]\n");
877 exceptions_throw_unsatisfiedlinkerror(m->name);
882 #endif /* !defined(WITH_STATIC_CLASSPATH) */
885 /* native_new_and_init *********************************************************
887 Creates a new object on the heap and calls the initializer.
888 Returns the object pointer or NULL if memory is exhausted.
890 *******************************************************************************/
892 java_handle_t *native_new_and_init(classinfo *c)
898 vm_abort("native_new_and_init: c == NULL");
907 /* try to find the initializer */
909 m = class_findmethod(c, utf_init, utf_void__void);
911 /* ATTENTION: returning the object here is ok, since the class may
912 not have an initializer */
917 /* call initializer */
919 (void) vm_call_method(m, o);
925 java_handle_t *native_new_and_init_string(classinfo *c, java_handle_t *s)
931 vm_abort("native_new_and_init_string: c == NULL");
940 /* find initializer */
942 m = class_findmethod(c, utf_init, utf_java_lang_String__void);
944 /* initializer not found */
949 /* call initializer */
951 (void) vm_call_method(m, o, s);
958 * These are local overrides for various environment variables in Emacs.
959 * Please do not remove this and leave it at the end of the file, where
960 * Emacs will automagically detect them.
961 * ---------------------------------------------------------------------
964 * indent-tabs-mode: t